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