1 /* $NetBSD: fd.c,v 1.71 2009/07/31 15:55:10 tsutsui Exp $ */
4 * Copyright (c) 1995 Leo Weppelman.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * This file contains a driver for the Floppy Disk Controller (FDC)
30 * on the Atari TT. It uses the WD 1772 chip, modified for steprates.
32 * The ST floppy disk controller shares the access to the DMA circuitry
33 * with other devices. For this reason the floppy disk controller makes
34 * use of some special DMA accessing code.
36 * Interrupts from the FDC are in fact DMA interrupts which get their
37 * first level handling in 'dma.c' . If the floppy driver is currently
38 * using DMA the interrupt is signalled to 'fdcint'.
41 * - Test it with 2 drives (I don't have them)
42 * - Test it with an HD-drive (Don't have that either)
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.71 2009/07/31 15:55:10 tsutsui Exp $");
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/callout.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
57 #include <sys/device.h>
58 #include <sys/ioctl.h>
59 #include <sys/fcntl.h>
61 #include <sys/disklabel.h>
63 #include <sys/dkbad.h>
64 #include <atari/atari/device.h>
65 #include <atari/atari/stalloc.h>
66 #include <machine/disklabel.h>
67 #include <machine/iomap.h>
68 #include <machine/mfp.h>
69 #include <machine/dma.h>
70 #include <machine/video.h>
71 #include <machine/cpu.h>
72 #include <atari/dev/ym2149reg.h>
73 #include <atari/dev/fdreg.h>
76 * Be verbose for debugging
78 /*#define FLP_DEBUG 1 */
80 #define FDC_MAX_DMA_AD 0x1000000 /* No DMA possible beyond */
82 /* Parameters for the disk drive. */
83 #define SECTOR_SIZE 512 /* physical sector size in bytes */
84 #define NR_DRIVES 2 /* maximum number of drives */
85 #define NR_TYPES 3 /* number of diskette/drive combinations*/
86 #define MAX_ERRORS 10 /* how often to try rd/wt before quitting*/
87 #define STEP_DELAY 6000 /* 6ms (6000us) delay after stepping */
90 #define INV_TRK 32000 /* Should fit in unsigned short */
91 #define INV_PART NR_TYPES
96 #define FLP_IDLE 0x00 /* floppy is idle */
97 #define FLP_MON 0x01 /* idle with motor on */
98 #define FLP_STAT 0x02 /* determine floppy status */
99 #define FLP_XFER 0x04 /* read/write data from floppy */
104 #define FLP_MONDELAY (3 * hz) /* motor-on delay */
105 #define FLP_XFERDELAY (2 * hz) /* timeout on transfer */
110 #define FLP_DD 0 /* Double density */
111 #define FLP_HD 1 /* High density */
114 #define b_block b_resid /* FIXME: this is not the place */
117 * Global data for all physical floppy devices
119 static short selected
= 0; /* drive/head currently selected*/
120 static short motoron
= 0; /* motor is spinning */
121 static short nopens
= 0; /* Number of opens executed */
123 static short fd_state
= FLP_IDLE
; /* Current driver state */
124 static int lock_stat
= 0; /* DMA locking status */
125 static short fd_cmd
= 0; /* command being executed */
126 static const char *fd_error
= NULL
; /* error from fd_xfer_ok() */
129 * Private per device data
132 struct device sc_dv
; /* generic device info */
133 struct disk dkdev
; /* generic disk info */
134 struct bufq_state
*bufq
; /* queue of buf's */
135 struct callout sc_motor_ch
;
136 int unit
; /* unit for atari controlling hw*/
137 int nheads
; /* number of heads in use */
138 int nsectors
; /* number of sectors/track */
139 int density
; /* density code */
140 int nblocks
; /* number of blocks on disk */
141 int curtrk
; /* track head positioned on */
142 short flags
; /* misc flags */
143 short part
; /* Current open partition */
144 int sector
; /* logical sector for I/O */
145 char *io_data
; /* KVA for data transfer */
146 int io_bytes
; /* bytes left for I/O */
147 int io_dir
; /* B_READ/B_WRITE */
148 int errcnt
; /* current error count */
149 u_char
*bounceb
; /* Bounce buffer */
156 #define FLPF_NOTRESP 0x001 /* Unit not responding */
157 #define FLPF_ISOPEN 0x002 /* Unit is open */
158 #define FLPF_SPARE 0x004 /* Not used */
159 #define FLPF_HAVELAB 0x008 /* We have a valid label */
160 #define FLPF_BOUNCE 0x010 /* Now using the bounce buffer */
161 #define FLPF_WRTPROT 0x020 /* Unit is write-protected */
162 #define FLPF_EMPTY 0x040 /* Unit is empty */
163 #define FLPF_INOPEN 0x080 /* Currently being opened */
164 #define FLPF_GETSTAT 0x100 /* Getting unit status */
167 int nheads
; /* Heads in use */
168 int nsectors
; /* sectors per track */
169 int nblocks
; /* number of blocks */
170 int density
; /* density code */
171 const char *descr
; /* type description */
172 } fdtypes
[NR_TYPES
] = {
173 { 1, 9, 720 , FLP_DD
, "360KB" }, /* 360 Kb */
174 { 2, 9, 1440 , FLP_DD
, "720KB" }, /* 720 Kb */
175 { 2, 18, 2880 , FLP_HD
, "1.44MB" }, /* 1.44 Mb */
178 #define FLP_TYPE_360 0 /* XXX: Please keep these in */
179 #define FLP_TYPE_720 1 /* sync with the numbering in */
180 #define FLP_TYPE_144 2 /* 'fdtypes' right above! */
183 * This is set only once at attach time. The value is determined by reading
184 * the configuration switches and is one of the FLP_TYPE_*'s.
185 * This is simular to the way Atari handles the _FLP cookie.
187 static short def_type
= 0; /* Reflects config-switches */
189 #define FLP_DEFTYPE 1 /* 720Kb, reasonable default */
190 #define FLP_TYPE(dev) ( DISKPART(dev) == 0 ? def_type : DISKPART(dev) - 1 )
192 typedef void (*FPV
)(void *);
194 dev_type_open(fdopen
);
195 dev_type_close(fdclose
);
196 dev_type_read(fdread
);
197 dev_type_write(fdwrite
);
198 dev_type_ioctl(fdioctl
);
199 dev_type_strategy(fdstrategy
);
202 * Private drive functions....
204 static void fdstart(struct fd_softc
*);
205 static void fddone(struct fd_softc
*);
206 static void fdstatus(struct fd_softc
*);
207 static void fd_xfer(struct fd_softc
*);
208 static void fdcint(struct fd_softc
*);
209 static int fd_xfer_ok(struct fd_softc
*);
210 static void fdmotoroff(struct fd_softc
*);
211 static void fdminphys(struct buf
*);
212 static void fdtestdrv(struct fd_softc
*);
213 static void fdgetdefaultlabel(struct fd_softc
*, struct disklabel
*,
215 static int fdgetdisklabel(struct fd_softc
*, dev_t
);
216 static int fdselect(int, int, int);
217 static void fddeselect(void);
218 static void fdmoff(struct fd_softc
*);
219 u_char
read_fdreg(u_short
);
220 void write_fdreg(u_short
, u_short
);
221 u_char
read_dmastat(void);
223 extern inline u_char
read_fdreg(u_short regno
)
225 DMA
->dma_mode
= regno
;
226 return(DMA
->dma_data
);
229 extern inline void write_fdreg(u_short regno
, u_short val
)
231 DMA
->dma_mode
= regno
;
235 extern inline u_char
read_dmastat(void)
237 DMA
->dma_mode
= FDC_CS
| DMA_SCREG
;
238 return(DMA
->dma_stat
);
242 * Config switch stuff. Used only for the floppy type for now. That's
244 * XXX: If needed in more places, it should be moved to it's own include file.
245 * Note: This location _must_ be read as an u_short. Failure to do so
246 * will return garbage!
248 static u_short
rd_cfg_switch(void);
249 static u_short
rd_cfg_switch(void)
251 return(*((volatile u_short
*)AD_CFG_SWITCH
));
255 * Switch definitions.
256 * Note: ON reads as a zero bit!
258 #define CFG_SWITCH_NOHD 0x4000
261 * Autoconfig stuff....
263 extern struct cfdriver fd_cd
;
265 static int fdcmatch(struct device
*, struct cfdata
*, void *);
266 static int fdcprint(void *, const char *);
267 static void fdcattach(struct device
*, struct device
*, void *);
269 CFATTACH_DECL(fdc
, sizeof(struct device
),
270 fdcmatch
, fdcattach
, NULL
, NULL
);
272 const struct bdevsw fd_bdevsw
= {
273 fdopen
, fdclose
, fdstrategy
, fdioctl
, nodump
, nosize
, D_DISK
276 const struct cdevsw fd_cdevsw
= {
277 fdopen
, fdclose
, fdread
, fdwrite
, fdioctl
,
278 nostop
, notty
, nopoll
, nommap
, nokqfilter
, D_DISK
282 fdcmatch(struct device
*pdp
, struct cfdata
*cfp
, void *auxp
)
284 static int fdc_matched
= 0;
286 /* Match only once */
287 if(strcmp("fdc", auxp
) || fdc_matched
)
294 fdcattach(struct device
*pdp
, struct device
*dp
, void *auxp
)
296 struct fd_softc fdsoftc
;
297 int i
, nfound
, first_found
;
299 nfound
= first_found
= 0;
302 for(i
= 0; i
< NR_DRIVES
; i
++) {
305 * Test if unit is present
309 st_dmagrab((dma_farg
)fdcint
, (dma_farg
)fdtestdrv
, &fdsoftc
,
311 st_dmafree(&fdsoftc
, &lock_stat
);
313 if(!(fdsoftc
.flags
& FLPF_NOTRESP
)) {
317 config_found(dp
, (void*)i
, fdcprint
);
322 struct fd_softc
*fdsc
= getsoftc(fd_cd
, first_found
);
325 * Make sure motor will be turned of when a floppy is
326 * inserted in the first selected drive.
328 fdselect(first_found
, 0, FLP_DD
);
330 callout_reset(&fdsc
->sc_motor_ch
, 0, (FPV
)fdmotoroff
, fdsc
);
333 * enable disk related interrupts
335 MFP
->mf_ierb
|= IB_DINT
;
336 MFP
->mf_iprb
= (u_int8_t
)~IB_DINT
;
337 MFP
->mf_imrb
|= IB_DINT
;
342 fdcprint(void *auxp
, const char *pnp
)
345 aprint_normal("fd%d at %s:", (int)auxp
, pnp
);
350 static int fdmatch(struct device
*, struct cfdata
*, void *);
351 static void fdattach(struct device
*, struct device
*, void *);
353 struct dkdriver fddkdriver
= { fdstrategy
};
355 CFATTACH_DECL(fd
, sizeof(struct fd_softc
),
356 fdmatch
, fdattach
, NULL
, NULL
);
358 extern struct cfdriver fd_cd
;
361 fdmatch(struct device
*pdp
, struct cfdata
*cfp
, void *auxp
)
367 fdattach(struct device
*pdp
, struct device
*dp
, void *auxp
)
370 struct fd_types
*type
;
373 sc
= device_private(dp
);
375 callout_init(&sc
->sc_motor_ch
, 0);
378 * Find out if an Ajax chip might be installed. Set the default
379 * floppy type accordingly.
381 swtch
= rd_cfg_switch();
382 def_type
= (swtch
& CFG_SWITCH_NOHD
) ? FLP_TYPE_720
: FLP_TYPE_144
;
383 type
= &fdtypes
[def_type
];
385 printf(": %s %d cyl, %d head, %d sec\n", type
->descr
,
386 type
->nblocks
/ (type
->nsectors
* type
->nheads
), type
->nheads
,
390 * Initialize and attach the disk structure.
392 disk_init(&sc
->dkdev
, sc
->sc_dv
.dv_xname
, &fddkdriver
);
393 disk_attach(&sc
->dkdev
);
397 fdioctl(dev_t dev
, u_long cmd
, void * addr
, int flag
, struct lwp
*l
)
401 sc
= getsoftc(fd_cd
, DISKUNIT(dev
));
403 if((sc
->flags
& FLPF_HAVELAB
) == 0)
410 *(struct disklabel
*)addr
= *(sc
->dkdev
.dk_label
);
413 ((struct partinfo
*)addr
)->disklab
=
415 ((struct partinfo
*)addr
)->part
=
416 &sc
->dkdev
.dk_label
->d_partitions
[RAW_PART
];
418 #ifdef notyet /* XXX LWP */
427 fdgetdefaultlabel(sc
, (struct disklabel
*)addr
,
435 * Open the device. If this is the first open on both the floppy devices,
436 * intialize the controller.
437 * Note that partition info on the floppy device is used to distinguise
438 * between 780Kb and 360Kb floppy's.
443 fdopen(dev_t dev
, int flags
, int devtype
, struct lwp
*l
)
449 printf("fdopen dev=0x%x\n", dev
);
452 if(FLP_TYPE(dev
) >= NR_TYPES
)
455 if((sc
= getsoftc(fd_cd
, DISKUNIT(dev
))) == NULL
)
459 * If no floppy currently open, reset the controller and select
465 printf("fdopen device not yet open\n");
468 write_fdreg(FDC_CS
, IRUPT
);
473 * Sleep while other process is opening the device
476 while(sc
->flags
& FLPF_INOPEN
)
477 tsleep((void *)sc
, PRIBIO
, "fdopen", 0);
480 if(!(sc
->flags
& FLPF_ISOPEN
)) {
482 * Initialise some driver values.
487 type
= FLP_TYPE(dev
);
489 bufq_alloc(&sc
->bufq
, "disksort", BUFQ_SORT_RAWBLOCK
);
490 sc
->unit
= DISKUNIT(dev
);
492 sc
->nheads
= fdtypes
[type
].nheads
;
493 sc
->nsectors
= fdtypes
[type
].nsectors
;
494 sc
->nblocks
= fdtypes
[type
].nblocks
;
495 sc
->density
= fdtypes
[type
].density
;
496 sc
->curtrk
= INV_TRK
;
499 sc
->bounceb
= (u_char
*)alloc_stmem(SECTOR_SIZE
, &addr
);
500 if(sc
->bounceb
== NULL
)
501 return(ENOMEM
); /* XXX */
504 * Go get write protect + loaded status
506 sc
->flags
|= FLPF_INOPEN
|FLPF_GETSTAT
;
508 st_dmagrab((dma_farg
)fdcint
, (dma_farg
)fdstatus
, sc
,
510 while(sc
->flags
& FLPF_GETSTAT
)
511 tsleep((void *)sc
, PRIBIO
, "fdopen", 0);
515 if((sc
->flags
& FLPF_WRTPROT
) && (flags
& FWRITE
)) {
519 if(sc
->flags
& FLPF_EMPTY
) {
523 sc
->flags
&= ~(FLPF_INOPEN
|FLPF_GETSTAT
);
524 sc
->flags
|= FLPF_ISOPEN
;
528 * Multiply opens are granted when accessing the same type of
529 * floppy (eq. the same partition).
531 if(sc
->density
!= fdtypes
[DISKPART(dev
)].density
)
532 return(ENXIO
); /* XXX temporarely out of business */
534 fdgetdisklabel(sc
, dev
);
536 printf("fdopen open succeeded on type %d\n", sc
->part
);
542 fdclose(dev_t dev
, int flags
, int devtype
, struct lwp
*l
)
546 sc
= getsoftc(fd_cd
, DISKUNIT(dev
));
547 free_stmem(sc
->bounceb
);
552 printf("Closed floppy device -- nopens: %d\n", nopens
);
558 fdstrategy(struct buf
*bp
)
561 struct disklabel
*lp
;
564 sc
= getsoftc(fd_cd
, DISKUNIT(bp
->b_dev
));
567 printf("fdstrategy: %p, b_bcount: %ld\n", bp
, bp
->b_bcount
);
571 * check for valid partition and bounds
573 lp
= sc
->dkdev
.dk_label
;
574 if ((sc
->flags
& FLPF_HAVELAB
) == 0) {
578 if (bp
->b_blkno
< 0 || (bp
->b_bcount
% SECTOR_SIZE
)) {
579 bp
->b_error
= EINVAL
;
582 if (bp
->b_bcount
== 0)
585 sz
= howmany(bp
->b_bcount
, SECTOR_SIZE
);
587 if (bp
->b_blkno
+ sz
> sc
->nblocks
) {
588 sz
= sc
->nblocks
- bp
->b_blkno
;
589 if (sz
== 0) /* Exactly at EndOfDisk */
591 if (sz
< 0) { /* Past EndOfDisk */
592 bp
->b_error
= EINVAL
;
596 if (bp
->b_flags
& B_RAW
)
597 bp
->b_bcount
= sz
<< DEV_BSHIFT
;
598 else bp
->b_bcount
= sz
* lp
->d_secsize
;
601 /* No partition translation. */
602 bp
->b_rawblkno
= bp
->b_blkno
;
605 * queue the buf and kick the low level code
608 bufq_put(sc
->bufq
, bp
); /* XXX disksort_cylinder */
610 if (fd_state
& FLP_MON
)
611 callout_stop(&sc
->sc_motor_ch
);
613 st_dmagrab((dma_farg
)fdcint
, (dma_farg
)fdstart
, sc
,
620 bp
->b_resid
= bp
->b_bcount
;
625 fdread(dev_t dev
, struct uio
*uio
, int flags
)
627 return(physio(fdstrategy
, NULL
, dev
, B_READ
, fdminphys
, uio
));
631 fdwrite(dev_t dev
, struct uio
*uio
, int flags
)
633 return(physio(fdstrategy
, NULL
, dev
, B_WRITE
, fdminphys
, uio
));
637 * Called through DMA-dispatcher, get status.
640 fdstatus(struct fd_softc
*sc
)
643 printf("fdstatus\n");
651 * Called through the DMA-dispatcher. So we know we are the only ones
652 * messing with the floppy-controller.
653 * Initialize some fields in the fdsoftc for the state-machine and get
657 fdstart(struct fd_softc
*sc
)
661 bp
= bufq_peek(sc
->bufq
);
662 sc
->sector
= bp
->b_blkno
; /* Start sector for I/O */
663 sc
->io_data
= bp
->b_data
; /* KVA base for I/O */
664 sc
->io_bytes
= bp
->b_bcount
; /* Transfer size in bytes */
665 sc
->io_dir
= bp
->b_flags
& B_READ
;/* Direction of transfer */
666 sc
->errcnt
= 0; /* No errors yet */
667 fd_state
= FLP_XFER
; /* Yes, we're going to transfer */
669 /* Instrumentation. */
670 disk_busy(&sc
->dkdev
);
676 * The current transaction is finished (for good or bad). Let go of
677 * the DMA-resources. Call biodone() to finish the transaction.
678 * Find a new transaction to work on.
681 fddone(register struct fd_softc
*sc
)
684 struct fd_softc
*sc1
;
688 * Give others a chance to use the DMA.
690 st_dmafree(sc
, &lock_stat
);
693 if(fd_state
!= FLP_STAT
) {
695 * Finish current transaction.
698 bp
= bufq_get(sc
->bufq
);
704 printf("fddone: unit: %d, buf: %p, resid: %d\n",sc
->unit
,bp
,
707 bp
->b_resid
= sc
->io_bytes
;
709 disk_unbusy(&sc
->dkdev
, (bp
->b_bcount
- bp
->b_resid
),
710 (bp
->b_flags
& B_READ
));
717 return; /* XXX Is this possible? */
720 * Find a new transaction on round-robin basis.
722 for(i
= sc
->unit
+ 1; ;i
++) {
723 if(i
>= fd_cd
.cd_ndevs
)
725 if((sc1
= device_lookup_private(&fd_cd
, i
)) == NULL
)
727 if (bufq_peek(sc1
->bufq
) != NULL
)
730 callout_reset(&sc
->sc_motor_ch
, FLP_MONDELAY
,
731 (FPV
)fdmotoroff
, sc
);
733 printf("fddone: Nothing to do\n");
735 return; /* No work */
740 printf("fddone: Staring job on unit %d\n", sc1
->unit
);
742 st_dmagrab((dma_farg
)fdcint
, (dma_farg
)fdstart
, sc1
, &lock_stat
, 0);
746 fdselect(int drive
, int head
, int dense
)
750 printf("fdselect: drive=%d, head=%d, dense=%d\n", drive
, head
, dense
);
752 i
= ((drive
== 1) ? PA_FLOP1
: PA_FLOP0
) | head
;
758 DMA
->dma_drvmode
= 0;
761 DMA
->dma_drvmode
= (FDC_HDSET
|FDC_HDSIG
);
764 panic("fdselect: unknown density code");
768 ym2149_fd_select((i
^ PA_FDSEL
));
776 ym2149_fd_select(PA_FDSEL
);
777 motoron
= selected
= 0;
778 DMA
->dma_drvmode
= 0;
781 /****************************************************************************
782 * The following functions assume to be running as a result of a *
783 * disk-interrupt (e.q. spl = splbio). *
784 * They form the finit-state machine, the actual driver. *
786 * fdstart()/ --> fd_xfer() -> activate hardware *
789 * +-- not ready -<------------+ *
791 * fdmotoroff()/ --> fdcint() -> fd_xfer_ok() ---+ *
794 * finished ---> fdone() *
796 ****************************************************************************/
798 fd_xfer(struct fd_softc
*sc
)
801 register int track
, sector
, hbit
;
808 * Calculate head/track values
810 track
= sc
->sector
/ sc
->nsectors
;
811 head
= track
% sc
->nheads
;
812 track
= track
/ sc
->nheads
;
814 printf("fd_xfer: sector:%d,head:%d,track:%d\n", sc
->sector
,head
,
821 * FLP_STAT only wants to recalibrate
823 sc
->curtrk
= INV_TRK
;
826 panic("fd_xfer: wrong state (0x%x)", fd_state
);
832 hbit
= fdselect(sc
->unit
, head
, sc
->density
) ? HBIT
: 0;
834 if(sc
->curtrk
== INV_TRK
) {
836 * Recalibrate, since we lost track of head positioning.
837 * The floppy disk controller has no way of determining its
838 * absolute arm position (track). Instead, it steps the
839 * arm a track at a time and keeps track of where it
840 * thinks it is (in software). However, after a SEEK, the
841 * hardware reads information from the diskette telling
842 * where the arm actually is. If the arm is in the wrong place,
843 * a recalibration is done, which forces the arm to track 0.
844 * This way the controller can get back into sync with reality.
847 write_fdreg(FDC_CS
, RESTORE
|VBIT
|hbit
);
848 callout_reset(&sc
->sc_motor_ch
, FLP_XFERDELAY
,
849 (FPV
)fdmotoroff
, sc
);
852 printf("fd_xfer:Recalibrating drive %d\n", sc
->unit
);
857 write_fdreg(FDC_TR
, sc
->curtrk
);
860 * Issue a SEEK command on the indicated drive unless the arm is
861 * already positioned on the correct track.
863 if(track
!= sc
->curtrk
) {
864 sc
->curtrk
= track
; /* be optimistic */
865 write_fdreg(FDC_DR
, track
);
866 write_fdreg(FDC_CS
, SEEK
|RATE6
|VBIT
|hbit
);
867 callout_reset(&sc
->sc_motor_ch
, FLP_XFERDELAY
,
868 (FPV
)fdmotoroff
, sc
);
871 printf("fd_xfer:Seek to track %d on drive %d\n",track
,sc
->unit
);
877 * The drive is now on the proper track. Read or write 1 block.
879 sector
= sc
->sector
% sc
->nsectors
;
880 sector
++; /* start numbering at 1 */
882 write_fdreg(FDC_SR
, sector
);
884 phys_addr
= (u_long
)kvtop(sc
->io_data
);
885 if(phys_addr
>= FDC_MAX_DMA_AD
) {
887 * We _must_ bounce this address
889 phys_addr
= (u_long
)kvtop(sc
->bounceb
);
890 if(sc
->io_dir
== B_WRITE
)
891 memcpy(sc
->bounceb
, sc
->io_data
, SECTOR_SIZE
);
892 sc
->flags
|= FLPF_BOUNCE
;
894 st_dmaaddr_set((void *)phys_addr
); /* DMA address setup */
897 printf("fd_xfer:Start io (io_addr:%lx)\n", (u_long
)kvtop(sc
->io_data
));
900 if(sc
->io_dir
== B_READ
) {
901 /* Issue the command */
902 st_dmacomm(DMA_FDC
| DMA_SCREG
, 1);
903 write_fdreg(FDC_CS
, F_READ
|hbit
);
907 /* Issue the command */
908 st_dmacomm(DMA_WRBIT
| DMA_FDC
| DMA_SCREG
, 1);
909 write_fdreg(DMA_WRBIT
| FDC_CS
, F_WRITE
|hbit
|EBIT
|PBIT
);
912 callout_reset(&sc
->sc_motor_ch
, FLP_XFERDELAY
, (FPV
)fdmotoroff
, sc
);
915 /* return values of fd_xfer_ok(): */
922 * Hardware interrupt function.
925 fdcint(struct fd_softc
*sc
)
930 printf("fdcint: unit = %d\n", sc
->unit
);
934 * Cancel timeout (we made it, didn't we)
936 callout_stop(&sc
->sc_motor_ch
);
938 switch(fd_xfer_ok(sc
)) {
940 if(++(sc
->errcnt
) < MAX_ERRORS
) {
942 * Command failed but still retries left.
949 * Non recoverable error. Fall back to motor-on
952 if(fd_error
!= NULL
) {
953 printf("Floppy error: %s\n", fd_error
);
957 if(fd_state
== FLP_STAT
) {
958 sc
->flags
|= FLPF_EMPTY
;
959 sc
->flags
&= ~FLPF_GETSTAT
;
965 bp
= bufq_peek(sc
->bufq
);
973 * Start next part of state machine.
978 * Command ok and finished. Reset error-counter.
979 * If there are no more bytes to transfer fall back
980 * to motor-on idle state.
984 if(fd_state
== FLP_STAT
) {
985 sc
->flags
&= ~FLPF_GETSTAT
;
991 if((sc
->flags
& FLPF_BOUNCE
) && (sc
->io_dir
== B_READ
))
992 memcpy(sc
->io_data
, sc
->bounceb
, SECTOR_SIZE
);
993 sc
->flags
&= ~FLPF_BOUNCE
;
996 sc
->io_data
+= SECTOR_SIZE
;
997 sc
->io_bytes
-= SECTOR_SIZE
;
998 if(sc
->io_bytes
<= 0)
1001 if(fd_state
== FLP_MON
)
1007 * Determine status of last command. Should only be called through
1010 * X_ERROR : Error on command; might succeed next time.
1011 * X_FAIL : Error on command; will never succeed.
1012 * X_AGAIN : Part of a command succeeded, call 'fd_xfer()' to complete.
1013 * X_OK : Command succeeded and is complete.
1015 * This function only affects sc->curtrk.
1018 fd_xfer_ok(register struct fd_softc
*sc
)
1020 register int status
;
1023 printf("fd_xfer_ok: cmd: 0x%x, state: 0x%x\n", fd_cmd
, fd_state
);
1028 * Timeout. Force a recalibrate before we try again.
1030 status
= read_fdreg(FDC_CS
);
1032 fd_error
= "Timeout";
1033 sc
->curtrk
= INV_TRK
;
1037 * Test for DMA error
1039 status
= read_dmastat();
1040 if(!(status
& DMAOK
)) {
1041 fd_error
= "DMA error";
1045 * Get controller status and check for errors.
1047 status
= read_fdreg(FDC_CS
);
1048 if(status
& (RNF
| CRCERR
| LD_T00
)) {
1049 fd_error
= "Read error";
1051 sc
->curtrk
= INV_TRK
;
1057 * Test for DMA error
1059 status
= read_dmastat();
1060 if(!(status
& DMAOK
)) {
1061 fd_error
= "DMA error";
1065 * Get controller status and check for errors.
1067 status
= read_fdreg(FDC_CS
);
1068 if(status
& WRI_PRO
) {
1069 fd_error
= "Write protected";
1072 if(status
& (RNF
| CRCERR
| LD_T00
)) {
1073 fd_error
= "Write error";
1074 sc
->curtrk
= INV_TRK
;
1079 status
= read_fdreg(FDC_CS
);
1080 if(status
& (RNF
| CRCERR
)) {
1081 fd_error
= "Seek error";
1082 sc
->curtrk
= INV_TRK
;
1088 * Determine if the recalibration succeeded.
1090 status
= read_fdreg(FDC_CS
);
1092 fd_error
= "Recalibrate error";
1093 /* reset controller */
1094 write_fdreg(FDC_CS
, IRUPT
);
1095 sc
->curtrk
= INV_TRK
;
1099 if(fd_state
== FLP_STAT
) {
1100 if(status
& WRI_PRO
)
1101 sc
->flags
|= FLPF_WRTPROT
;
1106 fd_error
= "Driver error: fd_xfer_ok : Unknown state";
1113 * All timeouts will call this function.
1116 fdmotoroff(struct fd_softc
*sc
)
1121 * Get at harware interrupt level
1126 printf("fdmotoroff, state = 0x%x\n", fd_state
);
1133 * Timeout during a transfer; cancel transaction
1134 * set command to 'IRUPT'.
1135 * A drive-interrupt is simulated to trigger the state
1139 * Cancel current transaction
1142 write_fdreg(FDC_CS
, IRUPT
);
1144 (void)read_fdreg(FDC_CS
);
1145 write_fdreg(FDC_CS
, RESTORE
);
1155 st_dmagrab((dma_farg
)fdcint
, (dma_farg
)fdmoff
,
1158 else fd_state
= FLP_IDLE
;
1165 * min byte count to whats left of the track in question
1168 fdminphys(struct buf
*bp
)
1170 struct fd_softc
*sc
;
1173 if((sc
= getsoftc(fd_cd
, DISKUNIT(bp
->b_dev
))) == NULL
)
1174 panic("fdminphys: couldn't get softc");
1176 sec
= bp
->b_blkno
% (sc
->nsectors
* sc
->nheads
);
1177 toff
= sec
* SECTOR_SIZE
;
1178 tsz
= sc
->nsectors
* sc
->nheads
* SECTOR_SIZE
;
1181 printf("fdminphys: before %ld", bp
->b_bcount
);
1184 bp
->b_bcount
= min(bp
->b_bcount
, tsz
- toff
);
1187 printf(" after %ld\n", bp
->b_bcount
);
1194 * Called from fdmotoroff to turn the motor actually off....
1195 * This can't be done in fdmotoroff itself, because exclusive access to the
1196 * DMA controller is needed to read the FDC-status register. The function
1197 * 'fdmoff()' always runs as the result of a 'dmagrab()'.
1198 * We need to test the status-register because we want to be sure that the
1199 * drive motor is really off before deselecting the drive. The FDC only
1200 * turns off the drive motor after having seen 10 index-pulses. You only
1201 * get index-pulses when a drive is selected....This means that if the
1202 * drive is deselected when the motor is still spinning, it will continue
1203 * to spin _even_ when you insert a floppy later on...
1206 fdmoff(struct fd_softc
*fdsoftc
)
1210 if ((fd_state
== FLP_MON
) && selected
) {
1211 tmp
= read_fdreg(FDC_CS
);
1212 if (!(tmp
& MOTORON
)) {
1214 fd_state
= FLP_IDLE
;
1217 callout_reset(&fdsoftc
->sc_motor_ch
, 10*FLP_MONDELAY
,
1218 (FPV
)fdmotoroff
, fdsoftc
);
1220 st_dmafree(fdsoftc
, &tmp
);
1224 * Used to find out wich drives are actually connected. We do this by issuing
1225 * is 'RESTORE' command and check if the 'track-0' bit is set. This also works
1226 * if the drive is present but no floppy is inserted.
1229 fdtestdrv(struct fd_softc
*fdsoftc
)
1234 * Select the right unit and head.
1236 fdselect(fdsoftc
->unit
, 0, FLP_DD
);
1238 write_fdreg(FDC_CS
, RESTORE
|HBIT
);
1241 * Wait for about 2 seconds.
1245 status
= read_fdreg(FDC_CS
);
1246 if(status
& (RNF
|BUSY
)) {
1247 write_fdreg(FDC_CS
, IRUPT
); /* reset controller */
1251 if(!(status
& LD_T00
))
1252 fdsoftc
->flags
|= FLPF_NOTRESP
;
1258 fdgetdefaultlabel(struct fd_softc
*sc
, struct disklabel
*lp
, int part
)
1261 memset(lp
, 0, sizeof(struct disklabel
));
1263 lp
->d_secsize
= SECTOR_SIZE
;
1264 lp
->d_ntracks
= sc
->nheads
;
1265 lp
->d_nsectors
= sc
->nsectors
;
1266 lp
->d_secpercyl
= lp
->d_ntracks
* lp
->d_nsectors
;
1267 lp
->d_ncylinders
= sc
->nblocks
/ lp
->d_secpercyl
;
1268 lp
->d_secperunit
= sc
->nblocks
;
1270 lp
->d_type
= DTYPE_FLOPPY
;
1271 lp
->d_rpm
= 300; /* good guess I suppose. */
1272 lp
->d_interleave
= 1; /* FIXME: is this OK? */
1275 lp
->d_npartitions
= part
+ 1;
1276 lp
->d_trkseek
= STEP_DELAY
;
1277 lp
->d_magic
= DISKMAGIC
;
1278 lp
->d_magic2
= DISKMAGIC
;
1279 lp
->d_checksum
= dkcksum(lp
);
1280 lp
->d_partitions
[part
].p_size
= lp
->d_secperunit
;
1281 lp
->d_partitions
[part
].p_fstype
= FS_UNUSED
;
1282 lp
->d_partitions
[part
].p_fsize
= 1024;
1283 lp
->d_partitions
[part
].p_frag
= 8;
1287 * Build disk label. For now we only create a label from what we know
1291 fdgetdisklabel(struct fd_softc
*sc
, dev_t dev
)
1293 struct disklabel
*lp
;
1297 * If we already got one, get out.
1299 if(sc
->flags
& FLPF_HAVELAB
)
1303 printf("fdgetdisklabel()\n");
1307 lp
= sc
->dkdev
.dk_label
;
1308 fdgetdefaultlabel(sc
, lp
, part
);
1309 sc
->flags
|= FLPF_HAVELAB
;