fd.c revision 1.37.6.3 1 1.37.6.3 nathanw /* $NetBSD: fd.c,v 1.37.6.3 2002/09/17 21:13:42 nathanw Exp $ */
2 1.37.6.2 nathanw
3 1.37.6.2 nathanw /*
4 1.37.6.2 nathanw * Copyright (c) 1995 Leo Weppelman.
5 1.37.6.2 nathanw * All rights reserved.
6 1.37.6.2 nathanw *
7 1.37.6.2 nathanw * Redistribution and use in source and binary forms, with or without
8 1.37.6.2 nathanw * modification, are permitted provided that the following conditions
9 1.37.6.2 nathanw * are met:
10 1.37.6.2 nathanw * 1. Redistributions of source code must retain the above copyright
11 1.37.6.2 nathanw * notice, this list of conditions and the following disclaimer.
12 1.37.6.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
13 1.37.6.2 nathanw * notice, this list of conditions and the following disclaimer in the
14 1.37.6.2 nathanw * documentation and/or other materials provided with the distribution.
15 1.37.6.2 nathanw * 3. All advertising materials mentioning features or use of this software
16 1.37.6.2 nathanw * must display the following acknowledgement:
17 1.37.6.2 nathanw * This product includes software developed by Leo Weppelman.
18 1.37.6.2 nathanw * 4. The name of the author may not be used to endorse or promote products
19 1.37.6.2 nathanw * derived from this software without specific prior written permission
20 1.37.6.2 nathanw *
21 1.37.6.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.37.6.2 nathanw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.37.6.2 nathanw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.37.6.2 nathanw * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.37.6.2 nathanw * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.37.6.2 nathanw * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.37.6.2 nathanw * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.37.6.2 nathanw * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.37.6.2 nathanw * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.37.6.2 nathanw * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.37.6.2 nathanw */
32 1.37.6.2 nathanw
33 1.37.6.2 nathanw /*
34 1.37.6.2 nathanw * This file contains a driver for the Floppy Disk Controller (FDC)
35 1.37.6.2 nathanw * on the Atari TT. It uses the WD 1772 chip, modified for steprates.
36 1.37.6.2 nathanw *
37 1.37.6.2 nathanw * The ST floppy disk controller shares the access to the DMA circuitry
38 1.37.6.2 nathanw * with other devices. For this reason the floppy disk controller makes
39 1.37.6.2 nathanw * use of some special DMA accessing code.
40 1.37.6.2 nathanw *
41 1.37.6.2 nathanw * Interrupts from the FDC are in fact DMA interrupts which get their
42 1.37.6.2 nathanw * first level handling in 'dma.c' . If the floppy driver is currently
43 1.37.6.2 nathanw * using DMA the interrupt is signalled to 'fdcint'.
44 1.37.6.2 nathanw *
45 1.37.6.2 nathanw * TODO:
46 1.37.6.2 nathanw * - Test it with 2 drives (I don't have them)
47 1.37.6.2 nathanw * - Test it with an HD-drive (Don't have that either)
48 1.37.6.2 nathanw * - Finish ioctl's
49 1.37.6.2 nathanw */
50 1.37.6.2 nathanw
51 1.37.6.2 nathanw #include <sys/param.h>
52 1.37.6.2 nathanw #include <sys/systm.h>
53 1.37.6.2 nathanw #include <sys/callout.h>
54 1.37.6.2 nathanw #include <sys/kernel.h>
55 1.37.6.2 nathanw #include <sys/malloc.h>
56 1.37.6.2 nathanw #include <sys/buf.h>
57 1.37.6.2 nathanw #include <sys/proc.h>
58 1.37.6.2 nathanw #include <sys/device.h>
59 1.37.6.2 nathanw #include <sys/ioctl.h>
60 1.37.6.2 nathanw #include <sys/fcntl.h>
61 1.37.6.2 nathanw #include <sys/conf.h>
62 1.37.6.2 nathanw #include <sys/disklabel.h>
63 1.37.6.2 nathanw #include <sys/disk.h>
64 1.37.6.2 nathanw #include <sys/dkbad.h>
65 1.37.6.2 nathanw #include <atari/atari/device.h>
66 1.37.6.2 nathanw #include <atari/atari/stalloc.h>
67 1.37.6.2 nathanw #include <machine/disklabel.h>
68 1.37.6.2 nathanw #include <machine/iomap.h>
69 1.37.6.2 nathanw #include <machine/mfp.h>
70 1.37.6.2 nathanw #include <machine/dma.h>
71 1.37.6.2 nathanw #include <machine/video.h>
72 1.37.6.2 nathanw #include <machine/cpu.h>
73 1.37.6.2 nathanw #include <atari/dev/ym2149reg.h>
74 1.37.6.2 nathanw #include <atari/dev/fdreg.h>
75 1.37.6.2 nathanw
76 1.37.6.2 nathanw /*
77 1.37.6.2 nathanw * Be verbose for debugging
78 1.37.6.2 nathanw */
79 1.37.6.2 nathanw /*#define FLP_DEBUG 1 */
80 1.37.6.2 nathanw
81 1.37.6.2 nathanw #define FDC_MAX_DMA_AD 0x1000000 /* No DMA possible beyond */
82 1.37.6.2 nathanw
83 1.37.6.2 nathanw /* Parameters for the disk drive. */
84 1.37.6.2 nathanw #define SECTOR_SIZE 512 /* physical sector size in bytes */
85 1.37.6.2 nathanw #define NR_DRIVES 2 /* maximum number of drives */
86 1.37.6.2 nathanw #define NR_TYPES 3 /* number of diskette/drive combinations*/
87 1.37.6.2 nathanw #define MAX_ERRORS 10 /* how often to try rd/wt before quitting*/
88 1.37.6.2 nathanw #define STEP_DELAY 6000 /* 6ms (6000us) delay after stepping */
89 1.37.6.2 nathanw
90 1.37.6.2 nathanw
91 1.37.6.2 nathanw #define INV_TRK 32000 /* Should fit in unsigned short */
92 1.37.6.2 nathanw #define INV_PART NR_TYPES
93 1.37.6.2 nathanw
94 1.37.6.2 nathanw /*
95 1.37.6.2 nathanw * Driver states
96 1.37.6.2 nathanw */
97 1.37.6.2 nathanw #define FLP_IDLE 0x00 /* floppy is idle */
98 1.37.6.2 nathanw #define FLP_MON 0x01 /* idle with motor on */
99 1.37.6.2 nathanw #define FLP_STAT 0x02 /* determine floppy status */
100 1.37.6.2 nathanw #define FLP_XFER 0x04 /* read/write data from floppy */
101 1.37.6.2 nathanw
102 1.37.6.2 nathanw /*
103 1.37.6.2 nathanw * Timer delay's
104 1.37.6.2 nathanw */
105 1.37.6.2 nathanw #define FLP_MONDELAY (3 * hz) /* motor-on delay */
106 1.37.6.2 nathanw #define FLP_XFERDELAY (2 * hz) /* timeout on transfer */
107 1.37.6.2 nathanw
108 1.37.6.2 nathanw /*
109 1.37.6.2 nathanw * The density codes
110 1.37.6.2 nathanw */
111 1.37.6.2 nathanw #define FLP_DD 0 /* Double density */
112 1.37.6.2 nathanw #define FLP_HD 1 /* High density */
113 1.37.6.2 nathanw
114 1.37.6.2 nathanw
115 1.37.6.2 nathanw #define b_block b_resid /* FIXME: this is not the place */
116 1.37.6.2 nathanw
117 1.37.6.2 nathanw /*
118 1.37.6.2 nathanw * Global data for all physical floppy devices
119 1.37.6.2 nathanw */
120 1.37.6.2 nathanw static short selected = 0; /* drive/head currently selected*/
121 1.37.6.2 nathanw static short motoron = 0; /* motor is spinning */
122 1.37.6.2 nathanw static short nopens = 0; /* Number of opens executed */
123 1.37.6.2 nathanw
124 1.37.6.2 nathanw static short fd_state = FLP_IDLE; /* Current driver state */
125 1.37.6.2 nathanw static int lock_stat= 0; /* dma locking status */
126 1.37.6.2 nathanw static short fd_cmd = 0; /* command being executed */
127 1.37.6.2 nathanw static char *fd_error= NULL; /* error from fd_xfer_ok() */
128 1.37.6.2 nathanw
129 1.37.6.2 nathanw /*
130 1.37.6.2 nathanw * Private per device data
131 1.37.6.2 nathanw */
132 1.37.6.2 nathanw struct fd_softc {
133 1.37.6.2 nathanw struct device sc_dv; /* generic device info */
134 1.37.6.2 nathanw struct disk dkdev; /* generic disk info */
135 1.37.6.2 nathanw struct bufq_state bufq; /* queue of buf's */
136 1.37.6.2 nathanw struct callout sc_motor_ch;
137 1.37.6.2 nathanw int unit; /* unit for atari controlling hw*/
138 1.37.6.2 nathanw int nheads; /* number of heads in use */
139 1.37.6.2 nathanw int nsectors; /* number of sectors/track */
140 1.37.6.2 nathanw int density; /* density code */
141 1.37.6.2 nathanw int nblocks; /* number of blocks on disk */
142 1.37.6.2 nathanw int curtrk; /* track head positioned on */
143 1.37.6.2 nathanw short flags; /* misc flags */
144 1.37.6.2 nathanw short part; /* Current open partition */
145 1.37.6.2 nathanw int sector; /* logical sector for I/O */
146 1.37.6.2 nathanw caddr_t io_data; /* KVA for data transfer */
147 1.37.6.2 nathanw int io_bytes; /* bytes left for I/O */
148 1.37.6.2 nathanw int io_dir; /* B_READ/B_WRITE */
149 1.37.6.2 nathanw int errcnt; /* current error count */
150 1.37.6.2 nathanw u_char *bounceb; /* Bounce buffer */
151 1.37.6.2 nathanw
152 1.37.6.2 nathanw };
153 1.37.6.2 nathanw
154 1.37.6.2 nathanw /*
155 1.37.6.2 nathanw * Flags in fd_softc:
156 1.37.6.2 nathanw */
157 1.37.6.2 nathanw #define FLPF_NOTRESP 0x001 /* Unit not responding */
158 1.37.6.2 nathanw #define FLPF_ISOPEN 0x002 /* Unit is open */
159 1.37.6.2 nathanw #define FLPF_SPARE 0x004 /* Not used */
160 1.37.6.2 nathanw #define FLPF_HAVELAB 0x008 /* We have a valid label */
161 1.37.6.2 nathanw #define FLPF_BOUNCE 0x010 /* Now using the bounce buffer */
162 1.37.6.2 nathanw #define FLPF_WRTPROT 0x020 /* Unit is write-protected */
163 1.37.6.2 nathanw #define FLPF_EMPTY 0x040 /* Unit is empty */
164 1.37.6.2 nathanw #define FLPF_INOPEN 0x080 /* Currently being opened */
165 1.37.6.2 nathanw #define FLPF_GETSTAT 0x100 /* Getting unit status */
166 1.37.6.2 nathanw
167 1.37.6.2 nathanw struct fd_types {
168 1.37.6.2 nathanw int nheads; /* Heads in use */
169 1.37.6.2 nathanw int nsectors; /* sectors per track */
170 1.37.6.2 nathanw int nblocks; /* number of blocks */
171 1.37.6.2 nathanw int density; /* density code */
172 1.37.6.2 nathanw const char *descr; /* type description */
173 1.37.6.2 nathanw } fdtypes[NR_TYPES] = {
174 1.37.6.2 nathanw { 1, 9, 720 , FLP_DD , "360KB" }, /* 360 Kb */
175 1.37.6.2 nathanw { 2, 9, 1440 , FLP_DD , "720KB" }, /* 720 Kb */
176 1.37.6.2 nathanw { 2, 18, 2880 , FLP_HD , "1.44MB" }, /* 1.44 Mb */
177 1.37.6.2 nathanw };
178 1.37.6.2 nathanw
179 1.37.6.2 nathanw #define FLP_TYPE_360 0 /* XXX: Please keep these in */
180 1.37.6.2 nathanw #define FLP_TYPE_720 1 /* sync with the numbering in */
181 1.37.6.2 nathanw #define FLP_TYPE_144 2 /* 'fdtypes' right above! */
182 1.37.6.2 nathanw
183 1.37.6.2 nathanw /*
184 1.37.6.2 nathanw * This is set only once at attach time. The value is determined by reading
185 1.37.6.2 nathanw * the configuration switches and is one of the FLP_TYPE_*'s.
186 1.37.6.2 nathanw * This is simular to the way Atari handles the _FLP cookie.
187 1.37.6.2 nathanw */
188 1.37.6.2 nathanw static short def_type = 0; /* Reflects config-switches */
189 1.37.6.2 nathanw
190 1.37.6.2 nathanw #define FLP_DEFTYPE 1 /* 720Kb, reasonable default */
191 1.37.6.2 nathanw #define FLP_TYPE(dev) ( DISKPART(dev) == 0 ? def_type : DISKPART(dev) - 1 )
192 1.37.6.2 nathanw
193 1.37.6.2 nathanw typedef void (*FPV) __P((void *));
194 1.37.6.2 nathanw
195 1.37.6.2 nathanw dev_type_open(fdopen);
196 1.37.6.2 nathanw dev_type_close(fdclose);
197 1.37.6.2 nathanw dev_type_read(fdread);
198 1.37.6.2 nathanw dev_type_write(fdwrite);
199 1.37.6.2 nathanw dev_type_ioctl(fdioctl);
200 1.37.6.3 nathanw dev_type_strategy(fdstrategy);
201 1.37.6.2 nathanw
202 1.37.6.2 nathanw /*
203 1.37.6.2 nathanw * Private drive functions....
204 1.37.6.2 nathanw */
205 1.37.6.2 nathanw static void fdstart __P((struct fd_softc *));
206 1.37.6.2 nathanw static void fddone __P((struct fd_softc *));
207 1.37.6.2 nathanw static void fdstatus __P((struct fd_softc *));
208 1.37.6.2 nathanw static void fd_xfer __P((struct fd_softc *));
209 1.37.6.2 nathanw static void fdcint __P((struct fd_softc *));
210 1.37.6.2 nathanw static int fd_xfer_ok __P((struct fd_softc *));
211 1.37.6.2 nathanw static void fdmotoroff __P((struct fd_softc *));
212 1.37.6.2 nathanw static void fdminphys __P((struct buf *));
213 1.37.6.2 nathanw static void fdtestdrv __P((struct fd_softc *));
214 1.37.6.2 nathanw static void fdgetdefaultlabel __P((struct fd_softc *, struct disklabel *,
215 1.37.6.2 nathanw int));
216 1.37.6.2 nathanw static int fdgetdisklabel __P((struct fd_softc *, dev_t));
217 1.37.6.2 nathanw static int fdselect __P((int, int, int));
218 1.37.6.2 nathanw static void fddeselect __P((void));
219 1.37.6.2 nathanw static void fdmoff __P((struct fd_softc *));
220 1.37.6.2 nathanw u_char read_fdreg __P((u_short));
221 1.37.6.2 nathanw void write_fdreg __P((u_short, u_short));
222 1.37.6.2 nathanw u_char read_dmastat __P((void));
223 1.37.6.2 nathanw
224 1.37.6.2 nathanw extern __inline__ u_char read_fdreg(u_short regno)
225 1.37.6.2 nathanw {
226 1.37.6.2 nathanw DMA->dma_mode = regno;
227 1.37.6.2 nathanw return(DMA->dma_data);
228 1.37.6.2 nathanw }
229 1.37.6.2 nathanw
230 1.37.6.2 nathanw extern __inline__ void write_fdreg(u_short regno, u_short val)
231 1.37.6.2 nathanw {
232 1.37.6.2 nathanw DMA->dma_mode = regno;
233 1.37.6.2 nathanw DMA->dma_data = val;
234 1.37.6.2 nathanw }
235 1.37.6.2 nathanw
236 1.37.6.2 nathanw extern __inline__ u_char read_dmastat(void)
237 1.37.6.2 nathanw {
238 1.37.6.2 nathanw DMA->dma_mode = FDC_CS | DMA_SCREG;
239 1.37.6.2 nathanw return(DMA->dma_stat);
240 1.37.6.2 nathanw }
241 1.37.6.2 nathanw
242 1.37.6.2 nathanw /*
243 1.37.6.2 nathanw * Config switch stuff. Used only for the floppy type for now. That's
244 1.37.6.2 nathanw * why it's here...
245 1.37.6.2 nathanw * XXX: If needed in more places, it should be moved to it's own include file.
246 1.37.6.2 nathanw * Note: This location _must_ be read as an u_short. Failure to do so
247 1.37.6.2 nathanw * will return garbage!
248 1.37.6.2 nathanw */
249 1.37.6.2 nathanw static u_short rd_cfg_switch __P((void));
250 1.37.6.2 nathanw static u_short rd_cfg_switch(void)
251 1.37.6.2 nathanw {
252 1.37.6.2 nathanw return(*((u_short*)AD_CFG_SWITCH));
253 1.37.6.2 nathanw }
254 1.37.6.2 nathanw
255 1.37.6.2 nathanw /*
256 1.37.6.2 nathanw * Switch definitions.
257 1.37.6.2 nathanw * Note: ON reads as a zero bit!
258 1.37.6.2 nathanw */
259 1.37.6.2 nathanw #define CFG_SWITCH_NOHD 0x4000
260 1.37.6.2 nathanw
261 1.37.6.2 nathanw /*
262 1.37.6.2 nathanw * Autoconfig stuff....
263 1.37.6.2 nathanw */
264 1.37.6.2 nathanw extern struct cfdriver fd_cd;
265 1.37.6.2 nathanw
266 1.37.6.2 nathanw static int fdcmatch __P((struct device *, struct cfdata *, void *));
267 1.37.6.2 nathanw static int fdcprint __P((void *, const char *));
268 1.37.6.2 nathanw static void fdcattach __P((struct device *, struct device *, void *));
269 1.37.6.2 nathanw
270 1.37.6.2 nathanw struct cfattach fdc_ca = {
271 1.37.6.2 nathanw sizeof(struct device), fdcmatch, fdcattach
272 1.37.6.2 nathanw };
273 1.37.6.2 nathanw
274 1.37.6.3 nathanw const struct bdevsw fd_bdevsw = {
275 1.37.6.3 nathanw fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
276 1.37.6.3 nathanw };
277 1.37.6.3 nathanw
278 1.37.6.3 nathanw const struct cdevsw fd_cdevsw = {
279 1.37.6.3 nathanw fdopen, fdclose, fdread, fdwrite, fdioctl,
280 1.37.6.3 nathanw nostop, notty, nopoll, nommap, D_DISK
281 1.37.6.3 nathanw };
282 1.37.6.3 nathanw
283 1.37.6.2 nathanw static int
284 1.37.6.2 nathanw fdcmatch(pdp, cfp, auxp)
285 1.37.6.2 nathanw struct device *pdp;
286 1.37.6.2 nathanw struct cfdata *cfp;
287 1.37.6.2 nathanw void *auxp;
288 1.37.6.2 nathanw {
289 1.37.6.2 nathanw static int fdc_matched = 0;
290 1.37.6.2 nathanw
291 1.37.6.2 nathanw /* Match only once */
292 1.37.6.2 nathanw if(strcmp("fdc", auxp) || fdc_matched)
293 1.37.6.2 nathanw return(0);
294 1.37.6.2 nathanw fdc_matched = 1;
295 1.37.6.2 nathanw return(1);
296 1.37.6.2 nathanw }
297 1.37.6.2 nathanw
298 1.37.6.2 nathanw static void
299 1.37.6.2 nathanw fdcattach(pdp, dp, auxp)
300 1.37.6.2 nathanw struct device *pdp, *dp;
301 1.37.6.2 nathanw void *auxp;
302 1.37.6.2 nathanw {
303 1.37.6.2 nathanw struct fd_softc fdsoftc;
304 1.37.6.2 nathanw int i, nfound, first_found;
305 1.37.6.2 nathanw
306 1.37.6.2 nathanw nfound = first_found = 0;
307 1.37.6.2 nathanw printf("\n");
308 1.37.6.2 nathanw fddeselect();
309 1.37.6.2 nathanw for(i = 0; i < NR_DRIVES; i++) {
310 1.37.6.2 nathanw
311 1.37.6.2 nathanw /*
312 1.37.6.2 nathanw * Test if unit is present
313 1.37.6.2 nathanw */
314 1.37.6.2 nathanw fdsoftc.unit = i;
315 1.37.6.2 nathanw fdsoftc.flags = 0;
316 1.37.6.2 nathanw st_dmagrab((dma_farg)fdcint, (dma_farg)fdtestdrv, &fdsoftc,
317 1.37.6.2 nathanw &lock_stat, 0);
318 1.37.6.2 nathanw st_dmafree(&fdsoftc, &lock_stat);
319 1.37.6.2 nathanw
320 1.37.6.2 nathanw if(!(fdsoftc.flags & FLPF_NOTRESP)) {
321 1.37.6.2 nathanw if(!nfound)
322 1.37.6.2 nathanw first_found = i;
323 1.37.6.2 nathanw nfound++;
324 1.37.6.2 nathanw config_found(dp, (void*)i, fdcprint);
325 1.37.6.2 nathanw }
326 1.37.6.2 nathanw }
327 1.37.6.2 nathanw
328 1.37.6.2 nathanw if(nfound) {
329 1.37.6.2 nathanw struct fd_softc *fdsc = getsoftc(fd_cd, first_found);
330 1.37.6.2 nathanw
331 1.37.6.2 nathanw /*
332 1.37.6.2 nathanw * Make sure motor will be turned of when a floppy is
333 1.37.6.2 nathanw * inserted in the first selected drive.
334 1.37.6.2 nathanw */
335 1.37.6.2 nathanw fdselect(first_found, 0, FLP_DD);
336 1.37.6.2 nathanw fd_state = FLP_MON;
337 1.37.6.2 nathanw callout_reset(&fdsc->sc_motor_ch, 0, (FPV)fdmotoroff, fdsc);
338 1.37.6.2 nathanw
339 1.37.6.2 nathanw /*
340 1.37.6.2 nathanw * enable disk related interrupts
341 1.37.6.2 nathanw */
342 1.37.6.2 nathanw MFP->mf_ierb |= IB_DINT;
343 1.37.6.2 nathanw MFP->mf_iprb = (u_int8_t)~IB_DINT;
344 1.37.6.2 nathanw MFP->mf_imrb |= IB_DINT;
345 1.37.6.2 nathanw }
346 1.37.6.2 nathanw }
347 1.37.6.2 nathanw
348 1.37.6.2 nathanw static int
349 1.37.6.2 nathanw fdcprint(auxp, pnp)
350 1.37.6.2 nathanw void *auxp;
351 1.37.6.2 nathanw const char *pnp;
352 1.37.6.2 nathanw {
353 1.37.6.2 nathanw if (pnp != NULL)
354 1.37.6.2 nathanw printf("fd%d at %s:", (int)auxp, pnp);
355 1.37.6.2 nathanw
356 1.37.6.2 nathanw return(UNCONF);
357 1.37.6.2 nathanw }
358 1.37.6.2 nathanw
359 1.37.6.2 nathanw static int fdmatch __P((struct device *, struct cfdata *, void *));
360 1.37.6.2 nathanw static void fdattach __P((struct device *, struct device *, void *));
361 1.37.6.2 nathanw
362 1.37.6.2 nathanw struct dkdriver fddkdriver = { fdstrategy };
363 1.37.6.2 nathanw
364 1.37.6.2 nathanw struct cfattach fd_ca = {
365 1.37.6.2 nathanw sizeof(struct fd_softc), fdmatch, fdattach
366 1.37.6.2 nathanw };
367 1.37.6.2 nathanw
368 1.37.6.2 nathanw extern struct cfdriver fd_cd;
369 1.37.6.2 nathanw
370 1.37.6.2 nathanw static int
371 1.37.6.2 nathanw fdmatch(pdp, cfp, auxp)
372 1.37.6.2 nathanw struct device *pdp;
373 1.37.6.2 nathanw struct cfdata *cfp;
374 1.37.6.2 nathanw void *auxp;
375 1.37.6.2 nathanw {
376 1.37.6.2 nathanw return(1);
377 1.37.6.2 nathanw }
378 1.37.6.2 nathanw
379 1.37.6.2 nathanw static void
380 1.37.6.2 nathanw fdattach(pdp, dp, auxp)
381 1.37.6.2 nathanw struct device *pdp, *dp;
382 1.37.6.2 nathanw void *auxp;
383 1.37.6.2 nathanw {
384 1.37.6.2 nathanw struct fd_softc *sc;
385 1.37.6.2 nathanw struct fd_types *type;
386 1.37.6.2 nathanw u_short swtch;
387 1.37.6.2 nathanw
388 1.37.6.2 nathanw sc = (struct fd_softc *)dp;
389 1.37.6.2 nathanw
390 1.37.6.2 nathanw callout_init(&sc->sc_motor_ch);
391 1.37.6.2 nathanw
392 1.37.6.2 nathanw /*
393 1.37.6.2 nathanw * Find out if an Ajax chip might be installed. Set the default
394 1.37.6.2 nathanw * floppy type accordingly.
395 1.37.6.2 nathanw */
396 1.37.6.2 nathanw swtch = rd_cfg_switch();
397 1.37.6.2 nathanw def_type = (swtch & CFG_SWITCH_NOHD) ? FLP_TYPE_720 : FLP_TYPE_144;
398 1.37.6.2 nathanw type = &fdtypes[def_type];
399 1.37.6.2 nathanw
400 1.37.6.2 nathanw printf(": %s %d cyl, %d head, %d sec\n", type->descr,
401 1.37.6.2 nathanw type->nblocks / (type->nsectors * type->nheads), type->nheads,
402 1.37.6.2 nathanw type->nsectors);
403 1.37.6.2 nathanw
404 1.37.6.2 nathanw /*
405 1.37.6.2 nathanw * Initialize and attach the disk structure.
406 1.37.6.2 nathanw */
407 1.37.6.2 nathanw sc->dkdev.dk_name = sc->sc_dv.dv_xname;
408 1.37.6.2 nathanw sc->dkdev.dk_driver = &fddkdriver;
409 1.37.6.2 nathanw disk_attach(&sc->dkdev);
410 1.37.6.2 nathanw }
411 1.37.6.2 nathanw
412 1.37.6.2 nathanw int
413 1.37.6.2 nathanw fdioctl(dev, cmd, addr, flag, p)
414 1.37.6.2 nathanw dev_t dev;
415 1.37.6.2 nathanw u_long cmd;
416 1.37.6.2 nathanw int flag;
417 1.37.6.2 nathanw caddr_t addr;
418 1.37.6.2 nathanw struct proc *p;
419 1.37.6.2 nathanw {
420 1.37.6.2 nathanw struct fd_softc *sc;
421 1.37.6.2 nathanw
422 1.37.6.2 nathanw sc = getsoftc(fd_cd, DISKUNIT(dev));
423 1.37.6.2 nathanw
424 1.37.6.2 nathanw if((sc->flags & FLPF_HAVELAB) == 0)
425 1.37.6.2 nathanw return(EBADF);
426 1.37.6.2 nathanw
427 1.37.6.2 nathanw switch(cmd) {
428 1.37.6.2 nathanw case DIOCSBAD:
429 1.37.6.2 nathanw return(EINVAL);
430 1.37.6.2 nathanw case DIOCGDINFO:
431 1.37.6.2 nathanw *(struct disklabel *)addr = *(sc->dkdev.dk_label);
432 1.37.6.2 nathanw return(0);
433 1.37.6.2 nathanw case DIOCGPART:
434 1.37.6.2 nathanw ((struct partinfo *)addr)->disklab =
435 1.37.6.2 nathanw sc->dkdev.dk_label;
436 1.37.6.2 nathanw ((struct partinfo *)addr)->part =
437 1.37.6.2 nathanw &sc->dkdev.dk_label->d_partitions[RAW_PART];
438 1.37.6.2 nathanw return(0);
439 1.37.6.2 nathanw #ifdef notyet /* XXX LWP */
440 1.37.6.2 nathanw case DIOCSRETRIES:
441 1.37.6.2 nathanw case DIOCSSTEP:
442 1.37.6.2 nathanw case DIOCSDINFO:
443 1.37.6.2 nathanw case DIOCWDINFO:
444 1.37.6.2 nathanw case DIOCWLABEL:
445 1.37.6.2 nathanw break;
446 1.37.6.2 nathanw #endif /* notyet */
447 1.37.6.2 nathanw case DIOCGDEFLABEL:
448 1.37.6.2 nathanw fdgetdefaultlabel(sc, (struct disklabel *)addr,
449 1.37.6.2 nathanw RAW_PART);
450 1.37.6.2 nathanw return(0);
451 1.37.6.2 nathanw }
452 1.37.6.2 nathanw return(ENOTTY);
453 1.37.6.2 nathanw }
454 1.37.6.2 nathanw
455 1.37.6.2 nathanw /*
456 1.37.6.2 nathanw * Open the device. If this is the first open on both the floppy devices,
457 1.37.6.2 nathanw * intialize the controller.
458 1.37.6.2 nathanw * Note that partition info on the floppy device is used to distinguise
459 1.37.6.2 nathanw * between 780Kb and 360Kb floppy's.
460 1.37.6.2 nathanw * partition 0: 360Kb
461 1.37.6.2 nathanw * partition 1: 780Kb
462 1.37.6.2 nathanw */
463 1.37.6.2 nathanw int
464 1.37.6.2 nathanw fdopen(dev, flags, devtype, proc)
465 1.37.6.2 nathanw dev_t dev;
466 1.37.6.2 nathanw int flags, devtype;
467 1.37.6.2 nathanw struct proc *proc;
468 1.37.6.2 nathanw {
469 1.37.6.2 nathanw struct fd_softc *sc;
470 1.37.6.2 nathanw int sps;
471 1.37.6.2 nathanw
472 1.37.6.2 nathanw #ifdef FLP_DEBUG
473 1.37.6.2 nathanw printf("fdopen dev=0x%x\n", dev);
474 1.37.6.2 nathanw #endif
475 1.37.6.2 nathanw
476 1.37.6.2 nathanw if(FLP_TYPE(dev) >= NR_TYPES)
477 1.37.6.2 nathanw return(ENXIO);
478 1.37.6.2 nathanw
479 1.37.6.2 nathanw if((sc = getsoftc(fd_cd, DISKUNIT(dev))) == NULL)
480 1.37.6.2 nathanw return(ENXIO);
481 1.37.6.2 nathanw
482 1.37.6.2 nathanw /*
483 1.37.6.2 nathanw * If no floppy currently open, reset the controller and select
484 1.37.6.2 nathanw * floppy type.
485 1.37.6.2 nathanw */
486 1.37.6.2 nathanw if(!nopens) {
487 1.37.6.2 nathanw
488 1.37.6.2 nathanw #ifdef FLP_DEBUG
489 1.37.6.2 nathanw printf("fdopen device not yet open\n");
490 1.37.6.2 nathanw #endif
491 1.37.6.2 nathanw nopens++;
492 1.37.6.2 nathanw write_fdreg(FDC_CS, IRUPT);
493 1.37.6.2 nathanw delay(40);
494 1.37.6.2 nathanw }
495 1.37.6.2 nathanw
496 1.37.6.2 nathanw /*
497 1.37.6.2 nathanw * Sleep while other process is opening the device
498 1.37.6.2 nathanw */
499 1.37.6.2 nathanw sps = splbio();
500 1.37.6.2 nathanw while(sc->flags & FLPF_INOPEN)
501 1.37.6.2 nathanw tsleep((caddr_t)sc, PRIBIO, "fdopen", 0);
502 1.37.6.2 nathanw splx(sps);
503 1.37.6.2 nathanw
504 1.37.6.2 nathanw if(!(sc->flags & FLPF_ISOPEN)) {
505 1.37.6.2 nathanw /*
506 1.37.6.2 nathanw * Initialise some driver values.
507 1.37.6.2 nathanw */
508 1.37.6.2 nathanw int type;
509 1.37.6.2 nathanw void *addr;
510 1.37.6.2 nathanw
511 1.37.6.2 nathanw type = FLP_TYPE(dev);
512 1.37.6.2 nathanw
513 1.37.6.2 nathanw bufq_alloc(&sc->bufq, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
514 1.37.6.2 nathanw sc->unit = DISKUNIT(dev);
515 1.37.6.2 nathanw sc->part = RAW_PART;
516 1.37.6.2 nathanw sc->nheads = fdtypes[type].nheads;
517 1.37.6.2 nathanw sc->nsectors = fdtypes[type].nsectors;
518 1.37.6.2 nathanw sc->nblocks = fdtypes[type].nblocks;
519 1.37.6.2 nathanw sc->density = fdtypes[type].density;
520 1.37.6.2 nathanw sc->curtrk = INV_TRK;
521 1.37.6.2 nathanw sc->sector = 0;
522 1.37.6.2 nathanw sc->errcnt = 0;
523 1.37.6.2 nathanw sc->bounceb = (u_char*)alloc_stmem(SECTOR_SIZE, &addr);
524 1.37.6.2 nathanw if(sc->bounceb == NULL)
525 1.37.6.2 nathanw return(ENOMEM); /* XXX */
526 1.37.6.2 nathanw
527 1.37.6.2 nathanw /*
528 1.37.6.2 nathanw * Go get write protect + loaded status
529 1.37.6.2 nathanw */
530 1.37.6.2 nathanw sc->flags |= FLPF_INOPEN|FLPF_GETSTAT;
531 1.37.6.2 nathanw sps = splbio();
532 1.37.6.2 nathanw st_dmagrab((dma_farg)fdcint, (dma_farg)fdstatus, sc,
533 1.37.6.2 nathanw &lock_stat, 0);
534 1.37.6.2 nathanw while(sc->flags & FLPF_GETSTAT)
535 1.37.6.2 nathanw tsleep((caddr_t)sc, PRIBIO, "fdopen", 0);
536 1.37.6.2 nathanw splx(sps);
537 1.37.6.2 nathanw wakeup((caddr_t)sc);
538 1.37.6.2 nathanw
539 1.37.6.2 nathanw if((sc->flags & FLPF_WRTPROT) && (flags & FWRITE)) {
540 1.37.6.2 nathanw sc->flags = 0;
541 1.37.6.2 nathanw return(EPERM);
542 1.37.6.2 nathanw }
543 1.37.6.2 nathanw if(sc->flags & FLPF_EMPTY) {
544 1.37.6.2 nathanw sc->flags = 0;
545 1.37.6.2 nathanw return(ENXIO);
546 1.37.6.2 nathanw }
547 1.37.6.2 nathanw sc->flags &= ~(FLPF_INOPEN|FLPF_GETSTAT);
548 1.37.6.2 nathanw sc->flags |= FLPF_ISOPEN;
549 1.37.6.2 nathanw }
550 1.37.6.2 nathanw else {
551 1.37.6.2 nathanw /*
552 1.37.6.2 nathanw * Multiply opens are granted when accessing the same type of
553 1.37.6.2 nathanw * floppy (eq. the same partition).
554 1.37.6.2 nathanw */
555 1.37.6.2 nathanw if(sc->density != fdtypes[DISKPART(dev)].density)
556 1.37.6.2 nathanw return(ENXIO); /* XXX temporarely out of business */
557 1.37.6.2 nathanw }
558 1.37.6.2 nathanw fdgetdisklabel(sc, dev);
559 1.37.6.2 nathanw #ifdef FLP_DEBUG
560 1.37.6.2 nathanw printf("fdopen open succeeded on type %d\n", sc->part);
561 1.37.6.2 nathanw #endif
562 1.37.6.2 nathanw return (0);
563 1.37.6.2 nathanw }
564 1.37.6.2 nathanw
565 1.37.6.2 nathanw int
566 1.37.6.2 nathanw fdclose(dev, flags, devtype, proc)
567 1.37.6.2 nathanw dev_t dev;
568 1.37.6.2 nathanw int flags, devtype;
569 1.37.6.2 nathanw struct proc *proc;
570 1.37.6.2 nathanw {
571 1.37.6.2 nathanw struct fd_softc *sc;
572 1.37.6.2 nathanw
573 1.37.6.2 nathanw sc = getsoftc(fd_cd, DISKUNIT(dev));
574 1.37.6.2 nathanw free_stmem(sc->bounceb);
575 1.37.6.2 nathanw sc->flags = 0;
576 1.37.6.2 nathanw nopens--;
577 1.37.6.2 nathanw
578 1.37.6.2 nathanw #ifdef FLP_DEBUG
579 1.37.6.2 nathanw printf("Closed floppy device -- nopens: %d\n", nopens);
580 1.37.6.2 nathanw #endif
581 1.37.6.2 nathanw return(0);
582 1.37.6.2 nathanw }
583 1.37.6.2 nathanw
584 1.37.6.2 nathanw void
585 1.37.6.2 nathanw fdstrategy(bp)
586 1.37.6.2 nathanw struct buf *bp;
587 1.37.6.2 nathanw {
588 1.37.6.2 nathanw struct fd_softc *sc;
589 1.37.6.2 nathanw struct disklabel *lp;
590 1.37.6.2 nathanw int sps, sz;
591 1.37.6.2 nathanw
592 1.37.6.2 nathanw sc = getsoftc(fd_cd, DISKUNIT(bp->b_dev));
593 1.37.6.2 nathanw
594 1.37.6.2 nathanw #ifdef FLP_DEBUG
595 1.37.6.2 nathanw printf("fdstrategy: %p, b_bcount: %ld\n", bp, bp->b_bcount);
596 1.37.6.2 nathanw #endif
597 1.37.6.2 nathanw
598 1.37.6.2 nathanw /*
599 1.37.6.2 nathanw * check for valid partition and bounds
600 1.37.6.2 nathanw */
601 1.37.6.2 nathanw lp = sc->dkdev.dk_label;
602 1.37.6.2 nathanw if ((sc->flags & FLPF_HAVELAB) == 0) {
603 1.37.6.2 nathanw bp->b_error = EIO;
604 1.37.6.2 nathanw goto bad;
605 1.37.6.2 nathanw }
606 1.37.6.2 nathanw if (bp->b_blkno < 0 || (bp->b_bcount % SECTOR_SIZE)) {
607 1.37.6.2 nathanw bp->b_error = EINVAL;
608 1.37.6.2 nathanw goto bad;
609 1.37.6.2 nathanw }
610 1.37.6.2 nathanw if (bp->b_bcount == 0)
611 1.37.6.2 nathanw goto done;
612 1.37.6.2 nathanw
613 1.37.6.2 nathanw sz = howmany(bp->b_bcount, SECTOR_SIZE);
614 1.37.6.2 nathanw
615 1.37.6.2 nathanw if (bp->b_blkno + sz > sc->nblocks) {
616 1.37.6.2 nathanw sz = sc->nblocks - bp->b_blkno;
617 1.37.6.2 nathanw if (sz == 0) /* Exactly at EndOfDisk */
618 1.37.6.2 nathanw goto done;
619 1.37.6.2 nathanw if (sz < 0) { /* Past EndOfDisk */
620 1.37.6.2 nathanw bp->b_error = EINVAL;
621 1.37.6.2 nathanw goto bad;
622 1.37.6.2 nathanw }
623 1.37.6.2 nathanw /* Trucate it */
624 1.37.6.2 nathanw if (bp->b_flags & B_RAW)
625 1.37.6.2 nathanw bp->b_bcount = sz << DEV_BSHIFT;
626 1.37.6.2 nathanw else bp->b_bcount = sz * lp->d_secsize;
627 1.37.6.2 nathanw }
628 1.37.6.2 nathanw
629 1.37.6.2 nathanw /* No partition translation. */
630 1.37.6.2 nathanw bp->b_rawblkno = bp->b_blkno;
631 1.37.6.2 nathanw
632 1.37.6.2 nathanw /*
633 1.37.6.2 nathanw * queue the buf and kick the low level code
634 1.37.6.2 nathanw */
635 1.37.6.2 nathanw sps = splbio();
636 1.37.6.2 nathanw BUFQ_PUT(&sc->bufq, bp); /* XXX disksort_cylinder */
637 1.37.6.2 nathanw if (!lock_stat) {
638 1.37.6.2 nathanw if (fd_state & FLP_MON)
639 1.37.6.2 nathanw callout_stop(&sc->sc_motor_ch);
640 1.37.6.2 nathanw fd_state = FLP_IDLE;
641 1.37.6.2 nathanw st_dmagrab((dma_farg)fdcint, (dma_farg)fdstart, sc,
642 1.37.6.2 nathanw &lock_stat, 0);
643 1.37.6.2 nathanw }
644 1.37.6.2 nathanw splx(sps);
645 1.37.6.2 nathanw
646 1.37.6.2 nathanw return;
647 1.37.6.2 nathanw bad:
648 1.37.6.2 nathanw bp->b_flags |= B_ERROR;
649 1.37.6.2 nathanw done:
650 1.37.6.2 nathanw bp->b_resid = bp->b_bcount;
651 1.37.6.2 nathanw biodone(bp);
652 1.37.6.2 nathanw }
653 1.37.6.2 nathanw
654 1.37.6.2 nathanw int
655 1.37.6.2 nathanw fdread(dev, uio, flags)
656 1.37.6.2 nathanw dev_t dev;
657 1.37.6.2 nathanw struct uio *uio;
658 1.37.6.2 nathanw int flags;
659 1.37.6.2 nathanw {
660 1.37.6.2 nathanw return(physio(fdstrategy, NULL, dev, B_READ, fdminphys, uio));
661 1.37.6.2 nathanw }
662 1.37.6.2 nathanw
663 1.37.6.2 nathanw int
664 1.37.6.2 nathanw fdwrite(dev, uio, flags)
665 1.37.6.2 nathanw dev_t dev;
666 1.37.6.2 nathanw struct uio *uio;
667 1.37.6.2 nathanw int flags;
668 1.37.6.2 nathanw {
669 1.37.6.2 nathanw return(physio(fdstrategy, NULL, dev, B_WRITE, fdminphys, uio));
670 1.37.6.2 nathanw }
671 1.37.6.2 nathanw
672 1.37.6.2 nathanw /*
673 1.37.6.2 nathanw * Called through DMA-dispatcher, get status.
674 1.37.6.2 nathanw */
675 1.37.6.2 nathanw static void
676 1.37.6.2 nathanw fdstatus(sc)
677 1.37.6.2 nathanw struct fd_softc *sc;
678 1.37.6.2 nathanw {
679 1.37.6.2 nathanw #ifdef FLP_DEBUG
680 1.37.6.2 nathanw printf("fdstatus\n");
681 1.37.6.2 nathanw #endif
682 1.37.6.2 nathanw sc->errcnt = 0;
683 1.37.6.2 nathanw fd_state = FLP_STAT;
684 1.37.6.2 nathanw fd_xfer(sc);
685 1.37.6.2 nathanw }
686 1.37.6.2 nathanw
687 1.37.6.2 nathanw /*
688 1.37.6.2 nathanw * Called through the dma-dispatcher. So we know we are the only ones
689 1.37.6.2 nathanw * messing with the floppy-controler.
690 1.37.6.2 nathanw * Initialize some fields in the fdsoftc for the state-machine and get
691 1.37.6.2 nathanw * it going.
692 1.37.6.2 nathanw */
693 1.37.6.2 nathanw static void
694 1.37.6.2 nathanw fdstart(sc)
695 1.37.6.2 nathanw struct fd_softc *sc;
696 1.37.6.2 nathanw {
697 1.37.6.2 nathanw struct buf *bp;
698 1.37.6.2 nathanw
699 1.37.6.2 nathanw bp = BUFQ_PEEK(&sc->bufq);
700 1.37.6.2 nathanw sc->sector = bp->b_blkno; /* Start sector for I/O */
701 1.37.6.2 nathanw sc->io_data = bp->b_data; /* KVA base for I/O */
702 1.37.6.2 nathanw sc->io_bytes = bp->b_bcount; /* Transfer size in bytes */
703 1.37.6.2 nathanw sc->io_dir = bp->b_flags & B_READ;/* Direction of transfer */
704 1.37.6.2 nathanw sc->errcnt = 0; /* No errors yet */
705 1.37.6.2 nathanw fd_state = FLP_XFER; /* Yes, we're going to transfer */
706 1.37.6.2 nathanw
707 1.37.6.2 nathanw /* Instrumentation. */
708 1.37.6.2 nathanw disk_busy(&sc->dkdev);
709 1.37.6.2 nathanw
710 1.37.6.2 nathanw fd_xfer(sc);
711 1.37.6.2 nathanw }
712 1.37.6.2 nathanw
713 1.37.6.2 nathanw /*
714 1.37.6.2 nathanw * The current transaction is finished (for good or bad). Let go of
715 1.37.6.2 nathanw * the dma-resources. Call biodone() to finish the transaction.
716 1.37.6.2 nathanw * Find a new transaction to work on.
717 1.37.6.2 nathanw */
718 1.37.6.2 nathanw static void
719 1.37.6.2 nathanw fddone(sc)
720 1.37.6.2 nathanw register struct fd_softc *sc;
721 1.37.6.2 nathanw {
722 1.37.6.2 nathanw struct buf *bp;
723 1.37.6.2 nathanw struct fd_softc *sc1;
724 1.37.6.2 nathanw int i, sps;
725 1.37.6.2 nathanw
726 1.37.6.2 nathanw /*
727 1.37.6.2 nathanw * Give others a chance to use the dma.
728 1.37.6.2 nathanw */
729 1.37.6.2 nathanw st_dmafree(sc, &lock_stat);
730 1.37.6.2 nathanw
731 1.37.6.2 nathanw
732 1.37.6.2 nathanw if(fd_state != FLP_STAT) {
733 1.37.6.2 nathanw /*
734 1.37.6.2 nathanw * Finish current transaction.
735 1.37.6.2 nathanw */
736 1.37.6.2 nathanw sps = splbio();
737 1.37.6.2 nathanw bp = BUFQ_GET(&sc->bufq);
738 1.37.6.2 nathanw if (bp == NULL)
739 1.37.6.2 nathanw panic("fddone");
740 1.37.6.2 nathanw splx(sps);
741 1.37.6.2 nathanw
742 1.37.6.2 nathanw #ifdef FLP_DEBUG
743 1.37.6.2 nathanw printf("fddone: unit: %d, buf: %p, resid: %d\n",sc->unit,bp,
744 1.37.6.2 nathanw sc->io_bytes);
745 1.37.6.2 nathanw #endif
746 1.37.6.2 nathanw bp->b_resid = sc->io_bytes;
747 1.37.6.2 nathanw
748 1.37.6.2 nathanw disk_unbusy(&sc->dkdev, (bp->b_bcount - bp->b_resid));
749 1.37.6.2 nathanw
750 1.37.6.2 nathanw biodone(bp);
751 1.37.6.2 nathanw }
752 1.37.6.2 nathanw fd_state = FLP_MON;
753 1.37.6.2 nathanw
754 1.37.6.2 nathanw if(lock_stat)
755 1.37.6.2 nathanw return; /* XXX Is this possible? */
756 1.37.6.2 nathanw
757 1.37.6.2 nathanw /*
758 1.37.6.2 nathanw * Find a new transaction on round-robin basis.
759 1.37.6.2 nathanw */
760 1.37.6.2 nathanw for(i = sc->unit + 1; ;i++) {
761 1.37.6.2 nathanw if(i >= fd_cd.cd_ndevs)
762 1.37.6.2 nathanw i = 0;
763 1.37.6.2 nathanw if((sc1 = fd_cd.cd_devs[i]) == NULL)
764 1.37.6.2 nathanw continue;
765 1.37.6.2 nathanw if (BUFQ_PEEK(&sc1->bufq) != NULL)
766 1.37.6.2 nathanw break;
767 1.37.6.2 nathanw if(i == sc->unit) {
768 1.37.6.2 nathanw callout_reset(&sc->sc_motor_ch, FLP_MONDELAY,
769 1.37.6.2 nathanw (FPV)fdmotoroff, sc);
770 1.37.6.2 nathanw #ifdef FLP_DEBUG
771 1.37.6.2 nathanw printf("fddone: Nothing to do\n");
772 1.37.6.2 nathanw #endif
773 1.37.6.2 nathanw return; /* No work */
774 1.37.6.2 nathanw }
775 1.37.6.2 nathanw }
776 1.37.6.2 nathanw fd_state = FLP_IDLE;
777 1.37.6.2 nathanw #ifdef FLP_DEBUG
778 1.37.6.2 nathanw printf("fddone: Staring job on unit %d\n", sc1->unit);
779 1.37.6.2 nathanw #endif
780 1.37.6.2 nathanw st_dmagrab((dma_farg)fdcint, (dma_farg)fdstart, sc1, &lock_stat, 0);
781 1.37.6.2 nathanw }
782 1.37.6.2 nathanw
783 1.37.6.2 nathanw static int
784 1.37.6.2 nathanw fdselect(drive, head, dense)
785 1.37.6.2 nathanw int drive, head, dense;
786 1.37.6.2 nathanw {
787 1.37.6.2 nathanw int i, spinning;
788 1.37.6.2 nathanw #ifdef FLP_DEBUG
789 1.37.6.2 nathanw printf("fdselect: drive=%d, head=%d, dense=%d\n", drive, head, dense);
790 1.37.6.2 nathanw #endif
791 1.37.6.2 nathanw i = ((drive == 1) ? PA_FLOP1 : PA_FLOP0) | head;
792 1.37.6.2 nathanw spinning = motoron;
793 1.37.6.2 nathanw motoron = 1;
794 1.37.6.2 nathanw
795 1.37.6.2 nathanw switch(dense) {
796 1.37.6.2 nathanw case FLP_DD:
797 1.37.6.2 nathanw DMA->dma_drvmode = 0;
798 1.37.6.2 nathanw break;
799 1.37.6.2 nathanw case FLP_HD:
800 1.37.6.2 nathanw DMA->dma_drvmode = (FDC_HDSET|FDC_HDSIG);
801 1.37.6.2 nathanw break;
802 1.37.6.2 nathanw default:
803 1.37.6.2 nathanw panic("fdselect: unknown density code\n");
804 1.37.6.2 nathanw }
805 1.37.6.2 nathanw if(i != selected) {
806 1.37.6.2 nathanw selected = i;
807 1.37.6.2 nathanw ym2149_fd_select((i ^ PA_FDSEL));
808 1.37.6.2 nathanw }
809 1.37.6.2 nathanw return(spinning);
810 1.37.6.2 nathanw }
811 1.37.6.2 nathanw
812 1.37.6.2 nathanw static void
813 1.37.6.2 nathanw fddeselect()
814 1.37.6.2 nathanw {
815 1.37.6.2 nathanw ym2149_fd_select(PA_FDSEL);
816 1.37.6.2 nathanw motoron = selected = 0;
817 1.37.6.2 nathanw DMA->dma_drvmode = 0;
818 1.37.6.2 nathanw }
819 1.37.6.2 nathanw
820 1.37.6.2 nathanw /****************************************************************************
821 1.37.6.2 nathanw * The following functions assume to be running as a result of a *
822 1.37.6.2 nathanw * disk-interrupt (e.q. spl = splbio). *
823 1.37.6.2 nathanw * They form the finit-state machine, the actual driver. *
824 1.37.6.2 nathanw * *
825 1.37.6.2 nathanw * fdstart()/ --> fd_xfer() -> activate hardware *
826 1.37.6.2 nathanw * fdopen() ^ *
827 1.37.6.2 nathanw * | *
828 1.37.6.2 nathanw * +-- not ready -<------------+ *
829 1.37.6.2 nathanw * | *
830 1.37.6.2 nathanw * fdmotoroff()/ --> fdcint() -> fd_xfer_ok() ---+ *
831 1.37.6.2 nathanw * h/w interrupt | *
832 1.37.6.2 nathanw * \|/ *
833 1.37.6.2 nathanw * finished ---> fdone() *
834 1.37.6.2 nathanw * *
835 1.37.6.2 nathanw ****************************************************************************/
836 1.37.6.2 nathanw static void
837 1.37.6.2 nathanw fd_xfer(sc)
838 1.37.6.2 nathanw struct fd_softc *sc;
839 1.37.6.2 nathanw {
840 1.37.6.2 nathanw register int head;
841 1.37.6.2 nathanw register int track, sector, hbit;
842 1.37.6.2 nathanw u_long phys_addr;
843 1.37.6.2 nathanw
844 1.37.6.2 nathanw head = track = 0;
845 1.37.6.2 nathanw switch(fd_state) {
846 1.37.6.2 nathanw case FLP_XFER:
847 1.37.6.2 nathanw /*
848 1.37.6.2 nathanw * Calculate head/track values
849 1.37.6.2 nathanw */
850 1.37.6.2 nathanw track = sc->sector / sc->nsectors;
851 1.37.6.2 nathanw head = track % sc->nheads;
852 1.37.6.2 nathanw track = track / sc->nheads;
853 1.37.6.2 nathanw #ifdef FLP_DEBUG
854 1.37.6.2 nathanw printf("fd_xfer: sector:%d,head:%d,track:%d\n", sc->sector,head,
855 1.37.6.2 nathanw track);
856 1.37.6.2 nathanw #endif
857 1.37.6.2 nathanw break;
858 1.37.6.2 nathanw
859 1.37.6.2 nathanw case FLP_STAT:
860 1.37.6.2 nathanw /*
861 1.37.6.2 nathanw * FLP_STAT only wants to recalibrate
862 1.37.6.2 nathanw */
863 1.37.6.2 nathanw sc->curtrk = INV_TRK;
864 1.37.6.2 nathanw break;
865 1.37.6.2 nathanw default:
866 1.37.6.2 nathanw panic("fd_xfer: wrong state (0x%x)", fd_state);
867 1.37.6.2 nathanw }
868 1.37.6.2 nathanw
869 1.37.6.2 nathanw /*
870 1.37.6.2 nathanw * Select the drive.
871 1.37.6.2 nathanw */
872 1.37.6.2 nathanw hbit = fdselect(sc->unit, head, sc->density) ? HBIT : 0;
873 1.37.6.2 nathanw
874 1.37.6.2 nathanw if(sc->curtrk == INV_TRK) {
875 1.37.6.2 nathanw /*
876 1.37.6.2 nathanw * Recalibrate, since we lost track of head positioning.
877 1.37.6.2 nathanw * The floppy disk controller has no way of determining its
878 1.37.6.2 nathanw * absolute arm position (track). Instead, it steps the
879 1.37.6.2 nathanw * arm a track at a time and keeps track of where it
880 1.37.6.2 nathanw * thinks it is (in software). However, after a SEEK, the
881 1.37.6.2 nathanw * hardware reads information from the diskette telling
882 1.37.6.2 nathanw * where the arm actually is. If the arm is in the wrong place,
883 1.37.6.2 nathanw * a recalibration is done, which forces the arm to track 0.
884 1.37.6.2 nathanw * This way the controller can get back into sync with reality.
885 1.37.6.2 nathanw */
886 1.37.6.2 nathanw fd_cmd = RESTORE;
887 1.37.6.2 nathanw write_fdreg(FDC_CS, RESTORE|VBIT|hbit);
888 1.37.6.2 nathanw callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
889 1.37.6.2 nathanw (FPV)fdmotoroff, sc);
890 1.37.6.2 nathanw
891 1.37.6.2 nathanw #ifdef FLP_DEBUG
892 1.37.6.2 nathanw printf("fd_xfer:Recalibrating drive %d\n", sc->unit);
893 1.37.6.2 nathanw #endif
894 1.37.6.2 nathanw return;
895 1.37.6.2 nathanw }
896 1.37.6.2 nathanw
897 1.37.6.2 nathanw write_fdreg(FDC_TR, sc->curtrk);
898 1.37.6.2 nathanw
899 1.37.6.2 nathanw /*
900 1.37.6.2 nathanw * Issue a SEEK command on the indicated drive unless the arm is
901 1.37.6.2 nathanw * already positioned on the correct track.
902 1.37.6.2 nathanw */
903 1.37.6.2 nathanw if(track != sc->curtrk) {
904 1.37.6.2 nathanw sc->curtrk = track; /* be optimistic */
905 1.37.6.2 nathanw write_fdreg(FDC_DR, track);
906 1.37.6.2 nathanw write_fdreg(FDC_CS, SEEK|RATE6|VBIT|hbit);
907 1.37.6.2 nathanw callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
908 1.37.6.2 nathanw (FPV)fdmotoroff, sc);
909 1.37.6.2 nathanw fd_cmd = SEEK;
910 1.37.6.2 nathanw #ifdef FLP_DEBUG
911 1.37.6.2 nathanw printf("fd_xfer:Seek to track %d on drive %d\n",track,sc->unit);
912 1.37.6.2 nathanw #endif
913 1.37.6.2 nathanw return;
914 1.37.6.2 nathanw }
915 1.37.6.2 nathanw
916 1.37.6.2 nathanw /*
917 1.37.6.2 nathanw * The drive is now on the proper track. Read or write 1 block.
918 1.37.6.2 nathanw */
919 1.37.6.2 nathanw sector = sc->sector % sc->nsectors;
920 1.37.6.2 nathanw sector++; /* start numbering at 1 */
921 1.37.6.2 nathanw
922 1.37.6.2 nathanw write_fdreg(FDC_SR, sector);
923 1.37.6.2 nathanw
924 1.37.6.2 nathanw phys_addr = (u_long)kvtop(sc->io_data);
925 1.37.6.2 nathanw if(phys_addr >= FDC_MAX_DMA_AD) {
926 1.37.6.2 nathanw /*
927 1.37.6.2 nathanw * We _must_ bounce this address
928 1.37.6.2 nathanw */
929 1.37.6.2 nathanw phys_addr = (u_long)kvtop(sc->bounceb);
930 1.37.6.2 nathanw if(sc->io_dir == B_WRITE)
931 1.37.6.2 nathanw bcopy(sc->io_data, sc->bounceb, SECTOR_SIZE);
932 1.37.6.2 nathanw sc->flags |= FLPF_BOUNCE;
933 1.37.6.2 nathanw }
934 1.37.6.2 nathanw st_dmaaddr_set((caddr_t)phys_addr); /* DMA address setup */
935 1.37.6.2 nathanw
936 1.37.6.2 nathanw #ifdef FLP_DEBUG
937 1.37.6.2 nathanw printf("fd_xfer:Start io (io_addr:%lx)\n", (u_long)kvtop(sc->io_data));
938 1.37.6.2 nathanw #endif
939 1.37.6.2 nathanw
940 1.37.6.2 nathanw if(sc->io_dir == B_READ) {
941 1.37.6.2 nathanw /* Issue the command */
942 1.37.6.2 nathanw st_dmacomm(DMA_FDC | DMA_SCREG, 1);
943 1.37.6.2 nathanw write_fdreg(FDC_CS, F_READ|hbit);
944 1.37.6.2 nathanw fd_cmd = F_READ;
945 1.37.6.2 nathanw }
946 1.37.6.2 nathanw else {
947 1.37.6.2 nathanw /* Issue the command */
948 1.37.6.2 nathanw st_dmacomm(DMA_WRBIT | DMA_FDC | DMA_SCREG, 1);
949 1.37.6.2 nathanw write_fdreg(DMA_WRBIT | FDC_CS, F_WRITE|hbit|EBIT|PBIT);
950 1.37.6.2 nathanw fd_cmd = F_WRITE;
951 1.37.6.2 nathanw }
952 1.37.6.2 nathanw callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY, (FPV)fdmotoroff, sc);
953 1.37.6.2 nathanw }
954 1.37.6.2 nathanw
955 1.37.6.2 nathanw /* return values of fd_xfer_ok(): */
956 1.37.6.2 nathanw #define X_OK 0
957 1.37.6.2 nathanw #define X_AGAIN 1
958 1.37.6.2 nathanw #define X_ERROR 2
959 1.37.6.2 nathanw #define X_FAIL 3
960 1.37.6.2 nathanw
961 1.37.6.2 nathanw /*
962 1.37.6.2 nathanw * Hardware interrupt function.
963 1.37.6.2 nathanw */
964 1.37.6.2 nathanw static void
965 1.37.6.2 nathanw fdcint(sc)
966 1.37.6.2 nathanw struct fd_softc *sc;
967 1.37.6.2 nathanw {
968 1.37.6.2 nathanw struct buf *bp;
969 1.37.6.2 nathanw
970 1.37.6.2 nathanw #ifdef FLP_DEBUG
971 1.37.6.2 nathanw printf("fdcint: unit = %d\n", sc->unit);
972 1.37.6.2 nathanw #endif
973 1.37.6.2 nathanw
974 1.37.6.2 nathanw /*
975 1.37.6.2 nathanw * Cancel timeout (we made it, didn't we)
976 1.37.6.2 nathanw */
977 1.37.6.2 nathanw callout_stop(&sc->sc_motor_ch);
978 1.37.6.2 nathanw
979 1.37.6.2 nathanw switch(fd_xfer_ok(sc)) {
980 1.37.6.2 nathanw case X_ERROR :
981 1.37.6.2 nathanw if(++(sc->errcnt) < MAX_ERRORS) {
982 1.37.6.2 nathanw /*
983 1.37.6.2 nathanw * Command failed but still retries left.
984 1.37.6.2 nathanw */
985 1.37.6.2 nathanw break;
986 1.37.6.2 nathanw }
987 1.37.6.2 nathanw /* FALL THROUGH */
988 1.37.6.2 nathanw case X_FAIL :
989 1.37.6.2 nathanw /*
990 1.37.6.2 nathanw * Non recoverable error. Fall back to motor-on
991 1.37.6.2 nathanw * idle-state.
992 1.37.6.2 nathanw */
993 1.37.6.2 nathanw if(fd_error != NULL) {
994 1.37.6.2 nathanw printf("Floppy error: %s\n", fd_error);
995 1.37.6.2 nathanw fd_error = NULL;
996 1.37.6.2 nathanw }
997 1.37.6.2 nathanw
998 1.37.6.2 nathanw if(fd_state == FLP_STAT) {
999 1.37.6.2 nathanw sc->flags |= FLPF_EMPTY;
1000 1.37.6.2 nathanw sc->flags &= ~FLPF_GETSTAT;
1001 1.37.6.2 nathanw wakeup((caddr_t)sc);
1002 1.37.6.2 nathanw fddone(sc);
1003 1.37.6.2 nathanw return;
1004 1.37.6.2 nathanw }
1005 1.37.6.2 nathanw
1006 1.37.6.2 nathanw bp = BUFQ_PEEK(&sc->bufq);
1007 1.37.6.2 nathanw
1008 1.37.6.2 nathanw bp->b_error = EIO;
1009 1.37.6.2 nathanw bp->b_flags |= B_ERROR;
1010 1.37.6.2 nathanw fd_state = FLP_MON;
1011 1.37.6.2 nathanw
1012 1.37.6.2 nathanw break;
1013 1.37.6.2 nathanw case X_AGAIN:
1014 1.37.6.2 nathanw /*
1015 1.37.6.2 nathanw * Start next part of state machine.
1016 1.37.6.2 nathanw */
1017 1.37.6.2 nathanw break;
1018 1.37.6.2 nathanw case X_OK:
1019 1.37.6.2 nathanw /*
1020 1.37.6.2 nathanw * Command ok and finished. Reset error-counter.
1021 1.37.6.2 nathanw * If there are no more bytes to transfer fall back
1022 1.37.6.2 nathanw * to motor-on idle state.
1023 1.37.6.2 nathanw */
1024 1.37.6.2 nathanw sc->errcnt = 0;
1025 1.37.6.2 nathanw
1026 1.37.6.2 nathanw if(fd_state == FLP_STAT) {
1027 1.37.6.2 nathanw sc->flags &= ~FLPF_GETSTAT;
1028 1.37.6.2 nathanw wakeup((caddr_t)sc);
1029 1.37.6.2 nathanw fddone(sc);
1030 1.37.6.2 nathanw return;
1031 1.37.6.2 nathanw }
1032 1.37.6.2 nathanw
1033 1.37.6.2 nathanw if((sc->flags & FLPF_BOUNCE) && (sc->io_dir == B_READ))
1034 1.37.6.2 nathanw bcopy(sc->bounceb, sc->io_data, SECTOR_SIZE);
1035 1.37.6.2 nathanw sc->flags &= ~FLPF_BOUNCE;
1036 1.37.6.2 nathanw
1037 1.37.6.2 nathanw sc->sector++;
1038 1.37.6.2 nathanw sc->io_data += SECTOR_SIZE;
1039 1.37.6.2 nathanw sc->io_bytes -= SECTOR_SIZE;
1040 1.37.6.2 nathanw if(sc->io_bytes <= 0)
1041 1.37.6.2 nathanw fd_state = FLP_MON;
1042 1.37.6.2 nathanw }
1043 1.37.6.2 nathanw if(fd_state == FLP_MON)
1044 1.37.6.2 nathanw fddone(sc);
1045 1.37.6.2 nathanw else fd_xfer(sc);
1046 1.37.6.2 nathanw }
1047 1.37.6.2 nathanw
1048 1.37.6.2 nathanw /*
1049 1.37.6.2 nathanw * Determine status of last command. Should only be called through
1050 1.37.6.2 nathanw * 'fdcint()'.
1051 1.37.6.2 nathanw * Returns:
1052 1.37.6.2 nathanw * X_ERROR : Error on command; might succeed next time.
1053 1.37.6.2 nathanw * X_FAIL : Error on command; will never succeed.
1054 1.37.6.2 nathanw * X_AGAIN : Part of a command succeeded, call 'fd_xfer()' to complete.
1055 1.37.6.2 nathanw * X_OK : Command succeeded and is complete.
1056 1.37.6.2 nathanw *
1057 1.37.6.2 nathanw * This function only affects sc->curtrk.
1058 1.37.6.2 nathanw */
1059 1.37.6.2 nathanw static int
1060 1.37.6.2 nathanw fd_xfer_ok(sc)
1061 1.37.6.2 nathanw register struct fd_softc *sc;
1062 1.37.6.2 nathanw {
1063 1.37.6.2 nathanw register int status;
1064 1.37.6.2 nathanw
1065 1.37.6.2 nathanw #ifdef FLP_DEBUG
1066 1.37.6.2 nathanw printf("fd_xfer_ok: cmd: 0x%x, state: 0x%x\n", fd_cmd, fd_state);
1067 1.37.6.2 nathanw #endif
1068 1.37.6.2 nathanw switch(fd_cmd) {
1069 1.37.6.2 nathanw case IRUPT:
1070 1.37.6.2 nathanw /*
1071 1.37.6.2 nathanw * Timeout. Force a recalibrate before we try again.
1072 1.37.6.2 nathanw */
1073 1.37.6.2 nathanw status = read_fdreg(FDC_CS);
1074 1.37.6.2 nathanw
1075 1.37.6.2 nathanw fd_error = "Timeout";
1076 1.37.6.2 nathanw sc->curtrk = INV_TRK;
1077 1.37.6.2 nathanw return(X_ERROR);
1078 1.37.6.2 nathanw case F_READ:
1079 1.37.6.2 nathanw /*
1080 1.37.6.2 nathanw * Test for DMA error
1081 1.37.6.2 nathanw */
1082 1.37.6.2 nathanw status = read_dmastat();
1083 1.37.6.2 nathanw if(!(status & DMAOK)) {
1084 1.37.6.2 nathanw fd_error = "Dma error";
1085 1.37.6.2 nathanw return(X_ERROR);
1086 1.37.6.2 nathanw }
1087 1.37.6.2 nathanw /*
1088 1.37.6.2 nathanw * Get controller status and check for errors.
1089 1.37.6.2 nathanw */
1090 1.37.6.2 nathanw status = read_fdreg(FDC_CS);
1091 1.37.6.2 nathanw if(status & (RNF | CRCERR | LD_T00)) {
1092 1.37.6.2 nathanw fd_error = "Read error";
1093 1.37.6.2 nathanw if(status & RNF)
1094 1.37.6.2 nathanw sc->curtrk = INV_TRK;
1095 1.37.6.2 nathanw return(X_ERROR);
1096 1.37.6.2 nathanw }
1097 1.37.6.2 nathanw break;
1098 1.37.6.2 nathanw case F_WRITE:
1099 1.37.6.2 nathanw /*
1100 1.37.6.2 nathanw * Test for DMA error
1101 1.37.6.2 nathanw */
1102 1.37.6.2 nathanw status = read_dmastat();
1103 1.37.6.2 nathanw if(!(status & DMAOK)) {
1104 1.37.6.2 nathanw fd_error = "Dma error";
1105 1.37.6.2 nathanw return(X_ERROR);
1106 1.37.6.2 nathanw }
1107 1.37.6.2 nathanw /*
1108 1.37.6.2 nathanw * Get controller status and check for errors.
1109 1.37.6.2 nathanw */
1110 1.37.6.2 nathanw status = read_fdreg(FDC_CS);
1111 1.37.6.2 nathanw if(status & WRI_PRO) {
1112 1.37.6.2 nathanw fd_error = "Write protected";
1113 1.37.6.2 nathanw return(X_FAIL);
1114 1.37.6.2 nathanw }
1115 1.37.6.2 nathanw if(status & (RNF | CRCERR | LD_T00)) {
1116 1.37.6.2 nathanw fd_error = "Write error";
1117 1.37.6.2 nathanw sc->curtrk = INV_TRK;
1118 1.37.6.2 nathanw return(X_ERROR);
1119 1.37.6.2 nathanw }
1120 1.37.6.2 nathanw break;
1121 1.37.6.2 nathanw case SEEK:
1122 1.37.6.2 nathanw status = read_fdreg(FDC_CS);
1123 1.37.6.2 nathanw if(status & (RNF | CRCERR)) {
1124 1.37.6.2 nathanw fd_error = "Seek error";
1125 1.37.6.2 nathanw sc->curtrk = INV_TRK;
1126 1.37.6.2 nathanw return(X_ERROR);
1127 1.37.6.2 nathanw }
1128 1.37.6.2 nathanw return(X_AGAIN);
1129 1.37.6.2 nathanw case RESTORE:
1130 1.37.6.2 nathanw /*
1131 1.37.6.2 nathanw * Determine if the recalibration succeeded.
1132 1.37.6.2 nathanw */
1133 1.37.6.2 nathanw status = read_fdreg(FDC_CS);
1134 1.37.6.2 nathanw if(status & RNF) {
1135 1.37.6.2 nathanw fd_error = "Recalibrate error";
1136 1.37.6.2 nathanw /* reset controller */
1137 1.37.6.2 nathanw write_fdreg(FDC_CS, IRUPT);
1138 1.37.6.2 nathanw sc->curtrk = INV_TRK;
1139 1.37.6.2 nathanw return(X_ERROR);
1140 1.37.6.2 nathanw }
1141 1.37.6.2 nathanw sc->curtrk = 0;
1142 1.37.6.2 nathanw if(fd_state == FLP_STAT) {
1143 1.37.6.2 nathanw if(status & WRI_PRO)
1144 1.37.6.2 nathanw sc->flags |= FLPF_WRTPROT;
1145 1.37.6.2 nathanw break;
1146 1.37.6.2 nathanw }
1147 1.37.6.2 nathanw return(X_AGAIN);
1148 1.37.6.2 nathanw default:
1149 1.37.6.2 nathanw fd_error = "Driver error: fd_xfer_ok : Unknown state";
1150 1.37.6.2 nathanw return(X_FAIL);
1151 1.37.6.2 nathanw }
1152 1.37.6.2 nathanw return(X_OK);
1153 1.37.6.2 nathanw }
1154 1.37.6.2 nathanw
1155 1.37.6.2 nathanw /*
1156 1.37.6.2 nathanw * All timeouts will call this function.
1157 1.37.6.2 nathanw */
1158 1.37.6.2 nathanw static void
1159 1.37.6.2 nathanw fdmotoroff(sc)
1160 1.37.6.2 nathanw struct fd_softc *sc;
1161 1.37.6.2 nathanw {
1162 1.37.6.2 nathanw int sps;
1163 1.37.6.2 nathanw
1164 1.37.6.2 nathanw /*
1165 1.37.6.2 nathanw * Get at harware interrupt level
1166 1.37.6.2 nathanw */
1167 1.37.6.2 nathanw sps = splbio();
1168 1.37.6.2 nathanw
1169 1.37.6.2 nathanw #if FLP_DEBUG
1170 1.37.6.2 nathanw printf("fdmotoroff, state = 0x%x\n", fd_state);
1171 1.37.6.2 nathanw #endif
1172 1.37.6.2 nathanw
1173 1.37.6.2 nathanw switch(fd_state) {
1174 1.37.6.2 nathanw case FLP_STAT :
1175 1.37.6.2 nathanw case FLP_XFER :
1176 1.37.6.2 nathanw /*
1177 1.37.6.2 nathanw * Timeout during a transfer; cancel transaction
1178 1.37.6.2 nathanw * set command to 'IRUPT'.
1179 1.37.6.2 nathanw * A drive-interrupt is simulated to trigger the state
1180 1.37.6.2 nathanw * machine.
1181 1.37.6.2 nathanw */
1182 1.37.6.2 nathanw /*
1183 1.37.6.2 nathanw * Cancel current transaction
1184 1.37.6.2 nathanw */
1185 1.37.6.2 nathanw fd_cmd = IRUPT;
1186 1.37.6.2 nathanw write_fdreg(FDC_CS, IRUPT);
1187 1.37.6.2 nathanw delay(20);
1188 1.37.6.2 nathanw (void)read_fdreg(FDC_CS);
1189 1.37.6.2 nathanw write_fdreg(FDC_CS, RESTORE);
1190 1.37.6.2 nathanw break;
1191 1.37.6.2 nathanw
1192 1.37.6.2 nathanw case FLP_MON :
1193 1.37.6.2 nathanw /*
1194 1.37.6.2 nathanw * Turn motor off.
1195 1.37.6.2 nathanw */
1196 1.37.6.2 nathanw if(selected) {
1197 1.37.6.2 nathanw int tmp;
1198 1.37.6.2 nathanw
1199 1.37.6.2 nathanw st_dmagrab((dma_farg)fdcint, (dma_farg)fdmoff,
1200 1.37.6.2 nathanw sc, &tmp, 0);
1201 1.37.6.2 nathanw }
1202 1.37.6.2 nathanw else fd_state = FLP_IDLE;
1203 1.37.6.2 nathanw break;
1204 1.37.6.2 nathanw }
1205 1.37.6.2 nathanw splx(sps);
1206 1.37.6.2 nathanw }
1207 1.37.6.2 nathanw
1208 1.37.6.2 nathanw /*
1209 1.37.6.2 nathanw * min byte count to whats left of the track in question
1210 1.37.6.2 nathanw */
1211 1.37.6.2 nathanw static void
1212 1.37.6.2 nathanw fdminphys(bp)
1213 1.37.6.2 nathanw struct buf *bp;
1214 1.37.6.2 nathanw {
1215 1.37.6.2 nathanw struct fd_softc *sc;
1216 1.37.6.2 nathanw int sec, toff, tsz;
1217 1.37.6.2 nathanw
1218 1.37.6.2 nathanw if((sc = getsoftc(fd_cd, DISKUNIT(bp->b_dev))) == NULL)
1219 1.37.6.2 nathanw panic("fdminphys: couldn't get softc");
1220 1.37.6.2 nathanw
1221 1.37.6.2 nathanw sec = bp->b_blkno % (sc->nsectors * sc->nheads);
1222 1.37.6.2 nathanw toff = sec * SECTOR_SIZE;
1223 1.37.6.2 nathanw tsz = sc->nsectors * sc->nheads * SECTOR_SIZE;
1224 1.37.6.2 nathanw
1225 1.37.6.2 nathanw #ifdef FLP_DEBUG
1226 1.37.6.2 nathanw printf("fdminphys: before %ld", bp->b_bcount);
1227 1.37.6.2 nathanw #endif
1228 1.37.6.2 nathanw
1229 1.37.6.2 nathanw bp->b_bcount = min(bp->b_bcount, tsz - toff);
1230 1.37.6.2 nathanw
1231 1.37.6.2 nathanw #ifdef FLP_DEBUG
1232 1.37.6.2 nathanw printf(" after %ld\n", bp->b_bcount);
1233 1.37.6.2 nathanw #endif
1234 1.37.6.2 nathanw
1235 1.37.6.2 nathanw minphys(bp);
1236 1.37.6.2 nathanw }
1237 1.37.6.2 nathanw
1238 1.37.6.2 nathanw /*
1239 1.37.6.2 nathanw * Called from fdmotoroff to turn the motor actually off....
1240 1.37.6.2 nathanw * This can't be done in fdmotoroff itself, because exclusive access to the
1241 1.37.6.2 nathanw * DMA controller is needed to read the FDC-status register. The function
1242 1.37.6.2 nathanw * 'fdmoff()' always runs as the result of a 'dmagrab()'.
1243 1.37.6.2 nathanw * We need to test the status-register because we want to be sure that the
1244 1.37.6.2 nathanw * drive motor is really off before deselecting the drive. The FDC only
1245 1.37.6.2 nathanw * turns off the drive motor after having seen 10 index-pulses. You only
1246 1.37.6.2 nathanw * get index-pulses when a drive is selected....This means that if the
1247 1.37.6.2 nathanw * drive is deselected when the motor is still spinning, it will continue
1248 1.37.6.2 nathanw * to spin _even_ when you insert a floppy later on...
1249 1.37.6.2 nathanw */
1250 1.37.6.2 nathanw static void
1251 1.37.6.2 nathanw fdmoff(fdsoftc)
1252 1.37.6.2 nathanw struct fd_softc *fdsoftc;
1253 1.37.6.2 nathanw {
1254 1.37.6.2 nathanw int tmp;
1255 1.37.6.2 nathanw
1256 1.37.6.2 nathanw if ((fd_state == FLP_MON) && selected) {
1257 1.37.6.2 nathanw tmp = read_fdreg(FDC_CS);
1258 1.37.6.2 nathanw if (!(tmp & MOTORON)) {
1259 1.37.6.2 nathanw fddeselect();
1260 1.37.6.2 nathanw fd_state = FLP_IDLE;
1261 1.37.6.2 nathanw }
1262 1.37.6.2 nathanw else
1263 1.37.6.2 nathanw callout_reset(&fdsoftc->sc_motor_ch, 10*FLP_MONDELAY,
1264 1.37.6.2 nathanw (FPV)fdmotoroff, fdsoftc);
1265 1.37.6.2 nathanw }
1266 1.37.6.2 nathanw st_dmafree(fdsoftc, &tmp);
1267 1.37.6.2 nathanw }
1268 1.37.6.2 nathanw
1269 1.37.6.2 nathanw /*
1270 1.37.6.2 nathanw * Used to find out wich drives are actually connected. We do this by issuing
1271 1.37.6.2 nathanw * is 'RESTORE' command and check if the 'track-0' bit is set. This also works
1272 1.37.6.2 nathanw * if the drive is present but no floppy is inserted.
1273 1.37.6.2 nathanw */
1274 1.37.6.2 nathanw static void
1275 1.37.6.2 nathanw fdtestdrv(fdsoftc)
1276 1.37.6.2 nathanw struct fd_softc *fdsoftc;
1277 1.37.6.2 nathanw {
1278 1.37.6.2 nathanw int status;
1279 1.37.6.2 nathanw
1280 1.37.6.2 nathanw /*
1281 1.37.6.2 nathanw * Select the right unit and head.
1282 1.37.6.2 nathanw */
1283 1.37.6.2 nathanw fdselect(fdsoftc->unit, 0, FLP_DD);
1284 1.37.6.2 nathanw
1285 1.37.6.2 nathanw write_fdreg(FDC_CS, RESTORE|HBIT);
1286 1.37.6.2 nathanw
1287 1.37.6.2 nathanw /*
1288 1.37.6.2 nathanw * Wait for about 2 seconds.
1289 1.37.6.2 nathanw */
1290 1.37.6.2 nathanw delay(2000000);
1291 1.37.6.2 nathanw
1292 1.37.6.2 nathanw status = read_fdreg(FDC_CS);
1293 1.37.6.2 nathanw if(status & (RNF|BUSY)) {
1294 1.37.6.2 nathanw write_fdreg(FDC_CS, IRUPT); /* reset controller */
1295 1.37.6.2 nathanw delay(40);
1296 1.37.6.2 nathanw }
1297 1.37.6.2 nathanw
1298 1.37.6.2 nathanw if(!(status & LD_T00))
1299 1.37.6.2 nathanw fdsoftc->flags |= FLPF_NOTRESP;
1300 1.37.6.2 nathanw
1301 1.37.6.2 nathanw fddeselect();
1302 1.37.6.2 nathanw }
1303 1.37.6.2 nathanw
1304 1.37.6.2 nathanw static void
1305 1.37.6.2 nathanw fdgetdefaultlabel(sc, lp, part)
1306 1.37.6.2 nathanw struct fd_softc *sc;
1307 1.37.6.2 nathanw struct disklabel *lp;
1308 1.37.6.2 nathanw int part;
1309 1.37.6.2 nathanw {
1310 1.37.6.2 nathanw
1311 1.37.6.2 nathanw bzero(lp, sizeof(struct disklabel));
1312 1.37.6.2 nathanw
1313 1.37.6.2 nathanw lp->d_secsize = SECTOR_SIZE;
1314 1.37.6.2 nathanw lp->d_ntracks = sc->nheads;
1315 1.37.6.2 nathanw lp->d_nsectors = sc->nsectors;
1316 1.37.6.2 nathanw lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1317 1.37.6.2 nathanw lp->d_ncylinders = sc->nblocks / lp->d_secpercyl;
1318 1.37.6.2 nathanw lp->d_secperunit = sc->nblocks;
1319 1.37.6.2 nathanw
1320 1.37.6.2 nathanw lp->d_type = DTYPE_FLOPPY;
1321 1.37.6.2 nathanw lp->d_rpm = 300; /* good guess I suppose. */
1322 1.37.6.2 nathanw lp->d_interleave = 1; /* FIXME: is this OK? */
1323 1.37.6.2 nathanw lp->d_bbsize = 0;
1324 1.37.6.2 nathanw lp->d_sbsize = 0;
1325 1.37.6.2 nathanw lp->d_npartitions = part + 1;
1326 1.37.6.2 nathanw lp->d_trkseek = STEP_DELAY;
1327 1.37.6.2 nathanw lp->d_magic = DISKMAGIC;
1328 1.37.6.2 nathanw lp->d_magic2 = DISKMAGIC;
1329 1.37.6.2 nathanw lp->d_checksum = dkcksum(lp);
1330 1.37.6.2 nathanw lp->d_partitions[part].p_size = lp->d_secperunit;
1331 1.37.6.2 nathanw lp->d_partitions[part].p_fstype = FS_UNUSED;
1332 1.37.6.2 nathanw lp->d_partitions[part].p_fsize = 1024;
1333 1.37.6.2 nathanw lp->d_partitions[part].p_frag = 8;
1334 1.37.6.2 nathanw }
1335 1.37.6.2 nathanw
1336 1.37.6.2 nathanw /*
1337 1.37.6.2 nathanw * Build disk label. For now we only create a label from what we know
1338 1.37.6.2 nathanw * from 'sc'.
1339 1.37.6.2 nathanw */
1340 1.37.6.2 nathanw static int
1341 1.37.6.2 nathanw fdgetdisklabel(sc, dev)
1342 1.37.6.2 nathanw struct fd_softc *sc;
1343 1.37.6.2 nathanw dev_t dev;
1344 1.37.6.2 nathanw {
1345 1.37.6.2 nathanw struct disklabel *lp;
1346 1.37.6.2 nathanw int part;
1347 1.37.6.2 nathanw
1348 1.37.6.2 nathanw /*
1349 1.37.6.2 nathanw * If we already got one, get out.
1350 1.37.6.2 nathanw */
1351 1.37.6.2 nathanw if(sc->flags & FLPF_HAVELAB)
1352 1.37.6.2 nathanw return(0);
1353 1.37.6.2 nathanw
1354 1.37.6.2 nathanw #ifdef FLP_DEBUG
1355 1.37.6.2 nathanw printf("fdgetdisklabel()\n");
1356 1.37.6.2 nathanw #endif
1357 1.37.6.2 nathanw
1358 1.37.6.2 nathanw part = RAW_PART;
1359 1.37.6.2 nathanw lp = sc->dkdev.dk_label;
1360 1.37.6.2 nathanw fdgetdefaultlabel(sc, lp, part);
1361 1.37.6.2 nathanw sc->flags |= FLPF_HAVELAB;
1362 1.37.6.2 nathanw
1363 1.37.6.2 nathanw return(0);
1364 1.37.6.2 nathanw }
1365