fd.c revision 1.117 1 /* $NetBSD: fd.c,v 1.117 2005/06/04 04:34:12 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1990 The Regents of the University of California.
41 * All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * Don Ahn.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)fd.c 7.4 (Berkeley) 5/25/91
71 */
72
73 /*-
74 * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.
75 *
76 * This code is derived from software contributed to Berkeley by
77 * Don Ahn.
78 *
79 * Redistribution and use in source and binary forms, with or without
80 * modification, are permitted provided that the following conditions
81 * are met:
82 * 1. Redistributions of source code must retain the above copyright
83 * notice, this list of conditions and the following disclaimer.
84 * 2. Redistributions in binary form must reproduce the above copyright
85 * notice, this list of conditions and the following disclaimer in the
86 * documentation and/or other materials provided with the distribution.
87 * 3. All advertising materials mentioning features or use of this software
88 * must display the following acknowledgement:
89 * This product includes software developed by the University of
90 * California, Berkeley and its contributors.
91 * 4. Neither the name of the University nor the names of its contributors
92 * may be used to endorse or promote products derived from this software
93 * without specific prior written permission.
94 *
95 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
96 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
97 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
98 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
99 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
100 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
101 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
103 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
104 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
105 * SUCH DAMAGE.
106 *
107 * @(#)fd.c 7.4 (Berkeley) 5/25/91
108 */
109
110 #include <sys/cdefs.h>
111 __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.117 2005/06/04 04:34:12 tsutsui Exp $");
112
113 #include "opt_ddb.h"
114 #include "opt_md.h"
115
116 #include <sys/param.h>
117 #include <sys/systm.h>
118 #include <sys/callout.h>
119 #include <sys/kernel.h>
120 #include <sys/file.h>
121 #include <sys/ioctl.h>
122 #include <sys/device.h>
123 #include <sys/disklabel.h>
124 #include <sys/disk.h>
125 #include <sys/fdio.h>
126 #include <sys/buf.h>
127 #include <sys/bufq.h>
128 #include <sys/malloc.h>
129 #include <sys/proc.h>
130 #include <sys/uio.h>
131 #include <sys/stat.h>
132 #include <sys/syslog.h>
133 #include <sys/queue.h>
134 #include <sys/conf.h>
135
136 #include <dev/cons.h>
137
138 #include <uvm/uvm_extern.h>
139
140 #include <machine/autoconf.h>
141 #include <machine/intr.h>
142
143 #include <sparc/sparc/auxreg.h>
144 #include <sparc/dev/fdreg.h>
145 #include <sparc/dev/fdvar.h>
146
147 #define FDUNIT(dev) (minor(dev) / 8)
148 #define FDTYPE(dev) (minor(dev) % 8)
149
150 /* XXX misuse a flag to identify format operation */
151 #define B_FORMAT B_XXX
152
153 #define FD_DEBUG
154 #ifdef FD_DEBUG
155 int fdc_debug = 0;
156 #endif
157
158 enum fdc_state {
159 DEVIDLE = 0,
160 MOTORWAIT, /* 1 */
161 DOSEEK, /* 2 */
162 SEEKWAIT, /* 3 */
163 SEEKTIMEDOUT, /* 4 */
164 SEEKCOMPLETE, /* 5 */
165 DOIO, /* 6 */
166 IOCOMPLETE, /* 7 */
167 IOTIMEDOUT, /* 8 */
168 IOCLEANUPWAIT, /* 9 */
169 IOCLEANUPTIMEDOUT,/*10 */
170 DORESET, /* 11 */
171 RESETCOMPLETE, /* 12 */
172 RESETTIMEDOUT, /* 13 */
173 DORECAL, /* 14 */
174 RECALWAIT, /* 15 */
175 RECALTIMEDOUT, /* 16 */
176 RECALCOMPLETE, /* 17 */
177 DODSKCHG, /* 18 */
178 DSKCHGWAIT, /* 19 */
179 DSKCHGTIMEDOUT, /* 20 */
180 };
181
182 /* software state, per controller */
183 struct fdc_softc {
184 struct device sc_dev; /* boilerplate */
185 bus_space_tag_t sc_bustag;
186
187 struct callout sc_timo_ch; /* timeout callout */
188 struct callout sc_intr_ch; /* pseudo-intr callout */
189
190 struct fd_softc *sc_fd[4]; /* pointers to children */
191 TAILQ_HEAD(drivehead, fd_softc) sc_drives;
192 enum fdc_state sc_state;
193 int sc_flags;
194 #define FDC_82077 0x01
195 #define FDC_NEEDHEADSETTLE 0x02
196 #define FDC_EIS 0x04
197 #define FDC_NEEDMOTORWAIT 0x08
198 int sc_errors; /* number of retries so far */
199 int sc_overruns; /* number of DMA overruns */
200 int sc_cfg; /* current configuration */
201 struct fdcio sc_io;
202 #define sc_handle sc_io.fdcio_handle
203 #define sc_reg_msr sc_io.fdcio_reg_msr
204 #define sc_reg_fifo sc_io.fdcio_reg_fifo
205 #define sc_reg_dor sc_io.fdcio_reg_dor
206 #define sc_reg_dir sc_io.fdcio_reg_dir
207 #define sc_reg_drs sc_io.fdcio_reg_msr
208 #define sc_itask sc_io.fdcio_itask
209 #define sc_istatus sc_io.fdcio_istatus
210 #define sc_data sc_io.fdcio_data
211 #define sc_tc sc_io.fdcio_tc
212 #define sc_nstat sc_io.fdcio_nstat
213 #define sc_status sc_io.fdcio_status
214 #define sc_intrcnt sc_io.fdcio_intrcnt
215
216 void *sc_sicookie; /* softintr(9) cookie */
217 };
218
219 extern struct fdcio *fdciop; /* I/O descriptor used in fdintr.s */
220
221 /* controller driver configuration */
222 int fdcmatch_mainbus __P((struct device *, struct cfdata *, void *));
223 int fdcmatch_obio __P((struct device *, struct cfdata *, void *));
224 void fdcattach_mainbus __P((struct device *, struct device *, void *));
225 void fdcattach_obio __P((struct device *, struct device *, void *));
226
227 int fdcattach __P((struct fdc_softc *, int));
228
229 CFATTACH_DECL(fdc_mainbus, sizeof(struct fdc_softc),
230 fdcmatch_mainbus, fdcattach_mainbus, NULL, NULL);
231
232 CFATTACH_DECL(fdc_obio, sizeof(struct fdc_softc),
233 fdcmatch_obio, fdcattach_obio, NULL, NULL);
234
235 __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
236
237 /*
238 * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
239 * we tell them apart.
240 */
241 struct fd_type {
242 int sectrac; /* sectors per track */
243 int heads; /* number of heads */
244 int seccyl; /* sectors per cylinder */
245 int secsize; /* size code for sectors */
246 int datalen; /* data len when secsize = 0 */
247 int steprate; /* step rate and head unload time */
248 int gap1; /* gap len between sectors */
249 int gap2; /* formatting gap */
250 int cylinders; /* total num of cylinders */
251 int size; /* size of disk in sectors */
252 int step; /* steps per cylinder */
253 int rate; /* transfer speed code */
254 int fillbyte; /* format fill byte */
255 int interleave; /* interleave factor (formatting) */
256 const char *name;
257 };
258
259 /* The order of entries in the following table is important -- BEWARE! */
260 struct fd_type fd_types[] = {
261 { 18,2,36,2,0xff,0xcf,0x1b,0x54,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" }, /* 1.44MB diskette */
262 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" }, /* 3.5" 720kB diskette */
263 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x" }, /* 360kB in 720kB drive */
264 { 8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS,0xf6,1, "1.2MB/NEC" } /* 1.2 MB japanese format */
265 };
266
267 /* software state, per disk (with up to 4 disks per ctlr) */
268 struct fd_softc {
269 struct device sc_dv; /* generic device info */
270 struct disk sc_dk; /* generic disk info */
271
272 struct fd_type *sc_deftype; /* default type descriptor */
273 struct fd_type *sc_type; /* current type descriptor */
274
275 struct callout sc_motoron_ch;
276 struct callout sc_motoroff_ch;
277
278 daddr_t sc_blkno; /* starting block number */
279 int sc_bcount; /* byte count left */
280 int sc_skip; /* bytes already transferred */
281 int sc_nblks; /* number of blocks currently transferring */
282 int sc_nbytes; /* number of bytes currently transferring */
283
284 int sc_drive; /* physical unit number */
285 int sc_flags;
286 #define FD_OPEN 0x01 /* it's open */
287 #define FD_MOTOR 0x02 /* motor should be on */
288 #define FD_MOTOR_WAIT 0x04 /* motor coming up */
289 int sc_cylin; /* where we think the head is */
290 int sc_opts; /* user-set options */
291
292 void *sc_sdhook; /* shutdownhook cookie */
293
294 TAILQ_ENTRY(fd_softc) sc_drivechain;
295 int sc_ops; /* I/O ops since last switch */
296 struct bufq_state sc_q; /* pending I/O requests */
297 int sc_active; /* number of active I/O requests */
298 };
299
300 /* floppy driver configuration */
301 int fdmatch __P((struct device *, struct cfdata *, void *));
302 void fdattach __P((struct device *, struct device *, void *));
303
304 CFATTACH_DECL(fd, sizeof(struct fd_softc),
305 fdmatch, fdattach, NULL, NULL);
306
307 extern struct cfdriver fd_cd;
308
309 dev_type_open(fdopen);
310 dev_type_close(fdclose);
311 dev_type_read(fdread);
312 dev_type_write(fdwrite);
313 dev_type_ioctl(fdioctl);
314 dev_type_strategy(fdstrategy);
315
316 const struct bdevsw fd_bdevsw = {
317 fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
318 };
319
320 const struct cdevsw fd_cdevsw = {
321 fdopen, fdclose, fdread, fdwrite, fdioctl,
322 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
323 };
324
325 void fdgetdisklabel __P((dev_t));
326 int fd_get_parms __P((struct fd_softc *));
327 void fdstart __P((struct fd_softc *));
328 int fdprint __P((void *, const char *));
329
330 struct dkdriver fddkdriver = { fdstrategy };
331
332 struct fd_type *fd_nvtotype __P((char *, int, int));
333 void fd_set_motor __P((struct fdc_softc *));
334 void fd_motor_off __P((void *));
335 void fd_motor_on __P((void *));
336 int fdcresult __P((struct fdc_softc *));
337 int fdc_wrfifo __P((struct fdc_softc *, u_char));
338 void fdcstart __P((struct fdc_softc *));
339 void fdcstatus __P((struct fdc_softc *, const char *));
340 void fdc_reset __P((struct fdc_softc *));
341 int fdc_diskchange __P((struct fdc_softc *));
342 void fdctimeout __P((void *));
343 void fdcpseudointr __P((void *));
344 int fdc_c_hwintr __P((void *));
345 void fdchwintr __P((void));
346 void fdcswintr __P((void *));
347 int fdcstate __P((struct fdc_softc *));
348 void fdcretry __P((struct fdc_softc *));
349 void fdfinish __P((struct fd_softc *, struct buf *));
350 int fdformat __P((dev_t, struct ne7_fd_formb *, struct proc *));
351 void fd_do_eject __P((struct fd_softc *));
352 void fd_mountroot_hook __P((struct device *));
353 static int fdconf __P((struct fdc_softc *));
354 static void establish_chip_type __P((
355 struct fdc_softc *,
356 bus_space_tag_t,
357 bus_addr_t,
358 bus_size_t,
359 bus_space_handle_t));
360
361
362 #define OBP_FDNAME (CPU_ISSUN4M ? "SUNW,fdtwo" : "fd")
363
364 int
365 fdcmatch_mainbus(parent, match, aux)
366 struct device *parent;
367 struct cfdata *match;
368 void *aux;
369 {
370 struct mainbus_attach_args *ma = aux;
371
372 /*
373 * Floppy controller is on mainbus on sun4c.
374 */
375 if (!CPU_ISSUN4C)
376 return (0);
377
378 /* sun4c PROMs call the controller "fd" */
379 if (strcmp("fd", ma->ma_name) != 0)
380 return (0);
381
382 return (bus_space_probe(ma->ma_bustag,
383 ma->ma_paddr,
384 1, /* probe size */
385 0, /* offset */
386 0, /* flags */
387 NULL, NULL));
388 }
389
390 int
391 fdcmatch_obio(parent, match, aux)
392 struct device *parent;
393 struct cfdata *match;
394 void *aux;
395 {
396 union obio_attach_args *uoba = aux;
397 struct sbus_attach_args *sa;
398
399 /*
400 * Floppy controller is on obio on sun4m.
401 */
402 if (uoba->uoba_isobio4 != 0)
403 return (0);
404
405 sa = &uoba->uoba_sbus;
406
407 /* sun4m PROMs call the controller "SUNW,fdtwo" */
408 if (strcmp("SUNW,fdtwo", sa->sa_name) != 0)
409 return (0);
410
411 return (bus_space_probe(sa->sa_bustag,
412 sbus_bus_addr(sa->sa_bustag,
413 sa->sa_slot, sa->sa_offset),
414 1, /* probe size */
415 0, /* offset */
416 0, /* flags */
417 NULL, NULL));
418 }
419
420 static void
421 establish_chip_type(fdc, tag, addr, size, handle)
422 struct fdc_softc *fdc;
423 bus_space_tag_t tag;
424 bus_addr_t addr;
425 bus_size_t size;
426 bus_space_handle_t handle;
427 {
428 u_int8_t v;
429
430 /*
431 * This hack from Chris Torek: apparently DOR really
432 * addresses MSR/DRS on a 82072.
433 * We used to rely on the VERSION command to tell the
434 * difference (which did not work).
435 */
436
437 /* First, check the size of the register bank */
438 if (size < 8)
439 /* It isn't a 82077 */
440 return;
441
442 /* Then probe the DOR register offset */
443 if (bus_space_probe(tag, addr,
444 1, /* probe size */
445 FDREG77_DOR, /* offset */
446 0, /* flags */
447 NULL, NULL) == 0) {
448
449 /* It isn't a 82077 */
450 return;
451 }
452
453 v = bus_space_read_1(tag, handle, FDREG77_DOR);
454 if (v == NE7_RQM) {
455 /*
456 * Value in DOR looks like it's really MSR
457 */
458 bus_space_write_1(tag, handle, FDREG77_DOR, FDC_250KBPS);
459 v = bus_space_read_1(tag, handle, FDREG77_DOR);
460 if (v == NE7_RQM) {
461 /*
462 * The value in the DOR didn't stick;
463 * it isn't a 82077
464 */
465 return;
466 }
467 }
468
469 fdc->sc_flags |= FDC_82077;
470 }
471
472 /*
473 * Arguments passed between fdcattach and fdprobe.
474 */
475 struct fdc_attach_args {
476 int fa_drive;
477 struct fd_type *fa_deftype;
478 };
479
480 /*
481 * Print the location of a disk drive (called just before attaching the
482 * the drive). If `fdc' is not NULL, the drive was found but was not
483 * in the system config file; print the drive name as well.
484 * Return QUIET (config_find ignores this if the device was configured) to
485 * avoid printing `fdN not configured' messages.
486 */
487 int
488 fdprint(aux, fdc)
489 void *aux;
490 const char *fdc;
491 {
492 register struct fdc_attach_args *fa = aux;
493
494 if (!fdc)
495 aprint_normal(" drive %d", fa->fa_drive);
496 return (QUIET);
497 }
498
499 /*
500 * Configure several parameters and features on the FDC.
501 * Return 0 on success.
502 */
503 static int
504 fdconf(fdc)
505 struct fdc_softc *fdc;
506 {
507 int vroom;
508
509 if (fdc_wrfifo(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
510 return (-1);
511
512 /*
513 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
514 * the PROM thinks is appropriate.
515 */
516 if ((vroom = fdc->sc_status[7]) == 0)
517 vroom = 0x64;
518
519 /* Configure controller to use FIFO and Implied Seek */
520 if (fdc_wrfifo(fdc, NE7CMD_CFG) != 0)
521 return (-1);
522 if (fdc_wrfifo(fdc, vroom) != 0)
523 return (-1);
524 if (fdc_wrfifo(fdc, fdc->sc_cfg) != 0)
525 return (-1);
526 if (fdc_wrfifo(fdc, 0) != 0) /* PRETRK */
527 return (-1);
528 /* No result phase for the NE7CMD_CFG command */
529
530 if ((fdc->sc_flags & FDC_82077) != 0) {
531 /* Lock configuration across soft resets. */
532 if (fdc_wrfifo(fdc, NE7CMD_LOCK | CFG_LOCK) != 0 ||
533 fdcresult(fdc) != 1) {
534 #ifdef DEBUG
535 printf("fdconf: CFGLOCK failed");
536 #endif
537 return (-1);
538 }
539 }
540
541 return (0);
542 #if 0
543 if (fdc_wrfifo(fdc, NE7CMD_VERSION) == 0 &&
544 fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
545 if (fdc_debug)
546 printf("[version cmd]");
547 }
548 #endif
549 }
550
551 void
552 fdcattach_mainbus(parent, self, aux)
553 struct device *parent, *self;
554 void *aux;
555 {
556 struct fdc_softc *fdc = (void *)self;
557 struct mainbus_attach_args *ma = aux;
558
559 fdc->sc_bustag = ma->ma_bustag;
560
561 if (bus_space_map(
562 ma->ma_bustag,
563 ma->ma_paddr,
564 ma->ma_size,
565 BUS_SPACE_MAP_LINEAR,
566 &fdc->sc_handle) != 0) {
567 printf("%s: cannot map registers\n", self->dv_xname);
568 return;
569 }
570
571 establish_chip_type(fdc,
572 ma->ma_bustag,
573 ma->ma_paddr,
574 ma->ma_size,
575 fdc->sc_handle);
576
577 if (fdcattach(fdc, ma->ma_pri) != 0)
578 bus_space_unmap(ma->ma_bustag, fdc->sc_handle, ma->ma_size);
579 }
580
581 void
582 fdcattach_obio(parent, self, aux)
583 struct device *parent, *self;
584 void *aux;
585 {
586 struct fdc_softc *fdc = (void *)self;
587 union obio_attach_args *uoba = aux;
588 struct sbus_attach_args *sa = &uoba->uoba_sbus;
589
590 if (sa->sa_nintr == 0) {
591 printf(": no interrupt line configured\n");
592 return;
593 }
594
595 fdc->sc_bustag = sa->sa_bustag;
596
597 if (sbus_bus_map(sa->sa_bustag,
598 sa->sa_slot, sa->sa_offset, sa->sa_size,
599 BUS_SPACE_MAP_LINEAR, &fdc->sc_handle) != 0) {
600 printf("%s: cannot map control registers\n",
601 self->dv_xname);
602 return;
603 }
604
605 establish_chip_type(fdc,
606 sa->sa_bustag,
607 sbus_bus_addr(sa->sa_bustag, sa->sa_slot, sa->sa_offset),
608 sa->sa_size,
609 fdc->sc_handle);
610
611 if (strcmp(prom_getpropstring(sa->sa_node, "status"), "disabled") == 0) {
612 printf(": no drives attached\n");
613 return;
614 }
615
616 if (fdcattach(fdc, sa->sa_pri) != 0)
617 bus_space_unmap(sa->sa_bustag, fdc->sc_handle, sa->sa_size);
618 }
619
620 int
621 fdcattach(fdc, pri)
622 struct fdc_softc *fdc;
623 int pri;
624 {
625 struct fdc_attach_args fa;
626 int drive_attached;
627 char code;
628
629 callout_init(&fdc->sc_timo_ch);
630 callout_init(&fdc->sc_intr_ch);
631
632 fdc->sc_state = DEVIDLE;
633 fdc->sc_itask = FDC_ITASK_NONE;
634 fdc->sc_istatus = FDC_ISTATUS_NONE;
635 fdc->sc_flags |= FDC_EIS;
636 TAILQ_INIT(&fdc->sc_drives);
637
638 if ((fdc->sc_flags & FDC_82077) != 0) {
639 fdc->sc_reg_msr = FDREG77_MSR;
640 fdc->sc_reg_fifo = FDREG77_FIFO;
641 fdc->sc_reg_dor = FDREG77_DOR;
642 fdc->sc_reg_dir = FDREG77_DIR;
643 code = '7';
644 fdc->sc_flags |= FDC_NEEDMOTORWAIT;
645 } else {
646 fdc->sc_reg_msr = FDREG72_MSR;
647 fdc->sc_reg_fifo = FDREG72_FIFO;
648 fdc->sc_reg_dor = 0;
649 code = '2';
650 }
651
652 /*
653 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
654 * Note: CFG_EFIFO is active-low, initial threshold value: 8
655 */
656 fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
657 if (fdconf(fdc) != 0) {
658 printf("%s: no drives attached\n", fdc->sc_dev.dv_xname);
659 return (-1);
660 }
661
662 fdciop = &fdc->sc_io;
663 if (bus_intr_establish2(fdc->sc_bustag, pri, 0,
664 fdc_c_hwintr, fdc, fdchwintr) == NULL) {
665 printf("\n%s: cannot register interrupt handler\n",
666 fdc->sc_dev.dv_xname);
667 return (-1);
668 }
669
670 fdc->sc_sicookie = softintr_establish(IPL_BIO, fdcswintr, fdc);
671 if (fdc->sc_sicookie == NULL) {
672 printf("\n%s: cannot register soft interrupt handler\n",
673 fdc->sc_dev.dv_xname);
674 return (-1);
675 }
676 printf(" softpri %d: chip 8207%c\n", IPL_SOFTFDC, code);
677
678 evcnt_attach_dynamic(&fdc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
679 fdc->sc_dev.dv_xname, "intr");
680
681 /* physical limit: four drives per controller. */
682 drive_attached = 0;
683 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
684 fa.fa_deftype = NULL; /* unknown */
685 fa.fa_deftype = &fd_types[0]; /* XXX */
686 if (config_found(&fdc->sc_dev, (void *)&fa, fdprint) != NULL)
687 drive_attached = 1;
688 }
689
690 if (drive_attached == 0) {
691 /* XXX - dis-establish interrupts here */
692 /* return (-1); */
693 }
694
695 return (0);
696 }
697
698 int
699 fdmatch(parent, match, aux)
700 struct device *parent;
701 struct cfdata *match;
702 void *aux;
703 {
704 struct fdc_softc *fdc = (void *)parent;
705 bus_space_tag_t t = fdc->sc_bustag;
706 bus_space_handle_t h = fdc->sc_handle;
707 struct fdc_attach_args *fa = aux;
708 int drive = fa->fa_drive;
709 int n, ok;
710
711 if (drive > 0)
712 /* XXX - for now, punt on more than one drive */
713 return (0);
714
715 if ((fdc->sc_flags & FDC_82077) != 0) {
716 /* select drive and turn on motor */
717 bus_space_write_1(t, h, fdc->sc_reg_dor,
718 drive | FDO_FRST | FDO_MOEN(drive));
719 /* wait for motor to spin up */
720 delay(250000);
721 } else {
722 auxregbisc(AUXIO4C_FDS, 0);
723 }
724 fdc->sc_nstat = 0;
725 fdc_wrfifo(fdc, NE7CMD_RECAL);
726 fdc_wrfifo(fdc, drive);
727
728 /* Wait for recalibration to complete */
729 for (n = 0; n < 10000; n++) {
730 u_int8_t v;
731
732 delay(1000);
733 v = bus_space_read_1(t, h, fdc->sc_reg_msr);
734 if ((v & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
735 /* wait a bit longer till device *really* is ready */
736 delay(100000);
737 if (fdc_wrfifo(fdc, NE7CMD_SENSEI))
738 break;
739 if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
740 /*
741 * Got `invalid command'; we interpret it
742 * to mean that the re-calibrate hasn't in
743 * fact finished yet
744 */
745 continue;
746 break;
747 }
748 }
749 n = fdc->sc_nstat;
750 #ifdef FD_DEBUG
751 if (fdc_debug) {
752 int i;
753 printf("fdprobe: %d stati:", n);
754 for (i = 0; i < n; i++)
755 printf(" 0x%x", fdc->sc_status[i]);
756 printf("\n");
757 }
758 #endif
759 ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
760
761 /* turn off motor */
762 if ((fdc->sc_flags & FDC_82077) != 0) {
763 /* deselect drive and turn motor off */
764 bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
765 } else {
766 auxregbisc(0, AUXIO4C_FDS);
767 }
768
769 return (ok);
770 }
771
772 /*
773 * Controller is working, and drive responded. Attach it.
774 */
775 void
776 fdattach(parent, self, aux)
777 struct device *parent, *self;
778 void *aux;
779 {
780 struct fdc_softc *fdc = (void *)parent;
781 struct fd_softc *fd = (void *)self;
782 struct fdc_attach_args *fa = aux;
783 struct fd_type *type = fa->fa_deftype;
784 int drive = fa->fa_drive;
785
786 callout_init(&fd->sc_motoron_ch);
787 callout_init(&fd->sc_motoroff_ch);
788
789 /* XXX Allow `flags' to override device type? */
790
791 if (type)
792 printf(": %s %d cyl, %d head, %d sec\n", type->name,
793 type->cylinders, type->heads, type->sectrac);
794 else
795 printf(": density unknown\n");
796
797 bufq_alloc(&fd->sc_q, BUFQ_DISKSORT|BUFQ_SORT_CYLINDER);
798 fd->sc_cylin = -1;
799 fd->sc_drive = drive;
800 fd->sc_deftype = type;
801 fdc->sc_fd[drive] = fd;
802
803 fdc_wrfifo(fdc, NE7CMD_SPECIFY);
804 fdc_wrfifo(fdc, type->steprate);
805 /* XXX head load time == 6ms */
806 fdc_wrfifo(fdc, 6 | NE7_SPECIFY_NODMA);
807
808 /*
809 * Initialize and attach the disk structure.
810 */
811 fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
812 fd->sc_dk.dk_driver = &fddkdriver;
813 disk_attach(&fd->sc_dk);
814
815 /*
816 * Establish a mountroot_hook anyway in case we booted
817 * with RB_ASKNAME and get selected as the boot device.
818 */
819 mountroothook_establish(fd_mountroot_hook, &fd->sc_dv);
820
821 /* Make sure the drive motor gets turned off at shutdown time. */
822 fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
823 }
824
825 __inline struct fd_type *
826 fd_dev_to_type(fd, dev)
827 struct fd_softc *fd;
828 dev_t dev;
829 {
830 int type = FDTYPE(dev);
831
832 if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
833 return (NULL);
834 return (type ? &fd_types[type - 1] : fd->sc_deftype);
835 }
836
837 void
838 fdstrategy(bp)
839 register struct buf *bp; /* IO operation to perform */
840 {
841 struct fd_softc *fd;
842 int unit = FDUNIT(bp->b_dev);
843 int sz;
844 int s;
845
846 /* Valid unit, controller, and request? */
847 if (unit >= fd_cd.cd_ndevs ||
848 (fd = fd_cd.cd_devs[unit]) == 0 ||
849 bp->b_blkno < 0 ||
850 (((bp->b_bcount % FD_BSIZE(fd)) != 0 ||
851 (bp->b_blkno * DEV_BSIZE) % FD_BSIZE(fd) != 0) &&
852 (bp->b_flags & B_FORMAT) == 0)) {
853 bp->b_error = EINVAL;
854 goto bad;
855 }
856
857 /* If it's a null transfer, return immediately. */
858 if (bp->b_bcount == 0)
859 goto done;
860
861 sz = howmany(bp->b_bcount, DEV_BSIZE);
862
863 if (bp->b_blkno + sz > (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)) {
864 sz = (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)
865 - bp->b_blkno;
866 if (sz == 0) {
867 /* If exactly at end of disk, return EOF. */
868 bp->b_resid = bp->b_bcount;
869 goto done;
870 }
871 if (sz < 0) {
872 /* If past end of disk, return EINVAL. */
873 bp->b_error = EINVAL;
874 goto bad;
875 }
876 /* Otherwise, truncate request. */
877 bp->b_bcount = sz << DEV_BSHIFT;
878 }
879
880 bp->b_rawblkno = bp->b_blkno;
881 bp->b_cylinder = (bp->b_blkno * DEV_BSIZE) /
882 (FD_BSIZE(fd) * fd->sc_type->seccyl);
883
884 #ifdef FD_DEBUG
885 if (fdc_debug > 1)
886 printf("fdstrategy: b_blkno %lld b_bcount %d blkno %lld cylin %d\n",
887 (long long)bp->b_blkno, bp->b_bcount,
888 (long long)fd->sc_blkno, bp->b_cylinder);
889 #endif
890
891 /* Queue transfer on drive, activate drive and controller if idle. */
892 s = splbio();
893 BUFQ_PUT(&fd->sc_q, bp);
894 callout_stop(&fd->sc_motoroff_ch); /* a good idea */
895 if (fd->sc_active == 0)
896 fdstart(fd);
897 #ifdef DIAGNOSTIC
898 else {
899 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
900 if (fdc->sc_state == DEVIDLE) {
901 printf("fdstrategy: controller inactive\n");
902 fdcstart(fdc);
903 }
904 }
905 #endif
906 splx(s);
907 return;
908
909 bad:
910 bp->b_flags |= B_ERROR;
911 done:
912 /* Toss transfer; we're done early. */
913 biodone(bp);
914 }
915
916 void
917 fdstart(fd)
918 struct fd_softc *fd;
919 {
920 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
921 int active = fdc->sc_drives.tqh_first != 0;
922
923 /* Link into controller queue. */
924 fd->sc_active = 1;
925 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
926
927 /* If controller not already active, start it. */
928 if (!active)
929 fdcstart(fdc);
930 }
931
932 void
933 fdfinish(fd, bp)
934 struct fd_softc *fd;
935 struct buf *bp;
936 {
937 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
938
939 /*
940 * Move this drive to the end of the queue to give others a `fair'
941 * chance. We only force a switch if N operations are completed while
942 * another drive is waiting to be serviced, since there is a long motor
943 * startup delay whenever we switch.
944 */
945 (void)BUFQ_GET(&fd->sc_q);
946 if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
947 fd->sc_ops = 0;
948 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
949 if (BUFQ_PEEK(&fd->sc_q) != NULL) {
950 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
951 } else
952 fd->sc_active = 0;
953 }
954 bp->b_resid = fd->sc_bcount;
955 fd->sc_skip = 0;
956
957 biodone(bp);
958 /* turn off motor 5s from now */
959 callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
960 fdc->sc_state = DEVIDLE;
961 }
962
963 void
964 fdc_reset(fdc)
965 struct fdc_softc *fdc;
966 {
967 bus_space_tag_t t = fdc->sc_bustag;
968 bus_space_handle_t h = fdc->sc_handle;
969
970 if ((fdc->sc_flags & FDC_82077) != 0) {
971 bus_space_write_1(t, h, fdc->sc_reg_dor,
972 FDO_FDMAEN | FDO_MOEN(0));
973 }
974
975 bus_space_write_1(t, h, fdc->sc_reg_drs, DRS_RESET);
976 delay(10);
977 bus_space_write_1(t, h, fdc->sc_reg_drs, 0);
978
979 if ((fdc->sc_flags & FDC_82077) != 0) {
980 bus_space_write_1(t, h, fdc->sc_reg_dor,
981 FDO_FRST | FDO_FDMAEN | FDO_DS);
982 }
983 #ifdef FD_DEBUG
984 if (fdc_debug)
985 printf("fdc reset\n");
986 #endif
987 }
988
989 void
990 fd_set_motor(fdc)
991 struct fdc_softc *fdc;
992 {
993 struct fd_softc *fd;
994 u_char status;
995 int n;
996
997 if ((fdc->sc_flags & FDC_82077) != 0) {
998 status = FDO_FRST | FDO_FDMAEN;
999 if ((fd = fdc->sc_drives.tqh_first) != NULL)
1000 status |= fd->sc_drive;
1001
1002 for (n = 0; n < 4; n++)
1003 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
1004 status |= FDO_MOEN(n);
1005 bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
1006 fdc->sc_reg_dor, status);
1007 } else {
1008
1009 for (n = 0; n < 4; n++) {
1010 if ((fd = fdc->sc_fd[n]) != NULL &&
1011 (fd->sc_flags & FD_MOTOR) != 0) {
1012 auxregbisc(AUXIO4C_FDS, 0);
1013 return;
1014 }
1015 }
1016 auxregbisc(0, AUXIO4C_FDS);
1017 }
1018 }
1019
1020 void
1021 fd_motor_off(arg)
1022 void *arg;
1023 {
1024 struct fd_softc *fd = arg;
1025 int s;
1026
1027 s = splbio();
1028 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1029 fd_set_motor((struct fdc_softc *)fd->sc_dv.dv_parent);
1030 splx(s);
1031 }
1032
1033 void
1034 fd_motor_on(arg)
1035 void *arg;
1036 {
1037 struct fd_softc *fd = arg;
1038 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
1039 int s;
1040
1041 s = splbio();
1042 fd->sc_flags &= ~FD_MOTOR_WAIT;
1043 if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
1044 (void) fdcstate(fdc);
1045 splx(s);
1046 }
1047
1048 /*
1049 * Get status bytes off the FDC after a command has finished
1050 * Returns the number of status bytes read; -1 on error.
1051 * The return value is also stored in `sc_nstat'.
1052 */
1053 int
1054 fdcresult(fdc)
1055 struct fdc_softc *fdc;
1056 {
1057 bus_space_tag_t t = fdc->sc_bustag;
1058 bus_space_handle_t h = fdc->sc_handle;
1059 int j, n = 0;
1060
1061 for (j = 10000; j; j--) {
1062 u_int8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
1063 v &= (NE7_DIO | NE7_RQM | NE7_CB);
1064 if (v == NE7_RQM)
1065 return (fdc->sc_nstat = n);
1066 if (v == (NE7_DIO | NE7_RQM | NE7_CB)) {
1067 if (n >= sizeof(fdc->sc_status)) {
1068 log(LOG_ERR, "fdcresult: overrun\n");
1069 return (-1);
1070 }
1071 fdc->sc_status[n++] =
1072 bus_space_read_1(t, h, fdc->sc_reg_fifo);
1073 } else
1074 delay(1);
1075 }
1076
1077 log(LOG_ERR, "fdcresult: timeout\n");
1078 return (fdc->sc_nstat = -1);
1079 }
1080
1081 /*
1082 * Write a command byte to the FDC.
1083 * Returns 0 on success; -1 on failure (i.e. timeout)
1084 */
1085 int
1086 fdc_wrfifo(fdc, x)
1087 struct fdc_softc *fdc;
1088 u_int8_t x;
1089 {
1090 bus_space_tag_t t = fdc->sc_bustag;
1091 bus_space_handle_t h = fdc->sc_handle;
1092 int i;
1093
1094 for (i = 100000; i-- > 0;) {
1095 u_int8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
1096 if ((v & (NE7_DIO|NE7_RQM)) == NE7_RQM) {
1097 /* The chip is ready */
1098 bus_space_write_1(t, h, fdc->sc_reg_fifo, x);
1099 return (0);
1100 }
1101 delay(1);
1102 }
1103 return (-1);
1104 }
1105
1106 int
1107 fdc_diskchange(fdc)
1108 struct fdc_softc *fdc;
1109 {
1110 if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
1111 bus_space_tag_t t = fdc->sc_bustag;
1112 bus_space_handle_t h = fdc->sc_handle;
1113 u_int8_t v = bus_space_read_1(t, h, fdc->sc_reg_dir);
1114 return ((v & FDI_DCHG) != 0);
1115 } else if (CPU_ISSUN4C) {
1116 return ((*AUXIO4C_REG & AUXIO4C_FDC) != 0);
1117 }
1118 return (0);
1119 }
1120
1121 int
1122 fdopen(dev, flags, fmt, p)
1123 dev_t dev;
1124 int flags, fmt;
1125 struct proc *p;
1126 {
1127 int unit, pmask;
1128 struct fd_softc *fd;
1129 struct fd_type *type;
1130
1131 unit = FDUNIT(dev);
1132 if (unit >= fd_cd.cd_ndevs)
1133 return (ENXIO);
1134 fd = fd_cd.cd_devs[unit];
1135 if (fd == NULL)
1136 return (ENXIO);
1137 type = fd_dev_to_type(fd, dev);
1138 if (type == NULL)
1139 return (ENXIO);
1140
1141 if ((fd->sc_flags & FD_OPEN) != 0 &&
1142 fd->sc_type != type)
1143 return (EBUSY);
1144
1145 fd->sc_type = type;
1146 fd->sc_cylin = -1;
1147 fd->sc_flags |= FD_OPEN;
1148
1149 /*
1150 * Only update the disklabel if we're not open anywhere else.
1151 */
1152 if (fd->sc_dk.dk_openmask == 0)
1153 fdgetdisklabel(dev);
1154
1155 pmask = (1 << DISKPART(dev));
1156
1157 switch (fmt) {
1158 case S_IFCHR:
1159 fd->sc_dk.dk_copenmask |= pmask;
1160 break;
1161
1162 case S_IFBLK:
1163 fd->sc_dk.dk_bopenmask |= pmask;
1164 break;
1165 }
1166 fd->sc_dk.dk_openmask =
1167 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
1168
1169 return (0);
1170 }
1171
1172 int
1173 fdclose(dev, flags, fmt, p)
1174 dev_t dev;
1175 int flags, fmt;
1176 struct proc *p;
1177 {
1178 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1179 int pmask = (1 << DISKPART(dev));
1180
1181 fd->sc_flags &= ~FD_OPEN;
1182 fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
1183
1184 switch (fmt) {
1185 case S_IFCHR:
1186 fd->sc_dk.dk_copenmask &= ~pmask;
1187 break;
1188
1189 case S_IFBLK:
1190 fd->sc_dk.dk_bopenmask &= ~pmask;
1191 break;
1192 }
1193 fd->sc_dk.dk_openmask =
1194 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
1195
1196 return (0);
1197 }
1198
1199 int
1200 fdread(dev, uio, flag)
1201 dev_t dev;
1202 struct uio *uio;
1203 int flag;
1204 {
1205
1206 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
1207 }
1208
1209 int
1210 fdwrite(dev, uio, flag)
1211 dev_t dev;
1212 struct uio *uio;
1213 int flag;
1214 {
1215
1216 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
1217 }
1218
1219 void
1220 fdcstart(fdc)
1221 struct fdc_softc *fdc;
1222 {
1223
1224 #ifdef DIAGNOSTIC
1225 /* only got here if controller's drive queue was inactive; should
1226 be in idle state */
1227 if (fdc->sc_state != DEVIDLE) {
1228 printf("fdcstart: not idle\n");
1229 return;
1230 }
1231 #endif
1232 (void) fdcstate(fdc);
1233 }
1234
1235 void
1236 fdcstatus(fdc, s)
1237 struct fdc_softc *fdc;
1238 const char *s;
1239 {
1240 struct fd_softc *fd = fdc->sc_drives.tqh_first;
1241 int n;
1242 char bits[64];
1243
1244 /* Just print last status */
1245 n = fdc->sc_nstat;
1246
1247 #if 0
1248 /*
1249 * A 82072 seems to return <invalid command> on
1250 * gratuitous Sense Interrupt commands.
1251 */
1252 if (n == 0 && (fdc->sc_flags & FDC_82077) != 0) {
1253 fdc_wrfifo(fdc, NE7CMD_SENSEI);
1254 (void) fdcresult(fdc);
1255 n = 2;
1256 }
1257 #endif
1258
1259 printf("%s: %s: state %d",
1260 fd ? fd->sc_dv.dv_xname : "fdc", s, fdc->sc_state);
1261
1262 switch (n) {
1263 case 0:
1264 printf("\n");
1265 break;
1266 case 2:
1267 printf(" (st0 %s cyl %d)\n",
1268 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
1269 bits, sizeof(bits)), fdc->sc_status[1]);
1270 break;
1271 case 7:
1272 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
1273 NE7_ST0BITS, bits, sizeof(bits)));
1274 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
1275 NE7_ST1BITS, bits, sizeof(bits)));
1276 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
1277 NE7_ST2BITS, bits, sizeof(bits)));
1278 printf(" cyl %d head %d sec %d)\n",
1279 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1280 break;
1281 #ifdef DIAGNOSTIC
1282 default:
1283 printf(" fdcstatus: weird size: %d\n", n);
1284 break;
1285 #endif
1286 }
1287 }
1288
1289 void
1290 fdctimeout(arg)
1291 void *arg;
1292 {
1293 struct fdc_softc *fdc = arg;
1294 struct fd_softc *fd;
1295 int s;
1296
1297 s = splbio();
1298 fd = fdc->sc_drives.tqh_first;
1299 if (fd == NULL) {
1300 printf("%s: timeout but no I/O pending: state %d, istatus=%d\n",
1301 fdc->sc_dev.dv_xname,
1302 fdc->sc_state, fdc->sc_istatus);
1303 fdc->sc_state = DEVIDLE;
1304 goto out;
1305 }
1306
1307 if (BUFQ_PEEK(&fd->sc_q) != NULL)
1308 fdc->sc_state++;
1309 else
1310 fdc->sc_state = DEVIDLE;
1311
1312 (void) fdcstate(fdc);
1313 out:
1314 splx(s);
1315
1316 }
1317
1318 void
1319 fdcpseudointr(arg)
1320 void *arg;
1321 {
1322 struct fdc_softc *fdc = arg;
1323 int s;
1324
1325 /* Just ensure it has the right spl. */
1326 s = splbio();
1327 (void) fdcstate(fdc);
1328 splx(s);
1329 }
1330
1331
1332 /*
1333 * hardware interrupt entry point: used only if no `fast trap' * (in-window)
1334 * handler is available. Unfortunately, we have no reliable way to
1335 * determine that the interrupt really came from the floppy controller;
1336 * just hope that the other devices that share this interrupt level
1337 * can do better..
1338 */
1339 int
1340 fdc_c_hwintr(arg)
1341 void *arg;
1342 {
1343 struct fdc_softc *fdc = arg;
1344 bus_space_tag_t t = fdc->sc_bustag;
1345 bus_space_handle_t h = fdc->sc_handle;
1346
1347 switch (fdc->sc_itask) {
1348 case FDC_ITASK_NONE:
1349 return (0);
1350 case FDC_ITASK_SENSEI:
1351 if (fdc_wrfifo(fdc, NE7CMD_SENSEI) != 0 || fdcresult(fdc) == -1)
1352 fdc->sc_istatus = FDC_ISTATUS_ERROR;
1353 else
1354 fdc->sc_istatus = FDC_ISTATUS_DONE;
1355 softintr_schedule(fdc->sc_sicookie);
1356 return (1);
1357 case FDC_ITASK_RESULT:
1358 if (fdcresult(fdc) == -1)
1359 fdc->sc_istatus = FDC_ISTATUS_ERROR;
1360 else
1361 fdc->sc_istatus = FDC_ISTATUS_DONE;
1362 softintr_schedule(fdc->sc_sicookie);
1363 return (1);
1364 case FDC_ITASK_DMA:
1365 /* Proceed with pseudo-DMA below */
1366 break;
1367 default:
1368 printf("fdc: stray hard interrupt: itask=%d\n", fdc->sc_itask);
1369 fdc->sc_istatus = FDC_ISTATUS_SPURIOUS;
1370 softintr_schedule(fdc->sc_sicookie);
1371 return (1);
1372 }
1373
1374 /*
1375 * Pseudo DMA in progress
1376 */
1377 for (;;) {
1378 u_int8_t msr;
1379
1380 msr = bus_space_read_1(t, h, fdc->sc_reg_msr);
1381
1382 if ((msr & NE7_RQM) == 0)
1383 /* That's all this round */
1384 break;
1385
1386 if ((msr & NE7_NDM) == 0) {
1387 fdcresult(fdc);
1388 fdc->sc_istatus = FDC_ISTATUS_DONE;
1389 softintr_schedule(fdc->sc_sicookie);
1390 #ifdef FD_DEBUG
1391 if (fdc_debug > 1)
1392 printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
1393 #endif
1394 break;
1395 }
1396
1397 /* Another byte can be transferred */
1398 if ((msr & NE7_DIO) != 0)
1399 *fdc->sc_data =
1400 bus_space_read_1(t, h, fdc->sc_reg_fifo);
1401 else
1402 bus_space_write_1(t, h, fdc->sc_reg_fifo,
1403 *fdc->sc_data);
1404
1405 fdc->sc_data++;
1406 if (--fdc->sc_tc == 0) {
1407 fdc->sc_istatus = FDC_ISTATUS_DONE;
1408 FTC_FLIP;
1409 fdcresult(fdc);
1410 softintr_schedule(fdc->sc_sicookie);
1411 break;
1412 }
1413 }
1414 return (1);
1415 }
1416
1417 void
1418 fdcswintr(arg)
1419 void *arg;
1420 {
1421 struct fdc_softc *fdc = arg;
1422
1423 if (fdc->sc_istatus == FDC_ISTATUS_NONE)
1424 /* This (software) interrupt is not for us */
1425 return;
1426
1427 switch (fdc->sc_istatus) {
1428 case FDC_ISTATUS_ERROR:
1429 printf("fdc: ierror status: state %d\n", fdc->sc_state);
1430 break;
1431 case FDC_ISTATUS_SPURIOUS:
1432 printf("fdc: spurious interrupt: state %d\n", fdc->sc_state);
1433 break;
1434 }
1435
1436 fdcstate(fdc);
1437 return;
1438 }
1439
1440 int
1441 fdcstate(fdc)
1442 struct fdc_softc *fdc;
1443 {
1444 #define st0 fdc->sc_status[0]
1445 #define st1 fdc->sc_status[1]
1446 #define cyl fdc->sc_status[1]
1447 #define FDC_WRFIFO(fdc, c) do { \
1448 if (fdc_wrfifo(fdc, (c))) { \
1449 goto xxx; \
1450 } \
1451 } while(0)
1452
1453 struct fd_softc *fd;
1454 struct buf *bp;
1455 int read, head, sec, nblks;
1456 struct fd_type *type;
1457 struct ne7_fd_formb *finfo = NULL;
1458
1459 if (fdc->sc_istatus == FDC_ISTATUS_ERROR) {
1460 /* Prevent loop if the reset sequence produces errors */
1461 if (fdc->sc_state != RESETCOMPLETE &&
1462 fdc->sc_state != RECALWAIT &&
1463 fdc->sc_state != RECALCOMPLETE)
1464 fdc->sc_state = DORESET;
1465 }
1466
1467 /* Clear I task/status field */
1468 fdc->sc_istatus = FDC_ISTATUS_NONE;
1469 fdc->sc_itask = FDC_ITASK_NONE;
1470
1471 loop:
1472 /* Is there a drive for the controller to do a transfer with? */
1473 fd = fdc->sc_drives.tqh_first;
1474 if (fd == NULL) {
1475 fdc->sc_state = DEVIDLE;
1476 return (0);
1477 }
1478
1479 /* Is there a transfer to this drive? If not, deactivate drive. */
1480 bp = BUFQ_PEEK(&fd->sc_q);
1481 if (bp == NULL) {
1482 fd->sc_ops = 0;
1483 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1484 fd->sc_active = 0;
1485 goto loop;
1486 }
1487
1488 if (bp->b_flags & B_FORMAT)
1489 finfo = (struct ne7_fd_formb *)bp->b_data;
1490
1491 switch (fdc->sc_state) {
1492 case DEVIDLE:
1493 fdc->sc_errors = 0;
1494 fd->sc_skip = 0;
1495 fd->sc_bcount = bp->b_bcount;
1496 fd->sc_blkno = (bp->b_blkno * DEV_BSIZE) / FD_BSIZE(fd);
1497 callout_stop(&fd->sc_motoroff_ch);
1498 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1499 fdc->sc_state = MOTORWAIT;
1500 return (1);
1501 }
1502 if ((fd->sc_flags & FD_MOTOR) == 0) {
1503 /* Turn on the motor, being careful about pairing. */
1504 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1505 if (ofd && ofd->sc_flags & FD_MOTOR) {
1506 callout_stop(&ofd->sc_motoroff_ch);
1507 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1508 }
1509 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1510 fd_set_motor(fdc);
1511 fdc->sc_state = MOTORWAIT;
1512 if ((fdc->sc_flags & FDC_NEEDMOTORWAIT) != 0) { /*XXX*/
1513 /* Allow .25s for motor to stabilize. */
1514 callout_reset(&fd->sc_motoron_ch, hz / 4,
1515 fd_motor_on, fd);
1516 } else {
1517 fd->sc_flags &= ~FD_MOTOR_WAIT;
1518 goto loop;
1519 }
1520 return (1);
1521 }
1522 /* Make sure the right drive is selected. */
1523 fd_set_motor(fdc);
1524
1525 if (fdc_diskchange(fdc))
1526 goto dodskchg;
1527
1528 /*FALLTHROUGH*/
1529 case DOSEEK:
1530 doseek:
1531 if ((fdc->sc_flags & FDC_EIS) &&
1532 (bp->b_flags & B_FORMAT) == 0) {
1533 fd->sc_cylin = bp->b_cylinder;
1534 /* We use implied seek */
1535 goto doio;
1536 }
1537
1538 if (fd->sc_cylin == bp->b_cylinder)
1539 goto doio;
1540
1541 fd->sc_cylin = -1;
1542 fdc->sc_state = SEEKWAIT;
1543 fdc->sc_nstat = 0;
1544
1545 fd->sc_dk.dk_seek++;
1546
1547 disk_busy(&fd->sc_dk);
1548 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1549
1550 /* specify command */
1551 FDC_WRFIFO(fdc, NE7CMD_SPECIFY);
1552 FDC_WRFIFO(fdc, fd->sc_type->steprate);
1553 /* XXX head load time == 6ms */
1554 FDC_WRFIFO(fdc, 6 | NE7_SPECIFY_NODMA);
1555
1556 fdc->sc_itask = FDC_ITASK_SENSEI;
1557 /* seek function */
1558 FDC_WRFIFO(fdc, NE7CMD_SEEK);
1559 FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
1560 FDC_WRFIFO(fdc, bp->b_cylinder * fd->sc_type->step);
1561 return (1);
1562
1563 case DODSKCHG:
1564 dodskchg:
1565 /*
1566 * Disk change: force a seek operation by going to cyl 1
1567 * followed by a recalibrate.
1568 */
1569 disk_busy(&fd->sc_dk);
1570 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1571 fd->sc_cylin = -1;
1572 fdc->sc_nstat = 0;
1573 fdc->sc_state = DSKCHGWAIT;
1574
1575 fdc->sc_itask = FDC_ITASK_SENSEI;
1576 /* seek function */
1577 FDC_WRFIFO(fdc, NE7CMD_SEEK);
1578 FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
1579 FDC_WRFIFO(fdc, 1 * fd->sc_type->step);
1580 return (1);
1581
1582 case DSKCHGWAIT:
1583 callout_stop(&fdc->sc_timo_ch);
1584 disk_unbusy(&fd->sc_dk, 0, 0);
1585 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1586 cyl != 1 * fd->sc_type->step) {
1587 fdcstatus(fdc, "dskchg seek failed");
1588 fdc->sc_state = DORESET;
1589 } else
1590 fdc->sc_state = DORECAL;
1591
1592 if (fdc_diskchange(fdc)) {
1593 printf("%s: cannot clear disk change status\n",
1594 fdc->sc_dev.dv_xname);
1595 fdc->sc_state = DORESET;
1596 }
1597 goto loop;
1598
1599 case DOIO:
1600 doio:
1601 if (finfo != NULL)
1602 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1603 (char *)finfo;
1604 type = fd->sc_type;
1605 sec = fd->sc_blkno % type->seccyl;
1606 nblks = type->seccyl - sec;
1607 nblks = min(nblks, fd->sc_bcount / FD_BSIZE(fd));
1608 nblks = min(nblks, FDC_MAXIOSIZE / FD_BSIZE(fd));
1609 fd->sc_nblks = nblks;
1610 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FD_BSIZE(fd);
1611 head = sec / type->sectrac;
1612 sec -= head * type->sectrac;
1613 #ifdef DIAGNOSTIC
1614 {int block;
1615 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
1616 if (block != fd->sc_blkno) {
1617 printf("fdcintr: block %d != blkno %d\n", block, (int)fd->sc_blkno);
1618 #ifdef DDB
1619 Debugger();
1620 #endif
1621 }}
1622 #endif
1623 read = bp->b_flags & B_READ;
1624
1625 /* Setup for pseudo DMA */
1626 fdc->sc_data = bp->b_data + fd->sc_skip;
1627 fdc->sc_tc = fd->sc_nbytes;
1628
1629 bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
1630 fdc->sc_reg_drs, type->rate);
1631 #ifdef FD_DEBUG
1632 if (fdc_debug > 1)
1633 printf("fdcstate: doio: %s drive %d "
1634 "track %d head %d sec %d nblks %d\n",
1635 finfo ? "format" :
1636 (read ? "read" : "write"),
1637 fd->sc_drive, fd->sc_cylin, head, sec, nblks);
1638 #endif
1639 fdc->sc_state = IOCOMPLETE;
1640 fdc->sc_itask = FDC_ITASK_DMA;
1641 fdc->sc_nstat = 0;
1642
1643 disk_busy(&fd->sc_dk);
1644
1645 /* allow 3 seconds for operation */
1646 callout_reset(&fdc->sc_timo_ch, 3 * hz, fdctimeout, fdc);
1647
1648 if (finfo != NULL) {
1649 /* formatting */
1650 FDC_WRFIFO(fdc, NE7CMD_FORMAT);
1651 FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
1652 FDC_WRFIFO(fdc, finfo->fd_formb_secshift);
1653 FDC_WRFIFO(fdc, finfo->fd_formb_nsecs);
1654 FDC_WRFIFO(fdc, finfo->fd_formb_gaplen);
1655 FDC_WRFIFO(fdc, finfo->fd_formb_fillbyte);
1656 } else {
1657 if (read)
1658 FDC_WRFIFO(fdc, NE7CMD_READ);
1659 else
1660 FDC_WRFIFO(fdc, NE7CMD_WRITE);
1661 FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
1662 FDC_WRFIFO(fdc, fd->sc_cylin); /*track*/
1663 FDC_WRFIFO(fdc, head);
1664 FDC_WRFIFO(fdc, sec + 1); /*sector+1*/
1665 FDC_WRFIFO(fdc, type->secsize);/*sector size*/
1666 FDC_WRFIFO(fdc, type->sectrac);/*secs/track*/
1667 FDC_WRFIFO(fdc, type->gap1); /*gap1 size*/
1668 FDC_WRFIFO(fdc, type->datalen);/*data length*/
1669 }
1670
1671 return (1); /* will return later */
1672
1673 case SEEKWAIT:
1674 callout_stop(&fdc->sc_timo_ch);
1675 fdc->sc_state = SEEKCOMPLETE;
1676 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1677 /* allow 1/50 second for heads to settle */
1678 callout_reset(&fdc->sc_intr_ch, hz / 50,
1679 fdcpseudointr, fdc);
1680 return (1); /* will return later */
1681 }
1682 /*FALLTHROUGH*/
1683 case SEEKCOMPLETE:
1684 /* no data on seek */
1685 disk_unbusy(&fd->sc_dk, 0, 0);
1686
1687 /* Make sure seek really happened. */
1688 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1689 cyl != bp->b_cylinder * fd->sc_type->step) {
1690 #ifdef FD_DEBUG
1691 if (fdc_debug)
1692 fdcstatus(fdc, "seek failed");
1693 #endif
1694 fdcretry(fdc);
1695 goto loop;
1696 }
1697 fd->sc_cylin = bp->b_cylinder;
1698 goto doio;
1699
1700 case IOTIMEDOUT:
1701 /*
1702 * Try to abort the I/O operation without resetting
1703 * the chip first. Poke TC and arrange to pick up
1704 * the timed out I/O command's status.
1705 */
1706 fdc->sc_itask = FDC_ITASK_RESULT;
1707 fdc->sc_state = IOCLEANUPWAIT;
1708 fdc->sc_nstat = 0;
1709 /* 1/10 second should be enough */
1710 callout_reset(&fdc->sc_timo_ch, hz / 10, fdctimeout, fdc);
1711 FTC_FLIP;
1712 return (1);
1713
1714 case IOCLEANUPTIMEDOUT:
1715 case SEEKTIMEDOUT:
1716 case RECALTIMEDOUT:
1717 case RESETTIMEDOUT:
1718 case DSKCHGTIMEDOUT:
1719 fdcstatus(fdc, "timeout");
1720
1721 /* All other timeouts always roll through to a chip reset */
1722 fdcretry(fdc);
1723
1724 /* Force reset, no matter what fdcretry() says */
1725 fdc->sc_state = DORESET;
1726 goto loop;
1727
1728 case IOCLEANUPWAIT: /* IO FAILED, cleanup succeeded */
1729 callout_stop(&fdc->sc_timo_ch);
1730 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
1731 (bp->b_flags & B_READ));
1732 fdcretry(fdc);
1733 goto loop;
1734
1735 case IOCOMPLETE: /* IO DONE, post-analyze */
1736 callout_stop(&fdc->sc_timo_ch);
1737
1738 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
1739 (bp->b_flags & B_READ));
1740
1741 if (fdc->sc_nstat != 7 || st1 != 0 ||
1742 ((st0 & 0xf8) != 0 &&
1743 ((st0 & 0xf8) != 0x20 || (fdc->sc_cfg & CFG_EIS) == 0))) {
1744 #ifdef FD_DEBUG
1745 if (fdc_debug) {
1746 fdcstatus(fdc,
1747 bp->b_flags & B_READ
1748 ? "read failed" : "write failed");
1749 printf("blkno %lld nblks %d nstat %d tc %d\n",
1750 (long long)fd->sc_blkno, fd->sc_nblks,
1751 fdc->sc_nstat, fdc->sc_tc);
1752 }
1753 #endif
1754 if (fdc->sc_nstat == 7 &&
1755 (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
1756
1757 /*
1758 * Silently retry overruns if no other
1759 * error bit is set. Adjust threshold.
1760 */
1761 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1762 if (thr < 15) {
1763 thr++;
1764 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1765 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1766 #ifdef FD_DEBUG
1767 if (fdc_debug)
1768 printf("fdc: %d -> threshold\n", thr);
1769 #endif
1770 fdconf(fdc);
1771 fdc->sc_overruns = 0;
1772 }
1773 if (++fdc->sc_overruns < 3) {
1774 fdc->sc_state = DOIO;
1775 goto loop;
1776 }
1777 }
1778 fdcretry(fdc);
1779 goto loop;
1780 }
1781 if (fdc->sc_errors) {
1782 diskerr(bp, "fd", "soft error", LOG_PRINTF,
1783 fd->sc_skip / FD_BSIZE(fd),
1784 (struct disklabel *)NULL);
1785 printf("\n");
1786 fdc->sc_errors = 0;
1787 } else {
1788 if (--fdc->sc_overruns < -20) {
1789 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1790 if (thr > 0) {
1791 thr--;
1792 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1793 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1794 #ifdef FD_DEBUG
1795 if (fdc_debug)
1796 printf("fdc: %d -> threshold\n", thr);
1797 #endif
1798 fdconf(fdc);
1799 }
1800 fdc->sc_overruns = 0;
1801 }
1802 }
1803 fd->sc_blkno += fd->sc_nblks;
1804 fd->sc_skip += fd->sc_nbytes;
1805 fd->sc_bcount -= fd->sc_nbytes;
1806 if (finfo == NULL && fd->sc_bcount > 0) {
1807 bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
1808 goto doseek;
1809 }
1810 fdfinish(fd, bp);
1811 goto loop;
1812
1813 case DORESET:
1814 /* try a reset, keep motor on */
1815 fd_set_motor(fdc);
1816 delay(100);
1817 fdc->sc_nstat = 0;
1818 fdc->sc_itask = FDC_ITASK_SENSEI;
1819 fdc->sc_state = RESETCOMPLETE;
1820 callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
1821 fdc_reset(fdc);
1822 return (1); /* will return later */
1823
1824 case RESETCOMPLETE:
1825 callout_stop(&fdc->sc_timo_ch);
1826 fdconf(fdc);
1827
1828 /* FALLTHROUGH */
1829 case DORECAL:
1830 fdc->sc_state = RECALWAIT;
1831 fdc->sc_itask = FDC_ITASK_SENSEI;
1832 fdc->sc_nstat = 0;
1833 callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1834 /* recalibrate function */
1835 FDC_WRFIFO(fdc, NE7CMD_RECAL);
1836 FDC_WRFIFO(fdc, fd->sc_drive);
1837 return (1); /* will return later */
1838
1839 case RECALWAIT:
1840 callout_stop(&fdc->sc_timo_ch);
1841 fdc->sc_state = RECALCOMPLETE;
1842 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1843 /* allow 1/30 second for heads to settle */
1844 callout_reset(&fdc->sc_intr_ch, hz / 30,
1845 fdcpseudointr, fdc);
1846 return (1); /* will return later */
1847 }
1848
1849 case RECALCOMPLETE:
1850 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1851 #ifdef FD_DEBUG
1852 if (fdc_debug)
1853 fdcstatus(fdc, "recalibrate failed");
1854 #endif
1855 fdcretry(fdc);
1856 goto loop;
1857 }
1858 fd->sc_cylin = 0;
1859 goto doseek;
1860
1861 case MOTORWAIT:
1862 if (fd->sc_flags & FD_MOTOR_WAIT)
1863 return (1); /* time's not up yet */
1864 goto doseek;
1865
1866 default:
1867 fdcstatus(fdc, "stray interrupt");
1868 return (1);
1869 }
1870 #ifdef DIAGNOSTIC
1871 panic("fdcintr: impossible");
1872 #endif
1873
1874 xxx:
1875 /*
1876 * We get here if the chip locks up in FDC_WRFIFO()
1877 * Cancel any operation and schedule a reset
1878 */
1879 callout_stop(&fdc->sc_timo_ch);
1880 fdcretry(fdc);
1881 (fdc)->sc_state = DORESET;
1882 goto loop;
1883
1884 #undef st0
1885 #undef st1
1886 #undef cyl
1887 }
1888
1889 void
1890 fdcretry(fdc)
1891 struct fdc_softc *fdc;
1892 {
1893 struct fd_softc *fd;
1894 struct buf *bp;
1895 int error = EIO;
1896
1897 fd = fdc->sc_drives.tqh_first;
1898 bp = BUFQ_PEEK(&fd->sc_q);
1899
1900 fdc->sc_overruns = 0;
1901 if (fd->sc_opts & FDOPT_NORETRY)
1902 goto fail;
1903
1904 switch (fdc->sc_errors) {
1905 case 0:
1906 if (fdc->sc_nstat == 7 &&
1907 (fdc->sc_status[0] & 0xd8) == 0x40 &&
1908 (fdc->sc_status[1] & 0x2) == 0x2) {
1909 printf("%s: read-only medium\n", fd->sc_dv.dv_xname);
1910 error = EROFS;
1911 goto failsilent;
1912 }
1913 /* try again */
1914 fdc->sc_state =
1915 (fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
1916 break;
1917
1918 case 1: case 2: case 3:
1919 /* didn't work; try recalibrating */
1920 fdc->sc_state = DORECAL;
1921 break;
1922
1923 case 4:
1924 if (fdc->sc_nstat == 7 &&
1925 fdc->sc_status[0] == 0 &&
1926 fdc->sc_status[1] == 0 &&
1927 fdc->sc_status[2] == 0) {
1928 /*
1929 * We've retried a few times and we've got
1930 * valid status and all three status bytes
1931 * are zero. Assume this condition is the
1932 * result of no disk loaded into the drive.
1933 */
1934 printf("%s: no medium?\n", fd->sc_dv.dv_xname);
1935 error = ENODEV;
1936 goto failsilent;
1937 }
1938
1939 /* still no go; reset the bastard */
1940 fdc->sc_state = DORESET;
1941 break;
1942
1943 default:
1944 fail:
1945 if ((fd->sc_opts & FDOPT_SILENT) == 0) {
1946 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1947 fd->sc_skip / FD_BSIZE(fd),
1948 (struct disklabel *)NULL);
1949 printf("\n");
1950 fdcstatus(fdc, "controller status");
1951 }
1952
1953 failsilent:
1954 bp->b_flags |= B_ERROR;
1955 bp->b_error = error;
1956 fdfinish(fd, bp);
1957 }
1958 fdc->sc_errors++;
1959 }
1960
1961 int
1962 fdioctl(dev, cmd, addr, flag, p)
1963 dev_t dev;
1964 u_long cmd;
1965 caddr_t addr;
1966 int flag;
1967 struct proc *p;
1968 {
1969 struct fd_softc *fd;
1970 struct fdc_softc *fdc;
1971 struct fdformat_parms *form_parms;
1972 struct fdformat_cmd *form_cmd;
1973 struct ne7_fd_formb *fd_formb;
1974 int il[FD_MAX_NSEC + 1];
1975 int unit;
1976 int i, j;
1977 int error;
1978
1979 unit = FDUNIT(dev);
1980 if (unit >= fd_cd.cd_ndevs)
1981 return (ENXIO);
1982
1983 fd = fd_cd.cd_devs[FDUNIT(dev)];
1984 fdc = (struct fdc_softc *)fd->sc_dv.dv_parent;
1985
1986 switch (cmd) {
1987 case DIOCGDINFO:
1988 *(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1989 return 0;
1990
1991 case DIOCWLABEL:
1992 if ((flag & FWRITE) == 0)
1993 return EBADF;
1994 /* XXX do something */
1995 return (0);
1996
1997 case DIOCWDINFO:
1998 if ((flag & FWRITE) == 0)
1999 return (EBADF);
2000
2001 error = setdisklabel(fd->sc_dk.dk_label,
2002 (struct disklabel *)addr, 0,
2003 fd->sc_dk.dk_cpulabel);
2004 if (error)
2005 return (error);
2006
2007 error = writedisklabel(dev, fdstrategy,
2008 fd->sc_dk.dk_label,
2009 fd->sc_dk.dk_cpulabel);
2010 return (error);
2011
2012 case DIOCLOCK:
2013 /*
2014 * Nothing to do here, really.
2015 */
2016 return (0);
2017
2018 case DIOCEJECT:
2019 if (*(int *)addr == 0) {
2020 int part = DISKPART(dev);
2021 /*
2022 * Don't force eject: check that we are the only
2023 * partition open. If so, unlock it.
2024 */
2025 if ((fd->sc_dk.dk_openmask & ~(1 << part)) != 0 ||
2026 fd->sc_dk.dk_bopenmask + fd->sc_dk.dk_copenmask !=
2027 fd->sc_dk.dk_openmask) {
2028 return (EBUSY);
2029 }
2030 }
2031 /* FALLTHROUGH */
2032 case ODIOCEJECT:
2033 fd_do_eject(fd);
2034 return (0);
2035
2036 case FDIOCGETFORMAT:
2037 form_parms = (struct fdformat_parms *)addr;
2038 form_parms->fdformat_version = FDFORMAT_VERSION;
2039 form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
2040 form_parms->ncyl = fd->sc_type->cylinders;
2041 form_parms->nspt = fd->sc_type->sectrac;
2042 form_parms->ntrk = fd->sc_type->heads;
2043 form_parms->stepspercyl = fd->sc_type->step;
2044 form_parms->gaplen = fd->sc_type->gap2;
2045 form_parms->fillbyte = fd->sc_type->fillbyte;
2046 form_parms->interleave = fd->sc_type->interleave;
2047 switch (fd->sc_type->rate) {
2048 case FDC_500KBPS:
2049 form_parms->xfer_rate = 500 * 1024;
2050 break;
2051 case FDC_300KBPS:
2052 form_parms->xfer_rate = 300 * 1024;
2053 break;
2054 case FDC_250KBPS:
2055 form_parms->xfer_rate = 250 * 1024;
2056 break;
2057 default:
2058 return (EINVAL);
2059 }
2060 return (0);
2061
2062 case FDIOCSETFORMAT:
2063 if ((flag & FWRITE) == 0)
2064 return (EBADF); /* must be opened for writing */
2065
2066 form_parms = (struct fdformat_parms *)addr;
2067 if (form_parms->fdformat_version != FDFORMAT_VERSION)
2068 return (EINVAL);/* wrong version of formatting prog */
2069
2070 i = form_parms->nbps >> 7;
2071 if ((form_parms->nbps & 0x7f) || ffs(i) == 0 ||
2072 i & ~(1 << (ffs(i)-1)))
2073 /* not a power-of-two multiple of 128 */
2074 return (EINVAL);
2075
2076 switch (form_parms->xfer_rate) {
2077 case 500 * 1024:
2078 fd->sc_type->rate = FDC_500KBPS;
2079 break;
2080 case 300 * 1024:
2081 fd->sc_type->rate = FDC_300KBPS;
2082 break;
2083 case 250 * 1024:
2084 fd->sc_type->rate = FDC_250KBPS;
2085 break;
2086 default:
2087 return (EINVAL);
2088 }
2089
2090 if (form_parms->nspt > FD_MAX_NSEC ||
2091 form_parms->fillbyte > 0xff ||
2092 form_parms->interleave > 0xff)
2093 return EINVAL;
2094 fd->sc_type->sectrac = form_parms->nspt;
2095 if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
2096 return EINVAL;
2097 fd->sc_type->heads = form_parms->ntrk;
2098 fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
2099 fd->sc_type->secsize = ffs(i)-1;
2100 fd->sc_type->gap2 = form_parms->gaplen;
2101 fd->sc_type->cylinders = form_parms->ncyl;
2102 fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
2103 form_parms->nbps / DEV_BSIZE;
2104 fd->sc_type->step = form_parms->stepspercyl;
2105 fd->sc_type->fillbyte = form_parms->fillbyte;
2106 fd->sc_type->interleave = form_parms->interleave;
2107 return (0);
2108
2109 case FDIOCFORMAT_TRACK:
2110 if((flag & FWRITE) == 0)
2111 /* must be opened for writing */
2112 return (EBADF);
2113 form_cmd = (struct fdformat_cmd *)addr;
2114 if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
2115 /* wrong version of formatting prog */
2116 return (EINVAL);
2117
2118 if (form_cmd->head >= fd->sc_type->heads ||
2119 form_cmd->cylinder >= fd->sc_type->cylinders) {
2120 return (EINVAL);
2121 }
2122
2123 fd_formb = malloc(sizeof(struct ne7_fd_formb),
2124 M_TEMP, M_NOWAIT);
2125 if (fd_formb == 0)
2126 return (ENOMEM);
2127
2128 fd_formb->head = form_cmd->head;
2129 fd_formb->cyl = form_cmd->cylinder;
2130 fd_formb->transfer_rate = fd->sc_type->rate;
2131 fd_formb->fd_formb_secshift = fd->sc_type->secsize;
2132 fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
2133 fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
2134 fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
2135
2136 bzero(il, sizeof il);
2137 for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
2138 while (il[(j%fd_formb->fd_formb_nsecs) + 1])
2139 j++;
2140 il[(j%fd_formb->fd_formb_nsecs) + 1] = i;
2141 j += fd->sc_type->interleave;
2142 }
2143 for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
2144 fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
2145 fd_formb->fd_formb_headno(i) = form_cmd->head;
2146 fd_formb->fd_formb_secno(i) = il[i+1];
2147 fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
2148 }
2149
2150 error = fdformat(dev, fd_formb, p);
2151 free(fd_formb, M_TEMP);
2152 return error;
2153
2154 case FDIOCGETOPTS: /* get drive options */
2155 *(int *)addr = fd->sc_opts;
2156 return (0);
2157
2158 case FDIOCSETOPTS: /* set drive options */
2159 fd->sc_opts = *(int *)addr;
2160 return (0);
2161
2162 #ifdef FD_DEBUG
2163 case _IO('f', 100):
2164 fdc_wrfifo(fdc, NE7CMD_DUMPREG);
2165 fdcresult(fdc);
2166 printf("fdc: dumpreg(%d regs): <", fdc->sc_nstat);
2167 for (i = 0; i < fdc->sc_nstat; i++)
2168 printf(" 0x%x", fdc->sc_status[i]);
2169 printf(">\n");
2170 return (0);
2171
2172 case _IOW('f', 101, int):
2173 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
2174 fdc->sc_cfg |= (*(int *)addr & CFG_THRHLD_MASK);
2175 fdconf(fdc);
2176 return (0);
2177
2178 case _IO('f', 102):
2179 fdc_wrfifo(fdc, NE7CMD_SENSEI);
2180 fdcresult(fdc);
2181 printf("fdc: sensei(%d regs): <", fdc->sc_nstat);
2182 for (i=0; i< fdc->sc_nstat; i++)
2183 printf(" 0x%x", fdc->sc_status[i]);
2184 printf(">\n");
2185 return (0);
2186 #endif
2187 default:
2188 return (ENOTTY);
2189 }
2190
2191 #ifdef DIAGNOSTIC
2192 panic("fdioctl: impossible");
2193 #endif
2194 }
2195
2196 int
2197 fdformat(dev, finfo, p)
2198 dev_t dev;
2199 struct ne7_fd_formb *finfo;
2200 struct proc *p;
2201 {
2202 int rv = 0, s;
2203 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
2204 struct fd_type *type = fd->sc_type;
2205 struct buf *bp;
2206
2207 /* set up a buffer header for fdstrategy() */
2208 s = splbio();
2209 bp = (struct buf *)pool_get(&bufpool, PR_NOWAIT);
2210 splx(s);
2211 if (bp == NULL)
2212 return (ENOBUFS);
2213
2214 memset((void *)bp, 0, sizeof(struct buf));
2215 BUF_INIT(bp);
2216 bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
2217 bp->b_proc = p;
2218 bp->b_dev = dev;
2219
2220 /*
2221 * Calculate a fake blkno, so fdstrategy() would initiate a
2222 * seek to the requested cylinder.
2223 */
2224 bp->b_blkno = ((finfo->cyl * (type->sectrac * type->heads)
2225 + finfo->head * type->sectrac) * FD_BSIZE(fd))
2226 / DEV_BSIZE;
2227
2228 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2229 bp->b_data = (caddr_t)finfo;
2230
2231 #ifdef FD_DEBUG
2232 if (fdc_debug) {
2233 int i;
2234
2235 printf("fdformat: blkno 0x%llx count %d\n",
2236 (unsigned long long)bp->b_blkno, bp->b_bcount);
2237
2238 printf("\tcyl:\t%d\n", finfo->cyl);
2239 printf("\thead:\t%d\n", finfo->head);
2240 printf("\tnsecs:\t%d\n", finfo->fd_formb_nsecs);
2241 printf("\tsshft:\t%d\n", finfo->fd_formb_secshift);
2242 printf("\tgaplen:\t%d\n", finfo->fd_formb_gaplen);
2243 printf("\ttrack data:");
2244 for (i = 0; i < finfo->fd_formb_nsecs; i++) {
2245 printf(" [c%d h%d s%d]",
2246 finfo->fd_formb_cylno(i),
2247 finfo->fd_formb_headno(i),
2248 finfo->fd_formb_secno(i) );
2249 if (finfo->fd_formb_secsize(i) != 2)
2250 printf("<sz:%d>", finfo->fd_formb_secsize(i));
2251 }
2252 printf("\n");
2253 }
2254 #endif
2255
2256 /* now do the format */
2257 fdstrategy(bp);
2258
2259 /* ...and wait for it to complete */
2260 rv = biowait(bp);
2261 s = splbio();
2262 pool_put(&bufpool, bp);
2263 splx(s);
2264 return (rv);
2265 }
2266
2267 void
2268 fdgetdisklabel(dev)
2269 dev_t dev;
2270 {
2271 int unit = FDUNIT(dev), i;
2272 struct fd_softc *fd = fd_cd.cd_devs[unit];
2273 struct disklabel *lp = fd->sc_dk.dk_label;
2274 struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
2275
2276 bzero(lp, sizeof(struct disklabel));
2277 bzero(lp, sizeof(struct cpu_disklabel));
2278
2279 lp->d_type = DTYPE_FLOPPY;
2280 lp->d_secsize = FD_BSIZE(fd);
2281 lp->d_secpercyl = fd->sc_type->seccyl;
2282 lp->d_nsectors = fd->sc_type->sectrac;
2283 lp->d_ncylinders = fd->sc_type->cylinders;
2284 lp->d_ntracks = fd->sc_type->heads; /* Go figure... */
2285 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
2286 lp->d_rpm = 3600; /* XXX like it matters... */
2287
2288 strncpy(lp->d_typename, "floppy", sizeof(lp->d_typename));
2289 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
2290 lp->d_interleave = 1;
2291
2292 lp->d_partitions[RAW_PART].p_offset = 0;
2293 lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
2294 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
2295 lp->d_npartitions = RAW_PART + 1;
2296
2297 lp->d_magic = DISKMAGIC;
2298 lp->d_magic2 = DISKMAGIC;
2299 lp->d_checksum = dkcksum(lp);
2300
2301 /*
2302 * Call the generic disklabel extraction routine. If there's
2303 * not a label there, fake it.
2304 */
2305 if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) {
2306 strncpy(lp->d_packname, "default label",
2307 sizeof(lp->d_packname));
2308 /*
2309 * Reset the partition info; it might have gotten
2310 * trashed in readdisklabel().
2311 *
2312 * XXX Why do we have to do this? readdisklabel()
2313 * should be safe...
2314 */
2315 for (i = 0; i < MAXPARTITIONS; ++i) {
2316 lp->d_partitions[i].p_offset = 0;
2317 if (i == RAW_PART) {
2318 lp->d_partitions[i].p_size =
2319 lp->d_secpercyl * lp->d_ncylinders;
2320 lp->d_partitions[i].p_fstype = FS_BSDFFS;
2321 } else {
2322 lp->d_partitions[i].p_size = 0;
2323 lp->d_partitions[i].p_fstype = FS_UNUSED;
2324 }
2325 }
2326 lp->d_npartitions = RAW_PART + 1;
2327 }
2328 }
2329
2330 void
2331 fd_do_eject(fd)
2332 struct fd_softc *fd;
2333 {
2334 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
2335
2336 if (CPU_ISSUN4C) {
2337 auxregbisc(AUXIO4C_FDS, AUXIO4C_FEJ);
2338 delay(10);
2339 auxregbisc(AUXIO4C_FEJ, AUXIO4C_FDS);
2340 return;
2341 }
2342 if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
2343 bus_space_tag_t t = fdc->sc_bustag;
2344 bus_space_handle_t h = fdc->sc_handle;
2345 u_int8_t dor = FDO_FRST | FDO_FDMAEN | FDO_MOEN(0);
2346
2347 bus_space_write_1(t, h, fdc->sc_reg_dor, dor | FDO_EJ);
2348 delay(10);
2349 bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
2350 return;
2351 }
2352 }
2353
2354 #ifdef MEMORY_DISK_HOOKS
2355 int fd_read_md_image __P((size_t *, caddr_t *));
2356 #endif
2357
2358 /* ARGSUSED */
2359 void
2360 fd_mountroot_hook(dev)
2361 struct device *dev;
2362 {
2363 int c;
2364
2365 fd_do_eject((struct fd_softc *)dev);
2366 printf("Insert filesystem floppy and press return.");
2367 for (;;) {
2368 c = cngetc();
2369 if ((c == '\r') || (c == '\n')) {
2370 printf("\n");
2371 break;
2372 }
2373 }
2374 }
2375
2376 #ifdef MEMORY_DISK_HOOKS
2377
2378 #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT)
2379
2380 int
2381 fd_read_md_image(sizep, addrp)
2382 size_t *sizep;
2383 caddr_t *addrp;
2384 {
2385 struct buf buf, *bp = &buf;
2386 dev_t dev;
2387 off_t offset;
2388 caddr_t addr;
2389
2390 dev = makedev(54,0); /* XXX */
2391
2392 MALLOC(addr, caddr_t, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK);
2393 *addrp = addr;
2394
2395 if (fdopen(dev, 0, S_IFCHR, NULL))
2396 panic("fd: mountroot: fdopen");
2397
2398 offset = 0;
2399
2400 for (;;) {
2401 bp->b_dev = dev;
2402 bp->b_error = 0;
2403 bp->b_resid = 0;
2404 bp->b_proc = NULL;
2405 bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_READ;
2406 bp->b_blkno = btodb(offset);
2407 bp->b_bcount = DEV_BSIZE;
2408 bp->b_data = addr;
2409 fdstrategy(bp);
2410 while ((bp->b_flags & B_DONE) == 0) {
2411 tsleep((caddr_t)bp, PRIBIO + 1, "physio", 0);
2412 }
2413 if (bp->b_error)
2414 panic("fd: mountroot: fdread error %d", bp->b_error);
2415
2416 if (bp->b_resid != 0)
2417 break;
2418
2419 addr += DEV_BSIZE;
2420 offset += DEV_BSIZE;
2421 if (offset + DEV_BSIZE > FDMICROROOTSIZE)
2422 break;
2423 }
2424 (void)fdclose(dev, 0, S_IFCHR, NULL);
2425 *sizep = offset;
2426 fd_do_eject(fd_cd.cd_devs[FDUNIT(dev)]);
2427 return (0);
2428 }
2429 #endif
2430