fdc.c revision 1.9 1 /* $NetBSD: fdc.c,v 1.9 2007/03/09 21:02:30 jnemeth Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1990 The Regents of the University of California.
41 * All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * Don Ahn.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)fd.c 7.4 (Berkeley) 5/25/91
71 */
72
73 /*-
74 * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.
75 *
76 * This code is derived from software contributed to Berkeley by
77 * Don Ahn.
78 *
79 * Redistribution and use in source and binary forms, with or without
80 * modification, are permitted provided that the following conditions
81 * are met:
82 * 1. Redistributions of source code must retain the above copyright
83 * notice, this list of conditions and the following disclaimer.
84 * 2. Redistributions in binary form must reproduce the above copyright
85 * notice, this list of conditions and the following disclaimer in the
86 * documentation and/or other materials provided with the distribution.
87 * 3. All advertising materials mentioning features or use of this software
88 * must display the following acknowledgement:
89 * This product includes software developed by the University of
90 * California, Berkeley and its contributors.
91 * 4. Neither the name of the University nor the names of its contributors
92 * may be used to endorse or promote products derived from this software
93 * without specific prior written permission.
94 *
95 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
96 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
97 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
98 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
99 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
100 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
101 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
103 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
104 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
105 * SUCH DAMAGE.
106 *
107 * @(#)fd.c 7.4 (Berkeley) 5/25/91
108 */
109
110 #include <sys/cdefs.h>
111 __KERNEL_RCSID(0, "$NetBSD: fdc.c,v 1.9 2007/03/09 21:02:30 jnemeth Exp $");
112
113 #include "opt_ddb.h"
114 #include "opt_md.h"
115
116 #include <sys/param.h>
117 #include <sys/systm.h>
118 #include <sys/callout.h>
119 #include <sys/kernel.h>
120 #include <sys/file.h>
121 #include <sys/ioctl.h>
122 #include <sys/device.h>
123 #include <sys/disklabel.h>
124 #include <sys/disk.h>
125 #include <sys/fdio.h>
126 #include <sys/buf.h>
127 #include <sys/bufq.h>
128 #include <sys/malloc.h>
129 #include <sys/proc.h>
130 #include <sys/uio.h>
131 #include <sys/stat.h>
132 #include <sys/syslog.h>
133 #include <sys/queue.h>
134 #include <sys/conf.h>
135
136 #include <dev/cons.h>
137
138 #include <uvm/uvm_extern.h>
139
140 #include <machine/autoconf.h>
141 #include <machine/intr.h>
142
143 #ifdef SUN4
144 #include <sparc/sparc/auxreg.h>
145 #include <sparc/dev/fdreg.h>
146 #include <sparc/dev/fdvar.h>
147 #elif SUN4U
148 #include <dev/ebus/ebusreg.h>
149 #include <dev/ebus/ebusvar.h>
150 /* #include <sparc/sparc/auxreg.h> */
151 #include <sparc64/dev/auxioreg.h>
152 #include <sparc64/dev/auxiovar.h>
153 #include <sparc64/dev/fdcreg.h>
154 #include <sparc64/dev/fdcvar.h>
155 #endif
156
157 #include <prop/proplib.h>
158
159 #define FDUNIT(dev) (minor(dev) / 8)
160 #define FDTYPE(dev) (minor(dev) % 8)
161
162 #ifdef SUN4U
163 #define FTC_FLIP \
164 do { \
165 auxio_fd_control(AUXIO_LED_FTC); \
166 auxio_fd_control(0); \
167 } while (0)
168 #else
169 #define FTC_FLIP
170 #endif
171
172 /* (mis)use device use flag to identify format operation */
173 #define B_FORMAT B_DEVPRIVATE
174
175 #define FD_DEBUG
176 #ifdef FD_DEBUG
177 int fdc_debug = 0;
178 #endif
179
180 enum fdc_state {
181 DEVIDLE = 0,
182 MOTORWAIT, /* 1 */
183 DOSEEK, /* 2 */
184 SEEKWAIT, /* 3 */
185 SEEKTIMEDOUT, /* 4 */
186 SEEKCOMPLETE, /* 5 */
187 DOIO, /* 6 */
188 IOCOMPLETE, /* 7 */
189 IOTIMEDOUT, /* 8 */
190 IOCLEANUPWAIT, /* 9 */
191 IOCLEANUPTIMEDOUT,/*10 */
192 DORESET, /* 11 */
193 RESETCOMPLETE, /* 12 */
194 RESETTIMEDOUT, /* 13 */
195 DORECAL, /* 14 */
196 RECALWAIT, /* 15 */
197 RECALTIMEDOUT, /* 16 */
198 RECALCOMPLETE, /* 17 */
199 DODSKCHG, /* 18 */
200 DSKCHGWAIT, /* 19 */
201 DSKCHGTIMEDOUT, /* 20 */
202 };
203
204 /* software state, per controller */
205 struct fdc_softc {
206 struct device sc_dev; /* boilerplate */
207 bus_space_tag_t sc_bustag;
208
209 struct callout sc_timo_ch; /* timeout callout */
210 struct callout sc_intr_ch; /* pseudo-intr callout */
211
212 struct fd_softc *sc_fd[4]; /* pointers to children */
213 TAILQ_HEAD(drivehead, fd_softc) sc_drives;
214 enum fdc_state sc_state;
215 int sc_flags;
216 #define FDC_82077 0x01
217 #define FDC_NEEDHEADSETTLE 0x02
218 #define FDC_EIS 0x04
219 #define FDC_NEEDMOTORWAIT 0x08
220 #define FDC_NOEJECT 0x10
221 #define FDC_EBUS 0x20
222 int sc_errors; /* number of retries so far */
223 int sc_overruns; /* number of DMA overruns */
224 int sc_cfg; /* current configuration */
225 struct fdcio sc_io;
226 #define sc_handle sc_io.fdcio_handle
227 #define sc_reg_msr sc_io.fdcio_reg_msr
228 #define sc_reg_fifo sc_io.fdcio_reg_fifo
229 #define sc_reg_dor sc_io.fdcio_reg_dor
230 #define sc_reg_dir sc_io.fdcio_reg_dir
231 #define sc_reg_drs sc_io.fdcio_reg_msr
232 #define sc_itask sc_io.fdcio_itask
233 #define sc_istatus sc_io.fdcio_istatus
234 #define sc_data sc_io.fdcio_data
235 #define sc_tc sc_io.fdcio_tc
236 #define sc_nstat sc_io.fdcio_nstat
237 #define sc_status sc_io.fdcio_status
238 #define sc_intrcnt sc_io.fdcio_intrcnt
239
240 void *sc_sicookie; /* softintr(9) cookie */
241 };
242
243 #ifdef SUN4
244 extern struct fdcio *fdciop; /* I/O descriptor used in fdintr.s */
245 #endif
246
247 /* controller driver configuration */
248 #ifdef SUN4
249 int fdcmatch_mainbus(struct device *, struct cfdata *, void*);
250 int fdcmatch_obio(struct device *, struct cfdata *, void *);
251 void fdcattach_mainbus(struct device *, struct device *, void *);
252 void fdcattach_obio(struct device *, struct device *, void *);
253 #elif SUN4U
254 int fdcmatch_sbus(struct device *, struct cfdata *, void *);
255 int fdcmatch_ebus(struct device *, struct cfdata *, void *);
256 void fdcattach_sbus(struct device *, struct device *, void *);
257 void fdcattach_ebus(struct device *, struct device *, void *);
258 #endif
259
260 int fdcattach(struct fdc_softc *, int);
261
262 #ifdef SUN4
263 CFATTACH_DECL(fdc_mainbus, sizeof(struct fdc_softc),
264 fdcmatch_mainbus, fdcattach_mainbus, NULL, NULL);
265
266 CFATTACH_DECL(fdc_obio, sizeof(struct fdc_softc),
267 fdcmatch_obio, fdcattach_obio, NULL, NULL);
268 #elif SUN4U
269 CFATTACH_DECL(fdc_sbus, sizeof(struct fdc_softc),
270 fdcmatch_sbus, fdcattach_sbus, NULL, NULL);
271
272 CFATTACH_DECL(fdc_ebus, sizeof(struct fdc_softc),
273 fdcmatch_ebus, fdcattach_ebus, NULL, NULL);
274 #endif
275
276 inline struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
277
278 /*
279 * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
280 * we tell them apart.
281 */
282 struct fd_type {
283 int sectrac; /* sectors per track */
284 int heads; /* number of heads */
285 int seccyl; /* sectors per cylinder */
286 int secsize; /* size code for sectors */
287 int datalen; /* data len when secsize = 0 */
288 int steprate; /* step rate and head unload time */
289 int gap1; /* gap len between sectors */
290 int gap2; /* formatting gap */
291 int cylinders; /* total num of cylinders */
292 int size; /* size of disk in sectors */
293 int step; /* steps per cylinder */
294 int rate; /* transfer speed code */
295 int fillbyte; /* format fill byte */
296 int interleave; /* interleave factor (formatting) */
297 const char *name;
298 };
299
300 /* The order of entries in the following table is important -- BEWARE! */
301 struct fd_type fd_types[] = {
302 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" }, /* 1.44MB diskette */
303 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" }, /* 3.5" 720kB diskette */
304 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x" }, /* 360kB in 720kB drive */
305 { 8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS,0xf6,1, "1.2MB/NEC" } /* 1.2 MB japanese format */
306 };
307
308 /* software state, per disk (with up to 4 disks per ctlr) */
309 struct fd_softc {
310 struct device sc_dv; /* generic device info */
311 struct disk sc_dk; /* generic disk info */
312
313 struct fd_type *sc_deftype; /* default type descriptor */
314 struct fd_type *sc_type; /* current type descriptor */
315
316 struct callout sc_motoron_ch;
317 struct callout sc_motoroff_ch;
318
319 daddr_t sc_blkno; /* starting block number */
320 int sc_bcount; /* byte count left */
321 int sc_skip; /* bytes already transferred */
322 int sc_nblks; /* number of blocks currently transferring */
323 int sc_nbytes; /* number of bytes currently transferring */
324
325 int sc_drive; /* physical unit number */
326 int sc_flags;
327 #define FD_OPEN 0x01 /* it's open */
328 #define FD_MOTOR 0x02 /* motor should be on */
329 #define FD_MOTOR_WAIT 0x04 /* motor coming up */
330 int sc_cylin; /* where we think the head is */
331 int sc_opts; /* user-set options */
332
333 void *sc_sdhook; /* shutdownhook cookie */
334
335 TAILQ_ENTRY(fd_softc) sc_drivechain;
336 int sc_ops; /* I/O ops since last switch */
337 struct bufq_state *sc_q;/* pending I/O requests */
338 int sc_active; /* number of active I/O requests */
339 };
340
341 /* floppy driver configuration */
342 int fdmatch(struct device *, struct cfdata *, void *);
343 void fdattach(struct device *, struct device *, void *);
344
345 CFATTACH_DECL(fd, sizeof(struct fd_softc),
346 fdmatch, fdattach, NULL, NULL);
347
348 extern struct cfdriver fd_cd;
349
350 dev_type_open(fdopen);
351 dev_type_close(fdclose);
352 dev_type_read(fdread);
353 dev_type_write(fdwrite);
354 dev_type_ioctl(fdioctl);
355 dev_type_strategy(fdstrategy);
356
357 const struct bdevsw fd_bdevsw = {
358 fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
359 };
360
361 const struct cdevsw fd_cdevsw = {
362 fdopen, fdclose, fdread, fdwrite, fdioctl,
363 nostop, notty, nopoll, nommap, nokqfilter, 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(struct device *);
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_properties(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(struct device *parent, struct cfdata *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(struct device *parent, struct cfdata *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(struct device *parent, struct cfdata *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(struct device *parent, struct cfdata *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(struct device *parent, struct device *self, void *aux)
610 {
611 struct fdc_softc *fdc = (void *)self;
612 struct mainbus_attach_args *ma = aux;
613
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 printf("%s: cannot map registers\n", self->dv_xname);
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(struct device *parent, struct device *self, void *aux)
638 {
639 struct fdc_softc *fdc = (void *)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_bustag = sa->sa_bustag;
649
650 if (sbus_bus_map(sa->sa_bustag,
651 sa->sa_slot, sa->sa_offset, sa->sa_size,
652 BUS_SPACE_MAP_LINEAR, &fdc->sc_handle) != 0) {
653 printf("%s: cannot map control registers\n",
654 self->dv_xname);
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 print(": 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(struct device *parent, struct device *self, void *aux)
677 {
678 struct fdc_softc *fdc = (void *)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_bustag = sa->sa_bustag;
692
693 if (bus_space_map(sa->sa_bustag, BUS_ADDR(sa->sa_slot, sa->sa_offset),
694 sa->sa_size, 0, &fdc->sc_handle) != 0) {
695 printf(": cannot map control registers\n");
696 return;
697 }
698
699 establish_chip_type(fdc,
700 sa->sa_bustag,
701 BUS_ADDR(sa->sa_slot, sa->sa_offset),
702 sa->sa_size,
703 fdc->sc_handle);
704
705 if (strcmp(prom_getpropstring(sa->sa_node, "status"), "disabled") == 0) {
706 printf(": no drives attached\n");
707 return;
708 }
709
710 if (prom_getproplen(sa->sa_node, "manual") >= 0)
711 fdc->sc_flags |= FDC_NOEJECT;
712
713
714 if (fdcattach(fdc, sa->sa_pri) != 0)
715 bus_space_unmap(sa->sa_bustag, fdc->sc_handle, sa->sa_size);
716 }
717
718 void
719 fdcattach_ebus(struct device *parent, struct device *self, void *aux)
720 {
721 struct fdc_softc *fdc = (void *)self;
722 struct ebus_attach_args *ea = aux;
723 int map_vaddr;
724
725 if (ea->ea_nintr == 0) {
726 printf(": no interrupt line configured\n");
727 return;
728 }
729
730 if (ea->ea_nreg < 3) {
731 printf(": expected 3 registers, only got %d\n",
732 ea->ea_nreg);
733 return;
734 }
735
736 fdc->sc_bustag = ea->ea_bustag;
737
738 if (ea->ea_nvaddr > 0) {
739 sparc_promaddr_to_handle(ea->ea_bustag,
740 ea->ea_vaddr[0], &fdc->sc_handle);
741 map_vaddr = 1;
742 } else if (bus_space_map(fdc->sc_bustag,
743 EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
744 ea->ea_reg[0].size, 0, &fdc->sc_handle) == 0) {
745 map_vaddr = 0;
746 } else {
747 printf(": can't map control registers\n");
748 return;
749 }
750
751 establish_chip_type(fdc,
752 fdc->sc_bustag,
753 map_vaddr ? ea->ea_vaddr[0] :
754 EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
755 ea->ea_reg[0].size,
756 fdc->sc_handle);
757
758 fdc->sc_flags |= FDC_EBUS;
759
760 if (prom_getproplen(ea->ea_node, "manual") >= 0)
761 fdc->sc_flags |= FDC_NOEJECT;
762
763 if (fdcattach(fdc, ea->ea_intr[0]) != 0)
764 if (map_vaddr == 0)
765 bus_space_unmap(ea->ea_bustag, fdc->sc_handle,
766 ea->ea_reg[0].size);
767 }
768 #endif
769
770 int
771 fdcattach(struct fdc_softc *fdc, int pri)
772 {
773 struct fdc_attach_args fa;
774 int drive_attached;
775 char code;
776
777 callout_init(&fdc->sc_timo_ch);
778 callout_init(&fdc->sc_intr_ch);
779
780 fdc->sc_state = DEVIDLE;
781 fdc->sc_itask = FDC_ITASK_NONE;
782 fdc->sc_istatus = FDC_ISTATUS_NONE;
783 fdc->sc_flags |= FDC_EIS;
784 TAILQ_INIT(&fdc->sc_drives);
785
786 if ((fdc->sc_flags & FDC_82077) != 0) {
787 fdc->sc_reg_msr = FDREG77_MSR;
788 fdc->sc_reg_fifo = FDREG77_FIFO;
789 fdc->sc_reg_dor = FDREG77_DOR;
790 fdc->sc_reg_dir = FDREG77_DIR;
791 code = '7';
792 fdc->sc_flags |= FDC_NEEDMOTORWAIT;
793 } else {
794 fdc->sc_reg_msr = FDREG72_MSR;
795 fdc->sc_reg_fifo = FDREG72_FIFO;
796 fdc->sc_reg_dor = 0;
797 code = '2';
798 }
799
800 /*
801 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
802 * Note: CFG_EFIFO is active-low, initial threshold value: 8
803 */
804 fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
805 if (fdconf(fdc) != 0) {
806 printf(": no drives attached\n");
807 return -1;
808 }
809
810 #ifdef SUN4
811 fdc->sc_sicookie = softintr_establish(IPL_BIO, fdcswintr, fdc);
812 #elif SUN4U
813 fdc->sc_sicookie = softintr_establish(IPL_FDSOFT, fdcswintr, fdc);
814 #endif
815 if (fdc->sc_sicookie == NULL) {
816 printf("\n%s: cannot register soft interrupt handler\n",
817 fdc->sc_dev.dv_xname);
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 printf("\n%s: cannot register interrupt handler\n",
840 fdc->sc_dev.dv_xname);
841 callout_stop(&fdc->sc_timo_ch);
842 callout_stop(&fdc->sc_intr_ch);
843 softintr_disestablish(fdc->sc_sicookie);
844 return -1;
845 }
846
847 evcnt_attach_dynamic(&fdc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
848 fdc->sc_dev.dv_xname, "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(struct device *parent, struct cfdata *match, void *aux)
869 {
870 struct fdc_softc *fdc = (void *)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(struct device *parent, struct device *self, void *aux)
947 {
948 struct fdc_softc *fdc = (void *)parent;
949 struct fd_softc *fd = (void *)self;
950 struct fdc_attach_args *fa = aux;
951 struct fd_type *type = fa->fa_deftype;
952 int drive = fa->fa_drive;
953
954 callout_init(&fd->sc_motoron_ch);
955 callout_init(&fd->sc_motoroff_ch);
956
957 /* XXX Allow `flags' to override device type? */
958
959 if (type)
960 printf(": %s %d cyl, %d head, %d sec\n", type->name,
961 type->cylinders, type->heads, type->sectrac);
962 else
963 printf(": density unknown\n");
964
965 bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
966 fd->sc_cylin = -1;
967 fd->sc_drive = drive;
968 fd->sc_deftype = type;
969 fdc->sc_fd[drive] = fd;
970
971 fdc_wrfifo(fdc, NE7CMD_SPECIFY);
972 fdc_wrfifo(fdc, type->steprate);
973 /* XXX head load time == 6ms */
974 fdc_wrfifo(fdc, 6 | NE7_SPECIFY_NODMA);
975
976 /*
977 * Initialize and attach the disk structure.
978 */
979 fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
980 fd->sc_dk.dk_driver = &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_dv);
988
989 fd_set_properties(fd);
990
991 /* Make sure the drive motor gets turned off at shutdown time. */
992 fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
993 }
994
995 inline struct fd_type *
996 fd_dev_to_type(struct fd_softc *fd, dev_t dev)
997 {
998 int type = FDTYPE(dev);
999
1000 if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
1001 return NULL;
1002 return type ? &fd_types[type - 1] : fd->sc_deftype;
1003 }
1004
1005 void
1006 fdstrategy(struct buf *bp)
1007 {
1008 struct fd_softc *fd;
1009 int unit = FDUNIT(bp->b_dev);
1010 int sz;
1011 int s;
1012
1013 /* Valid unit, controller, and request? */
1014 if (unit >= fd_cd.cd_ndevs ||
1015 (fd = fd_cd.cd_devs[unit]) == 0 ||
1016 bp->b_blkno < 0 ||
1017 (((bp->b_bcount % FD_BSIZE(fd)) != 0 ||
1018 (bp->b_blkno * DEV_BSIZE) % FD_BSIZE(fd) != 0) &&
1019 (bp->b_flags & B_FORMAT) == 0)) {
1020 bp->b_error = EINVAL;
1021 goto bad;
1022 }
1023
1024 /* If it's a null transfer, return immediately. */
1025 if (bp->b_bcount == 0)
1026 goto done;
1027
1028 sz = howmany(bp->b_bcount, DEV_BSIZE);
1029
1030 if (bp->b_blkno + sz > (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)) {
1031 sz = (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)
1032 - bp->b_blkno;
1033 if (sz == 0) {
1034 /* If exactly at end of disk, return EOF. */
1035 bp->b_resid = bp->b_bcount;
1036 goto done;
1037 }
1038 if (sz < 0) {
1039 /* If past end of disk, return EINVAL. */
1040 bp->b_error = EINVAL;
1041 goto bad;
1042 }
1043 /* Otherwise, truncate request. */
1044 bp->b_bcount = sz << DEV_BSHIFT;
1045 }
1046
1047 bp->b_rawblkno = bp->b_blkno;
1048 bp->b_cylinder = (bp->b_blkno * DEV_BSIZE) /
1049 (FD_BSIZE(fd) * fd->sc_type->seccyl);
1050
1051 #ifdef FD_DEBUG
1052 if (fdc_debug > 1)
1053 printf("fdstrategy: b_blkno %lld b_bcount %d blkno %lld cylin %d sz %d\n",
1054 (long long)bp->b_blkno, bp->b_bcount,
1055 (long long)fd->sc_blkno, bp->b_cylinder, sz);
1056 #endif
1057
1058 /* Queue transfer on drive, activate drive and controller if idle. */
1059 s = splbio();
1060 BUFQ_PUT(fd->sc_q, bp);
1061 callout_stop(&fd->sc_motoroff_ch); /* a good idea */
1062 if (fd->sc_active == 0)
1063 fdstart(fd);
1064 #ifdef DIAGNOSTIC
1065 else {
1066 struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
1067 if (fdc->sc_state == DEVIDLE) {
1068 printf("fdstrategy: controller inactive\n");
1069 fdcstart(fdc);
1070 }
1071 }
1072 #endif
1073 splx(s);
1074 return;
1075
1076 bad:
1077 bp->b_flags |= B_ERROR;
1078 done:
1079 /* Toss transfer; we're done early. */
1080 biodone(bp);
1081 }
1082
1083 void
1084 fdstart(struct fd_softc *fd)
1085 {
1086 struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
1087 int active = fdc->sc_drives.tqh_first != 0;
1088
1089 /* Link into controller queue. */
1090 fd->sc_active = 1;
1091 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
1092
1093 /* If controller not already active, start it. */
1094 if (!active)
1095 fdcstart(fdc);
1096 }
1097
1098 void
1099 fdfinish(struct fd_softc *fd, struct buf *bp)
1100 {
1101 struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
1102
1103 /*
1104 * Move this drive to the end of the queue to give others a `fair'
1105 * chance. We only force a switch if N operations are completed while
1106 * another drive is waiting to be serviced, since there is a long motor
1107 * startup delay whenever we switch.
1108 */
1109 (void)BUFQ_GET(fd->sc_q);
1110 if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
1111 fd->sc_ops = 0;
1112 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1113 if (BUFQ_PEEK(fd->sc_q) != NULL) {
1114 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
1115 } else
1116 fd->sc_active = 0;
1117 }
1118 bp->b_resid = fd->sc_bcount;
1119 fd->sc_skip = 0;
1120
1121 biodone(bp);
1122 /* turn off motor 5s from now */
1123 callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
1124 fdc->sc_state = DEVIDLE;
1125 }
1126
1127 void
1128 fdc_reset(struct fdc_softc *fdc)
1129 {
1130 bus_space_tag_t t = fdc->sc_bustag;
1131 bus_space_handle_t h = fdc->sc_handle;
1132
1133 if ((fdc->sc_flags & FDC_82077) != 0) {
1134 bus_space_write_1(t, h, fdc->sc_reg_dor,
1135 FDO_FDMAEN | FDO_MOEN(0));
1136 }
1137
1138 bus_space_write_1(t, h, fdc->sc_reg_drs, DRS_RESET);
1139 delay(10);
1140 bus_space_write_1(t, h, fdc->sc_reg_drs, 0);
1141
1142 if ((fdc->sc_flags & FDC_82077) != 0) {
1143 bus_space_write_1(t, h, fdc->sc_reg_dor,
1144 FDO_FRST | FDO_FDMAEN | FDO_DS);
1145 }
1146 #ifdef FD_DEBUG
1147 if (fdc_debug)
1148 printf("fdc reset\n");
1149 #endif
1150 }
1151
1152 void
1153 fd_set_motor(struct fdc_softc *fdc)
1154 {
1155 struct fd_softc *fd;
1156 u_char status;
1157 int n;
1158
1159 if ((fdc->sc_flags & FDC_82077) != 0) {
1160 status = FDO_FRST | FDO_FDMAEN;
1161 if ((fd = fdc->sc_drives.tqh_first) != NULL)
1162 status |= fd->sc_drive;
1163
1164 for (n = 0; n < 4; n++)
1165 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
1166 status |= FDO_MOEN(n);
1167 bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
1168 fdc->sc_reg_dor, status);
1169 #ifdef SUN4
1170 } else {
1171
1172 for (n = 0; n < 4; n++) {
1173 if ((fd = fdc->sc_fd[n]) != NULL &&
1174 (fd->sc_flags & FD_MOTOR) != 0) {
1175 auxregbisc(AUXIO4C_FDS, 0);
1176 return;
1177 }
1178 }
1179 auxregbisc(0, AUXIO4C_FDS);
1180 #endif
1181 }
1182 }
1183
1184 void
1185 fd_motor_off(void *arg)
1186 {
1187 struct fd_softc *fd = arg;
1188 int s;
1189
1190 s = splbio();
1191 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1192 fd_set_motor((struct fdc_softc *)device_parent(&fd->sc_dv));
1193 splx(s);
1194 }
1195
1196 void
1197 fd_motor_on(void *arg)
1198 {
1199 struct fd_softc *fd = arg;
1200 struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
1201 int s;
1202
1203 s = splbio();
1204 fd->sc_flags &= ~FD_MOTOR_WAIT;
1205 if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
1206 (void)fdcstate(fdc);
1207 splx(s);
1208 }
1209
1210 /*
1211 * Get status bytes off the FDC after a command has finished
1212 * Returns the number of status bytes read; -1 on error.
1213 * The return value is also stored in `sc_nstat'.
1214 */
1215 int
1216 fdcresult(struct fdc_softc *fdc)
1217 {
1218 bus_space_tag_t t = fdc->sc_bustag;
1219 bus_space_handle_t h = fdc->sc_handle;
1220 int j, n = 0;
1221
1222 for (j = 10000; j; j--) {
1223 uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
1224 v &= (NE7_DIO | NE7_RQM | NE7_CB);
1225 if (v == NE7_RQM)
1226 return fdc->sc_nstat = n;
1227 if (v == (NE7_DIO | NE7_RQM | NE7_CB)) {
1228 if (n >= sizeof(fdc->sc_status)) {
1229 log(LOG_ERR, "fdcresult: overrun\n");
1230 return -1;
1231 }
1232 fdc->sc_status[n++] =
1233 bus_space_read_1(t, h, fdc->sc_reg_fifo);
1234 } else
1235 delay(1);
1236 }
1237
1238 log(LOG_ERR, "fdcresult: timeout\n");
1239 return fdc->sc_nstat = -1;
1240 }
1241
1242 /*
1243 * Write a command byte to the FDC.
1244 * Returns 0 on success; -1 on failure (i.e. timeout)
1245 */
1246 int
1247 fdc_wrfifo(struct fdc_softc *fdc, uint8_t x)
1248 {
1249 bus_space_tag_t t = fdc->sc_bustag;
1250 bus_space_handle_t h = fdc->sc_handle;
1251 int i;
1252
1253 for (i = 100000; i-- > 0;) {
1254 uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
1255 if ((v & (NE7_DIO|NE7_RQM)) == NE7_RQM) {
1256 /* The chip is ready */
1257 bus_space_write_1(t, h, fdc->sc_reg_fifo, x);
1258 return 0;
1259 }
1260 delay(1);
1261 }
1262 return -1;
1263 }
1264
1265 int
1266 fdc_diskchange(struct fdc_softc *fdc)
1267 {
1268
1269 #ifdef SUN4
1270 if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
1271 #endif
1272 bus_space_tag_t t = fdc->sc_bustag;
1273 bus_space_handle_t h = fdc->sc_handle;
1274 uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_dir);
1275 return (v & FDI_DCHG) != 0;
1276 #ifdef SUN4
1277 } else if (CPU_ISSUN4C) {
1278 return (*AUXIO4C_REG & AUXIO4C_FDC) != 0;
1279 }
1280 return 0;
1281 #endif
1282 }
1283
1284 int
1285 fdopen(dev_t dev, int flags, int fmt, struct lwp *l)
1286 {
1287 int unit, pmask;
1288 struct fd_softc *fd;
1289 struct fd_type *type;
1290
1291 unit = FDUNIT(dev);
1292 if (unit >= fd_cd.cd_ndevs)
1293 return ENXIO;
1294 fd = fd_cd.cd_devs[unit];
1295 if (fd == NULL)
1296 return ENXIO;
1297 type = fd_dev_to_type(fd, dev);
1298 if (type == NULL)
1299 return ENXIO;
1300
1301 if ((fd->sc_flags & FD_OPEN) != 0 &&
1302 fd->sc_type != type)
1303 return EBUSY;
1304
1305 fd->sc_type = type;
1306 fd->sc_cylin = -1;
1307 fd->sc_flags |= FD_OPEN;
1308
1309 /*
1310 * Only update the disklabel if we're not open anywhere else.
1311 */
1312 if (fd->sc_dk.dk_openmask == 0)
1313 fdgetdisklabel(dev);
1314
1315 pmask = (1 << DISKPART(dev));
1316
1317 switch (fmt) {
1318 case S_IFCHR:
1319 fd->sc_dk.dk_copenmask |= pmask;
1320 break;
1321
1322 case S_IFBLK:
1323 fd->sc_dk.dk_bopenmask |= pmask;
1324 break;
1325 }
1326 fd->sc_dk.dk_openmask =
1327 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
1328
1329 return 0;
1330 }
1331
1332 int
1333 fdclose(dev_t dev, int flags, int fmt, struct lwp *l)
1334 {
1335 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1336 int pmask = (1 << DISKPART(dev));
1337
1338 fd->sc_flags &= ~FD_OPEN;
1339 fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
1340
1341 switch (fmt) {
1342 case S_IFCHR:
1343 fd->sc_dk.dk_copenmask &= ~pmask;
1344 break;
1345
1346 case S_IFBLK:
1347 fd->sc_dk.dk_bopenmask &= ~pmask;
1348 break;
1349 }
1350 fd->sc_dk.dk_openmask =
1351 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
1352
1353 return 0;
1354 }
1355
1356 int
1357 fdread(dev_t dev, struct uio *uio, int flag)
1358 {
1359
1360 return physio(fdstrategy, NULL, dev, B_READ, minphys, uio);
1361 }
1362
1363 int
1364 fdwrite(dev_t dev, struct uio *uio, int flag)
1365 {
1366
1367 return physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio);
1368 }
1369
1370 void
1371 fdcstart(struct fdc_softc *fdc)
1372 {
1373
1374 #ifdef DIAGNOSTIC
1375 /* only got here if controller's drive queue was inactive; should
1376 be in idle state */
1377 if (fdc->sc_state != DEVIDLE) {
1378 printf("fdcstart: not idle\n");
1379 return;
1380 }
1381 #endif
1382 (void)fdcstate(fdc);
1383 }
1384
1385 void
1386 fdcstatus(struct fdc_softc *fdc, const char *s)
1387 {
1388 struct fd_softc *fd = fdc->sc_drives.tqh_first;
1389 int n;
1390 char bits[64];
1391
1392 /* Just print last status */
1393 n = fdc->sc_nstat;
1394
1395 #if 0
1396 /*
1397 * A 82072 seems to return <invalid command> on
1398 * gratuitous Sense Interrupt commands.
1399 */
1400 if (n == 0 && (fdc->sc_flags & FDC_82077) != 0) {
1401 fdc_wrfifo(fdc, NE7CMD_SENSEI);
1402 (void)fdcresult(fdc);
1403 n = 2;
1404 }
1405 #endif
1406
1407 printf("%s: %s: state %d",
1408 fd ? fd->sc_dv.dv_xname : "fdc", s, fdc->sc_state);
1409
1410 switch (n) {
1411 case 0:
1412 printf("\n");
1413 break;
1414 case 2:
1415 printf(" (st0 %s cyl %d)\n",
1416 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
1417 bits, sizeof(bits)), fdc->sc_status[1]);
1418 break;
1419 case 7:
1420 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
1421 NE7_ST0BITS, bits, sizeof(bits)));
1422 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
1423 NE7_ST1BITS, bits, sizeof(bits)));
1424 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
1425 NE7_ST2BITS, bits, sizeof(bits)));
1426 printf(" cyl %d head %d sec %d)\n",
1427 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1428 break;
1429 #ifdef DIAGNOSTIC
1430 default:
1431 printf(" fdcstatus: weird size: %d\n", n);
1432 break;
1433 #endif
1434 }
1435 }
1436
1437 void
1438 fdctimeout(void *arg)
1439 {
1440 struct fdc_softc *fdc = arg;
1441 struct fd_softc *fd;
1442 int s;
1443
1444 s = splbio();
1445 fd = fdc->sc_drives.tqh_first;
1446 if (fd == NULL) {
1447 printf("%s: timeout but no I/O pending: state %d, istatus=%d\n",
1448 fdc->sc_dev.dv_xname,
1449 fdc->sc_state, fdc->sc_istatus);
1450 fdc->sc_state = DEVIDLE;
1451 goto out;
1452 }
1453
1454 if (BUFQ_PEEK(fd->sc_q) != NULL)
1455 fdc->sc_state++;
1456 else
1457 fdc->sc_state = DEVIDLE;
1458
1459 (void)fdcstate(fdc);
1460 out:
1461 splx(s);
1462
1463 }
1464
1465 void
1466 fdcpseudointr(void *arg)
1467 {
1468 struct fdc_softc *fdc = arg;
1469 int s;
1470
1471 /* Just ensure it has the right spl. */
1472 s = splbio();
1473 (void)fdcstate(fdc);
1474 splx(s);
1475 }
1476
1477
1478 /*
1479 * hardware interrupt entry point: used only if no `fast trap' * (in-window)
1480 * handler is available. Unfortunately, we have no reliable way to
1481 * determine that the interrupt really came from the floppy controller;
1482 * just hope that the other devices that share this interrupt level
1483 * can do better..
1484 */
1485 int
1486 fdc_c_hwintr(void *arg)
1487 {
1488 struct fdc_softc *fdc = arg;
1489 bus_space_tag_t t = fdc->sc_bustag;
1490 bus_space_handle_t h = fdc->sc_handle;
1491
1492 switch (fdc->sc_itask) {
1493 case FDC_ITASK_NONE:
1494 return 0;
1495 case FDC_ITASK_SENSEI:
1496 if (fdc_wrfifo(fdc, NE7CMD_SENSEI) != 0 || fdcresult(fdc) == -1)
1497 fdc->sc_istatus = FDC_ISTATUS_ERROR;
1498 else
1499 fdc->sc_istatus = FDC_ISTATUS_DONE;
1500 softintr_schedule(fdc->sc_sicookie);
1501 return 1;
1502 case FDC_ITASK_RESULT:
1503 if (fdcresult(fdc) == -1)
1504 fdc->sc_istatus = FDC_ISTATUS_ERROR;
1505 else
1506 fdc->sc_istatus = FDC_ISTATUS_DONE;
1507 softintr_schedule(fdc->sc_sicookie);
1508 return 1;
1509 case FDC_ITASK_DMA:
1510 /* Proceed with pseudo-DMA below */
1511 break;
1512 default:
1513 printf("fdc: stray hard interrupt: itask=%d\n", fdc->sc_itask);
1514 fdc->sc_istatus = FDC_ISTATUS_SPURIOUS;
1515 softintr_schedule(fdc->sc_sicookie);
1516 return 1;
1517 }
1518
1519 /*
1520 * Pseudo DMA in progress
1521 */
1522 for (;;) {
1523 uint8_t msr;
1524
1525 msr = bus_space_read_1(t, h, fdc->sc_reg_msr);
1526
1527 if ((msr & NE7_RQM) == 0)
1528 /* That's all this round */
1529 break;
1530
1531 if ((msr & NE7_NDM) == 0) {
1532 fdcresult(fdc);
1533 fdc->sc_istatus = FDC_ISTATUS_DONE;
1534 softintr_schedule(fdc->sc_sicookie);
1535 #ifdef FD_DEBUG
1536 if (fdc_debug > 1)
1537 #ifdef SUN4
1538 printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
1539 #elif SUN4U
1540 printf("fdc: overrun: msr = %x, tc = %d\n",
1541 msr, fdc->sc_tc);
1542 #endif
1543 #endif
1544 break;
1545 }
1546
1547 /* Another byte can be transferred */
1548 if ((msr & NE7_DIO) != 0)
1549 *fdc->sc_data =
1550 bus_space_read_1(t, h, fdc->sc_reg_fifo);
1551 else
1552 bus_space_write_1(t, h, fdc->sc_reg_fifo,
1553 *fdc->sc_data);
1554
1555 fdc->sc_data++;
1556 if (--fdc->sc_tc == 0) {
1557 fdc->sc_istatus = FDC_ISTATUS_DONE;
1558 FTC_FLIP;
1559 fdcresult(fdc);
1560 softintr_schedule(fdc->sc_sicookie);
1561 break;
1562 }
1563 }
1564 return 1;
1565 }
1566
1567 void
1568 fdcswintr(void *arg)
1569 {
1570 struct fdc_softc *fdc = arg;
1571
1572 if (fdc->sc_istatus == FDC_ISTATUS_NONE)
1573 /* This (software) interrupt is not for us */
1574 return;
1575
1576 switch (fdc->sc_istatus) {
1577 case FDC_ISTATUS_ERROR:
1578 printf("fdc: ierror status: state %d\n", fdc->sc_state);
1579 break;
1580 case FDC_ISTATUS_SPURIOUS:
1581 printf("fdc: spurious interrupt: state %d\n", fdc->sc_state);
1582 break;
1583 }
1584
1585 fdcstate(fdc);
1586 return;
1587 }
1588
1589 int
1590 fdcstate(struct fdc_softc *fdc)
1591 {
1592
1593 #define st0 fdc->sc_status[0]
1594 #define st1 fdc->sc_status[1]
1595 #define cyl fdc->sc_status[1]
1596 #define FDC_WRFIFO(fdc, c) do { \
1597 if (fdc_wrfifo(fdc, (c))) { \
1598 goto xxx; \
1599 } \
1600 } while(0)
1601
1602 struct fd_softc *fd;
1603 struct buf *bp;
1604 int read, head, sec, nblks;
1605 struct fd_type *type;
1606 struct ne7_fd_formb *finfo = NULL;
1607
1608 if (fdc->sc_istatus == FDC_ISTATUS_ERROR) {
1609 /* Prevent loop if the reset sequence produces errors */
1610 if (fdc->sc_state != RESETCOMPLETE &&
1611 fdc->sc_state != RECALWAIT &&
1612 fdc->sc_state != RECALCOMPLETE)
1613 fdc->sc_state = DORESET;
1614 }
1615
1616 /* Clear I task/status field */
1617 fdc->sc_istatus = FDC_ISTATUS_NONE;
1618 fdc->sc_itask = FDC_ITASK_NONE;
1619
1620 loop:
1621 /* Is there a drive for the controller to do a transfer with? */
1622 fd = fdc->sc_drives.tqh_first;
1623 if (fd == NULL) {
1624 fdc->sc_state = DEVIDLE;
1625 return 0;
1626 }
1627
1628 /* Is there a transfer to this drive? If not, deactivate drive. */
1629 bp = BUFQ_PEEK(fd->sc_q);
1630 if (bp == NULL) {
1631 fd->sc_ops = 0;
1632 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1633 fd->sc_active = 0;
1634 goto loop;
1635 }
1636
1637 if (bp->b_flags & B_FORMAT)
1638 finfo = (struct ne7_fd_formb *)bp->b_data;
1639
1640 switch (fdc->sc_state) {
1641 case DEVIDLE:
1642 fdc->sc_errors = 0;
1643 fd->sc_skip = 0;
1644 fd->sc_bcount = bp->b_bcount;
1645 fd->sc_blkno = (bp->b_blkno * DEV_BSIZE) / FD_BSIZE(fd);
1646 callout_stop(&fd->sc_motoroff_ch);
1647 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1648 fdc->sc_state = MOTORWAIT;
1649 return 1;
1650 }
1651 if ((fd->sc_flags & FD_MOTOR) == 0) {
1652 /* Turn on the motor, being careful about pairing. */
1653 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1654 if (ofd && ofd->sc_flags & FD_MOTOR) {
1655 callout_stop(&ofd->sc_motoroff_ch);
1656 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1657 }
1658 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1659 fd_set_motor(fdc);
1660 fdc->sc_state = MOTORWAIT;
1661 if ((fdc->sc_flags & FDC_NEEDMOTORWAIT) != 0) { /*XXX*/
1662 /* Allow .25s for motor to stabilize. */
1663 callout_reset(&fd->sc_motoron_ch, hz / 4,
1664 fd_motor_on, fd);
1665 } else {
1666 fd->sc_flags &= ~FD_MOTOR_WAIT;
1667 goto loop;
1668 }
1669 return 1;
1670 }
1671 /* Make sure the right drive is selected. */
1672 fd_set_motor(fdc);
1673
1674 if (fdc_diskchange(fdc))
1675 goto dodskchg;
1676
1677 /*FALLTHROUGH*/
1678 case DOSEEK:
1679 doseek:
1680 if ((fdc->sc_flags & FDC_EIS) &&
1681 (bp->b_flags & B_FORMAT) == 0) {
1682 fd->sc_cylin = bp->b_cylinder;
1683 /* We use implied seek */
1684 goto doio;
1685 }
1686
1687 if (fd->sc_cylin == bp->b_cylinder)
1688 goto doio;
1689
1690 fd->sc_cylin = -1;
1691 fdc->sc_state = SEEKWAIT;
1692 fdc->sc_nstat = 0;
1693
1694 iostat_seek(fd->sc_dk.dk_stats);
1695
1696 disk_busy(&fd->sc_dk);
1697 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1698
1699 /* specify command */
1700 FDC_WRFIFO(fdc, NE7CMD_SPECIFY);
1701 FDC_WRFIFO(fdc, fd->sc_type->steprate);
1702 /* XXX head load time == 6ms */
1703 FDC_WRFIFO(fdc, 6 | NE7_SPECIFY_NODMA);
1704
1705 fdc->sc_itask = FDC_ITASK_SENSEI;
1706 /* seek function */
1707 FDC_WRFIFO(fdc, NE7CMD_SEEK);
1708 FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
1709 FDC_WRFIFO(fdc, bp->b_cylinder * fd->sc_type->step);
1710 return 1;
1711
1712 case DODSKCHG:
1713 dodskchg:
1714 /*
1715 * Disk change: force a seek operation by going to cyl 1
1716 * followed by a recalibrate.
1717 */
1718 disk_busy(&fd->sc_dk);
1719 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1720 fd->sc_cylin = -1;
1721 fdc->sc_nstat = 0;
1722 fdc->sc_state = DSKCHGWAIT;
1723
1724 fdc->sc_itask = FDC_ITASK_SENSEI;
1725 /* seek function */
1726 FDC_WRFIFO(fdc, NE7CMD_SEEK);
1727 FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
1728 FDC_WRFIFO(fdc, 1 * fd->sc_type->step);
1729 return 1;
1730
1731 case DSKCHGWAIT:
1732 callout_stop(&fdc->sc_timo_ch);
1733 disk_unbusy(&fd->sc_dk, 0, 0);
1734 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1735 cyl != 1 * fd->sc_type->step) {
1736 fdcstatus(fdc, "dskchg seek failed");
1737 fdc->sc_state = DORESET;
1738 } else
1739 fdc->sc_state = DORECAL;
1740
1741 if (fdc_diskchange(fdc)) {
1742 printf("%s: cannot clear disk change status\n",
1743 fdc->sc_dev.dv_xname);
1744 fdc->sc_state = DORESET;
1745 }
1746 goto loop;
1747
1748 case DOIO:
1749 doio:
1750 if (finfo != NULL)
1751 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1752 (char *)finfo;
1753 type = fd->sc_type;
1754 sec = fd->sc_blkno % type->seccyl;
1755 nblks = type->seccyl - sec;
1756 nblks = min(nblks, fd->sc_bcount / FD_BSIZE(fd));
1757 nblks = min(nblks, FDC_MAXIOSIZE / FD_BSIZE(fd));
1758 fd->sc_nblks = nblks;
1759 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FD_BSIZE(fd);
1760 head = sec / type->sectrac;
1761 sec -= head * type->sectrac;
1762 #ifdef DIAGNOSTIC
1763 {int block;
1764 block = (fd->sc_cylin * type->heads + head) * type->sectrac +
1765 sec;
1766 if (block != fd->sc_blkno) {
1767 printf("fdcintr: block %d != blkno %d\n", block,
1768 (int)fd->sc_blkno);
1769 #ifdef DDB
1770 Debugger();
1771 #endif
1772 }}
1773 #endif
1774 read = bp->b_flags & B_READ;
1775
1776 /* Setup for pseudo DMA */
1777 fdc->sc_data = (char *)bp->b_data + fd->sc_skip;
1778 fdc->sc_tc = fd->sc_nbytes;
1779
1780 bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
1781 fdc->sc_reg_drs, type->rate);
1782 #ifdef FD_DEBUG
1783 if (fdc_debug > 1)
1784 printf("fdcstate: doio: %s drive %d "
1785 "track %d head %d sec %d nblks %d\n",
1786 finfo ? "format" :
1787 (read ? "read" : "write"),
1788 fd->sc_drive, fd->sc_cylin, head, sec, nblks);
1789 #endif
1790 fdc->sc_state = IOCOMPLETE;
1791 fdc->sc_itask = FDC_ITASK_DMA;
1792 fdc->sc_nstat = 0;
1793
1794 disk_busy(&fd->sc_dk);
1795
1796 /* allow 3 seconds for operation */
1797 callout_reset(&fdc->sc_timo_ch, 3 * hz, fdctimeout, fdc);
1798
1799 if (finfo != NULL) {
1800 /* formatting */
1801 FDC_WRFIFO(fdc, NE7CMD_FORMAT);
1802 FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
1803 FDC_WRFIFO(fdc, finfo->fd_formb_secshift);
1804 FDC_WRFIFO(fdc, finfo->fd_formb_nsecs);
1805 FDC_WRFIFO(fdc, finfo->fd_formb_gaplen);
1806 FDC_WRFIFO(fdc, finfo->fd_formb_fillbyte);
1807 } else {
1808 if (read)
1809 FDC_WRFIFO(fdc, NE7CMD_READ);
1810 else
1811 FDC_WRFIFO(fdc, NE7CMD_WRITE);
1812 FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
1813 FDC_WRFIFO(fdc, fd->sc_cylin); /*track*/
1814 FDC_WRFIFO(fdc, head);
1815 FDC_WRFIFO(fdc, sec + 1); /*sector+1*/
1816 FDC_WRFIFO(fdc, type->secsize); /*sector size*/
1817 FDC_WRFIFO(fdc, type->sectrac); /*secs/track*/
1818 FDC_WRFIFO(fdc, type->gap1); /*gap1 size*/
1819 FDC_WRFIFO(fdc, type->datalen); /*data length*/
1820 }
1821
1822 return 1; /* will return later */
1823
1824 case SEEKWAIT:
1825 callout_stop(&fdc->sc_timo_ch);
1826 fdc->sc_state = SEEKCOMPLETE;
1827 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1828 /* allow 1/50 second for heads to settle */
1829 callout_reset(&fdc->sc_intr_ch, hz / 50,
1830 fdcpseudointr, fdc);
1831 return 1; /* will return later */
1832 }
1833 /*FALLTHROUGH*/
1834 case SEEKCOMPLETE:
1835 /* no data on seek */
1836 disk_unbusy(&fd->sc_dk, 0, 0);
1837
1838 /* Make sure seek really happened. */
1839 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1840 cyl != bp->b_cylinder * fd->sc_type->step) {
1841 #ifdef FD_DEBUG
1842 if (fdc_debug)
1843 fdcstatus(fdc, "seek failed");
1844 #endif
1845 fdcretry(fdc);
1846 goto loop;
1847 }
1848 fd->sc_cylin = bp->b_cylinder;
1849 goto doio;
1850
1851 case IOTIMEDOUT:
1852 /*
1853 * Try to abort the I/O operation without resetting
1854 * the chip first. Poke TC and arrange to pick up
1855 * the timed out I/O command's status.
1856 */
1857 fdc->sc_itask = FDC_ITASK_RESULT;
1858 fdc->sc_state = IOCLEANUPWAIT;
1859 fdc->sc_nstat = 0;
1860 /* 1/10 second should be enough */
1861 callout_reset(&fdc->sc_timo_ch, hz / 10, fdctimeout, fdc);
1862 FTC_FLIP;
1863 return 1;
1864
1865 case IOCLEANUPTIMEDOUT:
1866 case SEEKTIMEDOUT:
1867 case RECALTIMEDOUT:
1868 case RESETTIMEDOUT:
1869 case DSKCHGTIMEDOUT:
1870 fdcstatus(fdc, "timeout");
1871
1872 /* All other timeouts always roll through to a chip reset */
1873 fdcretry(fdc);
1874
1875 /* Force reset, no matter what fdcretry() says */
1876 fdc->sc_state = DORESET;
1877 goto loop;
1878
1879 case IOCLEANUPWAIT: /* IO FAILED, cleanup succeeded */
1880 callout_stop(&fdc->sc_timo_ch);
1881 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
1882 (bp->b_flags & B_READ));
1883 fdcretry(fdc);
1884 goto loop;
1885
1886 case IOCOMPLETE: /* IO DONE, post-analyze */
1887 callout_stop(&fdc->sc_timo_ch);
1888
1889 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
1890 (bp->b_flags & B_READ));
1891
1892 if (fdc->sc_nstat != 7 || st1 != 0 ||
1893 ((st0 & 0xf8) != 0 &&
1894 ((st0 & 0xf8) != 0x20 || (fdc->sc_cfg & CFG_EIS) == 0))) {
1895 #ifdef FD_DEBUG
1896 if (fdc_debug) {
1897 fdcstatus(fdc, bp->b_flags & B_READ ?
1898 "read failed" : "write failed");
1899 printf("blkno %lld nblks %d nstat %d tc %d\n",
1900 (long long)fd->sc_blkno, fd->sc_nblks,
1901 fdc->sc_nstat, fdc->sc_tc);
1902 }
1903 #endif
1904 if (fdc->sc_nstat == 7 &&
1905 (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
1906
1907 /*
1908 * Silently retry overruns if no other
1909 * error bit is set. Adjust threshold.
1910 */
1911 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1912 if (thr < 15) {
1913 thr++;
1914 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1915 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1916 #ifdef FD_DEBUG
1917 if (fdc_debug)
1918 printf("fdc: %d -> threshold\n",
1919 thr);
1920 #endif
1921 fdconf(fdc);
1922 fdc->sc_overruns = 0;
1923 }
1924 if (++fdc->sc_overruns < 3) {
1925 fdc->sc_state = DOIO;
1926 goto loop;
1927 }
1928 }
1929 fdcretry(fdc);
1930 goto loop;
1931 }
1932 if (fdc->sc_errors) {
1933 diskerr(bp, "fd", "soft error", LOG_PRINTF,
1934 fd->sc_skip / FD_BSIZE(fd),
1935 (struct disklabel *)NULL);
1936 printf("\n");
1937 fdc->sc_errors = 0;
1938 } else {
1939 if (--fdc->sc_overruns < -20) {
1940 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1941 if (thr > 0) {
1942 thr--;
1943 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1944 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1945 #ifdef FD_DEBUG
1946 if (fdc_debug)
1947 printf("fdc: %d -> threshold\n",
1948 thr);
1949 #endif
1950 fdconf(fdc);
1951 }
1952 fdc->sc_overruns = 0;
1953 }
1954 }
1955 fd->sc_blkno += fd->sc_nblks;
1956 fd->sc_skip += fd->sc_nbytes;
1957 fd->sc_bcount -= fd->sc_nbytes;
1958 if (finfo == NULL && fd->sc_bcount > 0) {
1959 bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
1960 goto doseek;
1961 }
1962 fdfinish(fd, bp);
1963 goto loop;
1964
1965 case DORESET:
1966 /* try a reset, keep motor on */
1967 fd_set_motor(fdc);
1968 delay(100);
1969 fdc->sc_nstat = 0;
1970 fdc->sc_itask = FDC_ITASK_SENSEI;
1971 fdc->sc_state = RESETCOMPLETE;
1972 callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
1973 fdc_reset(fdc);
1974 return 1; /* will return later */
1975
1976 case RESETCOMPLETE:
1977 callout_stop(&fdc->sc_timo_ch);
1978 fdconf(fdc);
1979
1980 /* FALLTHROUGH */
1981 case DORECAL:
1982 fdc->sc_state = RECALWAIT;
1983 fdc->sc_itask = FDC_ITASK_SENSEI;
1984 fdc->sc_nstat = 0;
1985 callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1986 /* recalibrate function */
1987 FDC_WRFIFO(fdc, NE7CMD_RECAL);
1988 FDC_WRFIFO(fdc, fd->sc_drive);
1989 return 1; /* will return later */
1990
1991 case RECALWAIT:
1992 callout_stop(&fdc->sc_timo_ch);
1993 fdc->sc_state = RECALCOMPLETE;
1994 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1995 /* allow 1/30 second for heads to settle */
1996 callout_reset(&fdc->sc_intr_ch, hz / 30,
1997 fdcpseudointr, fdc);
1998 return 1; /* will return later */
1999 }
2000
2001 case RECALCOMPLETE:
2002 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
2003 #ifdef FD_DEBUG
2004 if (fdc_debug)
2005 fdcstatus(fdc, "recalibrate failed");
2006 #endif
2007 fdcretry(fdc);
2008 goto loop;
2009 }
2010 fd->sc_cylin = 0;
2011 goto doseek;
2012
2013 case MOTORWAIT:
2014 if (fd->sc_flags & FD_MOTOR_WAIT)
2015 return 1; /* time's not up yet */
2016 goto doseek;
2017
2018 default:
2019 fdcstatus(fdc, "stray interrupt");
2020 return 1;
2021 }
2022 #ifdef DIAGNOSTIC
2023 panic("fdcintr: impossible");
2024 #endif
2025
2026 xxx:
2027 /*
2028 * We get here if the chip locks up in FDC_WRFIFO()
2029 * Cancel any operation and schedule a reset
2030 */
2031 callout_stop(&fdc->sc_timo_ch);
2032 fdcretry(fdc);
2033 fdc->sc_state = DORESET;
2034 goto loop;
2035
2036 #undef st0
2037 #undef st1
2038 #undef cyl
2039 }
2040
2041 void
2042 fdcretry(struct fdc_softc *fdc)
2043 {
2044 struct fd_softc *fd;
2045 struct buf *bp;
2046 int error = EIO;
2047
2048 fd = fdc->sc_drives.tqh_first;
2049 bp = BUFQ_PEEK(fd->sc_q);
2050
2051 fdc->sc_overruns = 0;
2052 if (fd->sc_opts & FDOPT_NORETRY)
2053 goto fail;
2054
2055 switch (fdc->sc_errors) {
2056 case 0:
2057 if (fdc->sc_nstat == 7 &&
2058 (fdc->sc_status[0] & 0xd8) == 0x40 &&
2059 (fdc->sc_status[1] & 0x2) == 0x2) {
2060 printf("%s: read-only medium\n", fd->sc_dv.dv_xname);
2061 error = EROFS;
2062 goto failsilent;
2063 }
2064 /* try again */
2065 fdc->sc_state =
2066 (fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
2067 break;
2068
2069 case 1: case 2: case 3:
2070 /* didn't work; try recalibrating */
2071 fdc->sc_state = DORECAL;
2072 break;
2073
2074 case 4:
2075 if (fdc->sc_nstat == 7 &&
2076 fdc->sc_status[0] == 0 &&
2077 fdc->sc_status[1] == 0 &&
2078 fdc->sc_status[2] == 0) {
2079 /*
2080 * We've retried a few times and we've got
2081 * valid status and all three status bytes
2082 * are zero. Assume this condition is the
2083 * result of no disk loaded into the drive.
2084 */
2085 printf("%s: no medium?\n", fd->sc_dv.dv_xname);
2086 error = ENODEV;
2087 goto failsilent;
2088 }
2089
2090 /* still no go; reset the bastard */
2091 fdc->sc_state = DORESET;
2092 break;
2093
2094 default:
2095 fail:
2096 if ((fd->sc_opts & FDOPT_SILENT) == 0) {
2097 diskerr(bp, "fd", "hard error", LOG_PRINTF,
2098 fd->sc_skip / FD_BSIZE(fd),
2099 (struct disklabel *)NULL);
2100 printf("\n");
2101 fdcstatus(fdc, "controller status");
2102 }
2103
2104 failsilent:
2105 bp->b_flags |= B_ERROR;
2106 bp->b_error = error;
2107 fdfinish(fd, bp);
2108 }
2109 fdc->sc_errors++;
2110 }
2111
2112 int
2113 fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
2114 {
2115 struct fd_softc *fd;
2116 struct fdc_softc *fdc;
2117 struct fdformat_parms *form_parms;
2118 struct fdformat_cmd *form_cmd;
2119 struct ne7_fd_formb *fd_formb;
2120 int il[FD_MAX_NSEC + 1];
2121 int unit;
2122 int i, j;
2123 int error;
2124
2125 unit = FDUNIT(dev);
2126 if (unit >= fd_cd.cd_ndevs)
2127 return ENXIO;
2128
2129 fd = fd_cd.cd_devs[FDUNIT(dev)];
2130 fdc = (struct fdc_softc *)device_parent(&fd->sc_dv);
2131
2132 switch (cmd) {
2133 case DIOCGDINFO:
2134 *(struct disklabel *)addr = *(fd->sc_dk.dk_label);
2135 return 0;
2136
2137 case DIOCWLABEL:
2138 if ((flag & FWRITE) == 0)
2139 return EBADF;
2140 /* XXX do something */
2141 return 0;
2142
2143 case DIOCWDINFO:
2144 if ((flag & FWRITE) == 0)
2145 return EBADF;
2146
2147 error = setdisklabel(fd->sc_dk.dk_label,
2148 (struct disklabel *)addr, 0,
2149 fd->sc_dk.dk_cpulabel);
2150 if (error)
2151 return error;
2152
2153 error = writedisklabel(dev, fdstrategy,
2154 fd->sc_dk.dk_label,
2155 fd->sc_dk.dk_cpulabel);
2156 return error;
2157
2158 case DIOCLOCK:
2159 /*
2160 * Nothing to do here, really.
2161 */
2162 return 0;
2163
2164 case DIOCEJECT:
2165 if (*(int *)addr == 0) {
2166 int part = DISKPART(dev);
2167 /*
2168 * Don't force eject: check that we are the only
2169 * partition open. If so, unlock it.
2170 */
2171 if ((fd->sc_dk.dk_openmask & ~(1 << part)) != 0 ||
2172 fd->sc_dk.dk_bopenmask + fd->sc_dk.dk_copenmask !=
2173 fd->sc_dk.dk_openmask) {
2174 return EBUSY;
2175 }
2176 }
2177 /* FALLTHROUGH */
2178 case ODIOCEJECT:
2179 if (fdc->sc_flags & FDC_NOEJECT)
2180 return EINVAL;
2181 fd_do_eject(fd);
2182 return 0;
2183
2184 case FDIOCGETFORMAT:
2185 form_parms = (struct fdformat_parms *)addr;
2186 form_parms->fdformat_version = FDFORMAT_VERSION;
2187 form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
2188 form_parms->ncyl = fd->sc_type->cylinders;
2189 form_parms->nspt = fd->sc_type->sectrac;
2190 form_parms->ntrk = fd->sc_type->heads;
2191 form_parms->stepspercyl = fd->sc_type->step;
2192 form_parms->gaplen = fd->sc_type->gap2;
2193 form_parms->fillbyte = fd->sc_type->fillbyte;
2194 form_parms->interleave = fd->sc_type->interleave;
2195 switch (fd->sc_type->rate) {
2196 case FDC_500KBPS:
2197 form_parms->xfer_rate = 500 * 1024;
2198 break;
2199 case FDC_300KBPS:
2200 form_parms->xfer_rate = 300 * 1024;
2201 break;
2202 case FDC_250KBPS:
2203 form_parms->xfer_rate = 250 * 1024;
2204 break;
2205 default:
2206 return EINVAL;
2207 }
2208 return 0;
2209
2210 case FDIOCSETFORMAT:
2211 if ((flag & FWRITE) == 0)
2212 return EBADF; /* must be opened for writing */
2213
2214 form_parms = (struct fdformat_parms *)addr;
2215 if (form_parms->fdformat_version != FDFORMAT_VERSION)
2216 return EINVAL;/* wrong version of formatting prog */
2217
2218 i = form_parms->nbps >> 7;
2219 if ((form_parms->nbps & 0x7f) || ffs(i) == 0 ||
2220 i & ~(1 << (ffs(i)-1)))
2221 /* not a power-of-two multiple of 128 */
2222 return EINVAL;
2223
2224 switch (form_parms->xfer_rate) {
2225 case 500 * 1024:
2226 fd->sc_type->rate = FDC_500KBPS;
2227 break;
2228 case 300 * 1024:
2229 fd->sc_type->rate = FDC_300KBPS;
2230 break;
2231 case 250 * 1024:
2232 fd->sc_type->rate = FDC_250KBPS;
2233 break;
2234 default:
2235 return EINVAL;
2236 }
2237
2238 if (form_parms->nspt > FD_MAX_NSEC ||
2239 form_parms->fillbyte > 0xff ||
2240 form_parms->interleave > 0xff)
2241 return EINVAL;
2242 fd->sc_type->sectrac = form_parms->nspt;
2243 if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
2244 return EINVAL;
2245 fd->sc_type->heads = form_parms->ntrk;
2246 fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
2247 fd->sc_type->secsize = ffs(i)-1;
2248 fd->sc_type->gap2 = form_parms->gaplen;
2249 fd->sc_type->cylinders = form_parms->ncyl;
2250 fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
2251 form_parms->nbps / DEV_BSIZE;
2252 fd->sc_type->step = form_parms->stepspercyl;
2253 fd->sc_type->fillbyte = form_parms->fillbyte;
2254 fd->sc_type->interleave = form_parms->interleave;
2255 return 0;
2256
2257 case FDIOCFORMAT_TRACK:
2258 if((flag & FWRITE) == 0)
2259 /* must be opened for writing */
2260 return EBADF;
2261 form_cmd = (struct fdformat_cmd *)addr;
2262 if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
2263 /* wrong version of formatting prog */
2264 return EINVAL;
2265
2266 if (form_cmd->head >= fd->sc_type->heads ||
2267 form_cmd->cylinder >= fd->sc_type->cylinders) {
2268 return EINVAL;
2269 }
2270
2271 fd_formb = malloc(sizeof(struct ne7_fd_formb),
2272 M_TEMP, M_NOWAIT);
2273 if (fd_formb == 0)
2274 return ENOMEM;
2275
2276 fd_formb->head = form_cmd->head;
2277 fd_formb->cyl = form_cmd->cylinder;
2278 fd_formb->transfer_rate = fd->sc_type->rate;
2279 fd_formb->fd_formb_secshift = fd->sc_type->secsize;
2280 fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
2281 fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
2282 fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
2283
2284 bzero(il, sizeof il);
2285 for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
2286 while (il[(j % fd_formb->fd_formb_nsecs) + 1])
2287 j++;
2288 il[(j % fd_formb->fd_formb_nsecs) + 1] = i;
2289 j += fd->sc_type->interleave;
2290 }
2291 for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
2292 fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
2293 fd_formb->fd_formb_headno(i) = form_cmd->head;
2294 fd_formb->fd_formb_secno(i) = il[i + 1];
2295 fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
2296 }
2297
2298 error = fdformat(dev, fd_formb, l->l_proc);
2299 free(fd_formb, M_TEMP);
2300 return error;
2301
2302 case FDIOCGETOPTS: /* get drive options */
2303 *(int *)addr = fd->sc_opts;
2304 return 0;
2305
2306 case FDIOCSETOPTS: /* set drive options */
2307 fd->sc_opts = *(int *)addr;
2308 return 0;
2309
2310 #ifdef FD_DEBUG
2311 case _IO('f', 100):
2312 fdc_wrfifo(fdc, NE7CMD_DUMPREG);
2313 fdcresult(fdc);
2314 printf("fdc: dumpreg(%d regs): <", fdc->sc_nstat);
2315 for (i = 0; i < fdc->sc_nstat; i++)
2316 printf(" 0x%x", fdc->sc_status[i]);
2317 printf(">\n");
2318 return 0;
2319
2320 case _IOW('f', 101, int):
2321 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
2322 fdc->sc_cfg |= (*(int *)addr & CFG_THRHLD_MASK);
2323 fdconf(fdc);
2324 return 0;
2325
2326 case _IO('f', 102):
2327 fdc_wrfifo(fdc, NE7CMD_SENSEI);
2328 fdcresult(fdc);
2329 printf("fdc: sensei(%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 #endif
2335 default:
2336 return ENOTTY;
2337 }
2338
2339 #ifdef DIAGNOSTIC
2340 panic("fdioctl: impossible");
2341 #endif
2342 }
2343
2344 int
2345 fdformat(dev_t dev, struct ne7_fd_formb *finfo, struct proc *p)
2346 {
2347 int rv = 0;
2348 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
2349 struct fd_type *type = fd->sc_type;
2350 struct buf *bp;
2351
2352 /* set up a buffer header for fdstrategy() */
2353 bp = getiobuf_nowait();
2354 if (bp == NULL)
2355 return ENOBUFS;
2356
2357 bp->b_vp = NULL;
2358 bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
2359 bp->b_proc = p;
2360 bp->b_dev = dev;
2361
2362 /*
2363 * Calculate a fake blkno, so fdstrategy() would initiate a
2364 * seek to the requested cylinder.
2365 */
2366 bp->b_blkno = ((finfo->cyl * (type->sectrac * type->heads)
2367 + finfo->head * type->sectrac) * FD_BSIZE(fd))
2368 / DEV_BSIZE;
2369
2370 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2371 bp->b_data = (void *)finfo;
2372
2373 #ifdef FD_DEBUG
2374 if (fdc_debug) {
2375 int i;
2376
2377 printf("fdformat: blkno 0x%llx count %d\n",
2378 (unsigned long long)bp->b_blkno, bp->b_bcount);
2379
2380 printf("\tcyl:\t%d\n", finfo->cyl);
2381 printf("\thead:\t%d\n", finfo->head);
2382 printf("\tnsecs:\t%d\n", finfo->fd_formb_nsecs);
2383 printf("\tsshft:\t%d\n", finfo->fd_formb_secshift);
2384 printf("\tgaplen:\t%d\n", finfo->fd_formb_gaplen);
2385 printf("\ttrack data:");
2386 for (i = 0; i < finfo->fd_formb_nsecs; i++) {
2387 printf(" [c%d h%d s%d]",
2388 finfo->fd_formb_cylno(i),
2389 finfo->fd_formb_headno(i),
2390 finfo->fd_formb_secno(i) );
2391 if (finfo->fd_formb_secsize(i) != 2)
2392 printf("<sz:%d>", finfo->fd_formb_secsize(i));
2393 }
2394 printf("\n");
2395 }
2396 #endif
2397
2398 /* now do the format */
2399 fdstrategy(bp);
2400
2401 /* ...and wait for it to complete */
2402 rv = biowait(bp);
2403 putiobuf(bp);
2404 return rv;
2405 }
2406
2407 void
2408 fdgetdisklabel(dev_t dev)
2409 {
2410 int unit = FDUNIT(dev), i;
2411 struct fd_softc *fd = fd_cd.cd_devs[unit];
2412 struct disklabel *lp = fd->sc_dk.dk_label;
2413 struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
2414
2415 bzero(lp, sizeof(struct disklabel));
2416 bzero(lp, sizeof(struct cpu_disklabel));
2417
2418 lp->d_type = DTYPE_FLOPPY;
2419 lp->d_secsize = FD_BSIZE(fd);
2420 lp->d_secpercyl = fd->sc_type->seccyl;
2421 lp->d_nsectors = fd->sc_type->sectrac;
2422 lp->d_ncylinders = fd->sc_type->cylinders;
2423 lp->d_ntracks = fd->sc_type->heads; /* Go figure... */
2424 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
2425 lp->d_rpm = 300; /* XXX like it matters... */
2426
2427 strncpy(lp->d_typename, "floppy disk", sizeof(lp->d_typename));
2428 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
2429 lp->d_interleave = 1;
2430 lp->d_flags = D_REMOVABLE;
2431
2432 lp->d_partitions[RAW_PART].p_offset = 0;
2433 lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
2434 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
2435 lp->d_npartitions = RAW_PART + 1;
2436
2437 lp->d_magic = DISKMAGIC;
2438 lp->d_magic2 = DISKMAGIC;
2439 lp->d_checksum = dkcksum(lp);
2440
2441 /*
2442 * Call the generic disklabel extraction routine. If there's
2443 * not a label there, fake it.
2444 */
2445 if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) {
2446 strncpy(lp->d_packname, "default label",
2447 sizeof(lp->d_packname));
2448 /*
2449 * Reset the partition info; it might have gotten
2450 * trashed in readdisklabel().
2451 *
2452 * XXX Why do we have to do this? readdisklabel()
2453 * should be safe...
2454 */
2455 for (i = 0; i < MAXPARTITIONS; ++i) {
2456 lp->d_partitions[i].p_offset = 0;
2457 if (i == RAW_PART) {
2458 lp->d_partitions[i].p_size =
2459 lp->d_secpercyl * lp->d_ncylinders;
2460 lp->d_partitions[i].p_fstype = FS_BSDFFS;
2461 } else {
2462 lp->d_partitions[i].p_size = 0;
2463 lp->d_partitions[i].p_fstype = FS_UNUSED;
2464 }
2465 }
2466 lp->d_npartitions = RAW_PART + 1;
2467 }
2468 }
2469
2470 void
2471 fd_do_eject(struct fd_softc *fd)
2472 {
2473 struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
2474
2475 #ifdef SUN4
2476 if (CPU_ISSUN4C) {
2477 auxregbisc(AUXIO4C_FDS, AUXIO4C_FEJ);
2478 delay(10);
2479 auxregbisc(AUXIO4C_FEJ, AUXIO4C_FDS);
2480 return;
2481 }
2482 if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
2483 #endif
2484 bus_space_tag_t t = fdc->sc_bustag;
2485 bus_space_handle_t h = fdc->sc_handle;
2486 uint8_t dor = FDO_FRST | FDO_FDMAEN | FDO_MOEN(0);
2487
2488 bus_space_write_1(t, h, fdc->sc_reg_dor, dor | FDO_EJ);
2489 delay(10);
2490 bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
2491 return;
2492 #ifdef SUN4
2493 }
2494 #endif
2495 }
2496
2497 /* ARGSUSED */
2498 void
2499 fd_mountroot_hook(struct device *dev)
2500 {
2501 int c;
2502
2503 fd_do_eject((struct fd_softc *)dev);
2504 printf("Insert filesystem floppy and press return.");
2505 for (;;) {
2506 c = cngetc();
2507 if ((c == '\r') || (c == '\n')) {
2508 printf("\n");
2509 break;
2510 }
2511 }
2512 }
2513
2514 #ifdef MEMORY_DISK_HOOKS
2515
2516 #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT)
2517
2518 int
2519 fd_read_md_image(size_t *sizep, void **addrp)
2520 {
2521 struct buf buf, *bp = &buf;
2522 dev_t dev;
2523 off_t offset;
2524 char *addr;
2525
2526 dev = makedev(54,0); /* XXX */
2527
2528 MALLOC(addr, void *, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK);
2529 *addrp = addr;
2530
2531 if (fdopen(dev, 0, S_IFCHR, NULL))
2532 panic("fd: mountroot: fdopen");
2533
2534 offset = 0;
2535
2536 for (;;) {
2537 bp->b_dev = dev;
2538 bp->b_error = 0;
2539 bp->b_resid = 0;
2540 bp->b_proc = NULL;
2541 bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_READ;
2542 bp->b_blkno = btodb(offset);
2543 bp->b_bcount = DEV_BSIZE;
2544 bp->b_data = addr;
2545 fdstrategy(bp);
2546 while ((bp->b_flags & B_DONE) == 0) {
2547 tsleep((void *)bp, PRIBIO + 1, "physio", 0);
2548 }
2549 if (bp->b_error)
2550 panic("fd: mountroot: fdread error %d", bp->b_error);
2551
2552 if (bp->b_resid != 0)
2553 break;
2554
2555 addr += DEV_BSIZE;
2556 offset += DEV_BSIZE;
2557 if (offset + DEV_BSIZE > FDMICROROOTSIZE)
2558 break;
2559 }
2560 (void)fdclose(dev, 0, S_IFCHR, NULL);
2561 *sizep = offset;
2562 fd_do_eject(fd_cd.cd_devs[FDUNIT(dev)]);
2563 return 0;
2564 }
2565 #endif /* MEMORY_DISK_HOOKS */
2566
2567 static void
2568 fd_set_properties(struct fd_softc *fd)
2569 {
2570 prop_dictionary_t disk_info, odisk_info, geom;
2571 struct fd_type *fdt;
2572 int secsize;
2573
2574 fdt = fd->sc_deftype;
2575
2576 disk_info = prop_dictionary_create();
2577
2578 geom = prop_dictionary_create();
2579
2580 prop_dictionary_set_uint64(geom, "sectors-per-unit",
2581 fdt->size);
2582
2583 switch (fdt->secsize) {
2584 case 2:
2585 secsize = 512;
2586 break;
2587 case 3:
2588 secsize = 1024;
2589 break;
2590 default:
2591 secsize = 0;
2592 }
2593
2594 prop_dictionary_set_uint32(geom, "sector-size",
2595 secsize);
2596
2597 prop_dictionary_set_uint16(geom, "sectors-per-track",
2598 fdt->sectrac);
2599
2600 prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
2601 fdt->heads);
2602
2603 prop_dictionary_set_uint64(geom, "cylinders-per-unit",
2604 fdt->cylinders);
2605
2606 prop_dictionary_set(disk_info, "geometry", geom);
2607 prop_object_release(geom);
2608
2609 prop_dictionary_set(device_properties(&fd->sc_dv),
2610 "disk-info", disk_info);
2611
2612 /*
2613 * Don't release disk_info here; we keep a reference to it.
2614 * disk_detach() will release it when we go away.
2615 */
2616
2617 odisk_info = fd->sc_dk.dk_info;
2618 fd->sc_dk.dk_info = disk_info;
2619 if (odisk_info)
2620 prop_object_release(odisk_info);
2621 }
2622