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