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