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