fd.c revision 1.5 1 /* $NetBSD: fd.c,v 1.5 2002/09/06 13:18:43 gehenna Exp $ */
2 /* $OpenBSD: fd.c,v 1.6 1998/10/03 21:18:57 millert Exp $ */
3 /* NetBSD: fd.c,v 1.78 1995/07/04 07:23:09 mycroft Exp */
4
5 /*-
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Charles M. Hannum.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 /*-
42 * Copyright (c) 1990 The Regents of the University of California.
43 * All rights reserved.
44 *
45 * This code is derived from software contributed to Berkeley by
46 * Don Ahn.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. All advertising materials mentioning features or use of this software
57 * must display the following acknowledgement:
58 * This product includes software developed by the University of
59 * California, Berkeley and its contributors.
60 * 4. Neither the name of the University nor the names of its contributors
61 * may be used to endorse or promote products derived from this software
62 * without specific prior written permission.
63 *
64 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 * SUCH DAMAGE.
75 *
76 * @(#)fd.c 7.4 (Berkeley) 5/25/91
77 */
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/callout.h>
82 #include <sys/kernel.h>
83 #include <sys/conf.h>
84 #include <sys/file.h>
85 #include <sys/ioctl.h>
86 #include <sys/device.h>
87 #include <sys/disklabel.h>
88 #include <sys/dkstat.h>
89 #include <sys/disk.h>
90 #include <sys/buf.h>
91 #include <sys/uio.h>
92 #include <sys/syslog.h>
93 #include <sys/queue.h>
94
95 #include <uvm/uvm_extern.h>
96
97 #include <dev/cons.h>
98
99 #include <machine/bus.h>
100 #include <machine/cpu.h>
101
102 #include <arc/jazz/fdreg.h>
103 #include <arc/jazz/fdcvar.h>
104
105 #include "locators.h"
106
107 #define FDUNIT(dev) DISKUNIT(dev)
108 #define FDTYPE(dev) DISKPART(dev)
109
110 /* controller driver configuration */
111 int fdprint(void *, const char *);
112
113 /*
114 * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
115 * we tell them apart.
116 */
117 struct fd_type {
118 int sectrac; /* sectors per track */
119 int heads; /* number of heads */
120 int seccyl; /* sectors per cylinder */
121 int secsize; /* size code for sectors */
122 int datalen; /* data len when secsize = 0 */
123 int steprate; /* step rate and head unload time */
124 int gap1; /* gap len between sectors */
125 int gap2; /* formatting gap */
126 int cyls; /* total num of cylinders */
127 int size; /* size of disk in sectors */
128 int step; /* steps per cylinder */
129 int rate; /* transfer speed code */
130 const char *name;
131 };
132
133 /* The order of entries in the following table is important -- BEWARE! */
134 struct fd_type fd_types[] = {
135 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB" }, /* 1.44MB diskette */
136 { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB" }, /* 1.2 MB AT-diskettes */
137 { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" }, /* 360kB in 1.2MB drive */
138 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" }, /* 360kB PC diskettes */
139 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB" }, /* 3.5" 720kB diskette */
140 { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x" }, /* 720kB in 1.2MB drive */
141 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x" }, /* 360kB in 720kB drive */
142 };
143
144 /* software state, per disk (with up to 4 disks per ctlr) */
145 struct fd_softc {
146 struct device sc_dev;
147 struct disk sc_dk;
148
149 const struct fd_type *sc_deftype; /* default type descriptor */
150 struct fd_type *sc_type; /* current type descriptor */
151 struct fd_type sc_type_copy; /* copy for fiddling when formatting */
152
153 struct callout sc_motoron_ch;
154 struct callout sc_motoroff_ch;
155
156 daddr_t sc_blkno; /* starting block number */
157 int sc_bcount; /* byte count left */
158 int sc_opts; /* user-set options */
159 int sc_skip; /* bytes already transferred */
160 int sc_nblks; /* number of blocks currently transferring */
161 int sc_nbytes; /* number of bytes currently transferring */
162
163 int sc_drive; /* physical unit number */
164 int sc_flags;
165 #define FD_OPEN 0x01 /* it's open */
166 #define FD_MOTOR 0x02 /* motor should be on */
167 #define FD_MOTOR_WAIT 0x04 /* motor coming up */
168 int sc_cylin; /* where we think the head is */
169
170 void *sc_sdhook; /* saved shutdown hook for drive. */
171
172 TAILQ_ENTRY(fd_softc) sc_drivechain;
173 int sc_ops; /* I/O ops since last switch */
174 struct bufq_state sc_q; /* pending I/O requests */
175 int sc_active; /* number of active I/O operations */
176 };
177
178 /* floppy driver configuration */
179 int fdprobe __P((struct device *, struct cfdata *, void *));
180 void fdattach __P((struct device *, struct device *, void *));
181
182 extern struct cfdriver fd_cd;
183
184 struct cfattach fd_ca = {
185 sizeof(struct fd_softc), fdprobe, fdattach
186 };
187
188 dev_type_open(fdopen);
189 dev_type_close(fdclose);
190 dev_type_read(fdread);
191 dev_type_write(fdwrite);
192 dev_type_ioctl(fdioctl);
193 dev_type_strategy(fdstrategy);
194
195 const struct bdevsw fd_bdevsw = {
196 fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
197 };
198
199 const struct cdevsw fd_cdevsw = {
200 fdopen, fdclose, fdread, fdwrite, fdioctl,
201 nostop, notty, nopoll, nommap, D_DISK
202 };
203
204 void fdgetdisklabel(struct fd_softc *);
205 int fd_get_parms(struct fd_softc *);
206 void fdstrategy(struct buf *);
207 void fdstart(struct fd_softc *);
208
209 struct dkdriver fddkdriver = { fdstrategy };
210
211 #if 0
212 const struct fd_type *fd_nvtotype(char *, int, int);
213 #endif
214 void fd_set_motor(struct fdc_softc *fdc, int reset);
215 void fd_motor_off(void *arg);
216 void fd_motor_on(void *arg);
217 int fdcresult(struct fdc_softc *fdc);
218 void fdcstart(struct fdc_softc *fdc);
219 void fdcstatus(struct device *dv, int n, char *s);
220 void fdctimeout(void *arg);
221 void fdcpseudointr(void *arg);
222 void fdcretry(struct fdc_softc *fdc);
223 void fdfinish(struct fd_softc *fd, struct buf *bp);
224 __inline const struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
225 void fd_mountroot_hook(struct device *);
226
227 /*
228 * Arguments passed between fdcattach and fdprobe.
229 */
230 struct fdc_attach_args {
231 int fa_drive;
232 const struct fd_type *fa_deftype;
233 };
234
235 /*
236 * Print the location of a disk drive (called just before attaching the
237 * the drive). If `fdc' is not NULL, the drive was found but was not
238 * in the system config file; print the drive name as well.
239 * Return QUIET (config_find ignores this if the device was configured) to
240 * avoid printing `fdN not configured' messages.
241 */
242 int
243 fdprint(aux, fdc)
244 void *aux;
245 const char *fdc;
246 {
247 register struct fdc_attach_args *fa = aux;
248
249 if (!fdc)
250 printf(" drive %d", fa->fa_drive);
251 return QUIET;
252 }
253
254 void
255 fdcattach(fdc)
256 struct fdc_softc *fdc;
257 {
258 struct fdc_attach_args fa;
259 bus_space_tag_t iot;
260 bus_space_handle_t ioh;
261 int type;
262
263 iot = fdc->sc_iot;
264 ioh = fdc->sc_ioh;
265 callout_init(&fdc->sc_timo_ch);
266 callout_init(&fdc->sc_intr_ch);
267
268 fdc->sc_state = DEVIDLE;
269 TAILQ_INIT(&fdc->sc_drives);
270
271 /*
272 * No way yet to determine default disk types.
273 * we assume 1.44 3.5" type for the moment.
274 */
275 type = 0;
276
277 /* physical limit: two drives per controller. */
278 for (fa.fa_drive = 0; fa.fa_drive < 2; fa.fa_drive++) {
279 fa.fa_deftype = &fd_types[type];
280 (void)config_found(&fdc->sc_dev, (void *)&fa, fdprint);
281 }
282 }
283
284 int
285 fdprobe(parent, match, aux)
286 struct device *parent;
287 struct cfdata *match;
288 void *aux;
289 {
290 struct fdc_softc *fdc = (void *)parent;
291 struct cfdata *cf = match;
292 struct fdc_attach_args *fa = aux;
293 int drive = fa->fa_drive;
294 bus_space_tag_t iot = fdc->sc_iot;
295 bus_space_handle_t ioh = fdc->sc_ioh;
296 int n;
297
298 if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
299 cf->cf_loc[FDCCF_DRIVE] != drive)
300 return 0;
301
302 /* select drive and turn on motor */
303 bus_space_write_1(iot, ioh, FDOUT, drive | FDO_FRST | FDO_MOEN(drive));
304 /* wait for motor to spin up */
305 delay(250000);
306 out_fdc(iot, ioh, NE7CMD_RECAL);
307 out_fdc(iot, ioh, drive);
308 /* wait for recalibrate */
309 delay(2000000);
310 out_fdc(iot, ioh, NE7CMD_SENSEI);
311 n = fdcresult(fdc);
312 #ifdef FD_DEBUG
313 {
314 int i;
315 printf("fdprobe: status");
316 for (i = 0; i < n; i++)
317 printf(" %x", fdc->sc_status[i]);
318 printf("\n");
319 }
320 #endif
321 if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
322 return 0;
323 /* turn off motor */
324 bus_space_write_1(iot, ioh, FDOUT, FDO_FRST);
325
326 return 1;
327 }
328
329 /*
330 * Controller is working, and drive responded. Attach it.
331 */
332 void
333 fdattach(parent, self, aux)
334 struct device *parent, *self;
335 void *aux;
336 {
337 struct fdc_softc *fdc = (void *)parent;
338 struct fd_softc *fd = (void *)self;
339 struct fdc_attach_args *fa = aux;
340 const struct fd_type *type = fa->fa_deftype;
341 int drive = fa->fa_drive;
342
343 callout_init(&fd->sc_motoron_ch);
344 callout_init(&fd->sc_motoroff_ch);
345
346 /* XXX Allow `flags' to override device type? */
347
348 if (type)
349 printf(": %s, %d cyl, %d head, %d sec\n", type->name,
350 type->cyls, type->heads, type->sectrac);
351 else
352 printf(": density unknown\n");
353
354 bufq_alloc(&fd->sc_q, BUFQ_DISKSORT|BUFQ_SORT_CYLINDER);
355 fd->sc_cylin = -1;
356 fd->sc_drive = drive;
357 fd->sc_deftype = type;
358 fdc->sc_fd[drive] = fd;
359
360 /*
361 * Initialize and attach the disk structure.
362 */
363 fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
364 fd->sc_dk.dk_driver = &fddkdriver;
365 disk_attach(&fd->sc_dk);
366
367 /* Establish a mountroot hook. */
368 mountroothook_establish(fd_mountroot_hook, &fd->sc_dev);
369
370 /* Needed to power off if the motor is on when we halt. */
371 fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
372 }
373
374 #if 0
375 /*
376 * Translate nvram type into internal data structure. Return NULL for
377 * none/unknown/unusable.
378 */
379 const struct fd_type *
380 fd_nvtotype(fdc, nvraminfo, drive)
381 char *fdc;
382 int nvraminfo, drive;
383 {
384 int type;
385
386 type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
387 #if 0
388 switch (type) {
389 case NVRAM_DISKETTE_NONE:
390 return NULL;
391 case NVRAM_DISKETTE_12M:
392 return &fd_types[1];
393 case NVRAM_DISKETTE_TYPE5:
394 case NVRAM_DISKETTE_TYPE6:
395 /* XXX We really ought to handle 2.88MB format. */
396 case NVRAM_DISKETTE_144M:
397 return &fd_types[0];
398 case NVRAM_DISKETTE_360K:
399 return &fd_types[3];
400 case NVRAM_DISKETTE_720K:
401 return &fd_types[4];
402 default:
403 printf("%s: drive %d: unknown device type 0x%x\n",
404 fdc, drive, type);
405 return NULL;
406 }
407 #else
408 return &fd_types[0]; /* Use only 1.44 for now */
409 #endif
410 }
411 #endif
412
413 __inline const struct fd_type *
414 fd_dev_to_type(fd, dev)
415 struct fd_softc *fd;
416 dev_t dev;
417 {
418 int type = FDTYPE(dev);
419
420 if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
421 return NULL;
422 return type ? &fd_types[type - 1] : fd->sc_deftype;
423 }
424
425 void
426 fdstrategy(bp)
427 register struct buf *bp; /* IO operation to perform */
428 {
429 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(bp->b_dev));
430 int sz;
431 int s;
432
433 /* Valid unit, controller, and request? */
434 if (bp->b_blkno < 0 ||
435 (bp->b_bcount % FDC_BSIZE) != 0) {
436 bp->b_error = EINVAL;
437 goto bad;
438 }
439
440 /* If it's a null transfer, return immediately. */
441 if (bp->b_bcount == 0)
442 goto done;
443
444 sz = howmany(bp->b_bcount, FDC_BSIZE);
445
446 if (bp->b_blkno + sz > fd->sc_type->size) {
447 sz = fd->sc_type->size - bp->b_blkno;
448 if (sz == 0) {
449 /* If exactly at end of disk, return EOF. */
450 goto done;
451 }
452 if (sz < 0) {
453 /* If past end of disk, return EINVAL. */
454 bp->b_error = EINVAL;
455 goto bad;
456 }
457 /* Otherwise, truncate request. */
458 bp->b_bcount = sz << DEV_BSHIFT;
459 }
460
461 bp->b_rawblkno = bp->b_blkno;
462 bp->b_cylinder =
463 bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
464
465 #ifdef FD_DEBUG
466 printf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld sz %d\n",
467 bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder, sz);
468 #endif
469
470 /* Queue transfer on drive, activate drive and controller if idle. */
471 s = splbio();
472 BUFQ_PUT(&fd->sc_q, bp);
473 callout_stop(&fd->sc_motoroff_ch); /* a good idea */
474 if (fd->sc_active == 0)
475 fdstart(fd);
476 #ifdef DIAGNOSTIC
477 else {
478 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
479 if (fdc->sc_state == DEVIDLE) {
480 printf("fdstrategy: controller inactive\n");
481 fdcstart(fdc);
482 }
483 }
484 #endif
485 splx(s);
486 return;
487
488 bad:
489 bp->b_flags |= B_ERROR;
490 done:
491 /* Toss transfer; we're done early. */
492 bp->b_resid = bp->b_bcount;
493 biodone(bp);
494 }
495
496 void
497 fdstart(fd)
498 struct fd_softc *fd;
499 {
500 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
501 int active = fdc->sc_drives.tqh_first != 0;
502
503 /* Link into controller queue. */
504 fd->sc_active = 1;
505 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
506
507 /* If controller not already active, start it. */
508 if (!active)
509 fdcstart(fdc);
510 }
511
512 void
513 fdfinish(fd, bp)
514 struct fd_softc *fd;
515 struct buf *bp;
516 {
517 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
518
519 /*
520 * Move this drive to the end of the queue to give others a `fair'
521 * chance. We only force a switch if N operations are completed while
522 * another drive is waiting to be serviced, since there is a long motor
523 * startup delay whenever we switch.
524 */
525 (void)BUFQ_GET(&fd->sc_q);
526 if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
527 fd->sc_ops = 0;
528 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
529 if (BUFQ_PEEK(&fd->sc_q) != NULL)
530 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
531 else
532 fd->sc_active = 0;
533 }
534 bp->b_resid = fd->sc_bcount;
535 fd->sc_skip = 0;
536 biodone(bp);
537 /* turn off motor 5s from now */
538 callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
539 fdc->sc_state = DEVIDLE;
540 }
541
542 int
543 fdread(dev, uio, flags)
544 dev_t dev;
545 struct uio *uio;
546 int flags;
547 {
548
549 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
550 }
551
552 int
553 fdwrite(dev, uio, flags)
554 dev_t dev;
555 struct uio *uio;
556 int flags;
557 {
558
559 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
560 }
561
562 void
563 fd_set_motor(fdc, reset)
564 struct fdc_softc *fdc;
565 int reset;
566 {
567 struct fd_softc *fd;
568 u_char status;
569 int n;
570
571 if ((fd = fdc->sc_drives.tqh_first) != NULL)
572 status = fd->sc_drive;
573 else
574 status = 0;
575 if (!reset)
576 status |= FDO_FRST | FDO_FDMAEN;
577 for (n = 0; n < 4; n++)
578 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
579 status |= FDO_MOEN(n);
580 bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, FDOUT, status);
581 }
582
583 void
584 fd_motor_off(arg)
585 void *arg;
586 {
587 struct fd_softc *fd = arg;
588 int s;
589
590 s = splbio();
591 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
592 fd_set_motor((struct fdc_softc *)fd->sc_dev.dv_parent, 0);
593 splx(s);
594 }
595
596 void
597 fd_motor_on(arg)
598 void *arg;
599 {
600 struct fd_softc *fd = arg;
601 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
602 int s;
603
604 s = splbio();
605 fd->sc_flags &= ~FD_MOTOR_WAIT;
606 if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
607 (void) fdcintr(fdc);
608 splx(s);
609 }
610
611 int
612 fdcresult(fdc)
613 struct fdc_softc *fdc;
614 {
615 bus_space_tag_t iot = fdc->sc_iot;
616 bus_space_handle_t ioh = fdc->sc_ioh;
617 u_char i;
618 int j = 100000,
619 n = 0;
620
621 for (; j; j--) {
622 i = bus_space_read_1(iot, ioh, FDSTS) &
623 (NE7_DIO | NE7_RQM | NE7_CB);
624 if (i == NE7_RQM)
625 return n;
626 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
627 if (n >= sizeof(fdc->sc_status)) {
628 log(LOG_ERR, "fdcresult: overrun\n");
629 return -1;
630 }
631 fdc->sc_status[n++] =
632 bus_space_read_1(iot, ioh, FDDATA);
633 }
634 delay(10);
635 }
636 log(LOG_ERR, "fdcresult: timeout\n");
637 return -1;
638 }
639
640 int
641 out_fdc(iot, ioh, x)
642 bus_space_tag_t iot;
643 bus_space_handle_t ioh;
644 u_char x;
645 {
646 int i = 100000;
647
648 while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_DIO) && i-- > 0);
649 if (i <= 0)
650 return -1;
651 while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_RQM) == 0 && i-- > 0);
652 if (i <= 0)
653 return -1;
654 bus_space_write_1(iot, ioh, FDDATA, x);
655 return 0;
656 }
657
658 int
659 fdopen(dev, flags, mode, p)
660 dev_t dev;
661 int flags;
662 int mode;
663 struct proc *p;
664 {
665 struct fd_softc *fd;
666 const struct fd_type *type;
667
668 fd = device_lookup(&fd_cd, FDUNIT(dev));
669 if (fd == NULL)
670 return ENXIO;
671
672 type = fd_dev_to_type(fd, dev);
673 if (type == NULL)
674 return ENXIO;
675
676 if ((fd->sc_flags & FD_OPEN) != 0 &&
677 memcmp(fd->sc_type, type, sizeof(*type)))
678 return EBUSY;
679
680 fd->sc_type_copy = *type;
681 fd->sc_type = &fd->sc_type_copy;
682 fd->sc_cylin = -1;
683 fd->sc_flags |= FD_OPEN;
684
685 return 0;
686 }
687
688 int
689 fdclose(dev, flags, mode, p)
690 dev_t dev;
691 int flags;
692 int mode;
693 struct proc *p;
694 {
695 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
696
697 fd->sc_flags &= ~FD_OPEN;
698 return 0;
699 }
700
701 void
702 fdcstart(fdc)
703 struct fdc_softc *fdc;
704 {
705
706 #ifdef DIAGNOSTIC
707 /* only got here if controller's drive queue was inactive; should
708 be in idle state */
709 if (fdc->sc_state != DEVIDLE) {
710 printf("fdcstart: not idle\n");
711 return;
712 }
713 #endif
714 (void) fdcintr(fdc);
715 }
716
717 void
718 fdcstatus(dv, n, s)
719 struct device *dv;
720 int n;
721 char *s;
722 {
723 struct fdc_softc *fdc = (void *)dv->dv_parent;
724 char bits[64];
725
726 if (n == 0) {
727 out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
728 (void) fdcresult(fdc);
729 n = 2;
730 }
731
732 printf("%s: %s", dv->dv_xname, s);
733
734 switch (n) {
735 case 0:
736 printf("\n");
737 break;
738 case 2:
739 printf(" (st0 %s cyl %d)\n",
740 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
741 bits, sizeof(bits)), fdc->sc_status[1]);
742 break;
743 case 7:
744 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
745 NE7_ST0BITS, bits, sizeof(bits)));
746 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
747 NE7_ST1BITS, bits, sizeof(bits)));
748 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
749 NE7_ST2BITS, bits, sizeof(bits)));
750 printf(" cyl %d head %d sec %d)\n",
751 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
752 break;
753 #ifdef DIAGNOSTIC
754 default:
755 printf("\nfdcstatus: weird size");
756 break;
757 #endif
758 }
759 }
760
761 void
762 fdctimeout(arg)
763 void *arg;
764 {
765 struct fdc_softc *fdc = arg;
766 struct fd_softc *fd = fdc->sc_drives.tqh_first;
767 int s;
768
769 s = splbio();
770 #ifdef DEBUG
771 log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state);
772 #endif
773 fdcstatus(&fd->sc_dev, 0, "timeout");
774
775 if (BUFQ_PEEK(&fd->sc_q) != NULL)
776 fdc->sc_state++;
777 else
778 fdc->sc_state = DEVIDLE;
779
780 (void) fdcintr(fdc);
781 splx(s);
782 }
783
784 void
785 fdcpseudointr(arg)
786 void *arg;
787 {
788 int s;
789
790 /* Just ensure it has the right spl. */
791 s = splbio();
792 (void) fdcintr(arg);
793 splx(s);
794 }
795
796 int
797 fdcintr(arg)
798 void *arg;
799 {
800 struct fdc_softc *fdc = arg;
801 #define st0 fdc->sc_status[0]
802 #define cyl fdc->sc_status[1]
803 struct fd_softc *fd;
804 struct buf *bp;
805 bus_space_tag_t iot = fdc->sc_iot;
806 bus_space_handle_t ioh = fdc->sc_ioh;
807 int read, head, sec, i, nblks;
808 struct fd_type *type;
809
810 loop:
811 /* Is there a drive for the controller to do a transfer with? */
812 fd = fdc->sc_drives.tqh_first;
813 if (fd == NULL) {
814 fdc->sc_state = DEVIDLE;
815 return 1;
816 }
817
818 /* Is there a transfer to this drive? If not, deactivate drive. */
819 bp = BUFQ_PEEK(&fd->sc_q);
820 if (bp == NULL) {
821 fd->sc_ops = 0;
822 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
823 fd->sc_active = 0;
824 goto loop;
825 }
826
827 switch (fdc->sc_state) {
828 case DEVIDLE:
829 fdc->sc_errors = 0;
830 fd->sc_skip = 0;
831 fd->sc_bcount = bp->b_bcount;
832 fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
833 callout_stop(&fd->sc_motoroff_ch);
834 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
835 fdc->sc_state = MOTORWAIT;
836 return 1;
837 }
838 if ((fd->sc_flags & FD_MOTOR) == 0) {
839 /* Turn on the motor, being careful about pairing. */
840 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
841 if (ofd && ofd->sc_flags & FD_MOTOR) {
842 callout_stop(&ofd->sc_motoroff_ch);
843 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
844 }
845 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
846 fd_set_motor(fdc, 0);
847 fdc->sc_state = MOTORWAIT;
848 /* Allow .25s for motor to stabilize. */
849 callout_reset(&fd->sc_motoron_ch, hz / 4,
850 fd_motor_on, fd);
851 return 1;
852 }
853 /* Make sure the right drive is selected. */
854 fd_set_motor(fdc, 0);
855
856 /* fall through */
857 case DOSEEK:
858 doseek:
859 if (fd->sc_cylin == bp->b_cylinder)
860 goto doio;
861
862 out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
863 out_fdc(iot, ioh, fd->sc_type->steprate);
864 out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */
865
866 out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */
867 out_fdc(iot, ioh, fd->sc_drive); /* drive number */
868 out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
869
870 fd->sc_cylin = -1;
871 fdc->sc_state = SEEKWAIT;
872
873 fd->sc_dk.dk_seek++;
874 disk_busy(&fd->sc_dk);
875
876 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
877 return 1;
878
879 case DOIO:
880 doio:
881 type = fd->sc_type;
882 sec = fd->sc_blkno % type->seccyl;
883 nblks = type->seccyl - sec;
884 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
885 nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE);
886 fd->sc_nblks = nblks;
887 fd->sc_nbytes = nblks * FDC_BSIZE;
888 head = sec / type->sectrac;
889 sec -= head * type->sectrac;
890 #ifdef DIAGNOSTIC
891 {
892 int block;
893 block = (fd->sc_cylin * type->heads + head) *
894 type->sectrac + sec;
895 if (block != fd->sc_blkno) {
896 printf("fdcintr: block %d != blkno %d\n",
897 block, fd->sc_blkno);
898 #ifdef DDB
899 Debugger();
900 #endif
901 }
902 }
903 #endif
904 read = (bp->b_flags & B_READ) != 0;
905 FDCDMA_START(fdc, bp->b_data + fd->sc_skip,
906 fd->sc_nbytes, read);
907 bus_space_write_1(iot, ioh, FDCTL, type->rate);
908 #ifdef FD_DEBUG
909 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
910 read ? "read" : "write", fd->sc_drive, fd->sc_cylin, head,
911 sec, nblks);
912 #endif
913 if (read)
914 out_fdc(iot, ioh, NE7CMD_READ); /* READ */
915 else
916 out_fdc(iot, ioh, NE7CMD_WRITE);/* WRITE */
917 out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
918 out_fdc(iot, ioh, fd->sc_cylin); /* track */
919 out_fdc(iot, ioh, head);
920 out_fdc(iot, ioh, sec + 1); /* sector + 1 */
921 out_fdc(iot, ioh, type->secsize); /* sector size */
922 out_fdc(iot, ioh, type->sectrac); /* sectors/track */
923 out_fdc(iot, ioh, type->gap1); /* gap1 size */
924 out_fdc(iot, ioh, type->datalen); /* data length */
925 fdc->sc_state = IOCOMPLETE;
926
927 disk_busy(&fd->sc_dk);
928
929 /* allow 2 seconds for operation */
930 callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
931 return 1; /* will return later */
932
933 case SEEKWAIT:
934 callout_stop(&fdc->sc_timo_ch);
935 fdc->sc_state = SEEKCOMPLETE;
936 /* allow 1/50 second for heads to settle */
937 callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc);
938 return 1;
939
940 case SEEKCOMPLETE:
941 disk_unbusy(&fd->sc_dk, 0);
942
943 /* Make sure seek really happened. */
944 out_fdc(iot, ioh, NE7CMD_SENSEI);
945 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
946 cyl != bp->b_cylinder * fd->sc_type->step) {
947 #ifdef FD_DEBUG
948 fdcstatus(&fd->sc_dev, 2, "seek failed");
949 #endif
950 fdcretry(fdc);
951 goto loop;
952 }
953 fd->sc_cylin = bp->b_cylinder;
954 goto doio;
955
956 case IOTIMEDOUT:
957 FDCDMA_ABORT(fdc);
958
959 case SEEKTIMEDOUT:
960 case RECALTIMEDOUT:
961 case RESETTIMEDOUT:
962 fdcretry(fdc);
963 goto loop;
964
965 case IOCOMPLETE: /* IO DONE, post-analyze */
966 callout_stop(&fdc->sc_timo_ch);
967
968 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
969
970 i = fdcresult(fdc);
971 if (i != 7 || (st0 & 0xf8) != 0) {
972 FDCDMA_ABORT(fdc);
973 #ifdef FD_DEBUG
974 fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
975 "read failed" : "write failed");
976 printf("blkno %d nblks %d\n",
977 fd->sc_blkno, fd->sc_nblks);
978 #endif
979 fdcretry(fdc);
980 goto loop;
981 }
982 FDCDMA_DONE(fdc);
983 if (fdc->sc_errors) {
984 diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
985 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
986 printf("\n");
987 fdc->sc_errors = 0;
988 }
989 fd->sc_blkno += fd->sc_nblks;
990 fd->sc_skip += fd->sc_nbytes;
991 fd->sc_bcount -= fd->sc_nbytes;
992 if (fd->sc_bcount > 0) {
993 bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
994 goto doseek;
995 }
996 fdfinish(fd, bp);
997 goto loop;
998
999 case DORESET:
1000 /* try a reset, keep motor on */
1001 fd_set_motor(fdc, 1);
1002 delay(100);
1003 fd_set_motor(fdc, 0);
1004 fdc->sc_state = RESETCOMPLETE;
1005 callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
1006 return 1; /* will return later */
1007
1008 case RESETCOMPLETE:
1009 callout_stop(&fdc->sc_timo_ch);
1010 /* clear the controller output buffer */
1011 for (i = 0; i < 4; i++) {
1012 out_fdc(iot, ioh, NE7CMD_SENSEI);
1013 (void) fdcresult(fdc);
1014 }
1015
1016 /* fall through */
1017 case DORECAL:
1018 out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
1019 out_fdc(iot, ioh, fd->sc_drive);
1020 fdc->sc_state = RECALWAIT;
1021 callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1022 return 1; /* will return later */
1023
1024 case RECALWAIT:
1025 callout_stop(&fdc->sc_timo_ch);
1026 fdc->sc_state = RECALCOMPLETE;
1027 /* allow 1/30 second for heads to settle */
1028 callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
1029 return 1; /* will return later */
1030
1031 case RECALCOMPLETE:
1032 out_fdc(iot, ioh, NE7CMD_SENSEI);
1033 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1034 #ifdef FD_DEBUG
1035 fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
1036 #endif
1037 fdcretry(fdc);
1038 goto loop;
1039 }
1040 fd->sc_cylin = 0;
1041 goto doseek;
1042
1043 case MOTORWAIT:
1044 if (fd->sc_flags & FD_MOTOR_WAIT)
1045 return 1; /* time's not up yet */
1046 goto doseek;
1047
1048 default:
1049 fdcstatus(&fd->sc_dev, 0, "stray interrupt");
1050 return 1;
1051 }
1052 #ifdef DIAGNOSTIC
1053 panic("fdcintr: impossible");
1054 #endif
1055 #undef st0
1056 #undef cyl
1057 }
1058
1059 void
1060 fdcretry(fdc)
1061 struct fdc_softc *fdc;
1062 {
1063 struct fd_softc *fd;
1064 struct buf *bp;
1065 char bits[64];
1066
1067 fd = fdc->sc_drives.tqh_first;
1068 bp = BUFQ_PEEK(&fd->sc_q);
1069
1070 switch (fdc->sc_errors) {
1071 case 0:
1072 /* try again */
1073 fdc->sc_state = DOSEEK;
1074 break;
1075
1076 case 1: case 2: case 3:
1077 /* didn't work; try recalibrating */
1078 fdc->sc_state = DORECAL;
1079 break;
1080
1081 case 4:
1082 /* still no go; reset the bastard */
1083 fdc->sc_state = DORESET;
1084 break;
1085
1086 default:
1087 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1088 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1089
1090 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
1091 NE7_ST0BITS, bits, sizeof(bits)));
1092 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
1093 NE7_ST1BITS, bits, sizeof(bits)));
1094 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
1095 NE7_ST2BITS, bits, sizeof(bits)));
1096 printf(" cyl %d head %d sec %d)\n",
1097 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1098
1099 bp->b_flags |= B_ERROR;
1100 bp->b_error = EIO;
1101 fdfinish(fd, bp);
1102 }
1103 fdc->sc_errors++;
1104 }
1105
1106 int
1107 fdioctl(dev, cmd, addr, flag, p)
1108 dev_t dev;
1109 u_long cmd;
1110 caddr_t addr;
1111 int flag;
1112 struct proc *p;
1113 {
1114 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
1115 struct disklabel buffer;
1116 int error;
1117
1118 switch (cmd) {
1119 case DIOCGDINFO:
1120 memset(&buffer, 0, sizeof(buffer));
1121
1122 buffer.d_secpercyl = fd->sc_type->seccyl;
1123 buffer.d_type = DTYPE_FLOPPY;
1124 buffer.d_secsize = FDC_BSIZE;
1125
1126 if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
1127 return EINVAL;
1128
1129 *(struct disklabel *)addr = buffer;
1130 return 0;
1131
1132 case DIOCWLABEL:
1133 if ((flag & FWRITE) == 0)
1134 return EBADF;
1135 /* XXX do something */
1136 return 0;
1137
1138 case DIOCWDINFO:
1139 if ((flag & FWRITE) == 0)
1140 return EBADF;
1141
1142 error = setdisklabel(&buffer, (struct disklabel *)addr,
1143 0, NULL);
1144 if (error)
1145 return error;
1146
1147 error = writedisklabel(dev, fdstrategy, &buffer, NULL);
1148 return error;
1149
1150 default:
1151 return ENOTTY;
1152 }
1153
1154 #ifdef DIAGNOSTIC
1155 panic("fdioctl: impossible");
1156 #endif
1157 }
1158
1159 /*
1160 * Mountroot hook: prompt the user to enter the root file system floppy.
1161 */
1162 void
1163 fd_mountroot_hook(dev)
1164 struct device *dev;
1165 {
1166 int c;
1167
1168 printf("Insert filesystem floppy and press return.");
1169 cnpollc(1);
1170 for (;;) {
1171 c = cngetc();
1172 if ((c == '\r') || (c == '\n')) {
1173 printf("\n");
1174 break;
1175 }
1176 }
1177 cnpollc(0);
1178 }
1179