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