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