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