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