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