1*0f9e9ec2Sjsg /* $OpenBSD: fd.c,v 1.53 2024/05/13 01:15:50 jsg Exp $ */
2323e9233Smiod /* $NetBSD: fd.c,v 1.112 2003/08/07 16:29:35 agc Exp $ */
3323e9233Smiod
4323e9233Smiod /*-
5323e9233Smiod * Copyright (c) 2000 The NetBSD Foundation, Inc.
6323e9233Smiod * All rights reserved.
7323e9233Smiod *
8323e9233Smiod * This code is derived from software contributed to The NetBSD Foundation
9323e9233Smiod * by Paul Kranenburg.
10323e9233Smiod *
11323e9233Smiod * Redistribution and use in source and binary forms, with or without
12323e9233Smiod * modification, are permitted provided that the following conditions
13323e9233Smiod * are met:
14323e9233Smiod * 1. Redistributions of source code must retain the above copyright
15323e9233Smiod * notice, this list of conditions and the following disclaimer.
16323e9233Smiod * 2. Redistributions in binary form must reproduce the above copyright
17323e9233Smiod * notice, this list of conditions and the following disclaimer in the
18323e9233Smiod * documentation and/or other materials provided with the distribution.
19323e9233Smiod *
20323e9233Smiod * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21323e9233Smiod * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22323e9233Smiod * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23323e9233Smiod * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24323e9233Smiod * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25323e9233Smiod * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26323e9233Smiod * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27323e9233Smiod * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28323e9233Smiod * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29323e9233Smiod * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30323e9233Smiod * POSSIBILITY OF SUCH DAMAGE.
31323e9233Smiod */
32323e9233Smiod
33323e9233Smiod /*-
34323e9233Smiod * Copyright (c) 1990 The Regents of the University of California.
35323e9233Smiod * All rights reserved.
36323e9233Smiod *
37323e9233Smiod * This code is derived from software contributed to Berkeley by
38323e9233Smiod * Don Ahn.
39323e9233Smiod *
40323e9233Smiod * Redistribution and use in source and binary forms, with or without
41323e9233Smiod * modification, are permitted provided that the following conditions
42323e9233Smiod * are met:
43323e9233Smiod * 1. Redistributions of source code must retain the above copyright
44323e9233Smiod * notice, this list of conditions and the following disclaimer.
45323e9233Smiod * 2. Redistributions in binary form must reproduce the above copyright
46323e9233Smiod * notice, this list of conditions and the following disclaimer in the
47323e9233Smiod * documentation and/or other materials provided with the distribution.
48323e9233Smiod * 3. Neither the name of the University nor the names of its contributors
49323e9233Smiod * may be used to endorse or promote products derived from this software
50323e9233Smiod * without specific prior written permission.
51323e9233Smiod *
52323e9233Smiod * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53323e9233Smiod * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54323e9233Smiod * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55323e9233Smiod * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56323e9233Smiod * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57323e9233Smiod * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58323e9233Smiod * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59323e9233Smiod * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60323e9233Smiod * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61323e9233Smiod * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62323e9233Smiod * SUCH DAMAGE.
63323e9233Smiod *
64323e9233Smiod * @(#)fd.c 7.4 (Berkeley) 5/25/91
65323e9233Smiod */
66323e9233Smiod
67323e9233Smiod /*-
68323e9233Smiod * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.
69323e9233Smiod * Copyright (c) 1995 Paul Kranenburg.
70323e9233Smiod *
71323e9233Smiod * This code is derived from software contributed to Berkeley by
72323e9233Smiod * Don Ahn.
73323e9233Smiod *
74323e9233Smiod * Redistribution and use in source and binary forms, with or without
75323e9233Smiod * modification, are permitted provided that the following conditions
76323e9233Smiod * are met:
77323e9233Smiod * 1. Redistributions of source code must retain the above copyright
78323e9233Smiod * notice, this list of conditions and the following disclaimer.
79323e9233Smiod * 2. Redistributions in binary form must reproduce the above copyright
80323e9233Smiod * notice, this list of conditions and the following disclaimer in the
81323e9233Smiod * documentation and/or other materials provided with the distribution.
82323e9233Smiod * 3. All advertising materials mentioning features or use of this software
83323e9233Smiod * must display the following acknowledgement:
84323e9233Smiod * This product includes software developed by the University of
85323e9233Smiod * California, Berkeley and its contributors.
86323e9233Smiod * 4. Neither the name of the University nor the names of its contributors
87323e9233Smiod * may be used to endorse or promote products derived from this software
88323e9233Smiod * without specific prior written permission.
89323e9233Smiod *
90323e9233Smiod * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
91323e9233Smiod * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
92323e9233Smiod * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
93323e9233Smiod * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
94323e9233Smiod * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
95323e9233Smiod * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
96323e9233Smiod * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
97323e9233Smiod * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
98323e9233Smiod * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
99323e9233Smiod * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
100323e9233Smiod * SUCH DAMAGE.
101323e9233Smiod *
102323e9233Smiod * @(#)fd.c 7.4 (Berkeley) 5/25/91
103323e9233Smiod */
104323e9233Smiod
105323e9233Smiod #include <sys/param.h>
106323e9233Smiod #include <sys/systm.h>
107323e9233Smiod #include <sys/timeout.h>
108323e9233Smiod #include <sys/kernel.h>
109c0cd3489Sguenther #include <sys/fcntl.h>
110323e9233Smiod #include <sys/ioctl.h>
111323e9233Smiod #include <sys/conf.h>
112323e9233Smiod #include <sys/device.h>
113323e9233Smiod #include <sys/disklabel.h>
114323e9233Smiod #include <sys/disk.h>
115323e9233Smiod #include <sys/buf.h>
116323e9233Smiod #include <sys/malloc.h>
117323e9233Smiod #include <sys/proc.h>
118323e9233Smiod #include <sys/uio.h>
119323e9233Smiod #include <sys/mtio.h>
120323e9233Smiod #include <sys/stat.h>
121323e9233Smiod #include <sys/syslog.h>
122323e9233Smiod #include <sys/queue.h>
123da7f2c0cSdlg #include <sys/dkio.h>
124323e9233Smiod
125323e9233Smiod #include <dev/cons.h>
126323e9233Smiod
127323e9233Smiod #include <uvm/uvm_extern.h>
128323e9233Smiod
129323e9233Smiod #include <machine/autoconf.h>
130323e9233Smiod #include <machine/conf.h>
131323e9233Smiod #include <machine/intr.h>
132323e9233Smiod #include <machine/ioctl_fd.h>
133323e9233Smiod
134323e9233Smiod #include <sparc64/dev/auxioreg.h>
135323e9233Smiod #include <sparc64/dev/auxiovar.h>
136323e9233Smiod #include <sparc64/dev/ebusreg.h>
137323e9233Smiod #include <sparc64/dev/ebusvar.h>
138323e9233Smiod #include <sparc64/dev/fdreg.h>
139323e9233Smiod #include <sparc64/dev/fdvar.h>
140323e9233Smiod
141323e9233Smiod #define FDUNIT(dev) ((minor(dev) / MAXPARTITIONS) / 8)
142323e9233Smiod #define FDTYPE(dev) ((minor(dev) / MAXPARTITIONS) % 8)
143323e9233Smiod
144323e9233Smiod #define FTC_FLIP \
145323e9233Smiod do { \
146323e9233Smiod auxio_fd_control(AUXIO_LED_FTC); \
147323e9233Smiod auxio_fd_control(0); \
148323e9233Smiod } while (0)
149323e9233Smiod
150323e9233Smiod /* XXX misuse a flag to identify format operation */
151323e9233Smiod #define B_FORMAT B_XXX
152323e9233Smiod
153323e9233Smiod #ifdef FD_DEBUG
154323e9233Smiod int fdc_debug = 0;
155323e9233Smiod #endif
156323e9233Smiod
157323e9233Smiod enum fdc_state {
158323e9233Smiod DEVIDLE = 0,
159323e9233Smiod MOTORWAIT, /* 1 */
160323e9233Smiod DOSEEK, /* 2 */
161323e9233Smiod SEEKWAIT, /* 3 */
162323e9233Smiod SEEKTIMEDOUT, /* 4 */
163323e9233Smiod SEEKCOMPLETE, /* 5 */
164323e9233Smiod DOIO, /* 6 */
165323e9233Smiod IOCOMPLETE, /* 7 */
166323e9233Smiod IOTIMEDOUT, /* 8 */
167323e9233Smiod IOCLEANUPWAIT, /* 9 */
168323e9233Smiod IOCLEANUPTIMEDOUT,/*10 */
169323e9233Smiod DORESET, /* 11 */
170323e9233Smiod RESETCOMPLETE, /* 12 */
171323e9233Smiod RESETTIMEDOUT, /* 13 */
172323e9233Smiod DORECAL, /* 14 */
173323e9233Smiod RECALWAIT, /* 15 */
174323e9233Smiod RECALTIMEDOUT, /* 16 */
175323e9233Smiod RECALCOMPLETE, /* 17 */
176323e9233Smiod DODSKCHG, /* 18 */
177323e9233Smiod DSKCHGWAIT, /* 19 */
178323e9233Smiod DSKCHGTIMEDOUT, /* 20 */
179323e9233Smiod };
180323e9233Smiod
181323e9233Smiod /* software state, per controller */
182323e9233Smiod struct fdc_softc {
183323e9233Smiod struct device sc_dev; /* boilerplate */
184323e9233Smiod bus_space_tag_t sc_bustag;
185323e9233Smiod
186323e9233Smiod struct timeout fdctimeout_to;
187323e9233Smiod struct timeout fdcpseudointr_to;
188323e9233Smiod
189323e9233Smiod struct fd_softc *sc_fd[4]; /* pointers to children */
190323e9233Smiod TAILQ_HEAD(drivehead, fd_softc) sc_drives;
191323e9233Smiod enum fdc_state sc_state;
192323e9233Smiod int sc_flags;
193323e9233Smiod #define FDC_EBUS 0x01
194323e9233Smiod #define FDC_NEEDHEADSETTLE 0x02
195323e9233Smiod #define FDC_EIS 0x04
196323e9233Smiod #define FDC_NEEDMOTORWAIT 0x08
197323e9233Smiod #define FDC_NOEJECT 0x10
198323e9233Smiod int sc_errors; /* number of retries so far */
199323e9233Smiod int sc_overruns; /* number of DMA overruns */
200323e9233Smiod int sc_cfg; /* current configuration */
201323e9233Smiod struct fdcio sc_io;
202323e9233Smiod #define sc_handle sc_io.fdcio_handle
203323e9233Smiod #define sc_itask sc_io.fdcio_itask
204323e9233Smiod #define sc_istatus sc_io.fdcio_istatus
205323e9233Smiod #define sc_data sc_io.fdcio_data
206323e9233Smiod #define sc_tc sc_io.fdcio_tc
207323e9233Smiod #define sc_nstat sc_io.fdcio_nstat
208323e9233Smiod #define sc_status sc_io.fdcio_status
209323e9233Smiod
210323e9233Smiod void *sc_sicookie; /* softintr(9) cookie */
211323e9233Smiod };
212323e9233Smiod
213323e9233Smiod /* controller driver configuration */
214323e9233Smiod int fdcmatch_sbus(struct device *, void *, void *);
215323e9233Smiod int fdcmatch_ebus(struct device *, void *, void *);
216323e9233Smiod void fdcattach_sbus(struct device *, struct device *, void *);
217323e9233Smiod void fdcattach_ebus(struct device *, struct device *, void *);
218323e9233Smiod
219323e9233Smiod int fdcattach(struct fdc_softc *, int);
220323e9233Smiod
221eb7eaf8dSmpi const struct cfattach fdc_sbus_ca = {
222323e9233Smiod sizeof(struct fdc_softc), fdcmatch_sbus, fdcattach_sbus
223323e9233Smiod };
224323e9233Smiod
225eb7eaf8dSmpi const struct cfattach fdc_ebus_ca = {
226323e9233Smiod sizeof(struct fdc_softc), fdcmatch_ebus, fdcattach_ebus
227323e9233Smiod };
228323e9233Smiod
229323e9233Smiod struct cfdriver fdc_cd = {
230323e9233Smiod NULL, "fdc", DV_DULL
231323e9233Smiod };
232323e9233Smiod
233323e9233Smiod __inline struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
234323e9233Smiod
235323e9233Smiod /* The order of entries in the following table is important -- BEWARE! */
236323e9233Smiod struct fd_type fd_types[] = {
237d59b4371Smiod { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS, "1.44MB" }, /* 1.44MB diskette */
238323e9233Smiod { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB" }, /* 3.5" 720kB diskette */
239323e9233Smiod { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x" }, /* 360kB in 720kB drive */
240323e9233Smiod { 8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS, "1.2MB/NEC" } /* 1.2 MB japanese format */
241323e9233Smiod };
242323e9233Smiod
243323e9233Smiod /* software state, per disk (with up to 4 disks per ctlr) */
244323e9233Smiod struct fd_softc {
245323e9233Smiod struct device sc_dv; /* generic device info */
246323e9233Smiod struct disk sc_dk; /* generic disk info */
247323e9233Smiod
248323e9233Smiod struct fd_type *sc_deftype; /* default type descriptor */
249323e9233Smiod struct fd_type *sc_type; /* current type descriptor */
250323e9233Smiod
251323e9233Smiod struct timeout sc_motoron_to;
252323e9233Smiod struct timeout sc_motoroff_to;
253323e9233Smiod
2541abdbfdeSderaadt daddr_t sc_blkno; /* starting block number */
255323e9233Smiod int sc_bcount; /* byte count left */
256323e9233Smiod int sc_skip; /* bytes already transferred */
257323e9233Smiod int sc_nblks; /* number of blocks currently transferring */
258323e9233Smiod int sc_nbytes; /* number of bytes currently transferring */
259323e9233Smiod
260323e9233Smiod int sc_drive; /* physical unit number */
261323e9233Smiod int sc_flags;
262323e9233Smiod #define FD_OPEN 0x01 /* it's open */
263323e9233Smiod #define FD_MOTOR 0x02 /* motor should be on */
264323e9233Smiod #define FD_MOTOR_WAIT 0x04 /* motor coming up */
265323e9233Smiod int sc_cylin; /* where we think the head is */
266323e9233Smiod int sc_opts; /* user-set options */
267323e9233Smiod
268323e9233Smiod TAILQ_ENTRY(fd_softc) sc_drivechain;
269323e9233Smiod int sc_ops; /* I/O ops since last switch */
270274ffc92Sdlg struct bufq sc_bufq; /* pending I/O requests */
271274ffc92Sdlg struct buf *sc_bp; /* current I/O */
272323e9233Smiod };
273323e9233Smiod
274323e9233Smiod /* floppy driver configuration */
275323e9233Smiod int fdmatch(struct device *, void *, void *);
276323e9233Smiod void fdattach(struct device *, struct device *, void *);
277c06fda6dSderaadt int fdactivate(struct device *, int);
278323e9233Smiod
279eb7eaf8dSmpi const struct cfattach fd_ca = {
280c06fda6dSderaadt sizeof(struct fd_softc), fdmatch, fdattach,
281c06fda6dSderaadt NULL, fdactivate
282323e9233Smiod };
283323e9233Smiod
284323e9233Smiod struct cfdriver fd_cd = {
285323e9233Smiod NULL, "fd", DV_DISK
286323e9233Smiod };
287323e9233Smiod
288df591ed6Sderaadt int fdgetdisklabel(dev_t, struct fd_softc *, struct disklabel *, int);
289323e9233Smiod void fdstrategy(struct buf *);
290323e9233Smiod void fdstart(struct fd_softc *);
291323e9233Smiod int fdprint(void *, const char *);
292323e9233Smiod
293323e9233Smiod struct fd_type *fd_nvtotype(char *, int, int);
294323e9233Smiod void fd_set_motor(struct fdc_softc *fdc);
295323e9233Smiod void fd_motor_off(void *arg);
296323e9233Smiod void fd_motor_on(void *arg);
297323e9233Smiod int fdcresult(struct fdc_softc *fdc);
298323e9233Smiod int fdc_wrfifo(struct fdc_softc *fdc, u_char x);
299323e9233Smiod void fdcstart(struct fdc_softc *fdc);
300323e9233Smiod void fdcstatus(struct fdc_softc *fdc, char *s);
301323e9233Smiod void fdc_reset(struct fdc_softc *fdc);
302323e9233Smiod int fdc_diskchange(struct fdc_softc *fdc);
303323e9233Smiod void fdctimeout(void *arg);
304323e9233Smiod void fdcpseudointr(void *arg);
305323e9233Smiod int fdchwintr(void *);
306323e9233Smiod void fdcswintr(void *);
307323e9233Smiod int fdcstate(struct fdc_softc *);
308323e9233Smiod void fdcretry(struct fdc_softc *fdc);
309323e9233Smiod void fdfinish(struct fd_softc *fd, struct buf *bp);
310323e9233Smiod int fdformat(dev_t, struct fd_formb *, struct proc *);
311323e9233Smiod void fd_do_eject(struct fd_softc *);
312323e9233Smiod static int fdconf(struct fdc_softc *);
313323e9233Smiod
314323e9233Smiod int
fdcmatch_sbus(struct device * parent,void * match,void * aux)315bd535c51Sclaudio fdcmatch_sbus(struct device *parent, void *match, void *aux)
316323e9233Smiod {
317323e9233Smiod struct sbus_attach_args *sa = aux;
318323e9233Smiod
319323e9233Smiod return (strcmp("SUNW,fdtwo", sa->sa_name) == 0);
320323e9233Smiod }
321323e9233Smiod
322323e9233Smiod void
fdcattach_sbus(struct device * parent,struct device * self,void * aux)323bd535c51Sclaudio fdcattach_sbus(struct device *parent, struct device *self, void *aux)
324323e9233Smiod {
325323e9233Smiod struct fdc_softc *fdc = (void *)self;
326323e9233Smiod struct sbus_attach_args *sa = aux;
327323e9233Smiod
328323e9233Smiod if (sa->sa_nintr == 0) {
329323e9233Smiod printf(": no interrupt line configured\n");
330323e9233Smiod return;
331323e9233Smiod }
332323e9233Smiod
333323e9233Smiod if (auxio_fd_control(0) != 0) {
334323e9233Smiod printf(": can't attach before auxio\n");
335323e9233Smiod return;
336323e9233Smiod }
337323e9233Smiod
338323e9233Smiod fdc->sc_bustag = sa->sa_bustag;
339323e9233Smiod
340323e9233Smiod if (sbus_bus_map(sa->sa_bustag,
341323e9233Smiod sa->sa_slot, sa->sa_offset, sa->sa_size,
342323e9233Smiod BUS_SPACE_MAP_LINEAR, 0, &fdc->sc_handle) != 0) {
343323e9233Smiod printf(": cannot map control registers\n");
344323e9233Smiod return;
345323e9233Smiod }
346323e9233Smiod
347323e9233Smiod if (strcmp(getpropstring(sa->sa_node, "status"), "disabled") == 0) {
348323e9233Smiod printf(": no drives attached\n");
349323e9233Smiod return;
350323e9233Smiod }
351323e9233Smiod
3525d1cc9b2Smiod if (getproplen(sa->sa_node, "manual") >= 0)
353323e9233Smiod fdc->sc_flags |= FDC_NOEJECT;
354323e9233Smiod
355323e9233Smiod if (fdcattach(fdc, sa->sa_pri) != 0)
356323e9233Smiod bus_space_unmap(sa->sa_bustag, fdc->sc_handle, sa->sa_size);
357323e9233Smiod }
358323e9233Smiod
359323e9233Smiod int
fdcmatch_ebus(struct device * parent,void * match,void * aux)360bd535c51Sclaudio fdcmatch_ebus(struct device *parent, void *match, void *aux)
361323e9233Smiod {
362323e9233Smiod struct ebus_attach_args *ea = aux;
363323e9233Smiod
364323e9233Smiod return (strcmp("fdthree", ea->ea_name) == 0);
365323e9233Smiod }
366323e9233Smiod
367323e9233Smiod void
fdcattach_ebus(struct device * parent,struct device * self,void * aux)368bd535c51Sclaudio fdcattach_ebus(struct device *parent, struct device *self, void *aux)
369323e9233Smiod {
370323e9233Smiod struct fdc_softc *fdc = (void *)self;
371323e9233Smiod struct ebus_attach_args *ea = aux;
372323e9233Smiod
373323e9233Smiod if (ea->ea_nintrs == 0) {
374323e9233Smiod printf(": no interrupt line configured\n");
375323e9233Smiod return;
376323e9233Smiod }
377323e9233Smiod
378323e9233Smiod if (ea->ea_nregs < 3) {
3791d7045e0Smiod printf(": expected 3 registers, only got %d\n",
380323e9233Smiod ea->ea_nregs);
381323e9233Smiod return;
382323e9233Smiod }
383323e9233Smiod
384323e9233Smiod if (ea->ea_nvaddrs > 0) {
385323e9233Smiod if (bus_space_map(ea->ea_memtag, ea->ea_vaddrs[0], 0,
386323e9233Smiod BUS_SPACE_MAP_PROMADDRESS, &fdc->sc_handle) != 0) {
387323e9233Smiod printf(": can't map control registers\n");
388323e9233Smiod return;
389323e9233Smiod }
390323e9233Smiod fdc->sc_bustag = ea->ea_memtag;
391323e9233Smiod } else if (ebus_bus_map(ea->ea_memtag, 0,
392323e9233Smiod EBUS_PADDR_FROM_REG(&ea->ea_regs[0]),
393323e9233Smiod ea->ea_regs[0].size, 0, 0, &fdc->sc_handle) == 0) {
394323e9233Smiod fdc->sc_bustag = ea->ea_memtag;
395323e9233Smiod } else if (ebus_bus_map(ea->ea_iotag, 0,
396323e9233Smiod EBUS_PADDR_FROM_REG(&ea->ea_regs[0]),
397323e9233Smiod ea->ea_regs[0].size, 0, 0, &fdc->sc_handle) == 0) {
398323e9233Smiod fdc->sc_bustag = ea->ea_iotag;
399323e9233Smiod } else {
400323e9233Smiod printf(": can't map control registers\n");
401323e9233Smiod return;
402323e9233Smiod }
403323e9233Smiod
404323e9233Smiod if (strcmp(getpropstring(ea->ea_node, "status"), "disabled") == 0) {
405323e9233Smiod printf(": no drives attached\n");
406323e9233Smiod return;
407323e9233Smiod }
408323e9233Smiod
409323e9233Smiod fdc->sc_flags |= FDC_EBUS;
410323e9233Smiod
4115d1cc9b2Smiod if (getproplen(ea->ea_node, "manual") >= 0)
412323e9233Smiod fdc->sc_flags |= FDC_NOEJECT;
413323e9233Smiod
414323e9233Smiod /* XXX unmapping if it fails */
415323e9233Smiod fdcattach(fdc, ea->ea_intrs[0]);
416323e9233Smiod }
417323e9233Smiod
418323e9233Smiod /*
419323e9233Smiod * Arguments passed between fdcattach and fdprobe.
420323e9233Smiod */
421323e9233Smiod struct fdc_attach_args {
422323e9233Smiod int fa_drive;
423323e9233Smiod struct fd_type *fa_deftype;
424323e9233Smiod };
425323e9233Smiod
426323e9233Smiod /*
427323e9233Smiod * Print the location of a disk drive (called just before attaching the
428323e9233Smiod * the drive). If `fdc' is not NULL, the drive was found but was not
429323e9233Smiod * in the system config file; print the drive name as well.
430323e9233Smiod * Return QUIET (config_find ignores this if the device was configured) to
431323e9233Smiod * avoid printing `fdN not configured' messages.
432323e9233Smiod */
433323e9233Smiod int
fdprint(void * aux,const char * fdc)434bd535c51Sclaudio fdprint(void *aux, const char *fdc)
435323e9233Smiod {
436323e9233Smiod register struct fdc_attach_args *fa = aux;
437323e9233Smiod
438323e9233Smiod if (!fdc)
439323e9233Smiod printf(" drive %d", fa->fa_drive);
440323e9233Smiod return (QUIET);
441323e9233Smiod }
442323e9233Smiod
443323e9233Smiod /*
444323e9233Smiod * Configure several parameters and features on the FDC.
445323e9233Smiod * Return 0 on success.
446323e9233Smiod */
447323e9233Smiod static int
fdconf(struct fdc_softc * fdc)448bd535c51Sclaudio fdconf(struct fdc_softc *fdc)
449323e9233Smiod {
450323e9233Smiod int vroom;
451323e9233Smiod
452323e9233Smiod if (fdc_wrfifo(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
453323e9233Smiod return (-1);
454323e9233Smiod
455323e9233Smiod /*
456323e9233Smiod * dumpreg[7] seems to be a motor-off timeout; set it to whatever
457323e9233Smiod * the PROM thinks is appropriate.
458323e9233Smiod */
459323e9233Smiod if ((vroom = fdc->sc_status[7]) == 0)
460323e9233Smiod vroom = 0x64;
461323e9233Smiod
462323e9233Smiod /* Configure controller to use FIFO and Implied Seek */
463323e9233Smiod if (fdc_wrfifo(fdc, NE7CMD_CFG) != 0)
464323e9233Smiod return (-1);
465323e9233Smiod if (fdc_wrfifo(fdc, vroom) != 0)
466323e9233Smiod return (-1);
467323e9233Smiod if (fdc_wrfifo(fdc, fdc->sc_cfg) != 0)
468323e9233Smiod return (-1);
469323e9233Smiod if (fdc_wrfifo(fdc, 0) != 0) /* PRETRK */
470323e9233Smiod return (-1);
471323e9233Smiod /* No result phase for the NE7CMD_CFG command */
472323e9233Smiod
473323e9233Smiod /* Lock configuration across soft resets. */
474323e9233Smiod if (fdc_wrfifo(fdc, NE7CMD_LOCK | CFG_LOCK) != 0 ||
475323e9233Smiod fdcresult(fdc) != 1) {
476323e9233Smiod #ifdef FD_DEBUG
477323e9233Smiod printf("fdconf: CFGLOCK failed");
478323e9233Smiod #endif
479323e9233Smiod return (-1);
480323e9233Smiod }
481323e9233Smiod
482323e9233Smiod return (0);
483323e9233Smiod #if 0
484323e9233Smiod if (fdc_wrfifo(fdc, NE7CMD_VERSION) == 0 &&
485323e9233Smiod fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
486323e9233Smiod if (fdc_debug)
487323e9233Smiod printf("[version cmd]");
488323e9233Smiod }
489323e9233Smiod #endif
490323e9233Smiod }
491323e9233Smiod
492323e9233Smiod int
fdcattach(struct fdc_softc * fdc,int pri)493bd535c51Sclaudio fdcattach(struct fdc_softc *fdc, int pri)
494323e9233Smiod {
495323e9233Smiod struct fdc_attach_args fa;
496323e9233Smiod int drive_attached;
497323e9233Smiod
498323e9233Smiod timeout_set(&fdc->fdctimeout_to, fdctimeout, fdc);
499323e9233Smiod timeout_set(&fdc->fdcpseudointr_to, fdcpseudointr, fdc);
500323e9233Smiod
501323e9233Smiod fdc->sc_state = DEVIDLE;
502323e9233Smiod fdc->sc_itask = FDC_ITASK_NONE;
503323e9233Smiod fdc->sc_istatus = FDC_ISTATUS_NONE;
504323e9233Smiod fdc->sc_flags |= FDC_EIS | FDC_NEEDMOTORWAIT;
505323e9233Smiod TAILQ_INIT(&fdc->sc_drives);
506323e9233Smiod
507323e9233Smiod /*
508323e9233Smiod * Configure controller; enable FIFO, Implied seek, no POLL mode?.
509323e9233Smiod * Note: CFG_EFIFO is active-low, initial threshold value: 8
510323e9233Smiod */
511323e9233Smiod fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
512323e9233Smiod if (fdconf(fdc) != 0) {
513323e9233Smiod printf("\n%s: no drives attached\n", fdc->sc_dev.dv_xname);
514323e9233Smiod return (-1);
515323e9233Smiod }
516323e9233Smiod
517323e9233Smiod if (bus_intr_establish(fdc->sc_bustag, pri, IPL_BIO,
518323e9233Smiod 0, fdchwintr, fdc, fdc->sc_dev.dv_xname) == NULL) {
519323e9233Smiod printf("\n%s: cannot register interrupt handler\n",
520323e9233Smiod fdc->sc_dev.dv_xname);
521323e9233Smiod return (-1);
522323e9233Smiod }
523323e9233Smiod
524323e9233Smiod fdc->sc_sicookie = softintr_establish(IPL_BIO, fdcswintr, fdc);
525323e9233Smiod if (fdc->sc_sicookie == NULL) {
526323e9233Smiod printf("\n%s: cannot register soft interrupt handler\n",
527323e9233Smiod fdc->sc_dev.dv_xname);
528323e9233Smiod return (-1);
529323e9233Smiod }
5305d1cc9b2Smiod
5315d1cc9b2Smiod if (fdc->sc_flags & FDC_NOEJECT)
5325d1cc9b2Smiod printf(": manual eject");
5335d1cc9b2Smiod printf("\n");
534323e9233Smiod
535323e9233Smiod /* physical limit: four drives per controller. */
536323e9233Smiod drive_attached = 0;
537323e9233Smiod for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
538323e9233Smiod fa.fa_deftype = NULL; /* unknown */
539323e9233Smiod fa.fa_deftype = &fd_types[0]; /* XXX */
540323e9233Smiod if (config_found(&fdc->sc_dev, (void *)&fa, fdprint) != NULL)
541323e9233Smiod drive_attached = 1;
542323e9233Smiod }
543323e9233Smiod
544323e9233Smiod if (drive_attached == 0) {
545323e9233Smiod /* XXX - dis-establish interrupts here */
546323e9233Smiod /* return (-1); */
547323e9233Smiod }
548323e9233Smiod
549323e9233Smiod return (0);
550323e9233Smiod }
551323e9233Smiod
552323e9233Smiod int
fdmatch(struct device * parent,void * match,void * aux)553bd535c51Sclaudio fdmatch(struct device *parent, void *match, void *aux)
554323e9233Smiod {
555323e9233Smiod struct fdc_softc *fdc = (void *)parent;
556323e9233Smiod bus_space_tag_t t = fdc->sc_bustag;
557323e9233Smiod bus_space_handle_t h = fdc->sc_handle;
558323e9233Smiod struct fdc_attach_args *fa = aux;
559323e9233Smiod int drive = fa->fa_drive;
560323e9233Smiod int n, ok;
561323e9233Smiod
562323e9233Smiod if (drive > 0)
563323e9233Smiod /* XXX - for now, punt on more than one drive */
564323e9233Smiod return (0);
565323e9233Smiod
566323e9233Smiod /* select drive and turn on motor */
567323e9233Smiod bus_space_write_1(t, h, FDREG77_DOR,
568323e9233Smiod drive | FDO_FRST | FDO_MOEN(drive));
569323e9233Smiod /* wait for motor to spin up */
570323e9233Smiod delay(250000);
571323e9233Smiod
572323e9233Smiod fdc->sc_nstat = 0;
573323e9233Smiod fdc_wrfifo(fdc, NE7CMD_RECAL);
574323e9233Smiod fdc_wrfifo(fdc, drive);
575323e9233Smiod
576323e9233Smiod /* Wait for recalibration to complete */
577323e9233Smiod for (n = 0; n < 10000; n++) {
578323e9233Smiod u_int8_t v;
579323e9233Smiod
580323e9233Smiod delay(1000);
581323e9233Smiod v = bus_space_read_1(t, h, FDREG77_MSR);
582323e9233Smiod if ((v & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
583323e9233Smiod /* wait a bit longer till device *really* is ready */
584323e9233Smiod delay(100000);
585323e9233Smiod if (fdc_wrfifo(fdc, NE7CMD_SENSEI))
586323e9233Smiod break;
587323e9233Smiod if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
588323e9233Smiod /*
589323e9233Smiod * Got `invalid command'; we interpret it
590323e9233Smiod * to mean that the re-calibrate hasn't in
591323e9233Smiod * fact finished yet
592323e9233Smiod */
593323e9233Smiod continue;
594323e9233Smiod break;
595323e9233Smiod }
596323e9233Smiod }
597323e9233Smiod n = fdc->sc_nstat;
598323e9233Smiod #ifdef FD_DEBUG
599323e9233Smiod if (fdc_debug) {
600323e9233Smiod int i;
601323e9233Smiod printf("fdprobe: %d stati:", n);
602323e9233Smiod for (i = 0; i < n; i++)
603323e9233Smiod printf(" 0x%x", fdc->sc_status[i]);
604323e9233Smiod printf("\n");
605323e9233Smiod }
606323e9233Smiod #endif
607323e9233Smiod ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
608323e9233Smiod
609323e9233Smiod /* deselect drive and turn motor off */
610323e9233Smiod bus_space_write_1(t, h, FDREG77_DOR, FDO_FRST | FDO_DS);
611323e9233Smiod
612323e9233Smiod return (ok);
613323e9233Smiod }
614323e9233Smiod
615323e9233Smiod /*
616323e9233Smiod * Controller is working, and drive responded. Attach it.
617323e9233Smiod */
618323e9233Smiod void
fdattach(struct device * parent,struct device * self,void * aux)619bd535c51Sclaudio fdattach(struct device *parent, struct device *self, void *aux)
620323e9233Smiod {
621323e9233Smiod struct fdc_softc *fdc = (void *)parent;
622323e9233Smiod struct fd_softc *fd = (void *)self;
623323e9233Smiod struct fdc_attach_args *fa = aux;
624323e9233Smiod struct fd_type *type = fa->fa_deftype;
625323e9233Smiod int drive = fa->fa_drive;
626323e9233Smiod
627323e9233Smiod timeout_set(&fd->sc_motoron_to, fd_motor_on, fd);
628323e9233Smiod timeout_set(&fd->sc_motoroff_to, fd_motor_off, fd);
629323e9233Smiod
630323e9233Smiod /* XXX Allow `flags' to override device type? */
631323e9233Smiod
632323e9233Smiod if (type)
633323e9233Smiod printf(": %s %d cyl, %d head, %d sec\n", type->name,
634323e9233Smiod type->tracks, type->heads, type->sectrac);
635323e9233Smiod else
636323e9233Smiod printf(": density unknown\n");
637323e9233Smiod
638323e9233Smiod fd->sc_cylin = -1;
639323e9233Smiod fd->sc_drive = drive;
640323e9233Smiod fd->sc_deftype = type;
641323e9233Smiod fdc->sc_fd[drive] = fd;
642323e9233Smiod
643323e9233Smiod fdc_wrfifo(fdc, NE7CMD_SPECIFY);
644323e9233Smiod fdc_wrfifo(fdc, type->steprate);
645323e9233Smiod /* XXX head load time == 6ms */
646323e9233Smiod fdc_wrfifo(fdc, 6 | NE7_SPECIFY_NODMA);
647323e9233Smiod
648323e9233Smiod /*
649323e9233Smiod * Initialize and attach the disk structure.
650323e9233Smiod */
65128696e49Sjsing fd->sc_dk.dk_flags = DKF_NOLABELREAD;
652323e9233Smiod fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
653274ffc92Sdlg bufq_init(&fd->sc_bufq, BUFQ_DEFAULT);
6542690bc4bSjsing disk_attach(&fd->sc_dv, &fd->sc_dk);
655c06fda6dSderaadt }
656323e9233Smiod
657c06fda6dSderaadt int
fdactivate(struct device * self,int act)658c06fda6dSderaadt fdactivate(struct device *self, int act)
659c06fda6dSderaadt {
660c06fda6dSderaadt int ret = 0;
661c06fda6dSderaadt
662c06fda6dSderaadt switch (act) {
663c06fda6dSderaadt case DVACT_POWERDOWN:
664323e9233Smiod /* Make sure the drive motor gets turned off at shutdown time. */
665c06fda6dSderaadt fd_motor_off(self);
666c06fda6dSderaadt break;
667c06fda6dSderaadt }
668c06fda6dSderaadt
669c06fda6dSderaadt return (ret);
670323e9233Smiod }
671323e9233Smiod
672323e9233Smiod __inline struct fd_type *
fd_dev_to_type(struct fd_softc * fd,dev_t dev)673bd535c51Sclaudio fd_dev_to_type(struct fd_softc *fd, dev_t dev)
674323e9233Smiod {
675323e9233Smiod int type = FDTYPE(dev);
676323e9233Smiod
677323e9233Smiod if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
678323e9233Smiod return (NULL);
679323e9233Smiod return (type ? &fd_types[type - 1] : fd->sc_deftype);
680323e9233Smiod }
681323e9233Smiod
682323e9233Smiod void
fdstrategy(struct buf * bp)683bd535c51Sclaudio fdstrategy(struct buf *bp)
684323e9233Smiod {
685323e9233Smiod struct fd_softc *fd;
686323e9233Smiod int unit = FDUNIT(bp->b_dev);
687323e9233Smiod int sz;
688323e9233Smiod int s;
689323e9233Smiod
690323e9233Smiod /* Valid unit, controller, and request? */
691323e9233Smiod if (unit >= fd_cd.cd_ndevs ||
692323e9233Smiod (fd = fd_cd.cd_devs[unit]) == 0 ||
693323e9233Smiod bp->b_blkno < 0 ||
694323e9233Smiod (((bp->b_bcount % FD_BSIZE(fd)) != 0 ||
695323e9233Smiod (bp->b_blkno * DEV_BSIZE) % FD_BSIZE(fd) != 0) &&
696323e9233Smiod (bp->b_flags & B_FORMAT) == 0)) {
697323e9233Smiod bp->b_error = EINVAL;
698323e9233Smiod goto bad;
699323e9233Smiod }
700323e9233Smiod
701323e9233Smiod /* If it's a null transfer, return immediately. */
702323e9233Smiod if (bp->b_bcount == 0)
703323e9233Smiod goto done;
704323e9233Smiod
70527f37992Sdlg bp->b_resid = bp->b_bcount;
706323e9233Smiod sz = howmany(bp->b_bcount, DEV_BSIZE);
707323e9233Smiod
708323e9233Smiod if (bp->b_blkno + sz > (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)) {
709323e9233Smiod sz = (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)
710323e9233Smiod - bp->b_blkno;
711323e9233Smiod if (sz == 0) {
712323e9233Smiod /* If exactly at end of disk, return EOF. */
713323e9233Smiod goto done;
714323e9233Smiod }
715323e9233Smiod if (sz < 0) {
716323e9233Smiod /* If past end of disk, return EINVAL. */
717323e9233Smiod bp->b_error = EINVAL;
718323e9233Smiod goto bad;
719323e9233Smiod }
720323e9233Smiod /* Otherwise, truncate request. */
721323e9233Smiod bp->b_bcount = sz << DEV_BSHIFT;
722323e9233Smiod }
723323e9233Smiod
724323e9233Smiod #ifdef FD_DEBUG
725323e9233Smiod if (fdc_debug > 1)
72627f37992Sdlg printf("fdstrategy: b_blkno %lld b_bcount %d blkno %lld\n",
727bb4f4faeSkrw (long long)bp->b_blkno, bp->b_bcount,
72827f37992Sdlg (long long)fd->sc_blkno);
729323e9233Smiod #endif
730323e9233Smiod
731274ffc92Sdlg /* Queue transfer */
732274ffc92Sdlg bufq_queue(&fd->sc_bufq, bp);
733274ffc92Sdlg
734323e9233Smiod /* Queue transfer on drive, activate drive and controller if idle. */
735323e9233Smiod s = splbio();
736323e9233Smiod timeout_del(&fd->sc_motoroff_to); /* a good idea */
737274ffc92Sdlg if (fd->sc_bp == NULL)
738323e9233Smiod fdstart(fd);
739323e9233Smiod #ifdef DIAGNOSTIC
740323e9233Smiod else {
741323e9233Smiod struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
742323e9233Smiod if (fdc->sc_state == DEVIDLE) {
743323e9233Smiod printf("fdstrategy: controller inactive\n");
744323e9233Smiod fdcstart(fdc);
745323e9233Smiod }
746323e9233Smiod }
747323e9233Smiod #endif
748323e9233Smiod splx(s);
749323e9233Smiod return;
750323e9233Smiod
751323e9233Smiod bad:
752323e9233Smiod bp->b_flags |= B_ERROR;
753323e9233Smiod done:
754323e9233Smiod /* Toss transfer; we're done early. */
755323e9233Smiod s = splbio();
756323e9233Smiod biodone(bp);
757323e9233Smiod splx(s);
758323e9233Smiod }
759323e9233Smiod
760323e9233Smiod void
fdstart(struct fd_softc * fd)761bd535c51Sclaudio fdstart(struct fd_softc *fd)
762323e9233Smiod {
763323e9233Smiod struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
764323e9233Smiod int active = !TAILQ_EMPTY(&fdc->sc_drives);
765323e9233Smiod
766323e9233Smiod /* Link into controller queue. */
767274ffc92Sdlg fd->sc_bp = bufq_dequeue(&fd->sc_bufq);
768323e9233Smiod TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
769323e9233Smiod
770323e9233Smiod /* If controller not already active, start it. */
771323e9233Smiod if (!active)
772323e9233Smiod fdcstart(fdc);
773323e9233Smiod }
774323e9233Smiod
775323e9233Smiod void
fdfinish(struct fd_softc * fd,struct buf * bp)776bd535c51Sclaudio fdfinish(struct fd_softc *fd, struct buf *bp)
777323e9233Smiod {
778323e9233Smiod struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
779323e9233Smiod
780274ffc92Sdlg fd->sc_skip = 0;
781274ffc92Sdlg fd->sc_bp = bufq_dequeue(&fd->sc_bufq);
782274ffc92Sdlg
783323e9233Smiod /*
784323e9233Smiod * Move this drive to the end of the queue to give others a `fair'
785323e9233Smiod * chance. We only force a switch if N operations are completed while
786323e9233Smiod * another drive is waiting to be serviced, since there is a long motor
787323e9233Smiod * startup delay whenever we switch.
788323e9233Smiod */
789323e9233Smiod if (TAILQ_NEXT(fd, sc_drivechain) != NULL && ++fd->sc_ops >= 8) {
790323e9233Smiod fd->sc_ops = 0;
791323e9233Smiod TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
792274ffc92Sdlg if (fd->sc_bp != NULL)
793323e9233Smiod TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
794323e9233Smiod }
795323e9233Smiod
796323e9233Smiod biodone(bp);
797323e9233Smiod /* turn off motor 5s from now */
79829e86e5eSblambert timeout_add_sec(&fd->sc_motoroff_to, 5);
799323e9233Smiod fdc->sc_state = DEVIDLE;
800323e9233Smiod }
801323e9233Smiod
802323e9233Smiod void
fdc_reset(struct fdc_softc * fdc)803bd535c51Sclaudio fdc_reset(struct fdc_softc *fdc)
804323e9233Smiod {
805323e9233Smiod bus_space_tag_t t = fdc->sc_bustag;
806323e9233Smiod bus_space_handle_t h = fdc->sc_handle;
807323e9233Smiod
808323e9233Smiod bus_space_write_1(t, h, FDREG77_DOR, FDO_FDMAEN | FDO_MOEN(0));
809323e9233Smiod
810323e9233Smiod bus_space_write_1(t, h, FDREG77_DRS, DRS_RESET);
811323e9233Smiod delay(10);
812323e9233Smiod bus_space_write_1(t, h, FDREG77_DRS, 0);
813323e9233Smiod
814323e9233Smiod bus_space_write_1(t, h, FDREG77_DOR,
815323e9233Smiod FDO_FRST | FDO_FDMAEN | FDO_DS);
816323e9233Smiod #ifdef FD_DEBUG
817323e9233Smiod if (fdc_debug)
818323e9233Smiod printf("fdc reset\n");
819323e9233Smiod #endif
820323e9233Smiod }
821323e9233Smiod
822323e9233Smiod void
fd_set_motor(struct fdc_softc * fdc)823bd535c51Sclaudio fd_set_motor(struct fdc_softc *fdc)
824323e9233Smiod {
825323e9233Smiod struct fd_softc *fd;
826323e9233Smiod u_char status;
827323e9233Smiod int n;
828323e9233Smiod
829323e9233Smiod status = FDO_FRST | FDO_FDMAEN;
830323e9233Smiod if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
831323e9233Smiod status |= fd->sc_drive;
832323e9233Smiod
833323e9233Smiod for (n = 0; n < 4; n++)
834323e9233Smiod if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
835323e9233Smiod status |= FDO_MOEN(n);
836323e9233Smiod bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
837323e9233Smiod FDREG77_DOR, status);
838323e9233Smiod }
839323e9233Smiod
840323e9233Smiod void
fd_motor_off(void * arg)841bd535c51Sclaudio fd_motor_off(void *arg)
842323e9233Smiod {
843323e9233Smiod struct fd_softc *fd = arg;
844323e9233Smiod int s;
845323e9233Smiod
846323e9233Smiod s = splbio();
847323e9233Smiod fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
848323e9233Smiod fd_set_motor((struct fdc_softc *)fd->sc_dv.dv_parent);
849323e9233Smiod splx(s);
850323e9233Smiod }
851323e9233Smiod
852323e9233Smiod void
fd_motor_on(void * arg)853bd535c51Sclaudio fd_motor_on(void *arg)
854323e9233Smiod {
855323e9233Smiod struct fd_softc *fd = arg;
856323e9233Smiod struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
857323e9233Smiod int s;
858323e9233Smiod
859323e9233Smiod s = splbio();
860323e9233Smiod fd->sc_flags &= ~FD_MOTOR_WAIT;
861323e9233Smiod if (fd == TAILQ_FIRST(&fdc->sc_drives) && fdc->sc_state == MOTORWAIT)
862323e9233Smiod (void) fdcstate(fdc);
863323e9233Smiod splx(s);
864323e9233Smiod }
865323e9233Smiod
866323e9233Smiod /*
867323e9233Smiod * Get status bytes off the FDC after a command has finished
868323e9233Smiod * Returns the number of status bytes read; -1 on error.
869323e9233Smiod * The return value is also stored in `sc_nstat'.
870323e9233Smiod */
871323e9233Smiod int
fdcresult(struct fdc_softc * fdc)872bd535c51Sclaudio fdcresult(struct fdc_softc *fdc)
873323e9233Smiod {
874323e9233Smiod bus_space_tag_t t = fdc->sc_bustag;
875323e9233Smiod bus_space_handle_t h = fdc->sc_handle;
876323e9233Smiod int j, n = 0;
877323e9233Smiod
8785d1cc9b2Smiod for (j = 100000; j; j--) {
879323e9233Smiod u_int8_t v = bus_space_read_1(t, h, FDREG77_MSR);
880323e9233Smiod v &= (NE7_DIO | NE7_RQM | NE7_CB);
881323e9233Smiod if (v == NE7_RQM)
882323e9233Smiod return (fdc->sc_nstat = n);
883323e9233Smiod if (v == (NE7_DIO | NE7_RQM | NE7_CB)) {
884323e9233Smiod if (n >= sizeof(fdc->sc_status)) {
885323e9233Smiod log(LOG_ERR, "fdcresult: overrun\n");
886323e9233Smiod return (-1);
887323e9233Smiod }
888323e9233Smiod fdc->sc_status[n++] =
889323e9233Smiod bus_space_read_1(t, h, FDREG77_FIFO);
890323e9233Smiod } else
891323e9233Smiod delay(1);
892323e9233Smiod }
893323e9233Smiod
894323e9233Smiod log(LOG_ERR, "fdcresult: timeout\n");
895323e9233Smiod return (fdc->sc_nstat = -1);
896323e9233Smiod }
897323e9233Smiod
898323e9233Smiod /*
899323e9233Smiod * Write a command byte to the FDC.
900323e9233Smiod * Returns 0 on success; -1 on failure (i.e. timeout)
901323e9233Smiod */
902323e9233Smiod int
fdc_wrfifo(struct fdc_softc * fdc,u_int8_t x)903bd535c51Sclaudio fdc_wrfifo(struct fdc_softc *fdc, u_int8_t x)
904323e9233Smiod {
905323e9233Smiod bus_space_tag_t t = fdc->sc_bustag;
906323e9233Smiod bus_space_handle_t h = fdc->sc_handle;
907323e9233Smiod int i;
908323e9233Smiod
909323e9233Smiod for (i = 100000; i-- != 0;) {
910323e9233Smiod u_int8_t v = bus_space_read_1(t, h, FDREG77_MSR);
911323e9233Smiod if ((v & (NE7_DIO|NE7_RQM)) == NE7_RQM) {
912323e9233Smiod /* The chip is ready */
913323e9233Smiod bus_space_write_1(t, h, FDREG77_FIFO, x);
914323e9233Smiod return (0);
915323e9233Smiod }
916323e9233Smiod delay(1);
917323e9233Smiod }
918323e9233Smiod return (-1);
919323e9233Smiod }
920323e9233Smiod
921323e9233Smiod int
fdc_diskchange(struct fdc_softc * fdc)922bd535c51Sclaudio fdc_diskchange(struct fdc_softc *fdc)
923323e9233Smiod {
924323e9233Smiod bus_space_tag_t t = fdc->sc_bustag;
925323e9233Smiod bus_space_handle_t h = fdc->sc_handle;
926323e9233Smiod
927323e9233Smiod u_int8_t v = bus_space_read_1(t, h, FDREG77_DIR);
928323e9233Smiod return ((v & FDI_DCHG) != 0);
929323e9233Smiod }
930323e9233Smiod
931323e9233Smiod int
fdopen(dev_t dev,int flags,int fmt,struct proc * p)932bd535c51Sclaudio fdopen(dev_t dev, int flags, int fmt, struct proc *p)
933323e9233Smiod {
934323e9233Smiod int unit, pmask;
935323e9233Smiod struct fd_softc *fd;
936323e9233Smiod struct fd_type *type;
937323e9233Smiod
938323e9233Smiod unit = FDUNIT(dev);
939323e9233Smiod if (unit >= fd_cd.cd_ndevs)
940323e9233Smiod return (ENXIO);
941323e9233Smiod fd = fd_cd.cd_devs[unit];
942323e9233Smiod if (fd == NULL)
943323e9233Smiod return (ENXIO);
944323e9233Smiod type = fd_dev_to_type(fd, dev);
945323e9233Smiod if (type == NULL)
946323e9233Smiod return (ENXIO);
947323e9233Smiod
948323e9233Smiod if ((fd->sc_flags & FD_OPEN) != 0 &&
949323e9233Smiod fd->sc_type != type)
950323e9233Smiod return (EBUSY);
951323e9233Smiod
952323e9233Smiod fd->sc_type = type;
953323e9233Smiod fd->sc_cylin = -1;
954323e9233Smiod fd->sc_flags |= FD_OPEN;
955323e9233Smiod
956323e9233Smiod /*
957323e9233Smiod * Only update the disklabel if we're not open anywhere else.
958323e9233Smiod */
959323e9233Smiod if (fd->sc_dk.dk_openmask == 0)
960190f1464Skrw fdgetdisklabel(dev, fd, fd->sc_dk.dk_label, 0);
961323e9233Smiod
962323e9233Smiod pmask = (1 << DISKPART(dev));
963323e9233Smiod
964323e9233Smiod switch (fmt) {
965323e9233Smiod case S_IFCHR:
966323e9233Smiod fd->sc_dk.dk_copenmask |= pmask;
967323e9233Smiod break;
968323e9233Smiod
969323e9233Smiod case S_IFBLK:
970323e9233Smiod fd->sc_dk.dk_bopenmask |= pmask;
971323e9233Smiod break;
972323e9233Smiod }
973323e9233Smiod fd->sc_dk.dk_openmask =
974323e9233Smiod fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
975323e9233Smiod
976323e9233Smiod return (0);
977323e9233Smiod }
978323e9233Smiod
979323e9233Smiod int
fdclose(dev_t dev,int flags,int fmt,struct proc * p)980bd535c51Sclaudio fdclose(dev_t dev, int flags, int fmt, struct proc *p)
981323e9233Smiod {
982323e9233Smiod struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
983323e9233Smiod int pmask = (1 << DISKPART(dev));
984323e9233Smiod
985323e9233Smiod fd->sc_flags &= ~FD_OPEN;
986323e9233Smiod fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
987323e9233Smiod
988323e9233Smiod switch (fmt) {
989323e9233Smiod case S_IFCHR:
990323e9233Smiod fd->sc_dk.dk_copenmask &= ~pmask;
991323e9233Smiod break;
992323e9233Smiod
993323e9233Smiod case S_IFBLK:
994323e9233Smiod fd->sc_dk.dk_bopenmask &= ~pmask;
995323e9233Smiod break;
996323e9233Smiod }
997323e9233Smiod fd->sc_dk.dk_openmask =
998323e9233Smiod fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
999323e9233Smiod
1000323e9233Smiod return (0);
1001323e9233Smiod }
1002323e9233Smiod
1003323e9233Smiod int
fdread(dev_t dev,struct uio * uio,int flag)1004bd535c51Sclaudio fdread(dev_t dev, struct uio *uio, int flag)
1005323e9233Smiod {
1006323e9233Smiod
100790f6ca67Smatthew return (physio(fdstrategy, dev, B_READ, minphys, uio));
1008323e9233Smiod }
1009323e9233Smiod
1010323e9233Smiod int
fdwrite(dev_t dev,struct uio * uio,int flag)1011bd535c51Sclaudio fdwrite(dev_t dev, struct uio *uio, int flag)
1012323e9233Smiod {
1013323e9233Smiod
101490f6ca67Smatthew return (physio(fdstrategy, dev, B_WRITE, minphys, uio));
1015323e9233Smiod }
1016323e9233Smiod
1017323e9233Smiod void
fdcstart(struct fdc_softc * fdc)1018bd535c51Sclaudio fdcstart(struct fdc_softc *fdc)
1019323e9233Smiod {
1020323e9233Smiod
1021323e9233Smiod #ifdef DIAGNOSTIC
1022323e9233Smiod /* only got here if controller's drive queue was inactive; should
1023323e9233Smiod be in idle state */
1024323e9233Smiod if (fdc->sc_state != DEVIDLE) {
1025323e9233Smiod printf("fdcstart: not idle\n");
1026323e9233Smiod return;
1027323e9233Smiod }
1028323e9233Smiod #endif
1029323e9233Smiod (void) fdcstate(fdc);
1030323e9233Smiod }
1031323e9233Smiod
1032323e9233Smiod void
fdcstatus(struct fdc_softc * fdc,char * s)1033bd535c51Sclaudio fdcstatus(struct fdc_softc *fdc, char *s)
1034323e9233Smiod {
1035545205eaSpyr struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
1036323e9233Smiod int n;
1037323e9233Smiod
1038323e9233Smiod /* Just print last status */
1039323e9233Smiod n = fdc->sc_nstat;
1040323e9233Smiod
1041323e9233Smiod #if 0
1042323e9233Smiod if (n == 0) {
1043323e9233Smiod fdc_wrfifo(fdc, NE7CMD_SENSEI);
1044323e9233Smiod (void) fdcresult(fdc);
1045323e9233Smiod n = 2;
1046323e9233Smiod }
1047323e9233Smiod #endif
1048323e9233Smiod
1049323e9233Smiod printf("%s: %s: state %d",
1050323e9233Smiod fd ? fd->sc_dv.dv_xname : "fdc", s, fdc->sc_state);
1051323e9233Smiod
1052323e9233Smiod switch (n) {
1053323e9233Smiod case 0:
1054323e9233Smiod printf("\n");
1055323e9233Smiod break;
1056323e9233Smiod case 2:
1057323e9233Smiod printf(" (st0 %b cyl %d)\n",
1058323e9233Smiod fdc->sc_status[0], NE7_ST0BITS,
1059323e9233Smiod fdc->sc_status[1]);
1060323e9233Smiod break;
1061323e9233Smiod case 7:
1062323e9233Smiod printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
1063323e9233Smiod fdc->sc_status[0], NE7_ST0BITS,
1064323e9233Smiod fdc->sc_status[1], NE7_ST1BITS,
1065323e9233Smiod fdc->sc_status[2], NE7_ST2BITS,
1066323e9233Smiod fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1067323e9233Smiod break;
1068323e9233Smiod #ifdef DIAGNOSTIC
1069323e9233Smiod default:
1070323e9233Smiod printf(" fdcstatus: weird size: %d\n", n);
1071323e9233Smiod break;
1072323e9233Smiod #endif
1073323e9233Smiod }
1074323e9233Smiod }
1075323e9233Smiod
1076323e9233Smiod void
fdctimeout(void * arg)1077bd535c51Sclaudio fdctimeout(void *arg)
1078323e9233Smiod {
1079323e9233Smiod struct fdc_softc *fdc = arg;
1080323e9233Smiod struct fd_softc *fd;
1081323e9233Smiod int s;
1082323e9233Smiod
1083323e9233Smiod s = splbio();
1084323e9233Smiod fd = TAILQ_FIRST(&fdc->sc_drives);
1085323e9233Smiod if (fd == NULL) {
1086323e9233Smiod printf("%s: timeout but no I/O pending: state %d, istatus=%d\n",
1087323e9233Smiod fdc->sc_dev.dv_xname, fdc->sc_state, fdc->sc_istatus);
1088323e9233Smiod fdc->sc_state = DEVIDLE;
1089323e9233Smiod goto out;
1090323e9233Smiod }
1091323e9233Smiod
1092274ffc92Sdlg if (fd->sc_bp != NULL)
1093323e9233Smiod fdc->sc_state++;
1094323e9233Smiod else
1095323e9233Smiod fdc->sc_state = DEVIDLE;
1096323e9233Smiod
1097323e9233Smiod (void) fdcstate(fdc);
1098323e9233Smiod out:
1099323e9233Smiod splx(s);
1100323e9233Smiod
1101323e9233Smiod }
1102323e9233Smiod
1103323e9233Smiod void
fdcpseudointr(void * arg)1104bd535c51Sclaudio fdcpseudointr(void *arg)
1105323e9233Smiod {
1106323e9233Smiod struct fdc_softc *fdc = arg;
1107323e9233Smiod int s;
1108323e9233Smiod
1109323e9233Smiod /* Just ensure it has the right spl. */
1110323e9233Smiod s = splbio();
1111323e9233Smiod (void) fdcstate(fdc);
1112323e9233Smiod splx(s);
1113323e9233Smiod }
1114323e9233Smiod
1115323e9233Smiod
1116323e9233Smiod /*
1117323e9233Smiod * Hardware interrupt entry point.
1118323e9233Smiod * Unfortunately, we have no reliable way to determine that the
1119323e9233Smiod * interrupt really came from the floppy controller; just hope
1120323e9233Smiod * that the other devices that share this interrupt can do better..
1121323e9233Smiod */
1122323e9233Smiod int
fdchwintr(void * arg)1123bd535c51Sclaudio fdchwintr(void *arg)
1124323e9233Smiod {
1125323e9233Smiod struct fdc_softc *fdc = arg;
1126323e9233Smiod bus_space_tag_t t = fdc->sc_bustag;
1127323e9233Smiod bus_space_handle_t h = fdc->sc_handle;
1128323e9233Smiod
1129323e9233Smiod switch (fdc->sc_itask) {
1130323e9233Smiod case FDC_ITASK_NONE:
1131323e9233Smiod return (0);
1132323e9233Smiod case FDC_ITASK_SENSEI:
1133323e9233Smiod if (fdc_wrfifo(fdc, NE7CMD_SENSEI) != 0 || fdcresult(fdc) == -1)
1134323e9233Smiod fdc->sc_istatus = FDC_ISTATUS_ERROR;
1135323e9233Smiod else
1136323e9233Smiod fdc->sc_istatus = FDC_ISTATUS_DONE;
1137323e9233Smiod softintr_schedule(fdc->sc_sicookie);
1138323e9233Smiod return (1);
1139323e9233Smiod case FDC_ITASK_RESULT:
1140323e9233Smiod if (fdcresult(fdc) == -1)
1141323e9233Smiod fdc->sc_istatus = FDC_ISTATUS_ERROR;
1142323e9233Smiod else
1143323e9233Smiod fdc->sc_istatus = FDC_ISTATUS_DONE;
1144323e9233Smiod softintr_schedule(fdc->sc_sicookie);
1145323e9233Smiod return (1);
1146323e9233Smiod case FDC_ITASK_DMA:
1147323e9233Smiod /* Proceed with pseudo-DMA below */
1148323e9233Smiod break;
1149323e9233Smiod default:
1150323e9233Smiod printf("fdc: stray hard interrupt: itask=%d\n", fdc->sc_itask);
1151323e9233Smiod fdc->sc_istatus = FDC_ISTATUS_SPURIOUS;
1152323e9233Smiod softintr_schedule(fdc->sc_sicookie);
1153323e9233Smiod return (1);
1154323e9233Smiod }
1155323e9233Smiod
1156323e9233Smiod /*
1157323e9233Smiod * Pseudo DMA in progress
1158323e9233Smiod */
1159323e9233Smiod for (;;) {
1160323e9233Smiod u_int8_t msr;
1161323e9233Smiod
1162323e9233Smiod msr = bus_space_read_1(t, h, FDREG77_MSR);
1163323e9233Smiod
1164323e9233Smiod if ((msr & NE7_RQM) == 0)
1165323e9233Smiod /* That's all this round */
1166323e9233Smiod break;
1167323e9233Smiod
1168323e9233Smiod if ((msr & NE7_NDM) == 0) {
1169323e9233Smiod fdcresult(fdc);
1170323e9233Smiod fdc->sc_istatus = FDC_ISTATUS_DONE;
1171323e9233Smiod softintr_schedule(fdc->sc_sicookie);
1172323e9233Smiod #ifdef FD_DEBUG
1173323e9233Smiod if (fdc_debug > 1)
1174323e9233Smiod printf("fdc: overrun: msr = %x, tc = %d\n",
1175323e9233Smiod msr, fdc->sc_tc);
1176323e9233Smiod #endif
1177323e9233Smiod break;
1178323e9233Smiod }
1179323e9233Smiod
1180323e9233Smiod /* Another byte can be transferred */
1181323e9233Smiod if ((msr & NE7_DIO) != 0)
1182323e9233Smiod *fdc->sc_data =
1183323e9233Smiod bus_space_read_1(t, h, FDREG77_FIFO);
1184323e9233Smiod else
1185323e9233Smiod bus_space_write_1(t, h, FDREG77_FIFO,
1186323e9233Smiod *fdc->sc_data);
1187323e9233Smiod
1188323e9233Smiod fdc->sc_data++;
1189323e9233Smiod if (--fdc->sc_tc == 0) {
1190323e9233Smiod fdc->sc_istatus = FDC_ISTATUS_DONE;
1191323e9233Smiod FTC_FLIP;
1192323e9233Smiod fdcresult(fdc);
1193323e9233Smiod softintr_schedule(fdc->sc_sicookie);
1194323e9233Smiod break;
1195323e9233Smiod }
1196323e9233Smiod }
1197323e9233Smiod return (1);
1198323e9233Smiod }
1199323e9233Smiod
1200323e9233Smiod void
fdcswintr(void * arg)1201bd535c51Sclaudio fdcswintr(void *arg)
1202323e9233Smiod {
1203323e9233Smiod struct fdc_softc *fdc = arg;
1204323e9233Smiod int s;
1205323e9233Smiod
1206323e9233Smiod if (fdc->sc_istatus == FDC_ISTATUS_NONE)
1207323e9233Smiod /* This (software) interrupt is not for us */
1208323e9233Smiod return;
1209323e9233Smiod
1210323e9233Smiod switch (fdc->sc_istatus) {
1211323e9233Smiod case FDC_ISTATUS_ERROR:
1212323e9233Smiod printf("fdc: ierror status: state %d\n", fdc->sc_state);
1213323e9233Smiod break;
1214323e9233Smiod case FDC_ISTATUS_SPURIOUS:
1215323e9233Smiod printf("fdc: spurious interrupt: state %d\n", fdc->sc_state);
1216323e9233Smiod break;
1217323e9233Smiod }
1218323e9233Smiod
1219323e9233Smiod s = splbio();
1220323e9233Smiod fdcstate(fdc);
1221323e9233Smiod splx(s);
1222323e9233Smiod return;
1223323e9233Smiod }
1224323e9233Smiod
1225323e9233Smiod int
fdcstate(struct fdc_softc * fdc)1226bd535c51Sclaudio fdcstate(struct fdc_softc *fdc)
1227323e9233Smiod {
1228323e9233Smiod #define st0 fdc->sc_status[0]
1229323e9233Smiod #define st1 fdc->sc_status[1]
1230323e9233Smiod #define cyl fdc->sc_status[1]
1231323e9233Smiod #define FDC_WRFIFO(fdc, c) \
1232323e9233Smiod do { \
1233323e9233Smiod if (fdc_wrfifo(fdc, (c))) { \
1234323e9233Smiod goto xxx; \
1235323e9233Smiod } \
1236323e9233Smiod } while(0)
1237323e9233Smiod
1238323e9233Smiod struct fd_softc *fd;
1239323e9233Smiod struct buf *bp;
124027f37992Sdlg int read, head, sec, nblks, cylin;
1241323e9233Smiod struct fd_type *type;
1242323e9233Smiod struct fd_formb *finfo = NULL;
1243323e9233Smiod
1244323e9233Smiod if (fdc->sc_istatus == FDC_ISTATUS_ERROR) {
1245323e9233Smiod /* Prevent loop if the reset sequence produces errors */
1246323e9233Smiod if (fdc->sc_state != RESETCOMPLETE &&
1247323e9233Smiod fdc->sc_state != RECALWAIT &&
1248323e9233Smiod fdc->sc_state != RECALCOMPLETE)
1249323e9233Smiod fdc->sc_state = DORESET;
1250323e9233Smiod }
1251323e9233Smiod
1252323e9233Smiod /* Clear I task/status field */
1253323e9233Smiod fdc->sc_istatus = FDC_ISTATUS_NONE;
1254323e9233Smiod fdc->sc_itask = FDC_ITASK_NONE;
1255323e9233Smiod
1256323e9233Smiod loop:
1257323e9233Smiod /* Is there a drive for the controller to do a transfer with? */
1258323e9233Smiod fd = TAILQ_FIRST(&fdc->sc_drives);
1259323e9233Smiod if (fd == NULL) {
1260323e9233Smiod fdc->sc_state = DEVIDLE;
1261323e9233Smiod return (0);
1262323e9233Smiod }
1263323e9233Smiod
1264323e9233Smiod /* Is there a transfer to this drive? If not, deactivate drive. */
1265274ffc92Sdlg bp = fd->sc_bp;
1266323e9233Smiod if (bp == NULL) {
1267323e9233Smiod fd->sc_ops = 0;
1268323e9233Smiod TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1269323e9233Smiod goto loop;
1270323e9233Smiod }
1271323e9233Smiod
1272323e9233Smiod if (bp->b_flags & B_FORMAT)
1273323e9233Smiod finfo = (struct fd_formb *)bp->b_data;
1274323e9233Smiod
127527f37992Sdlg cylin = ((bp->b_blkno * DEV_BSIZE) - (bp->b_bcount - bp->b_resid)) /
127627f37992Sdlg (FD_BSIZE(fd) * fd->sc_type->seccyl);
127727f37992Sdlg
1278323e9233Smiod switch (fdc->sc_state) {
1279323e9233Smiod case DEVIDLE:
1280323e9233Smiod fdc->sc_errors = 0;
1281323e9233Smiod fd->sc_skip = 0;
1282323e9233Smiod fd->sc_bcount = bp->b_bcount;
1283323e9233Smiod fd->sc_blkno = (bp->b_blkno * DEV_BSIZE) / FD_BSIZE(fd);
1284323e9233Smiod timeout_del(&fd->sc_motoroff_to);
1285323e9233Smiod if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1286323e9233Smiod fdc->sc_state = MOTORWAIT;
1287323e9233Smiod return (1);
1288323e9233Smiod }
1289323e9233Smiod if ((fd->sc_flags & FD_MOTOR) == 0) {
1290323e9233Smiod /* Turn on the motor, being careful about pairing. */
1291323e9233Smiod struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1292323e9233Smiod if (ofd && ofd->sc_flags & FD_MOTOR) {
1293323e9233Smiod timeout_del(&ofd->sc_motoroff_to);
1294323e9233Smiod ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1295323e9233Smiod }
1296323e9233Smiod fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1297323e9233Smiod fd_set_motor(fdc);
1298323e9233Smiod fdc->sc_state = MOTORWAIT;
1299323e9233Smiod if ((fdc->sc_flags & FDC_NEEDMOTORWAIT) != 0) { /*XXX*/
1300323e9233Smiod /* Allow .25s for motor to stabilize. */
13013db450efSblambert timeout_add_msec(&fd->sc_motoron_to, 250);
1302323e9233Smiod } else {
1303323e9233Smiod fd->sc_flags &= ~FD_MOTOR_WAIT;
1304323e9233Smiod goto loop;
1305323e9233Smiod }
1306323e9233Smiod return (1);
1307323e9233Smiod }
1308323e9233Smiod /* Make sure the right drive is selected. */
1309323e9233Smiod fd_set_motor(fdc);
1310323e9233Smiod
1311323e9233Smiod if (fdc_diskchange(fdc))
1312323e9233Smiod goto dodskchg;
1313323e9233Smiod
1314323e9233Smiod /*FALLTHROUGH*/
1315323e9233Smiod case DOSEEK:
1316323e9233Smiod doseek:
1317323e9233Smiod if ((fdc->sc_flags & FDC_EIS) &&
1318323e9233Smiod (bp->b_flags & B_FORMAT) == 0) {
131927f37992Sdlg fd->sc_cylin = cylin;
1320323e9233Smiod /* We use implied seek */
1321323e9233Smiod goto doio;
1322323e9233Smiod }
1323323e9233Smiod
1324608d71c4Sbrad if (fd->sc_cylin == cylin)
1325323e9233Smiod goto doio;
1326323e9233Smiod
1327323e9233Smiod fd->sc_cylin = -1;
1328323e9233Smiod fdc->sc_state = SEEKWAIT;
1329323e9233Smiod fdc->sc_nstat = 0;
1330323e9233Smiod
1331323e9233Smiod fd->sc_dk.dk_seek++;
1332323e9233Smiod
1333323e9233Smiod disk_busy(&fd->sc_dk);
133429e86e5eSblambert timeout_add_sec(&fdc->fdctimeout_to, 4);
1335323e9233Smiod
1336323e9233Smiod /* specify command */
1337323e9233Smiod FDC_WRFIFO(fdc, NE7CMD_SPECIFY);
1338323e9233Smiod FDC_WRFIFO(fdc, fd->sc_type->steprate);
1339323e9233Smiod /* XXX head load time == 6ms */
1340323e9233Smiod FDC_WRFIFO(fdc, 6 | NE7_SPECIFY_NODMA);
1341323e9233Smiod
1342323e9233Smiod fdc->sc_itask = FDC_ITASK_SENSEI;
1343323e9233Smiod /* seek function */
1344323e9233Smiod FDC_WRFIFO(fdc, NE7CMD_SEEK);
1345323e9233Smiod FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
134627f37992Sdlg FDC_WRFIFO(fdc, cylin * fd->sc_type->step);
1347323e9233Smiod return (1);
1348323e9233Smiod
1349323e9233Smiod case DODSKCHG:
1350323e9233Smiod dodskchg:
1351323e9233Smiod /*
1352323e9233Smiod * Disk change: force a seek operation by going to cyl 1
1353323e9233Smiod * followed by a recalibrate.
1354323e9233Smiod */
1355323e9233Smiod disk_busy(&fd->sc_dk);
135629e86e5eSblambert timeout_add_sec(&fdc->fdctimeout_to, 4);
1357323e9233Smiod fd->sc_cylin = -1;
1358323e9233Smiod fdc->sc_nstat = 0;
1359323e9233Smiod fdc->sc_state = DSKCHGWAIT;
1360323e9233Smiod
1361323e9233Smiod fdc->sc_itask = FDC_ITASK_SENSEI;
1362323e9233Smiod /* seek function */
1363323e9233Smiod FDC_WRFIFO(fdc, NE7CMD_SEEK);
1364323e9233Smiod FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
1365323e9233Smiod FDC_WRFIFO(fdc, 1 * fd->sc_type->step);
1366323e9233Smiod return (1);
1367323e9233Smiod
1368323e9233Smiod case DSKCHGWAIT:
1369323e9233Smiod timeout_del(&fdc->fdctimeout_to);
1370d40269afSderaadt disk_unbusy(&fd->sc_dk, 0, 0, 0);
1371323e9233Smiod if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1372323e9233Smiod cyl != 1 * fd->sc_type->step) {
1373323e9233Smiod fdcstatus(fdc, "dskchg seek failed");
1374323e9233Smiod fdc->sc_state = DORESET;
1375323e9233Smiod } else
1376323e9233Smiod fdc->sc_state = DORECAL;
1377323e9233Smiod
1378323e9233Smiod if (fdc_diskchange(fdc)) {
1379323e9233Smiod printf("%s: cannot clear disk change status\n",
1380323e9233Smiod fdc->sc_dev.dv_xname);
1381323e9233Smiod fdc->sc_state = DORESET;
1382323e9233Smiod }
1383323e9233Smiod goto loop;
1384323e9233Smiod
1385323e9233Smiod case DOIO:
1386323e9233Smiod doio:
1387323e9233Smiod if (finfo != NULL)
1388323e9233Smiod fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1389323e9233Smiod (char *)finfo;
1390323e9233Smiod type = fd->sc_type;
1391323e9233Smiod sec = fd->sc_blkno % type->seccyl;
1392323e9233Smiod nblks = type->seccyl - sec;
1393323e9233Smiod nblks = min(nblks, fd->sc_bcount / FD_BSIZE(fd));
1394323e9233Smiod nblks = min(nblks, FDC_MAXIOSIZE / FD_BSIZE(fd));
1395323e9233Smiod fd->sc_nblks = nblks;
1396323e9233Smiod fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FD_BSIZE(fd);
1397323e9233Smiod head = sec / type->sectrac;
1398323e9233Smiod sec -= head * type->sectrac;
1399323e9233Smiod #ifdef DIAGNOSTIC
140015aab03cSderaadt {
14011abdbfdeSderaadt daddr_t block;
140215aab03cSderaadt
140315aab03cSderaadt block = (fd->sc_cylin * type->heads + head) *
140415aab03cSderaadt type->sectrac + sec;
1405323e9233Smiod if (block != fd->sc_blkno) {
1406bb4f4faeSkrw printf("fdcintr: block %lld != blkno %lld\n",
1407bb4f4faeSkrw (long long)block, (long long)fd->sc_blkno);
1408206598dbSmiod #if defined(FD_DEBUG) && defined(DDB)
1409e97088d6Smpi db_enter();
1410323e9233Smiod #endif
141115aab03cSderaadt }
141215aab03cSderaadt }
1413323e9233Smiod #endif
1414323e9233Smiod read = bp->b_flags & B_READ;
1415323e9233Smiod
1416323e9233Smiod /* Setup for pseudo DMA */
1417323e9233Smiod fdc->sc_data = bp->b_data + fd->sc_skip;
1418323e9233Smiod fdc->sc_tc = fd->sc_nbytes;
1419323e9233Smiod
1420323e9233Smiod bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
1421323e9233Smiod FDREG77_DRS, type->rate);
1422323e9233Smiod #ifdef FD_DEBUG
1423323e9233Smiod if (fdc_debug > 1)
1424323e9233Smiod printf("fdcstate: doio: %s drive %d "
1425323e9233Smiod "track %d head %d sec %d nblks %d\n",
1426323e9233Smiod finfo ? "format" :
1427323e9233Smiod (read ? "read" : "write"),
1428323e9233Smiod fd->sc_drive, fd->sc_cylin, head, sec, nblks);
1429323e9233Smiod #endif
1430323e9233Smiod fdc->sc_state = IOCOMPLETE;
1431323e9233Smiod fdc->sc_itask = FDC_ITASK_DMA;
1432323e9233Smiod fdc->sc_nstat = 0;
1433323e9233Smiod
1434323e9233Smiod disk_busy(&fd->sc_dk);
1435323e9233Smiod
1436323e9233Smiod /* allow 3 seconds for operation */
143729e86e5eSblambert timeout_add_sec(&fdc->fdctimeout_to, 3);
1438323e9233Smiod
1439323e9233Smiod if (finfo != NULL) {
1440323e9233Smiod /* formatting */
1441323e9233Smiod FDC_WRFIFO(fdc, NE7CMD_FORMAT);
1442323e9233Smiod FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
1443323e9233Smiod FDC_WRFIFO(fdc, finfo->fd_formb_secshift);
1444323e9233Smiod FDC_WRFIFO(fdc, finfo->fd_formb_nsecs);
1445323e9233Smiod FDC_WRFIFO(fdc, finfo->fd_formb_gaplen);
1446323e9233Smiod FDC_WRFIFO(fdc, finfo->fd_formb_fillbyte);
1447323e9233Smiod } else {
1448323e9233Smiod if (read)
1449323e9233Smiod FDC_WRFIFO(fdc, NE7CMD_READ);
1450323e9233Smiod else
1451323e9233Smiod FDC_WRFIFO(fdc, NE7CMD_WRITE);
1452323e9233Smiod FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
1453323e9233Smiod FDC_WRFIFO(fdc, fd->sc_cylin); /*track*/
1454323e9233Smiod FDC_WRFIFO(fdc, head);
1455323e9233Smiod FDC_WRFIFO(fdc, sec + 1); /*sector+1*/
1456323e9233Smiod FDC_WRFIFO(fdc, type->secsize); /*sector size*/
1457323e9233Smiod FDC_WRFIFO(fdc, type->sectrac); /*secs/track*/
1458323e9233Smiod FDC_WRFIFO(fdc, type->gap1); /*gap1 size*/
1459323e9233Smiod FDC_WRFIFO(fdc, type->datalen); /*data length*/
1460323e9233Smiod }
1461323e9233Smiod
1462323e9233Smiod return (1); /* will return later */
1463323e9233Smiod
1464323e9233Smiod case SEEKWAIT:
1465323e9233Smiod timeout_del(&fdc->fdctimeout_to);
1466323e9233Smiod fdc->sc_state = SEEKCOMPLETE;
1467323e9233Smiod if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1468323e9233Smiod /* allow 1/50 second for heads to settle */
14693db450efSblambert timeout_add_msec(&fdc->fdcpseudointr_to, 20);
1470323e9233Smiod return (1); /* will return later */
1471323e9233Smiod }
1472323e9233Smiod /*FALLTHROUGH*/
1473323e9233Smiod case SEEKCOMPLETE:
1474323e9233Smiod /* no data on seek */
1475d40269afSderaadt disk_unbusy(&fd->sc_dk, 0, 0, 0);
1476323e9233Smiod
1477323e9233Smiod /* Make sure seek really happened. */
1478323e9233Smiod if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
147927f37992Sdlg cyl != cylin * fd->sc_type->step) {
1480323e9233Smiod #ifdef FD_DEBUG
1481323e9233Smiod if (fdc_debug)
1482323e9233Smiod fdcstatus(fdc, "seek failed");
1483323e9233Smiod #endif
1484323e9233Smiod fdcretry(fdc);
1485323e9233Smiod goto loop;
1486323e9233Smiod }
148727f37992Sdlg fd->sc_cylin = cylin;
1488323e9233Smiod goto doio;
1489323e9233Smiod
1490323e9233Smiod case IOTIMEDOUT:
1491323e9233Smiod /*
1492323e9233Smiod * Try to abort the I/O operation without resetting
1493323e9233Smiod * the chip first. Poke TC and arrange to pick up
1494323e9233Smiod * the timed out I/O command's status.
1495323e9233Smiod */
1496323e9233Smiod fdc->sc_itask = FDC_ITASK_RESULT;
1497323e9233Smiod fdc->sc_state = IOCLEANUPWAIT;
1498323e9233Smiod fdc->sc_nstat = 0;
1499323e9233Smiod /* 1/10 second should be enough */
15003db450efSblambert timeout_add_msec(&fdc->fdctimeout_to, 100);
1501323e9233Smiod return (1);
1502323e9233Smiod
1503323e9233Smiod case IOCLEANUPTIMEDOUT:
1504323e9233Smiod case SEEKTIMEDOUT:
1505323e9233Smiod case RECALTIMEDOUT:
1506323e9233Smiod case RESETTIMEDOUT:
1507323e9233Smiod case DSKCHGTIMEDOUT:
1508323e9233Smiod fdcstatus(fdc, "timeout");
1509323e9233Smiod
1510323e9233Smiod /* All other timeouts always roll through to a chip reset */
1511323e9233Smiod fdcretry(fdc);
1512323e9233Smiod
1513323e9233Smiod /* Force reset, no matter what fdcretry() says */
1514323e9233Smiod fdc->sc_state = DORESET;
1515323e9233Smiod goto loop;
1516323e9233Smiod
1517323e9233Smiod case IOCLEANUPWAIT: /* IO FAILED, cleanup succeeded */
1518323e9233Smiod timeout_del(&fdc->fdctimeout_to);
1519323e9233Smiod disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
1520d40269afSderaadt bp->b_blkno, (bp->b_flags & B_READ));
1521323e9233Smiod fdcretry(fdc);
1522323e9233Smiod goto loop;
1523323e9233Smiod
1524323e9233Smiod case IOCOMPLETE: /* IO DONE, post-analyze */
1525323e9233Smiod timeout_del(&fdc->fdctimeout_to);
1526323e9233Smiod
1527323e9233Smiod disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
1528d40269afSderaadt bp->b_blkno, (bp->b_flags & B_READ));
1529323e9233Smiod
1530323e9233Smiod if (fdc->sc_nstat != 7 || st1 != 0 ||
1531323e9233Smiod ((st0 & 0xf8) != 0 &&
1532323e9233Smiod ((st0 & 0xf8) != 0x20 || (fdc->sc_cfg & CFG_EIS) == 0))) {
1533323e9233Smiod #ifdef FD_DEBUG
1534323e9233Smiod if (fdc_debug) {
1535323e9233Smiod fdcstatus(fdc,
1536323e9233Smiod bp->b_flags & B_READ
1537323e9233Smiod ? "read failed" : "write failed");
1538323e9233Smiod printf("blkno %lld nblks %d nstat %d tc %d\n",
1539bb4f4faeSkrw (long long)fd->sc_blkno, fd->sc_nblks,
1540323e9233Smiod fdc->sc_nstat, fdc->sc_tc);
1541323e9233Smiod }
1542323e9233Smiod #endif
1543323e9233Smiod if (fdc->sc_nstat == 7 &&
1544323e9233Smiod (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
1545323e9233Smiod
1546323e9233Smiod /*
1547323e9233Smiod * Silently retry overruns if no other
1548323e9233Smiod * error bit is set. Adjust threshold.
1549323e9233Smiod */
1550323e9233Smiod int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1551323e9233Smiod if (thr < 15) {
1552323e9233Smiod thr++;
1553323e9233Smiod fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1554323e9233Smiod fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1555323e9233Smiod #ifdef FD_DEBUG
1556323e9233Smiod if (fdc_debug)
1557323e9233Smiod printf("fdc: %d -> threshold\n", thr);
1558323e9233Smiod #endif
1559323e9233Smiod fdconf(fdc);
1560323e9233Smiod fdc->sc_overruns = 0;
1561323e9233Smiod }
1562323e9233Smiod if (++fdc->sc_overruns < 3) {
1563323e9233Smiod fdc->sc_state = DOIO;
1564323e9233Smiod goto loop;
1565323e9233Smiod }
1566323e9233Smiod }
1567323e9233Smiod fdcretry(fdc);
1568323e9233Smiod goto loop;
1569323e9233Smiod }
1570323e9233Smiod if (fdc->sc_errors) {
1571323e9233Smiod diskerr(bp, "fd", "soft error", LOG_PRINTF,
1572323e9233Smiod fd->sc_skip / FD_BSIZE(fd),
1573323e9233Smiod (struct disklabel *)NULL);
1574323e9233Smiod printf("\n");
1575323e9233Smiod fdc->sc_errors = 0;
1576323e9233Smiod } else {
1577323e9233Smiod if (--fdc->sc_overruns < -20) {
1578323e9233Smiod int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1579323e9233Smiod if (thr > 0) {
1580323e9233Smiod thr--;
1581323e9233Smiod fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1582323e9233Smiod fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1583323e9233Smiod #ifdef FD_DEBUG
1584323e9233Smiod if (fdc_debug)
1585323e9233Smiod printf("fdc: %d -> threshold\n", thr);
1586323e9233Smiod #endif
1587323e9233Smiod fdconf(fdc);
1588323e9233Smiod }
1589323e9233Smiod fdc->sc_overruns = 0;
1590323e9233Smiod }
1591323e9233Smiod }
1592323e9233Smiod fd->sc_blkno += fd->sc_nblks;
1593323e9233Smiod fd->sc_skip += fd->sc_nbytes;
1594323e9233Smiod fd->sc_bcount -= fd->sc_nbytes;
159527f37992Sdlg bp->b_resid -= fd->sc_nbytes;
1596323e9233Smiod if (finfo == NULL && fd->sc_bcount > 0) {
159727f37992Sdlg cylin = fd->sc_blkno / fd->sc_type->seccyl;
1598323e9233Smiod goto doseek;
1599323e9233Smiod }
1600323e9233Smiod fdfinish(fd, bp);
1601323e9233Smiod goto loop;
1602323e9233Smiod
1603323e9233Smiod case DORESET:
1604323e9233Smiod /* try a reset, keep motor on */
1605323e9233Smiod fd_set_motor(fdc);
1606323e9233Smiod delay(100);
1607323e9233Smiod fdc->sc_nstat = 0;
1608323e9233Smiod fdc->sc_itask = FDC_ITASK_SENSEI;
1609323e9233Smiod fdc->sc_state = RESETCOMPLETE;
16103db450efSblambert timeout_add_msec(&fdc->fdctimeout_to, 500);
1611323e9233Smiod fdc_reset(fdc);
1612323e9233Smiod return (1); /* will return later */
1613323e9233Smiod
1614323e9233Smiod case RESETCOMPLETE:
1615323e9233Smiod timeout_del(&fdc->fdctimeout_to);
1616323e9233Smiod fdconf(fdc);
1617323e9233Smiod
1618323e9233Smiod /* FALLTHROUGH */
1619323e9233Smiod case DORECAL:
1620323e9233Smiod fdc->sc_state = RECALWAIT;
1621323e9233Smiod fdc->sc_itask = FDC_ITASK_SENSEI;
1622323e9233Smiod fdc->sc_nstat = 0;
162329e86e5eSblambert timeout_add_sec(&fdc->fdctimeout_to, 5);
1624323e9233Smiod /* recalibrate function */
1625323e9233Smiod FDC_WRFIFO(fdc, NE7CMD_RECAL);
1626323e9233Smiod FDC_WRFIFO(fdc, fd->sc_drive);
1627323e9233Smiod return (1); /* will return later */
1628323e9233Smiod
1629323e9233Smiod case RECALWAIT:
1630323e9233Smiod timeout_del(&fdc->fdctimeout_to);
1631323e9233Smiod fdc->sc_state = RECALCOMPLETE;
1632323e9233Smiod if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1633323e9233Smiod /* allow 1/30 second for heads to settle */
1634b5d79655Scheloha timeout_add_msec(&fdc->fdcpseudointr_to, 1000 / 30);
1635323e9233Smiod return (1); /* will return later */
1636323e9233Smiod }
1637323e9233Smiod
1638323e9233Smiod case RECALCOMPLETE:
1639323e9233Smiod if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1640323e9233Smiod #ifdef FD_DEBUG
1641323e9233Smiod if (fdc_debug)
1642323e9233Smiod fdcstatus(fdc, "recalibrate failed");
1643323e9233Smiod #endif
1644323e9233Smiod fdcretry(fdc);
1645323e9233Smiod goto loop;
1646323e9233Smiod }
1647323e9233Smiod fd->sc_cylin = 0;
1648323e9233Smiod goto doseek;
1649323e9233Smiod
1650323e9233Smiod case MOTORWAIT:
1651323e9233Smiod if (fd->sc_flags & FD_MOTOR_WAIT)
1652323e9233Smiod return (1); /* time's not up yet */
1653323e9233Smiod goto doseek;
1654323e9233Smiod
1655323e9233Smiod default:
1656323e9233Smiod fdcstatus(fdc, "stray interrupt");
1657323e9233Smiod return (1);
1658323e9233Smiod }
1659323e9233Smiod #ifdef DIAGNOSTIC
1660323e9233Smiod panic("fdcintr: impossible");
1661323e9233Smiod #endif
1662323e9233Smiod
1663323e9233Smiod xxx:
1664323e9233Smiod /*
1665323e9233Smiod * We get here if the chip locks up in FDC_WRFIFO()
1666323e9233Smiod * Cancel any operation and schedule a reset
1667323e9233Smiod */
1668323e9233Smiod timeout_del(&fdc->fdctimeout_to);
1669323e9233Smiod fdcretry(fdc);
1670323e9233Smiod fdc->sc_state = DORESET;
1671323e9233Smiod goto loop;
1672323e9233Smiod
1673323e9233Smiod #undef st0
1674323e9233Smiod #undef st1
1675323e9233Smiod #undef cyl
1676323e9233Smiod }
1677323e9233Smiod
1678323e9233Smiod void
fdcretry(struct fdc_softc * fdc)1679bd535c51Sclaudio fdcretry(struct fdc_softc *fdc)
1680323e9233Smiod {
1681323e9233Smiod struct fd_softc *fd;
1682323e9233Smiod struct buf *bp;
1683323e9233Smiod int error = EIO;
1684323e9233Smiod
1685323e9233Smiod fd = TAILQ_FIRST(&fdc->sc_drives);
1686274ffc92Sdlg bp = fd->sc_bp;
1687323e9233Smiod
1688323e9233Smiod fdc->sc_overruns = 0;
1689323e9233Smiod if (fd->sc_opts & FDOPT_NORETRY)
1690323e9233Smiod goto fail;
1691323e9233Smiod
1692323e9233Smiod switch (fdc->sc_errors) {
1693323e9233Smiod case 0:
1694323e9233Smiod if (fdc->sc_nstat == 7 &&
1695323e9233Smiod (fdc->sc_status[0] & 0xd8) == 0x40 &&
1696323e9233Smiod (fdc->sc_status[1] & 0x2) == 0x2) {
1697323e9233Smiod printf("%s: read-only medium\n", fd->sc_dv.dv_xname);
1698323e9233Smiod error = EROFS;
1699323e9233Smiod goto failsilent;
1700323e9233Smiod }
1701323e9233Smiod /* try again */
1702323e9233Smiod fdc->sc_state =
1703323e9233Smiod (fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
1704323e9233Smiod break;
1705323e9233Smiod
1706323e9233Smiod case 1: case 2: case 3:
1707323e9233Smiod /* didn't work; try recalibrating */
1708323e9233Smiod fdc->sc_state = DORECAL;
1709323e9233Smiod break;
1710323e9233Smiod
1711323e9233Smiod case 4:
1712323e9233Smiod if (fdc->sc_nstat == 7 &&
1713323e9233Smiod fdc->sc_status[0] == 0 &&
1714323e9233Smiod fdc->sc_status[1] == 0 &&
1715323e9233Smiod fdc->sc_status[2] == 0) {
1716323e9233Smiod /*
1717323e9233Smiod * We've retried a few times and we've got
1718323e9233Smiod * valid status and all three status bytes
1719323e9233Smiod * are zero. Assume this condition is the
1720323e9233Smiod * result of no disk loaded into the drive.
1721323e9233Smiod */
1722323e9233Smiod printf("%s: no medium?\n", fd->sc_dv.dv_xname);
1723323e9233Smiod error = ENODEV;
1724323e9233Smiod goto failsilent;
1725323e9233Smiod }
1726323e9233Smiod
1727323e9233Smiod /* still no go; reset the bastard */
1728323e9233Smiod fdc->sc_state = DORESET;
1729323e9233Smiod break;
1730323e9233Smiod
1731323e9233Smiod default:
1732323e9233Smiod fail:
1733323e9233Smiod if ((fd->sc_opts & FDOPT_SILENT) == 0) {
1734323e9233Smiod diskerr(bp, "fd", "hard error", LOG_PRINTF,
1735323e9233Smiod fd->sc_skip / FD_BSIZE(fd),
1736323e9233Smiod (struct disklabel *)NULL);
1737323e9233Smiod printf("\n");
1738323e9233Smiod fdcstatus(fdc, "controller status");
1739323e9233Smiod }
1740323e9233Smiod
1741323e9233Smiod failsilent:
1742323e9233Smiod bp->b_flags |= B_ERROR;
1743323e9233Smiod bp->b_error = error;
174427f37992Sdlg bp->b_resid = bp->b_bcount;
1745323e9233Smiod fdfinish(fd, bp);
1746323e9233Smiod }
1747323e9233Smiod fdc->sc_errors++;
1748323e9233Smiod }
1749323e9233Smiod
17501abdbfdeSderaadt daddr_t
fdsize(dev_t dev)1751bd535c51Sclaudio fdsize(dev_t dev)
1752323e9233Smiod {
1753323e9233Smiod
1754323e9233Smiod /* Swapping to floppies would not make sense. */
1755323e9233Smiod return (-1);
1756323e9233Smiod }
1757323e9233Smiod
1758323e9233Smiod int
fddump(dev_t dev,daddr_t blkno,caddr_t va,size_t size)1759bd535c51Sclaudio fddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1760323e9233Smiod {
1761323e9233Smiod
1762323e9233Smiod /* Not implemented. */
1763323e9233Smiod return (EINVAL);
1764323e9233Smiod }
1765323e9233Smiod
1766323e9233Smiod int
fdioctl(dev_t dev,u_long cmd,caddr_t addr,int flag,struct proc * p)1767bd535c51Sclaudio fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
1768323e9233Smiod {
1769323e9233Smiod struct fd_softc *fd;
1770323e9233Smiod struct fdc_softc *fdc;
1771e6586b6fSkrw struct disklabel *lp;
1772323e9233Smiod int unit;
1773323e9233Smiod int error;
1774323e9233Smiod #ifdef FD_DEBUG
1775323e9233Smiod int i;
1776323e9233Smiod #endif
1777323e9233Smiod
1778323e9233Smiod unit = FDUNIT(dev);
1779323e9233Smiod if (unit >= fd_cd.cd_ndevs)
1780323e9233Smiod return (ENXIO);
1781323e9233Smiod
1782323e9233Smiod fd = fd_cd.cd_devs[FDUNIT(dev)];
1783323e9233Smiod fdc = (struct fdc_softc *)fd->sc_dv.dv_parent;
1784323e9233Smiod
1785323e9233Smiod switch (cmd) {
1786e6586b6fSkrw case DIOCRLDINFO:
1787e6586b6fSkrw lp = malloc(sizeof(*lp), M_TEMP, M_WAITOK);
1788e6586b6fSkrw fdgetdisklabel(dev, fd, lp, 0);
1789e6586b6fSkrw bcopy(lp, fd->sc_dk.dk_label, sizeof(*lp));
1790f8e6c425Stedu free(lp, M_TEMP, 0);
1791e6586b6fSkrw return 0;
1792e6586b6fSkrw
1793e6586b6fSkrw case DIOCGPDINFO:
1794e6586b6fSkrw fdgetdisklabel(dev, fd, (struct disklabel *)addr, 1);
1795e6586b6fSkrw return 0;
1796e6586b6fSkrw
1797323e9233Smiod case DIOCGDINFO:
1798323e9233Smiod *(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1799323e9233Smiod return 0;
1800323e9233Smiod
1801e6586b6fSkrw case DIOCGPART:
1802e6586b6fSkrw ((struct partinfo *)addr)->disklab = fd->sc_dk.dk_label;
1803e6586b6fSkrw ((struct partinfo *)addr)->part =
1804e6586b6fSkrw &fd->sc_dk.dk_label->d_partitions[DISKPART(dev)];
1805e6586b6fSkrw return 0;
1806e6586b6fSkrw
1807323e9233Smiod case DIOCWDINFO:
18082e4cb731Skrw case DIOCSDINFO:
1809323e9233Smiod if ((flag & FWRITE) == 0)
1810323e9233Smiod return (EBADF);
1811323e9233Smiod
1812323e9233Smiod error = setdisklabel(fd->sc_dk.dk_label,
1813c326f117Sderaadt (struct disklabel *)addr, 0);
18142e4cb731Skrw if (error == 0) {
18152e4cb731Skrw if (cmd == DIOCWDINFO)
18162e4cb731Skrw error = writedisklabel(DISKLABELDEV(dev),
18172e4cb731Skrw fdstrategy, fd->sc_dk.dk_label);
18182e4cb731Skrw }
1819323e9233Smiod return (error);
1820323e9233Smiod
1821323e9233Smiod case DIOCLOCK:
1822323e9233Smiod /*
1823323e9233Smiod * Nothing to do here, really.
1824323e9233Smiod */
1825323e9233Smiod return (0);
1826323e9233Smiod
1827323e9233Smiod case MTIOCTOP:
1828323e9233Smiod if (((struct mtop *)addr)->mt_op != MTOFFL)
1829323e9233Smiod return (EIO);
1830323e9233Smiod /* FALLTHROUGH */
1831323e9233Smiod case DIOCEJECT:
1832323e9233Smiod if (fdc->sc_flags & FDC_NOEJECT)
1833323e9233Smiod return (ENODEV);
1834323e9233Smiod fd_do_eject(fd);
1835323e9233Smiod return (0);
1836323e9233Smiod
1837323e9233Smiod case FD_FORM:
1838323e9233Smiod if ((flag & FWRITE) == 0)
1839323e9233Smiod return (EBADF); /* must be opened for writing */
1840323e9233Smiod else if (((struct fd_formb *)addr)->format_version !=
1841323e9233Smiod FD_FORMAT_VERSION)
1842323e9233Smiod return (EINVAL);/* wrong version of formatting prog */
1843323e9233Smiod else
1844323e9233Smiod return fdformat(dev, (struct fd_formb *)addr, p);
1845323e9233Smiod break;
1846323e9233Smiod
1847323e9233Smiod case FD_GTYPE: /* get drive options */
1848323e9233Smiod *(struct fd_type *)addr = *fd->sc_type;
1849323e9233Smiod return (0);
1850323e9233Smiod
1851323e9233Smiod case FD_GOPTS: /* get drive options */
1852323e9233Smiod *(int *)addr = fd->sc_opts;
1853323e9233Smiod return (0);
1854323e9233Smiod
1855323e9233Smiod case FD_SOPTS: /* set drive options */
1856323e9233Smiod fd->sc_opts = *(int *)addr;
1857323e9233Smiod return (0);
1858323e9233Smiod
1859323e9233Smiod #ifdef FD_DEBUG
1860323e9233Smiod case _IO('f', 100):
1861323e9233Smiod fdc_wrfifo(fdc, NE7CMD_DUMPREG);
1862323e9233Smiod fdcresult(fdc);
1863323e9233Smiod printf("fdc: dumpreg(%d regs): <", fdc->sc_nstat);
1864323e9233Smiod for (i = 0; i < fdc->sc_nstat; i++)
1865323e9233Smiod printf(" 0x%x", fdc->sc_status[i]);
1866323e9233Smiod printf(">\n");
1867323e9233Smiod return (0);
1868323e9233Smiod
1869323e9233Smiod case _IOW('f', 101, int):
1870323e9233Smiod fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1871323e9233Smiod fdc->sc_cfg |= (*(int *)addr & CFG_THRHLD_MASK);
1872323e9233Smiod fdconf(fdc);
1873323e9233Smiod return (0);
1874323e9233Smiod
1875323e9233Smiod case _IO('f', 102):
1876323e9233Smiod fdc_wrfifo(fdc, NE7CMD_SENSEI);
1877323e9233Smiod fdcresult(fdc);
1878323e9233Smiod printf("fdc: sensei(%d regs): <", fdc->sc_nstat);
1879323e9233Smiod for (i=0; i< fdc->sc_nstat; i++)
1880323e9233Smiod printf(" 0x%x", fdc->sc_status[i]);
1881323e9233Smiod printf(">\n");
1882323e9233Smiod return (0);
1883323e9233Smiod #endif
1884323e9233Smiod default:
1885323e9233Smiod return (ENOTTY);
1886323e9233Smiod }
1887323e9233Smiod }
1888323e9233Smiod
1889323e9233Smiod int
fdformat(dev_t dev,struct fd_formb * finfo,struct proc * p)1890bd535c51Sclaudio fdformat(dev_t dev, struct fd_formb *finfo, struct proc *p)
1891323e9233Smiod {
1892323e9233Smiod int rv = 0;
1893323e9233Smiod struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1894323e9233Smiod struct fd_type *type = fd->sc_type;
1895323e9233Smiod struct buf *bp;
1896323e9233Smiod
1897323e9233Smiod /* set up a buffer header for fdstrategy() */
18987107272fSkrw bp = malloc(sizeof(*bp), M_TEMP, M_NOWAIT | M_ZERO);
1899323e9233Smiod if (bp == NULL)
1900323e9233Smiod return (ENOBUFS);
1901323e9233Smiod
1902c10f2b41Sderaadt bp->b_flags = B_BUSY | B_PHYS | B_FORMAT | B_RAW;
1903323e9233Smiod bp->b_proc = p;
1904323e9233Smiod bp->b_dev = dev;
1905323e9233Smiod
1906323e9233Smiod /*
1907323e9233Smiod * Calculate a fake blkno, so fdstrategy() would initiate a
1908323e9233Smiod * seek to the requested cylinder.
1909323e9233Smiod */
1910323e9233Smiod bp->b_blkno = ((finfo->cyl * (type->sectrac * type->heads)
1911323e9233Smiod + finfo->head * type->sectrac) * FD_BSIZE(fd))
1912323e9233Smiod / DEV_BSIZE;
1913323e9233Smiod
1914323e9233Smiod bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1915323e9233Smiod bp->b_data = (caddr_t)finfo;
1916323e9233Smiod
1917323e9233Smiod #ifdef FD_DEBUG
1918323e9233Smiod if (fdc_debug) {
1919323e9233Smiod int i;
1920323e9233Smiod
1921323e9233Smiod printf("fdformat: blkno 0x%llx count %ld\n",
1922323e9233Smiod (unsigned long long)bp->b_blkno, bp->b_bcount);
1923323e9233Smiod
1924323e9233Smiod printf("\tcyl:\t%d\n", finfo->cyl);
1925323e9233Smiod printf("\thead:\t%d\n", finfo->head);
1926323e9233Smiod printf("\tnsecs:\t%d\n", finfo->fd_formb_nsecs);
1927323e9233Smiod printf("\tsshft:\t%d\n", finfo->fd_formb_secshift);
1928323e9233Smiod printf("\tgaplen:\t%d\n", finfo->fd_formb_gaplen);
1929323e9233Smiod printf("\ttrack data:");
1930323e9233Smiod for (i = 0; i < finfo->fd_formb_nsecs; i++) {
1931323e9233Smiod printf(" [c%d h%d s%d]",
1932323e9233Smiod finfo->fd_formb_cylno(i),
1933323e9233Smiod finfo->fd_formb_headno(i),
1934323e9233Smiod finfo->fd_formb_secno(i) );
1935323e9233Smiod if (finfo->fd_formb_secsize(i) != 2)
1936323e9233Smiod printf("<sz:%d>", finfo->fd_formb_secsize(i));
1937323e9233Smiod }
1938323e9233Smiod printf("\n");
1939323e9233Smiod }
1940323e9233Smiod #endif
1941323e9233Smiod
1942323e9233Smiod /* now do the format */
1943323e9233Smiod fdstrategy(bp);
1944323e9233Smiod
1945323e9233Smiod /* ...and wait for it to complete */
1946323e9233Smiod rv = biowait(bp);
1947f8e6c425Stedu free(bp, M_TEMP, 0);
1948323e9233Smiod return (rv);
1949323e9233Smiod }
1950323e9233Smiod
1951df591ed6Sderaadt int
fdgetdisklabel(dev_t dev,struct fd_softc * fd,struct disklabel * lp,int spoofonly)1952190f1464Skrw fdgetdisklabel(dev_t dev, struct fd_softc *fd, struct disklabel *lp,
1953190f1464Skrw int spoofonly)
1954323e9233Smiod {
1955323e9233Smiod bzero(lp, sizeof(struct disklabel));
1956323e9233Smiod
1957323e9233Smiod lp->d_type = DTYPE_FLOPPY;
1958323e9233Smiod lp->d_secsize = FD_BSIZE(fd);
1959323e9233Smiod lp->d_secpercyl = fd->sc_type->seccyl;
1960323e9233Smiod lp->d_nsectors = fd->sc_type->sectrac;
1961323e9233Smiod lp->d_ncylinders = fd->sc_type->tracks;
1962323e9233Smiod lp->d_ntracks = fd->sc_type->heads; /* Go figure... */
19631c51b504Skrw DL_SETDSIZE(lp, (u_int64_t)lp->d_secpercyl * lp->d_ncylinders);
1964323e9233Smiod
1965323e9233Smiod strncpy(lp->d_typename, "floppy disk", sizeof(lp->d_typename));
1966323e9233Smiod strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
196722230921Sderaadt lp->d_version = 1;
1968323e9233Smiod
1969323e9233Smiod lp->d_magic = DISKMAGIC;
1970323e9233Smiod lp->d_magic2 = DISKMAGIC;
1971323e9233Smiod lp->d_checksum = dkcksum(lp);
1972323e9233Smiod
1973323e9233Smiod /*
1974323e9233Smiod * Call the generic disklabel extraction routine. If there's
1975323e9233Smiod * not a label there, fake it.
1976323e9233Smiod */
1977df591ed6Sderaadt return readdisklabel(DISKLABELDEV(dev), fdstrategy, lp, spoofonly);
1978323e9233Smiod }
1979323e9233Smiod
1980323e9233Smiod void
fd_do_eject(struct fd_softc * fd)1981bd535c51Sclaudio fd_do_eject(struct fd_softc *fd)
1982323e9233Smiod {
1983323e9233Smiod struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
1984323e9233Smiod bus_space_tag_t t = fdc->sc_bustag;
1985323e9233Smiod bus_space_handle_t h = fdc->sc_handle;
1986323e9233Smiod u_int8_t dor = FDO_FRST | FDO_FDMAEN | FDO_MOEN(0);
1987323e9233Smiod
1988323e9233Smiod bus_space_write_1(t, h, FDREG77_DOR, dor | FDO_EJ);
1989323e9233Smiod delay(10);
1990323e9233Smiod bus_space_write_1(t, h, FDREG77_DOR, FDO_FRST | FDO_DS);
1991323e9233Smiod }
1992