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