fd.c revision 1.1 1 1.1 pk /* $NetBSD: fd.c,v 1.1 1995/02/17 20:28:32 pk Exp $ */
2 1.1 pk
3 1.1 pk /*-
4 1.1 pk * Copyright (c) 1993, 1994, 1995 Charles Hannum.
5 1.1 pk * Copyright (c) 1995 Paul Kranenburg.
6 1.1 pk * Copyright (c) 1990 The Regents of the University of California.
7 1.1 pk * All rights reserved.
8 1.1 pk *
9 1.1 pk * This code is derived from software contributed to Berkeley by
10 1.1 pk * Don Ahn.
11 1.1 pk *
12 1.1 pk * Redistribution and use in source and binary forms, with or without
13 1.1 pk * modification, are permitted provided that the following conditions
14 1.1 pk * are met:
15 1.1 pk * 1. Redistributions of source code must retain the above copyright
16 1.1 pk * notice, this list of conditions and the following disclaimer.
17 1.1 pk * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 pk * notice, this list of conditions and the following disclaimer in the
19 1.1 pk * documentation and/or other materials provided with the distribution.
20 1.1 pk * 3. All advertising materials mentioning features or use of this software
21 1.1 pk * must display the following acknowledgement:
22 1.1 pk * This product includes software developed by the University of
23 1.1 pk * California, Berkeley and its contributors.
24 1.1 pk * 4. Neither the name of the University nor the names of its contributors
25 1.1 pk * may be used to endorse or promote products derived from this software
26 1.1 pk * without specific prior written permission.
27 1.1 pk *
28 1.1 pk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 pk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 pk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 pk * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 pk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 pk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 pk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 pk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 pk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 pk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 pk * SUCH DAMAGE.
39 1.1 pk *
40 1.1 pk * @(#)fd.c 7.4 (Berkeley) 5/25/91
41 1.1 pk */
42 1.1 pk
43 1.1 pk #include <sys/param.h>
44 1.1 pk #include <sys/systm.h>
45 1.1 pk #include <sys/kernel.h>
46 1.1 pk #include <sys/conf.h>
47 1.1 pk #include <sys/file.h>
48 1.1 pk #include <sys/ioctl.h>
49 1.1 pk #include <sys/device.h>
50 1.1 pk #include <sys/disklabel.h>
51 1.1 pk #include <sys/dkstat.h>
52 1.1 pk #include <sys/disk.h>
53 1.1 pk #include <sys/buf.h>
54 1.1 pk #include <sys/uio.h>
55 1.1 pk #include <sys/syslog.h>
56 1.1 pk #include <sys/queue.h>
57 1.1 pk
58 1.1 pk #include <machine/cpu.h>
59 1.1 pk #include <machine/autoconf.h>
60 1.1 pk #include <sparc/dev/fdreg.h>
61 1.1 pk #include <sparc/sparc/auxreg.h>
62 1.1 pk
63 1.1 pk #define FDUNIT(dev) (minor(dev) / 8)
64 1.1 pk #define FDTYPE(dev) (minor(dev) % 8)
65 1.1 pk
66 1.1 pk #define b_cylin b_resid
67 1.1 pk
68 1.1 pk enum fdc_state {
69 1.1 pk DEVIDLE = 0,
70 1.1 pk MOTORWAIT,
71 1.1 pk DOSEEK,
72 1.1 pk SEEKWAIT,
73 1.1 pk SEEKTIMEDOUT,
74 1.1 pk SEEKCOMPLETE,
75 1.1 pk DOIO,
76 1.1 pk PSEUDODMA,
77 1.1 pk IOTIMEDOUT,
78 1.1 pk IOCOMPLETE,
79 1.1 pk DORESET,
80 1.1 pk RESETCOMPLETE,
81 1.1 pk RESETTIMEDOUT,
82 1.1 pk DORECAL,
83 1.1 pk RECALWAIT,
84 1.1 pk RECALTIMEDOUT,
85 1.1 pk RECALCOMPLETE,
86 1.1 pk };
87 1.1 pk
88 1.1 pk /* software state, per controller */
89 1.1 pk struct fdc_softc {
90 1.1 pk struct dkdevice sc_dk; /* boilerplate */
91 1.1 pk struct intrhand sc_sih;
92 1.1 pk struct intrhand sc_hih;
93 1.1 pk caddr_t sc_reg;
94 1.1 pk /*
95 1.1 pk * 82072 (sun4c) and 82077 (sun4m) controllers have different
96 1.1 pk * register layout; so we cache some here.
97 1.1 pk */
98 1.1 pk volatile u_int8_t *sc_reg_msr;
99 1.1 pk #define sc_reg_drs sc_reg_msr
100 1.1 pk volatile u_int8_t *sc_reg_data;
101 1.1 pk volatile u_int8_t *sc_reg_dor; /* 82077 only */
102 1.1 pk
103 1.1 pk /* Auxialiary pseudo-dma vars */
104 1.1 pk char *sc_bufp; /* next char to send/receive */
105 1.1 pk int sc_tc; /* bytes to go until Terminal Count */
106 1.1 pk
107 1.1 pk struct fd_softc *sc_fd[4]; /* pointers to children */
108 1.1 pk TAILQ_HEAD(drivehead, fd_softc) sc_drives;
109 1.1 pk enum fdc_state sc_state;
110 1.1 pk int sc_flags;
111 1.1 pk #define FDC_82077 0x01
112 1.1 pk #define FDC_NEEDSENSEI 0x02 /* XXX - do sense in hwintr */
113 1.1 pk int sc_errors; /* number of retries so far */
114 1.1 pk u_char sc_status[10]; /* copy of registers */
115 1.1 pk int sc_nstat; /* # of valid status bytes */
116 1.1 pk int sc_threshold; /* FIFO threshold value */
117 1.1 pk };
118 1.1 pk
119 1.1 pk /* controller driver configuration */
120 1.1 pk int fdcmatch __P((struct device *, void *, void *));
121 1.1 pk void fdcattach __P((struct device *, struct device *, void *));
122 1.1 pk
123 1.1 pk struct cfdriver fdccd = {
124 1.1 pk NULL, "fdc", fdcmatch, fdcattach, DV_DULL, sizeof(struct fdc_softc)
125 1.1 pk };
126 1.1 pk
127 1.1 pk /*
128 1.1 pk * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
129 1.1 pk * we tell them apart.
130 1.1 pk */
131 1.1 pk struct fd_type {
132 1.1 pk int sectrac; /* sectors per track */
133 1.1 pk int heads; /* number of heads */
134 1.1 pk int seccyl; /* sectors per cylinder */
135 1.1 pk int secsize; /* size code for sectors */
136 1.1 pk int datalen; /* data len when secsize = 0 */
137 1.1 pk int steprate; /* step rate and head unload time */
138 1.1 pk int gap1; /* gap len between sectors */
139 1.1 pk int gap2; /* formatting gap */
140 1.1 pk int tracks; /* total num of tracks */
141 1.1 pk int size; /* size of disk in sectors */
142 1.1 pk int step; /* steps per cylinder */
143 1.1 pk int rate; /* transfer speed code */
144 1.1 pk char *name;
145 1.1 pk };
146 1.1 pk
147 1.1 pk /* The order of entries in the following table is important -- BEWARE! */
148 1.1 pk struct fd_type fd_types[] = {
149 1.1 pk { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB" }, /* 1.44MB diskette */
150 1.1 pk { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,"1.2MB" }, /* 1.2 MB AT-diskettes */
151 1.1 pk { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,"360KB/AT" }, /* 360kB in 1.2MB drive */
152 1.1 pk { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,"360KB/PC" }, /* 360kB PC diskettes */
153 1.1 pk { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,"720KB" }, /* 3.5" 720kB diskette */
154 1.1 pk { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,"720KB/x" }, /* 720kB in 1.2MB drive */
155 1.1 pk { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,"360KB/x" }, /* 360kB in 720kB drive */
156 1.1 pk };
157 1.1 pk
158 1.1 pk /* software state, per disk (with up to 4 disks per ctlr) */
159 1.1 pk struct fd_softc {
160 1.1 pk struct dkdevice sc_dk;
161 1.1 pk
162 1.1 pk struct fd_type *sc_deftype; /* default type descriptor */
163 1.1 pk struct fd_type *sc_type; /* current type descriptor */
164 1.1 pk
165 1.1 pk daddr_t sc_blkno; /* starting block number */
166 1.1 pk int sc_bcount; /* byte count left */
167 1.1 pk int sc_skip; /* bytes already transferred */
168 1.1 pk int sc_nblks; /* number of blocks currently tranferring */
169 1.1 pk int sc_nbytes; /* number of bytes currently tranferring */
170 1.1 pk
171 1.1 pk int sc_drive; /* physical unit number */
172 1.1 pk int sc_flags;
173 1.1 pk #define FD_OPEN 0x01 /* it's open */
174 1.1 pk #define FD_MOTOR 0x02 /* motor should be on */
175 1.1 pk #define FD_MOTOR_WAIT 0x04 /* motor coming up */
176 1.1 pk int sc_cylin; /* where we think the head is */
177 1.1 pk
178 1.1 pk TAILQ_ENTRY(fd_softc) sc_drivechain;
179 1.1 pk int sc_ops; /* I/O ops since last switch */
180 1.1 pk struct buf sc_q; /* head of buf chain */
181 1.1 pk };
182 1.1 pk
183 1.1 pk /* floppy driver configuration */
184 1.1 pk int fdmatch __P((struct device *, void *, void *));
185 1.1 pk void fdattach __P((struct device *, struct device *, void *));
186 1.1 pk
187 1.1 pk struct cfdriver fdcd = {
188 1.1 pk NULL, "fd", fdmatch, fdattach, DV_DISK, sizeof(struct fd_softc)
189 1.1 pk };
190 1.1 pk
191 1.1 pk void fdgetdisklabel __P((struct fd_softc *));
192 1.1 pk int fd_get_parms __P((struct fd_softc *));
193 1.1 pk void fdstrategy __P((struct buf *));
194 1.1 pk void fdstart __P((struct fd_softc *));
195 1.1 pk
196 1.1 pk struct dkdriver fddkdriver = { fdstrategy };
197 1.1 pk
198 1.1 pk struct fd_type *fd_nvtotype __P((char *, int, int));
199 1.1 pk void fd_set_motor __P((struct fdc_softc *fdc, int reset));
200 1.1 pk void fd_motor_off __P((void *arg));
201 1.1 pk void fd_motor_on __P((void *arg));
202 1.1 pk int fdcresult __P((struct fdc_softc *fdc));
203 1.1 pk int out_fdc __P((struct fdc_softc *fdc, u_char x));
204 1.1 pk void fdcstart __P((struct fdc_softc *fdc));
205 1.1 pk void fdcstatus __P((struct device *dv, int n, char *s));
206 1.1 pk void fdctimeout __P((void *arg));
207 1.1 pk void fdcpseudointr __P((void *arg));
208 1.1 pk int fdchwintr __P((struct fdc_softc *));
209 1.1 pk int fdcswintr __P((struct fdc_softc *));
210 1.1 pk void fdcretry __P((struct fdc_softc *fdc));
211 1.1 pk void fdfinish __P((struct fd_softc *fd, struct buf *bp));
212 1.1 pk
213 1.1 pk #define PIL_SOFTFLOP 4 /* XXX - to psl.h */
214 1.1 pk #define IE_FDSOFT IE_L4
215 1.1 pk
216 1.1 pk int
217 1.1 pk fdcmatch(parent, match, aux)
218 1.1 pk struct device *parent;
219 1.1 pk void *match, *aux;
220 1.1 pk {
221 1.1 pk struct cfdata *cf = match;
222 1.1 pk register struct confargs *ca = aux;
223 1.1 pk register struct romaux *ra = &ca->ca_ra;
224 1.1 pk
225 1.1 pk /* Sun PROMs call the controller an "fd" */
226 1.1 pk if (strcmp("fd", ra->ra_name))
227 1.1 pk return (0);
228 1.1 pk if (ca->ca_bustype == BUS_MAIN)
229 1.1 pk return (1);
230 1.1 pk
231 1.1 pk return (0);
232 1.1 pk }
233 1.1 pk
234 1.1 pk /*
235 1.1 pk * Arguments passed between fdcattach and fdprobe.
236 1.1 pk */
237 1.1 pk struct fdc_attach_args {
238 1.1 pk int fa_drive;
239 1.1 pk struct fd_type *fa_deftype;
240 1.1 pk };
241 1.1 pk
242 1.1 pk /*
243 1.1 pk * Print the location of a disk drive (called just before attaching the
244 1.1 pk * the drive). If `fdc' is not NULL, the drive was found but was not
245 1.1 pk * in the system config file; print the drive name as well.
246 1.1 pk * Return QUIET (config_find ignores this if the device was configured) to
247 1.1 pk * avoid printing `fdN not configured' messages.
248 1.1 pk */
249 1.1 pk int
250 1.1 pk fdprint(aux, fdc)
251 1.1 pk void *aux;
252 1.1 pk char *fdc;
253 1.1 pk {
254 1.1 pk register struct fdc_attach_args *fa = aux;
255 1.1 pk
256 1.1 pk if (!fdc)
257 1.1 pk printf(" drive %d", fa->fa_drive);
258 1.1 pk return QUIET;
259 1.1 pk }
260 1.1 pk
261 1.1 pk static void
262 1.1 pk fdconf(fdc)
263 1.1 pk struct fdc_softc *fdc;
264 1.1 pk {
265 1.1 pk int vroom;
266 1.1 pk
267 1.1 pk if (out_fdc(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
268 1.1 pk return;
269 1.1 pk
270 1.1 pk /*
271 1.1 pk * dumpreg[7] seems to be a motor-off timeout; set it to whatever
272 1.1 pk * the PROM thinks is appropriate.
273 1.1 pk */
274 1.1 pk if ((vroom = fdc->sc_status[7]) == 0)
275 1.1 pk vroom = 0x64;
276 1.1 pk
277 1.1 pk /* Configure controller to use FIFO and Implied Seek */
278 1.1 pk out_fdc(fdc, NE7CMD_CFG);
279 1.1 pk out_fdc(fdc, vroom);
280 1.1 pk /* Note: CFG_EFIFO is active-low */
281 1.1 pk out_fdc(fdc, CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|fdc->sc_threshold);
282 1.1 pk out_fdc(fdc, 0); /* PRETRK */
283 1.1 pk /* No result phase */
284 1.1 pk }
285 1.1 pk
286 1.1 pk void
287 1.1 pk fdcattach(parent, self, aux)
288 1.1 pk struct device *parent, *self;
289 1.1 pk void *aux;
290 1.1 pk {
291 1.1 pk register struct confargs *ca = aux;
292 1.1 pk struct fdc_softc *fdc = (void *)self;
293 1.1 pk struct fdc_attach_args fa;
294 1.1 pk int n, pri;
295 1.1 pk
296 1.1 pk if (ca->ca_ra.ra_vaddr)
297 1.1 pk fdc->sc_reg = (caddr_t)ca->ca_ra.ra_vaddr;
298 1.1 pk else
299 1.1 pk fdc->sc_reg = (caddr_t)mapiodev(ca->ca_ra.ra_paddr,
300 1.1 pk ca->ca_ra.ra_len,
301 1.1 pk ca->ca_bustype);
302 1.1 pk
303 1.1 pk if (cputyp == CPU_SUN4M) {
304 1.1 pk fdc->sc_reg_msr = &((struct fdreg_sun4m *)fdc->sc_reg)->fd_msr;
305 1.1 pk fdc->sc_reg_data = &((struct fdreg_sun4m *)fdc->sc_reg)->fd_data;
306 1.1 pk fdc->sc_reg_dor = &((struct fdreg_sun4m *)fdc->sc_reg)->fd_dor;
307 1.1 pk } else {
308 1.1 pk fdc->sc_reg_msr = &((struct fdreg_sun4c *)fdc->sc_reg)->fd_msr;
309 1.1 pk fdc->sc_reg_data = &((struct fdreg_sun4c *)fdc->sc_reg)->fd_data;
310 1.1 pk }
311 1.1 pk
312 1.1 pk pri = ca->ca_ra.ra_intr[0].int_pri;
313 1.1 pk fdc->sc_hih.ih_fun = (void *)fdchwintr;
314 1.1 pk fdc->sc_hih.ih_arg = fdc;
315 1.1 pk intr_establish(pri, &fdc->sc_hih);
316 1.1 pk fdc->sc_sih.ih_fun = (void *)fdcswintr;
317 1.1 pk fdc->sc_sih.ih_arg = fdc;
318 1.1 pk intr_establish(PIL_SOFTFLOP, &fdc->sc_sih);
319 1.1 pk
320 1.1 pk fdc->sc_state = DEVIDLE;
321 1.1 pk TAILQ_INIT(&fdc->sc_drives);
322 1.1 pk
323 1.1 pk if (out_fdc(fdc, NE7CMD_VERSION))
324 1.1 pk return;
325 1.1 pk n = fdcresult(fdc);
326 1.1 pk if (n == 1 && fdc->sc_status[0] == 0x90) {
327 1.1 pk fdc->sc_flags |= FDC_82077;
328 1.1 pk if (cputyp != CPU_SUN4M)
329 1.1 pk printf(" Hmmm.. ");
330 1.1 pk } else {
331 1.1 pk /* Not a 82077 */
332 1.1 pk if (cputyp != CPU_SUN4C)
333 1.1 pk printf(" Hmmm.. ");
334 1.1 pk }
335 1.1 pk
336 1.1 pk /* Configure controller; set FIFO threshold */
337 1.1 pk fdc->sc_threshold = 15;
338 1.1 pk fdconf(fdc);
339 1.1 pk
340 1.1 pk if (fdc->sc_flags & FDC_82077) {
341 1.1 pk /* Lock configuration across soft resets. */
342 1.1 pk out_fdc(fdc, NE7CMD_LOCK | CFG_LOCK);
343 1.1 pk if (fdcresult(fdc) != 1)
344 1.1 pk printf(" CFGLOCK: unexpected response");
345 1.1 pk }
346 1.1 pk
347 1.1 pk printf(" pri %d, softpri %d: chip %s\n", pri, PIL_SOFTFLOP,
348 1.1 pk (fdc->sc_flags & FDC_82077)?"82077":"82072");
349 1.1 pk
350 1.1 pk /* physical limit: four drives per controller. */
351 1.1 pk for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
352 1.1 pk fa.fa_deftype = NULL; /* unknown */
353 1.1 pk fa.fa_deftype = &fd_types[0]; /* XXX */
354 1.1 pk (void)config_found(self, (void *)&fa, fdprint);
355 1.1 pk }
356 1.1 pk }
357 1.1 pk
358 1.1 pk int
359 1.1 pk fdmatch(parent, match, aux)
360 1.1 pk struct device *parent;
361 1.1 pk void *match, *aux;
362 1.1 pk {
363 1.1 pk struct fdc_softc *fdc = (void *)parent;
364 1.1 pk struct cfdata *cf = match;
365 1.1 pk struct fdc_attach_args *fa = aux;
366 1.1 pk int drive = fa->fa_drive;
367 1.1 pk int n;
368 1.1 pk
369 1.1 pk if (fdc->sc_flags & FDC_82077) {
370 1.1 pk /* select drive and turn on motor */
371 1.1 pk *fdc->sc_reg_dor = drive | FDO_FRST | FDO_MOEN(drive);
372 1.1 pk /* wait for motor to spin up */
373 1.1 pk delay(250000);
374 1.1 pk } else {
375 1.1 pk if (drive > 0)
376 1.1 pk /* XXX - drive 0 always answers */
377 1.1 pk return 0;
378 1.1 pk auxregbisc(AUXIO_FDS, 0);
379 1.1 pk }
380 1.1 pk fdc->sc_flags |= FDC_NEEDSENSEI;
381 1.1 pk fdc->sc_nstat = 0;
382 1.1 pk out_fdc(fdc, NE7CMD_RECAL);
383 1.1 pk out_fdc(fdc, drive);
384 1.1 pk /* wait for recalibrate */
385 1.1 pk for (n = 0; n < 100000; n++) {
386 1.1 pk #if doesnotwork
387 1.1 pk delay(10);
388 1.1 pk if ((*fdc->sc_reg_msr & (NE7_RQM|NE7_DIO|NE7_CB|0x1)) == NE7_RQM) {
389 1.1 pk out_fdc(fdc, NE7CMD_SENSEI);
390 1.1 pk fdcresult(fdc);
391 1.1 pk break;
392 1.1 pk }
393 1.1 pk #else
394 1.1 pk /* Let interrupts in, briefly */
395 1.1 pk /* XXX - possible spurious interrupts from other
396 1.1 pk not-yet-configured devices */
397 1.1 pk int s = splhigh();
398 1.1 pk splx(10<<8);
399 1.1 pk delay(10);
400 1.1 pk splx(s);
401 1.1 pk if (fdc->sc_nstat != 0)
402 1.1 pk break;
403 1.1 pk #endif
404 1.1 pk }
405 1.1 pk fdc->sc_flags &= ~FDC_NEEDSENSEI;
406 1.1 pk n = fdc->sc_nstat;
407 1.1 pk #ifdef FD_DEBUG
408 1.1 pk {
409 1.1 pk int i;
410 1.1 pk printf("fdprobe: status");
411 1.1 pk for (i = 0; i < n; i++)
412 1.1 pk printf(" %x", fdc->sc_status[i]);
413 1.1 pk printf("\n");
414 1.1 pk }
415 1.1 pk #endif
416 1.1 pk if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
417 1.1 pk return 0;
418 1.1 pk /* turn off motor */
419 1.1 pk if (fdc->sc_flags & FDC_82077) {
420 1.1 pk /* select drive and turn on motor */
421 1.1 pk *fdc->sc_reg_dor = FDO_FRST;
422 1.1 pk } else {
423 1.1 pk auxregbisc(0, AUXIO_FDS);
424 1.1 pk }
425 1.1 pk
426 1.1 pk return 1;
427 1.1 pk }
428 1.1 pk
429 1.1 pk /*
430 1.1 pk * Controller is working, and drive responded. Attach it.
431 1.1 pk */
432 1.1 pk void
433 1.1 pk fdattach(parent, self, aux)
434 1.1 pk struct device *parent, *self;
435 1.1 pk void *aux;
436 1.1 pk {
437 1.1 pk struct fdc_softc *fdc = (void *)parent;
438 1.1 pk struct fd_softc *fd = (void *)self;
439 1.1 pk struct fdc_attach_args *fa = aux;
440 1.1 pk struct fd_type *type = fa->fa_deftype;
441 1.1 pk int drive = fa->fa_drive;
442 1.1 pk
443 1.1 pk /* XXX Allow `flags' to override device type? */
444 1.1 pk
445 1.1 pk if (type)
446 1.1 pk printf(": %s %d cyl, %d head, %d sec\n", type->name,
447 1.1 pk type->tracks, type->heads, type->sectrac);
448 1.1 pk else
449 1.1 pk printf(": density unknown\n");
450 1.1 pk
451 1.1 pk fd->sc_cylin = -1;
452 1.1 pk fd->sc_drive = drive;
453 1.1 pk fd->sc_deftype = type;
454 1.1 pk fdc->sc_fd[drive] = fd;
455 1.1 pk fd->sc_dk.dk_driver = &fddkdriver;
456 1.1 pk #if 0
457 1.1 pk /* XXX Need to do some more fiddling with sc_dk. */
458 1.1 pk /* XXX sparc's dk_establish is bogus */
459 1.1 pk dk_establish(&fd->sc_dk, &fd->sc_dk.dk_dev);
460 1.1 pk #endif
461 1.1 pk }
462 1.1 pk
463 1.1 pk inline struct fd_type *
464 1.1 pk fd_dev_to_type(fd, dev)
465 1.1 pk struct fd_softc *fd;
466 1.1 pk dev_t dev;
467 1.1 pk {
468 1.1 pk int type = FDTYPE(dev);
469 1.1 pk
470 1.1 pk if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
471 1.1 pk return NULL;
472 1.1 pk return type ? &fd_types[type - 1] : fd->sc_deftype;
473 1.1 pk }
474 1.1 pk
475 1.1 pk void
476 1.1 pk fdstrategy(bp)
477 1.1 pk register struct buf *bp; /* IO operation to perform */
478 1.1 pk {
479 1.1 pk struct fd_softc *fd;
480 1.1 pk int unit = FDUNIT(bp->b_dev);
481 1.1 pk int sz;
482 1.1 pk int s;
483 1.1 pk
484 1.1 pk /* Valid unit, controller, and request? */
485 1.1 pk if (unit >= fdcd.cd_ndevs ||
486 1.1 pk (fd = fdcd.cd_devs[unit]) == 0 ||
487 1.1 pk bp->b_blkno < 0 ||
488 1.1 pk (bp->b_bcount % FDC_BSIZE) != 0) {
489 1.1 pk bp->b_error = EINVAL;
490 1.1 pk goto bad;
491 1.1 pk }
492 1.1 pk
493 1.1 pk /* If it's a null transfer, return immediately. */
494 1.1 pk if (bp->b_bcount == 0)
495 1.1 pk goto done;
496 1.1 pk
497 1.1 pk sz = howmany(bp->b_bcount, FDC_BSIZE);
498 1.1 pk
499 1.1 pk if (bp->b_blkno + sz > fd->sc_type->size) {
500 1.1 pk sz = fd->sc_type->size - bp->b_blkno;
501 1.1 pk if (sz == 0) {
502 1.1 pk /* If exactly at end of disk, return EOF. */
503 1.1 pk bp->b_resid = bp->b_bcount;
504 1.1 pk goto done;
505 1.1 pk }
506 1.1 pk if (sz < 0) {
507 1.1 pk /* If past end of disk, return EINVAL. */
508 1.1 pk bp->b_error = EINVAL;
509 1.1 pk goto bad;
510 1.1 pk }
511 1.1 pk /* Otherwise, truncate request. */
512 1.1 pk bp->b_bcount = sz << DEV_BSHIFT;
513 1.1 pk }
514 1.1 pk
515 1.1 pk bp->b_cylin = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
516 1.1 pk
517 1.1 pk #ifdef FD_DEBUG
518 1.1 pk printf("fdstrategy: b_blkno %d b_bcount %d blkno %d cylin %d\n",
519 1.1 pk bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylin);
520 1.1 pk #endif
521 1.1 pk
522 1.1 pk /* Queue transfer on drive, activate drive and controller if idle. */
523 1.1 pk s = splbio();
524 1.1 pk disksort(&fd->sc_q, bp);
525 1.1 pk untimeout(fd_motor_off, fd); /* a good idea */
526 1.1 pk if (!fd->sc_q.b_active)
527 1.1 pk fdstart(fd);
528 1.1 pk #ifdef DIAGNOSTIC
529 1.1 pk else {
530 1.1 pk struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
531 1.1 pk if (fdc->sc_state == DEVIDLE) {
532 1.1 pk printf("fdstrategy: controller inactive\n");
533 1.1 pk fdcstart(fdc);
534 1.1 pk }
535 1.1 pk }
536 1.1 pk #endif
537 1.1 pk splx(s);
538 1.1 pk return;
539 1.1 pk
540 1.1 pk bad:
541 1.1 pk bp->b_flags |= B_ERROR;
542 1.1 pk done:
543 1.1 pk /* Toss transfer; we're done early. */
544 1.1 pk biodone(bp);
545 1.1 pk }
546 1.1 pk
547 1.1 pk void
548 1.1 pk fdstart(fd)
549 1.1 pk struct fd_softc *fd;
550 1.1 pk {
551 1.1 pk struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
552 1.1 pk int active = fdc->sc_drives.tqh_first != 0;
553 1.1 pk
554 1.1 pk /* Link into controller queue. */
555 1.1 pk fd->sc_q.b_active = 1;
556 1.1 pk TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
557 1.1 pk
558 1.1 pk /* If controller not already active, start it. */
559 1.1 pk if (!active)
560 1.1 pk fdcstart(fdc);
561 1.1 pk }
562 1.1 pk
563 1.1 pk void
564 1.1 pk fdfinish(fd, bp)
565 1.1 pk struct fd_softc *fd;
566 1.1 pk struct buf *bp;
567 1.1 pk {
568 1.1 pk struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
569 1.1 pk
570 1.1 pk /*
571 1.1 pk * Move this drive to the end of the queue to give others a `fair'
572 1.1 pk * chance. We only force a switch if N operations are completed while
573 1.1 pk * another drive is waiting to be serviced, since there is a long motor
574 1.1 pk * startup delay whenever we switch.
575 1.1 pk */
576 1.1 pk if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
577 1.1 pk fd->sc_ops = 0;
578 1.1 pk TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
579 1.1 pk if (bp->b_actf) {
580 1.1 pk TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
581 1.1 pk } else
582 1.1 pk fd->sc_q.b_active = 0;
583 1.1 pk }
584 1.1 pk bp->b_resid = fd->sc_bcount;
585 1.1 pk fd->sc_skip = 0;
586 1.1 pk fd->sc_q.b_actf = bp->b_actf;
587 1.1 pk biodone(bp);
588 1.1 pk /* turn off motor 5s from now */
589 1.1 pk timeout(fd_motor_off, fd, 5 * hz);
590 1.1 pk fdc->sc_state = DEVIDLE;
591 1.1 pk }
592 1.1 pk
593 1.1 pk void
594 1.1 pk fd_set_motor(fdc, reset)
595 1.1 pk struct fdc_softc *fdc;
596 1.1 pk int reset;
597 1.1 pk {
598 1.1 pk struct fd_softc *fd;
599 1.1 pk u_char status;
600 1.1 pk int n;
601 1.1 pk
602 1.1 pk if (fdc->sc_flags & FDC_82077) {
603 1.1 pk if (fd = fdc->sc_drives.tqh_first)
604 1.1 pk status = fd->sc_drive;
605 1.1 pk else
606 1.1 pk status = 0;
607 1.1 pk if (!reset)
608 1.1 pk status |= FDO_FRST | FDO_FDMAEN;
609 1.1 pk for (n = 0; n < 4; n++)
610 1.1 pk if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
611 1.1 pk status |= FDO_MOEN(n);
612 1.1 pk *fdc->sc_reg_dor = status;
613 1.1 pk } else {
614 1.1 pk int on = 0;
615 1.1 pk
616 1.1 pk for (n = 0; n < 4; n++)
617 1.1 pk if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
618 1.1 pk on = 1;
619 1.1 pk if (on) {
620 1.1 pk auxregbisc(AUXIO_FDS, 0);
621 1.1 pk } else {
622 1.1 pk auxregbisc(0, AUXIO_FDS);
623 1.1 pk }
624 1.1 pk delay(10);
625 1.1 pk if (reset) {
626 1.1 pk *fdc->sc_reg_drs = DRS_RESET;
627 1.1 pk delay(10);
628 1.1 pk *fdc->sc_reg_drs = 0;
629 1.1 pk #ifdef FD_DEBUG
630 1.1 pk printf("fdc reset\n");
631 1.1 pk #endif
632 1.1 pk fdconf(fdc);
633 1.1 pk }
634 1.1 pk
635 1.1 pk }
636 1.1 pk }
637 1.1 pk
638 1.1 pk void
639 1.1 pk fd_motor_off(arg)
640 1.1 pk void *arg;
641 1.1 pk {
642 1.1 pk struct fd_softc *fd = arg;
643 1.1 pk int s;
644 1.1 pk
645 1.1 pk s = splbio();
646 1.1 pk fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
647 1.1 pk fd_set_motor((struct fdc_softc *)fd->sc_dk.dk_dev.dv_parent, 0);
648 1.1 pk splx(s);
649 1.1 pk }
650 1.1 pk
651 1.1 pk void
652 1.1 pk fd_motor_on(arg)
653 1.1 pk void *arg;
654 1.1 pk {
655 1.1 pk struct fd_softc *fd = arg;
656 1.1 pk struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
657 1.1 pk int s;
658 1.1 pk
659 1.1 pk s = splbio();
660 1.1 pk fd->sc_flags &= ~FD_MOTOR_WAIT;
661 1.1 pk if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
662 1.1 pk (void) fdcswintr(fdc);
663 1.1 pk splx(s);
664 1.1 pk }
665 1.1 pk
666 1.1 pk int
667 1.1 pk fdcresult(fdc)
668 1.1 pk struct fdc_softc *fdc;
669 1.1 pk {
670 1.1 pk u_char i;
671 1.1 pk int j = 100000,
672 1.1 pk n = 0;
673 1.1 pk
674 1.1 pk for (; j; j--) {
675 1.1 pk i = *fdc->sc_reg_msr & (NE7_DIO | NE7_RQM | NE7_CB);
676 1.1 pk if (i == NE7_RQM)
677 1.1 pk return (fdc->sc_nstat = n);
678 1.1 pk if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
679 1.1 pk if (n >= sizeof(fdc->sc_status)) {
680 1.1 pk log(LOG_ERR, "fdcresult: overrun\n");
681 1.1 pk return -1;
682 1.1 pk }
683 1.1 pk fdc->sc_status[n++] = *fdc->sc_reg_data;
684 1.1 pk }
685 1.1 pk }
686 1.1 pk log(LOG_ERR, "fdcresult: timeout\n");
687 1.1 pk return (fdc->sc_nstat = -1);
688 1.1 pk }
689 1.1 pk
690 1.1 pk int
691 1.1 pk out_fdc(fdc, x)
692 1.1 pk struct fdc_softc *fdc;
693 1.1 pk u_char x;
694 1.1 pk {
695 1.1 pk int i = 100000;
696 1.1 pk
697 1.1 pk while (((*fdc->sc_reg_msr & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0)
698 1.1 pk delay(1);
699 1.1 pk if (i <= 0)
700 1.1 pk return -1;
701 1.1 pk
702 1.1 pk delay(1);
703 1.1 pk *fdc->sc_reg_data = x;
704 1.1 pk return 0;
705 1.1 pk }
706 1.1 pk
707 1.1 pk int
708 1.1 pk Fdopen(dev, flags)
709 1.1 pk dev_t dev;
710 1.1 pk int flags;
711 1.1 pk {
712 1.1 pk int unit;
713 1.1 pk struct fd_softc *fd;
714 1.1 pk struct fd_type *type;
715 1.1 pk
716 1.1 pk unit = FDUNIT(dev);
717 1.1 pk if (unit >= fdcd.cd_ndevs)
718 1.1 pk return ENXIO;
719 1.1 pk fd = fdcd.cd_devs[unit];
720 1.1 pk if (fd == 0)
721 1.1 pk return ENXIO;
722 1.1 pk type = fd_dev_to_type(fd, dev);
723 1.1 pk if (type == NULL)
724 1.1 pk return ENXIO;
725 1.1 pk
726 1.1 pk if ((fd->sc_flags & FD_OPEN) != 0 &&
727 1.1 pk fd->sc_type != type)
728 1.1 pk return EBUSY;
729 1.1 pk
730 1.1 pk fd->sc_type = type;
731 1.1 pk fd->sc_cylin = -1;
732 1.1 pk fd->sc_flags |= FD_OPEN;
733 1.1 pk
734 1.1 pk return 0;
735 1.1 pk }
736 1.1 pk
737 1.1 pk int
738 1.1 pk Fdclose(dev, flags)
739 1.1 pk dev_t dev;
740 1.1 pk int flags;
741 1.1 pk {
742 1.1 pk struct fd_softc *fd = fdcd.cd_devs[FDUNIT(dev)];
743 1.1 pk
744 1.1 pk fd->sc_flags &= ~FD_OPEN;
745 1.1 pk return 0;
746 1.1 pk }
747 1.1 pk
748 1.1 pk void
749 1.1 pk fdcstart(fdc)
750 1.1 pk struct fdc_softc *fdc;
751 1.1 pk {
752 1.1 pk
753 1.1 pk #ifdef DIAGNOSTIC
754 1.1 pk /* only got here if controller's drive queue was inactive; should
755 1.1 pk be in idle state */
756 1.1 pk if (fdc->sc_state != DEVIDLE) {
757 1.1 pk printf("fdcstart: not idle\n");
758 1.1 pk return;
759 1.1 pk }
760 1.1 pk #endif
761 1.1 pk (void) fdcswintr(fdc);
762 1.1 pk }
763 1.1 pk
764 1.1 pk void
765 1.1 pk fdcstatus(dv, n, s)
766 1.1 pk struct device *dv;
767 1.1 pk int n;
768 1.1 pk char *s;
769 1.1 pk {
770 1.1 pk struct fdc_softc *fdc = (void *)dv->dv_parent;
771 1.1 pk
772 1.1 pk #if 0
773 1.1 pk /*
774 1.1 pk * A 82072 seems to return <invalid command> on
775 1.1 pk * gratuitous Sense Interrupt commands.
776 1.1 pk */
777 1.1 pk if (n == 0 && (fdc->sc_flags & FDC_82077)) {
778 1.1 pk out_fdc(fdc, NE7CMD_SENSEI);
779 1.1 pk (void) fdcresult(fdc);
780 1.1 pk n = 2;
781 1.1 pk }
782 1.1 pk #endif
783 1.1 pk
784 1.1 pk /* Just print last status */
785 1.1 pk n = fdc->sc_nstat;
786 1.1 pk
787 1.1 pk printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
788 1.1 pk
789 1.1 pk switch (n) {
790 1.1 pk case 0:
791 1.1 pk printf("\n");
792 1.1 pk break;
793 1.1 pk case 2:
794 1.1 pk printf(" (st0 %b cyl %d)\n",
795 1.1 pk fdc->sc_status[0], NE7_ST0BITS,
796 1.1 pk fdc->sc_status[1]);
797 1.1 pk break;
798 1.1 pk case 7:
799 1.1 pk printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
800 1.1 pk fdc->sc_status[0], NE7_ST0BITS,
801 1.1 pk fdc->sc_status[1], NE7_ST1BITS,
802 1.1 pk fdc->sc_status[2], NE7_ST2BITS,
803 1.1 pk fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
804 1.1 pk break;
805 1.1 pk #ifdef DIAGNOSTIC
806 1.1 pk default:
807 1.1 pk printf("\nfdcstatus: weird size");
808 1.1 pk break;
809 1.1 pk #endif
810 1.1 pk }
811 1.1 pk }
812 1.1 pk
813 1.1 pk void
814 1.1 pk fdctimeout(arg)
815 1.1 pk void *arg;
816 1.1 pk {
817 1.1 pk struct fdc_softc *fdc = arg;
818 1.1 pk struct fd_softc *fd = fdc->sc_drives.tqh_first;
819 1.1 pk int s;
820 1.1 pk
821 1.1 pk s = splbio();
822 1.1 pk fdcstatus(&fd->sc_dk.dk_dev, 0, "timeout");
823 1.1 pk
824 1.1 pk if (fd->sc_q.b_actf)
825 1.1 pk fdc->sc_state++;
826 1.1 pk else
827 1.1 pk fdc->sc_state = DEVIDLE;
828 1.1 pk
829 1.1 pk (void) fdcswintr(fdc);
830 1.1 pk splx(s);
831 1.1 pk }
832 1.1 pk
833 1.1 pk /*
834 1.1 pk * hardware interrupt entry point: must be converted to `fast'
835 1.1 pk * (in-window) handler.
836 1.1 pk */
837 1.1 pk int
838 1.1 pk fdchwintr(fdc)
839 1.1 pk struct fdc_softc *fdc;
840 1.1 pk {
841 1.1 pk struct fd_softc *fd;
842 1.1 pk struct buf *bp;
843 1.1 pk int read;
844 1.1 pk
845 1.1 pk if (fdc->sc_flags & FDC_NEEDSENSEI) {
846 1.1 pk out_fdc(fdc, NE7CMD_SENSEI);
847 1.1 pk fdcresult(fdc);
848 1.1 pk ienab_bis(IE_FDSOFT);
849 1.1 pk return 1;
850 1.1 pk }
851 1.1 pk
852 1.1 pk /* Is there a drive for the controller to do a transfer with? */
853 1.1 pk fd = fdc->sc_drives.tqh_first;
854 1.1 pk if (fd == NULL || (bp = fd->sc_q.b_actf) == NULL) {
855 1.1 pk printf("fd: stray hard interrupt... ");
856 1.1 pk auxregbisc(AUXIO_FTC, 0);
857 1.1 pk delay(10);
858 1.1 pk auxregbisc(0, AUXIO_FTC|AUXIO_FDS);
859 1.1 pk fdcresult(fdc);
860 1.1 pk return 1;
861 1.1 pk }
862 1.1 pk
863 1.1 pk if (fdc->sc_state != PSEUDODMA) {
864 1.1 pk ienab_bis(IE_FDSOFT);
865 1.1 pk return 1;
866 1.1 pk }
867 1.1 pk
868 1.1 pk read = bp->b_flags & B_READ;
869 1.1 pk for (;;) {
870 1.1 pk register int msr;
871 1.1 pk
872 1.1 pk msr = *fdc->sc_reg_msr;
873 1.1 pk
874 1.1 pk if ((msr & NE7_RQM) == 0)
875 1.1 pk break;
876 1.1 pk
877 1.1 pk if ((msr & NE7_NDM) == 0) {
878 1.1 pk fdcresult(fdc);
879 1.1 pk fdc->sc_state = IOCOMPLETE; /* not really */
880 1.1 pk ienab_bis(IE_FDSOFT);
881 1.1 pk printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
882 1.1 pk break;
883 1.1 pk }
884 1.1 pk
885 1.1 pk if (msr & NE7_DIO) {
886 1.1 pk #ifdef DIAGNOSTIC
887 1.1 pk if (!read)
888 1.1 pk printf("fdxfer: false read\n");
889 1.1 pk #endif
890 1.1 pk *fdc->sc_bufp++ = *fdc->sc_reg_data;
891 1.1 pk } else {
892 1.1 pk #ifdef DIAGNOSTIC
893 1.1 pk if (read)
894 1.1 pk printf("fdxfer: false write\n");
895 1.1 pk #endif
896 1.1 pk *fdc->sc_reg_data = *fdc->sc_bufp++;
897 1.1 pk }
898 1.1 pk if (--fdc->sc_tc == 0) {
899 1.1 pk auxregbisc(AUXIO_FTC, 0);
900 1.1 pk fdc->sc_state = IOCOMPLETE;
901 1.1 pk delay(10);
902 1.1 pk auxregbisc(0, AUXIO_FTC);
903 1.1 pk fdcresult(fdc);
904 1.1 pk ienab_bis(IE_FDSOFT);
905 1.1 pk break;
906 1.1 pk }
907 1.1 pk }
908 1.1 pk return 1;
909 1.1 pk }
910 1.1 pk
911 1.1 pk int
912 1.1 pk fdcswintr(fdc)
913 1.1 pk struct fdc_softc *fdc;
914 1.1 pk {
915 1.1 pk #define st0 fdc->sc_status[0]
916 1.1 pk #define st1 fdc->sc_status[1]
917 1.1 pk #define cyl fdc->sc_status[1]
918 1.1 pk struct fd_softc *fd;
919 1.1 pk struct buf *bp;
920 1.1 pk int read, head, trac, sec, i, s, nblks;
921 1.1 pk struct fd_type *type;
922 1.1 pk
923 1.1 pk loop:
924 1.1 pk /* Is there a drive for the controller to do a transfer with? */
925 1.1 pk fd = fdc->sc_drives.tqh_first;
926 1.1 pk if (fd == NULL) {
927 1.1 pk fdc->sc_state = DEVIDLE;
928 1.1 pk return 0;
929 1.1 pk }
930 1.1 pk
931 1.1 pk /* Is there a transfer to this drive? If not, deactivate drive. */
932 1.1 pk bp = fd->sc_q.b_actf;
933 1.1 pk if (bp == NULL) {
934 1.1 pk fd->sc_ops = 0;
935 1.1 pk TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
936 1.1 pk fd->sc_q.b_active = 0;
937 1.1 pk goto loop;
938 1.1 pk }
939 1.1 pk
940 1.1 pk switch (fdc->sc_state) {
941 1.1 pk case DEVIDLE:
942 1.1 pk fdc->sc_errors = 0;
943 1.1 pk fd->sc_skip = 0;
944 1.1 pk fd->sc_bcount = bp->b_bcount;
945 1.1 pk fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
946 1.1 pk untimeout(fd_motor_off, fd);
947 1.1 pk if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
948 1.1 pk fdc->sc_state = MOTORWAIT;
949 1.1 pk return 1;
950 1.1 pk }
951 1.1 pk if ((fd->sc_flags & FD_MOTOR) == 0) {
952 1.1 pk /* Turn on the motor, being careful about pairing. */
953 1.1 pk struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
954 1.1 pk if (ofd && ofd->sc_flags & FD_MOTOR) {
955 1.1 pk untimeout(fd_motor_off, ofd);
956 1.1 pk ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
957 1.1 pk }
958 1.1 pk fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
959 1.1 pk fd_set_motor(fdc, 0);
960 1.1 pk fdc->sc_state = MOTORWAIT;
961 1.1 pk /* Allow .25s for motor to stabilize. */
962 1.1 pk timeout(fd_motor_on, fd, hz / 4);
963 1.1 pk return 1;
964 1.1 pk }
965 1.1 pk /* Make sure the right drive is selected. */
966 1.1 pk fd_set_motor(fdc, 0);
967 1.1 pk
968 1.1 pk /* fall through */
969 1.1 pk case DOSEEK:
970 1.1 pk doseek:
971 1.1 pk #if 1 /* We use implied seek */
972 1.1 pk fd->sc_cylin = bp->b_cylin;
973 1.1 pk /* Fall through to doio */
974 1.1 pk #else
975 1.1 pk if (fd->sc_cylin == bp->b_cylin)
976 1.1 pk goto doio;
977 1.1 pk
978 1.1 pk out_fdc(fdc, NE7CMD_SPECIFY);/* specify command */
979 1.1 pk out_fdc(fdc, fd->sc_type->steprate);
980 1.1 pk out_fdc(fdc, 6); /* XXX head load time == 6ms */
981 1.1 pk
982 1.1 pk fdc->sc_flags |= FDC_NEEDSENSEI;
983 1.1 pk out_fdc(fdc, NE7CMD_SEEK); /* seek function */
984 1.1 pk out_fdc(fdc, fd->sc_drive); /* drive number */
985 1.1 pk out_fdc(fdc, bp->b_cylin * fd->sc_type->step);
986 1.1 pk
987 1.1 pk fd->sc_cylin = -1;
988 1.1 pk fdc->sc_state = SEEKWAIT;
989 1.1 pk fdc->sc_nstat = 0;
990 1.1 pk timeout(fdctimeout, fdc, 4 * hz);
991 1.1 pk return 1;
992 1.1 pk #endif
993 1.1 pk
994 1.1 pk case DOIO:
995 1.1 pk doio:
996 1.1 pk type = fd->sc_type;
997 1.1 pk sec = fd->sc_blkno % type->seccyl;
998 1.1 pk nblks = type->seccyl - sec;
999 1.1 pk nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1000 1.1 pk nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
1001 1.1 pk fd->sc_nblks = nblks;
1002 1.1 pk fd->sc_nbytes = nblks * FDC_BSIZE;
1003 1.1 pk head = sec / type->sectrac;
1004 1.1 pk sec -= head * type->sectrac;
1005 1.1 pk #ifdef DIAGNOSTIC
1006 1.1 pk {int block;
1007 1.1 pk block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
1008 1.1 pk if (block != fd->sc_blkno) {
1009 1.1 pk printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
1010 1.1 pk #ifdef DDB
1011 1.1 pk Debugger();
1012 1.1 pk #endif
1013 1.1 pk }}
1014 1.1 pk #endif
1015 1.1 pk read = bp->b_flags & B_READ;
1016 1.1 pk
1017 1.1 pk /* Setup for pseudo DMA */
1018 1.1 pk fdc->sc_bufp = bp->b_data + fd->sc_skip;
1019 1.1 pk fdc->sc_tc = fd->sc_nbytes;
1020 1.1 pk
1021 1.1 pk *fdc->sc_reg_drs = type->rate;
1022 1.1 pk #ifdef FD_DEBUG
1023 1.1 pk printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
1024 1.1 pk read ? "read" : "write", fd->sc_drive, fd->sc_cylin, head,
1025 1.1 pk sec, nblks);
1026 1.1 pk #endif
1027 1.1 pk fdc->sc_state = PSEUDODMA;
1028 1.1 pk fdc->sc_nstat = 0;
1029 1.1 pk if (read)
1030 1.1 pk out_fdc(fdc, NE7CMD_READ); /* READ */
1031 1.1 pk else
1032 1.1 pk out_fdc(fdc, NE7CMD_WRITE); /* WRITE */
1033 1.1 pk out_fdc(fdc, (head << 2) | fd->sc_drive);
1034 1.1 pk out_fdc(fdc, fd->sc_cylin); /* track */
1035 1.1 pk out_fdc(fdc, head);
1036 1.1 pk out_fdc(fdc, sec + 1); /* sector +1 */
1037 1.1 pk out_fdc(fdc, type->secsize); /* sector size */
1038 1.1 pk out_fdc(fdc, type->sectrac); /* sectors/track */
1039 1.1 pk out_fdc(fdc, type->gap1); /* gap1 size */
1040 1.1 pk out_fdc(fdc, type->datalen); /* data length */
1041 1.1 pk /* allow 2 seconds for operation */
1042 1.1 pk timeout(fdctimeout, fdc, 2 * hz);
1043 1.1 pk return 1; /* will return later */
1044 1.1 pk
1045 1.1 pk case SEEKWAIT:
1046 1.1 pk untimeout(fdctimeout, fdc);
1047 1.1 pk fdc->sc_state = SEEKCOMPLETE;
1048 1.1 pk #if 0
1049 1.1 pk /* ONLY WORKS WITH EDGE LEVEL INTERRUPTS! */
1050 1.1 pk /* allow 1/50 second for heads to settle */
1051 1.1 pk timeout(fdcpseudointr, fdc, hz / 50);
1052 1.1 pk return 1;
1053 1.1 pk #endif
1054 1.1 pk
1055 1.1 pk case SEEKCOMPLETE:
1056 1.1 pk /* Make sure seek really happened. */
1057 1.1 pk if ((fdc->sc_flags & FDC_NEEDSENSEI) == 0) {
1058 1.1 pk out_fdc(fdc, NE7CMD_SENSEI);
1059 1.1 pk fdcresult(fdc);
1060 1.1 pk }
1061 1.1 pk fdc->sc_flags &= ~FDC_NEEDSENSEI;
1062 1.1 pk if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1063 1.1 pk cyl != bp->b_cylin * fd->sc_type->step) {
1064 1.1 pk #ifdef FD_DEBUG
1065 1.1 pk fdcstatus(&fd->sc_dk.dk_dev, 2, "seek failed");
1066 1.1 pk #endif
1067 1.1 pk fdcretry(fdc);
1068 1.1 pk goto loop;
1069 1.1 pk }
1070 1.1 pk fd->sc_cylin = bp->b_cylin;
1071 1.1 pk goto doio;
1072 1.1 pk
1073 1.1 pk case IOTIMEDOUT:
1074 1.1 pk auxregbisc(AUXIO_FTC, 0);
1075 1.1 pk delay(10);
1076 1.1 pk auxregbisc(0, AUXIO_FTC);
1077 1.1 pk (void)fdcresult(fdc);
1078 1.1 pk case SEEKTIMEDOUT:
1079 1.1 pk case RECALTIMEDOUT:
1080 1.1 pk case RESETTIMEDOUT:
1081 1.1 pk fdc->sc_flags &= ~FDC_NEEDSENSEI;
1082 1.1 pk fdcretry(fdc);
1083 1.1 pk goto loop;
1084 1.1 pk
1085 1.1 pk case IOCOMPLETE: /* IO DONE, post-analyze */
1086 1.1 pk untimeout(fdctimeout, fdc);
1087 1.1 pk if (fdc->sc_nstat != 7 || (st0 & 0xf8) != 0 || st1 != 0) {
1088 1.1 pk #ifdef FD_DEBUG
1089 1.1 pk fdcstatus(&fd->sc_dk.dk_dev, 7, bp->b_flags & B_READ ?
1090 1.1 pk "read failed" : "write failed");
1091 1.1 pk printf("blkno %d nblks %d\n",
1092 1.1 pk fd->sc_blkno, fd->sc_nblks);
1093 1.1 pk #endif
1094 1.1 pk fdcretry(fdc);
1095 1.1 pk goto loop;
1096 1.1 pk }
1097 1.1 pk if (fdc->sc_errors) {
1098 1.1 pk diskerr(bp, "fd", "soft error", LOG_PRINTF,
1099 1.1 pk fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1100 1.1 pk printf("\n");
1101 1.1 pk fdc->sc_errors = 0;
1102 1.1 pk }
1103 1.1 pk fd->sc_blkno += fd->sc_nblks;
1104 1.1 pk fd->sc_skip += fd->sc_nbytes;
1105 1.1 pk fd->sc_bcount -= fd->sc_nbytes;
1106 1.1 pk if (fd->sc_bcount > 0) {
1107 1.1 pk bp->b_cylin = fd->sc_blkno / fd->sc_type->seccyl;
1108 1.1 pk goto doseek;
1109 1.1 pk }
1110 1.1 pk fdfinish(fd, bp);
1111 1.1 pk goto loop;
1112 1.1 pk
1113 1.1 pk case DORESET:
1114 1.1 pk /* try a reset, keep motor on */
1115 1.1 pk fd_set_motor(fdc, 1);
1116 1.1 pk delay(100);
1117 1.1 pk fd_set_motor(fdc, 0);
1118 1.1 pk fdc->sc_state = RESETCOMPLETE;
1119 1.1 pk timeout(fdctimeout, fdc, hz / 2);
1120 1.1 pk return 1; /* will return later */
1121 1.1 pk
1122 1.1 pk case RESETCOMPLETE:
1123 1.1 pk untimeout(fdctimeout, fdc);
1124 1.1 pk /* clear the controller output buffer */
1125 1.1 pk for (i = 0; i < 4; i++) {
1126 1.1 pk out_fdc(fdc, NE7CMD_SENSEI);
1127 1.1 pk (void) fdcresult(fdc);
1128 1.1 pk }
1129 1.1 pk
1130 1.1 pk /* fall through */
1131 1.1 pk case DORECAL:
1132 1.1 pk fdc->sc_state = RECALWAIT;
1133 1.1 pk fdc->sc_flags |= FDC_NEEDSENSEI;
1134 1.1 pk fdc->sc_nstat = 0;
1135 1.1 pk out_fdc(fdc, NE7CMD_RECAL); /* recalibrate function */
1136 1.1 pk out_fdc(fdc, fd->sc_drive);
1137 1.1 pk timeout(fdctimeout, fdc, 5 * hz);
1138 1.1 pk return 1; /* will return later */
1139 1.1 pk
1140 1.1 pk case RECALWAIT:
1141 1.1 pk untimeout(fdctimeout, fdc);
1142 1.1 pk fdc->sc_state = RECALCOMPLETE;
1143 1.1 pk /*
1144 1.1 pk * The i386 version used to wait another couple of ticks
1145 1.1 pk * here to allow the heads to settle.
1146 1.1 pk */
1147 1.1 pk #if 0
1148 1.1 pk /* Must handle interrupt now */
1149 1.1 pk /* allow 1/30 second for heads to settle */
1150 1.1 pk timeout(fdcpseudointr, fdc, hz / 30);
1151 1.1 pk return 1; /* will return later */
1152 1.1 pk #endif
1153 1.1 pk
1154 1.1 pk case RECALCOMPLETE:
1155 1.1 pk if ((fdc->sc_flags & FDC_NEEDSENSEI) == 0) {
1156 1.1 pk out_fdc(fdc, NE7CMD_SENSEI);
1157 1.1 pk fdcresult(fdc);
1158 1.1 pk }
1159 1.1 pk fdc->sc_flags &= ~FDC_NEEDSENSEI;
1160 1.1 pk if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1161 1.1 pk #ifdef FD_DEBUG
1162 1.1 pk fdcstatus(&fd->sc_dk.dk_dev, 2, "recalibrate failed");
1163 1.1 pk #endif
1164 1.1 pk fdcretry(fdc);
1165 1.1 pk goto loop;
1166 1.1 pk }
1167 1.1 pk fd->sc_cylin = 0;
1168 1.1 pk goto doseek;
1169 1.1 pk
1170 1.1 pk case MOTORWAIT:
1171 1.1 pk if (fd->sc_flags & FD_MOTOR_WAIT)
1172 1.1 pk return 1; /* time's not up yet */
1173 1.1 pk goto doseek;
1174 1.1 pk
1175 1.1 pk default:
1176 1.1 pk fdcstatus(&fd->sc_dk.dk_dev, 0, "stray interrupt");
1177 1.1 pk return 1;
1178 1.1 pk }
1179 1.1 pk #ifdef DIAGNOSTIC
1180 1.1 pk panic("fdcintr: impossible");
1181 1.1 pk #endif
1182 1.1 pk #undef st0
1183 1.1 pk #undef st1
1184 1.1 pk #undef cyl
1185 1.1 pk }
1186 1.1 pk
1187 1.1 pk void
1188 1.1 pk fdcretry(fdc)
1189 1.1 pk struct fdc_softc *fdc;
1190 1.1 pk {
1191 1.1 pk struct fd_softc *fd;
1192 1.1 pk struct buf *bp;
1193 1.1 pk
1194 1.1 pk fd = fdc->sc_drives.tqh_first;
1195 1.1 pk bp = fd->sc_q.b_actf;
1196 1.1 pk
1197 1.1 pk switch (fdc->sc_errors) {
1198 1.1 pk case 0:
1199 1.1 pk /* try again */
1200 1.1 pk fdc->sc_state = DOIO; /* was: SEEKCOMPLETE */
1201 1.1 pk break;
1202 1.1 pk
1203 1.1 pk case 1: case 2: case 3:
1204 1.1 pk /* didn't work; try recalibrating */
1205 1.1 pk fdc->sc_state = DORECAL;
1206 1.1 pk break;
1207 1.1 pk
1208 1.1 pk case 4:
1209 1.1 pk /* still no go; reset the bastard */
1210 1.1 pk fdc->sc_state = DORESET;
1211 1.1 pk break;
1212 1.1 pk
1213 1.1 pk default:
1214 1.1 pk diskerr(bp, "fd", "hard error", LOG_PRINTF,
1215 1.1 pk fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1216 1.1 pk printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
1217 1.1 pk fdc->sc_status[0], NE7_ST0BITS,
1218 1.1 pk fdc->sc_status[1], NE7_ST1BITS,
1219 1.1 pk fdc->sc_status[2], NE7_ST2BITS,
1220 1.1 pk fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1221 1.1 pk
1222 1.1 pk bp->b_flags |= B_ERROR;
1223 1.1 pk bp->b_error = EIO;
1224 1.1 pk fdfinish(fd, bp);
1225 1.1 pk }
1226 1.1 pk fdc->sc_errors++;
1227 1.1 pk }
1228 1.1 pk
1229 1.1 pk int
1230 1.1 pk fdsize(dev)
1231 1.1 pk dev_t dev;
1232 1.1 pk {
1233 1.1 pk
1234 1.1 pk /* Swapping to floppies would not make sense. */
1235 1.1 pk return -1;
1236 1.1 pk }
1237 1.1 pk
1238 1.1 pk int
1239 1.1 pk fddump()
1240 1.1 pk {
1241 1.1 pk
1242 1.1 pk /* Not implemented. */
1243 1.1 pk return EINVAL;
1244 1.1 pk }
1245 1.1 pk
1246 1.1 pk int
1247 1.1 pk fdioctl(dev, cmd, addr, flag)
1248 1.1 pk dev_t dev;
1249 1.1 pk u_long cmd;
1250 1.1 pk caddr_t addr;
1251 1.1 pk int flag;
1252 1.1 pk {
1253 1.1 pk struct fd_softc *fd = fdcd.cd_devs[FDUNIT(dev)];
1254 1.1 pk struct disklabel buffer;
1255 1.1 pk int error;
1256 1.1 pk
1257 1.1 pk switch (cmd) {
1258 1.1 pk case DIOCGDINFO:
1259 1.1 pk bzero(&buffer, sizeof(buffer));
1260 1.1 pk
1261 1.1 pk buffer.d_secpercyl = fd->sc_type->seccyl;
1262 1.1 pk buffer.d_type = DTYPE_FLOPPY;
1263 1.1 pk buffer.d_secsize = FDC_BSIZE;
1264 1.1 pk
1265 1.1 pk if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
1266 1.1 pk return EINVAL;
1267 1.1 pk
1268 1.1 pk *(struct disklabel *)addr = buffer;
1269 1.1 pk return 0;
1270 1.1 pk
1271 1.1 pk case DIOCWLABEL:
1272 1.1 pk if ((flag & FWRITE) == 0)
1273 1.1 pk return EBADF;
1274 1.1 pk /* XXX do something */
1275 1.1 pk return 0;
1276 1.1 pk
1277 1.1 pk case DIOCWDINFO:
1278 1.1 pk if ((flag & FWRITE) == 0)
1279 1.1 pk return EBADF;
1280 1.1 pk
1281 1.1 pk error = setdisklabel(&buffer, (struct disklabel *)addr, 0, NULL);
1282 1.1 pk if (error)
1283 1.1 pk return error;
1284 1.1 pk
1285 1.1 pk error = writedisklabel(dev, fdstrategy, &buffer, NULL);
1286 1.1 pk return error;
1287 1.1 pk
1288 1.1 pk case FDIOCEJECT:
1289 1.1 pk auxregbisc(AUXIO_FDS, AUXIO_FEJ);
1290 1.1 pk delay(10);
1291 1.1 pk auxregbisc(AUXIO_FEJ, AUXIO_FDS);
1292 1.1 pk return 0;
1293 1.1 pk #ifdef DEBUG
1294 1.1 pk case _IO('f', 100):
1295 1.1 pk {
1296 1.1 pk int i;
1297 1.1 pk struct fdc_softc *fdc = (struct fdc_softc *)
1298 1.1 pk fd->sc_dk.dk_dev.dv_parent;
1299 1.1 pk
1300 1.1 pk out_fdc(fdc, NE7CMD_DUMPREG);
1301 1.1 pk fdcresult(fdc);
1302 1.1 pk printf("dumpreg(%d regs): <", fdc->sc_nstat);
1303 1.1 pk for (i = 0; i < fdc->sc_nstat; i++)
1304 1.1 pk printf(" %x", fdc->sc_status[i]);
1305 1.1 pk printf(">\n");
1306 1.1 pk }
1307 1.1 pk
1308 1.1 pk return 0;
1309 1.1 pk case _IOW('f', 101, int):
1310 1.1 pk ((struct fdc_softc *)fd->sc_dk.dk_dev.dv_parent)->sc_threshold
1311 1.1 pk = *(int *)addr;
1312 1.1 pk fdconf(fd->sc_dk.dk_dev.dv_parent);
1313 1.1 pk return 0;
1314 1.1 pk case _IO('f', 102):
1315 1.1 pk {
1316 1.1 pk int i;
1317 1.1 pk struct fdc_softc *fdc = (struct fdc_softc *)
1318 1.1 pk fd->sc_dk.dk_dev.dv_parent;
1319 1.1 pk out_fdc(fdc, NE7CMD_SENSEI);
1320 1.1 pk fdcresult(fdc);
1321 1.1 pk printf("sensei(%d regs): <", fdc->sc_nstat);
1322 1.1 pk for (i=0; i< fdc->sc_nstat; i++)
1323 1.1 pk printf(" 0x%x", fdc->sc_status[i]);
1324 1.1 pk }
1325 1.1 pk printf(">\n");
1326 1.1 pk return 0;
1327 1.1 pk #endif
1328 1.1 pk default:
1329 1.1 pk return ENOTTY;
1330 1.1 pk }
1331 1.1 pk
1332 1.1 pk #ifdef DIAGNOSTIC
1333 1.1 pk panic("fdioctl: impossible");
1334 1.1 pk #endif
1335 1.1 pk }
1336