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