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