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