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