Deleted Added
full compact
fdc.c (109125) fdc.c (111815)
1/*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Don Ahn.
7 *
8 * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
9 * aided by the Linux floppy driver modifications from David Bateman
10 * (dbateman@eng.uts.edu.au).
11 *
12 * Copyright (c) 1993, 1994 by
13 * jc@irbs.UUCP (John Capo)
14 * vak@zebub.msk.su (Serge Vakulenko)
15 * ache@astral.msk.su (Andrew A. Chernov)
16 *
17 * Copyright (c) 1993, 1994, 1995 by
18 * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
19 * dufault@hda.com (Peter Dufault)
20 *
21 * Copyright (c) 2001 Joerg Wunsch,
22 * joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91
1/*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Don Ahn.
7 *
8 * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
9 * aided by the Linux floppy driver modifications from David Bateman
10 * (dbateman@eng.uts.edu.au).
11 *
12 * Copyright (c) 1993, 1994 by
13 * jc@irbs.UUCP (John Capo)
14 * vak@zebub.msk.su (Serge Vakulenko)
15 * ache@astral.msk.su (Andrew A. Chernov)
16 *
17 * Copyright (c) 1993, 1994, 1995 by
18 * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
19 * dufault@hda.com (Peter Dufault)
20 *
21 * Copyright (c) 2001 Joerg Wunsch,
22 * joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91
53 * $FreeBSD: head/sys/pc98/cbus/fdc.c 109125 2003-01-12 03:11:21Z nyan $
53 * $FreeBSD: head/sys/pc98/cbus/fdc.c 111815 2003-03-03 12:15:54Z phk $
54 */
55
56#include "opt_fdc.h"
57#include "card.h"
58
59#include <sys/param.h>
60#include <sys/systm.h>
61#include <sys/bio.h>
62#include <sys/bus.h>
63#include <sys/conf.h>
64#include <sys/devicestat.h>
65#include <sys/disk.h>
66#include <sys/fcntl.h>
67#include <sys/fdcio.h>
68#include <sys/filio.h>
69#include <sys/kernel.h>
70#include <sys/lock.h>
71#include <sys/malloc.h>
72#include <sys/module.h>
73#include <sys/mutex.h>
74#include <sys/proc.h>
75#include <sys/syslog.h>
76
77#include <machine/bus.h>
78#include <sys/rman.h>
79
80#include <machine/clock.h>
81#include <machine/resource.h>
82#include <machine/stdarg.h>
83
84#include <isa/isavar.h>
85#ifdef PC98
86#include <pc98/pc98/pc98.h>
87#include <pc98/pc98/pc98_machdep.h>
88#include <pc98/pc98/epsonio.h>
89#include <pc98/pc98/fdreg.h>
90#else
91#include <isa/isareg.h>
92#include <isa/fdreg.h>
93#include <isa/rtc.h>
94#endif
95
96enum fdc_type
97{
98 FDC_NE765, FDC_ENHANCED, FDC_UNKNOWN = -1
99};
100
101enum fdc_states {
102 DEVIDLE,
103 FINDWORK,
104 DOSEEK,
105 SEEKCOMPLETE ,
106 IOCOMPLETE,
107 RECALCOMPLETE,
108 STARTRECAL,
109 RESETCTLR,
110 SEEKWAIT,
111 RECALWAIT,
112 MOTORWAIT,
113 IOTIMEDOUT,
114 RESETCOMPLETE,
115 PIOREAD
116};
117
118#ifdef FDC_DEBUG
119static char const * const fdstates[] = {
120 "DEVIDLE",
121 "FINDWORK",
122 "DOSEEK",
123 "SEEKCOMPLETE",
124 "IOCOMPLETE",
125 "RECALCOMPLETE",
126 "STARTRECAL",
127 "RESETCTLR",
128 "SEEKWAIT",
129 "RECALWAIT",
130 "MOTORWAIT",
131 "IOTIMEDOUT",
132 "RESETCOMPLETE",
133 "PIOREAD"
134};
135#endif
136
137/*
138 * Per controller structure (softc).
139 */
140struct fdc_data
141{
142 int fdcu; /* our unit number */
143 int dmachan;
144 int flags;
145#define FDC_ATTACHED 0x01
146#define FDC_STAT_VALID 0x08
147#define FDC_HAS_FIFO 0x10
148#define FDC_NEEDS_RESET 0x20
149#define FDC_NODMA 0x40
150#define FDC_ISPNP 0x80
151#define FDC_ISPCMCIA 0x100
152 struct fd_data *fd;
153 int fdu; /* the active drive */
154 enum fdc_states state;
155 int retry;
156#ifndef PC98
157 int fdout; /* mirror of the w/o digital output reg */
158#endif
159 u_int status[7]; /* copy of the registers */
160 enum fdc_type fdct; /* chip version of FDC */
161 int fdc_errs; /* number of logged errors */
162 int dma_overruns; /* number of DMA overruns */
163 struct bio_queue_head head;
164 struct bio *bp; /* active buffer */
165#ifdef PC98
166 struct resource *res_ioport, *res_fdsio, *res_fdemsio;
167 struct resource *res_irq, *res_drq;
168 int rid_ioport, rid_irq, rid_drq;
169#else
170 struct resource *res_ioport, *res_ctl, *res_irq, *res_drq;
171 int rid_ioport, rid_ctl, rid_irq, rid_drq;
172#endif
173 int port_off;
174 bus_space_tag_t portt;
175 bus_space_handle_t porth;
176#ifdef PC98
177 bus_space_tag_t sc_fdsiot;
178 bus_space_handle_t sc_fdsioh;
179 bus_space_tag_t sc_fdemsiot;
180 bus_space_handle_t sc_fdemsioh;
181#else
182 bus_space_tag_t ctlt;
183 bus_space_handle_t ctlh;
184#endif
185 void *fdc_intr;
186 struct device *fdc_dev;
187#ifndef PC98
188 void (*fdctl_wr)(struct fdc_data *fdc, u_int8_t v);
189#endif
190};
191
192#define FDBIO_FORMAT BIO_CMD2
193
194typedef int fdu_t;
195typedef int fdcu_t;
196typedef int fdsu_t;
197typedef struct fd_data *fd_p;
198typedef struct fdc_data *fdc_p;
199typedef enum fdc_type fdc_t;
200
201#define FDUNIT(s) (((s) >> 6) & 3)
202#define FDNUMTOUNIT(n) (((n) & 3) << 6)
203#define FDTYPE(s) ((s) & 0x3f)
204
205/*
206 * fdc maintains a set (1!) of ivars per child of each controller.
207 */
208enum fdc_device_ivars {
209 FDC_IVAR_FDUNIT,
210};
211
212/*
213 * Simple access macros for the ivars.
214 */
215#define FDC_ACCESSOR(A, B, T) \
216static __inline T fdc_get_ ## A(device_t dev) \
217{ \
218 uintptr_t v; \
219 BUS_READ_IVAR(device_get_parent(dev), dev, FDC_IVAR_ ## B, &v); \
220 return (T) v; \
221}
222FDC_ACCESSOR(fdunit, FDUNIT, int)
223
224/* configuration flags for fdc */
225#define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */
226
227/* error returns for fd_cmd() */
228#define FD_FAILED -1
229#define FD_NOT_VALID -2
230#define FDC_ERRMAX 100 /* do not log more */
231/*
232 * Stop retrying after this many DMA overruns. Since each retry takes
233 * one revolution, with 300 rpm., 25 retries take approximately 5
234 * seconds which the read attempt will block in case the DMA overrun
235 * is persistent.
236 */
237#define FDC_DMAOV_MAX 25
238
239/*
240 * Timeout value for the PIO loops to wait until the FDC main status
241 * register matches our expectations (request for master, direction
242 * bit). This is supposed to be a number of microseconds, although
243 * timing might actually not be very accurate.
244 *
245 * Timeouts of 100 msec are believed to be required for some broken
246 * (old) hardware.
247 */
248#define FDSTS_TIMEOUT 100000
249
250/*
251 * Number of subdevices that can be used for different density types.
252 * By now, the lower 6 bit of the minor number are reserved for this,
253 * allowing for up to 64 subdevices, but we only use 16 out of this.
254 * Density #0 is used for automatic format detection, the other
255 * densities are available as programmable densities (for assignment
256 * by fdcontrol(8)).
257 * The upper 2 bits of the minor number are reserved for the subunit
258 * (drive #) per controller.
259 */
260#ifdef PC98
261#define NUMDENS 12
262#else
263#define NUMDENS 16
264#endif
265
266#define FDBIO_RDSECTID BIO_CMD1
267
268/*
269 * List of native drive densities. Order must match enum fd_drivetype
270 * in <sys/fdcio.h>. Upon attaching the drive, each of the
271 * programmable subdevices is initialized with the native density
272 * definition.
273 */
274#ifdef PC98
275static struct fd_type fd_native_types[] =
276{
277{ 0 }, /* FDT_NONE */
278{ 0 }, /* FDT_360K */
279{ 15,2,0xFF,0x1B,80,2400,0,2,0x54,1,0,0 }, /* FDT_12M */
280{ 0 }, /* FDT_720K */
281{ 18,2,0xFF,0x1B,80,2880,2,2,0x54,1,0,0 }, /* FDT_144M */
282{ 0 }, /* FDT_288M */
283};
284
285static struct fd_type fd_searchlist_12m[] = {
286{ 15,2,0xFF,0x1B,80,2400,0,2,0x54,1,0,0 }, /* 1.2M */
287{ 10,2,0xFF,0x10,82,1640,1,2,0x30,1,0,0 }, /* 820K */
288{ 10,2,0xFF,0x10,80,1600,1,2,0x30,1,0,0 }, /* 800K */
289{ 9,2,0xFF,0x20,80,1440,1,2,0x50,1,0,0 }, /* 720K */
290{ 9,2,0xFF,0x20,40, 720,1,2,0x50,1,0,FL_2STEP },/* 360K */
291{ 8,2,0xFF,0x2A,80,1280,1,2,0x50,1,0,0 }, /* 640K */
292{ 8,3,0xFF,0x35,77,1232,0,2,0x74,1,0,0 }, /* 1.23M 1024/sec */
293
294{ 8,3,0xFF,0x35,80,1280,0,2,0x74,1,0,0 }, /* 1.28M 1024/sec */
295};
296static struct fd_type fd_searchlist_144m[] = {
297{ 21,2,0xFF,0x04,82,3444,2,2,0x0C,2,0,0 }, /* 1.72M in 3mode */
298{ 18,2,0xFF,0x1B,82,2952,2,2,0x54,1,0,0 }, /* 1.48M in 3mode */
299{ 18,2,0xFF,0x1B,80,2880,2,2,0x54,1,0,0 }, /* 1.44M in 3mode */
300{ 15,2,0xFF,0x1B,80,2400,0,2,0x54,1,0,0 }, /* 1.2M */
301{ 10,2,0xFF,0x10,82,1640,1,2,0x30,1,0,0 }, /* 820K */
302{ 10,2,0xFF,0x10,80,1600,1,2,0x30,1,0,0 }, /* 800K */
303{ 9,2,0xFF,0x20,80,1440,1,2,0x50,1,0,0 }, /* 720K */
304{ 9,2,0xFF,0x20,40, 720,1,2,0x50,1,0,FL_2STEP },/* 360K */
305{ 8,2,0xFF,0x2A,80,1280,1,2,0x50,1,0,0 }, /* 640K */
306{ 8,3,0xFF,0x35,77,1232,0,2,0x74,1,0,0 }, /* 1.23M 1024/sec */
307
308{ 8,3,0xFF,0x35,80,1280,0,2,0x74,1,0,0 }, /* 1.28M 1024/sec */
309{ 9,3,0xFF,0x35,82,1476,0,2,0x47,1,0,0 }, /* 1.48M 1024/sec 9sec */
310#if 0
311{ 10,3,0xFF,0x1B,82,1640,2,2,0x54,1,0,0 }, /* 1.64M in 3mode - Reserve */
312#endif
313};
314#else /* PC98 */
315static struct fd_type fd_native_types[] =
316{
317{ 0 }, /* FDT_NONE */
318{ 9,2,0xFF,0x2A,40, 720,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_360K */
319{ 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* FDT_12M */
320{ 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_720K */
321{ 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
322#if 0 /* we currently don't handle 2.88 MB */
323{ 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /*FDT_288M*/
324#else
325{ 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
326#endif
327};
328
329/*
330 * 360 KB 5.25" and 720 KB 3.5" drives don't have automatic density
331 * selection, they just start out with their native density (or lose).
332 * So 1.2 MB 5.25", 1.44 MB 3.5", and 2.88 MB 3.5" drives have their
333 * respective lists of densities to search for.
334 */
335static struct fd_type fd_searchlist_12m[] = {
336{ 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* 1.2M */
337{ 9,2,0xFF,0x23,40, 720,FDC_300KBPS,2,0x50,1,0,FL_MFM|FL_2STEP }, /* 360K */
338{ 9,2,0xFF,0x20,80,1440,FDC_300KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
339};
340
341static struct fd_type fd_searchlist_144m[] = {
342{ 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
343{ 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
344};
345
346/* We search for 1.44M first since this is the most common case. */
347static struct fd_type fd_searchlist_288m[] = {
348{ 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
349#if 0
350{ 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /* 2.88M */
351#endif
352{ 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
353};
354#endif /* PC98 */
355
356#define MAX_SEC_SIZE (128 << 3)
357#define MAX_CYLINDER 85 /* some people really stress their drives
358 * up to cyl 82 */
359#define MAX_HEAD 1
360
361static devclass_t fdc_devclass;
362
363/*
364 * Per drive structure (softc).
365 */
366struct fd_data {
367 struct fdc_data *fdc; /* pointer to controller structure */
368 int fdsu; /* this units number on this controller */
369 enum fd_drivetype type; /* drive type */
370 struct fd_type *ft; /* pointer to current type descriptor */
371 struct fd_type fts[NUMDENS]; /* type descriptors */
372 int flags;
373#define FD_OPEN 0x01 /* it's open */
374#define FD_NONBLOCK 0x02 /* O_NONBLOCK set */
375#define FD_ACTIVE 0x04 /* it's active */
376#define FD_MOTOR 0x08 /* motor should be on */
377#define FD_MOTOR_WAIT 0x10 /* motor coming up */
378#define FD_UA 0x20 /* force unit attention */
379 int skip;
380 int hddrv;
381#define FD_NO_TRACK -2
382 int track; /* where we think the head is */
383 int options; /* user configurable options, see fdcio.h */
384 struct callout_handle toffhandle;
385 struct callout_handle tohandle;
386 struct devstat device_stats;
387 eventhandler_tag clonetag;
388 dev_t masterdev;
389 dev_t clonedevs[NUMDENS - 1];
390 device_t dev;
391 fdu_t fdu;
392#ifdef PC98
393 int pc98_trans;
394#endif
395};
396
397#ifdef PC98
398static bus_addr_t fdc_iat[] = {0, 2, 4};
399#endif
400
401struct fdc_ivars {
402 int fdunit;
403};
404static devclass_t fd_devclass;
405
406/* configuration flags for fd */
407#define FD_TYPEMASK 0x0f /* drive type, matches enum
408 * fd_drivetype; on i386 machines, if
409 * given as 0, use RTC type for fd0
410 * and fd1 */
411#define FD_DTYPE(flags) ((flags) & FD_TYPEMASK)
412#define FD_NO_CHLINE 0x10 /* drive does not support changeline
413 * aka. unit attention */
414#define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just
415 * assume it is there */
416
417#ifdef EPSON_NRDISK
418typedef unsigned int nrd_t;
419
420#define P_NRD_ADDRH 0xc24
421#define P_NRD_ADDRM 0xc22
422#define P_NRD_ADDRL 0xc20
423#define P_NRD_CHECK 0xc20
424#define P_NRD_DATA 0xc26
425#define P_NRD_LED 0xc36
426#define B_NRD_CHK 0x80
427#define B_NRD_LED 0x40
428#define A_NRD_INFO 0x2
429#define A_NRD_BASE 0x400
430#define NRD_STATUS 0x0
431#define NRD_ST0_HD 0x04
432
433static fdu_t nrdu=-1;
434static int nrdsec=0;
435static nrd_t nrdblkn=0;
436static nrd_t nrdaddr=0x0;
437
438#define nrd_check_ready() ({ \
439 (epson_inb(P_NRD_CHECK) & B_NRD_CHK) ? 0 : 1; \
440 })
441#define nrd_LED_on() epson_outb(P_NRD_LED, B_NRD_LED)
442#define nrd_LED_off() epson_outb(P_NRD_LED, ~B_NRD_LED)
443#define nrd_trac() ((int)(nrd_info(nrdaddr) & 0xff))
444#define nrd_head() ((int)((nrd_info(nrdaddr) >> 8) & 0xff))
445#define nrd_sec() ((int)(nrd_info(nrdaddr + 2) & 0xff))
446#define nrd_secsize() ((int)((nrd_info(A_NRD_INFO) >> 8) & 0xff))
447#define nrd_addrset(p) nrd_addr((nrd_t)((nrd_t)p+A_NRD_BASE))
448
449static inline void
450nrd_addr(addr)
451 nrd_t addr;
452{
453 epson_outb(P_NRD_ADDRH, (u_char)((addr >> 16) & 0x1f));
454 epson_outb(P_NRD_ADDRM, (u_char)((addr >> 8) & 0xff));
455 epson_outb(P_NRD_ADDRL, (u_char)(addr & 0xff));
456}
457
458static inline u_short
459nrd_info(addr)
460 nrd_t addr;
461{
462 nrd_addr(addr);
463 return (epson_inw(P_NRD_DATA));
464}
465#endif /* EPSON_NRDISK */
466
467/*
468 * Throughout this file the following conventions will be used:
469 *
470 * fd is a pointer to the fd_data struct for the drive in question
471 * fdc is a pointer to the fdc_data struct for the controller
472 * fdu is the floppy drive unit number
473 * fdcu is the floppy controller unit number
474 * fdsu is the floppy drive unit number on that controller. (sub-unit)
475 */
476
477/*
478 * Function declarations, same (chaotic) order as they appear in the
479 * file. Re-ordering is too late now, it would only obfuscate the
480 * diffs against old and offspring versions (like the PC98 one).
481 *
482 * Anyone adding functions here, please keep this sequence the same
483 * as below -- makes locating a particular function in the body much
484 * easier.
485 */
486#ifndef PC98
487static void fdout_wr(fdc_p, u_int8_t);
488#endif
489static u_int8_t fdsts_rd(fdc_p);
490static void fddata_wr(fdc_p, u_int8_t);
491static u_int8_t fddata_rd(fdc_p);
492#ifndef PC98
493static void fdctl_wr_isa(fdc_p, u_int8_t);
494#if NCARD > 0
495static void fdctl_wr_pcmcia(fdc_p, u_int8_t);
496#endif
497#if 0
498static u_int8_t fdin_rd(fdc_p);
499#endif
500#endif /* PC98 */
501static int fdc_err(struct fdc_data *, const char *);
502static int fd_cmd(struct fdc_data *, int, ...);
503static int enable_fifo(fdc_p fdc);
504static int fd_sense_drive_status(fdc_p, int *);
505static int fd_sense_int(fdc_p, int *, int *);
506static int fd_read_status(fdc_p);
507static int fdc_alloc_resources(struct fdc_data *);
508static void fdc_release_resources(struct fdc_data *);
509static int fdc_read_ivar(device_t, device_t, int, uintptr_t *);
510static int fdc_probe(device_t);
511#if NCARD > 0
512static int fdc_pccard_probe(device_t);
513#endif
514static int fdc_detach(device_t dev);
515static void fdc_add_child(device_t, const char *, int);
516static int fdc_attach(device_t);
517static int fdc_print_child(device_t, device_t);
518static void fd_clone (void *, char *, int, dev_t *);
519static int fd_probe(device_t);
520static int fd_attach(device_t);
521static int fd_detach(device_t);
522static void set_motor(struct fdc_data *, int, int);
523# define TURNON 1
524# define TURNOFF 0
525static timeout_t fd_turnoff;
526static timeout_t fd_motor_on;
527static void fd_turnon(struct fd_data *);
528static void fdc_reset(fdc_p);
529static int fd_in(struct fdc_data *, int *);
530static int out_fdc(struct fdc_data *, int);
531/*
532 * The open function is named Fdopen() to avoid confusion with fdopen()
533 * in fd(4). The difference is now only meaningful for debuggers.
534 */
535static d_open_t Fdopen;
536static d_close_t fdclose;
537static d_strategy_t fdstrategy;
538static void fdstart(struct fdc_data *);
539static timeout_t fd_iotimeout;
540static timeout_t fd_pseudointr;
541static driver_intr_t fdc_intr;
542static int fdcpio(fdc_p, long, caddr_t, u_int);
543static int fdautoselect(dev_t);
544static int fdstate(struct fdc_data *);
545static int retrier(struct fdc_data *);
546static void fdbiodone(struct bio *);
547static int fdmisccmd(dev_t, u_int, void *);
548static d_ioctl_t fdioctl;
549
550static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */
551
552#ifdef FDC_DEBUG
553/* CAUTION: fd_debug causes huge amounts of logging output */
554static int volatile fd_debug = 0;
555#define TRACE0(arg) do { if (fd_debug) printf(arg); } while (0)
556#define TRACE1(arg1, arg2) do { if (fd_debug) printf(arg1, arg2); } while (0)
557#else /* FDC_DEBUG */
558#define TRACE0(arg) do { } while (0)
559#define TRACE1(arg1, arg2) do { } while (0)
560#endif /* FDC_DEBUG */
561
562/*
563 * Bus space handling (access to low-level IO).
564 */
565#ifndef PC98
566static void
567fdout_wr(fdc_p fdc, u_int8_t v)
568{
569 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
570}
571#endif
572
573static u_int8_t
574fdsts_rd(fdc_p fdc)
575{
576 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
577}
578
579static void
580fddata_wr(fdc_p fdc, u_int8_t v)
581{
582 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
583}
584
585static u_int8_t
586fddata_rd(fdc_p fdc)
587{
588 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
589}
590
591#ifdef PC98
592static void
593fdctl_wr(fdc_p fdc, u_int8_t v)
594{
595 bus_space_write_1(fdc->portt, fdc->porth, FDCTL, v);
596}
597#endif
598
599#ifndef PC98
600static void
601fdctl_wr_isa(fdc_p fdc, u_int8_t v)
602{
603 bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
604}
605
606#if NCARD > 0
607static void
608fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v)
609{
610 bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v);
611}
612#endif
613
614static u_int8_t
615fdin_rd(fdc_p fdc)
616{
617 return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
618}
619#endif /* PC98 */
620
621#define CDEV_MAJOR 9
622static struct cdevsw fd_cdevsw = {
54 */
55
56#include "opt_fdc.h"
57#include "card.h"
58
59#include <sys/param.h>
60#include <sys/systm.h>
61#include <sys/bio.h>
62#include <sys/bus.h>
63#include <sys/conf.h>
64#include <sys/devicestat.h>
65#include <sys/disk.h>
66#include <sys/fcntl.h>
67#include <sys/fdcio.h>
68#include <sys/filio.h>
69#include <sys/kernel.h>
70#include <sys/lock.h>
71#include <sys/malloc.h>
72#include <sys/module.h>
73#include <sys/mutex.h>
74#include <sys/proc.h>
75#include <sys/syslog.h>
76
77#include <machine/bus.h>
78#include <sys/rman.h>
79
80#include <machine/clock.h>
81#include <machine/resource.h>
82#include <machine/stdarg.h>
83
84#include <isa/isavar.h>
85#ifdef PC98
86#include <pc98/pc98/pc98.h>
87#include <pc98/pc98/pc98_machdep.h>
88#include <pc98/pc98/epsonio.h>
89#include <pc98/pc98/fdreg.h>
90#else
91#include <isa/isareg.h>
92#include <isa/fdreg.h>
93#include <isa/rtc.h>
94#endif
95
96enum fdc_type
97{
98 FDC_NE765, FDC_ENHANCED, FDC_UNKNOWN = -1
99};
100
101enum fdc_states {
102 DEVIDLE,
103 FINDWORK,
104 DOSEEK,
105 SEEKCOMPLETE ,
106 IOCOMPLETE,
107 RECALCOMPLETE,
108 STARTRECAL,
109 RESETCTLR,
110 SEEKWAIT,
111 RECALWAIT,
112 MOTORWAIT,
113 IOTIMEDOUT,
114 RESETCOMPLETE,
115 PIOREAD
116};
117
118#ifdef FDC_DEBUG
119static char const * const fdstates[] = {
120 "DEVIDLE",
121 "FINDWORK",
122 "DOSEEK",
123 "SEEKCOMPLETE",
124 "IOCOMPLETE",
125 "RECALCOMPLETE",
126 "STARTRECAL",
127 "RESETCTLR",
128 "SEEKWAIT",
129 "RECALWAIT",
130 "MOTORWAIT",
131 "IOTIMEDOUT",
132 "RESETCOMPLETE",
133 "PIOREAD"
134};
135#endif
136
137/*
138 * Per controller structure (softc).
139 */
140struct fdc_data
141{
142 int fdcu; /* our unit number */
143 int dmachan;
144 int flags;
145#define FDC_ATTACHED 0x01
146#define FDC_STAT_VALID 0x08
147#define FDC_HAS_FIFO 0x10
148#define FDC_NEEDS_RESET 0x20
149#define FDC_NODMA 0x40
150#define FDC_ISPNP 0x80
151#define FDC_ISPCMCIA 0x100
152 struct fd_data *fd;
153 int fdu; /* the active drive */
154 enum fdc_states state;
155 int retry;
156#ifndef PC98
157 int fdout; /* mirror of the w/o digital output reg */
158#endif
159 u_int status[7]; /* copy of the registers */
160 enum fdc_type fdct; /* chip version of FDC */
161 int fdc_errs; /* number of logged errors */
162 int dma_overruns; /* number of DMA overruns */
163 struct bio_queue_head head;
164 struct bio *bp; /* active buffer */
165#ifdef PC98
166 struct resource *res_ioport, *res_fdsio, *res_fdemsio;
167 struct resource *res_irq, *res_drq;
168 int rid_ioport, rid_irq, rid_drq;
169#else
170 struct resource *res_ioport, *res_ctl, *res_irq, *res_drq;
171 int rid_ioport, rid_ctl, rid_irq, rid_drq;
172#endif
173 int port_off;
174 bus_space_tag_t portt;
175 bus_space_handle_t porth;
176#ifdef PC98
177 bus_space_tag_t sc_fdsiot;
178 bus_space_handle_t sc_fdsioh;
179 bus_space_tag_t sc_fdemsiot;
180 bus_space_handle_t sc_fdemsioh;
181#else
182 bus_space_tag_t ctlt;
183 bus_space_handle_t ctlh;
184#endif
185 void *fdc_intr;
186 struct device *fdc_dev;
187#ifndef PC98
188 void (*fdctl_wr)(struct fdc_data *fdc, u_int8_t v);
189#endif
190};
191
192#define FDBIO_FORMAT BIO_CMD2
193
194typedef int fdu_t;
195typedef int fdcu_t;
196typedef int fdsu_t;
197typedef struct fd_data *fd_p;
198typedef struct fdc_data *fdc_p;
199typedef enum fdc_type fdc_t;
200
201#define FDUNIT(s) (((s) >> 6) & 3)
202#define FDNUMTOUNIT(n) (((n) & 3) << 6)
203#define FDTYPE(s) ((s) & 0x3f)
204
205/*
206 * fdc maintains a set (1!) of ivars per child of each controller.
207 */
208enum fdc_device_ivars {
209 FDC_IVAR_FDUNIT,
210};
211
212/*
213 * Simple access macros for the ivars.
214 */
215#define FDC_ACCESSOR(A, B, T) \
216static __inline T fdc_get_ ## A(device_t dev) \
217{ \
218 uintptr_t v; \
219 BUS_READ_IVAR(device_get_parent(dev), dev, FDC_IVAR_ ## B, &v); \
220 return (T) v; \
221}
222FDC_ACCESSOR(fdunit, FDUNIT, int)
223
224/* configuration flags for fdc */
225#define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */
226
227/* error returns for fd_cmd() */
228#define FD_FAILED -1
229#define FD_NOT_VALID -2
230#define FDC_ERRMAX 100 /* do not log more */
231/*
232 * Stop retrying after this many DMA overruns. Since each retry takes
233 * one revolution, with 300 rpm., 25 retries take approximately 5
234 * seconds which the read attempt will block in case the DMA overrun
235 * is persistent.
236 */
237#define FDC_DMAOV_MAX 25
238
239/*
240 * Timeout value for the PIO loops to wait until the FDC main status
241 * register matches our expectations (request for master, direction
242 * bit). This is supposed to be a number of microseconds, although
243 * timing might actually not be very accurate.
244 *
245 * Timeouts of 100 msec are believed to be required for some broken
246 * (old) hardware.
247 */
248#define FDSTS_TIMEOUT 100000
249
250/*
251 * Number of subdevices that can be used for different density types.
252 * By now, the lower 6 bit of the minor number are reserved for this,
253 * allowing for up to 64 subdevices, but we only use 16 out of this.
254 * Density #0 is used for automatic format detection, the other
255 * densities are available as programmable densities (for assignment
256 * by fdcontrol(8)).
257 * The upper 2 bits of the minor number are reserved for the subunit
258 * (drive #) per controller.
259 */
260#ifdef PC98
261#define NUMDENS 12
262#else
263#define NUMDENS 16
264#endif
265
266#define FDBIO_RDSECTID BIO_CMD1
267
268/*
269 * List of native drive densities. Order must match enum fd_drivetype
270 * in <sys/fdcio.h>. Upon attaching the drive, each of the
271 * programmable subdevices is initialized with the native density
272 * definition.
273 */
274#ifdef PC98
275static struct fd_type fd_native_types[] =
276{
277{ 0 }, /* FDT_NONE */
278{ 0 }, /* FDT_360K */
279{ 15,2,0xFF,0x1B,80,2400,0,2,0x54,1,0,0 }, /* FDT_12M */
280{ 0 }, /* FDT_720K */
281{ 18,2,0xFF,0x1B,80,2880,2,2,0x54,1,0,0 }, /* FDT_144M */
282{ 0 }, /* FDT_288M */
283};
284
285static struct fd_type fd_searchlist_12m[] = {
286{ 15,2,0xFF,0x1B,80,2400,0,2,0x54,1,0,0 }, /* 1.2M */
287{ 10,2,0xFF,0x10,82,1640,1,2,0x30,1,0,0 }, /* 820K */
288{ 10,2,0xFF,0x10,80,1600,1,2,0x30,1,0,0 }, /* 800K */
289{ 9,2,0xFF,0x20,80,1440,1,2,0x50,1,0,0 }, /* 720K */
290{ 9,2,0xFF,0x20,40, 720,1,2,0x50,1,0,FL_2STEP },/* 360K */
291{ 8,2,0xFF,0x2A,80,1280,1,2,0x50,1,0,0 }, /* 640K */
292{ 8,3,0xFF,0x35,77,1232,0,2,0x74,1,0,0 }, /* 1.23M 1024/sec */
293
294{ 8,3,0xFF,0x35,80,1280,0,2,0x74,1,0,0 }, /* 1.28M 1024/sec */
295};
296static struct fd_type fd_searchlist_144m[] = {
297{ 21,2,0xFF,0x04,82,3444,2,2,0x0C,2,0,0 }, /* 1.72M in 3mode */
298{ 18,2,0xFF,0x1B,82,2952,2,2,0x54,1,0,0 }, /* 1.48M in 3mode */
299{ 18,2,0xFF,0x1B,80,2880,2,2,0x54,1,0,0 }, /* 1.44M in 3mode */
300{ 15,2,0xFF,0x1B,80,2400,0,2,0x54,1,0,0 }, /* 1.2M */
301{ 10,2,0xFF,0x10,82,1640,1,2,0x30,1,0,0 }, /* 820K */
302{ 10,2,0xFF,0x10,80,1600,1,2,0x30,1,0,0 }, /* 800K */
303{ 9,2,0xFF,0x20,80,1440,1,2,0x50,1,0,0 }, /* 720K */
304{ 9,2,0xFF,0x20,40, 720,1,2,0x50,1,0,FL_2STEP },/* 360K */
305{ 8,2,0xFF,0x2A,80,1280,1,2,0x50,1,0,0 }, /* 640K */
306{ 8,3,0xFF,0x35,77,1232,0,2,0x74,1,0,0 }, /* 1.23M 1024/sec */
307
308{ 8,3,0xFF,0x35,80,1280,0,2,0x74,1,0,0 }, /* 1.28M 1024/sec */
309{ 9,3,0xFF,0x35,82,1476,0,2,0x47,1,0,0 }, /* 1.48M 1024/sec 9sec */
310#if 0
311{ 10,3,0xFF,0x1B,82,1640,2,2,0x54,1,0,0 }, /* 1.64M in 3mode - Reserve */
312#endif
313};
314#else /* PC98 */
315static struct fd_type fd_native_types[] =
316{
317{ 0 }, /* FDT_NONE */
318{ 9,2,0xFF,0x2A,40, 720,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_360K */
319{ 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* FDT_12M */
320{ 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_720K */
321{ 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
322#if 0 /* we currently don't handle 2.88 MB */
323{ 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /*FDT_288M*/
324#else
325{ 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
326#endif
327};
328
329/*
330 * 360 KB 5.25" and 720 KB 3.5" drives don't have automatic density
331 * selection, they just start out with their native density (or lose).
332 * So 1.2 MB 5.25", 1.44 MB 3.5", and 2.88 MB 3.5" drives have their
333 * respective lists of densities to search for.
334 */
335static struct fd_type fd_searchlist_12m[] = {
336{ 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* 1.2M */
337{ 9,2,0xFF,0x23,40, 720,FDC_300KBPS,2,0x50,1,0,FL_MFM|FL_2STEP }, /* 360K */
338{ 9,2,0xFF,0x20,80,1440,FDC_300KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
339};
340
341static struct fd_type fd_searchlist_144m[] = {
342{ 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
343{ 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
344};
345
346/* We search for 1.44M first since this is the most common case. */
347static struct fd_type fd_searchlist_288m[] = {
348{ 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
349#if 0
350{ 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /* 2.88M */
351#endif
352{ 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
353};
354#endif /* PC98 */
355
356#define MAX_SEC_SIZE (128 << 3)
357#define MAX_CYLINDER 85 /* some people really stress their drives
358 * up to cyl 82 */
359#define MAX_HEAD 1
360
361static devclass_t fdc_devclass;
362
363/*
364 * Per drive structure (softc).
365 */
366struct fd_data {
367 struct fdc_data *fdc; /* pointer to controller structure */
368 int fdsu; /* this units number on this controller */
369 enum fd_drivetype type; /* drive type */
370 struct fd_type *ft; /* pointer to current type descriptor */
371 struct fd_type fts[NUMDENS]; /* type descriptors */
372 int flags;
373#define FD_OPEN 0x01 /* it's open */
374#define FD_NONBLOCK 0x02 /* O_NONBLOCK set */
375#define FD_ACTIVE 0x04 /* it's active */
376#define FD_MOTOR 0x08 /* motor should be on */
377#define FD_MOTOR_WAIT 0x10 /* motor coming up */
378#define FD_UA 0x20 /* force unit attention */
379 int skip;
380 int hddrv;
381#define FD_NO_TRACK -2
382 int track; /* where we think the head is */
383 int options; /* user configurable options, see fdcio.h */
384 struct callout_handle toffhandle;
385 struct callout_handle tohandle;
386 struct devstat device_stats;
387 eventhandler_tag clonetag;
388 dev_t masterdev;
389 dev_t clonedevs[NUMDENS - 1];
390 device_t dev;
391 fdu_t fdu;
392#ifdef PC98
393 int pc98_trans;
394#endif
395};
396
397#ifdef PC98
398static bus_addr_t fdc_iat[] = {0, 2, 4};
399#endif
400
401struct fdc_ivars {
402 int fdunit;
403};
404static devclass_t fd_devclass;
405
406/* configuration flags for fd */
407#define FD_TYPEMASK 0x0f /* drive type, matches enum
408 * fd_drivetype; on i386 machines, if
409 * given as 0, use RTC type for fd0
410 * and fd1 */
411#define FD_DTYPE(flags) ((flags) & FD_TYPEMASK)
412#define FD_NO_CHLINE 0x10 /* drive does not support changeline
413 * aka. unit attention */
414#define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just
415 * assume it is there */
416
417#ifdef EPSON_NRDISK
418typedef unsigned int nrd_t;
419
420#define P_NRD_ADDRH 0xc24
421#define P_NRD_ADDRM 0xc22
422#define P_NRD_ADDRL 0xc20
423#define P_NRD_CHECK 0xc20
424#define P_NRD_DATA 0xc26
425#define P_NRD_LED 0xc36
426#define B_NRD_CHK 0x80
427#define B_NRD_LED 0x40
428#define A_NRD_INFO 0x2
429#define A_NRD_BASE 0x400
430#define NRD_STATUS 0x0
431#define NRD_ST0_HD 0x04
432
433static fdu_t nrdu=-1;
434static int nrdsec=0;
435static nrd_t nrdblkn=0;
436static nrd_t nrdaddr=0x0;
437
438#define nrd_check_ready() ({ \
439 (epson_inb(P_NRD_CHECK) & B_NRD_CHK) ? 0 : 1; \
440 })
441#define nrd_LED_on() epson_outb(P_NRD_LED, B_NRD_LED)
442#define nrd_LED_off() epson_outb(P_NRD_LED, ~B_NRD_LED)
443#define nrd_trac() ((int)(nrd_info(nrdaddr) & 0xff))
444#define nrd_head() ((int)((nrd_info(nrdaddr) >> 8) & 0xff))
445#define nrd_sec() ((int)(nrd_info(nrdaddr + 2) & 0xff))
446#define nrd_secsize() ((int)((nrd_info(A_NRD_INFO) >> 8) & 0xff))
447#define nrd_addrset(p) nrd_addr((nrd_t)((nrd_t)p+A_NRD_BASE))
448
449static inline void
450nrd_addr(addr)
451 nrd_t addr;
452{
453 epson_outb(P_NRD_ADDRH, (u_char)((addr >> 16) & 0x1f));
454 epson_outb(P_NRD_ADDRM, (u_char)((addr >> 8) & 0xff));
455 epson_outb(P_NRD_ADDRL, (u_char)(addr & 0xff));
456}
457
458static inline u_short
459nrd_info(addr)
460 nrd_t addr;
461{
462 nrd_addr(addr);
463 return (epson_inw(P_NRD_DATA));
464}
465#endif /* EPSON_NRDISK */
466
467/*
468 * Throughout this file the following conventions will be used:
469 *
470 * fd is a pointer to the fd_data struct for the drive in question
471 * fdc is a pointer to the fdc_data struct for the controller
472 * fdu is the floppy drive unit number
473 * fdcu is the floppy controller unit number
474 * fdsu is the floppy drive unit number on that controller. (sub-unit)
475 */
476
477/*
478 * Function declarations, same (chaotic) order as they appear in the
479 * file. Re-ordering is too late now, it would only obfuscate the
480 * diffs against old and offspring versions (like the PC98 one).
481 *
482 * Anyone adding functions here, please keep this sequence the same
483 * as below -- makes locating a particular function in the body much
484 * easier.
485 */
486#ifndef PC98
487static void fdout_wr(fdc_p, u_int8_t);
488#endif
489static u_int8_t fdsts_rd(fdc_p);
490static void fddata_wr(fdc_p, u_int8_t);
491static u_int8_t fddata_rd(fdc_p);
492#ifndef PC98
493static void fdctl_wr_isa(fdc_p, u_int8_t);
494#if NCARD > 0
495static void fdctl_wr_pcmcia(fdc_p, u_int8_t);
496#endif
497#if 0
498static u_int8_t fdin_rd(fdc_p);
499#endif
500#endif /* PC98 */
501static int fdc_err(struct fdc_data *, const char *);
502static int fd_cmd(struct fdc_data *, int, ...);
503static int enable_fifo(fdc_p fdc);
504static int fd_sense_drive_status(fdc_p, int *);
505static int fd_sense_int(fdc_p, int *, int *);
506static int fd_read_status(fdc_p);
507static int fdc_alloc_resources(struct fdc_data *);
508static void fdc_release_resources(struct fdc_data *);
509static int fdc_read_ivar(device_t, device_t, int, uintptr_t *);
510static int fdc_probe(device_t);
511#if NCARD > 0
512static int fdc_pccard_probe(device_t);
513#endif
514static int fdc_detach(device_t dev);
515static void fdc_add_child(device_t, const char *, int);
516static int fdc_attach(device_t);
517static int fdc_print_child(device_t, device_t);
518static void fd_clone (void *, char *, int, dev_t *);
519static int fd_probe(device_t);
520static int fd_attach(device_t);
521static int fd_detach(device_t);
522static void set_motor(struct fdc_data *, int, int);
523# define TURNON 1
524# define TURNOFF 0
525static timeout_t fd_turnoff;
526static timeout_t fd_motor_on;
527static void fd_turnon(struct fd_data *);
528static void fdc_reset(fdc_p);
529static int fd_in(struct fdc_data *, int *);
530static int out_fdc(struct fdc_data *, int);
531/*
532 * The open function is named Fdopen() to avoid confusion with fdopen()
533 * in fd(4). The difference is now only meaningful for debuggers.
534 */
535static d_open_t Fdopen;
536static d_close_t fdclose;
537static d_strategy_t fdstrategy;
538static void fdstart(struct fdc_data *);
539static timeout_t fd_iotimeout;
540static timeout_t fd_pseudointr;
541static driver_intr_t fdc_intr;
542static int fdcpio(fdc_p, long, caddr_t, u_int);
543static int fdautoselect(dev_t);
544static int fdstate(struct fdc_data *);
545static int retrier(struct fdc_data *);
546static void fdbiodone(struct bio *);
547static int fdmisccmd(dev_t, u_int, void *);
548static d_ioctl_t fdioctl;
549
550static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */
551
552#ifdef FDC_DEBUG
553/* CAUTION: fd_debug causes huge amounts of logging output */
554static int volatile fd_debug = 0;
555#define TRACE0(arg) do { if (fd_debug) printf(arg); } while (0)
556#define TRACE1(arg1, arg2) do { if (fd_debug) printf(arg1, arg2); } while (0)
557#else /* FDC_DEBUG */
558#define TRACE0(arg) do { } while (0)
559#define TRACE1(arg1, arg2) do { } while (0)
560#endif /* FDC_DEBUG */
561
562/*
563 * Bus space handling (access to low-level IO).
564 */
565#ifndef PC98
566static void
567fdout_wr(fdc_p fdc, u_int8_t v)
568{
569 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
570}
571#endif
572
573static u_int8_t
574fdsts_rd(fdc_p fdc)
575{
576 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
577}
578
579static void
580fddata_wr(fdc_p fdc, u_int8_t v)
581{
582 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
583}
584
585static u_int8_t
586fddata_rd(fdc_p fdc)
587{
588 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
589}
590
591#ifdef PC98
592static void
593fdctl_wr(fdc_p fdc, u_int8_t v)
594{
595 bus_space_write_1(fdc->portt, fdc->porth, FDCTL, v);
596}
597#endif
598
599#ifndef PC98
600static void
601fdctl_wr_isa(fdc_p fdc, u_int8_t v)
602{
603 bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
604}
605
606#if NCARD > 0
607static void
608fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v)
609{
610 bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v);
611}
612#endif
613
614static u_int8_t
615fdin_rd(fdc_p fdc)
616{
617 return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
618}
619#endif /* PC98 */
620
621#define CDEV_MAJOR 9
622static struct cdevsw fd_cdevsw = {
623 /* open */ Fdopen,
624 /* close */ fdclose,
625 /* read */ physread,
626 /* write */ physwrite,
627 /* ioctl */ fdioctl,
628 /* poll */ nopoll,
629 /* mmap */ nommap,
630 /* strategy */ fdstrategy,
631 /* name */ "fd",
632 /* maj */ CDEV_MAJOR,
633 /* dump */ nodump,
634 /* psize */ nopsize,
635 /* flags */ D_DISK,
623 .d_open = Fdopen,
624 .d_close = fdclose,
625 .d_read = physread,
626 .d_write = physwrite,
627 .d_ioctl = fdioctl,
628 .d_strategy = fdstrategy,
629 .d_name = "fd",
630 .d_maj = CDEV_MAJOR,
631 .d_flags = D_DISK,
636};
637
638/*
639 * Auxiliary functions. Well, some only. Others are scattered
640 * throughout the entire file.
641 */
642static int
643fdc_err(struct fdc_data *fdc, const char *s)
644{
645 fdc->fdc_errs++;
646 if (s) {
647 if (fdc->fdc_errs < FDC_ERRMAX)
648 device_printf(fdc->fdc_dev, "%s", s);
649 else if (fdc->fdc_errs == FDC_ERRMAX)
650 device_printf(fdc->fdc_dev, "too many errors, not "
651 "logging any more\n");
652 }
653
654 return FD_FAILED;
655}
656
657/*
658 * fd_cmd: Send a command to the chip. Takes a varargs with this structure:
659 * Unit number,
660 * # of output bytes, output bytes as ints ...,
661 * # of input bytes, input bytes as ints ...
662 */
663static int
664fd_cmd(struct fdc_data *fdc, int n_out, ...)
665{
666 u_char cmd;
667 int n_in;
668 int n;
669 va_list ap;
670
671 va_start(ap, n_out);
672 cmd = (u_char)(va_arg(ap, int));
673 va_end(ap);
674 va_start(ap, n_out);
675 for (n = 0; n < n_out; n++)
676 {
677 if (out_fdc(fdc, va_arg(ap, int)) < 0)
678 {
679 char msg[50];
680 snprintf(msg, sizeof(msg),
681 "cmd %x failed at out byte %d of %d\n",
682 cmd, n + 1, n_out);
683 return fdc_err(fdc, msg);
684 }
685 }
686 n_in = va_arg(ap, int);
687 for (n = 0; n < n_in; n++)
688 {
689 int *ptr = va_arg(ap, int *);
690 if (fd_in(fdc, ptr) < 0)
691 {
692 char msg[50];
693 snprintf(msg, sizeof(msg),
694 "cmd %02x failed at in byte %d of %d\n",
695 cmd, n + 1, n_in);
696 return fdc_err(fdc, msg);
697 }
698 }
699
700 return 0;
701}
702
703static int
704enable_fifo(fdc_p fdc)
705{
706 int i, j;
707
708 if ((fdc->flags & FDC_HAS_FIFO) == 0) {
709
710 /*
711 * Cannot use fd_cmd the normal way here, since
712 * this might be an invalid command. Thus we send the
713 * first byte, and check for an early turn of data directon.
714 */
715
716 if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
717 return fdc_err(fdc, "Enable FIFO failed\n");
718
719 /* If command is invalid, return */
720 j = FDSTS_TIMEOUT;
721 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
722 != NE7_RQM && j-- > 0) {
723 if (i == (NE7_DIO | NE7_RQM)) {
724 fdc_reset(fdc);
725 return FD_FAILED;
726 }
727 DELAY(1);
728 }
729 if (j<0 ||
730 fd_cmd(fdc, 3,
731 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
732 fdc_reset(fdc);
733 return fdc_err(fdc, "Enable FIFO failed\n");
734 }
735 fdc->flags |= FDC_HAS_FIFO;
736 return 0;
737 }
738 if (fd_cmd(fdc, 4,
739 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
740 return fdc_err(fdc, "Re-enable FIFO failed\n");
741 return 0;
742}
743
744static int
745fd_sense_drive_status(fdc_p fdc, int *st3p)
746{
747 int st3;
748
749 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
750 {
751 return fdc_err(fdc, "Sense Drive Status failed\n");
752 }
753 if (st3p)
754 *st3p = st3;
755
756 return 0;
757}
758
759static int
760fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
761{
762 int cyl, st0, ret;
763
764#ifdef EPSON_NRDISK
765 if (fdc->fdu == nrdu) {
766 if (fdc->fd->track >= 0) nrdaddr = (fdc->fd->track + 1) * 8;
767 else nrdaddr = 0x0;
768 *st0p = nrd_head() ? NRD_ST0_HD : NRD_STATUS;
769 *cylp = nrd_trac();
770 }
771 else {
772#endif /* EPSON_NRDISK */
773 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
774 if (ret) {
775 (void)fdc_err(fdc,
776 "sense intr err reading stat reg 0\n");
777 return ret;
778 }
779
780 if (st0p)
781 *st0p = st0;
782
783 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
784 /*
785 * There doesn't seem to have been an interrupt.
786 */
787 return FD_NOT_VALID;
788 }
789
790 if (fd_in(fdc, &cyl) < 0) {
791 return fdc_err(fdc, "can't get cyl num\n");
792 }
793
794 if (cylp)
795 *cylp = cyl;
796
797#ifdef EPSON_NRDISK
798 }
799#endif /* EPSON_NRDISK */
800 return 0;
801}
802
803
804static int
805fd_read_status(fdc_p fdc)
806{
807 int i, ret;
808
809 for (i = ret = 0; i < 7; i++) {
810 /*
811 * XXX types are poorly chosen. Only bytes can be read
812 * from the hardware, but fdc->status[] wants u_ints and
813 * fd_in() gives ints.
814 */
815 int status;
816
817#ifdef EPSON_NRDISK
818 if (fdc->fdu == nrdu) {
819 switch (i) {
820 case 0: fdc->status[i] = nrd_head()
821 ? NRD_ST0_HD : NRD_STATUS; break;
822 case 1: fdc->status[i] = NRD_STATUS; break;
823 case 2: fdc->status[i] = NRD_STATUS; break;
824 case 3: fdc->status[i] = nrd_trac(); break;
825 case 4: fdc->status[i] = nrd_head(); break;
826 case 5: fdc->status[i] = nrdsec; break;
827 case 6: fdc->status[i] = nrd_secsize(); break;
828 }
829 ret = 0;
830 }
831 else {
832#endif /* EPSON_NRDISK */
833 ret = fd_in(fdc, &status);
834 fdc->status[i] = status;
835 if (ret != 0)
836 break;
837#ifdef EPSON_NRDISK
838 }
839#endif /* EPSON_NRDISK */
840 }
841
842 if (ret == 0)
843 fdc->flags |= FDC_STAT_VALID;
844 else
845 fdc->flags &= ~FDC_STAT_VALID;
846
847 return ret;
848}
849
850#ifdef PC98
851static int pc98_trans = 0; /* 0 : HD , 1 : DD , 2 : 1.44 */
852static int pc98_trans_prev = 0;
853
854static void set_density(fdc_p fdc)
855{
856 /* always motor on */
857 bus_space_write_1(fdc->sc_fdsiot, fdc->sc_fdsioh, 0,
858 (pc98_trans != 1 ? FDP_FDDEXC : 0) | FDP_PORTEXC);
859 DELAY(100);
860 fdctl_wr(fdc, FDC_RST | FDC_DMAE);
861 /* in the case of note W, always inhibit 100ms timer */
862}
863
864static int pc98_fd_check_ready(fdu_t fdu)
865{
866 fd_p fd = devclass_get_softc(fd_devclass, fdu);
867 struct fdc_data *fdc = fd->fdc;
868 int retry = 0, status;
869
870#ifdef EPSON_NRDISK
871 if (fdu == nrdu) {
872 if (nrd_check_ready()) return 0;
873 else return -1;
874 }
875#endif
876 while (retry++ < 30000) {
877 set_motor(fdc, fd->fdsu, TURNON);
878 out_fdc(fdc, NE7CMD_SENSED); /* Sense Drive Status */
879 DELAY(100);
880 out_fdc(fdc, fdu); /* Drive number */
881 DELAY(100);
882 if ((fd_in(fdc, &status) == 0) && (status & NE7_ST3_RD)) {
883 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
884 DELAY(10);
885 return 0;
886 }
887 }
888 return -1;
889}
890#endif /* PC98 */
891
892static int
893fdc_alloc_resources(struct fdc_data *fdc)
894{
895 device_t dev;
896#ifdef PC98
897 int rid;
898#else
899 int ispnp, ispcmcia, nports;
900#endif
901
902 dev = fdc->fdc_dev;
903#ifndef PC98
904 ispnp = (fdc->flags & FDC_ISPNP) != 0;
905 ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
906#endif
907 fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
908 fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
909
910#ifdef PC98
911 fdc->res_ioport = isa_alloc_resourcev(dev, SYS_RES_IOPORT,
912 &fdc->rid_ioport, fdc_iat,
913 3, RF_ACTIVE);
914 if (fdc->res_ioport == 0) {
915 device_printf(dev, "cannot reserve I/O port range\n");
916 return ENXIO;
917 }
918 isa_load_resourcev(fdc->res_ioport, fdc_iat, 3);
919#else
920 /*
921 * On standard ISA, we don't just use an 8 port range
922 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
923 * register at 0x3f6.
924 *
925 * Isn't PC hardware wonderful.
926 *
927 * The Y-E Data PCMCIA FDC doesn't have this problem, it
928 * uses the register with offset 6 for pseudo-DMA, and the
929 * one with offset 7 as control register.
930 */
931 nports = ispcmcia ? 8 : (ispnp ? 1 : 6);
932 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
933 &fdc->rid_ioport, 0ul, ~0ul,
934 nports, RF_ACTIVE);
935 if (fdc->res_ioport == 0) {
936 device_printf(dev, "cannot reserve I/O port range (%d ports)\n",
937 nports);
938 return ENXIO;
939 }
940#endif
941 fdc->portt = rman_get_bustag(fdc->res_ioport);
942 fdc->porth = rman_get_bushandle(fdc->res_ioport);
943
944#ifdef PC98
945 rid = 3;
946 bus_set_resource(dev, SYS_RES_IOPORT, rid, IO_FDPORT, 1);
947 fdc->res_fdsio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
948 1, RF_ACTIVE);
949 if (fdc->res_fdsio == 0)
950 return ENXIO;
951 fdc->sc_fdsiot = rman_get_bustag(fdc->res_fdsio);
952 fdc->sc_fdsioh = rman_get_bushandle(fdc->res_fdsio);
953
954 rid = 4;
955 bus_set_resource(dev, SYS_RES_IOPORT, rid, 0x4be, 1);
956 fdc->res_fdemsio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
957 1, RF_ACTIVE);
958 if (fdc->res_fdemsio == 0)
959 return ENXIO;
960 fdc->sc_fdemsiot = rman_get_bustag(fdc->res_fdemsio);
961 fdc->sc_fdemsioh = rman_get_bushandle(fdc->res_fdemsio);
962#endif
963
964#ifndef PC98
965 if (!ispcmcia) {
966 /*
967 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
968 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
969 * by checking the size and adjust the port address
970 * accordingly.
971 */
972 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
973 fdc->port_off = -2;
974
975 /*
976 * Register the control port range as rid 1 if it
977 * isn't there already. Most PnP BIOSen will have
978 * already done this but non-PnP configurations don't.
979 *
980 * And some (!!) report 0x3f2-0x3f5 and completely
981 * leave out the control register! It seems that some
982 * non-antique controller chips have a different
983 * method of programming the transfer speed which
984 * doesn't require the control register, but it's
985 * mighty bogus as the chip still responds to the
986 * address for the control register.
987 */
988 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
989 u_long ctlstart;
990
991 /* Find the control port, usually 0x3f7 */
992 ctlstart = rman_get_start(fdc->res_ioport) +
993 fdc->port_off + 7;
994
995 bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
996 }
997
998 /*
999 * Now (finally!) allocate the control port.
1000 */
1001 fdc->rid_ctl = 1;
1002 fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
1003 &fdc->rid_ctl,
1004 0ul, ~0ul, 1, RF_ACTIVE);
1005 if (fdc->res_ctl == 0) {
1006 device_printf(dev,
1007 "cannot reserve control I/O port range (control port)\n");
1008 return ENXIO;
1009 }
1010 fdc->ctlt = rman_get_bustag(fdc->res_ctl);
1011 fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
1012 }
1013#endif
1014
1015 fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
1016 &fdc->rid_irq, 0ul, ~0ul, 1,
1017 RF_ACTIVE);
1018 if (fdc->res_irq == 0) {
1019 device_printf(dev, "cannot reserve interrupt line\n");
1020 return ENXIO;
1021 }
1022
1023 if ((fdc->flags & FDC_NODMA) == 0) {
1024 fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
1025 &fdc->rid_drq, 0ul, ~0ul, 1,
1026 RF_ACTIVE);
1027 if (fdc->res_drq == 0) {
1028 device_printf(dev, "cannot reserve DMA request line\n");
1029 return ENXIO;
1030 }
1031 fdc->dmachan = fdc->res_drq->r_start;
1032 }
1033
1034 return 0;
1035}
1036
1037static void
1038fdc_release_resources(struct fdc_data *fdc)
1039{
1040 device_t dev;
1041
1042 dev = fdc->fdc_dev;
1043 if (fdc->res_irq != 0) {
1044 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
1045 fdc->res_irq);
1046 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
1047 fdc->res_irq);
1048 }
1049#ifndef PC98
1050 if (fdc->res_ctl != 0) {
1051 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
1052 fdc->res_ctl);
1053 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
1054 fdc->res_ctl);
1055 }
1056#endif
1057#ifdef PC98
1058 if (fdc->res_fdsio != 0) {
1059 bus_deactivate_resource(dev, SYS_RES_IOPORT, 3,
1060 fdc->res_fdsio);
1061 bus_release_resource(dev, SYS_RES_IOPORT, 3, fdc->res_fdsio);
1062 }
1063 if (fdc->res_fdemsio != 0) {
1064 bus_deactivate_resource(dev, SYS_RES_IOPORT, 4,
1065 fdc->res_fdemsio);
1066 bus_release_resource(dev, SYS_RES_IOPORT, 4, fdc->res_fdemsio);
1067 }
1068#endif
1069 if (fdc->res_ioport != 0) {
1070 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
1071 fdc->res_ioport);
1072 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
1073 fdc->res_ioport);
1074 }
1075 if (fdc->res_drq != 0) {
1076 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
1077 fdc->res_drq);
1078 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
1079 fdc->res_drq);
1080 }
1081}
1082
1083/*
1084 * Configuration/initialization stuff, per controller.
1085 */
1086
1087static struct isa_pnp_id fdc_ids[] = {
1088 {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
1089 {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
1090 {0}
1091};
1092
1093static int
1094fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1095{
1096 struct fdc_ivars *ivars = device_get_ivars(child);
1097
1098 switch (which) {
1099 case FDC_IVAR_FDUNIT:
1100 *result = ivars->fdunit;
1101 break;
1102 default:
1103 return ENOENT;
1104 }
1105 return 0;
1106}
1107
1108static int
1109fdc_probe(device_t dev)
1110{
1111#ifdef PC98
1112 int error;
1113#else
1114 int error, ic_type;
1115#endif
1116 struct fdc_data *fdc;
1117
1118 fdc = device_get_softc(dev);
1119 bzero(fdc, sizeof *fdc);
1120 fdc->fdc_dev = dev;
1121#ifndef PC98
1122 fdc->fdctl_wr = fdctl_wr_isa;
1123#endif
1124
1125 /* Check pnp ids */
1126 error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
1127 if (error == ENXIO)
1128 return ENXIO;
1129 if (error == 0)
1130 fdc->flags |= FDC_ISPNP;
1131
1132 /* Attempt to allocate our resources for the duration of the probe */
1133 error = fdc_alloc_resources(fdc);
1134 if (error)
1135 goto out;
1136
1137#ifndef PC98
1138 /* First - lets reset the floppy controller */
1139 fdout_wr(fdc, 0);
1140 DELAY(100);
1141 fdout_wr(fdc, FDO_FRST);
1142#endif
1143
1144 /* see if it can handle a command */
1145#ifdef PC98
1146 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(4, 240),
1147 NE7_SPEC_2(2, 0), 0)) {
1148 error = ENXIO;
1149 goto out;
1150 }
1151#else
1152 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
1153 NE7_SPEC_2(2, 0), 0)) {
1154 error = ENXIO;
1155 goto out;
1156 }
1157#endif
1158
1159#ifndef PC98
1160 if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
1161 ic_type = (u_char)ic_type;
1162 switch (ic_type) {
1163 case 0x80:
1164 device_set_desc(dev, "NEC 765 or clone");
1165 fdc->fdct = FDC_NE765;
1166 break;
1167 case 0x81: /* not mentioned in any hardware doc */
1168 case 0x90:
1169 device_set_desc(dev,
1170 "Enhanced floppy controller (i82077, NE72065 or clone)");
1171 fdc->fdct = FDC_ENHANCED;
1172 break;
1173 default:
1174 device_set_desc(dev, "Generic floppy controller");
1175 fdc->fdct = FDC_UNKNOWN;
1176 break;
1177 }
1178 }
1179#endif
1180
1181out:
1182 fdc_release_resources(fdc);
1183 return (error);
1184}
1185
1186#if NCARD > 0
1187
1188static int
1189fdc_pccard_probe(device_t dev)
1190{
1191 int error;
1192 struct fdc_data *fdc;
1193
1194 fdc = device_get_softc(dev);
1195 bzero(fdc, sizeof *fdc);
1196 fdc->fdc_dev = dev;
1197#ifndef PC98
1198 fdc->fdctl_wr = fdctl_wr_pcmcia;
1199#endif
1200
1201 fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
1202
1203 /* Attempt to allocate our resources for the duration of the probe */
1204 error = fdc_alloc_resources(fdc);
1205 if (error)
1206 goto out;
1207
1208#ifndef PC98
1209 /* First - lets reset the floppy controller */
1210 fdout_wr(fdc, 0);
1211 DELAY(100);
1212 fdout_wr(fdc, FDO_FRST);
1213#endif
1214
1215 /* see if it can handle a command */
1216#ifdef PC98
1217 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(4, 240),
1218 NE7_SPEC_2(2, 0), 0)) {
1219 error = ENXIO;
1220 goto out;
1221 }
1222#else
1223 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
1224 NE7_SPEC_2(2, 0), 0)) {
1225 error = ENXIO;
1226 goto out;
1227 }
1228#endif
1229
1230 device_set_desc(dev, "Y-E Data PCMCIA floppy");
1231 fdc->fdct = FDC_NE765;
1232
1233out:
1234 fdc_release_resources(fdc);
1235 return (error);
1236}
1237
1238#endif /* NCARD > 0 */
1239
1240static int
1241fdc_detach(device_t dev)
1242{
1243 struct fdc_data *fdc;
1244 int error;
1245
1246 fdc = device_get_softc(dev);
1247
1248 /* have our children detached first */
1249 if ((error = bus_generic_detach(dev)))
1250 return (error);
1251
1252#ifdef PC98
1253 /* reset controller, turn motor off */
1254 fdc_reset(fdc);
1255#else
1256 /* reset controller, turn motor off */
1257 fdout_wr(fdc, 0);
1258#endif
1259
1260 if ((fdc->flags & FDC_NODMA) == 0)
1261 isa_dma_release(fdc->dmachan);
1262
1263 if ((fdc->flags & FDC_ATTACHED) == 0) {
1264 device_printf(dev, "already unloaded\n");
1265 return (0);
1266 }
1267 fdc->flags &= ~FDC_ATTACHED;
1268
1269 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
1270 fdc->fdc_intr);
1271 fdc_release_resources(fdc);
1272 device_printf(dev, "unload\n");
1273 return (0);
1274}
1275
1276/*
1277 * Add a child device to the fdc controller. It will then be probed etc.
1278 */
1279static void
1280fdc_add_child(device_t dev, const char *name, int unit)
1281{
1282 int disabled, flags;
1283 struct fdc_ivars *ivar;
1284 device_t child;
1285
1286 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
1287 if (ivar == NULL)
1288 return;
1289 if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
1290 ivar->fdunit = 0;
1291 child = device_add_child(dev, name, unit);
1292 if (child == NULL) {
1293 free(ivar, M_DEVBUF);
1294 return;
1295 }
1296 device_set_ivars(child, ivar);
1297 if (resource_int_value(name, unit, "flags", &flags) == 0)
1298 device_set_flags(child, flags);
1299 if (resource_int_value(name, unit, "disabled", &disabled) == 0
1300 && disabled != 0)
1301 device_disable(child);
1302}
1303
1304static int
1305fdc_attach(device_t dev)
1306{
1307 struct fdc_data *fdc;
1308 const char *name, *dname;
1309 int i, error, dunit;
1310
1311 fdc = device_get_softc(dev);
1312 error = fdc_alloc_resources(fdc);
1313 if (error) {
1314 device_printf(dev, "cannot re-acquire resources\n");
1315 return error;
1316 }
1317 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
1318 INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc,
1319 &fdc->fdc_intr);
1320 if (error) {
1321 device_printf(dev, "cannot setup interrupt\n");
1322 return error;
1323 }
1324 fdc->fdcu = device_get_unit(dev);
1325 fdc->flags |= FDC_ATTACHED | FDC_NEEDS_RESET;
1326
1327 if ((fdc->flags & FDC_NODMA) == 0) {
1328 /*
1329 * Acquire the DMA channel forever, the driver will do
1330 * the rest
1331 * XXX should integrate with rman
1332 */
1333 isa_dma_acquire(fdc->dmachan);
1334 isa_dmainit(fdc->dmachan, MAX_SEC_SIZE);
1335 }
1336 fdc->state = DEVIDLE;
1337
1338#ifdef PC98
1339 /* reset controller, turn motor off, clear fdout mirror reg */
1340 fdc_reset(fdc);
1341#else
1342 /* reset controller, turn motor off, clear fdout mirror reg */
1343 fdout_wr(fdc, fdc->fdout = 0);
1344#endif
1345 bioq_init(&fdc->head);
1346
1347 /*
1348 * Probe and attach any children. We should probably detect
1349 * devices from the BIOS unless overridden.
1350 */
1351 name = device_get_nameunit(dev);
1352 i = 0;
1353 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0)
1354 fdc_add_child(dev, dname, dunit);
1355
1356 if ((error = bus_generic_attach(dev)) != 0)
1357 return (error);
1358
1359 return (0);
1360}
1361
1362static int
1363fdc_print_child(device_t me, device_t child)
1364{
1365 int retval = 0, flags;
1366
1367 retval += bus_print_child_header(me, child);
1368 retval += printf(" on %s drive %d", device_get_nameunit(me),
1369 fdc_get_fdunit(child));
1370 if ((flags = device_get_flags(me)) != 0)
1371 retval += printf(" flags %#x", flags);
1372 retval += printf("\n");
1373
1374 return (retval);
1375}
1376
1377static device_method_t fdc_methods[] = {
1378 /* Device interface */
1379 DEVMETHOD(device_probe, fdc_probe),
1380 DEVMETHOD(device_attach, fdc_attach),
1381 DEVMETHOD(device_detach, fdc_detach),
1382 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1383 DEVMETHOD(device_suspend, bus_generic_suspend),
1384 DEVMETHOD(device_resume, bus_generic_resume),
1385
1386 /* Bus interface */
1387 DEVMETHOD(bus_print_child, fdc_print_child),
1388 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
1389 /* Our children never use any other bus interface methods. */
1390
1391 { 0, 0 }
1392};
1393
1394static driver_t fdc_driver = {
1395 "fdc",
1396 fdc_methods,
1397 sizeof(struct fdc_data)
1398};
1399
1400DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
1401#ifndef PC98
1402DRIVER_MODULE(fdc, acpi, fdc_driver, fdc_devclass, 0, 0);
1403#endif
1404
1405#if NCARD > 0
1406
1407static device_method_t fdc_pccard_methods[] = {
1408 /* Device interface */
1409 DEVMETHOD(device_probe, fdc_pccard_probe),
1410 DEVMETHOD(device_attach, fdc_attach),
1411 DEVMETHOD(device_detach, fdc_detach),
1412 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1413 DEVMETHOD(device_suspend, bus_generic_suspend),
1414 DEVMETHOD(device_resume, bus_generic_resume),
1415
1416 /* Bus interface */
1417 DEVMETHOD(bus_print_child, fdc_print_child),
1418 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
1419 /* Our children never use any other bus interface methods. */
1420
1421 { 0, 0 }
1422};
1423
1424static driver_t fdc_pccard_driver = {
1425 "fdc",
1426 fdc_pccard_methods,
1427 sizeof(struct fdc_data)
1428};
1429
1430DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
1431
1432#endif /* NCARD > 0 */
1433
1434/*
1435 * Create a clone device upon request by devfs.
1436 */
1437static void
1438fd_clone(void *arg, char *name, int namelen, dev_t *dev)
1439{
1440 struct fd_data *fd;
1441 int i, u;
1442 char *n;
1443 size_t l;
1444
1445 fd = (struct fd_data *)arg;
1446 if (*dev != NODEV)
1447 return;
1448 if (dev_stdclone(name, &n, "fd", &u) != 2)
1449 return;
1450 if (u != fd->fdu)
1451 /* unit # mismatch */
1452 return;
1453 l = strlen(n);
1454 if (l == 1 && *n >= 'a' && *n <= 'h') {
1455 /*
1456 * Trailing letters a through h denote
1457 * pseudo-partitions. We don't support true
1458 * (UFS-style) partitions, so we just implement them
1459 * as symlinks if someone asks us nicely.
1460 */
1461 *dev = make_dev_alias(fd->masterdev, name);
1462 return;
1463 }
1464 if (l >= 2 && l <= 5 && *n == '.') {
1465 /*
1466 * Trailing numbers, preceded by a dot, denote
1467 * subdevices for different densities. Historically,
1468 * they have been named by density (like fd0.1440),
1469 * but we allow arbitrary numbers between 1 and 4
1470 * digits, so fd0.1 through fd0.15 are possible as
1471 * well.
1472 */
1473 for (i = 1; i < l; i++)
1474 if (n[i] < '0' || n[i] > '9')
1475 return;
1476 for (i = 0; i < NUMDENS - 1; i++)
1477 if (fd->clonedevs[i] == NODEV) {
1478 *dev = make_dev(&fd_cdevsw,
1479 FDNUMTOUNIT(u) + i + 1,
1480 UID_ROOT, GID_OPERATOR, 0640,
1481 name);
1482 fd->clonedevs[i] = *dev;
1483 return;
1484 }
1485 }
1486}
1487
1488/*
1489 * Configuration/initialization, per drive.
1490 */
1491static int
1492fd_probe(device_t dev)
1493{
1494 int i;
1495#ifndef PC98
1496 u_int st0, st3;
1497#endif
1498 struct fd_data *fd;
1499 struct fdc_data *fdc;
1500 fdsu_t fdsu;
1501 int flags;
1502
1503 fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
1504 fd = device_get_softc(dev);
1505 fdc = device_get_softc(device_get_parent(dev));
1506 flags = device_get_flags(dev);
1507
1508 bzero(fd, sizeof *fd);
1509 fd->dev = dev;
1510 fd->fdc = fdc;
1511 fd->fdsu = fdsu;
1512 fd->fdu = device_get_unit(dev);
1513 fd->flags = FD_UA; /* make sure fdautoselect() will be called */
1514
1515 fd->type = FD_DTYPE(flags);
1516#ifdef PC98
1517 if (fd->type == FDT_NONE && fd->fdu >= 0 && fd->fdu <= 3) {
1518 /* Look up what the BIOS thinks we have. */
1519 if ((PC98_SYSTEM_PARAMETER(0x5ae) >> fd->fdu) & 0x01)
1520 fd->type = FDT_144M;
1521#ifdef EPSON_NRDISK
1522 else if ((PC98_SYSTEM_PARAMETER(0x55c) >> fd->fdu) & 0x01) {
1523 fd->type = FDT_12M;
1524 switch (epson_machine_id) {
1525 case 0x20:
1526 case 0x27:
1527 if ((PC98_SYSTEM_PARAMETER(0x488) >> fd->fdu)
1528 & 0x01) {
1529 if (nrd_check_ready()) {
1530 nrd_LED_on();
1531 nrdu = fd->fdu;
1532 } else {
1533 fd->type = FDT_NONE;
1534 }
1535 }
1536 break;
1537 }
1538 }
1539#else /* !EPSON_NRDISK */
1540 else if ((PC98_SYSTEM_PARAMETER(0x55c) >> fd->fdu) & 0x01) {
1541 fd->type = FDT_12M;
1542 switch (epson_machine_id) {
1543 case 0x20:
1544 case 0x27:
1545 if ((PC98_SYSTEM_PARAMETER(0x488) >> fd->fdu)
1546 & 0x01)
1547 fd->type = FDT_NONE;
1548 break;
1549 }
1550 }
1551#endif /* EPSON_NRDISK */
1552 }
1553#else /* PC98 */
1554/*
1555 * XXX I think using __i386__ is wrong here since we actually want to probe
1556 * for the machine type, not the CPU type (so non-PC arch's like the PC98 will
1557 * fail the probe). However, for whatever reason, testing for _MACHINE_ARCH
1558 * == i386 breaks the test on FreeBSD/Alpha.
1559 */
1560#ifdef __i386__
1561 if (fd->type == FDT_NONE && (fd->fdu == 0 || fd->fdu == 1)) {
1562 /* Look up what the BIOS thinks we have. */
1563 if (fd->fdu == 0) {
1564 if ((fdc->flags & FDC_ISPCMCIA))
1565 /*
1566 * Somewhat special. No need to force the
1567 * user to set device flags, since the Y-E
1568 * Data PCMCIA floppy is always a 1.44 MB
1569 * device.
1570 */
1571 fd->type = FDT_144M;
1572 else
1573 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
1574 } else {
1575 fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
1576 }
1577 if (fd->type == FDT_288M_1)
1578 fd->type = FDT_288M;
1579 }
1580#endif /* __i386__ */
1581#endif /* PC98 */
1582
1583 /* is there a unit? */
1584 if (fd->type == FDT_NONE)
1585 return (ENXIO);
1586
1587#ifndef PC98
1588 /* select it */
1589 set_motor(fdc, fdsu, TURNON);
1590 fdc_reset(fdc); /* XXX reset, then unreset, etc. */
1591 DELAY(1000000); /* 1 sec */
1592
1593 /* XXX This doesn't work before the first set_motor() */
1594 if ((fdc->flags & FDC_HAS_FIFO) == 0 &&
1595 fdc->fdct == FDC_ENHANCED &&
1596 (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 &&
1597 enable_fifo(fdc) == 0) {
1598 device_printf(device_get_parent(dev),
1599 "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1600 }
1601
1602 if ((flags & FD_NO_PROBE) == 0) {
1603 /* If we're at track 0 first seek inwards. */
1604 if ((fd_sense_drive_status(fdc, &st3) == 0) &&
1605 (st3 & NE7_ST3_T0)) {
1606 /* Seek some steps... */
1607 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1608 /* ...wait a moment... */
1609 DELAY(300000);
1610 /* make ctrlr happy: */
1611 fd_sense_int(fdc, 0, 0);
1612 }
1613 }
1614
1615 for (i = 0; i < 2; i++) {
1616 /*
1617 * we must recalibrate twice, just in case the
1618 * heads have been beyond cylinder 76, since
1619 * most FDCs still barf when attempting to
1620 * recalibrate more than 77 steps
1621 */
1622 /* go back to 0: */
1623 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1624 /* a second being enough for full stroke seek*/
1625 DELAY(i == 0 ? 1000000 : 300000);
1626
1627 /* anything responding? */
1628 if (fd_sense_int(fdc, &st0, 0) == 0 &&
1629 (st0 & NE7_ST0_EC) == 0)
1630 break; /* already probed succesfully */
1631 }
1632 }
1633 }
1634
1635 set_motor(fdc, fdsu, TURNOFF);
1636
1637 if ((flags & FD_NO_PROBE) == 0 &&
1638 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
1639 return (ENXIO);
1640#endif /* PC98 */
1641
1642#ifdef PC98
1643 switch (fd->type) {
1644 case FDT_144M:
1645 /* Check 3mode I/F */
1646 fd->pc98_trans = 0;
1647 bus_space_write_1(fdc->sc_fdemsiot, fdc->sc_fdemsioh, 0,
1648 (fd->fdu << 5) | 0x10);
1649 if (!(bus_space_read_1(fdc->sc_fdemsiot, fdc->sc_fdemsioh, 0) &
1650 0x01)) {
1651 device_set_desc(dev, "1.44M FDD");
1652 fd->type = FDT_144M;
1653 break;
1654 }
1655
1656 device_printf(dev,
1657 "Warning: can't control 3mode I/F, fallback to 2mode.\n");
1658 /* FALLTHROUGH */
1659 case FDT_12M:
1660#ifdef EPSON_NRDISK
1661 if (fd->fdu == nrdu) {
1662 device_set_desc(dev, "EPSON RAM DRIVE");
1663 nrd_LED_off();
1664 } else
1665 device_set_desc(dev, "1M/640K FDD");
1666#else
1667 device_set_desc(dev, "1M/640K FDD");
1668#endif
1669 fd->type = FDT_12M;
1670 break;
1671 default:
1672 return (ENXIO);
1673 }
1674#else
1675 switch (fd->type) {
1676 case FDT_12M:
1677 device_set_desc(dev, "1200-KB 5.25\" drive");
1678 fd->type = FDT_12M;
1679 break;
1680 case FDT_144M:
1681 device_set_desc(dev, "1440-KB 3.5\" drive");
1682 fd->type = FDT_144M;
1683 break;
1684 case FDT_288M:
1685 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1686 fd->type = FDT_288M;
1687 break;
1688 case FDT_360K:
1689 device_set_desc(dev, "360-KB 5.25\" drive");
1690 fd->type = FDT_360K;
1691 break;
1692 case FDT_720K:
1693 device_set_desc(dev, "720-KB 3.5\" drive");
1694 fd->type = FDT_720K;
1695 break;
1696 default:
1697 return (ENXIO);
1698 }
1699#endif
1700 fd->track = FD_NO_TRACK;
1701 fd->fdc = fdc;
1702 fd->fdsu = fdsu;
1703 fd->options = 0;
1704#ifdef PC98
1705 fd->pc98_trans = 0;
1706#endif
1707 callout_handle_init(&fd->toffhandle);
1708 callout_handle_init(&fd->tohandle);
1709
1710 /* initialize densities for subdevices */
1711#ifdef PC98
1712 for (i = 0; i < NUMDENS; i++)
1713 memcpy(fd->fts + i, fd_searchlist_144m + i,
1714 sizeof(struct fd_type));
1715#else
1716 for (i = 0; i < NUMDENS; i++)
1717 memcpy(fd->fts + i, fd_native_types + fd->type,
1718 sizeof(struct fd_type));
1719#endif
1720 return (0);
1721}
1722
1723static int
1724fd_attach(device_t dev)
1725{
1726 struct fd_data *fd;
1727 int i;
1728
1729 fd = device_get_softc(dev);
1730 fd->clonetag = EVENTHANDLER_REGISTER(dev_clone, fd_clone, fd, 1000);
1731 fd->masterdev = make_dev(&fd_cdevsw, fd->fdu << 6,
1732 UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu);
1733 for (i = 0; i < NUMDENS - 1; i++)
1734 fd->clonedevs[i] = NODEV;
1735 devstat_add_entry(&fd->device_stats, device_get_name(dev),
1736 device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS,
1737 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
1738 DEVSTAT_PRIORITY_FD);
1739 return (0);
1740}
1741
1742static int
1743fd_detach(device_t dev)
1744{
1745 struct fd_data *fd;
1746 int i;
1747
1748 fd = device_get_softc(dev);
1749 untimeout(fd_turnoff, fd, fd->toffhandle);
1750 devstat_remove_entry(&fd->device_stats);
1751 destroy_dev(fd->masterdev);
1752 for (i = 0; i < NUMDENS - 1; i++)
1753 if (fd->clonedevs[i] != NODEV)
1754 destroy_dev(fd->clonedevs[i]);
1755 EVENTHANDLER_DEREGISTER(dev_clone, fd->clonetag);
1756
1757 return (0);
1758}
1759
1760static device_method_t fd_methods[] = {
1761 /* Device interface */
1762 DEVMETHOD(device_probe, fd_probe),
1763 DEVMETHOD(device_attach, fd_attach),
1764 DEVMETHOD(device_detach, fd_detach),
1765 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1766 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
1767 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
1768
1769 { 0, 0 }
1770};
1771
1772static driver_t fd_driver = {
1773 "fd",
1774 fd_methods,
1775 sizeof(struct fd_data)
1776};
1777
1778DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
1779
1780/*
1781 * More auxiliary functions.
1782 */
1783/*
1784 * Motor control stuff.
1785 * Remember to not deselect the drive we're working on.
1786 */
1787static void
1788set_motor(struct fdc_data *fdc, int fdsu, int turnon)
1789{
1790#ifdef PC98
1791 bus_space_write_1(fdc->sc_fdsiot, fdc->sc_fdsioh, 0,
1792 (pc98_trans != 1 ? FDP_FDDEXC : 0) | FDP_PORTEXC);
1793 DELAY(10);
1794 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
1795#else
1796 int fdout;
1797
1798 fdout = fdc->fdout;
1799 if (turnon) {
1800 fdout &= ~FDO_FDSEL;
1801 fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu;
1802 } else
1803 fdout &= ~(FDO_MOEN0 << fdsu);
1804 fdc->fdout = fdout;
1805 fdout_wr(fdc, fdout);
1806 TRACE1("[0x%x->FDOUT]", fdout);
1807#endif
1808}
1809
1810static void
1811fd_turnoff(void *xfd)
1812{
1813 int s;
1814 fd_p fd = xfd;
1815
1816 TRACE1("[fd%d: turnoff]", fd->fdu);
1817
1818 s = splbio();
1819 /*
1820 * Don't turn off the motor yet if the drive is active.
1821 *
1822 * If we got here, this could only mean we missed an interrupt.
1823 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
1824 * after a controller reset. Just schedule a pseudo-interrupt
1825 * so the state machine gets re-entered.
1826 */
1827 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
1828 fdc_intr(fd->fdc);
1829 splx(s);
1830 return;
1831 }
1832
1833 fd->flags &= ~FD_MOTOR;
1834 set_motor(fd->fdc, fd->fdsu, TURNOFF);
1835 splx(s);
1836}
1837
1838static void
1839fd_motor_on(void *xfd)
1840{
1841 int s;
1842 fd_p fd = xfd;
1843
1844 s = splbio();
1845 fd->flags &= ~FD_MOTOR_WAIT;
1846 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
1847 {
1848 fdc_intr(fd->fdc);
1849 }
1850 splx(s);
1851}
1852
1853static void
1854fd_turnon(fd_p fd)
1855{
1856 if(!(fd->flags & FD_MOTOR))
1857 {
1858 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
1859 set_motor(fd->fdc, fd->fdsu, TURNON);
1860 timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
1861 }
1862}
1863
1864static void
1865fdc_reset(fdc_p fdc)
1866{
1867 /* Try a reset, keep motor on */
1868#ifdef PC98
1869 set_density(fdc);
1870 if (pc98_machine_type & M_EPSON_PC98)
1871 fdctl_wr(fdc, FDC_RST | FDC_RDY | FDC_DD | FDC_MTON);
1872 else
1873 fdctl_wr(fdc, FDC_RST | FDC_RDY | FDC_DMAE | FDC_MTON);
1874 DELAY(200);
1875 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
1876 DELAY(10);
1877#else
1878 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1879 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1880 DELAY(100);
1881 /* enable FDC, but defer interrupts a moment */
1882 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
1883 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
1884 DELAY(100);
1885 fdout_wr(fdc, fdc->fdout);
1886 TRACE1("[0x%x->FDOUT]", fdc->fdout);
1887#endif
1888
1889 /* XXX after a reset, silently believe the FDC will accept commands */
1890#ifdef PC98
1891 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1892 NE7_SPEC_1(4, 240), NE7_SPEC_2(2, 0),
1893 0);
1894#else
1895 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1896 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1897 0);
1898#endif
1899 if (fdc->flags & FDC_HAS_FIFO)
1900 (void) enable_fifo(fdc);
1901}
1902
1903/*
1904 * FDC IO functions, take care of the main status register, timeout
1905 * in case the desired status bits are never set.
1906 *
1907 * These PIO loops initially start out with short delays between
1908 * each iteration in the expectation that the required condition
1909 * is usually met quickly, so it can be handled immediately. After
1910 * about 1 ms, stepping is increased to achieve a better timing
1911 * accuracy in the calls to DELAY().
1912 */
1913static int
1914fd_in(struct fdc_data *fdc, int *ptr)
1915{
1916 int i, j, step;
1917
1918 for (j = 0, step = 1;
1919 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) &&
1920 j < FDSTS_TIMEOUT;
1921 j += step) {
1922 if (i == NE7_RQM)
1923 return (fdc_err(fdc, "ready for output in input\n"));
1924 if (j == 1000)
1925 step = 1000;
1926 DELAY(step);
1927 }
1928 if (j >= FDSTS_TIMEOUT)
1929 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
1930#ifdef FDC_DEBUG
1931 i = fddata_rd(fdc);
1932 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1933 *ptr = i;
1934 return (0);
1935#else /* !FDC_DEBUG */
1936 i = fddata_rd(fdc);
1937 if (ptr)
1938 *ptr = i;
1939 return (0);
1940#endif /* FDC_DEBUG */
1941}
1942
1943static int
1944out_fdc(struct fdc_data *fdc, int x)
1945{
1946 int i, j, step;
1947
1948 for (j = 0, step = 1;
1949 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM &&
1950 j < FDSTS_TIMEOUT;
1951 j += step) {
1952 if (i == (NE7_DIO|NE7_RQM))
1953 return (fdc_err(fdc, "ready for input in output\n"));
1954 if (j == 1000)
1955 step = 1000;
1956 DELAY(step);
1957 }
1958 if (j >= FDSTS_TIMEOUT)
1959 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
1960
1961 /* Send the command and return */
1962 fddata_wr(fdc, x);
1963 TRACE1("[0x%x->FDDATA]", x);
1964 return (0);
1965}
1966
1967/*
1968 * Block device driver interface functions (interspersed with even more
1969 * auxiliary functions).
1970 */
1971static int
1972Fdopen(dev_t dev, int flags, int mode, struct thread *td)
1973{
1974 fdu_t fdu = FDUNIT(minor(dev));
1975 int type = FDTYPE(minor(dev));
1976 fd_p fd;
1977 fdc_p fdc;
1978 int rv, unitattn, dflags;
1979
1980 if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1981 return (ENXIO);
1982 fdc = fd->fdc;
1983 if ((fdc == NULL) || (fd->type == FDT_NONE))
1984 return (ENXIO);
1985 if (type > NUMDENS)
1986 return (ENXIO);
1987 dflags = device_get_flags(fd->dev);
1988 /*
1989 * This is a bit bogus. It's still possible that e. g. a
1990 * descriptor gets inherited to a child, but then it's at
1991 * least for the same subdevice. By checking FD_OPEN here, we
1992 * can ensure that a device isn't attempted to be opened with
1993 * different densities at the same time where the second open
1994 * could clobber the settings from the first one.
1995 */
1996 if (fd->flags & FD_OPEN)
1997 return (EBUSY);
1998
1999#ifdef PC98
2000 if (pc98_fd_check_ready(fdu) == -1)
2001 return(EIO);
2002#endif
2003
2004 if (type == 0) {
2005 if (flags & FNONBLOCK) {
2006 /*
2007 * Unfortunately, physio(9) discards its ioflag
2008 * argument, thus preventing us from seeing the
2009 * IO_NDELAY bit. So we need to keep track
2010 * ourselves.
2011 */
2012 fd->flags |= FD_NONBLOCK;
2013 fd->ft = 0;
2014 } else {
2015 /*
2016 * Figure out a unit attention condition.
2017 *
2018 * If UA has been forced, proceed.
2019 *
2020 * If motor is off, turn it on for a moment
2021 * and select our drive, in order to read the
2022 * UA hardware signal.
2023 *
2024 * If motor is on, and our drive is currently
2025 * selected, just read the hardware bit.
2026 *
2027 * If motor is on, but active for another
2028 * drive on that controller, we are lost. We
2029 * cannot risk to deselect the other drive, so
2030 * we just assume a forced UA condition to be
2031 * on the safe side.
2032 */
2033 unitattn = 0;
2034 if ((dflags & FD_NO_CHLINE) != 0 ||
2035 (fd->flags & FD_UA) != 0) {
2036 unitattn = 1;
2037 fd->flags &= ~FD_UA;
2038#ifndef PC98
2039 } else if (fdc->fdout & (FDO_MOEN0 | FDO_MOEN1 |
2040 FDO_MOEN2 | FDO_MOEN3)) {
2041 if ((fdc->fdout & FDO_FDSEL) == fd->fdsu)
2042 unitattn = fdin_rd(fdc) & FDI_DCHG;
2043 else
2044 unitattn = 1;
2045 } else {
2046 set_motor(fdc, fd->fdsu, TURNON);
2047 unitattn = fdin_rd(fdc) & FDI_DCHG;
2048 set_motor(fdc, fd->fdsu, TURNOFF);
2049#endif /* PC98 */
2050 }
2051 if (unitattn && (rv = fdautoselect(dev)) != 0)
2052 return (rv);
2053 }
2054 } else {
2055 fd->ft = fd->fts + type;
2056 }
2057 fd->flags |= FD_OPEN;
2058 /*
2059 * Clearing the DMA overrun counter at open time is a bit messy.
2060 * Since we're only managing one counter per controller, opening
2061 * the second drive could mess it up. Anyway, if the DMA overrun
2062 * condition is really persistent, it will eventually time out
2063 * still. OTOH, clearing it here will ensure we'll at least start
2064 * trying again after a previous (maybe even long ago) failure.
2065 * Also, this is merely a stop-gap measure only that should not
2066 * happen during normal operation, so we can tolerate it to be a
2067 * bit sloppy about this.
2068 */
2069 fdc->dma_overruns = 0;
2070
2071 return 0;
2072}
2073
2074static int
2075fdclose(dev_t dev, int flags, int mode, struct thread *td)
2076{
2077 fdu_t fdu = FDUNIT(minor(dev));
2078 struct fd_data *fd;
2079
2080 fd = devclass_get_softc(fd_devclass, fdu);
2081 fd->flags &= ~(FD_OPEN | FD_NONBLOCK);
2082 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
2083
2084 return (0);
2085}
2086
2087static void
2088fdstrategy(struct bio *bp)
2089{
2090 long blknum, nblocks;
2091 int s;
2092 fdu_t fdu;
2093 fdc_p fdc;
2094 fd_p fd;
2095 size_t fdblk;
2096
2097 fdu = FDUNIT(minor(bp->bio_dev));
2098 fd = devclass_get_softc(fd_devclass, fdu);
2099 if (fd == 0)
2100 panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
2101 (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev));
2102 fdc = fd->fdc;
2103 if (fd->type == FDT_NONE || fd->ft == 0) {
2104 bp->bio_error = ENXIO;
2105 bp->bio_flags |= BIO_ERROR;
2106 goto bad;
2107 }
2108 fdblk = 128 << (fd->ft->secsize);
2109 if (bp->bio_cmd != FDBIO_FORMAT && bp->bio_cmd != FDBIO_RDSECTID) {
2110 if (fd->flags & FD_NONBLOCK) {
2111 bp->bio_error = EAGAIN;
2112 bp->bio_flags |= BIO_ERROR;
2113 goto bad;
2114 }
2115 if (bp->bio_blkno < 0) {
2116 printf(
2117 "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
2118 fdu, (u_long)bp->bio_blkno, bp->bio_bcount);
2119 bp->bio_error = EINVAL;
2120 bp->bio_flags |= BIO_ERROR;
2121 goto bad;
2122 }
2123 if ((bp->bio_bcount % fdblk) != 0) {
2124 bp->bio_error = EINVAL;
2125 bp->bio_flags |= BIO_ERROR;
2126 goto bad;
2127 }
2128 }
2129
2130 /*
2131 * Set up block calculations.
2132 */
2133 if (bp->bio_blkno > 20000000) {
2134 /*
2135 * Reject unreasonably high block number, prevent the
2136 * multiplication below from overflowing.
2137 */
2138 bp->bio_error = EINVAL;
2139 bp->bio_flags |= BIO_ERROR;
2140 goto bad;
2141 }
2142 blknum = bp->bio_blkno * DEV_BSIZE / fdblk;
2143 nblocks = fd->ft->size;
2144 if (blknum + bp->bio_bcount / fdblk > nblocks) {
2145 if (blknum >= nblocks) {
2146 if (bp->bio_cmd == BIO_READ)
2147 bp->bio_resid = bp->bio_bcount;
2148 else {
2149 bp->bio_error = ENOSPC;
2150 bp->bio_flags |= BIO_ERROR;
2151 }
2152 goto bad; /* not always bad, but EOF */
2153 }
2154 bp->bio_bcount = (nblocks - blknum) * fdblk;
2155 }
2156 bp->bio_pblkno = blknum;
2157 s = splbio();
2158 bioqdisksort(&fdc->head, bp);
2159 untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
2160 devstat_start_transaction(&fd->device_stats);
2161 device_busy(fd->dev);
2162 fdstart(fdc);
2163 splx(s);
2164 return;
2165
2166bad:
2167 biodone(bp);
2168}
2169
2170/*
2171 * fdstart
2172 *
2173 * We have just queued something. If the controller is not busy
2174 * then simulate the case where it has just finished a command
2175 * So that it (the interrupt routine) looks on the queue for more
2176 * work to do and picks up what we just added.
2177 *
2178 * If the controller is already busy, we need do nothing, as it
2179 * will pick up our work when the present work completes.
2180 */
2181static void
2182fdstart(struct fdc_data *fdc)
2183{
2184 int s;
2185
2186 s = splbio();
2187 if(fdc->state == DEVIDLE)
2188 {
2189 fdc_intr(fdc);
2190 }
2191 splx(s);
2192}
2193
2194static void
2195fd_iotimeout(void *xfdc)
2196{
2197 fdc_p fdc;
2198 int s;
2199
2200 fdc = xfdc;
2201 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
2202
2203 /*
2204 * Due to IBM's brain-dead design, the FDC has a faked ready
2205 * signal, hardwired to ready == true. Thus, any command
2206 * issued if there's no diskette in the drive will _never_
2207 * complete, and must be aborted by resetting the FDC.
2208 * Many thanks, Big Blue!
2209 * The FDC must not be reset directly, since that would
2210 * interfere with the state machine. Instead, pretend that
2211 * the command completed but was invalid. The state machine
2212 * will reset the FDC and retry once.
2213 */
2214 s = splbio();
2215 fdc->status[0] = NE7_ST0_IC_IV;
2216 fdc->flags &= ~FDC_STAT_VALID;
2217 fdc->state = IOTIMEDOUT;
2218 fdc_intr(fdc);
2219 splx(s);
2220}
2221
2222/* Just ensure it has the right spl. */
2223static void
2224fd_pseudointr(void *xfdc)
2225{
2226 int s;
2227
2228 s = splbio();
2229 fdc_intr(xfdc);
2230 splx(s);
2231}
2232
2233/*
2234 * fdc_intr
2235 *
2236 * Keep calling the state machine until it returns a 0.
2237 * Always called at splbio.
2238 */
2239static void
2240fdc_intr(void *xfdc)
2241{
2242 fdc_p fdc = xfdc;
2243 while(fdstate(fdc))
2244 ;
2245}
2246
2247/*
2248 * Magic pseudo-DMA initialization for YE FDC. Sets count and
2249 * direction.
2250 */
2251#define SET_BCDR(fdc,wr,cnt,port) \
2252 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \
2253 ((cnt)-1) & 0xff); \
2254 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
2255 ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
2256
2257/*
2258 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy.
2259 */
2260static int
2261fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
2262{
2263 u_char *cptr = (u_char *)addr;
2264
2265 if (flags == BIO_READ) {
2266 if (fdc->state != PIOREAD) {
2267 fdc->state = PIOREAD;
2268 return(0);
2269 }
2270 SET_BCDR(fdc, 0, count, 0);
2271 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
2272 FDC_YE_DATAPORT, cptr, count);
2273 } else {
2274 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
2275 FDC_YE_DATAPORT, cptr, count);
2276 SET_BCDR(fdc, 0, count, 0);
2277 }
2278 return(1);
2279}
2280
2281/*
2282 * Try figuring out the density of the media present in our device.
2283 */
2284static int
2285fdautoselect(dev_t dev)
2286{
2287 fdu_t fdu;
2288 fd_p fd;
2289 struct fd_type *fdtp;
2290 struct fdc_readid id;
2291 int i, n, oopts, rv;
2292
2293 fdu = FDUNIT(minor(dev));
2294 fd = devclass_get_softc(fd_devclass, fdu);
2295
2296 switch (fd->type) {
2297 default:
2298 return (ENXIO);
2299
2300#ifndef PC98
2301 case FDT_360K:
2302 case FDT_720K:
2303 /* no autoselection on those drives */
2304 fd->ft = fd_native_types + fd->type;
2305 return (0);
2306#endif
2307
2308 case FDT_12M:
2309 fdtp = fd_searchlist_12m;
2310 n = sizeof fd_searchlist_12m / sizeof(struct fd_type);
2311 break;
2312
2313 case FDT_144M:
2314 fdtp = fd_searchlist_144m;
2315 n = sizeof fd_searchlist_144m / sizeof(struct fd_type);
2316 break;
2317
2318#ifndef PC98
2319 case FDT_288M:
2320 fdtp = fd_searchlist_288m;
2321 n = sizeof fd_searchlist_288m / sizeof(struct fd_type);
2322 break;
2323#endif
2324 }
2325
2326 /*
2327 * Try reading sector ID fields, first at cylinder 0, head 0,
2328 * then at cylinder 2, head N. We don't probe cylinder 1,
2329 * since for 5.25in DD media in a HD drive, there are no data
2330 * to read (2 step pulses per media cylinder required). For
2331 * two-sided media, the second probe always goes to head 1, so
2332 * we can tell them apart from single-sided media. As a
2333 * side-effect this means that single-sided media should be
2334 * mentioned in the search list after two-sided media of an
2335 * otherwise identical density. Media with a different number
2336 * of sectors per track but otherwise identical parameters
2337 * cannot be distinguished at all.
2338 *
2339 * If we successfully read an ID field on both cylinders where
2340 * the recorded values match our expectation, we are done.
2341 * Otherwise, we try the next density entry from the table.
2342 *
2343 * Stepping to cylinder 2 has the side-effect of clearing the
2344 * unit attention bit.
2345 */
2346 oopts = fd->options;
2347 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
2348 for (i = 0; i < n; i++, fdtp++) {
2349 fd->ft = fdtp;
2350
2351 id.cyl = id.head = 0;
2352 rv = fdmisccmd(dev, FDBIO_RDSECTID, &id);
2353 if (rv != 0)
2354 continue;
2355 if (id.cyl != 0 || id.head != 0 ||
2356 id.secshift != fdtp->secsize)
2357 continue;
2358 id.cyl = 2;
2359 id.head = fd->ft->heads - 1;
2360 rv = fdmisccmd(dev, FDBIO_RDSECTID, &id);
2361 if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
2362 id.secshift != fdtp->secsize)
2363 continue;
2364 if (rv == 0)
2365 break;
2366 }
2367
2368 fd->options = oopts;
2369 if (i == n) {
2370 if (bootverbose)
2371 device_printf(fd->dev, "autoselection failed\n");
2372 fd->ft = 0;
2373 return (EIO);
2374 } else {
2375 if (bootverbose)
2376 device_printf(fd->dev, "autoselected %d KB medium\n",
2377 fd->ft->size / 2);
2378 return (0);
2379 }
2380}
2381
2382
2383/*
2384 * The controller state machine.
2385 *
2386 * If it returns a non zero value, it should be called again immediately.
2387 */
2388static int
2389fdstate(fdc_p fdc)
2390{
2391 struct fdc_readid *idp;
2392 int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac;
2393 int st0, cyl, st3, idf, ne7cmd, mfm, steptrac;
2394 unsigned long blknum;
2395 fdu_t fdu = fdc->fdu;
2396 fd_p fd;
2397 register struct bio *bp;
2398 struct fd_formb *finfo = NULL;
2399 size_t fdblk;
2400
2401 bp = fdc->bp;
2402 if (bp == NULL) {
2403 bp = bioq_first(&fdc->head);
2404 if (bp != NULL) {
2405 bioq_remove(&fdc->head, bp);
2406 fdc->bp = bp;
2407 }
2408 }
2409 if (bp == NULL) {
2410 /*
2411 * Nothing left for this controller to do,
2412 * force into the IDLE state.
2413 */
2414 fdc->state = DEVIDLE;
2415 if (fdc->fd) {
2416 device_printf(fdc->fdc_dev,
2417 "unexpected valid fd pointer\n");
2418 fdc->fd = (fd_p) 0;
2419 fdc->fdu = -1;
2420 }
2421 TRACE1("[fdc%d IDLE]", fdc->fdcu);
2422 return (0);
2423 }
2424 fdu = FDUNIT(minor(bp->bio_dev));
2425 fd = devclass_get_softc(fd_devclass, fdu);
2426 fdblk = 128 << fd->ft->secsize;
2427 if (fdc->fd && (fd != fdc->fd))
2428 device_printf(fd->dev, "confused fd pointers\n");
2429 read = bp->bio_cmd == BIO_READ;
2430 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
2431 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
2432 if (read)
2433 idf = ISADMA_READ;
2434 else
2435 idf = ISADMA_WRITE;
2436 format = bp->bio_cmd == FDBIO_FORMAT;
2437 rdsectid = bp->bio_cmd == FDBIO_RDSECTID;
2438 if (format)
2439 finfo = (struct fd_formb *)bp->bio_data;
2440 TRACE1("fd%d", fdu);
2441 TRACE1("[%s]", fdstates[fdc->state]);
2442 TRACE1("(0x%x)", fd->flags);
2443 untimeout(fd_turnoff, fd, fd->toffhandle);
2444 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
2445 switch (fdc->state)
2446 {
2447 case DEVIDLE:
2448 case FINDWORK: /* we have found new work */
2449 fdc->retry = 0;
2450 fd->skip = 0;
2451 fdc->fd = fd;
2452 fdc->fdu = fdu;
2453#ifdef PC98
2454 pc98_trans = fd->ft->trans;
2455 if (pc98_trans_prev != pc98_trans) {
2456 int i;
2457 set_density(fdc);
2458 for (i = 0; i < 10; i++) {
2459 outb(0x5f, 0);
2460 outb(0x5f, 0);
2461 }
2462 pc98_trans_prev = pc98_trans;
2463 }
2464 if (pc98_trans != fd->pc98_trans) {
2465 if (fd->type == FDT_144M) {
2466 bus_space_write_1(fdc->sc_fdemsiot,
2467 fdc->sc_fdemsioh,
2468 0,
2469 (fdu << 5) | 0x10 |
2470 (pc98_trans >> 1));
2471 outb(0x5f, 0);
2472 outb(0x5f, 0);
2473 }
2474 fd->pc98_trans = pc98_trans;
2475 }
2476#else
2477 fdc->fdctl_wr(fdc, fd->ft->trans);
2478#endif
2479 TRACE1("[0x%x->FDCTL]", fd->ft->trans);
2480 /*
2481 * If the next drive has a motor startup pending, then
2482 * it will start up in its own good time.
2483 */
2484 if(fd->flags & FD_MOTOR_WAIT) {
2485 fdc->state = MOTORWAIT;
2486 return (0); /* will return later */
2487 }
2488 /*
2489 * Maybe if it's not starting, it SHOULD be starting.
2490 */
2491#ifdef EPSON_NRDISK
2492 if (fdu != nrdu) {
2493 if (!(fd->flags & FD_MOTOR))
2494 {
2495 fdc->state = MOTORWAIT;
2496 fd_turnon(fdu);
2497 return(0);
2498 }
2499 else /* at least make sure we are selected */
2500 {
2501 set_motor(fdcu, fd->fdsu, TURNON);
2502 }
2503 }
2504#else /* !EPSON_NRDISK */
2505 if (!(fd->flags & FD_MOTOR))
2506 {
2507 fdc->state = MOTORWAIT;
2508 fd_turnon(fd);
2509 return (0); /* will return later */
2510 }
2511 else /* at least make sure we are selected */
2512 {
2513 set_motor(fdc, fd->fdsu, TURNON);
2514 }
2515#endif
2516 if (fdc->flags & FDC_NEEDS_RESET) {
2517 fdc->state = RESETCTLR;
2518 fdc->flags &= ~FDC_NEEDS_RESET;
2519 } else
2520 fdc->state = DOSEEK;
2521 return (1); /* will return immediately */
2522
2523 case DOSEEK:
2524#ifdef PC98
2525 blknum = bp->bio_pblkno * DEV_BSIZE / fdblk + fd->skip / fdblk;
2526#else
2527 blknum = bp->bio_pblkno + fd->skip / fdblk;
2528#endif
2529 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2530 if (cylinder == fd->track)
2531 {
2532 fdc->state = SEEKCOMPLETE;
2533 return (1); /* will return immediately */
2534 }
2535#ifdef PC98
2536 pc98_fd_check_ready(fdu);
2537#endif
2538 if (fd_cmd(fdc, 3, NE7CMD_SEEK,
2539 fd->fdsu, cylinder * steptrac, 0))
2540 {
2541 /*
2542 * Seek command not accepted, looks like
2543 * the FDC went off to the Saints...
2544 */
2545 fdc->retry = 6; /* try a reset */
2546 return(retrier(fdc));
2547 }
2548 fd->track = FD_NO_TRACK;
2549 fdc->state = SEEKWAIT;
2550 return(0); /* will return later */
2551
2552 case SEEKWAIT:
2553 /* allow heads to settle */
2554 timeout(fd_pseudointr, fdc, hz / 16);
2555 fdc->state = SEEKCOMPLETE;
2556 return(0); /* will return later */
2557
2558 case SEEKCOMPLETE : /* seek done, start DMA */
2559#ifdef PC98
2560 blknum = bp->bio_pblkno * DEV_BSIZE / fdblk + fd->skip / fdblk;
2561#else
2562 blknum = bp->bio_pblkno + fd->skip / fdblk;
2563#endif
2564 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2565
2566 /* Make sure seek really happened. */
2567 if(fd->track == FD_NO_TRACK) {
2568 int descyl = cylinder * steptrac;
2569 do {
2570 /*
2571 * This might be a "ready changed" interrupt,
2572 * which cannot really happen since the
2573 * RDY pin is hardwired to + 5 volts. This
2574 * generally indicates a "bouncing" intr
2575 * line, so do one of the following:
2576 *
2577 * When running on an enhanced FDC that is
2578 * known to not go stuck after responding
2579 * with INVALID, fetch all interrupt states
2580 * until seeing either an INVALID or a
2581 * real interrupt condition.
2582 *
2583 * When running on a dumb old NE765, give
2584 * up immediately. The controller will
2585 * provide up to four dummy RC interrupt
2586 * conditions right after reset (for the
2587 * corresponding four drives), so this is
2588 * our only chance to get notice that it
2589 * was not the FDC that caused the interrupt.
2590 */
2591 if (fd_sense_int(fdc, &st0, &cyl)
2592 == FD_NOT_VALID)
2593 return (0); /* will return later */
2594 if(fdc->fdct == FDC_NE765
2595 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2596 return (0); /* hope for a real intr */
2597 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2598
2599 if (0 == descyl) {
2600 int failed = 0;
2601 /*
2602 * seek to cyl 0 requested; make sure we are
2603 * really there
2604 */
2605 if (fd_sense_drive_status(fdc, &st3))
2606 failed = 1;
2607#ifdef EPSON_NRDISK
2608 if (fdu == nrdu) st3 = NE7_ST3_T0;
2609#endif /* EPSON_NRDISK */
2610 if ((st3 & NE7_ST3_T0) == 0) {
2611 printf(
2612 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
2613 fdu, st3, NE7_ST3BITS);
2614 failed = 1;
2615 }
2616
2617 if (failed) {
2618 if(fdc->retry < 3)
2619 fdc->retry = 3;
2620 return (retrier(fdc));
2621 }
2622 }
2623#ifdef EPSON_NRDISK
2624 if (fdu == nrdu) cyl = descyl;
2625#endif
2626
2627 if (cyl != descyl) {
2628 printf(
2629 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
2630 fdu, descyl, cyl, st0);
2631 if (fdc->retry < 3)
2632 fdc->retry = 3;
2633 return (retrier(fdc));
2634 }
2635 }
2636
2637 fd->track = cylinder;
2638 if (format)
2639 fd->skip = (char *)&(finfo->fd_formb_cylno(0))
2640 - (char *)finfo;
2641#ifdef EPSON_NRDISK
2642 if (fdu != nrdu) {
2643#endif /* EPSON_NRDISK */
2644 if (!rdsectid && !(fdc->flags & FDC_NODMA))
2645 isa_dmastart(idf, bp->bio_data+fd->skip,
2646 format ? bp->bio_bcount : fdblk, fdc->dmachan);
2647#ifdef PC98
2648 blknum = bp->bio_pblkno * DEV_BSIZE / fdblk + fd->skip / fdblk;
2649#else
2650 blknum = bp->bio_pblkno + fd->skip / fdblk;
2651#endif
2652 sectrac = fd->ft->sectrac;
2653 sec = blknum % (sectrac * fd->ft->heads);
2654 head = sec / sectrac;
2655 sec = sec % sectrac + 1;
2656 if (head != 0 && fd->ft->offset_side2 != 0)
2657 sec += fd->ft->offset_side2;
2658 fd->hddrv = ((head&1)<<2)+fdu;
2659
2660 if(format || !(read || rdsectid))
2661 {
2662 /* make sure the drive is writable */
2663 if(fd_sense_drive_status(fdc, &st3) != 0)
2664 {
2665 /* stuck controller? */
2666 if (!(fdc->flags & FDC_NODMA))
2667 isa_dmadone(idf,
2668 bp->bio_data + fd->skip,
2669 format ? bp->bio_bcount : fdblk,
2670 fdc->dmachan);
2671 fdc->retry = 6; /* reset the beast */
2672 return (retrier(fdc));
2673 }
2674 if(st3 & NE7_ST3_WP)
2675 {
2676 /*
2677 * XXX YES! this is ugly.
2678 * in order to force the current operation
2679 * to fail, we will have to fake an FDC
2680 * error - all error handling is done
2681 * by the retrier()
2682 */
2683 fdc->status[0] = NE7_ST0_IC_AT;
2684 fdc->status[1] = NE7_ST1_NW;
2685 fdc->status[2] = 0;
2686 fdc->status[3] = fd->track;
2687 fdc->status[4] = head;
2688 fdc->status[5] = sec;
2689 fdc->retry = 8; /* break out immediately */
2690 fdc->state = IOTIMEDOUT; /* not really... */
2691 return (1); /* will return immediately */
2692 }
2693 }
2694
2695 if (format) {
2696 ne7cmd = NE7CMD_FORMAT | mfm;
2697 if (fdc->flags & FDC_NODMA) {
2698 /*
2699 * This seems to be necessary for
2700 * whatever obscure reason; if we omit
2701 * it, we end up filling the sector ID
2702 * fields of the newly formatted track
2703 * entirely with garbage, causing
2704 * `wrong cylinder' errors all over
2705 * the place when trying to read them
2706 * back.
2707 *
2708 * Umpf.
2709 */
2710 SET_BCDR(fdc, 1, bp->bio_bcount, 0);
2711
2712 (void)fdcpio(fdc,bp->bio_cmd,
2713 bp->bio_data+fd->skip,
2714 bp->bio_bcount);
2715
2716 }
2717 /* formatting */
2718 if(fd_cmd(fdc, 6, ne7cmd, head << 2 | fdu,
2719 finfo->fd_formb_secshift,
2720 finfo->fd_formb_nsecs,
2721 finfo->fd_formb_gaplen,
2722 finfo->fd_formb_fillbyte, 0)) {
2723 /* controller fell over */
2724 if (!(fdc->flags & FDC_NODMA))
2725 isa_dmadone(idf,
2726 bp->bio_data + fd->skip,
2727 format ? bp->bio_bcount : fdblk,
2728 fdc->dmachan);
2729 fdc->retry = 6;
2730 return (retrier(fdc));
2731 }
2732 } else if (rdsectid) {
2733 ne7cmd = NE7CMD_READID | mfm;
2734 if (fd_cmd(fdc, 2, ne7cmd, head << 2 | fdu, 0)) {
2735 /* controller jamming */
2736 fdc->retry = 6;
2737 return (retrier(fdc));
2738 }
2739 } else {
2740 /* read or write operation */
2741 ne7cmd = (read ? NE7CMD_READ | NE7CMD_SK : NE7CMD_WRITE) | mfm;
2742 if (fdc->flags & FDC_NODMA) {
2743 /*
2744 * This seems to be necessary even when
2745 * reading data.
2746 */
2747 SET_BCDR(fdc, 1, fdblk, 0);
2748
2749 /*
2750 * Perform the write pseudo-DMA before
2751 * the WRITE command is sent.
2752 */
2753 if (!read)
2754 (void)fdcpio(fdc,bp->bio_cmd,
2755 bp->bio_data+fd->skip,
2756 fdblk);
2757 }
2758 if (fd_cmd(fdc, 9,
2759 ne7cmd,
2760 head << 2 | fdu, /* head & unit */
2761 fd->track, /* track */
2762 head,
2763 sec, /* sector + 1 */
2764 fd->ft->secsize, /* sector size */
2765 sectrac, /* sectors/track */
2766 fd->ft->gap, /* gap size */
2767 fd->ft->datalen, /* data length */
2768 0)) {
2769 /* the beast is sleeping again */
2770 if (!(fdc->flags & FDC_NODMA))
2771 isa_dmadone(idf,
2772 bp->bio_data + fd->skip,
2773 format ? bp->bio_bcount : fdblk,
2774 fdc->dmachan);
2775 fdc->retry = 6;
2776 return (retrier(fdc));
2777 }
2778 }
2779 if (!rdsectid && (fdc->flags & FDC_NODMA))
2780 /*
2781 * If this is a read, then simply await interrupt
2782 * before performing PIO.
2783 */
2784 if (read && !fdcpio(fdc,bp->bio_cmd,
2785 bp->bio_data+fd->skip,fdblk)) {
2786 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
2787 return(0); /* will return later */
2788 }
2789
2790 /*
2791 * Write (or format) operation will fall through and
2792 * await completion interrupt.
2793 */
2794 fdc->state = IOCOMPLETE;
2795 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
2796 return (0); /* will return later */
2797#ifdef EPSON_NRDISK
2798 }
2799 else {
2800 nrdblkn = (nrd_t)((unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
2801 + fd->skip/fdblk);
2802 nrd_LED_on();
2803 nrd_addrset(fdblk * nrdblkn);
2804 while (!nrd_check_ready()) DELAY(1);
2805 if (read) epson_insw(P_NRD_DATA,
2806 bp->bio_data + fd->skip,
2807 fdblk / sizeof(short));
2808 else epson_outsw(P_NRD_DATA,
2809 bp->bio_data + fd->skip,
2810 (format ? bp->bio_bcount : fdblk)
2811 / sizeof(short));
2812
2813 blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
2814 + fd->skip/fdblk;
2815 sectrac = fd->ft->sectrac;
2816 sec = blknum % (sectrac * fd->ft->heads);
2817 head = sec / sectrac;
2818 sec = sec % sectrac + 1;
2819 fd->hddrv = ((head&1)<<2)+fdu;
2820
2821 if (nrdsec++ >= nrd_sec())
2822 nrdaddr = (nrd_t)(fd->track * 8 + head * 4);
2823 nrdsec = sec;
2824 fdc->state = IOCOMPLETE;
2825 }
2826#endif
2827
2828 case PIOREAD:
2829 /*
2830 * Actually perform the PIO read. The IOCOMPLETE case
2831 * removes the timeout for us.
2832 */
2833 (void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
2834 fdc->state = IOCOMPLETE;
2835 /* FALLTHROUGH */
2836 case IOCOMPLETE: /* IO done, post-analyze */
2837#ifdef EPSON_NRDISK
2838 if (fdu != nrdu)
2839 untimeout(fd_iotimeout, fdc, fd->tohandle);
2840#else
2841 untimeout(fd_iotimeout, fdc, fd->tohandle);
2842#endif
2843
2844 if (fd_read_status(fdc)) {
2845 if (!rdsectid && !(fdc->flags & FDC_NODMA))
2846 isa_dmadone(idf, bp->bio_data + fd->skip,
2847 format ? bp->bio_bcount : fdblk,
2848 fdc->dmachan);
2849 if (fdc->retry < 6)
2850 fdc->retry = 6; /* force a reset */
2851 return (retrier(fdc));
2852 }
2853
2854 fdc->state = IOTIMEDOUT;
2855
2856 /* FALLTHROUGH */
2857 case IOTIMEDOUT:
2858#ifdef EPSON_NRDISK
2859 if (fdu != nrdu) {
2860#endif /* EPSON_NRDISK */
2861 if (!rdsectid && !(fdc->flags & FDC_NODMA))
2862 isa_dmadone(idf, bp->bio_data + fd->skip,
2863 format ? bp->bio_bcount : fdblk, fdc->dmachan);
2864#ifdef EPSON_NRDISK
2865 }
2866 else nrd_LED_off();
2867#endif /* EPSON_NRDISK */
2868 if (fdc->status[0] & NE7_ST0_IC) {
2869 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2870 && fdc->status[1] & NE7_ST1_OR) {
2871 /*
2872 * DMA overrun. Someone hogged the bus and
2873 * didn't release it in time for the next
2874 * FDC transfer.
2875 *
2876 * We normally restart this without bumping
2877 * the retry counter. However, in case
2878 * something is seriously messed up (like
2879 * broken hardware), we rather limit the
2880 * number of retries so the IO operation
2881 * doesn't block indefinately.
2882 */
2883 if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2884 fdc->state = SEEKCOMPLETE;
2885 return (1);/* will return immediately */
2886 } /* else fall through */
2887 }
2888 if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
2889 && fdc->retry < 6)
2890 fdc->retry = 6; /* force a reset */
2891 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2892 && fdc->status[2] & NE7_ST2_WC
2893 && fdc->retry < 3)
2894 fdc->retry = 3; /* force recalibrate */
2895 return (retrier(fdc));
2896 }
2897 /* All OK */
2898 if (rdsectid) {
2899 /* copy out ID field contents */
2900 idp = (struct fdc_readid *)bp->bio_data;
2901 idp->cyl = fdc->status[3];
2902 idp->head = fdc->status[4];
2903 idp->sec = fdc->status[5];
2904 idp->secshift = fdc->status[6];
2905 }
2906 /* Operation successful, retry DMA overruns again next time. */
2907 fdc->dma_overruns = 0;
2908 fd->skip += fdblk;
2909 if (!rdsectid && !format && fd->skip < bp->bio_bcount) {
2910 /* set up next transfer */
2911 fdc->state = DOSEEK;
2912 } else {
2913 /* ALL DONE */
2914 fd->skip = 0;
2915 bp->bio_resid = 0;
2916 fdc->bp = NULL;
2917 device_unbusy(fd->dev);
2918 biofinish(bp, &fd->device_stats, 0);
2919 fdc->fd = (fd_p) 0;
2920 fdc->fdu = -1;
2921 fdc->state = FINDWORK;
2922 }
2923 return (1); /* will return immediately */
2924
2925 case RESETCTLR:
2926 fdc_reset(fdc);
2927 fdc->retry++;
2928 fdc->state = RESETCOMPLETE;
2929 return (0); /* will return later */
2930
2931 case RESETCOMPLETE:
2932 /*
2933 * Discard all the results from the reset so that they
2934 * can't cause an unexpected interrupt later.
2935 */
2936 for (i = 0; i < 4; i++)
2937 (void)fd_sense_int(fdc, &st0, &cyl);
2938 fdc->state = STARTRECAL;
2939 /* FALLTHROUGH */
2940 case STARTRECAL:
2941#ifdef PC98
2942 pc98_fd_check_ready(fdu);
2943#endif
2944 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2945 /* arrgl */
2946 fdc->retry = 6;
2947 return (retrier(fdc));
2948 }
2949 fdc->state = RECALWAIT;
2950 return (0); /* will return later */
2951
2952 case RECALWAIT:
2953 /* allow heads to settle */
2954 timeout(fd_pseudointr, fdc, hz / 8);
2955 fdc->state = RECALCOMPLETE;
2956 return (0); /* will return later */
2957
2958 case RECALCOMPLETE:
2959 do {
2960 /*
2961 * See SEEKCOMPLETE for a comment on this:
2962 */
2963 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2964 return (0); /* will return later */
2965 if(fdc->fdct == FDC_NE765
2966 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2967 return (0); /* hope for a real intr */
2968 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2969#ifdef EPSON_NRDISK
2970 if (fdu == nrdu) {
2971 st0 = NE7_ST0_IC_NT;
2972 cyl = 0;
2973 }
2974#endif
2975 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
2976 {
2977 if(fdc->retry > 3)
2978 /*
2979 * A recalibrate from beyond cylinder 77
2980 * will "fail" due to the FDC limitations;
2981 * since people used to complain much about
2982 * the failure message, try not logging
2983 * this one if it seems to be the first
2984 * time in a line.
2985 */
2986 printf("fd%d: recal failed ST0 %b cyl %d\n",
2987 fdu, st0, NE7_ST0BITS, cyl);
2988 if(fdc->retry < 3) fdc->retry = 3;
2989 return (retrier(fdc));
2990 }
2991 fd->track = 0;
2992 /* Seek (probably) necessary */
2993 fdc->state = DOSEEK;
2994 return (1); /* will return immediately */
2995
2996 case MOTORWAIT:
2997 if(fd->flags & FD_MOTOR_WAIT)
2998 {
2999 return (0); /* time's not up yet */
3000 }
3001 if (fdc->flags & FDC_NEEDS_RESET) {
3002 fdc->state = RESETCTLR;
3003 fdc->flags &= ~FDC_NEEDS_RESET;
3004 } else
3005 fdc->state = DOSEEK;
3006 return (1); /* will return immediately */
3007
3008 default:
3009 device_printf(fdc->fdc_dev, "unexpected FD int->");
3010 if (fd_read_status(fdc) == 0)
3011 printf("FDC status :%x %x %x %x %x %x %x ",
3012 fdc->status[0],
3013 fdc->status[1],
3014 fdc->status[2],
3015 fdc->status[3],
3016 fdc->status[4],
3017 fdc->status[5],
3018 fdc->status[6] );
3019 else
3020 printf("No status available ");
3021 if (fd_sense_int(fdc, &st0, &cyl) != 0)
3022 {
3023 printf("[controller is dead now]\n");
3024 return (0); /* will return later */
3025 }
3026 printf("ST0 = %x, PCN = %x\n", st0, cyl);
3027 return (0); /* will return later */
3028 }
3029 /* noone should ever get here */
3030}
3031
3032static int
3033retrier(struct fdc_data *fdc)
3034{
3035 struct bio *bp;
3036 struct fd_data *fd;
3037 int fdu;
3038
3039 bp = fdc->bp;
3040
3041 /* XXX shouldn't this be cached somewhere? */
3042 fdu = FDUNIT(minor(bp->bio_dev));
3043 fd = devclass_get_softc(fd_devclass, fdu);
3044 if (fd->options & FDOPT_NORETRY)
3045 goto fail;
3046
3047 switch (fdc->retry) {
3048 case 0: case 1: case 2:
3049 fdc->state = SEEKCOMPLETE;
3050 break;
3051 case 3: case 4: case 5:
3052 fdc->state = STARTRECAL;
3053 break;
3054 case 6:
3055 fdc->state = RESETCTLR;
3056 break;
3057 case 7:
3058 break;
3059 default:
3060 fail:
3061 if ((fd->options & FDOPT_NOERRLOG) == 0) {
3062 disk_err(bp, "hard error",
3063 fdc->fd->skip / DEV_BSIZE, 0);
3064 if (fdc->flags & FDC_STAT_VALID) {
3065 printf(
3066 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
3067 fdc->status[0], NE7_ST0BITS,
3068 fdc->status[1], NE7_ST1BITS,
3069 fdc->status[2], NE7_ST2BITS,
3070 fdc->status[3], fdc->status[4],
3071 fdc->status[5]);
3072 }
3073 else
3074 printf(" (No status)\n");
3075 }
3076 if ((fd->options & FDOPT_NOERROR) == 0) {
3077 bp->bio_flags |= BIO_ERROR;
3078 bp->bio_error = EIO;
3079 bp->bio_resid = bp->bio_bcount - fdc->fd->skip;
3080 } else
3081 bp->bio_resid = 0;
3082 fdc->bp = NULL;
3083 fdc->fd->skip = 0;
3084 device_unbusy(fd->dev);
3085 biofinish(bp, &fdc->fd->device_stats, 0);
3086 fdc->state = FINDWORK;
3087 fdc->flags |= FDC_NEEDS_RESET;
3088 fdc->fd = (fd_p) 0;
3089 fdc->fdu = -1;
3090 return (1);
3091 }
3092 fdc->retry++;
3093 return (1);
3094}
3095
3096static void
3097fdbiodone(struct bio *bp)
3098{
3099 wakeup(bp);
3100}
3101
3102static int
3103fdmisccmd(dev_t dev, u_int cmd, void *data)
3104{
3105 fdu_t fdu;
3106 fd_p fd;
3107 struct bio *bp;
3108 struct fd_formb *finfo;
3109 struct fdc_readid *idfield;
3110 size_t fdblk;
3111 int error;
3112
3113 fdu = FDUNIT(minor(dev));
3114 fd = devclass_get_softc(fd_devclass, fdu);
3115 fdblk = 128 << fd->ft->secsize;
3116 finfo = (struct fd_formb *)data;
3117 idfield = (struct fdc_readid *)data;
3118
3119 bp = malloc(sizeof(struct bio), M_TEMP, M_ZERO);
3120
3121 /*
3122 * Set up a bio request for fdstrategy(). bio_blkno is faked
3123 * so that fdstrategy() will seek to the the requested
3124 * cylinder, and use the desired head.
3125 */
3126 bp->bio_cmd = cmd;
3127 if (cmd == FDBIO_FORMAT) {
3128 bp->bio_blkno =
3129 (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) +
3130 finfo->head * fd->ft->sectrac) *
3131 fdblk / DEV_BSIZE;
3132 bp->bio_bcount = sizeof(struct fd_idfield_data) *
3133 finfo->fd_formb_nsecs;
3134 } else if (cmd == FDBIO_RDSECTID) {
3135 bp->bio_blkno =
3136 (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) +
3137 idfield->head * fd->ft->sectrac) *
3138 fdblk / DEV_BSIZE;
3139 bp->bio_bcount = sizeof(struct fdc_readid);
3140 } else
3141 panic("wrong cmd in fdmisccmd()");
3142 bp->bio_data = data;
3143 bp->bio_dev = dev;
3144 bp->bio_done = fdbiodone;
3145 bp->bio_flags = 0;
3146
3147 /* Now run the command. */
3148 fdstrategy(bp);
3149 error = biowait(bp, "fdcmd");
3150
3151 free(bp, M_TEMP);
3152 return (error);
3153}
3154
3155static int
3156fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
3157{
3158 fdu_t fdu;
3159 fd_p fd;
3160 struct fdc_status *fsp;
3161 struct fdc_readid *rid;
3162 size_t fdblk;
3163 int error, type;
3164
3165 fdu = FDUNIT(minor(dev));
3166 type = FDTYPE(minor(dev));
3167 fd = devclass_get_softc(fd_devclass, fdu);
3168
3169#ifdef PC98
3170 pc98_fd_check_ready(fdu);
3171#endif
3172
3173 /*
3174 * First, handle everything that could be done with
3175 * FD_NONBLOCK still being set.
3176 */
3177 switch (cmd) {
3178
3179 case DIOCGMEDIASIZE:
3180 *(off_t *)addr = (128 << (fd->ft->secsize)) * fd->ft->size;
3181 return (0);
3182
3183 case DIOCGSECTORSIZE:
3184 *(u_int *)addr = 128 << (fd->ft->secsize);
3185 return (0);
3186
3187 case FIONBIO:
3188 if (*(int *)addr != 0)
3189 fd->flags |= FD_NONBLOCK;
3190 else {
3191 if (fd->ft == 0) {
3192 /*
3193 * No drive type has been selected yet,
3194 * cannot turn FNONBLOCK off.
3195 */
3196 return (EINVAL);
3197 }
3198 fd->flags &= ~FD_NONBLOCK;
3199 }
3200 return (0);
3201
3202 case FIOASYNC:
3203 /* keep the generic fcntl() code happy */
3204 return (0);
3205
3206 case FD_GTYPE: /* get drive type */
3207 if (fd->ft == 0)
3208 /* no type known yet, return the native type */
3209 *(struct fd_type *)addr = fd_native_types[fd->type];
3210 else
3211 *(struct fd_type *)addr = *fd->ft;
3212 return (0);
3213
3214 case FD_STYPE: /* set drive type */
3215 if (type == 0) {
3216 /*
3217 * Allow setting drive type temporarily iff
3218 * currently unset. Used for fdformat so any
3219 * user can set it, and then start formatting.
3220 */
3221 if (fd->ft)
3222 return (EINVAL); /* already set */
3223 fd->ft = fd->fts;
3224 *fd->ft = *(struct fd_type *)addr;
3225 fd->flags |= FD_UA;
3226 } else {
3227 /*
3228 * Set density definition permanently. Only
3229 * allow for superuser.
3230 */
3231 if (suser(td) != 0)
3232 return (EPERM);
3233 fd->fts[type] = *(struct fd_type *)addr;
3234 }
3235 return (0);
3236
3237 case FD_GOPTS: /* get drive options */
3238 *(int *)addr = fd->options + (type == 0? FDOPT_AUTOSEL: 0);
3239 return (0);
3240
3241 case FD_SOPTS: /* set drive options */
3242 fd->options = *(int *)addr & ~FDOPT_AUTOSEL;
3243 return (0);
3244
3245#ifdef FDC_DEBUG
3246 case FD_DEBUG:
3247 if ((fd_debug != 0) != (*(int *)addr != 0)) {
3248 fd_debug = (*(int *)addr != 0);
3249 printf("fd%d: debugging turned %s\n",
3250 fd->fdu, fd_debug ? "on" : "off");
3251 }
3252 return (0);
3253#endif
3254
3255 case FD_CLRERR:
3256 if (suser(td) != 0)
3257 return (EPERM);
3258 fd->fdc->fdc_errs = 0;
3259 return (0);
3260
3261 case FD_GSTAT:
3262 fsp = (struct fdc_status *)addr;
3263 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
3264 return (EINVAL);
3265 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
3266 return (0);
3267
3268 case FD_GDTYPE:
3269 *(enum fd_drivetype *)addr = fd->type;
3270 return (0);
3271 }
3272
3273 /*
3274 * Now handle everything else. Make sure we have a valid
3275 * drive type.
3276 */
3277 if (fd->flags & FD_NONBLOCK)
3278 return (EAGAIN);
3279 if (fd->ft == 0)
3280 return (ENXIO);
3281 fdblk = 128 << fd->ft->secsize;
3282 error = 0;
3283
3284 switch (cmd) {
3285
3286 case FD_FORM:
3287 if ((flag & FWRITE) == 0)
3288 return (EBADF); /* must be opened for writing */
3289 if (((struct fd_formb *)addr)->format_version !=
3290 FD_FORMAT_VERSION)
3291 return (EINVAL); /* wrong version of formatting prog */
3292 error = fdmisccmd(dev, FDBIO_FORMAT, addr);
3293 break;
3294
3295 case FD_GTYPE: /* get drive type */
3296 *(struct fd_type *)addr = *fd->ft;
3297 break;
3298
3299 case FD_STYPE: /* set drive type */
3300 /* this is considered harmful; only allow for superuser */
3301 if (suser(td) != 0)
3302 return (EPERM);
3303 *fd->ft = *(struct fd_type *)addr;
3304 break;
3305
3306 case FD_GOPTS: /* get drive options */
3307 *(int *)addr = fd->options;
3308 break;
3309
3310 case FD_SOPTS: /* set drive options */
3311 fd->options = *(int *)addr;
3312 break;
3313
3314#ifdef FDC_DEBUG
3315 case FD_DEBUG:
3316 if ((fd_debug != 0) != (*(int *)addr != 0)) {
3317 fd_debug = (*(int *)addr != 0);
3318 printf("fd%d: debugging turned %s\n",
3319 fd->fdu, fd_debug ? "on" : "off");
3320 }
3321 break;
3322#endif
3323
3324 case FD_CLRERR:
3325 if (suser(td) != 0)
3326 return (EPERM);
3327 fd->fdc->fdc_errs = 0;
3328 break;
3329
3330 case FD_GSTAT:
3331 fsp = (struct fdc_status *)addr;
3332 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
3333 return (EINVAL);
3334 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
3335 break;
3336
3337 case FD_READID:
3338 rid = (struct fdc_readid *)addr;
3339 if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD)
3340 return (EINVAL);
3341 error = fdmisccmd(dev, FDBIO_RDSECTID, addr);
3342 break;
3343
3344 default:
3345 error = ENOTTY;
3346 break;
3347 }
3348 return (error);
3349}
632};
633
634/*
635 * Auxiliary functions. Well, some only. Others are scattered
636 * throughout the entire file.
637 */
638static int
639fdc_err(struct fdc_data *fdc, const char *s)
640{
641 fdc->fdc_errs++;
642 if (s) {
643 if (fdc->fdc_errs < FDC_ERRMAX)
644 device_printf(fdc->fdc_dev, "%s", s);
645 else if (fdc->fdc_errs == FDC_ERRMAX)
646 device_printf(fdc->fdc_dev, "too many errors, not "
647 "logging any more\n");
648 }
649
650 return FD_FAILED;
651}
652
653/*
654 * fd_cmd: Send a command to the chip. Takes a varargs with this structure:
655 * Unit number,
656 * # of output bytes, output bytes as ints ...,
657 * # of input bytes, input bytes as ints ...
658 */
659static int
660fd_cmd(struct fdc_data *fdc, int n_out, ...)
661{
662 u_char cmd;
663 int n_in;
664 int n;
665 va_list ap;
666
667 va_start(ap, n_out);
668 cmd = (u_char)(va_arg(ap, int));
669 va_end(ap);
670 va_start(ap, n_out);
671 for (n = 0; n < n_out; n++)
672 {
673 if (out_fdc(fdc, va_arg(ap, int)) < 0)
674 {
675 char msg[50];
676 snprintf(msg, sizeof(msg),
677 "cmd %x failed at out byte %d of %d\n",
678 cmd, n + 1, n_out);
679 return fdc_err(fdc, msg);
680 }
681 }
682 n_in = va_arg(ap, int);
683 for (n = 0; n < n_in; n++)
684 {
685 int *ptr = va_arg(ap, int *);
686 if (fd_in(fdc, ptr) < 0)
687 {
688 char msg[50];
689 snprintf(msg, sizeof(msg),
690 "cmd %02x failed at in byte %d of %d\n",
691 cmd, n + 1, n_in);
692 return fdc_err(fdc, msg);
693 }
694 }
695
696 return 0;
697}
698
699static int
700enable_fifo(fdc_p fdc)
701{
702 int i, j;
703
704 if ((fdc->flags & FDC_HAS_FIFO) == 0) {
705
706 /*
707 * Cannot use fd_cmd the normal way here, since
708 * this might be an invalid command. Thus we send the
709 * first byte, and check for an early turn of data directon.
710 */
711
712 if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
713 return fdc_err(fdc, "Enable FIFO failed\n");
714
715 /* If command is invalid, return */
716 j = FDSTS_TIMEOUT;
717 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
718 != NE7_RQM && j-- > 0) {
719 if (i == (NE7_DIO | NE7_RQM)) {
720 fdc_reset(fdc);
721 return FD_FAILED;
722 }
723 DELAY(1);
724 }
725 if (j<0 ||
726 fd_cmd(fdc, 3,
727 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
728 fdc_reset(fdc);
729 return fdc_err(fdc, "Enable FIFO failed\n");
730 }
731 fdc->flags |= FDC_HAS_FIFO;
732 return 0;
733 }
734 if (fd_cmd(fdc, 4,
735 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
736 return fdc_err(fdc, "Re-enable FIFO failed\n");
737 return 0;
738}
739
740static int
741fd_sense_drive_status(fdc_p fdc, int *st3p)
742{
743 int st3;
744
745 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
746 {
747 return fdc_err(fdc, "Sense Drive Status failed\n");
748 }
749 if (st3p)
750 *st3p = st3;
751
752 return 0;
753}
754
755static int
756fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
757{
758 int cyl, st0, ret;
759
760#ifdef EPSON_NRDISK
761 if (fdc->fdu == nrdu) {
762 if (fdc->fd->track >= 0) nrdaddr = (fdc->fd->track + 1) * 8;
763 else nrdaddr = 0x0;
764 *st0p = nrd_head() ? NRD_ST0_HD : NRD_STATUS;
765 *cylp = nrd_trac();
766 }
767 else {
768#endif /* EPSON_NRDISK */
769 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
770 if (ret) {
771 (void)fdc_err(fdc,
772 "sense intr err reading stat reg 0\n");
773 return ret;
774 }
775
776 if (st0p)
777 *st0p = st0;
778
779 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
780 /*
781 * There doesn't seem to have been an interrupt.
782 */
783 return FD_NOT_VALID;
784 }
785
786 if (fd_in(fdc, &cyl) < 0) {
787 return fdc_err(fdc, "can't get cyl num\n");
788 }
789
790 if (cylp)
791 *cylp = cyl;
792
793#ifdef EPSON_NRDISK
794 }
795#endif /* EPSON_NRDISK */
796 return 0;
797}
798
799
800static int
801fd_read_status(fdc_p fdc)
802{
803 int i, ret;
804
805 for (i = ret = 0; i < 7; i++) {
806 /*
807 * XXX types are poorly chosen. Only bytes can be read
808 * from the hardware, but fdc->status[] wants u_ints and
809 * fd_in() gives ints.
810 */
811 int status;
812
813#ifdef EPSON_NRDISK
814 if (fdc->fdu == nrdu) {
815 switch (i) {
816 case 0: fdc->status[i] = nrd_head()
817 ? NRD_ST0_HD : NRD_STATUS; break;
818 case 1: fdc->status[i] = NRD_STATUS; break;
819 case 2: fdc->status[i] = NRD_STATUS; break;
820 case 3: fdc->status[i] = nrd_trac(); break;
821 case 4: fdc->status[i] = nrd_head(); break;
822 case 5: fdc->status[i] = nrdsec; break;
823 case 6: fdc->status[i] = nrd_secsize(); break;
824 }
825 ret = 0;
826 }
827 else {
828#endif /* EPSON_NRDISK */
829 ret = fd_in(fdc, &status);
830 fdc->status[i] = status;
831 if (ret != 0)
832 break;
833#ifdef EPSON_NRDISK
834 }
835#endif /* EPSON_NRDISK */
836 }
837
838 if (ret == 0)
839 fdc->flags |= FDC_STAT_VALID;
840 else
841 fdc->flags &= ~FDC_STAT_VALID;
842
843 return ret;
844}
845
846#ifdef PC98
847static int pc98_trans = 0; /* 0 : HD , 1 : DD , 2 : 1.44 */
848static int pc98_trans_prev = 0;
849
850static void set_density(fdc_p fdc)
851{
852 /* always motor on */
853 bus_space_write_1(fdc->sc_fdsiot, fdc->sc_fdsioh, 0,
854 (pc98_trans != 1 ? FDP_FDDEXC : 0) | FDP_PORTEXC);
855 DELAY(100);
856 fdctl_wr(fdc, FDC_RST | FDC_DMAE);
857 /* in the case of note W, always inhibit 100ms timer */
858}
859
860static int pc98_fd_check_ready(fdu_t fdu)
861{
862 fd_p fd = devclass_get_softc(fd_devclass, fdu);
863 struct fdc_data *fdc = fd->fdc;
864 int retry = 0, status;
865
866#ifdef EPSON_NRDISK
867 if (fdu == nrdu) {
868 if (nrd_check_ready()) return 0;
869 else return -1;
870 }
871#endif
872 while (retry++ < 30000) {
873 set_motor(fdc, fd->fdsu, TURNON);
874 out_fdc(fdc, NE7CMD_SENSED); /* Sense Drive Status */
875 DELAY(100);
876 out_fdc(fdc, fdu); /* Drive number */
877 DELAY(100);
878 if ((fd_in(fdc, &status) == 0) && (status & NE7_ST3_RD)) {
879 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
880 DELAY(10);
881 return 0;
882 }
883 }
884 return -1;
885}
886#endif /* PC98 */
887
888static int
889fdc_alloc_resources(struct fdc_data *fdc)
890{
891 device_t dev;
892#ifdef PC98
893 int rid;
894#else
895 int ispnp, ispcmcia, nports;
896#endif
897
898 dev = fdc->fdc_dev;
899#ifndef PC98
900 ispnp = (fdc->flags & FDC_ISPNP) != 0;
901 ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
902#endif
903 fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
904 fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
905
906#ifdef PC98
907 fdc->res_ioport = isa_alloc_resourcev(dev, SYS_RES_IOPORT,
908 &fdc->rid_ioport, fdc_iat,
909 3, RF_ACTIVE);
910 if (fdc->res_ioport == 0) {
911 device_printf(dev, "cannot reserve I/O port range\n");
912 return ENXIO;
913 }
914 isa_load_resourcev(fdc->res_ioport, fdc_iat, 3);
915#else
916 /*
917 * On standard ISA, we don't just use an 8 port range
918 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
919 * register at 0x3f6.
920 *
921 * Isn't PC hardware wonderful.
922 *
923 * The Y-E Data PCMCIA FDC doesn't have this problem, it
924 * uses the register with offset 6 for pseudo-DMA, and the
925 * one with offset 7 as control register.
926 */
927 nports = ispcmcia ? 8 : (ispnp ? 1 : 6);
928 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
929 &fdc->rid_ioport, 0ul, ~0ul,
930 nports, RF_ACTIVE);
931 if (fdc->res_ioport == 0) {
932 device_printf(dev, "cannot reserve I/O port range (%d ports)\n",
933 nports);
934 return ENXIO;
935 }
936#endif
937 fdc->portt = rman_get_bustag(fdc->res_ioport);
938 fdc->porth = rman_get_bushandle(fdc->res_ioport);
939
940#ifdef PC98
941 rid = 3;
942 bus_set_resource(dev, SYS_RES_IOPORT, rid, IO_FDPORT, 1);
943 fdc->res_fdsio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
944 1, RF_ACTIVE);
945 if (fdc->res_fdsio == 0)
946 return ENXIO;
947 fdc->sc_fdsiot = rman_get_bustag(fdc->res_fdsio);
948 fdc->sc_fdsioh = rman_get_bushandle(fdc->res_fdsio);
949
950 rid = 4;
951 bus_set_resource(dev, SYS_RES_IOPORT, rid, 0x4be, 1);
952 fdc->res_fdemsio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
953 1, RF_ACTIVE);
954 if (fdc->res_fdemsio == 0)
955 return ENXIO;
956 fdc->sc_fdemsiot = rman_get_bustag(fdc->res_fdemsio);
957 fdc->sc_fdemsioh = rman_get_bushandle(fdc->res_fdemsio);
958#endif
959
960#ifndef PC98
961 if (!ispcmcia) {
962 /*
963 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
964 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
965 * by checking the size and adjust the port address
966 * accordingly.
967 */
968 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
969 fdc->port_off = -2;
970
971 /*
972 * Register the control port range as rid 1 if it
973 * isn't there already. Most PnP BIOSen will have
974 * already done this but non-PnP configurations don't.
975 *
976 * And some (!!) report 0x3f2-0x3f5 and completely
977 * leave out the control register! It seems that some
978 * non-antique controller chips have a different
979 * method of programming the transfer speed which
980 * doesn't require the control register, but it's
981 * mighty bogus as the chip still responds to the
982 * address for the control register.
983 */
984 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
985 u_long ctlstart;
986
987 /* Find the control port, usually 0x3f7 */
988 ctlstart = rman_get_start(fdc->res_ioport) +
989 fdc->port_off + 7;
990
991 bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
992 }
993
994 /*
995 * Now (finally!) allocate the control port.
996 */
997 fdc->rid_ctl = 1;
998 fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
999 &fdc->rid_ctl,
1000 0ul, ~0ul, 1, RF_ACTIVE);
1001 if (fdc->res_ctl == 0) {
1002 device_printf(dev,
1003 "cannot reserve control I/O port range (control port)\n");
1004 return ENXIO;
1005 }
1006 fdc->ctlt = rman_get_bustag(fdc->res_ctl);
1007 fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
1008 }
1009#endif
1010
1011 fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
1012 &fdc->rid_irq, 0ul, ~0ul, 1,
1013 RF_ACTIVE);
1014 if (fdc->res_irq == 0) {
1015 device_printf(dev, "cannot reserve interrupt line\n");
1016 return ENXIO;
1017 }
1018
1019 if ((fdc->flags & FDC_NODMA) == 0) {
1020 fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
1021 &fdc->rid_drq, 0ul, ~0ul, 1,
1022 RF_ACTIVE);
1023 if (fdc->res_drq == 0) {
1024 device_printf(dev, "cannot reserve DMA request line\n");
1025 return ENXIO;
1026 }
1027 fdc->dmachan = fdc->res_drq->r_start;
1028 }
1029
1030 return 0;
1031}
1032
1033static void
1034fdc_release_resources(struct fdc_data *fdc)
1035{
1036 device_t dev;
1037
1038 dev = fdc->fdc_dev;
1039 if (fdc->res_irq != 0) {
1040 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
1041 fdc->res_irq);
1042 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
1043 fdc->res_irq);
1044 }
1045#ifndef PC98
1046 if (fdc->res_ctl != 0) {
1047 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
1048 fdc->res_ctl);
1049 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
1050 fdc->res_ctl);
1051 }
1052#endif
1053#ifdef PC98
1054 if (fdc->res_fdsio != 0) {
1055 bus_deactivate_resource(dev, SYS_RES_IOPORT, 3,
1056 fdc->res_fdsio);
1057 bus_release_resource(dev, SYS_RES_IOPORT, 3, fdc->res_fdsio);
1058 }
1059 if (fdc->res_fdemsio != 0) {
1060 bus_deactivate_resource(dev, SYS_RES_IOPORT, 4,
1061 fdc->res_fdemsio);
1062 bus_release_resource(dev, SYS_RES_IOPORT, 4, fdc->res_fdemsio);
1063 }
1064#endif
1065 if (fdc->res_ioport != 0) {
1066 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
1067 fdc->res_ioport);
1068 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
1069 fdc->res_ioport);
1070 }
1071 if (fdc->res_drq != 0) {
1072 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
1073 fdc->res_drq);
1074 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
1075 fdc->res_drq);
1076 }
1077}
1078
1079/*
1080 * Configuration/initialization stuff, per controller.
1081 */
1082
1083static struct isa_pnp_id fdc_ids[] = {
1084 {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
1085 {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
1086 {0}
1087};
1088
1089static int
1090fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1091{
1092 struct fdc_ivars *ivars = device_get_ivars(child);
1093
1094 switch (which) {
1095 case FDC_IVAR_FDUNIT:
1096 *result = ivars->fdunit;
1097 break;
1098 default:
1099 return ENOENT;
1100 }
1101 return 0;
1102}
1103
1104static int
1105fdc_probe(device_t dev)
1106{
1107#ifdef PC98
1108 int error;
1109#else
1110 int error, ic_type;
1111#endif
1112 struct fdc_data *fdc;
1113
1114 fdc = device_get_softc(dev);
1115 bzero(fdc, sizeof *fdc);
1116 fdc->fdc_dev = dev;
1117#ifndef PC98
1118 fdc->fdctl_wr = fdctl_wr_isa;
1119#endif
1120
1121 /* Check pnp ids */
1122 error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
1123 if (error == ENXIO)
1124 return ENXIO;
1125 if (error == 0)
1126 fdc->flags |= FDC_ISPNP;
1127
1128 /* Attempt to allocate our resources for the duration of the probe */
1129 error = fdc_alloc_resources(fdc);
1130 if (error)
1131 goto out;
1132
1133#ifndef PC98
1134 /* First - lets reset the floppy controller */
1135 fdout_wr(fdc, 0);
1136 DELAY(100);
1137 fdout_wr(fdc, FDO_FRST);
1138#endif
1139
1140 /* see if it can handle a command */
1141#ifdef PC98
1142 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(4, 240),
1143 NE7_SPEC_2(2, 0), 0)) {
1144 error = ENXIO;
1145 goto out;
1146 }
1147#else
1148 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
1149 NE7_SPEC_2(2, 0), 0)) {
1150 error = ENXIO;
1151 goto out;
1152 }
1153#endif
1154
1155#ifndef PC98
1156 if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
1157 ic_type = (u_char)ic_type;
1158 switch (ic_type) {
1159 case 0x80:
1160 device_set_desc(dev, "NEC 765 or clone");
1161 fdc->fdct = FDC_NE765;
1162 break;
1163 case 0x81: /* not mentioned in any hardware doc */
1164 case 0x90:
1165 device_set_desc(dev,
1166 "Enhanced floppy controller (i82077, NE72065 or clone)");
1167 fdc->fdct = FDC_ENHANCED;
1168 break;
1169 default:
1170 device_set_desc(dev, "Generic floppy controller");
1171 fdc->fdct = FDC_UNKNOWN;
1172 break;
1173 }
1174 }
1175#endif
1176
1177out:
1178 fdc_release_resources(fdc);
1179 return (error);
1180}
1181
1182#if NCARD > 0
1183
1184static int
1185fdc_pccard_probe(device_t dev)
1186{
1187 int error;
1188 struct fdc_data *fdc;
1189
1190 fdc = device_get_softc(dev);
1191 bzero(fdc, sizeof *fdc);
1192 fdc->fdc_dev = dev;
1193#ifndef PC98
1194 fdc->fdctl_wr = fdctl_wr_pcmcia;
1195#endif
1196
1197 fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
1198
1199 /* Attempt to allocate our resources for the duration of the probe */
1200 error = fdc_alloc_resources(fdc);
1201 if (error)
1202 goto out;
1203
1204#ifndef PC98
1205 /* First - lets reset the floppy controller */
1206 fdout_wr(fdc, 0);
1207 DELAY(100);
1208 fdout_wr(fdc, FDO_FRST);
1209#endif
1210
1211 /* see if it can handle a command */
1212#ifdef PC98
1213 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(4, 240),
1214 NE7_SPEC_2(2, 0), 0)) {
1215 error = ENXIO;
1216 goto out;
1217 }
1218#else
1219 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
1220 NE7_SPEC_2(2, 0), 0)) {
1221 error = ENXIO;
1222 goto out;
1223 }
1224#endif
1225
1226 device_set_desc(dev, "Y-E Data PCMCIA floppy");
1227 fdc->fdct = FDC_NE765;
1228
1229out:
1230 fdc_release_resources(fdc);
1231 return (error);
1232}
1233
1234#endif /* NCARD > 0 */
1235
1236static int
1237fdc_detach(device_t dev)
1238{
1239 struct fdc_data *fdc;
1240 int error;
1241
1242 fdc = device_get_softc(dev);
1243
1244 /* have our children detached first */
1245 if ((error = bus_generic_detach(dev)))
1246 return (error);
1247
1248#ifdef PC98
1249 /* reset controller, turn motor off */
1250 fdc_reset(fdc);
1251#else
1252 /* reset controller, turn motor off */
1253 fdout_wr(fdc, 0);
1254#endif
1255
1256 if ((fdc->flags & FDC_NODMA) == 0)
1257 isa_dma_release(fdc->dmachan);
1258
1259 if ((fdc->flags & FDC_ATTACHED) == 0) {
1260 device_printf(dev, "already unloaded\n");
1261 return (0);
1262 }
1263 fdc->flags &= ~FDC_ATTACHED;
1264
1265 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
1266 fdc->fdc_intr);
1267 fdc_release_resources(fdc);
1268 device_printf(dev, "unload\n");
1269 return (0);
1270}
1271
1272/*
1273 * Add a child device to the fdc controller. It will then be probed etc.
1274 */
1275static void
1276fdc_add_child(device_t dev, const char *name, int unit)
1277{
1278 int disabled, flags;
1279 struct fdc_ivars *ivar;
1280 device_t child;
1281
1282 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
1283 if (ivar == NULL)
1284 return;
1285 if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
1286 ivar->fdunit = 0;
1287 child = device_add_child(dev, name, unit);
1288 if (child == NULL) {
1289 free(ivar, M_DEVBUF);
1290 return;
1291 }
1292 device_set_ivars(child, ivar);
1293 if (resource_int_value(name, unit, "flags", &flags) == 0)
1294 device_set_flags(child, flags);
1295 if (resource_int_value(name, unit, "disabled", &disabled) == 0
1296 && disabled != 0)
1297 device_disable(child);
1298}
1299
1300static int
1301fdc_attach(device_t dev)
1302{
1303 struct fdc_data *fdc;
1304 const char *name, *dname;
1305 int i, error, dunit;
1306
1307 fdc = device_get_softc(dev);
1308 error = fdc_alloc_resources(fdc);
1309 if (error) {
1310 device_printf(dev, "cannot re-acquire resources\n");
1311 return error;
1312 }
1313 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
1314 INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc,
1315 &fdc->fdc_intr);
1316 if (error) {
1317 device_printf(dev, "cannot setup interrupt\n");
1318 return error;
1319 }
1320 fdc->fdcu = device_get_unit(dev);
1321 fdc->flags |= FDC_ATTACHED | FDC_NEEDS_RESET;
1322
1323 if ((fdc->flags & FDC_NODMA) == 0) {
1324 /*
1325 * Acquire the DMA channel forever, the driver will do
1326 * the rest
1327 * XXX should integrate with rman
1328 */
1329 isa_dma_acquire(fdc->dmachan);
1330 isa_dmainit(fdc->dmachan, MAX_SEC_SIZE);
1331 }
1332 fdc->state = DEVIDLE;
1333
1334#ifdef PC98
1335 /* reset controller, turn motor off, clear fdout mirror reg */
1336 fdc_reset(fdc);
1337#else
1338 /* reset controller, turn motor off, clear fdout mirror reg */
1339 fdout_wr(fdc, fdc->fdout = 0);
1340#endif
1341 bioq_init(&fdc->head);
1342
1343 /*
1344 * Probe and attach any children. We should probably detect
1345 * devices from the BIOS unless overridden.
1346 */
1347 name = device_get_nameunit(dev);
1348 i = 0;
1349 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0)
1350 fdc_add_child(dev, dname, dunit);
1351
1352 if ((error = bus_generic_attach(dev)) != 0)
1353 return (error);
1354
1355 return (0);
1356}
1357
1358static int
1359fdc_print_child(device_t me, device_t child)
1360{
1361 int retval = 0, flags;
1362
1363 retval += bus_print_child_header(me, child);
1364 retval += printf(" on %s drive %d", device_get_nameunit(me),
1365 fdc_get_fdunit(child));
1366 if ((flags = device_get_flags(me)) != 0)
1367 retval += printf(" flags %#x", flags);
1368 retval += printf("\n");
1369
1370 return (retval);
1371}
1372
1373static device_method_t fdc_methods[] = {
1374 /* Device interface */
1375 DEVMETHOD(device_probe, fdc_probe),
1376 DEVMETHOD(device_attach, fdc_attach),
1377 DEVMETHOD(device_detach, fdc_detach),
1378 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1379 DEVMETHOD(device_suspend, bus_generic_suspend),
1380 DEVMETHOD(device_resume, bus_generic_resume),
1381
1382 /* Bus interface */
1383 DEVMETHOD(bus_print_child, fdc_print_child),
1384 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
1385 /* Our children never use any other bus interface methods. */
1386
1387 { 0, 0 }
1388};
1389
1390static driver_t fdc_driver = {
1391 "fdc",
1392 fdc_methods,
1393 sizeof(struct fdc_data)
1394};
1395
1396DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
1397#ifndef PC98
1398DRIVER_MODULE(fdc, acpi, fdc_driver, fdc_devclass, 0, 0);
1399#endif
1400
1401#if NCARD > 0
1402
1403static device_method_t fdc_pccard_methods[] = {
1404 /* Device interface */
1405 DEVMETHOD(device_probe, fdc_pccard_probe),
1406 DEVMETHOD(device_attach, fdc_attach),
1407 DEVMETHOD(device_detach, fdc_detach),
1408 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1409 DEVMETHOD(device_suspend, bus_generic_suspend),
1410 DEVMETHOD(device_resume, bus_generic_resume),
1411
1412 /* Bus interface */
1413 DEVMETHOD(bus_print_child, fdc_print_child),
1414 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
1415 /* Our children never use any other bus interface methods. */
1416
1417 { 0, 0 }
1418};
1419
1420static driver_t fdc_pccard_driver = {
1421 "fdc",
1422 fdc_pccard_methods,
1423 sizeof(struct fdc_data)
1424};
1425
1426DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
1427
1428#endif /* NCARD > 0 */
1429
1430/*
1431 * Create a clone device upon request by devfs.
1432 */
1433static void
1434fd_clone(void *arg, char *name, int namelen, dev_t *dev)
1435{
1436 struct fd_data *fd;
1437 int i, u;
1438 char *n;
1439 size_t l;
1440
1441 fd = (struct fd_data *)arg;
1442 if (*dev != NODEV)
1443 return;
1444 if (dev_stdclone(name, &n, "fd", &u) != 2)
1445 return;
1446 if (u != fd->fdu)
1447 /* unit # mismatch */
1448 return;
1449 l = strlen(n);
1450 if (l == 1 && *n >= 'a' && *n <= 'h') {
1451 /*
1452 * Trailing letters a through h denote
1453 * pseudo-partitions. We don't support true
1454 * (UFS-style) partitions, so we just implement them
1455 * as symlinks if someone asks us nicely.
1456 */
1457 *dev = make_dev_alias(fd->masterdev, name);
1458 return;
1459 }
1460 if (l >= 2 && l <= 5 && *n == '.') {
1461 /*
1462 * Trailing numbers, preceded by a dot, denote
1463 * subdevices for different densities. Historically,
1464 * they have been named by density (like fd0.1440),
1465 * but we allow arbitrary numbers between 1 and 4
1466 * digits, so fd0.1 through fd0.15 are possible as
1467 * well.
1468 */
1469 for (i = 1; i < l; i++)
1470 if (n[i] < '0' || n[i] > '9')
1471 return;
1472 for (i = 0; i < NUMDENS - 1; i++)
1473 if (fd->clonedevs[i] == NODEV) {
1474 *dev = make_dev(&fd_cdevsw,
1475 FDNUMTOUNIT(u) + i + 1,
1476 UID_ROOT, GID_OPERATOR, 0640,
1477 name);
1478 fd->clonedevs[i] = *dev;
1479 return;
1480 }
1481 }
1482}
1483
1484/*
1485 * Configuration/initialization, per drive.
1486 */
1487static int
1488fd_probe(device_t dev)
1489{
1490 int i;
1491#ifndef PC98
1492 u_int st0, st3;
1493#endif
1494 struct fd_data *fd;
1495 struct fdc_data *fdc;
1496 fdsu_t fdsu;
1497 int flags;
1498
1499 fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
1500 fd = device_get_softc(dev);
1501 fdc = device_get_softc(device_get_parent(dev));
1502 flags = device_get_flags(dev);
1503
1504 bzero(fd, sizeof *fd);
1505 fd->dev = dev;
1506 fd->fdc = fdc;
1507 fd->fdsu = fdsu;
1508 fd->fdu = device_get_unit(dev);
1509 fd->flags = FD_UA; /* make sure fdautoselect() will be called */
1510
1511 fd->type = FD_DTYPE(flags);
1512#ifdef PC98
1513 if (fd->type == FDT_NONE && fd->fdu >= 0 && fd->fdu <= 3) {
1514 /* Look up what the BIOS thinks we have. */
1515 if ((PC98_SYSTEM_PARAMETER(0x5ae) >> fd->fdu) & 0x01)
1516 fd->type = FDT_144M;
1517#ifdef EPSON_NRDISK
1518 else if ((PC98_SYSTEM_PARAMETER(0x55c) >> fd->fdu) & 0x01) {
1519 fd->type = FDT_12M;
1520 switch (epson_machine_id) {
1521 case 0x20:
1522 case 0x27:
1523 if ((PC98_SYSTEM_PARAMETER(0x488) >> fd->fdu)
1524 & 0x01) {
1525 if (nrd_check_ready()) {
1526 nrd_LED_on();
1527 nrdu = fd->fdu;
1528 } else {
1529 fd->type = FDT_NONE;
1530 }
1531 }
1532 break;
1533 }
1534 }
1535#else /* !EPSON_NRDISK */
1536 else if ((PC98_SYSTEM_PARAMETER(0x55c) >> fd->fdu) & 0x01) {
1537 fd->type = FDT_12M;
1538 switch (epson_machine_id) {
1539 case 0x20:
1540 case 0x27:
1541 if ((PC98_SYSTEM_PARAMETER(0x488) >> fd->fdu)
1542 & 0x01)
1543 fd->type = FDT_NONE;
1544 break;
1545 }
1546 }
1547#endif /* EPSON_NRDISK */
1548 }
1549#else /* PC98 */
1550/*
1551 * XXX I think using __i386__ is wrong here since we actually want to probe
1552 * for the machine type, not the CPU type (so non-PC arch's like the PC98 will
1553 * fail the probe). However, for whatever reason, testing for _MACHINE_ARCH
1554 * == i386 breaks the test on FreeBSD/Alpha.
1555 */
1556#ifdef __i386__
1557 if (fd->type == FDT_NONE && (fd->fdu == 0 || fd->fdu == 1)) {
1558 /* Look up what the BIOS thinks we have. */
1559 if (fd->fdu == 0) {
1560 if ((fdc->flags & FDC_ISPCMCIA))
1561 /*
1562 * Somewhat special. No need to force the
1563 * user to set device flags, since the Y-E
1564 * Data PCMCIA floppy is always a 1.44 MB
1565 * device.
1566 */
1567 fd->type = FDT_144M;
1568 else
1569 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
1570 } else {
1571 fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
1572 }
1573 if (fd->type == FDT_288M_1)
1574 fd->type = FDT_288M;
1575 }
1576#endif /* __i386__ */
1577#endif /* PC98 */
1578
1579 /* is there a unit? */
1580 if (fd->type == FDT_NONE)
1581 return (ENXIO);
1582
1583#ifndef PC98
1584 /* select it */
1585 set_motor(fdc, fdsu, TURNON);
1586 fdc_reset(fdc); /* XXX reset, then unreset, etc. */
1587 DELAY(1000000); /* 1 sec */
1588
1589 /* XXX This doesn't work before the first set_motor() */
1590 if ((fdc->flags & FDC_HAS_FIFO) == 0 &&
1591 fdc->fdct == FDC_ENHANCED &&
1592 (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 &&
1593 enable_fifo(fdc) == 0) {
1594 device_printf(device_get_parent(dev),
1595 "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1596 }
1597
1598 if ((flags & FD_NO_PROBE) == 0) {
1599 /* If we're at track 0 first seek inwards. */
1600 if ((fd_sense_drive_status(fdc, &st3) == 0) &&
1601 (st3 & NE7_ST3_T0)) {
1602 /* Seek some steps... */
1603 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1604 /* ...wait a moment... */
1605 DELAY(300000);
1606 /* make ctrlr happy: */
1607 fd_sense_int(fdc, 0, 0);
1608 }
1609 }
1610
1611 for (i = 0; i < 2; i++) {
1612 /*
1613 * we must recalibrate twice, just in case the
1614 * heads have been beyond cylinder 76, since
1615 * most FDCs still barf when attempting to
1616 * recalibrate more than 77 steps
1617 */
1618 /* go back to 0: */
1619 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1620 /* a second being enough for full stroke seek*/
1621 DELAY(i == 0 ? 1000000 : 300000);
1622
1623 /* anything responding? */
1624 if (fd_sense_int(fdc, &st0, 0) == 0 &&
1625 (st0 & NE7_ST0_EC) == 0)
1626 break; /* already probed succesfully */
1627 }
1628 }
1629 }
1630
1631 set_motor(fdc, fdsu, TURNOFF);
1632
1633 if ((flags & FD_NO_PROBE) == 0 &&
1634 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
1635 return (ENXIO);
1636#endif /* PC98 */
1637
1638#ifdef PC98
1639 switch (fd->type) {
1640 case FDT_144M:
1641 /* Check 3mode I/F */
1642 fd->pc98_trans = 0;
1643 bus_space_write_1(fdc->sc_fdemsiot, fdc->sc_fdemsioh, 0,
1644 (fd->fdu << 5) | 0x10);
1645 if (!(bus_space_read_1(fdc->sc_fdemsiot, fdc->sc_fdemsioh, 0) &
1646 0x01)) {
1647 device_set_desc(dev, "1.44M FDD");
1648 fd->type = FDT_144M;
1649 break;
1650 }
1651
1652 device_printf(dev,
1653 "Warning: can't control 3mode I/F, fallback to 2mode.\n");
1654 /* FALLTHROUGH */
1655 case FDT_12M:
1656#ifdef EPSON_NRDISK
1657 if (fd->fdu == nrdu) {
1658 device_set_desc(dev, "EPSON RAM DRIVE");
1659 nrd_LED_off();
1660 } else
1661 device_set_desc(dev, "1M/640K FDD");
1662#else
1663 device_set_desc(dev, "1M/640K FDD");
1664#endif
1665 fd->type = FDT_12M;
1666 break;
1667 default:
1668 return (ENXIO);
1669 }
1670#else
1671 switch (fd->type) {
1672 case FDT_12M:
1673 device_set_desc(dev, "1200-KB 5.25\" drive");
1674 fd->type = FDT_12M;
1675 break;
1676 case FDT_144M:
1677 device_set_desc(dev, "1440-KB 3.5\" drive");
1678 fd->type = FDT_144M;
1679 break;
1680 case FDT_288M:
1681 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1682 fd->type = FDT_288M;
1683 break;
1684 case FDT_360K:
1685 device_set_desc(dev, "360-KB 5.25\" drive");
1686 fd->type = FDT_360K;
1687 break;
1688 case FDT_720K:
1689 device_set_desc(dev, "720-KB 3.5\" drive");
1690 fd->type = FDT_720K;
1691 break;
1692 default:
1693 return (ENXIO);
1694 }
1695#endif
1696 fd->track = FD_NO_TRACK;
1697 fd->fdc = fdc;
1698 fd->fdsu = fdsu;
1699 fd->options = 0;
1700#ifdef PC98
1701 fd->pc98_trans = 0;
1702#endif
1703 callout_handle_init(&fd->toffhandle);
1704 callout_handle_init(&fd->tohandle);
1705
1706 /* initialize densities for subdevices */
1707#ifdef PC98
1708 for (i = 0; i < NUMDENS; i++)
1709 memcpy(fd->fts + i, fd_searchlist_144m + i,
1710 sizeof(struct fd_type));
1711#else
1712 for (i = 0; i < NUMDENS; i++)
1713 memcpy(fd->fts + i, fd_native_types + fd->type,
1714 sizeof(struct fd_type));
1715#endif
1716 return (0);
1717}
1718
1719static int
1720fd_attach(device_t dev)
1721{
1722 struct fd_data *fd;
1723 int i;
1724
1725 fd = device_get_softc(dev);
1726 fd->clonetag = EVENTHANDLER_REGISTER(dev_clone, fd_clone, fd, 1000);
1727 fd->masterdev = make_dev(&fd_cdevsw, fd->fdu << 6,
1728 UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu);
1729 for (i = 0; i < NUMDENS - 1; i++)
1730 fd->clonedevs[i] = NODEV;
1731 devstat_add_entry(&fd->device_stats, device_get_name(dev),
1732 device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS,
1733 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
1734 DEVSTAT_PRIORITY_FD);
1735 return (0);
1736}
1737
1738static int
1739fd_detach(device_t dev)
1740{
1741 struct fd_data *fd;
1742 int i;
1743
1744 fd = device_get_softc(dev);
1745 untimeout(fd_turnoff, fd, fd->toffhandle);
1746 devstat_remove_entry(&fd->device_stats);
1747 destroy_dev(fd->masterdev);
1748 for (i = 0; i < NUMDENS - 1; i++)
1749 if (fd->clonedevs[i] != NODEV)
1750 destroy_dev(fd->clonedevs[i]);
1751 EVENTHANDLER_DEREGISTER(dev_clone, fd->clonetag);
1752
1753 return (0);
1754}
1755
1756static device_method_t fd_methods[] = {
1757 /* Device interface */
1758 DEVMETHOD(device_probe, fd_probe),
1759 DEVMETHOD(device_attach, fd_attach),
1760 DEVMETHOD(device_detach, fd_detach),
1761 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1762 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
1763 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
1764
1765 { 0, 0 }
1766};
1767
1768static driver_t fd_driver = {
1769 "fd",
1770 fd_methods,
1771 sizeof(struct fd_data)
1772};
1773
1774DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
1775
1776/*
1777 * More auxiliary functions.
1778 */
1779/*
1780 * Motor control stuff.
1781 * Remember to not deselect the drive we're working on.
1782 */
1783static void
1784set_motor(struct fdc_data *fdc, int fdsu, int turnon)
1785{
1786#ifdef PC98
1787 bus_space_write_1(fdc->sc_fdsiot, fdc->sc_fdsioh, 0,
1788 (pc98_trans != 1 ? FDP_FDDEXC : 0) | FDP_PORTEXC);
1789 DELAY(10);
1790 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
1791#else
1792 int fdout;
1793
1794 fdout = fdc->fdout;
1795 if (turnon) {
1796 fdout &= ~FDO_FDSEL;
1797 fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu;
1798 } else
1799 fdout &= ~(FDO_MOEN0 << fdsu);
1800 fdc->fdout = fdout;
1801 fdout_wr(fdc, fdout);
1802 TRACE1("[0x%x->FDOUT]", fdout);
1803#endif
1804}
1805
1806static void
1807fd_turnoff(void *xfd)
1808{
1809 int s;
1810 fd_p fd = xfd;
1811
1812 TRACE1("[fd%d: turnoff]", fd->fdu);
1813
1814 s = splbio();
1815 /*
1816 * Don't turn off the motor yet if the drive is active.
1817 *
1818 * If we got here, this could only mean we missed an interrupt.
1819 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
1820 * after a controller reset. Just schedule a pseudo-interrupt
1821 * so the state machine gets re-entered.
1822 */
1823 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
1824 fdc_intr(fd->fdc);
1825 splx(s);
1826 return;
1827 }
1828
1829 fd->flags &= ~FD_MOTOR;
1830 set_motor(fd->fdc, fd->fdsu, TURNOFF);
1831 splx(s);
1832}
1833
1834static void
1835fd_motor_on(void *xfd)
1836{
1837 int s;
1838 fd_p fd = xfd;
1839
1840 s = splbio();
1841 fd->flags &= ~FD_MOTOR_WAIT;
1842 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
1843 {
1844 fdc_intr(fd->fdc);
1845 }
1846 splx(s);
1847}
1848
1849static void
1850fd_turnon(fd_p fd)
1851{
1852 if(!(fd->flags & FD_MOTOR))
1853 {
1854 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
1855 set_motor(fd->fdc, fd->fdsu, TURNON);
1856 timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
1857 }
1858}
1859
1860static void
1861fdc_reset(fdc_p fdc)
1862{
1863 /* Try a reset, keep motor on */
1864#ifdef PC98
1865 set_density(fdc);
1866 if (pc98_machine_type & M_EPSON_PC98)
1867 fdctl_wr(fdc, FDC_RST | FDC_RDY | FDC_DD | FDC_MTON);
1868 else
1869 fdctl_wr(fdc, FDC_RST | FDC_RDY | FDC_DMAE | FDC_MTON);
1870 DELAY(200);
1871 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
1872 DELAY(10);
1873#else
1874 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1875 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1876 DELAY(100);
1877 /* enable FDC, but defer interrupts a moment */
1878 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
1879 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
1880 DELAY(100);
1881 fdout_wr(fdc, fdc->fdout);
1882 TRACE1("[0x%x->FDOUT]", fdc->fdout);
1883#endif
1884
1885 /* XXX after a reset, silently believe the FDC will accept commands */
1886#ifdef PC98
1887 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1888 NE7_SPEC_1(4, 240), NE7_SPEC_2(2, 0),
1889 0);
1890#else
1891 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1892 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1893 0);
1894#endif
1895 if (fdc->flags & FDC_HAS_FIFO)
1896 (void) enable_fifo(fdc);
1897}
1898
1899/*
1900 * FDC IO functions, take care of the main status register, timeout
1901 * in case the desired status bits are never set.
1902 *
1903 * These PIO loops initially start out with short delays between
1904 * each iteration in the expectation that the required condition
1905 * is usually met quickly, so it can be handled immediately. After
1906 * about 1 ms, stepping is increased to achieve a better timing
1907 * accuracy in the calls to DELAY().
1908 */
1909static int
1910fd_in(struct fdc_data *fdc, int *ptr)
1911{
1912 int i, j, step;
1913
1914 for (j = 0, step = 1;
1915 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) &&
1916 j < FDSTS_TIMEOUT;
1917 j += step) {
1918 if (i == NE7_RQM)
1919 return (fdc_err(fdc, "ready for output in input\n"));
1920 if (j == 1000)
1921 step = 1000;
1922 DELAY(step);
1923 }
1924 if (j >= FDSTS_TIMEOUT)
1925 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
1926#ifdef FDC_DEBUG
1927 i = fddata_rd(fdc);
1928 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1929 *ptr = i;
1930 return (0);
1931#else /* !FDC_DEBUG */
1932 i = fddata_rd(fdc);
1933 if (ptr)
1934 *ptr = i;
1935 return (0);
1936#endif /* FDC_DEBUG */
1937}
1938
1939static int
1940out_fdc(struct fdc_data *fdc, int x)
1941{
1942 int i, j, step;
1943
1944 for (j = 0, step = 1;
1945 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM &&
1946 j < FDSTS_TIMEOUT;
1947 j += step) {
1948 if (i == (NE7_DIO|NE7_RQM))
1949 return (fdc_err(fdc, "ready for input in output\n"));
1950 if (j == 1000)
1951 step = 1000;
1952 DELAY(step);
1953 }
1954 if (j >= FDSTS_TIMEOUT)
1955 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
1956
1957 /* Send the command and return */
1958 fddata_wr(fdc, x);
1959 TRACE1("[0x%x->FDDATA]", x);
1960 return (0);
1961}
1962
1963/*
1964 * Block device driver interface functions (interspersed with even more
1965 * auxiliary functions).
1966 */
1967static int
1968Fdopen(dev_t dev, int flags, int mode, struct thread *td)
1969{
1970 fdu_t fdu = FDUNIT(minor(dev));
1971 int type = FDTYPE(minor(dev));
1972 fd_p fd;
1973 fdc_p fdc;
1974 int rv, unitattn, dflags;
1975
1976 if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1977 return (ENXIO);
1978 fdc = fd->fdc;
1979 if ((fdc == NULL) || (fd->type == FDT_NONE))
1980 return (ENXIO);
1981 if (type > NUMDENS)
1982 return (ENXIO);
1983 dflags = device_get_flags(fd->dev);
1984 /*
1985 * This is a bit bogus. It's still possible that e. g. a
1986 * descriptor gets inherited to a child, but then it's at
1987 * least for the same subdevice. By checking FD_OPEN here, we
1988 * can ensure that a device isn't attempted to be opened with
1989 * different densities at the same time where the second open
1990 * could clobber the settings from the first one.
1991 */
1992 if (fd->flags & FD_OPEN)
1993 return (EBUSY);
1994
1995#ifdef PC98
1996 if (pc98_fd_check_ready(fdu) == -1)
1997 return(EIO);
1998#endif
1999
2000 if (type == 0) {
2001 if (flags & FNONBLOCK) {
2002 /*
2003 * Unfortunately, physio(9) discards its ioflag
2004 * argument, thus preventing us from seeing the
2005 * IO_NDELAY bit. So we need to keep track
2006 * ourselves.
2007 */
2008 fd->flags |= FD_NONBLOCK;
2009 fd->ft = 0;
2010 } else {
2011 /*
2012 * Figure out a unit attention condition.
2013 *
2014 * If UA has been forced, proceed.
2015 *
2016 * If motor is off, turn it on for a moment
2017 * and select our drive, in order to read the
2018 * UA hardware signal.
2019 *
2020 * If motor is on, and our drive is currently
2021 * selected, just read the hardware bit.
2022 *
2023 * If motor is on, but active for another
2024 * drive on that controller, we are lost. We
2025 * cannot risk to deselect the other drive, so
2026 * we just assume a forced UA condition to be
2027 * on the safe side.
2028 */
2029 unitattn = 0;
2030 if ((dflags & FD_NO_CHLINE) != 0 ||
2031 (fd->flags & FD_UA) != 0) {
2032 unitattn = 1;
2033 fd->flags &= ~FD_UA;
2034#ifndef PC98
2035 } else if (fdc->fdout & (FDO_MOEN0 | FDO_MOEN1 |
2036 FDO_MOEN2 | FDO_MOEN3)) {
2037 if ((fdc->fdout & FDO_FDSEL) == fd->fdsu)
2038 unitattn = fdin_rd(fdc) & FDI_DCHG;
2039 else
2040 unitattn = 1;
2041 } else {
2042 set_motor(fdc, fd->fdsu, TURNON);
2043 unitattn = fdin_rd(fdc) & FDI_DCHG;
2044 set_motor(fdc, fd->fdsu, TURNOFF);
2045#endif /* PC98 */
2046 }
2047 if (unitattn && (rv = fdautoselect(dev)) != 0)
2048 return (rv);
2049 }
2050 } else {
2051 fd->ft = fd->fts + type;
2052 }
2053 fd->flags |= FD_OPEN;
2054 /*
2055 * Clearing the DMA overrun counter at open time is a bit messy.
2056 * Since we're only managing one counter per controller, opening
2057 * the second drive could mess it up. Anyway, if the DMA overrun
2058 * condition is really persistent, it will eventually time out
2059 * still. OTOH, clearing it here will ensure we'll at least start
2060 * trying again after a previous (maybe even long ago) failure.
2061 * Also, this is merely a stop-gap measure only that should not
2062 * happen during normal operation, so we can tolerate it to be a
2063 * bit sloppy about this.
2064 */
2065 fdc->dma_overruns = 0;
2066
2067 return 0;
2068}
2069
2070static int
2071fdclose(dev_t dev, int flags, int mode, struct thread *td)
2072{
2073 fdu_t fdu = FDUNIT(minor(dev));
2074 struct fd_data *fd;
2075
2076 fd = devclass_get_softc(fd_devclass, fdu);
2077 fd->flags &= ~(FD_OPEN | FD_NONBLOCK);
2078 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
2079
2080 return (0);
2081}
2082
2083static void
2084fdstrategy(struct bio *bp)
2085{
2086 long blknum, nblocks;
2087 int s;
2088 fdu_t fdu;
2089 fdc_p fdc;
2090 fd_p fd;
2091 size_t fdblk;
2092
2093 fdu = FDUNIT(minor(bp->bio_dev));
2094 fd = devclass_get_softc(fd_devclass, fdu);
2095 if (fd == 0)
2096 panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
2097 (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev));
2098 fdc = fd->fdc;
2099 if (fd->type == FDT_NONE || fd->ft == 0) {
2100 bp->bio_error = ENXIO;
2101 bp->bio_flags |= BIO_ERROR;
2102 goto bad;
2103 }
2104 fdblk = 128 << (fd->ft->secsize);
2105 if (bp->bio_cmd != FDBIO_FORMAT && bp->bio_cmd != FDBIO_RDSECTID) {
2106 if (fd->flags & FD_NONBLOCK) {
2107 bp->bio_error = EAGAIN;
2108 bp->bio_flags |= BIO_ERROR;
2109 goto bad;
2110 }
2111 if (bp->bio_blkno < 0) {
2112 printf(
2113 "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
2114 fdu, (u_long)bp->bio_blkno, bp->bio_bcount);
2115 bp->bio_error = EINVAL;
2116 bp->bio_flags |= BIO_ERROR;
2117 goto bad;
2118 }
2119 if ((bp->bio_bcount % fdblk) != 0) {
2120 bp->bio_error = EINVAL;
2121 bp->bio_flags |= BIO_ERROR;
2122 goto bad;
2123 }
2124 }
2125
2126 /*
2127 * Set up block calculations.
2128 */
2129 if (bp->bio_blkno > 20000000) {
2130 /*
2131 * Reject unreasonably high block number, prevent the
2132 * multiplication below from overflowing.
2133 */
2134 bp->bio_error = EINVAL;
2135 bp->bio_flags |= BIO_ERROR;
2136 goto bad;
2137 }
2138 blknum = bp->bio_blkno * DEV_BSIZE / fdblk;
2139 nblocks = fd->ft->size;
2140 if (blknum + bp->bio_bcount / fdblk > nblocks) {
2141 if (blknum >= nblocks) {
2142 if (bp->bio_cmd == BIO_READ)
2143 bp->bio_resid = bp->bio_bcount;
2144 else {
2145 bp->bio_error = ENOSPC;
2146 bp->bio_flags |= BIO_ERROR;
2147 }
2148 goto bad; /* not always bad, but EOF */
2149 }
2150 bp->bio_bcount = (nblocks - blknum) * fdblk;
2151 }
2152 bp->bio_pblkno = blknum;
2153 s = splbio();
2154 bioqdisksort(&fdc->head, bp);
2155 untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
2156 devstat_start_transaction(&fd->device_stats);
2157 device_busy(fd->dev);
2158 fdstart(fdc);
2159 splx(s);
2160 return;
2161
2162bad:
2163 biodone(bp);
2164}
2165
2166/*
2167 * fdstart
2168 *
2169 * We have just queued something. If the controller is not busy
2170 * then simulate the case where it has just finished a command
2171 * So that it (the interrupt routine) looks on the queue for more
2172 * work to do and picks up what we just added.
2173 *
2174 * If the controller is already busy, we need do nothing, as it
2175 * will pick up our work when the present work completes.
2176 */
2177static void
2178fdstart(struct fdc_data *fdc)
2179{
2180 int s;
2181
2182 s = splbio();
2183 if(fdc->state == DEVIDLE)
2184 {
2185 fdc_intr(fdc);
2186 }
2187 splx(s);
2188}
2189
2190static void
2191fd_iotimeout(void *xfdc)
2192{
2193 fdc_p fdc;
2194 int s;
2195
2196 fdc = xfdc;
2197 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
2198
2199 /*
2200 * Due to IBM's brain-dead design, the FDC has a faked ready
2201 * signal, hardwired to ready == true. Thus, any command
2202 * issued if there's no diskette in the drive will _never_
2203 * complete, and must be aborted by resetting the FDC.
2204 * Many thanks, Big Blue!
2205 * The FDC must not be reset directly, since that would
2206 * interfere with the state machine. Instead, pretend that
2207 * the command completed but was invalid. The state machine
2208 * will reset the FDC and retry once.
2209 */
2210 s = splbio();
2211 fdc->status[0] = NE7_ST0_IC_IV;
2212 fdc->flags &= ~FDC_STAT_VALID;
2213 fdc->state = IOTIMEDOUT;
2214 fdc_intr(fdc);
2215 splx(s);
2216}
2217
2218/* Just ensure it has the right spl. */
2219static void
2220fd_pseudointr(void *xfdc)
2221{
2222 int s;
2223
2224 s = splbio();
2225 fdc_intr(xfdc);
2226 splx(s);
2227}
2228
2229/*
2230 * fdc_intr
2231 *
2232 * Keep calling the state machine until it returns a 0.
2233 * Always called at splbio.
2234 */
2235static void
2236fdc_intr(void *xfdc)
2237{
2238 fdc_p fdc = xfdc;
2239 while(fdstate(fdc))
2240 ;
2241}
2242
2243/*
2244 * Magic pseudo-DMA initialization for YE FDC. Sets count and
2245 * direction.
2246 */
2247#define SET_BCDR(fdc,wr,cnt,port) \
2248 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \
2249 ((cnt)-1) & 0xff); \
2250 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
2251 ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
2252
2253/*
2254 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy.
2255 */
2256static int
2257fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
2258{
2259 u_char *cptr = (u_char *)addr;
2260
2261 if (flags == BIO_READ) {
2262 if (fdc->state != PIOREAD) {
2263 fdc->state = PIOREAD;
2264 return(0);
2265 }
2266 SET_BCDR(fdc, 0, count, 0);
2267 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
2268 FDC_YE_DATAPORT, cptr, count);
2269 } else {
2270 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
2271 FDC_YE_DATAPORT, cptr, count);
2272 SET_BCDR(fdc, 0, count, 0);
2273 }
2274 return(1);
2275}
2276
2277/*
2278 * Try figuring out the density of the media present in our device.
2279 */
2280static int
2281fdautoselect(dev_t dev)
2282{
2283 fdu_t fdu;
2284 fd_p fd;
2285 struct fd_type *fdtp;
2286 struct fdc_readid id;
2287 int i, n, oopts, rv;
2288
2289 fdu = FDUNIT(minor(dev));
2290 fd = devclass_get_softc(fd_devclass, fdu);
2291
2292 switch (fd->type) {
2293 default:
2294 return (ENXIO);
2295
2296#ifndef PC98
2297 case FDT_360K:
2298 case FDT_720K:
2299 /* no autoselection on those drives */
2300 fd->ft = fd_native_types + fd->type;
2301 return (0);
2302#endif
2303
2304 case FDT_12M:
2305 fdtp = fd_searchlist_12m;
2306 n = sizeof fd_searchlist_12m / sizeof(struct fd_type);
2307 break;
2308
2309 case FDT_144M:
2310 fdtp = fd_searchlist_144m;
2311 n = sizeof fd_searchlist_144m / sizeof(struct fd_type);
2312 break;
2313
2314#ifndef PC98
2315 case FDT_288M:
2316 fdtp = fd_searchlist_288m;
2317 n = sizeof fd_searchlist_288m / sizeof(struct fd_type);
2318 break;
2319#endif
2320 }
2321
2322 /*
2323 * Try reading sector ID fields, first at cylinder 0, head 0,
2324 * then at cylinder 2, head N. We don't probe cylinder 1,
2325 * since for 5.25in DD media in a HD drive, there are no data
2326 * to read (2 step pulses per media cylinder required). For
2327 * two-sided media, the second probe always goes to head 1, so
2328 * we can tell them apart from single-sided media. As a
2329 * side-effect this means that single-sided media should be
2330 * mentioned in the search list after two-sided media of an
2331 * otherwise identical density. Media with a different number
2332 * of sectors per track but otherwise identical parameters
2333 * cannot be distinguished at all.
2334 *
2335 * If we successfully read an ID field on both cylinders where
2336 * the recorded values match our expectation, we are done.
2337 * Otherwise, we try the next density entry from the table.
2338 *
2339 * Stepping to cylinder 2 has the side-effect of clearing the
2340 * unit attention bit.
2341 */
2342 oopts = fd->options;
2343 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
2344 for (i = 0; i < n; i++, fdtp++) {
2345 fd->ft = fdtp;
2346
2347 id.cyl = id.head = 0;
2348 rv = fdmisccmd(dev, FDBIO_RDSECTID, &id);
2349 if (rv != 0)
2350 continue;
2351 if (id.cyl != 0 || id.head != 0 ||
2352 id.secshift != fdtp->secsize)
2353 continue;
2354 id.cyl = 2;
2355 id.head = fd->ft->heads - 1;
2356 rv = fdmisccmd(dev, FDBIO_RDSECTID, &id);
2357 if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
2358 id.secshift != fdtp->secsize)
2359 continue;
2360 if (rv == 0)
2361 break;
2362 }
2363
2364 fd->options = oopts;
2365 if (i == n) {
2366 if (bootverbose)
2367 device_printf(fd->dev, "autoselection failed\n");
2368 fd->ft = 0;
2369 return (EIO);
2370 } else {
2371 if (bootverbose)
2372 device_printf(fd->dev, "autoselected %d KB medium\n",
2373 fd->ft->size / 2);
2374 return (0);
2375 }
2376}
2377
2378
2379/*
2380 * The controller state machine.
2381 *
2382 * If it returns a non zero value, it should be called again immediately.
2383 */
2384static int
2385fdstate(fdc_p fdc)
2386{
2387 struct fdc_readid *idp;
2388 int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac;
2389 int st0, cyl, st3, idf, ne7cmd, mfm, steptrac;
2390 unsigned long blknum;
2391 fdu_t fdu = fdc->fdu;
2392 fd_p fd;
2393 register struct bio *bp;
2394 struct fd_formb *finfo = NULL;
2395 size_t fdblk;
2396
2397 bp = fdc->bp;
2398 if (bp == NULL) {
2399 bp = bioq_first(&fdc->head);
2400 if (bp != NULL) {
2401 bioq_remove(&fdc->head, bp);
2402 fdc->bp = bp;
2403 }
2404 }
2405 if (bp == NULL) {
2406 /*
2407 * Nothing left for this controller to do,
2408 * force into the IDLE state.
2409 */
2410 fdc->state = DEVIDLE;
2411 if (fdc->fd) {
2412 device_printf(fdc->fdc_dev,
2413 "unexpected valid fd pointer\n");
2414 fdc->fd = (fd_p) 0;
2415 fdc->fdu = -1;
2416 }
2417 TRACE1("[fdc%d IDLE]", fdc->fdcu);
2418 return (0);
2419 }
2420 fdu = FDUNIT(minor(bp->bio_dev));
2421 fd = devclass_get_softc(fd_devclass, fdu);
2422 fdblk = 128 << fd->ft->secsize;
2423 if (fdc->fd && (fd != fdc->fd))
2424 device_printf(fd->dev, "confused fd pointers\n");
2425 read = bp->bio_cmd == BIO_READ;
2426 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
2427 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
2428 if (read)
2429 idf = ISADMA_READ;
2430 else
2431 idf = ISADMA_WRITE;
2432 format = bp->bio_cmd == FDBIO_FORMAT;
2433 rdsectid = bp->bio_cmd == FDBIO_RDSECTID;
2434 if (format)
2435 finfo = (struct fd_formb *)bp->bio_data;
2436 TRACE1("fd%d", fdu);
2437 TRACE1("[%s]", fdstates[fdc->state]);
2438 TRACE1("(0x%x)", fd->flags);
2439 untimeout(fd_turnoff, fd, fd->toffhandle);
2440 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
2441 switch (fdc->state)
2442 {
2443 case DEVIDLE:
2444 case FINDWORK: /* we have found new work */
2445 fdc->retry = 0;
2446 fd->skip = 0;
2447 fdc->fd = fd;
2448 fdc->fdu = fdu;
2449#ifdef PC98
2450 pc98_trans = fd->ft->trans;
2451 if (pc98_trans_prev != pc98_trans) {
2452 int i;
2453 set_density(fdc);
2454 for (i = 0; i < 10; i++) {
2455 outb(0x5f, 0);
2456 outb(0x5f, 0);
2457 }
2458 pc98_trans_prev = pc98_trans;
2459 }
2460 if (pc98_trans != fd->pc98_trans) {
2461 if (fd->type == FDT_144M) {
2462 bus_space_write_1(fdc->sc_fdemsiot,
2463 fdc->sc_fdemsioh,
2464 0,
2465 (fdu << 5) | 0x10 |
2466 (pc98_trans >> 1));
2467 outb(0x5f, 0);
2468 outb(0x5f, 0);
2469 }
2470 fd->pc98_trans = pc98_trans;
2471 }
2472#else
2473 fdc->fdctl_wr(fdc, fd->ft->trans);
2474#endif
2475 TRACE1("[0x%x->FDCTL]", fd->ft->trans);
2476 /*
2477 * If the next drive has a motor startup pending, then
2478 * it will start up in its own good time.
2479 */
2480 if(fd->flags & FD_MOTOR_WAIT) {
2481 fdc->state = MOTORWAIT;
2482 return (0); /* will return later */
2483 }
2484 /*
2485 * Maybe if it's not starting, it SHOULD be starting.
2486 */
2487#ifdef EPSON_NRDISK
2488 if (fdu != nrdu) {
2489 if (!(fd->flags & FD_MOTOR))
2490 {
2491 fdc->state = MOTORWAIT;
2492 fd_turnon(fdu);
2493 return(0);
2494 }
2495 else /* at least make sure we are selected */
2496 {
2497 set_motor(fdcu, fd->fdsu, TURNON);
2498 }
2499 }
2500#else /* !EPSON_NRDISK */
2501 if (!(fd->flags & FD_MOTOR))
2502 {
2503 fdc->state = MOTORWAIT;
2504 fd_turnon(fd);
2505 return (0); /* will return later */
2506 }
2507 else /* at least make sure we are selected */
2508 {
2509 set_motor(fdc, fd->fdsu, TURNON);
2510 }
2511#endif
2512 if (fdc->flags & FDC_NEEDS_RESET) {
2513 fdc->state = RESETCTLR;
2514 fdc->flags &= ~FDC_NEEDS_RESET;
2515 } else
2516 fdc->state = DOSEEK;
2517 return (1); /* will return immediately */
2518
2519 case DOSEEK:
2520#ifdef PC98
2521 blknum = bp->bio_pblkno * DEV_BSIZE / fdblk + fd->skip / fdblk;
2522#else
2523 blknum = bp->bio_pblkno + fd->skip / fdblk;
2524#endif
2525 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2526 if (cylinder == fd->track)
2527 {
2528 fdc->state = SEEKCOMPLETE;
2529 return (1); /* will return immediately */
2530 }
2531#ifdef PC98
2532 pc98_fd_check_ready(fdu);
2533#endif
2534 if (fd_cmd(fdc, 3, NE7CMD_SEEK,
2535 fd->fdsu, cylinder * steptrac, 0))
2536 {
2537 /*
2538 * Seek command not accepted, looks like
2539 * the FDC went off to the Saints...
2540 */
2541 fdc->retry = 6; /* try a reset */
2542 return(retrier(fdc));
2543 }
2544 fd->track = FD_NO_TRACK;
2545 fdc->state = SEEKWAIT;
2546 return(0); /* will return later */
2547
2548 case SEEKWAIT:
2549 /* allow heads to settle */
2550 timeout(fd_pseudointr, fdc, hz / 16);
2551 fdc->state = SEEKCOMPLETE;
2552 return(0); /* will return later */
2553
2554 case SEEKCOMPLETE : /* seek done, start DMA */
2555#ifdef PC98
2556 blknum = bp->bio_pblkno * DEV_BSIZE / fdblk + fd->skip / fdblk;
2557#else
2558 blknum = bp->bio_pblkno + fd->skip / fdblk;
2559#endif
2560 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2561
2562 /* Make sure seek really happened. */
2563 if(fd->track == FD_NO_TRACK) {
2564 int descyl = cylinder * steptrac;
2565 do {
2566 /*
2567 * This might be a "ready changed" interrupt,
2568 * which cannot really happen since the
2569 * RDY pin is hardwired to + 5 volts. This
2570 * generally indicates a "bouncing" intr
2571 * line, so do one of the following:
2572 *
2573 * When running on an enhanced FDC that is
2574 * known to not go stuck after responding
2575 * with INVALID, fetch all interrupt states
2576 * until seeing either an INVALID or a
2577 * real interrupt condition.
2578 *
2579 * When running on a dumb old NE765, give
2580 * up immediately. The controller will
2581 * provide up to four dummy RC interrupt
2582 * conditions right after reset (for the
2583 * corresponding four drives), so this is
2584 * our only chance to get notice that it
2585 * was not the FDC that caused the interrupt.
2586 */
2587 if (fd_sense_int(fdc, &st0, &cyl)
2588 == FD_NOT_VALID)
2589 return (0); /* will return later */
2590 if(fdc->fdct == FDC_NE765
2591 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2592 return (0); /* hope for a real intr */
2593 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2594
2595 if (0 == descyl) {
2596 int failed = 0;
2597 /*
2598 * seek to cyl 0 requested; make sure we are
2599 * really there
2600 */
2601 if (fd_sense_drive_status(fdc, &st3))
2602 failed = 1;
2603#ifdef EPSON_NRDISK
2604 if (fdu == nrdu) st3 = NE7_ST3_T0;
2605#endif /* EPSON_NRDISK */
2606 if ((st3 & NE7_ST3_T0) == 0) {
2607 printf(
2608 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
2609 fdu, st3, NE7_ST3BITS);
2610 failed = 1;
2611 }
2612
2613 if (failed) {
2614 if(fdc->retry < 3)
2615 fdc->retry = 3;
2616 return (retrier(fdc));
2617 }
2618 }
2619#ifdef EPSON_NRDISK
2620 if (fdu == nrdu) cyl = descyl;
2621#endif
2622
2623 if (cyl != descyl) {
2624 printf(
2625 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
2626 fdu, descyl, cyl, st0);
2627 if (fdc->retry < 3)
2628 fdc->retry = 3;
2629 return (retrier(fdc));
2630 }
2631 }
2632
2633 fd->track = cylinder;
2634 if (format)
2635 fd->skip = (char *)&(finfo->fd_formb_cylno(0))
2636 - (char *)finfo;
2637#ifdef EPSON_NRDISK
2638 if (fdu != nrdu) {
2639#endif /* EPSON_NRDISK */
2640 if (!rdsectid && !(fdc->flags & FDC_NODMA))
2641 isa_dmastart(idf, bp->bio_data+fd->skip,
2642 format ? bp->bio_bcount : fdblk, fdc->dmachan);
2643#ifdef PC98
2644 blknum = bp->bio_pblkno * DEV_BSIZE / fdblk + fd->skip / fdblk;
2645#else
2646 blknum = bp->bio_pblkno + fd->skip / fdblk;
2647#endif
2648 sectrac = fd->ft->sectrac;
2649 sec = blknum % (sectrac * fd->ft->heads);
2650 head = sec / sectrac;
2651 sec = sec % sectrac + 1;
2652 if (head != 0 && fd->ft->offset_side2 != 0)
2653 sec += fd->ft->offset_side2;
2654 fd->hddrv = ((head&1)<<2)+fdu;
2655
2656 if(format || !(read || rdsectid))
2657 {
2658 /* make sure the drive is writable */
2659 if(fd_sense_drive_status(fdc, &st3) != 0)
2660 {
2661 /* stuck controller? */
2662 if (!(fdc->flags & FDC_NODMA))
2663 isa_dmadone(idf,
2664 bp->bio_data + fd->skip,
2665 format ? bp->bio_bcount : fdblk,
2666 fdc->dmachan);
2667 fdc->retry = 6; /* reset the beast */
2668 return (retrier(fdc));
2669 }
2670 if(st3 & NE7_ST3_WP)
2671 {
2672 /*
2673 * XXX YES! this is ugly.
2674 * in order to force the current operation
2675 * to fail, we will have to fake an FDC
2676 * error - all error handling is done
2677 * by the retrier()
2678 */
2679 fdc->status[0] = NE7_ST0_IC_AT;
2680 fdc->status[1] = NE7_ST1_NW;
2681 fdc->status[2] = 0;
2682 fdc->status[3] = fd->track;
2683 fdc->status[4] = head;
2684 fdc->status[5] = sec;
2685 fdc->retry = 8; /* break out immediately */
2686 fdc->state = IOTIMEDOUT; /* not really... */
2687 return (1); /* will return immediately */
2688 }
2689 }
2690
2691 if (format) {
2692 ne7cmd = NE7CMD_FORMAT | mfm;
2693 if (fdc->flags & FDC_NODMA) {
2694 /*
2695 * This seems to be necessary for
2696 * whatever obscure reason; if we omit
2697 * it, we end up filling the sector ID
2698 * fields of the newly formatted track
2699 * entirely with garbage, causing
2700 * `wrong cylinder' errors all over
2701 * the place when trying to read them
2702 * back.
2703 *
2704 * Umpf.
2705 */
2706 SET_BCDR(fdc, 1, bp->bio_bcount, 0);
2707
2708 (void)fdcpio(fdc,bp->bio_cmd,
2709 bp->bio_data+fd->skip,
2710 bp->bio_bcount);
2711
2712 }
2713 /* formatting */
2714 if(fd_cmd(fdc, 6, ne7cmd, head << 2 | fdu,
2715 finfo->fd_formb_secshift,
2716 finfo->fd_formb_nsecs,
2717 finfo->fd_formb_gaplen,
2718 finfo->fd_formb_fillbyte, 0)) {
2719 /* controller fell over */
2720 if (!(fdc->flags & FDC_NODMA))
2721 isa_dmadone(idf,
2722 bp->bio_data + fd->skip,
2723 format ? bp->bio_bcount : fdblk,
2724 fdc->dmachan);
2725 fdc->retry = 6;
2726 return (retrier(fdc));
2727 }
2728 } else if (rdsectid) {
2729 ne7cmd = NE7CMD_READID | mfm;
2730 if (fd_cmd(fdc, 2, ne7cmd, head << 2 | fdu, 0)) {
2731 /* controller jamming */
2732 fdc->retry = 6;
2733 return (retrier(fdc));
2734 }
2735 } else {
2736 /* read or write operation */
2737 ne7cmd = (read ? NE7CMD_READ | NE7CMD_SK : NE7CMD_WRITE) | mfm;
2738 if (fdc->flags & FDC_NODMA) {
2739 /*
2740 * This seems to be necessary even when
2741 * reading data.
2742 */
2743 SET_BCDR(fdc, 1, fdblk, 0);
2744
2745 /*
2746 * Perform the write pseudo-DMA before
2747 * the WRITE command is sent.
2748 */
2749 if (!read)
2750 (void)fdcpio(fdc,bp->bio_cmd,
2751 bp->bio_data+fd->skip,
2752 fdblk);
2753 }
2754 if (fd_cmd(fdc, 9,
2755 ne7cmd,
2756 head << 2 | fdu, /* head & unit */
2757 fd->track, /* track */
2758 head,
2759 sec, /* sector + 1 */
2760 fd->ft->secsize, /* sector size */
2761 sectrac, /* sectors/track */
2762 fd->ft->gap, /* gap size */
2763 fd->ft->datalen, /* data length */
2764 0)) {
2765 /* the beast is sleeping again */
2766 if (!(fdc->flags & FDC_NODMA))
2767 isa_dmadone(idf,
2768 bp->bio_data + fd->skip,
2769 format ? bp->bio_bcount : fdblk,
2770 fdc->dmachan);
2771 fdc->retry = 6;
2772 return (retrier(fdc));
2773 }
2774 }
2775 if (!rdsectid && (fdc->flags & FDC_NODMA))
2776 /*
2777 * If this is a read, then simply await interrupt
2778 * before performing PIO.
2779 */
2780 if (read && !fdcpio(fdc,bp->bio_cmd,
2781 bp->bio_data+fd->skip,fdblk)) {
2782 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
2783 return(0); /* will return later */
2784 }
2785
2786 /*
2787 * Write (or format) operation will fall through and
2788 * await completion interrupt.
2789 */
2790 fdc->state = IOCOMPLETE;
2791 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
2792 return (0); /* will return later */
2793#ifdef EPSON_NRDISK
2794 }
2795 else {
2796 nrdblkn = (nrd_t)((unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
2797 + fd->skip/fdblk);
2798 nrd_LED_on();
2799 nrd_addrset(fdblk * nrdblkn);
2800 while (!nrd_check_ready()) DELAY(1);
2801 if (read) epson_insw(P_NRD_DATA,
2802 bp->bio_data + fd->skip,
2803 fdblk / sizeof(short));
2804 else epson_outsw(P_NRD_DATA,
2805 bp->bio_data + fd->skip,
2806 (format ? bp->bio_bcount : fdblk)
2807 / sizeof(short));
2808
2809 blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
2810 + fd->skip/fdblk;
2811 sectrac = fd->ft->sectrac;
2812 sec = blknum % (sectrac * fd->ft->heads);
2813 head = sec / sectrac;
2814 sec = sec % sectrac + 1;
2815 fd->hddrv = ((head&1)<<2)+fdu;
2816
2817 if (nrdsec++ >= nrd_sec())
2818 nrdaddr = (nrd_t)(fd->track * 8 + head * 4);
2819 nrdsec = sec;
2820 fdc->state = IOCOMPLETE;
2821 }
2822#endif
2823
2824 case PIOREAD:
2825 /*
2826 * Actually perform the PIO read. The IOCOMPLETE case
2827 * removes the timeout for us.
2828 */
2829 (void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
2830 fdc->state = IOCOMPLETE;
2831 /* FALLTHROUGH */
2832 case IOCOMPLETE: /* IO done, post-analyze */
2833#ifdef EPSON_NRDISK
2834 if (fdu != nrdu)
2835 untimeout(fd_iotimeout, fdc, fd->tohandle);
2836#else
2837 untimeout(fd_iotimeout, fdc, fd->tohandle);
2838#endif
2839
2840 if (fd_read_status(fdc)) {
2841 if (!rdsectid && !(fdc->flags & FDC_NODMA))
2842 isa_dmadone(idf, bp->bio_data + fd->skip,
2843 format ? bp->bio_bcount : fdblk,
2844 fdc->dmachan);
2845 if (fdc->retry < 6)
2846 fdc->retry = 6; /* force a reset */
2847 return (retrier(fdc));
2848 }
2849
2850 fdc->state = IOTIMEDOUT;
2851
2852 /* FALLTHROUGH */
2853 case IOTIMEDOUT:
2854#ifdef EPSON_NRDISK
2855 if (fdu != nrdu) {
2856#endif /* EPSON_NRDISK */
2857 if (!rdsectid && !(fdc->flags & FDC_NODMA))
2858 isa_dmadone(idf, bp->bio_data + fd->skip,
2859 format ? bp->bio_bcount : fdblk, fdc->dmachan);
2860#ifdef EPSON_NRDISK
2861 }
2862 else nrd_LED_off();
2863#endif /* EPSON_NRDISK */
2864 if (fdc->status[0] & NE7_ST0_IC) {
2865 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2866 && fdc->status[1] & NE7_ST1_OR) {
2867 /*
2868 * DMA overrun. Someone hogged the bus and
2869 * didn't release it in time for the next
2870 * FDC transfer.
2871 *
2872 * We normally restart this without bumping
2873 * the retry counter. However, in case
2874 * something is seriously messed up (like
2875 * broken hardware), we rather limit the
2876 * number of retries so the IO operation
2877 * doesn't block indefinately.
2878 */
2879 if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2880 fdc->state = SEEKCOMPLETE;
2881 return (1);/* will return immediately */
2882 } /* else fall through */
2883 }
2884 if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
2885 && fdc->retry < 6)
2886 fdc->retry = 6; /* force a reset */
2887 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2888 && fdc->status[2] & NE7_ST2_WC
2889 && fdc->retry < 3)
2890 fdc->retry = 3; /* force recalibrate */
2891 return (retrier(fdc));
2892 }
2893 /* All OK */
2894 if (rdsectid) {
2895 /* copy out ID field contents */
2896 idp = (struct fdc_readid *)bp->bio_data;
2897 idp->cyl = fdc->status[3];
2898 idp->head = fdc->status[4];
2899 idp->sec = fdc->status[5];
2900 idp->secshift = fdc->status[6];
2901 }
2902 /* Operation successful, retry DMA overruns again next time. */
2903 fdc->dma_overruns = 0;
2904 fd->skip += fdblk;
2905 if (!rdsectid && !format && fd->skip < bp->bio_bcount) {
2906 /* set up next transfer */
2907 fdc->state = DOSEEK;
2908 } else {
2909 /* ALL DONE */
2910 fd->skip = 0;
2911 bp->bio_resid = 0;
2912 fdc->bp = NULL;
2913 device_unbusy(fd->dev);
2914 biofinish(bp, &fd->device_stats, 0);
2915 fdc->fd = (fd_p) 0;
2916 fdc->fdu = -1;
2917 fdc->state = FINDWORK;
2918 }
2919 return (1); /* will return immediately */
2920
2921 case RESETCTLR:
2922 fdc_reset(fdc);
2923 fdc->retry++;
2924 fdc->state = RESETCOMPLETE;
2925 return (0); /* will return later */
2926
2927 case RESETCOMPLETE:
2928 /*
2929 * Discard all the results from the reset so that they
2930 * can't cause an unexpected interrupt later.
2931 */
2932 for (i = 0; i < 4; i++)
2933 (void)fd_sense_int(fdc, &st0, &cyl);
2934 fdc->state = STARTRECAL;
2935 /* FALLTHROUGH */
2936 case STARTRECAL:
2937#ifdef PC98
2938 pc98_fd_check_ready(fdu);
2939#endif
2940 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2941 /* arrgl */
2942 fdc->retry = 6;
2943 return (retrier(fdc));
2944 }
2945 fdc->state = RECALWAIT;
2946 return (0); /* will return later */
2947
2948 case RECALWAIT:
2949 /* allow heads to settle */
2950 timeout(fd_pseudointr, fdc, hz / 8);
2951 fdc->state = RECALCOMPLETE;
2952 return (0); /* will return later */
2953
2954 case RECALCOMPLETE:
2955 do {
2956 /*
2957 * See SEEKCOMPLETE for a comment on this:
2958 */
2959 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2960 return (0); /* will return later */
2961 if(fdc->fdct == FDC_NE765
2962 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2963 return (0); /* hope for a real intr */
2964 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2965#ifdef EPSON_NRDISK
2966 if (fdu == nrdu) {
2967 st0 = NE7_ST0_IC_NT;
2968 cyl = 0;
2969 }
2970#endif
2971 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
2972 {
2973 if(fdc->retry > 3)
2974 /*
2975 * A recalibrate from beyond cylinder 77
2976 * will "fail" due to the FDC limitations;
2977 * since people used to complain much about
2978 * the failure message, try not logging
2979 * this one if it seems to be the first
2980 * time in a line.
2981 */
2982 printf("fd%d: recal failed ST0 %b cyl %d\n",
2983 fdu, st0, NE7_ST0BITS, cyl);
2984 if(fdc->retry < 3) fdc->retry = 3;
2985 return (retrier(fdc));
2986 }
2987 fd->track = 0;
2988 /* Seek (probably) necessary */
2989 fdc->state = DOSEEK;
2990 return (1); /* will return immediately */
2991
2992 case MOTORWAIT:
2993 if(fd->flags & FD_MOTOR_WAIT)
2994 {
2995 return (0); /* time's not up yet */
2996 }
2997 if (fdc->flags & FDC_NEEDS_RESET) {
2998 fdc->state = RESETCTLR;
2999 fdc->flags &= ~FDC_NEEDS_RESET;
3000 } else
3001 fdc->state = DOSEEK;
3002 return (1); /* will return immediately */
3003
3004 default:
3005 device_printf(fdc->fdc_dev, "unexpected FD int->");
3006 if (fd_read_status(fdc) == 0)
3007 printf("FDC status :%x %x %x %x %x %x %x ",
3008 fdc->status[0],
3009 fdc->status[1],
3010 fdc->status[2],
3011 fdc->status[3],
3012 fdc->status[4],
3013 fdc->status[5],
3014 fdc->status[6] );
3015 else
3016 printf("No status available ");
3017 if (fd_sense_int(fdc, &st0, &cyl) != 0)
3018 {
3019 printf("[controller is dead now]\n");
3020 return (0); /* will return later */
3021 }
3022 printf("ST0 = %x, PCN = %x\n", st0, cyl);
3023 return (0); /* will return later */
3024 }
3025 /* noone should ever get here */
3026}
3027
3028static int
3029retrier(struct fdc_data *fdc)
3030{
3031 struct bio *bp;
3032 struct fd_data *fd;
3033 int fdu;
3034
3035 bp = fdc->bp;
3036
3037 /* XXX shouldn't this be cached somewhere? */
3038 fdu = FDUNIT(minor(bp->bio_dev));
3039 fd = devclass_get_softc(fd_devclass, fdu);
3040 if (fd->options & FDOPT_NORETRY)
3041 goto fail;
3042
3043 switch (fdc->retry) {
3044 case 0: case 1: case 2:
3045 fdc->state = SEEKCOMPLETE;
3046 break;
3047 case 3: case 4: case 5:
3048 fdc->state = STARTRECAL;
3049 break;
3050 case 6:
3051 fdc->state = RESETCTLR;
3052 break;
3053 case 7:
3054 break;
3055 default:
3056 fail:
3057 if ((fd->options & FDOPT_NOERRLOG) == 0) {
3058 disk_err(bp, "hard error",
3059 fdc->fd->skip / DEV_BSIZE, 0);
3060 if (fdc->flags & FDC_STAT_VALID) {
3061 printf(
3062 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
3063 fdc->status[0], NE7_ST0BITS,
3064 fdc->status[1], NE7_ST1BITS,
3065 fdc->status[2], NE7_ST2BITS,
3066 fdc->status[3], fdc->status[4],
3067 fdc->status[5]);
3068 }
3069 else
3070 printf(" (No status)\n");
3071 }
3072 if ((fd->options & FDOPT_NOERROR) == 0) {
3073 bp->bio_flags |= BIO_ERROR;
3074 bp->bio_error = EIO;
3075 bp->bio_resid = bp->bio_bcount - fdc->fd->skip;
3076 } else
3077 bp->bio_resid = 0;
3078 fdc->bp = NULL;
3079 fdc->fd->skip = 0;
3080 device_unbusy(fd->dev);
3081 biofinish(bp, &fdc->fd->device_stats, 0);
3082 fdc->state = FINDWORK;
3083 fdc->flags |= FDC_NEEDS_RESET;
3084 fdc->fd = (fd_p) 0;
3085 fdc->fdu = -1;
3086 return (1);
3087 }
3088 fdc->retry++;
3089 return (1);
3090}
3091
3092static void
3093fdbiodone(struct bio *bp)
3094{
3095 wakeup(bp);
3096}
3097
3098static int
3099fdmisccmd(dev_t dev, u_int cmd, void *data)
3100{
3101 fdu_t fdu;
3102 fd_p fd;
3103 struct bio *bp;
3104 struct fd_formb *finfo;
3105 struct fdc_readid *idfield;
3106 size_t fdblk;
3107 int error;
3108
3109 fdu = FDUNIT(minor(dev));
3110 fd = devclass_get_softc(fd_devclass, fdu);
3111 fdblk = 128 << fd->ft->secsize;
3112 finfo = (struct fd_formb *)data;
3113 idfield = (struct fdc_readid *)data;
3114
3115 bp = malloc(sizeof(struct bio), M_TEMP, M_ZERO);
3116
3117 /*
3118 * Set up a bio request for fdstrategy(). bio_blkno is faked
3119 * so that fdstrategy() will seek to the the requested
3120 * cylinder, and use the desired head.
3121 */
3122 bp->bio_cmd = cmd;
3123 if (cmd == FDBIO_FORMAT) {
3124 bp->bio_blkno =
3125 (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) +
3126 finfo->head * fd->ft->sectrac) *
3127 fdblk / DEV_BSIZE;
3128 bp->bio_bcount = sizeof(struct fd_idfield_data) *
3129 finfo->fd_formb_nsecs;
3130 } else if (cmd == FDBIO_RDSECTID) {
3131 bp->bio_blkno =
3132 (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) +
3133 idfield->head * fd->ft->sectrac) *
3134 fdblk / DEV_BSIZE;
3135 bp->bio_bcount = sizeof(struct fdc_readid);
3136 } else
3137 panic("wrong cmd in fdmisccmd()");
3138 bp->bio_data = data;
3139 bp->bio_dev = dev;
3140 bp->bio_done = fdbiodone;
3141 bp->bio_flags = 0;
3142
3143 /* Now run the command. */
3144 fdstrategy(bp);
3145 error = biowait(bp, "fdcmd");
3146
3147 free(bp, M_TEMP);
3148 return (error);
3149}
3150
3151static int
3152fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
3153{
3154 fdu_t fdu;
3155 fd_p fd;
3156 struct fdc_status *fsp;
3157 struct fdc_readid *rid;
3158 size_t fdblk;
3159 int error, type;
3160
3161 fdu = FDUNIT(minor(dev));
3162 type = FDTYPE(minor(dev));
3163 fd = devclass_get_softc(fd_devclass, fdu);
3164
3165#ifdef PC98
3166 pc98_fd_check_ready(fdu);
3167#endif
3168
3169 /*
3170 * First, handle everything that could be done with
3171 * FD_NONBLOCK still being set.
3172 */
3173 switch (cmd) {
3174
3175 case DIOCGMEDIASIZE:
3176 *(off_t *)addr = (128 << (fd->ft->secsize)) * fd->ft->size;
3177 return (0);
3178
3179 case DIOCGSECTORSIZE:
3180 *(u_int *)addr = 128 << (fd->ft->secsize);
3181 return (0);
3182
3183 case FIONBIO:
3184 if (*(int *)addr != 0)
3185 fd->flags |= FD_NONBLOCK;
3186 else {
3187 if (fd->ft == 0) {
3188 /*
3189 * No drive type has been selected yet,
3190 * cannot turn FNONBLOCK off.
3191 */
3192 return (EINVAL);
3193 }
3194 fd->flags &= ~FD_NONBLOCK;
3195 }
3196 return (0);
3197
3198 case FIOASYNC:
3199 /* keep the generic fcntl() code happy */
3200 return (0);
3201
3202 case FD_GTYPE: /* get drive type */
3203 if (fd->ft == 0)
3204 /* no type known yet, return the native type */
3205 *(struct fd_type *)addr = fd_native_types[fd->type];
3206 else
3207 *(struct fd_type *)addr = *fd->ft;
3208 return (0);
3209
3210 case FD_STYPE: /* set drive type */
3211 if (type == 0) {
3212 /*
3213 * Allow setting drive type temporarily iff
3214 * currently unset. Used for fdformat so any
3215 * user can set it, and then start formatting.
3216 */
3217 if (fd->ft)
3218 return (EINVAL); /* already set */
3219 fd->ft = fd->fts;
3220 *fd->ft = *(struct fd_type *)addr;
3221 fd->flags |= FD_UA;
3222 } else {
3223 /*
3224 * Set density definition permanently. Only
3225 * allow for superuser.
3226 */
3227 if (suser(td) != 0)
3228 return (EPERM);
3229 fd->fts[type] = *(struct fd_type *)addr;
3230 }
3231 return (0);
3232
3233 case FD_GOPTS: /* get drive options */
3234 *(int *)addr = fd->options + (type == 0? FDOPT_AUTOSEL: 0);
3235 return (0);
3236
3237 case FD_SOPTS: /* set drive options */
3238 fd->options = *(int *)addr & ~FDOPT_AUTOSEL;
3239 return (0);
3240
3241#ifdef FDC_DEBUG
3242 case FD_DEBUG:
3243 if ((fd_debug != 0) != (*(int *)addr != 0)) {
3244 fd_debug = (*(int *)addr != 0);
3245 printf("fd%d: debugging turned %s\n",
3246 fd->fdu, fd_debug ? "on" : "off");
3247 }
3248 return (0);
3249#endif
3250
3251 case FD_CLRERR:
3252 if (suser(td) != 0)
3253 return (EPERM);
3254 fd->fdc->fdc_errs = 0;
3255 return (0);
3256
3257 case FD_GSTAT:
3258 fsp = (struct fdc_status *)addr;
3259 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
3260 return (EINVAL);
3261 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
3262 return (0);
3263
3264 case FD_GDTYPE:
3265 *(enum fd_drivetype *)addr = fd->type;
3266 return (0);
3267 }
3268
3269 /*
3270 * Now handle everything else. Make sure we have a valid
3271 * drive type.
3272 */
3273 if (fd->flags & FD_NONBLOCK)
3274 return (EAGAIN);
3275 if (fd->ft == 0)
3276 return (ENXIO);
3277 fdblk = 128 << fd->ft->secsize;
3278 error = 0;
3279
3280 switch (cmd) {
3281
3282 case FD_FORM:
3283 if ((flag & FWRITE) == 0)
3284 return (EBADF); /* must be opened for writing */
3285 if (((struct fd_formb *)addr)->format_version !=
3286 FD_FORMAT_VERSION)
3287 return (EINVAL); /* wrong version of formatting prog */
3288 error = fdmisccmd(dev, FDBIO_FORMAT, addr);
3289 break;
3290
3291 case FD_GTYPE: /* get drive type */
3292 *(struct fd_type *)addr = *fd->ft;
3293 break;
3294
3295 case FD_STYPE: /* set drive type */
3296 /* this is considered harmful; only allow for superuser */
3297 if (suser(td) != 0)
3298 return (EPERM);
3299 *fd->ft = *(struct fd_type *)addr;
3300 break;
3301
3302 case FD_GOPTS: /* get drive options */
3303 *(int *)addr = fd->options;
3304 break;
3305
3306 case FD_SOPTS: /* set drive options */
3307 fd->options = *(int *)addr;
3308 break;
3309
3310#ifdef FDC_DEBUG
3311 case FD_DEBUG:
3312 if ((fd_debug != 0) != (*(int *)addr != 0)) {
3313 fd_debug = (*(int *)addr != 0);
3314 printf("fd%d: debugging turned %s\n",
3315 fd->fdu, fd_debug ? "on" : "off");
3316 }
3317 break;
3318#endif
3319
3320 case FD_CLRERR:
3321 if (suser(td) != 0)
3322 return (EPERM);
3323 fd->fdc->fdc_errs = 0;
3324 break;
3325
3326 case FD_GSTAT:
3327 fsp = (struct fdc_status *)addr;
3328 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
3329 return (EINVAL);
3330 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
3331 break;
3332
3333 case FD_READID:
3334 rid = (struct fdc_readid *)addr;
3335 if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD)
3336 return (EINVAL);
3337 error = fdmisccmd(dev, FDBIO_RDSECTID, addr);
3338 break;
3339
3340 default:
3341 error = ENOTTY;
3342 break;
3343 }
3344 return (error);
3345}