fd.c revision 1.12 1 /* $NetBSD: fd.c,v 1.12 2003/01/25 15:43:11 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 %d b_bcount %ld blkno %d cylin %ld sz %d\n",
472 bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder, sz);
473 #endif
474
475 /* Queue transfer on drive, activate drive and controller if idle. */
476 s = splbio();
477 BUFQ_PUT(&fd->sc_q, bp);
478 callout_stop(&fd->sc_motoroff_ch); /* a good idea */
479 if (fd->sc_active == 0)
480 fdstart(fd);
481 #ifdef DIAGNOSTIC
482 else {
483 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
484 if (fdc->sc_state == DEVIDLE) {
485 printf("fdstrategy: controller inactive\n");
486 fdcstart(fdc);
487 }
488 }
489 #endif
490 splx(s);
491 return;
492
493 bad:
494 bp->b_flags |= B_ERROR;
495 done:
496 /* Toss transfer; we're done early. */
497 bp->b_resid = bp->b_bcount;
498 biodone(bp);
499 }
500
501 void
502 fdstart(fd)
503 struct fd_softc *fd;
504 {
505 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
506 int active = fdc->sc_drives.tqh_first != 0;
507
508 /* Link into controller queue. */
509 fd->sc_active = 1;
510 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
511
512 /* If controller not already active, start it. */
513 if (!active)
514 fdcstart(fdc);
515 }
516
517 void
518 fdfinish(fd, bp)
519 struct fd_softc *fd;
520 struct buf *bp;
521 {
522 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
523
524 /*
525 * Move this drive to the end of the queue to give others a `fair'
526 * chance. We only force a switch if N operations are completed while
527 * another drive is waiting to be serviced, since there is a long motor
528 * startup delay whenever we switch.
529 */
530 (void)BUFQ_GET(&fd->sc_q);
531 if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
532 fd->sc_ops = 0;
533 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
534 if (BUFQ_PEEK(&fd->sc_q) != NULL)
535 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
536 else
537 fd->sc_active = 0;
538 }
539 bp->b_resid = fd->sc_bcount;
540 fd->sc_skip = 0;
541 biodone(bp);
542 /* turn off motor 5s from now */
543 callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
544 fdc->sc_state = DEVIDLE;
545 }
546
547 int
548 fdread(dev, uio, flags)
549 dev_t dev;
550 struct uio *uio;
551 int flags;
552 {
553
554 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
555 }
556
557 int
558 fdwrite(dev, uio, flags)
559 dev_t dev;
560 struct uio *uio;
561 int flags;
562 {
563
564 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
565 }
566
567 void
568 fd_set_motor(fdc, reset)
569 struct fdc_softc *fdc;
570 int reset;
571 {
572 struct fd_softc *fd;
573 u_char status;
574 int n;
575
576 if ((fd = fdc->sc_drives.tqh_first) != NULL)
577 status = fd->sc_drive;
578 else
579 status = 0;
580 if (!reset)
581 status |= FDO_FRST | FDO_FDMAEN;
582 for (n = 0; n < 4; n++)
583 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
584 status |= FDO_MOEN(n);
585 bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, FDOUT, status);
586 }
587
588 void
589 fd_motor_off(arg)
590 void *arg;
591 {
592 struct fd_softc *fd = arg;
593 int s;
594
595 s = splbio();
596 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
597 fd_set_motor((struct fdc_softc *)fd->sc_dev.dv_parent, 0);
598 splx(s);
599 }
600
601 void
602 fd_motor_on(arg)
603 void *arg;
604 {
605 struct fd_softc *fd = arg;
606 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
607 int s;
608
609 s = splbio();
610 fd->sc_flags &= ~FD_MOTOR_WAIT;
611 if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
612 (void) fdcintr(fdc);
613 splx(s);
614 }
615
616 int
617 fdcresult(fdc)
618 struct fdc_softc *fdc;
619 {
620 bus_space_tag_t iot = fdc->sc_iot;
621 bus_space_handle_t ioh = fdc->sc_ioh;
622 u_char i;
623 int j = 100000,
624 n = 0;
625
626 for (; j; j--) {
627 i = bus_space_read_1(iot, ioh, FDSTS) &
628 (NE7_DIO | NE7_RQM | NE7_CB);
629 if (i == NE7_RQM)
630 return n;
631 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
632 if (n >= sizeof(fdc->sc_status)) {
633 log(LOG_ERR, "fdcresult: overrun\n");
634 return -1;
635 }
636 fdc->sc_status[n++] =
637 bus_space_read_1(iot, ioh, FDDATA);
638 }
639 delay(10);
640 }
641 log(LOG_ERR, "fdcresult: timeout\n");
642 return -1;
643 }
644
645 int
646 out_fdc(iot, ioh, x)
647 bus_space_tag_t iot;
648 bus_space_handle_t ioh;
649 u_char x;
650 {
651 int i = 100000;
652
653 while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_DIO) && i-- > 0);
654 if (i <= 0)
655 return -1;
656 while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_RQM) == 0 && i-- > 0);
657 if (i <= 0)
658 return -1;
659 bus_space_write_1(iot, ioh, FDDATA, x);
660 return 0;
661 }
662
663 int
664 fdopen(dev, flags, mode, p)
665 dev_t dev;
666 int flags;
667 int mode;
668 struct proc *p;
669 {
670 struct fd_softc *fd;
671 const struct fd_type *type;
672
673 fd = device_lookup(&fd_cd, FDUNIT(dev));
674 if (fd == NULL)
675 return ENXIO;
676
677 type = fd_dev_to_type(fd, dev);
678 if (type == NULL)
679 return ENXIO;
680
681 if ((fd->sc_flags & FD_OPEN) != 0 &&
682 memcmp(fd->sc_type, type, sizeof(*type)))
683 return EBUSY;
684
685 fd->sc_type_copy = *type;
686 fd->sc_type = &fd->sc_type_copy;
687 fd->sc_cylin = -1;
688 fd->sc_flags |= FD_OPEN;
689
690 return 0;
691 }
692
693 int
694 fdclose(dev, flags, mode, p)
695 dev_t dev;
696 int flags;
697 int mode;
698 struct proc *p;
699 {
700 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
701
702 fd->sc_flags &= ~FD_OPEN;
703 return 0;
704 }
705
706 void
707 fdcstart(fdc)
708 struct fdc_softc *fdc;
709 {
710
711 #ifdef DIAGNOSTIC
712 /* only got here if controller's drive queue was inactive; should
713 be in idle state */
714 if (fdc->sc_state != DEVIDLE) {
715 printf("fdcstart: not idle\n");
716 return;
717 }
718 #endif
719 (void) fdcintr(fdc);
720 }
721
722 void
723 fdcstatus(dv, n, s)
724 struct device *dv;
725 int n;
726 char *s;
727 {
728 struct fdc_softc *fdc = (void *)dv->dv_parent;
729 char bits[64];
730
731 if (n == 0) {
732 out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
733 (void) fdcresult(fdc);
734 n = 2;
735 }
736
737 printf("%s: %s", dv->dv_xname, s);
738
739 switch (n) {
740 case 0:
741 printf("\n");
742 break;
743 case 2:
744 printf(" (st0 %s cyl %d)\n",
745 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
746 bits, sizeof(bits)), fdc->sc_status[1]);
747 break;
748 case 7:
749 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
750 NE7_ST0BITS, bits, sizeof(bits)));
751 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
752 NE7_ST1BITS, bits, sizeof(bits)));
753 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
754 NE7_ST2BITS, bits, sizeof(bits)));
755 printf(" cyl %d head %d sec %d)\n",
756 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
757 break;
758 #ifdef DIAGNOSTIC
759 default:
760 printf("\nfdcstatus: weird size");
761 break;
762 #endif
763 }
764 }
765
766 void
767 fdctimeout(arg)
768 void *arg;
769 {
770 struct fdc_softc *fdc = arg;
771 struct fd_softc *fd = fdc->sc_drives.tqh_first;
772 int s;
773
774 s = splbio();
775 #ifdef DEBUG
776 log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state);
777 #endif
778 fdcstatus(&fd->sc_dev, 0, "timeout");
779
780 if (BUFQ_PEEK(&fd->sc_q) != NULL)
781 fdc->sc_state++;
782 else
783 fdc->sc_state = DEVIDLE;
784
785 (void) fdcintr(fdc);
786 splx(s);
787 }
788
789 void
790 fdcpseudointr(arg)
791 void *arg;
792 {
793 int s;
794
795 /* Just ensure it has the right spl. */
796 s = splbio();
797 (void) fdcintr(arg);
798 splx(s);
799 }
800
801 int
802 fdcintr(arg)
803 void *arg;
804 {
805 struct fdc_softc *fdc = arg;
806 #define st0 fdc->sc_status[0]
807 #define cyl fdc->sc_status[1]
808 struct fd_softc *fd;
809 struct buf *bp;
810 bus_space_tag_t iot = fdc->sc_iot;
811 bus_space_handle_t ioh = fdc->sc_ioh;
812 int read, head, sec, i, nblks;
813 struct fd_type *type;
814
815 loop:
816 /* Is there a drive for the controller to do a transfer with? */
817 fd = fdc->sc_drives.tqh_first;
818 if (fd == NULL) {
819 fdc->sc_state = DEVIDLE;
820 return 1;
821 }
822
823 /* Is there a transfer to this drive? If not, deactivate drive. */
824 bp = BUFQ_PEEK(&fd->sc_q);
825 if (bp == NULL) {
826 fd->sc_ops = 0;
827 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
828 fd->sc_active = 0;
829 goto loop;
830 }
831
832 switch (fdc->sc_state) {
833 case DEVIDLE:
834 fdc->sc_errors = 0;
835 fd->sc_skip = 0;
836 fd->sc_bcount = bp->b_bcount;
837 fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
838 callout_stop(&fd->sc_motoroff_ch);
839 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
840 fdc->sc_state = MOTORWAIT;
841 return 1;
842 }
843 if ((fd->sc_flags & FD_MOTOR) == 0) {
844 /* Turn on the motor, being careful about pairing. */
845 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
846 if (ofd && ofd->sc_flags & FD_MOTOR) {
847 callout_stop(&ofd->sc_motoroff_ch);
848 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
849 }
850 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
851 fd_set_motor(fdc, 0);
852 fdc->sc_state = MOTORWAIT;
853 /* Allow .25s for motor to stabilize. */
854 callout_reset(&fd->sc_motoron_ch, hz / 4,
855 fd_motor_on, fd);
856 return 1;
857 }
858 /* Make sure the right drive is selected. */
859 fd_set_motor(fdc, 0);
860
861 /* fall through */
862 case DOSEEK:
863 doseek:
864 if (fd->sc_cylin == bp->b_cylinder)
865 goto doio;
866
867 out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
868 out_fdc(iot, ioh, fd->sc_type->steprate);
869 out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */
870
871 out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */
872 out_fdc(iot, ioh, fd->sc_drive); /* drive number */
873 out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
874
875 fd->sc_cylin = -1;
876 fdc->sc_state = SEEKWAIT;
877
878 fd->sc_dk.dk_seek++;
879 disk_busy(&fd->sc_dk);
880
881 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
882 return 1;
883
884 case DOIO:
885 doio:
886 type = fd->sc_type;
887 sec = fd->sc_blkno % type->seccyl;
888 nblks = type->seccyl - sec;
889 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
890 nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE);
891 fd->sc_nblks = nblks;
892 fd->sc_nbytes = nblks * FDC_BSIZE;
893 head = sec / type->sectrac;
894 sec -= head * type->sectrac;
895 #ifdef DIAGNOSTIC
896 {
897 int block;
898 block = (fd->sc_cylin * type->heads + head) *
899 type->sectrac + sec;
900 if (block != fd->sc_blkno) {
901 printf("fdcintr: block %d != blkno %" PRId64
902 "\n", block, fd->sc_blkno);
903 #ifdef DDB
904 Debugger();
905 #endif
906 }
907 }
908 #endif
909 read = (bp->b_flags & B_READ) != 0;
910 FDCDMA_START(fdc, bp->b_data + fd->sc_skip,
911 fd->sc_nbytes, read);
912 bus_space_write_1(iot, ioh, FDCTL, type->rate);
913 #ifdef FD_DEBUG
914 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
915 read ? "read" : "write", fd->sc_drive, fd->sc_cylin, head,
916 sec, nblks);
917 #endif
918 if (read)
919 out_fdc(iot, ioh, NE7CMD_READ); /* READ */
920 else
921 out_fdc(iot, ioh, NE7CMD_WRITE);/* WRITE */
922 out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
923 out_fdc(iot, ioh, fd->sc_cylin); /* track */
924 out_fdc(iot, ioh, head);
925 out_fdc(iot, ioh, sec + 1); /* sector + 1 */
926 out_fdc(iot, ioh, type->secsize); /* sector size */
927 out_fdc(iot, ioh, type->sectrac); /* sectors/track */
928 out_fdc(iot, ioh, type->gap1); /* gap1 size */
929 out_fdc(iot, ioh, type->datalen); /* data length */
930 fdc->sc_state = IOCOMPLETE;
931
932 disk_busy(&fd->sc_dk);
933
934 /* allow 2 seconds for operation */
935 callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
936 return 1; /* will return later */
937
938 case SEEKWAIT:
939 callout_stop(&fdc->sc_timo_ch);
940 fdc->sc_state = SEEKCOMPLETE;
941 /* allow 1/50 second for heads to settle */
942 callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc);
943 return 1;
944
945 case SEEKCOMPLETE:
946 disk_unbusy(&fd->sc_dk, 0, 0);
947
948 /* Make sure seek really happened. */
949 out_fdc(iot, ioh, NE7CMD_SENSEI);
950 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
951 cyl != bp->b_cylinder * fd->sc_type->step) {
952 #ifdef FD_DEBUG
953 fdcstatus(&fd->sc_dev, 2, "seek failed");
954 #endif
955 fdcretry(fdc);
956 goto loop;
957 }
958 fd->sc_cylin = bp->b_cylinder;
959 goto doio;
960
961 case IOTIMEDOUT:
962 FDCDMA_ABORT(fdc);
963
964 case SEEKTIMEDOUT:
965 case RECALTIMEDOUT:
966 case RESETTIMEDOUT:
967 fdcretry(fdc);
968 goto loop;
969
970 case IOCOMPLETE: /* IO DONE, post-analyze */
971 callout_stop(&fdc->sc_timo_ch);
972
973 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
974 (bp->b_flags & B_READ));
975
976 i = fdcresult(fdc);
977 if (i != 7 || (st0 & 0xf8) != 0) {
978 FDCDMA_ABORT(fdc);
979 #ifdef FD_DEBUG
980 fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
981 "read failed" : "write failed");
982 printf("blkno %d nblks %d\n",
983 fd->sc_blkno, fd->sc_nblks);
984 #endif
985 fdcretry(fdc);
986 goto loop;
987 }
988 FDCDMA_DONE(fdc);
989 if (fdc->sc_errors) {
990 diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
991 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
992 printf("\n");
993 fdc->sc_errors = 0;
994 }
995 fd->sc_blkno += fd->sc_nblks;
996 fd->sc_skip += fd->sc_nbytes;
997 fd->sc_bcount -= fd->sc_nbytes;
998 if (fd->sc_bcount > 0) {
999 bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
1000 goto doseek;
1001 }
1002 fdfinish(fd, bp);
1003 goto loop;
1004
1005 case DORESET:
1006 /* try a reset, keep motor on */
1007 fd_set_motor(fdc, 1);
1008 delay(100);
1009 fd_set_motor(fdc, 0);
1010 fdc->sc_state = RESETCOMPLETE;
1011 callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
1012 return 1; /* will return later */
1013
1014 case RESETCOMPLETE:
1015 callout_stop(&fdc->sc_timo_ch);
1016 /* clear the controller output buffer */
1017 for (i = 0; i < 4; i++) {
1018 out_fdc(iot, ioh, NE7CMD_SENSEI);
1019 (void) fdcresult(fdc);
1020 }
1021
1022 /* fall through */
1023 case DORECAL:
1024 out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
1025 out_fdc(iot, ioh, fd->sc_drive);
1026 fdc->sc_state = RECALWAIT;
1027 callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1028 return 1; /* will return later */
1029
1030 case RECALWAIT:
1031 callout_stop(&fdc->sc_timo_ch);
1032 fdc->sc_state = RECALCOMPLETE;
1033 /* allow 1/30 second for heads to settle */
1034 callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
1035 return 1; /* will return later */
1036
1037 case RECALCOMPLETE:
1038 out_fdc(iot, ioh, NE7CMD_SENSEI);
1039 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1040 #ifdef FD_DEBUG
1041 fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
1042 #endif
1043 fdcretry(fdc);
1044 goto loop;
1045 }
1046 fd->sc_cylin = 0;
1047 goto doseek;
1048
1049 case MOTORWAIT:
1050 if (fd->sc_flags & FD_MOTOR_WAIT)
1051 return 1; /* time's not up yet */
1052 goto doseek;
1053
1054 default:
1055 fdcstatus(&fd->sc_dev, 0, "stray interrupt");
1056 return 1;
1057 }
1058 #ifdef DIAGNOSTIC
1059 panic("fdcintr: impossible");
1060 #endif
1061 #undef st0
1062 #undef cyl
1063 }
1064
1065 void
1066 fdcretry(fdc)
1067 struct fdc_softc *fdc;
1068 {
1069 struct fd_softc *fd;
1070 struct buf *bp;
1071 char bits[64];
1072
1073 fd = fdc->sc_drives.tqh_first;
1074 bp = BUFQ_PEEK(&fd->sc_q);
1075
1076 switch (fdc->sc_errors) {
1077 case 0:
1078 /* try again */
1079 fdc->sc_state = DOSEEK;
1080 break;
1081
1082 case 1: case 2: case 3:
1083 /* didn't work; try recalibrating */
1084 fdc->sc_state = DORECAL;
1085 break;
1086
1087 case 4:
1088 /* still no go; reset the bastard */
1089 fdc->sc_state = DORESET;
1090 break;
1091
1092 default:
1093 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1094 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1095
1096 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
1097 NE7_ST0BITS, bits, sizeof(bits)));
1098 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
1099 NE7_ST1BITS, bits, sizeof(bits)));
1100 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
1101 NE7_ST2BITS, bits, sizeof(bits)));
1102 printf(" cyl %d head %d sec %d)\n",
1103 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1104
1105 bp->b_flags |= B_ERROR;
1106 bp->b_error = EIO;
1107 fdfinish(fd, bp);
1108 }
1109 fdc->sc_errors++;
1110 }
1111
1112 int
1113 fdioctl(dev, cmd, addr, flag, p)
1114 dev_t dev;
1115 u_long cmd;
1116 caddr_t addr;
1117 int flag;
1118 struct proc *p;
1119 {
1120 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
1121 struct disklabel buffer;
1122 int error;
1123
1124 switch (cmd) {
1125 case DIOCGDINFO:
1126 memset(&buffer, 0, sizeof(buffer));
1127
1128 buffer.d_secpercyl = fd->sc_type->seccyl;
1129 buffer.d_type = DTYPE_FLOPPY;
1130 buffer.d_secsize = FDC_BSIZE;
1131
1132 if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
1133 return EINVAL;
1134
1135 *(struct disklabel *)addr = buffer;
1136 return 0;
1137
1138 case DIOCWLABEL:
1139 if ((flag & FWRITE) == 0)
1140 return EBADF;
1141 /* XXX do something */
1142 return 0;
1143
1144 case DIOCWDINFO:
1145 if ((flag & FWRITE) == 0)
1146 return EBADF;
1147
1148 error = setdisklabel(&buffer, (struct disklabel *)addr,
1149 0, NULL);
1150 if (error)
1151 return error;
1152
1153 error = writedisklabel(dev, fdstrategy, &buffer, NULL);
1154 return error;
1155
1156 default:
1157 return ENOTTY;
1158 }
1159
1160 #ifdef DIAGNOSTIC
1161 panic("fdioctl: impossible");
1162 #endif
1163 }
1164
1165 /*
1166 * Mountroot hook: prompt the user to enter the root file system floppy.
1167 */
1168 void
1169 fd_mountroot_hook(dev)
1170 struct device *dev;
1171 {
1172 int c;
1173
1174 printf("Insert filesystem floppy and press return.");
1175 cnpollc(1);
1176 for (;;) {
1177 c = cngetc();
1178 if ((c == '\r') || (c == '\n')) {
1179 printf("\n");
1180 break;
1181 }
1182 }
1183 cnpollc(0);
1184 }
1185