1*360c7349Sandvar /* $NetBSD: fd.c,v 1.52 2023/08/29 21:55:11 andvar Exp $ */
2459f2585Sur /* $OpenBSD: fd.c,v 1.6 1998/10/03 21:18:57 millert Exp $ */
3459f2585Sur /* NetBSD: fd.c,v 1.78 1995/07/04 07:23:09 mycroft Exp */
4459f2585Sur
5459f2585Sur /*-
6459f2585Sur * Copyright (c) 1998 The NetBSD Foundation, Inc.
7459f2585Sur * All rights reserved.
8459f2585Sur *
9459f2585Sur * This code is derived from software contributed to The NetBSD Foundation
10459f2585Sur * by Charles M. Hannum.
11459f2585Sur *
12459f2585Sur * Redistribution and use in source and binary forms, with or without
13459f2585Sur * modification, are permitted provided that the following conditions
14459f2585Sur * are met:
15459f2585Sur * 1. Redistributions of source code must retain the above copyright
16459f2585Sur * notice, this list of conditions and the following disclaimer.
17459f2585Sur * 2. Redistributions in binary form must reproduce the above copyright
18459f2585Sur * notice, this list of conditions and the following disclaimer in the
19459f2585Sur * documentation and/or other materials provided with the distribution.
20459f2585Sur *
21459f2585Sur * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22459f2585Sur * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23459f2585Sur * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24459f2585Sur * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25459f2585Sur * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26459f2585Sur * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27459f2585Sur * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28459f2585Sur * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29459f2585Sur * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30459f2585Sur * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31459f2585Sur * POSSIBILITY OF SUCH DAMAGE.
32459f2585Sur */
33459f2585Sur
34459f2585Sur /*-
35459f2585Sur * Copyright (c) 1990 The Regents of the University of California.
36459f2585Sur * All rights reserved.
37459f2585Sur *
38459f2585Sur * This code is derived from software contributed to Berkeley by
39459f2585Sur * Don Ahn.
40459f2585Sur *
41459f2585Sur * Redistribution and use in source and binary forms, with or without
42459f2585Sur * modification, are permitted provided that the following conditions
43459f2585Sur * are met:
44459f2585Sur * 1. Redistributions of source code must retain the above copyright
45459f2585Sur * notice, this list of conditions and the following disclaimer.
46459f2585Sur * 2. Redistributions in binary form must reproduce the above copyright
47459f2585Sur * notice, this list of conditions and the following disclaimer in the
48459f2585Sur * documentation and/or other materials provided with the distribution.
49aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
50459f2585Sur * may be used to endorse or promote products derived from this software
51459f2585Sur * without specific prior written permission.
52459f2585Sur *
53459f2585Sur * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54459f2585Sur * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55459f2585Sur * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56459f2585Sur * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57459f2585Sur * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58459f2585Sur * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59459f2585Sur * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60459f2585Sur * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61459f2585Sur * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62459f2585Sur * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63459f2585Sur * SUCH DAMAGE.
64459f2585Sur *
65459f2585Sur * @(#)fd.c 7.4 (Berkeley) 5/25/91
66459f2585Sur */
67459f2585Sur
68a4183603Slukem #include <sys/cdefs.h>
69*360c7349Sandvar __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.52 2023/08/29 21:55:11 andvar Exp $");
70a4183603Slukem
71459f2585Sur #include <sys/param.h>
72459f2585Sur #include <sys/systm.h>
73459f2585Sur #include <sys/callout.h>
74459f2585Sur #include <sys/kernel.h>
75459f2585Sur #include <sys/conf.h>
76459f2585Sur #include <sys/file.h>
77459f2585Sur #include <sys/ioctl.h>
78459f2585Sur #include <sys/device.h>
79459f2585Sur #include <sys/disklabel.h>
80459f2585Sur #include <sys/disk.h>
81459f2585Sur #include <sys/buf.h>
8205f25dccSyamt #include <sys/bufq.h>
83459f2585Sur #include <sys/uio.h>
84459f2585Sur #include <sys/syslog.h>
85459f2585Sur #include <sys/queue.h>
86459f2585Sur
87459f2585Sur #include <uvm/uvm_extern.h>
88459f2585Sur
8994bba098Ssoda #include <dev/cons.h>
9094bba098Ssoda
91cf10107dSdyoung #include <sys/bus.h>
92459f2585Sur #include <machine/cpu.h>
93459f2585Sur
94459f2585Sur #include <arc/jazz/fdreg.h>
9594bba098Ssoda #include <arc/jazz/fdcvar.h>
96459f2585Sur
97c234c43aStsutsui #include "ioconf.h"
98459f2585Sur #include "locators.h"
99459f2585Sur
10094bba098Ssoda #define FDUNIT(dev) DISKUNIT(dev)
10194bba098Ssoda #define FDTYPE(dev) DISKPART(dev)
102459f2585Sur
103459f2585Sur /* controller driver configuration */
1040f31d9deStsutsui static int fdprint(void *, const char *);
105459f2585Sur
106459f2585Sur /*
107459f2585Sur * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
108459f2585Sur * we tell them apart.
109459f2585Sur */
110459f2585Sur struct fd_type {
111459f2585Sur int sectrac; /* sectors per track */
112459f2585Sur int heads; /* number of heads */
113459f2585Sur int seccyl; /* sectors per cylinder */
114459f2585Sur int secsize; /* size code for sectors */
115459f2585Sur int datalen; /* data len when secsize = 0 */
116459f2585Sur int steprate; /* step rate and head unload time */
117459f2585Sur int gap1; /* gap len between sectors */
118459f2585Sur int gap2; /* formatting gap */
11994bba098Ssoda int cyls; /* total num of cylinders */
120459f2585Sur int size; /* size of disk in sectors */
121459f2585Sur int step; /* steps per cylinder */
122459f2585Sur int rate; /* transfer speed code */
12394bba098Ssoda const char *name;
124459f2585Sur };
125459f2585Sur
126459f2585Sur /* The order of entries in the following table is important -- BEWARE! */
1270f31d9deStsutsui const static struct fd_type fd_types[] = {
12859dafa2cStsutsui /* 1.44MB diskette */
12959dafa2cStsutsui { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB" },
13059dafa2cStsutsui /* 1.2 MB AT-diskettes */
13159dafa2cStsutsui { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB" },
13259dafa2cStsutsui /* 360kB in 1.2MB drive */
13359dafa2cStsutsui { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" },
13459dafa2cStsutsui /* 360kB PC diskettes */
13559dafa2cStsutsui { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" },
13659dafa2cStsutsui /* 3.5" 720kB diskette */
13759dafa2cStsutsui { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB" },
13859dafa2cStsutsui /* 720kB in 1.2MB drive */
13959dafa2cStsutsui { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x" },
14059dafa2cStsutsui /* 360kB in 720kB drive */
14159dafa2cStsutsui { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x" },
142459f2585Sur };
143459f2585Sur
144459f2585Sur /* software state, per disk (with up to 4 disks per ctlr) */
145459f2585Sur struct fd_softc {
1460f31d9deStsutsui device_t sc_dev;
147459f2585Sur struct disk sc_dk;
148459f2585Sur
14994bba098Ssoda const struct fd_type *sc_deftype; /* default type descriptor */
150459f2585Sur struct fd_type *sc_type; /* current type descriptor */
15194bba098Ssoda struct fd_type sc_type_copy; /* copy for fiddling when formatting */
152459f2585Sur
153459f2585Sur struct callout sc_motoron_ch;
154459f2585Sur struct callout sc_motoroff_ch;
155459f2585Sur
156459f2585Sur daddr_t sc_blkno; /* starting block number */
157459f2585Sur int sc_bcount; /* byte count left */
15894bba098Ssoda int sc_opts; /* user-set options */
159459f2585Sur int sc_skip; /* bytes already transferred */
160cc0b6809Swiz int sc_nblks; /* number of blocks currently transferring */
161cc0b6809Swiz int sc_nbytes; /* number of bytes currently transferring */
162459f2585Sur
163459f2585Sur int sc_drive; /* physical unit number */
164459f2585Sur int sc_flags;
165459f2585Sur #define FD_OPEN 0x01 /* it's open */
166459f2585Sur #define FD_MOTOR 0x02 /* motor should be on */
167459f2585Sur #define FD_MOTOR_WAIT 0x04 /* motor coming up */
168459f2585Sur int sc_cylin; /* where we think the head is */
169459f2585Sur
170459f2585Sur TAILQ_ENTRY(fd_softc) sc_drivechain;
171459f2585Sur int sc_ops; /* I/O ops since last switch */
172aec75b1cSyamt struct bufq_state *sc_q;/* pending I/O requests */
173459f2585Sur int sc_active; /* number of active I/O operations */
174459f2585Sur };
175459f2585Sur
176459f2585Sur /* floppy driver configuration */
1770f31d9deStsutsui static int fdprobe(device_t, cfdata_t, void *);
1780f31d9deStsutsui static void fdattach(device_t, device_t, void *);
179459f2585Sur
1800f31d9deStsutsui CFATTACH_DECL_NEW(fd, sizeof(struct fd_softc), fdprobe, fdattach, NULL, NULL);
181459f2585Sur
18277a6b82bSgehenna dev_type_open(fdopen);
18377a6b82bSgehenna dev_type_close(fdclose);
18477a6b82bSgehenna dev_type_read(fdread);
18577a6b82bSgehenna dev_type_write(fdwrite);
18677a6b82bSgehenna dev_type_ioctl(fdioctl);
18777a6b82bSgehenna dev_type_strategy(fdstrategy);
18877a6b82bSgehenna
18977a6b82bSgehenna const struct bdevsw fd_bdevsw = {
190a68f9396Sdholland .d_open = fdopen,
191a68f9396Sdholland .d_close = fdclose,
192a68f9396Sdholland .d_strategy = fdstrategy,
193a68f9396Sdholland .d_ioctl = fdioctl,
194a68f9396Sdholland .d_dump = nodump,
195a68f9396Sdholland .d_psize = nosize,
1968c70ef39Sdholland .d_discard = nodiscard,
197a68f9396Sdholland .d_flag = D_DISK
19877a6b82bSgehenna };
19977a6b82bSgehenna
20077a6b82bSgehenna const struct cdevsw fd_cdevsw = {
201a68f9396Sdholland .d_open = fdopen,
202a68f9396Sdholland .d_close = fdclose,
203a68f9396Sdholland .d_read = fdread,
204a68f9396Sdholland .d_write = fdwrite,
205a68f9396Sdholland .d_ioctl = fdioctl,
206a68f9396Sdholland .d_stop = nostop,
207a68f9396Sdholland .d_tty = notty,
208a68f9396Sdholland .d_poll = nopoll,
209a68f9396Sdholland .d_mmap = nommap,
210a68f9396Sdholland .d_kqfilter = nokqfilter,
211f9228f42Sdholland .d_discard = nodiscard,
212a68f9396Sdholland .d_flag = D_DISK
21377a6b82bSgehenna };
21477a6b82bSgehenna
2150f31d9deStsutsui static void fdstart(struct fd_softc *);
216459f2585Sur
2176f00c789Smlelstv struct dkdriver fddkdriver = {
2186f00c789Smlelstv .d_strategy = fdstrategy
2196f00c789Smlelstv };
220459f2585Sur
2214ee6b340Stsutsui static bool fd_shutdown(device_t, int);
22294bba098Ssoda #if 0
2230f31d9deStsutsui static const struct fd_type *fd_nvtotype(char *, int, int);
22494bba098Ssoda #endif
2250f31d9deStsutsui static void fd_set_motor(struct fdc_softc *, int);
2260f31d9deStsutsui static void fd_motor_off(void *);
2270f31d9deStsutsui static void fd_motor_on(void *);
2280f31d9deStsutsui static int fdcresult(struct fdc_softc *);
2290f31d9deStsutsui static void fdcstart(struct fdc_softc *);
2300f31d9deStsutsui static void fdcstatus(device_t, int, const char *);
2310f31d9deStsutsui static void fdctimeout(void *);
2320f31d9deStsutsui static void fdcpseudointr(void *);
2330f31d9deStsutsui static void fdcretry(struct fdc_softc *);
2340f31d9deStsutsui static void fdfinish(struct fd_softc *, struct buf *);
2358255ce9eSmrg static const struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
2360f31d9deStsutsui static void fd_mountroot_hook(device_t);
237459f2585Sur
238459f2585Sur /*
239459f2585Sur * Arguments passed between fdcattach and fdprobe.
240459f2585Sur */
241459f2585Sur struct fdc_attach_args {
242459f2585Sur int fa_drive;
24394bba098Ssoda const struct fd_type *fa_deftype;
244459f2585Sur };
245459f2585Sur
246459f2585Sur /*
247459f2585Sur * Print the location of a disk drive (called just before attaching the
248459f2585Sur * the drive). If `fdc' is not NULL, the drive was found but was not
249459f2585Sur * in the system config file; print the drive name as well.
250459f2585Sur * Return QUIET (config_find ignores this if the device was configured) to
251459f2585Sur * avoid printing `fdN not configured' messages.
252459f2585Sur */
2530f31d9deStsutsui static int
fdprint(void * aux,const char * fdc)2547fe2a5a0Stsutsui fdprint(void *aux, const char *fdc)
255459f2585Sur {
2568e19dfb2Stsutsui struct fdc_attach_args *fa = aux;
257459f2585Sur
2580f31d9deStsutsui if (fdc == NULL)
259aec1389bSthorpej aprint_normal(" drive %d", fa->fa_drive);
260459f2585Sur return QUIET;
261459f2585Sur }
262459f2585Sur
263459f2585Sur void
fdcattach(struct fdc_softc * fdc)2647fe2a5a0Stsutsui fdcattach(struct fdc_softc *fdc)
265459f2585Sur {
266459f2585Sur struct fdc_attach_args fa;
267459f2585Sur int type;
268459f2585Sur
26988ab7da9Sad callout_init(&fdc->sc_timo_ch, 0);
27088ab7da9Sad callout_init(&fdc->sc_intr_ch, 0);
271459f2585Sur
27294bba098Ssoda fdc->sc_state = DEVIDLE;
27394bba098Ssoda TAILQ_INIT(&fdc->sc_drives);
274459f2585Sur
275459f2585Sur /*
276459f2585Sur * No way yet to determine default disk types.
277459f2585Sur * we assume 1.44 3.5" type for the moment.
278459f2585Sur */
279459f2585Sur type = 0;
280459f2585Sur
281459f2585Sur /* physical limit: two drives per controller. */
282459f2585Sur for (fa.fa_drive = 0; fa.fa_drive < 2; fa.fa_drive++) {
28394bba098Ssoda fa.fa_deftype = &fd_types[type];
2842685996bSthorpej (void)config_found(fdc->sc_dev, (void *)&fa, fdprint,
285c7fb772bSthorpej CFARGS_NONE);
286459f2585Sur }
287459f2585Sur }
288459f2585Sur
2890f31d9deStsutsui static int
fdprobe(device_t parent,cfdata_t cf,void * aux)2900f31d9deStsutsui fdprobe(device_t parent, cfdata_t cf , void *aux)
291459f2585Sur {
2920f31d9deStsutsui struct fdc_softc *fdc = device_private(parent);
293459f2585Sur struct fdc_attach_args *fa = aux;
294459f2585Sur int drive = fa->fa_drive;
29594bba098Ssoda bus_space_tag_t iot = fdc->sc_iot;
29694bba098Ssoda bus_space_handle_t ioh = fdc->sc_ioh;
297459f2585Sur int n;
298459f2585Sur
29994bba098Ssoda if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
30094bba098Ssoda cf->cf_loc[FDCCF_DRIVE] != drive)
301459f2585Sur return 0;
302459f2585Sur
303459f2585Sur /* select drive and turn on motor */
30494bba098Ssoda bus_space_write_1(iot, ioh, FDOUT, drive | FDO_FRST | FDO_MOEN(drive));
305459f2585Sur /* wait for motor to spin up */
30694bba098Ssoda delay(250000);
30794bba098Ssoda out_fdc(iot, ioh, NE7CMD_RECAL);
30894bba098Ssoda out_fdc(iot, ioh, drive);
309459f2585Sur /* wait for recalibrate */
310459f2585Sur delay(2000000);
31194bba098Ssoda out_fdc(iot, ioh, NE7CMD_SENSEI);
312459f2585Sur n = fdcresult(fdc);
313459f2585Sur #ifdef FD_DEBUG
314459f2585Sur {
315459f2585Sur int i;
3160f31d9deStsutsui aprint_debug("%s: status", __func__);
317459f2585Sur for (i = 0; i < n; i++)
3180f31d9deStsutsui aprint_debug(" %x", fdc->sc_status[i]);
3190f31d9deStsutsui aprint_debug("\n");
320459f2585Sur }
321459f2585Sur #endif
322459f2585Sur if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
323459f2585Sur return 0;
324459f2585Sur /* turn off motor */
32594bba098Ssoda bus_space_write_1(iot, ioh, FDOUT, FDO_FRST);
326459f2585Sur
327459f2585Sur return 1;
328459f2585Sur }
329459f2585Sur
330459f2585Sur /*
331459f2585Sur * Controller is working, and drive responded. Attach it.
332459f2585Sur */
333459f2585Sur void
fdattach(device_t parent,device_t self,void * aux)3340f31d9deStsutsui fdattach(device_t parent, device_t self, void *aux)
335459f2585Sur {
3360f31d9deStsutsui struct fdc_softc *fdc = device_private(parent);
3370f31d9deStsutsui struct fd_softc *fd = device_private(self);
338459f2585Sur struct fdc_attach_args *fa = aux;
33994bba098Ssoda const struct fd_type *type = fa->fa_deftype;
340459f2585Sur int drive = fa->fa_drive;
341459f2585Sur
3420f31d9deStsutsui fd->sc_dev = self;
3430f31d9deStsutsui
34488ab7da9Sad callout_init(&fd->sc_motoron_ch, 0);
34588ab7da9Sad callout_init(&fd->sc_motoroff_ch, 0);
346459f2585Sur
347459f2585Sur /* XXX Allow `flags' to override device type? */
348459f2585Sur
349459f2585Sur if (type)
35094bba098Ssoda printf(": %s, %d cyl, %d head, %d sec\n", type->name,
35194bba098Ssoda type->cyls, type->heads, type->sectrac);
352459f2585Sur else
353459f2585Sur printf(": density unknown\n");
354459f2585Sur
355aec75b1cSyamt bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
356459f2585Sur fd->sc_cylin = -1;
357459f2585Sur fd->sc_drive = drive;
358459f2585Sur fd->sc_deftype = type;
359459f2585Sur fdc->sc_fd[drive] = fd;
36094bba098Ssoda
36194bba098Ssoda /*
36294bba098Ssoda * Initialize and attach the disk structure.
36394bba098Ssoda */
3640f31d9deStsutsui disk_init(&fd->sc_dk, device_xname(fd->sc_dev), &fddkdriver);
36594bba098Ssoda disk_attach(&fd->sc_dk);
36694bba098Ssoda
36794bba098Ssoda /* Establish a mountroot hook. */
3680f31d9deStsutsui mountroothook_establish(fd_mountroot_hook, fd->sc_dev);
369459f2585Sur
370459f2585Sur /* Needed to power off if the motor is on when we halt. */
3714ee6b340Stsutsui if (!pmf_device_register1(self, NULL, NULL, fd_shutdown))
3724ee6b340Stsutsui aprint_error_dev(self, "couldn't establish power handler\n");
3734ee6b340Stsutsui }
3744ee6b340Stsutsui
3754ee6b340Stsutsui bool
fd_shutdown(device_t self,int howto)3764ee6b340Stsutsui fd_shutdown(device_t self, int howto)
3774ee6b340Stsutsui {
3784ee6b340Stsutsui struct fd_softc *fd;
3794ee6b340Stsutsui
3804ee6b340Stsutsui fd = device_private(self);
3814ee6b340Stsutsui fd_motor_off(fd);
3824ee6b340Stsutsui
3834ee6b340Stsutsui return true;
384459f2585Sur }
385459f2585Sur
38694bba098Ssoda #if 0
387459f2585Sur /*
388459f2585Sur * Translate nvram type into internal data structure. Return NULL for
389459f2585Sur * none/unknown/unusable.
390459f2585Sur */
3910f31d9deStsutsui static const struct fd_type *
3927fe2a5a0Stsutsui fd_nvtotype(char *fdc, int nvraminfo, int drive)
393459f2585Sur {
394459f2585Sur int type;
395459f2585Sur
396459f2585Sur type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
397459f2585Sur #if 0
398459f2585Sur switch (type) {
399459f2585Sur case NVRAM_DISKETTE_NONE:
400459f2585Sur return NULL;
401459f2585Sur case NVRAM_DISKETTE_12M:
402459f2585Sur return &fd_types[1];
403459f2585Sur case NVRAM_DISKETTE_TYPE5:
404459f2585Sur case NVRAM_DISKETTE_TYPE6:
405459f2585Sur /* XXX We really ought to handle 2.88MB format. */
406459f2585Sur case NVRAM_DISKETTE_144M:
407459f2585Sur return &fd_types[0];
408459f2585Sur case NVRAM_DISKETTE_360K:
409459f2585Sur return &fd_types[3];
410459f2585Sur case NVRAM_DISKETTE_720K:
411459f2585Sur return &fd_types[4];
412459f2585Sur default:
413459f2585Sur printf("%s: drive %d: unknown device type 0x%x\n",
414459f2585Sur fdc, drive, type);
415459f2585Sur return NULL;
416459f2585Sur }
417459f2585Sur #else
418459f2585Sur return &fd_types[0]; /* Use only 1.44 for now */
419459f2585Sur #endif
420459f2585Sur }
42194bba098Ssoda #endif
42294bba098Ssoda
4238255ce9eSmrg static const struct fd_type *
fd_dev_to_type(struct fd_softc * fd,dev_t dev)4247fe2a5a0Stsutsui fd_dev_to_type(struct fd_softc *fd, dev_t dev)
42594bba098Ssoda {
42694bba098Ssoda int type = FDTYPE(dev);
42794bba098Ssoda
4280f31d9deStsutsui if (type > __arraycount(fd_types))
42994bba098Ssoda return NULL;
43094bba098Ssoda return type ? &fd_types[type - 1] : fd->sc_deftype;
43194bba098Ssoda }
432459f2585Sur
433459f2585Sur void
fdstrategy(struct buf * bp)4347fe2a5a0Stsutsui fdstrategy(struct buf *bp)
435459f2585Sur {
4365b30cb96Stsutsui struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(bp->b_dev));
437459f2585Sur int sz;
438459f2585Sur int s;
439459f2585Sur
440459f2585Sur /* Valid unit, controller, and request? */
44194bba098Ssoda if (bp->b_blkno < 0 ||
442459f2585Sur (bp->b_bcount % FDC_BSIZE) != 0) {
443459f2585Sur bp->b_error = EINVAL;
444e448d821She goto done;
445459f2585Sur }
446459f2585Sur
447459f2585Sur /* If it's a null transfer, return immediately. */
448459f2585Sur if (bp->b_bcount == 0)
449459f2585Sur goto done;
450459f2585Sur
451459f2585Sur sz = howmany(bp->b_bcount, FDC_BSIZE);
452459f2585Sur
453459f2585Sur if (bp->b_blkno + sz > fd->sc_type->size) {
454459f2585Sur sz = fd->sc_type->size - bp->b_blkno;
455459f2585Sur if (sz == 0) {
456459f2585Sur /* If exactly at end of disk, return EOF. */
457459f2585Sur goto done;
458459f2585Sur }
459459f2585Sur if (sz < 0) {
460459f2585Sur /* If past end of disk, return EINVAL. */
461459f2585Sur bp->b_error = EINVAL;
46266fefd11Sad goto done;
463459f2585Sur }
464459f2585Sur /* Otherwise, truncate request. */
465459f2585Sur bp->b_bcount = sz << DEV_BSHIFT;
466459f2585Sur }
467459f2585Sur
468459f2585Sur bp->b_rawblkno = bp->b_blkno;
46994bba098Ssoda bp->b_cylinder =
47094bba098Ssoda bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
471459f2585Sur
472459f2585Sur #ifdef FD_DEBUG
473*360c7349Sandvar printf("%s: b_blkno %" PRId64 " b_bcount %d blkno %" PRId64
474*360c7349Sandvar " cylin %d sz %d\n", __func__,
475459f2585Sur bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder, sz);
476459f2585Sur #endif
477459f2585Sur
478459f2585Sur /* Queue transfer on drive, activate drive and controller if idle. */
479459f2585Sur s = splbio();
48070de9736Syamt bufq_put(fd->sc_q, bp);
481459f2585Sur callout_stop(&fd->sc_motoroff_ch); /* a good idea */
482459f2585Sur if (fd->sc_active == 0)
483459f2585Sur fdstart(fd);
484459f2585Sur #ifdef DIAGNOSTIC
485459f2585Sur else {
4860f31d9deStsutsui struct fdc_softc *fdc =
4870f31d9deStsutsui device_private(device_parent(fd->sc_dev));
488459f2585Sur if (fdc->sc_state == DEVIDLE) {
4890f31d9deStsutsui printf("%s: controller inactive\n", __func__);
490459f2585Sur fdcstart(fdc);
491459f2585Sur }
492459f2585Sur }
493459f2585Sur #endif
494459f2585Sur splx(s);
495459f2585Sur return;
496459f2585Sur
497459f2585Sur done:
498459f2585Sur /* Toss transfer; we're done early. */
49994bba098Ssoda bp->b_resid = bp->b_bcount;
500459f2585Sur biodone(bp);
501459f2585Sur }
502459f2585Sur
503459f2585Sur void
fdstart(struct fd_softc * fd)5047fe2a5a0Stsutsui fdstart(struct fd_softc *fd)
505459f2585Sur {
5060f31d9deStsutsui struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
507ec0d5b63Stsutsui int active = TAILQ_FIRST(&fdc->sc_drives) != 0;
508459f2585Sur
509459f2585Sur /* Link into controller queue. */
510459f2585Sur fd->sc_active = 1;
511459f2585Sur TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
512459f2585Sur
513459f2585Sur /* If controller not already active, start it. */
514459f2585Sur if (!active)
515459f2585Sur fdcstart(fdc);
516459f2585Sur }
517459f2585Sur
518459f2585Sur void
fdfinish(struct fd_softc * fd,struct buf * bp)5197fe2a5a0Stsutsui fdfinish(struct fd_softc *fd, struct buf *bp)
520459f2585Sur {
5210f31d9deStsutsui struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
522459f2585Sur
523459f2585Sur /*
524459f2585Sur * Move this drive to the end of the queue to give others a `fair'
525459f2585Sur * chance. We only force a switch if N operations are completed while
526459f2585Sur * another drive is waiting to be serviced, since there is a long motor
527459f2585Sur * startup delay whenever we switch.
528459f2585Sur */
52970de9736Syamt (void)bufq_get(fd->sc_q);
5300f31d9deStsutsui if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) {
531459f2585Sur fd->sc_ops = 0;
532459f2585Sur TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
53370de9736Syamt if (bufq_peek(fd->sc_q) != NULL)
534459f2585Sur TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
53594bba098Ssoda else
536459f2585Sur fd->sc_active = 0;
537459f2585Sur }
538459f2585Sur bp->b_resid = fd->sc_bcount;
539459f2585Sur fd->sc_skip = 0;
540459f2585Sur biodone(bp);
541459f2585Sur /* turn off motor 5s from now */
54294bba098Ssoda callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
543459f2585Sur fdc->sc_state = DEVIDLE;
544459f2585Sur }
545459f2585Sur
546459f2585Sur int
fdread(dev_t dev,struct uio * uio,int flags)5477fe2a5a0Stsutsui fdread(dev_t dev, struct uio *uio, int flags)
548459f2585Sur {
549459f2585Sur
5507fe2a5a0Stsutsui return physio(fdstrategy, NULL, dev, B_READ, minphys, uio);
551459f2585Sur }
552459f2585Sur
553459f2585Sur int
fdwrite(dev_t dev,struct uio * uio,int flags)5547fe2a5a0Stsutsui fdwrite(dev_t dev, struct uio *uio, int flags)
555459f2585Sur {
556459f2585Sur
5577fe2a5a0Stsutsui return physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio);
558459f2585Sur }
559459f2585Sur
560459f2585Sur void
fd_set_motor(struct fdc_softc * fdc,int reset)5617fe2a5a0Stsutsui fd_set_motor(struct fdc_softc *fdc, int reset)
562459f2585Sur {
563459f2585Sur struct fd_softc *fd;
564459f2585Sur u_char status;
565459f2585Sur int n;
566459f2585Sur
567ec0d5b63Stsutsui if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
568459f2585Sur status = fd->sc_drive;
569459f2585Sur else
570459f2585Sur status = 0;
571459f2585Sur if (!reset)
572459f2585Sur status |= FDO_FRST | FDO_FDMAEN;
573459f2585Sur for (n = 0; n < 4; n++)
574459f2585Sur if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
575459f2585Sur status |= FDO_MOEN(n);
57694bba098Ssoda bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, FDOUT, status);
577459f2585Sur }
578459f2585Sur
579459f2585Sur void
fd_motor_off(void * arg)5807fe2a5a0Stsutsui fd_motor_off(void *arg)
581459f2585Sur {
582459f2585Sur struct fd_softc *fd = arg;
5830f31d9deStsutsui struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
584459f2585Sur int s;
585459f2585Sur
586459f2585Sur s = splbio();
587459f2585Sur fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
5880f31d9deStsutsui fd_set_motor(fdc, 0);
589459f2585Sur splx(s);
590459f2585Sur }
591459f2585Sur
592459f2585Sur void
fd_motor_on(void * arg)5937fe2a5a0Stsutsui fd_motor_on(void *arg)
594459f2585Sur {
595459f2585Sur struct fd_softc *fd = arg;
5960f31d9deStsutsui struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
597459f2585Sur int s;
598459f2585Sur
599459f2585Sur s = splbio();
600459f2585Sur fd->sc_flags &= ~FD_MOTOR_WAIT;
601ec0d5b63Stsutsui if ((TAILQ_FIRST(&fdc->sc_drives) == fd) &&
602ec0d5b63Stsutsui (fdc->sc_state == MOTORWAIT))
603459f2585Sur (void)fdcintr(fdc);
604459f2585Sur splx(s);
605459f2585Sur }
606459f2585Sur
607459f2585Sur int
fdcresult(struct fdc_softc * fdc)6087fe2a5a0Stsutsui fdcresult(struct fdc_softc *fdc)
609459f2585Sur {
61094bba098Ssoda bus_space_tag_t iot = fdc->sc_iot;
61194bba098Ssoda bus_space_handle_t ioh = fdc->sc_ioh;
612459f2585Sur u_char i;
6130f31d9deStsutsui int j, n = 0;
614459f2585Sur
6150f31d9deStsutsui for (j = 100000; j; j--) {
61694bba098Ssoda i = bus_space_read_1(iot, ioh, FDSTS) &
61794bba098Ssoda (NE7_DIO | NE7_RQM | NE7_CB);
61894bba098Ssoda if (i == NE7_RQM)
619459f2585Sur return n;
620459f2585Sur if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
621459f2585Sur if (n >= sizeof(fdc->sc_status)) {
6220f31d9deStsutsui log(LOG_ERR, "%s: overrun\n", __func__);
623459f2585Sur return -1;
624459f2585Sur }
62594bba098Ssoda fdc->sc_status[n++] =
62694bba098Ssoda bus_space_read_1(iot, ioh, FDDATA);
627459f2585Sur }
62894bba098Ssoda delay(10);
629459f2585Sur }
6300f31d9deStsutsui log(LOG_ERR, "%s: timeout\n", __func__);
631459f2585Sur return -1;
632459f2585Sur }
633459f2585Sur
634459f2585Sur int
out_fdc(bus_space_tag_t iot,bus_space_handle_t ioh,uint8_t x)6350f31d9deStsutsui out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t x)
636459f2585Sur {
637459f2585Sur int i = 100000;
638459f2585Sur
63994bba098Ssoda while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_DIO) && i-- > 0);
640459f2585Sur if (i <= 0)
641459f2585Sur return -1;
64294bba098Ssoda while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_RQM) == 0 && i-- > 0);
643459f2585Sur if (i <= 0)
644459f2585Sur return -1;
64594bba098Ssoda bus_space_write_1(iot, ioh, FDDATA, x);
646459f2585Sur return 0;
647459f2585Sur }
648459f2585Sur
649459f2585Sur int
fdopen(dev_t dev,int flags,int mode,struct lwp * l)65095e1ffb1Schristos fdopen(dev_t dev, int flags, int mode, struct lwp *l)
651459f2585Sur {
652459f2585Sur struct fd_softc *fd;
65394bba098Ssoda const struct fd_type *type;
654459f2585Sur
6555b30cb96Stsutsui fd = device_lookup_private(&fd_cd, FDUNIT(dev));
65694bba098Ssoda if (fd == NULL)
657459f2585Sur return ENXIO;
658459f2585Sur
65994bba098Ssoda type = fd_dev_to_type(fd, dev);
660459f2585Sur if (type == NULL)
661459f2585Sur return ENXIO;
662459f2585Sur
663459f2585Sur if ((fd->sc_flags & FD_OPEN) != 0 &&
66494bba098Ssoda memcmp(fd->sc_type, type, sizeof(*type)))
665459f2585Sur return EBUSY;
666459f2585Sur
66794bba098Ssoda fd->sc_type_copy = *type;
66894bba098Ssoda fd->sc_type = &fd->sc_type_copy;
669459f2585Sur fd->sc_cylin = -1;
670459f2585Sur fd->sc_flags |= FD_OPEN;
671459f2585Sur
672459f2585Sur return 0;
673459f2585Sur }
674459f2585Sur
675459f2585Sur int
fdclose(dev_t dev,int flags,int mode,struct lwp * l)67695e1ffb1Schristos fdclose(dev_t dev, int flags, int mode, struct lwp *l)
677459f2585Sur {
6785b30cb96Stsutsui struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(dev));
679459f2585Sur
680459f2585Sur fd->sc_flags &= ~FD_OPEN;
681459f2585Sur return 0;
682459f2585Sur }
683459f2585Sur
684459f2585Sur void
fdcstart(struct fdc_softc * fdc)6857fe2a5a0Stsutsui fdcstart(struct fdc_softc *fdc)
686459f2585Sur {
687459f2585Sur
688459f2585Sur #ifdef DIAGNOSTIC
689459f2585Sur /* only got here if controller's drive queue was inactive; should
690459f2585Sur be in idle state */
691459f2585Sur if (fdc->sc_state != DEVIDLE) {
6920f31d9deStsutsui printf("%s: not idle\n", __func__);
693459f2585Sur return;
694459f2585Sur }
695459f2585Sur #endif
696459f2585Sur (void)fdcintr(fdc);
697459f2585Sur }
698459f2585Sur
6999a5d3f28Schristos static void
fdcpstatus(int n,struct fdc_softc * fdc)7009a5d3f28Schristos fdcpstatus(int n, struct fdc_softc *fdc)
7019a5d3f28Schristos {
7029a5d3f28Schristos char bits[64];
7039a5d3f28Schristos
7049a5d3f28Schristos switch (n) {
7059a5d3f28Schristos case 0:
7069a5d3f28Schristos printf("\n");
7079a5d3f28Schristos break;
7089a5d3f28Schristos case 2:
7099a5d3f28Schristos snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]);
7109a5d3f28Schristos printf(" (st0 %s cyl %d)\n", bits, fdc->sc_status[1]);
7119a5d3f28Schristos break;
7129a5d3f28Schristos case 7:
7139a5d3f28Schristos snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]);
7149a5d3f28Schristos printf(" (st0 %s", bits);
7159a5d3f28Schristos snprintb(bits, sizeof(bits), NE7_ST1BITS, fdc->sc_status[1]);
7169a5d3f28Schristos printf(" st1 %s", bits);
7179a5d3f28Schristos snprintb(bits, sizeof(bits), NE7_ST2BITS, fdc->sc_status[2]);
7189a5d3f28Schristos printf(" st2 %s", bits);
7199a5d3f28Schristos printf(" cyl %d head %d sec %d)\n",
7209a5d3f28Schristos fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
7219a5d3f28Schristos break;
7229a5d3f28Schristos #ifdef DIAGNOSTIC
7239a5d3f28Schristos default:
7249a5d3f28Schristos printf("\nfdcstatus: weird size");
7259a5d3f28Schristos break;
7269a5d3f28Schristos #endif
7279a5d3f28Schristos }
7289a5d3f28Schristos }
7299a5d3f28Schristos
730459f2585Sur void
fdcstatus(device_t dev,int n,const char * s)7310f31d9deStsutsui fdcstatus(device_t dev, int n, const char *s)
732459f2585Sur {
7330f31d9deStsutsui struct fdc_softc *fdc = device_private(device_parent(dev));
734459f2585Sur
735459f2585Sur if (n == 0) {
73694bba098Ssoda out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
737459f2585Sur (void)fdcresult(fdc);
738459f2585Sur n = 2;
739459f2585Sur }
740459f2585Sur
7410f31d9deStsutsui printf("%s: %s", device_xname(dev), s);
7429a5d3f28Schristos fdcpstatus(n, fdc);
743459f2585Sur }
744459f2585Sur
745459f2585Sur void
fdctimeout(void * arg)7467fe2a5a0Stsutsui fdctimeout(void *arg)
747459f2585Sur {
748459f2585Sur struct fdc_softc *fdc = arg;
749ec0d5b63Stsutsui struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
750459f2585Sur int s;
751459f2585Sur
752459f2585Sur s = splbio();
75394bba098Ssoda #ifdef DEBUG
7540f31d9deStsutsui log(LOG_ERR, "%s: state %d\n", __func__, fdc->sc_state);
75594bba098Ssoda #endif
7560f31d9deStsutsui fdcstatus(fd->sc_dev, 0, "timeout");
757459f2585Sur
75870de9736Syamt if (bufq_peek(fd->sc_q) != NULL)
759459f2585Sur fdc->sc_state++;
760459f2585Sur else
761459f2585Sur fdc->sc_state = DEVIDLE;
762459f2585Sur
763459f2585Sur (void)fdcintr(fdc);
764459f2585Sur splx(s);
765459f2585Sur }
766459f2585Sur
767459f2585Sur void
fdcpseudointr(void * arg)7687fe2a5a0Stsutsui fdcpseudointr(void *arg)
769459f2585Sur {
770459f2585Sur int s;
771459f2585Sur
772459f2585Sur /* Just ensure it has the right spl. */
773459f2585Sur s = splbio();
774459f2585Sur (void)fdcintr(arg);
775459f2585Sur splx(s);
776459f2585Sur }
777459f2585Sur
778459f2585Sur int
fdcintr(void * arg)7797fe2a5a0Stsutsui fdcintr(void *arg)
780459f2585Sur {
781459f2585Sur struct fdc_softc *fdc = arg;
782459f2585Sur #define st0 fdc->sc_status[0]
783459f2585Sur #define cyl fdc->sc_status[1]
784459f2585Sur struct fd_softc *fd;
785459f2585Sur struct buf *bp;
78694bba098Ssoda bus_space_tag_t iot = fdc->sc_iot;
78794bba098Ssoda bus_space_handle_t ioh = fdc->sc_ioh;
788459f2585Sur int read, head, sec, i, nblks;
789459f2585Sur struct fd_type *type;
790459f2585Sur
791459f2585Sur loop:
792459f2585Sur /* Is there a drive for the controller to do a transfer with? */
793ec0d5b63Stsutsui fd = TAILQ_FIRST(&fdc->sc_drives);
794459f2585Sur if (fd == NULL) {
795459f2585Sur fdc->sc_state = DEVIDLE;
796459f2585Sur return 1;
797459f2585Sur }
798459f2585Sur
799459f2585Sur /* Is there a transfer to this drive? If not, deactivate drive. */
80070de9736Syamt bp = bufq_peek(fd->sc_q);
801459f2585Sur if (bp == NULL) {
802459f2585Sur fd->sc_ops = 0;
803459f2585Sur TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
804459f2585Sur fd->sc_active = 0;
805459f2585Sur goto loop;
806459f2585Sur }
807459f2585Sur
808459f2585Sur switch (fdc->sc_state) {
809459f2585Sur case DEVIDLE:
810459f2585Sur fdc->sc_errors = 0;
811459f2585Sur fd->sc_skip = 0;
812459f2585Sur fd->sc_bcount = bp->b_bcount;
813459f2585Sur fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
814459f2585Sur callout_stop(&fd->sc_motoroff_ch);
815459f2585Sur if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
816459f2585Sur fdc->sc_state = MOTORWAIT;
817459f2585Sur return 1;
818459f2585Sur }
819459f2585Sur if ((fd->sc_flags & FD_MOTOR) == 0) {
820459f2585Sur /* Turn on the motor, being careful about pairing. */
821459f2585Sur struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
822459f2585Sur if (ofd && ofd->sc_flags & FD_MOTOR) {
823459f2585Sur callout_stop(&ofd->sc_motoroff_ch);
824459f2585Sur ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
825459f2585Sur }
826459f2585Sur fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
827459f2585Sur fd_set_motor(fdc, 0);
828459f2585Sur fdc->sc_state = MOTORWAIT;
82994bba098Ssoda /* Allow .25s for motor to stabilize. */
83094bba098Ssoda callout_reset(&fd->sc_motoron_ch, hz / 4,
831459f2585Sur fd_motor_on, fd);
832459f2585Sur return 1;
833459f2585Sur }
834459f2585Sur /* Make sure the right drive is selected. */
835459f2585Sur fd_set_motor(fdc, 0);
836459f2585Sur
837459f2585Sur /* fall through */
838459f2585Sur case DOSEEK:
839459f2585Sur doseek:
840459f2585Sur if (fd->sc_cylin == bp->b_cylinder)
841459f2585Sur goto doio;
842459f2585Sur
84394bba098Ssoda out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
84494bba098Ssoda out_fdc(iot, ioh, fd->sc_type->steprate);
84594bba098Ssoda out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */
846459f2585Sur
84794bba098Ssoda out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */
84894bba098Ssoda out_fdc(iot, ioh, fd->sc_drive); /* drive number */
84994bba098Ssoda out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
850459f2585Sur
851459f2585Sur fd->sc_cylin = -1;
852459f2585Sur fdc->sc_state = SEEKWAIT;
85394bba098Ssoda
8543c0adb7dSblymn iostat_seek(fd->sc_dk.dk_stats);
85594bba098Ssoda disk_busy(&fd->sc_dk);
85694bba098Ssoda
857459f2585Sur callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
858459f2585Sur return 1;
859459f2585Sur
860459f2585Sur case DOIO:
861459f2585Sur doio:
862459f2585Sur type = fd->sc_type;
863459f2585Sur sec = fd->sc_blkno % type->seccyl;
864459f2585Sur nblks = type->seccyl - sec;
865d1579b2dSriastradh nblks = uimin(nblks, fd->sc_bcount / FDC_BSIZE);
866d1579b2dSriastradh nblks = uimin(nblks, fdc->sc_maxiosize / FDC_BSIZE);
867459f2585Sur fd->sc_nblks = nblks;
868459f2585Sur fd->sc_nbytes = nblks * FDC_BSIZE;
869459f2585Sur head = sec / type->sectrac;
870459f2585Sur sec -= head * type->sectrac;
871459f2585Sur #ifdef DIAGNOSTIC
87294bba098Ssoda {
87394bba098Ssoda int block;
87494bba098Ssoda block = (fd->sc_cylin * type->heads + head) *
87594bba098Ssoda type->sectrac + sec;
876459f2585Sur if (block != fd->sc_blkno) {
8770f31d9deStsutsui printf("%s: block %d != blkno %" PRId64
8780f31d9deStsutsui "\n", __func__, block, fd->sc_blkno);
879459f2585Sur #ifdef DDB
880459f2585Sur Debugger();
881459f2585Sur #endif
88294bba098Ssoda }
88394bba098Ssoda }
884459f2585Sur #endif
88594bba098Ssoda read = (bp->b_flags & B_READ) != 0;
8860f31d9deStsutsui FDCDMA_START(fdc, (uint8_t *)bp->b_data + fd->sc_skip,
88794bba098Ssoda fd->sc_nbytes, read);
88894bba098Ssoda bus_space_write_1(iot, ioh, FDCTL, type->rate);
889459f2585Sur #ifdef FD_DEBUG
8900f31d9deStsutsui printf("%s: %s drive %d track %d head %d sec %d nblks %d\n",
8910f31d9deStsutsui __func__, read ? "read" : "write", fd->sc_drive,
8920f31d9deStsutsui fd->sc_cylin, head, sec, nblks);
893459f2585Sur #endif
894459f2585Sur if (read)
89594bba098Ssoda out_fdc(iot, ioh, NE7CMD_READ); /* READ */
896459f2585Sur else
89794bba098Ssoda out_fdc(iot, ioh, NE7CMD_WRITE);/* WRITE */
89894bba098Ssoda out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
89994bba098Ssoda out_fdc(iot, ioh, fd->sc_cylin); /* track */
90094bba098Ssoda out_fdc(iot, ioh, head);
90194bba098Ssoda out_fdc(iot, ioh, sec + 1); /* sector + 1 */
90294bba098Ssoda out_fdc(iot, ioh, type->secsize); /* sector size */
90394bba098Ssoda out_fdc(iot, ioh, type->sectrac); /* sectors/track */
90494bba098Ssoda out_fdc(iot, ioh, type->gap1); /* gap1 size */
90594bba098Ssoda out_fdc(iot, ioh, type->datalen); /* data length */
906459f2585Sur fdc->sc_state = IOCOMPLETE;
90794bba098Ssoda
90894bba098Ssoda disk_busy(&fd->sc_dk);
90994bba098Ssoda
910459f2585Sur /* allow 2 seconds for operation */
911459f2585Sur callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
912459f2585Sur return 1; /* will return later */
913459f2585Sur
914459f2585Sur case SEEKWAIT:
915459f2585Sur callout_stop(&fdc->sc_timo_ch);
916459f2585Sur fdc->sc_state = SEEKCOMPLETE;
917459f2585Sur /* allow 1/50 second for heads to settle */
91894bba098Ssoda callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc);
919459f2585Sur return 1;
920459f2585Sur
921459f2585Sur case SEEKCOMPLETE:
922603098b9Smrg disk_unbusy(&fd->sc_dk, 0, 0);
92394bba098Ssoda
924459f2585Sur /* Make sure seek really happened. */
92594bba098Ssoda out_fdc(iot, ioh, NE7CMD_SENSEI);
926459f2585Sur if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
927459f2585Sur cyl != bp->b_cylinder * fd->sc_type->step) {
928459f2585Sur #ifdef FD_DEBUG
9290f31d9deStsutsui fdcstatus(fd->sc_dev, 2, "seek failed");
930459f2585Sur #endif
931459f2585Sur fdcretry(fdc);
932459f2585Sur goto loop;
933459f2585Sur }
934459f2585Sur fd->sc_cylin = bp->b_cylinder;
935459f2585Sur goto doio;
936459f2585Sur
937459f2585Sur case IOTIMEDOUT:
93894bba098Ssoda FDCDMA_ABORT(fdc);
939459f2585Sur
940459f2585Sur case SEEKTIMEDOUT:
941459f2585Sur case RECALTIMEDOUT:
942459f2585Sur case RESETTIMEDOUT:
943459f2585Sur fdcretry(fdc);
944459f2585Sur goto loop;
945459f2585Sur
946459f2585Sur case IOCOMPLETE: /* IO DONE, post-analyze */
947459f2585Sur callout_stop(&fdc->sc_timo_ch);
94894bba098Ssoda
949603098b9Smrg disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
950603098b9Smrg (bp->b_flags & B_READ));
95194bba098Ssoda
95294bba098Ssoda i = fdcresult(fdc);
95394bba098Ssoda if (i != 7 || (st0 & 0xf8) != 0) {
95494bba098Ssoda FDCDMA_ABORT(fdc);
955459f2585Sur #ifdef FD_DEBUG
9560f31d9deStsutsui fdcstatus(fd->sc_dev, 7, bp->b_flags & B_READ ?
957459f2585Sur "read failed" : "write failed");
958eb506d3cStsutsui printf("blkno %" PRId64 " nblks %d\n",
959459f2585Sur fd->sc_blkno, fd->sc_nblks);
960459f2585Sur #endif
961459f2585Sur fdcretry(fdc);
962459f2585Sur goto loop;
963459f2585Sur }
96494bba098Ssoda FDCDMA_DONE(fdc);
965459f2585Sur if (fdc->sc_errors) {
96694bba098Ssoda diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
9670f31d9deStsutsui fd->sc_skip / FDC_BSIZE, NULL);
968459f2585Sur printf("\n");
969459f2585Sur fdc->sc_errors = 0;
970459f2585Sur }
971459f2585Sur fd->sc_blkno += fd->sc_nblks;
972459f2585Sur fd->sc_skip += fd->sc_nbytes;
973459f2585Sur fd->sc_bcount -= fd->sc_nbytes;
974459f2585Sur if (fd->sc_bcount > 0) {
975459f2585Sur bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
976459f2585Sur goto doseek;
977459f2585Sur }
978459f2585Sur fdfinish(fd, bp);
979459f2585Sur goto loop;
980459f2585Sur
981459f2585Sur case DORESET:
982459f2585Sur /* try a reset, keep motor on */
983459f2585Sur fd_set_motor(fdc, 1);
984459f2585Sur delay(100);
985459f2585Sur fd_set_motor(fdc, 0);
986459f2585Sur fdc->sc_state = RESETCOMPLETE;
987459f2585Sur callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
988459f2585Sur return 1; /* will return later */
989459f2585Sur
990459f2585Sur case RESETCOMPLETE:
991459f2585Sur callout_stop(&fdc->sc_timo_ch);
992459f2585Sur /* clear the controller output buffer */
993459f2585Sur for (i = 0; i < 4; i++) {
99494bba098Ssoda out_fdc(iot, ioh, NE7CMD_SENSEI);
995459f2585Sur (void)fdcresult(fdc);
996459f2585Sur }
997459f2585Sur
998459f2585Sur /* fall through */
999459f2585Sur case DORECAL:
100094bba098Ssoda out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
100194bba098Ssoda out_fdc(iot, ioh, fd->sc_drive);
1002459f2585Sur fdc->sc_state = RECALWAIT;
1003459f2585Sur callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1004459f2585Sur return 1; /* will return later */
1005459f2585Sur
1006459f2585Sur case RECALWAIT:
1007459f2585Sur callout_stop(&fdc->sc_timo_ch);
1008459f2585Sur fdc->sc_state = RECALCOMPLETE;
1009459f2585Sur /* allow 1/30 second for heads to settle */
101094bba098Ssoda callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
1011459f2585Sur return 1; /* will return later */
1012459f2585Sur
1013459f2585Sur case RECALCOMPLETE:
101494bba098Ssoda out_fdc(iot, ioh, NE7CMD_SENSEI);
1015459f2585Sur if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1016459f2585Sur #ifdef FD_DEBUG
10170f31d9deStsutsui fdcstatus(fd->sc_dev, 2, "recalibrate failed");
1018459f2585Sur #endif
1019459f2585Sur fdcretry(fdc);
1020459f2585Sur goto loop;
1021459f2585Sur }
1022459f2585Sur fd->sc_cylin = 0;
1023459f2585Sur goto doseek;
1024459f2585Sur
1025459f2585Sur case MOTORWAIT:
1026459f2585Sur if (fd->sc_flags & FD_MOTOR_WAIT)
1027459f2585Sur return 1; /* time's not up yet */
1028459f2585Sur goto doseek;
1029459f2585Sur
1030459f2585Sur default:
10310f31d9deStsutsui fdcstatus(fd->sc_dev, 0, "stray interrupt");
1032459f2585Sur return 1;
1033459f2585Sur }
1034459f2585Sur #ifdef DIAGNOSTIC
10350f31d9deStsutsui panic("%s: impossible", __func__);
1036459f2585Sur #endif
1037459f2585Sur #undef st0
1038459f2585Sur #undef cyl
1039459f2585Sur }
1040459f2585Sur
1041459f2585Sur void
fdcretry(struct fdc_softc * fdc)10427fe2a5a0Stsutsui fdcretry(struct fdc_softc *fdc)
1043459f2585Sur {
1044459f2585Sur struct fd_softc *fd;
1045459f2585Sur struct buf *bp;
1046459f2585Sur
1047ec0d5b63Stsutsui fd = TAILQ_FIRST(&fdc->sc_drives);
104870de9736Syamt bp = bufq_peek(fd->sc_q);
1049459f2585Sur
1050459f2585Sur switch (fdc->sc_errors) {
1051459f2585Sur case 0:
1052459f2585Sur /* try again */
105394bba098Ssoda fdc->sc_state = DOSEEK;
1054459f2585Sur break;
1055459f2585Sur
10560f31d9deStsutsui case 1:
10570f31d9deStsutsui case 2:
10580f31d9deStsutsui case 3:
1059459f2585Sur /* didn't work; try recalibrating */
1060459f2585Sur fdc->sc_state = DORECAL;
1061459f2585Sur break;
1062459f2585Sur
1063459f2585Sur case 4:
1064459f2585Sur /* still no go; reset the bastard */
1065459f2585Sur fdc->sc_state = DORESET;
1066459f2585Sur break;
1067459f2585Sur
1068459f2585Sur default:
1069459f2585Sur diskerr(bp, "fd", "hard error", LOG_PRINTF,
1070459f2585Sur fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1071459f2585Sur
10729a5d3f28Schristos fdcpstatus(7, fdc);
1073459f2585Sur bp->b_error = EIO;
1074459f2585Sur fdfinish(fd, bp);
1075459f2585Sur }
1076459f2585Sur fdc->sc_errors++;
1077459f2585Sur }
1078459f2585Sur
1079459f2585Sur int
fdioctl(dev_t dev,u_long cmd,void * addr,int flag,struct lwp * l)108053524e44Schristos fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1081459f2585Sur {
10825b30cb96Stsutsui struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(dev));
1083459f2585Sur struct disklabel buffer;
1084459f2585Sur int error;
1085459f2585Sur
1086459f2585Sur switch (cmd) {
1087459f2585Sur case DIOCGDINFO:
108894bba098Ssoda memset(&buffer, 0, sizeof(buffer));
1089459f2585Sur
1090459f2585Sur buffer.d_secpercyl = fd->sc_type->seccyl;
1091c182898bSchristos buffer.d_type = DKTYPE_FLOPPY;
1092459f2585Sur buffer.d_secsize = FDC_BSIZE;
1093459f2585Sur
1094459f2585Sur if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
1095459f2585Sur return EINVAL;
1096459f2585Sur
1097459f2585Sur *(struct disklabel *)addr = buffer;
1098459f2585Sur return 0;
1099459f2585Sur
1100459f2585Sur case DIOCWLABEL:
1101459f2585Sur if ((flag & FWRITE) == 0)
1102459f2585Sur return EBADF;
1103459f2585Sur /* XXX do something */
1104459f2585Sur return 0;
1105459f2585Sur
1106459f2585Sur case DIOCWDINFO:
1107459f2585Sur if ((flag & FWRITE) == 0)
1108459f2585Sur return EBADF;
1109459f2585Sur
111094bba098Ssoda error = setdisklabel(&buffer, (struct disklabel *)addr,
111194bba098Ssoda 0, NULL);
1112459f2585Sur if (error)
1113459f2585Sur return error;
1114459f2585Sur
1115459f2585Sur error = writedisklabel(dev, fdstrategy, &buffer, NULL);
1116459f2585Sur return error;
1117459f2585Sur
1118459f2585Sur default:
1119459f2585Sur return ENOTTY;
1120459f2585Sur }
1121459f2585Sur
1122459f2585Sur #ifdef DIAGNOSTIC
11230f31d9deStsutsui panic("%s: impossible", __func__);
1124459f2585Sur #endif
1125459f2585Sur }
112694bba098Ssoda
112794bba098Ssoda /*
112894bba098Ssoda * Mountroot hook: prompt the user to enter the root file system floppy.
112994bba098Ssoda */
113094bba098Ssoda void
fd_mountroot_hook(device_t dev)11310f31d9deStsutsui fd_mountroot_hook(device_t dev)
113294bba098Ssoda {
113394bba098Ssoda int c;
113494bba098Ssoda
113594bba098Ssoda printf("Insert filesystem floppy and press return.");
113694bba098Ssoda cnpollc(1);
113794bba098Ssoda for (;;) {
113894bba098Ssoda c = cngetc();
113994bba098Ssoda if ((c == '\r') || (c == '\n')) {
114094bba098Ssoda printf("\n");
114194bba098Ssoda break;
114294bba098Ssoda }
114394bba098Ssoda }
114494bba098Ssoda cnpollc(0);
114594bba098Ssoda }
1146