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