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