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