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