fd.c revision 1.5 1 /* $NetBSD: fd.c,v 1.5 1995/04/30 12:06:01 leo Exp $ */
2
3 /*
4 * Copyright (c) 1995 Leo Weppelman.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Leo Weppelman.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * This file contains a driver for the Floppy Disk Controller (FDC)
35 * on the Atari TT. It uses the WD 1772 chip, modified for steprates.
36 *
37 * The ST floppy disk controller shares the access to the DMA circuitry
38 * with other devices. For this reason the floppy disk controller makes
39 * use of some special DMA accessing code.
40 *
41 * Interrupts from the FDC are in fact DMA interrupts which get their
42 * first level handling in 'dma.c' . If the floppy driver is currently
43 * using DMA the interrupt is signalled to 'fdcint'.
44 *
45 * TODO:
46 * - Test it with 2 drives (I don't have them)
47 * - Test it with an HD-drive (Don't have that either)
48 * - Finish ioctl's
49 */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/buf.h>
56 #include <sys/device.h>
57 #include <sys/ioctl.h>
58 #include <sys/fcntl.h>
59 #include <sys/conf.h>
60 #include <sys/disklabel.h>
61 #include <sys/disk.h>
62 #include <sys/dkbad.h>
63 #include <atari/atari/device.h>
64 #include <machine/disklabel.h>
65 #include <machine/iomap.h>
66 #include <machine/mfp.h>
67 #include <machine/dma.h>
68 #include <machine/video.h>
69 #include <atari/dev/fdreg.h>
70
71 /*
72 * Be verbose for debugging
73 */
74 /*#define FLP_DEBUG 1 */
75
76 #define FDC_MAX_DMA_AD 0x1000000 /* No DMA possible beyond */
77
78 /* Parameters for the disk drive. */
79 #define SECTOR_SIZE 512 /* physical sector size in bytes */
80 #define NR_DRIVES 2 /* maximum number of drives */
81 #define NR_TYPES 3 /* number of diskette/drive combinations*/
82 #define MAX_ERRORS 10 /* how often to try rd/wt before quitting*/
83 #define STEP_DELAY 6000 /* 6ms (6000us) delay after stepping */
84
85
86 #define INV_TRK 32000 /* Should fit in unsigned short */
87 #define INV_PART NR_TYPES
88
89 /*
90 * Driver states
91 */
92 #define FLP_IDLE 0x00 /* floppy is idle */
93 #define FLP_MON 0x01 /* idle with motor on */
94 #define FLP_STAT 0x02 /* determine floppy status */
95 #define FLP_XFER 0x04 /* read/write data from floppy */
96
97 /*
98 * Timer delay's
99 */
100 #define FLP_MONDELAY (3 * hz) /* motor-on delay */
101 #define FLP_XFERDELAY (2 * hz) /* timeout on transfer */
102
103
104 #define b_block b_resid /* FIXME: this is not the place */
105
106 /*
107 * Global data for all physical floppy devices
108 */
109 static short selected = 0; /* drive/head currently selected*/
110 static short motoron = 0; /* motor is spinning */
111 static short nopens = 0; /* Number of opens executed */
112
113 static short fd_state = FLP_IDLE; /* Current driver state */
114 static int lock_stat= 0; /* dma locking status */
115 static short fd_cmd = 0; /* command being executed */
116 static char *fd_error= NULL; /* error from fd_xfer_ok() */
117
118 /*
119 * Private per device data
120 */
121 struct fd_softc {
122 struct dkdevice dkdev;
123 struct buf bufq; /* queue of buf's */
124 int unit; /* unit for atari controlling hw*/
125 int nheads; /* number of heads in use */
126 int nsectors; /* number of sectors/track */
127 int nblocks; /* number of blocks on disk */
128 int curtrk; /* track head positioned on */
129 short flags; /* misc flags */
130 short part; /* Current open partition */
131 int sector; /* logical sector for I/O */
132 caddr_t io_data; /* KVA for data transfer */
133 int io_bytes; /* bytes left for I/O */
134 int io_dir; /* B_READ/B_WRITE */
135 int errcnt; /* current error count */
136 u_char *bounceb; /* Bounce buffer */
137
138 };
139
140 /*
141 * Flags in fd_softc:
142 */
143 #define FLPF_NOTRESP 0x001 /* Unit not responding */
144 #define FLPF_ISOPEN 0x002 /* Unit is open */
145 #define FLPF_ISHD 0x004 /* Use High Density */
146 #define FLPF_HAVELAB 0x008 /* We have a valid label */
147 #define FLPF_BOUNCE 0x010 /* Now using the bounce buffer */
148 #define FLPF_WRTPROT 0x020 /* Unit is write-protected */
149 #define FLPF_EMPTY 0x040 /* Unit is empty */
150 #define FLPF_INOPEN 0x080 /* Currently being opened */
151 #define FLPF_GETSTAT 0x100 /* Getting unit status */
152
153 struct fd_types {
154 int nheads; /* Heads in use */
155 int nsectors; /* sectors per track */
156 int nblocks; /* number of blocks */
157 } fdtypes[NR_TYPES] = {
158 { 1, 9, 720 }, /* 360 Kb */
159 { 2, 9, 1440 }, /* 720 Kb */
160 { 1, 18, 2880 }, /* 1.44 Mb */
161 };
162
163 typedef void (*FPV)();
164
165 /*
166 * Private drive functions....
167 */
168 static void fdstart __P((struct fd_softc *));
169 static void fddone __P((struct fd_softc *));
170 static void fdstatus __P((struct fd_softc *));
171 static void fd_xfer __P((struct fd_softc *));
172 static void fdcint __P((struct fd_softc *));
173 static int fd_xfer_ok __P((struct fd_softc *));
174 static void fdmotoroff __P((struct fd_softc *));
175 static int fdminphys __P((struct buf *));
176 static void fdtestdrv __P((struct fd_softc *));
177 static int fdgetdisklabel __P((struct fd_softc *, dev_t));
178
179 extern __inline__ u_char read_fdreg(u_short regno)
180 {
181 DMA->dma_mode = regno;
182 return(DMA->dma_data);
183 }
184
185 extern __inline__ void write_fdreg(u_short regno, u_short val)
186 {
187 DMA->dma_mode = regno;
188 DMA->dma_data = val;
189 }
190
191 extern __inline__ u_char read_dmastat(void)
192 {
193 DMA->dma_mode = FDC_CS | DMA_SCREG;
194 return(DMA->dma_stat);
195 }
196
197 /*
198 * Autoconfig stuff....
199 */
200 static int fdcmatch __P((struct device *, struct cfdata *, void *));
201 static int fdcprint __P((void *, char *));
202 static void fdcattach __P((struct device *, struct device *, void *));
203
204 struct cfdriver fdccd = {
205 NULL, "fdc", (cfmatch_t)fdcmatch, fdcattach, DV_DULL,
206 sizeof(struct device), NULL, 0 };
207
208 static int
209 fdcmatch(pdp, cfp, auxp)
210 struct device *pdp;
211 struct cfdata *cfp;
212 void *auxp;
213 {
214 if(strcmp("fdc", auxp) || cfp->cf_unit != 0)
215 return(0);
216 return(1);
217 }
218
219 static void
220 fdcattach(pdp, dp, auxp)
221 struct device *pdp, *dp;
222 void *auxp;
223 {
224 struct fd_softc fdsoftc;
225 int i, nfound = 0;
226
227 printf("\n");
228 for(i = 0; i < NR_DRIVES; i++) {
229
230 /*
231 * Test if unit is present
232 */
233 fdsoftc.unit = i;
234 fdsoftc.flags = 0;
235 st_dmagrab(fdcint, fdtestdrv, &fdsoftc, &lock_stat, 0);
236 st_dmafree(&fdsoftc, &lock_stat);
237
238 if(!(fdsoftc.flags & FLPF_NOTRESP)) {
239 nfound++;
240 config_found(dp, (void*)i, fdcprint);
241 }
242 }
243
244 if(nfound) {
245
246 /*
247 * enable disk related interrupts
248 */
249 MFP->mf_ierb |= IB_DINT;
250 MFP->mf_iprb &= ~IB_DINT;
251 MFP->mf_imrb |= IB_DINT;
252 }
253 }
254
255 static int
256 fdcprint(auxp, pnp)
257 void *auxp;
258 char *pnp;
259 {
260 return(UNCONF);
261 }
262
263 static int fdmatch __P((struct device *, struct cfdata *, void *));
264 static void fdattach __P((struct device *, struct device *, void *));
265 void fdstrategy __P((struct buf *));
266 struct dkdriver fddkdriver = { fdstrategy };
267
268 struct cfdriver fdcd = {
269 NULL, "fd", (cfmatch_t)fdmatch, fdattach, DV_DISK,
270 sizeof(struct fd_softc), NULL, 0 };
271
272 static int
273 fdmatch(pdp, cfp, auxp)
274 struct device *pdp;
275 struct cfdata *cfp;
276 void *auxp;
277 {
278 int unit = (int)auxp;
279 return(1);
280 }
281
282 static void
283 fdattach(pdp, dp, auxp)
284 struct device *pdp, *dp;
285 void *auxp;
286 {
287 struct fd_softc *sc;
288
289 sc = (struct fd_softc *)dp;
290
291 printf("\n");
292
293 sc->dkdev.dk_driver = &fddkdriver;
294 }
295
296 fdioctl(dev, cmd, addr, flag, p)
297 dev_t dev;
298 u_long cmd;
299 int flag;
300 caddr_t addr;
301 struct proc *p;
302 {
303 struct fd_softc *sc;
304 void *data;
305
306 sc = getsoftc(fdcd, DISKUNIT(dev));
307
308 if((sc->flags & FLPF_HAVELAB) == 0)
309 return(EBADF);
310
311 switch(cmd) {
312 case DIOCSBAD:
313 return(EINVAL);
314 case DIOCGDINFO:
315 *(struct disklabel *)addr = sc->dkdev.dk_label;
316 return(0);
317 case DIOCGPART:
318 ((struct partinfo *)addr)->disklab =
319 &sc->dkdev.dk_label;
320 ((struct partinfo *)addr)->part =
321 &sc->dkdev.dk_label.d_partitions[DISKPART(dev)];
322 return(0);
323 #ifdef notyet /* XXX LWP */
324 case DIOCSRETRIES:
325 case DIOCSSTEP:
326 case DIOCSDINFO:
327 case DIOCWDINFO:
328 case DIOCWLABEL:
329 #endif /* notyet */
330 default:
331 return(ENOTTY);
332 }
333 }
334
335 /*
336 * Open the device. If this is the first open on both the floppy devices,
337 * intialize the controller.
338 * Note that partition info on the floppy device is used to distinguise
339 * between 780Kb and 360Kb floppy's.
340 * partition 0: 360Kb
341 * partition 1: 780Kb
342 */
343 Fdopen(dev, flags, devtype, proc)
344 dev_t dev;
345 int flags, devtype;
346 struct proc *proc;
347 {
348 struct fd_softc *sc;
349 int sps;
350
351 #ifdef FLP_DEBUG
352 printf("Fdopen dev=0x%x\n", dev);
353 #endif
354
355 if(DISKPART(dev) >= NR_TYPES)
356 return(ENXIO);
357
358 if((sc = getsoftc(fdcd, DISKUNIT(dev))) == NULL)
359 return(ENXIO);
360
361 /*
362 * If no floppy currently open, reset the controller and select
363 * floppy type.
364 */
365 if(!nopens) {
366
367 #ifdef FLP_DEBUG
368 printf("Fdopen device not yet open\n");
369 #endif
370 nopens++;
371 write_fdreg(FDC_CS, IRUPT);
372 }
373
374 /*
375 * Sleep while other process is opening the device
376 */
377 sps = splbio();
378 while(sc->flags & FLPF_INOPEN)
379 tsleep((caddr_t)sc, PRIBIO, "Fdopen", 0);
380 splx(sps);
381
382 if(!(sc->flags & FLPF_ISOPEN)) {
383 /*
384 * Initialise some driver values.
385 */
386 int part = DISKPART(dev);
387 void *addr;
388
389 sc->bufq.b_actf = NULL;
390 sc->unit = DISKUNIT(dev);
391 sc->part = part;
392 sc->nheads = fdtypes[part].nheads;
393 sc->nsectors = fdtypes[part].nsectors;
394 sc->nblocks = fdtypes[part].nblocks;
395 sc->curtrk = INV_TRK;
396 sc->sector = 0;
397 sc->errcnt = 0;
398 sc->bounceb = (u_char*)alloc_stmem(SECTOR_SIZE, &addr);
399 if(sc->bounceb == NULL)
400 return(ENOMEM); /* XXX */
401 if(sc->nsectors > 9) /* XXX */
402 sc->flags |= FLPF_ISHD;
403
404 /*
405 * Go get write protect + loaded status
406 */
407 sc->flags = FLPF_INOPEN|FLPF_GETSTAT;
408 sps = splbio();
409 st_dmagrab(fdcint, fdstatus, sc, &lock_stat, 0);
410 while(sc->flags & FLPF_GETSTAT)
411 tsleep((caddr_t)sc, PRIBIO, "Fdopen", 0);
412 splx(sps);
413 wakeup((caddr_t)sc);
414
415 if((sc->flags & FLPF_WRTPROT) && (flags & FWRITE)) {
416 sc->flags = 0;
417 return(EPERM);
418 }
419 if(sc->flags & FLPF_EMPTY) {
420 sc->flags = 0;
421 return(ENXIO);
422 }
423 sc->flags = FLPF_ISOPEN;
424 }
425 else {
426 /*
427 * Multiply opens are granted when accessing the same type of
428 * floppy (eq. the same partition).
429 */
430 if(sc->part != DISKPART(dev))
431 return(ENXIO); /* XXX temporarely out of business */
432 }
433 fdgetdisklabel(sc, dev);
434 #ifdef FLP_DEBUG
435 printf("Fdopen open succeeded on type %d\n", sc->part);
436 #endif
437 }
438
439 fdclose(dev, flags, devtype, proc)
440 dev_t dev;
441 int flags, devtype;
442 struct proc *proc;
443 {
444 struct fd_softc *sc;
445
446 sc = getsoftc(fdcd, DISKUNIT(dev));
447 free_stmem(sc->bounceb);
448 sc->flags = 0;
449 nopens--;
450
451 #ifdef FLP_DEBUG
452 printf("Closed floppy device -- nopens: %d\n", nopens);
453 #endif
454 return(0);
455 }
456
457 void
458 fdstrategy(bp)
459 struct buf *bp;
460 {
461 struct fd_softc *sc;
462 int sps, nblocks;
463
464 sc = getsoftc(fdcd, DISKUNIT(bp->b_dev));
465
466 #ifdef FLP_DEBUG
467 printf("fdstrategy: 0x%x\n", bp);
468 #endif
469
470 /*
471 * check for valid partition and bounds
472 */
473 nblocks = (bp->b_bcount + SECTOR_SIZE - 1) / SECTOR_SIZE;
474 if((bp->b_blkno < 0) || ((bp->b_blkno + nblocks) >= sc->nblocks)) {
475 if((bp->b_blkno == sc->nblocks) && (bp->b_flags & B_READ)) {
476 /*
477 * Read 1 block beyond, return EOF
478 */
479 bp->b_resid = bp->b_bcount;
480 goto done;
481 }
482 /*
483 * Try to limit the size of the transaction, adjust count
484 * if we succeed.
485 */
486 nblocks = sc->nblocks - bp->b_blkno;
487 if((nblocks <= 0) || (bp->b_blkno < 0)) {
488 bp->b_error = EINVAL;
489 bp->b_flags |= B_ERROR;
490 goto done;
491 }
492 bp->b_bcount = nblocks * SECTOR_SIZE;
493 }
494 if(bp->b_bcount == 0)
495 goto done;
496
497 /*
498 * Set order info for disksort
499 */
500 bp->b_block = bp->b_blkno / (sc->nsectors * sc->nheads);
501
502 /*
503 * queue the buf and kick the low level code
504 */
505 sps = splbio();
506 disksort(&sc->bufq, bp);
507 if(!lock_stat) {
508 if(fd_state & FLP_MON)
509 untimeout((FPV)fdmotoroff, (void*)sc);
510 fd_state = FLP_IDLE;
511 st_dmagrab(fdcint, fdstart, sc, &lock_stat, 0);
512 }
513 splx(sps);
514
515 return;
516 done:
517 bp->b_resid = bp->b_bcount;
518 biodone(bp);
519 }
520
521 /*
522 * no dumps to floppy disks thank you.
523 */
524 int
525 fddump(dev_t dev)
526 {
527 return(ENXIO);
528 }
529
530 /*
531 * no dumps to floppy disks thank you.
532 */
533 int
534 fdsize(dev)
535 dev_t dev;
536 {
537 return(-1);
538 }
539
540 int
541 fdread(dev, uio)
542 dev_t dev;
543 struct uio *uio;
544 {
545 return(physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
546 dev, B_READ, fdminphys, uio));
547 }
548
549 int
550 fdwrite(dev, uio)
551 dev_t dev;
552 struct uio *uio;
553 {
554 return(physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
555 dev, B_WRITE, fdminphys, uio));
556 }
557
558 /*
559 * Called through DMA-dispatcher, get status.
560 */
561 static void
562 fdstatus(sc)
563 struct fd_softc *sc;
564 {
565 #ifdef FLP_DEBUG
566 printf("fdstatus\n");
567 #endif
568 sc->errcnt = 0;
569 fd_state = FLP_STAT;
570
571 /*
572 * Make sure the floppy controller is the correct density mode
573 */
574 if(sc->flags & FLPF_ISHD)
575 DMA->dma_drvmode |= (FDC_HDSET|FDC_HDSIG);
576 else DMA->dma_drvmode &= ~(FDC_HDSET|FDC_HDSIG);
577 fd_xfer(sc);
578 }
579
580 /*
581 * Called through the dma-dispatcher. So we know we are the only ones
582 * messing with the floppy-controler.
583 * Initialize some fields in the fdsoftc for the state-machine and get
584 * it going.
585 */
586 static void
587 fdstart(sc)
588 struct fd_softc *sc;
589 {
590 struct buf *bp;
591
592 bp = sc->bufq.b_actf;
593 sc->sector = bp->b_blkno; /* Start sector for I/O */
594 sc->io_data = bp->b_data; /* KVA base for I/O */
595 sc->io_bytes = bp->b_bcount; /* Transfer size in bytes */
596 sc->io_dir = bp->b_flags & B_READ;/* Direction of transfer */
597 sc->errcnt = 0; /* No errors yet */
598 fd_state = FLP_XFER; /* Yes, we're going to transfer */
599
600 /*
601 * Make sure the floppy controller is the correct density mode
602 */
603 if(sc->flags & FLPF_ISHD)
604 DMA->dma_drvmode |= (FDC_HDSET|FDC_HDSIG);
605 else DMA->dma_drvmode &= ~(FDC_HDSET|FDC_HDSIG);
606 fd_xfer(sc);
607 }
608
609 /*
610 * The current transaction is finished (for good or bad). Let go of
611 * the the dma-resources. Call biodone() to finish the transaction.
612 * Find a new transaction to work on.
613 */
614 static void
615 fddone(sc)
616 register struct fd_softc *sc;
617 {
618 struct buf *bp, *dp;
619 struct fd_softc *sc1;
620 int i, sps;
621
622 /*
623 * Lower clock frequency of FDC (better for some old ones).
624 */
625 DMA->dma_drvmode &= ~(FDC_HDSET|FDC_HDSIG);
626
627 /*
628 * Give others a chance to use the dma.
629 */
630 st_dmafree(sc, &lock_stat);
631
632
633 if(fd_state != FLP_STAT) {
634 /*
635 * Finish current transaction.
636 */
637 sps = splbio();
638 dp = &sc->bufq;
639 bp = dp->b_actf;
640 if(bp == NULL)
641 panic("fddone");
642 dp->b_actf = bp->b_actf;
643 splx(sps);
644
645 #ifdef FLP_DEBUG
646 printf("fddone: unit: %d, buf: %x, resid: %d\n",sc->unit,bp,
647 sc->io_bytes);
648 #endif
649 bp->b_resid = sc->io_bytes;
650 biodone(bp);
651 }
652 fd_state = FLP_MON;
653
654 if(lock_stat)
655 return; /* XXX Is this possible? */
656
657 /*
658 * Find a new transaction on round-robin basis.
659 */
660 for(i = sc->unit + 1; ;i++) {
661 if(i >= fdcd.cd_ndevs)
662 i = 0;
663 if((sc1 = fdcd.cd_devs[i]) == NULL)
664 continue;
665 if(sc1->bufq.b_actf)
666 break;
667 if(i == sc->unit) {
668 timeout((FPV)fdmotoroff, (void*)sc, FLP_MONDELAY);
669 #ifdef FLP_DEBUG
670 printf("fddone: Nothing to do\n");
671 #endif
672 return; /* No work */
673 }
674 }
675 fd_state = FLP_IDLE;
676 #ifdef FLP_DEBUG
677 printf("fddone: Staring job on unit %d\n", sc1->unit);
678 #endif
679 st_dmagrab(fdcint, fdstart, sc1, &lock_stat, 0);
680 }
681
682 /****************************************************************************
683 * The following functions assume to be running as a result of a *
684 * disk-interrupt (e.q. spl = splbio). *
685 * They form the finit-state machine, the actual driver. *
686 * *
687 * fdstart()/ --> fd_xfer() -> activate hardware *
688 * fdopen() ^ *
689 * | *
690 * +-- not ready -<------------+ *
691 * | *
692 * fdmotoroff()/ --> fdcint() -> fd_xfer_ok() ---+ *
693 * h/w interrupt | *
694 * \|/ *
695 * finished ---> fdone() *
696 * *
697 ****************************************************************************/
698 static void
699 fd_xfer(sc)
700 struct fd_softc *sc;
701 {
702 register int head = 0;
703 register int track, sector, hbit;
704 int i;
705 u_long phys_addr;
706
707 switch(fd_state) {
708 case FLP_XFER:
709 /*
710 * Calculate head/track values
711 */
712 track = sc->sector / sc->nsectors;
713 head = track % sc->nheads;
714 track = track / sc->nheads;
715 #ifdef FLP_DEBUG
716 printf("fd_xfer: sector:%d,head:%d,track:%d\n", sc->sector,head,
717 track);
718 #endif
719 break;
720
721 case FLP_STAT:
722 /*
723 * FLP_STAT only wants to recalibrate
724 */
725 sc->curtrk = INV_TRK;
726 break;
727 default:
728 panic("fd_xfer: wrong state (0x%x)", fd_state);
729 }
730
731 /*
732 * Determine if the controller should check spin-up.
733 */
734 hbit = motoron ? HBIT : 0;
735 motoron = 1;
736
737 /*
738 * Select the right unit and head.
739 */
740 i = (sc->unit ? PA_FLOP1 : PA_FLOP0) | head;
741 if(i != selected) {
742 selected = i;
743 SOUND->sd_selr = YM_IOA;
744 SOUND->sd_wdat = (SOUND->sd_rdat & 0xF8) | (i ^ 0x07);
745 }
746
747 if(sc->curtrk == INV_TRK) {
748 /*
749 * Recalibrate, since we lost track of head positioning.
750 * The floppy disk controller has no way of determining its
751 * absolute arm position (track). Instead, it steps the
752 * arm a track at a time and keeps track of where it
753 * thinks it is (in software). However, after a SEEK, the
754 * hardware reads information from the diskette telling
755 * where the arm actually is. If the arm is in the wrong place,
756 * a recalibration is done, which forces the arm to track 0.
757 * This way the controller can get back into sync with reality.
758 */
759 write_fdreg(FDC_CS, RESTORE|VBIT|hbit);
760 fd_cmd = RESTORE;
761 timeout((FPV)fdmotoroff, (void*)sc, FLP_XFERDELAY);
762
763 #ifdef FLP_DEBUG
764 printf("fd_xfer:Recalibrating drive %d\n", sc->unit);
765 #endif
766 return;
767 }
768
769 write_fdreg(FDC_TR, sc->curtrk);
770
771 /*
772 * Issue a SEEK command on the indicated drive unless the arm is
773 * already positioned on the correct track.
774 */
775 if(track != sc->curtrk) {
776 sc->curtrk = track; /* be optimistic */
777 write_fdreg(FDC_DR, track);
778 write_fdreg(FDC_CS, SEEK|RATE6|VBIT|hbit);
779 timeout((FPV)fdmotoroff, (void*)sc, FLP_XFERDELAY);
780 fd_cmd = SEEK;
781 #ifdef FLP_DEBUG
782 printf("fd_xfer:Seek to track %d on drive %d\n",track,sc->unit);
783 #endif
784 return;
785 }
786
787 /*
788 * The drive is now on the proper track. Read or write 1 block.
789 */
790 sector = sc->sector % sc->nsectors;
791 sector++; /* start numbering at 1 */
792
793 write_fdreg(FDC_SR, sector);
794
795 phys_addr = (u_long)kvtop(sc->io_data);
796 if(phys_addr >= FDC_MAX_DMA_AD) {
797 /*
798 * We _must_ bounce this address
799 */
800 phys_addr = (u_long)kvtop(sc->bounceb);
801 if(sc->io_dir == B_WRITE)
802 bcopy(sc->io_data, sc->bounceb, SECTOR_SIZE);
803 sc->flags |= FLPF_BOUNCE;
804 }
805 st_dmaaddr((caddr_t)phys_addr); /* DMA address setup */
806
807 #ifdef FLP_DEBUG
808 printf("fd_xfer:Start io (io_addr:%x)\n", kvtop(sc->io_data));
809 #endif
810
811 if(sc->io_dir == B_READ) {
812 /* Issue the command */
813 st_dmacomm(DMA_FDC | DMA_SCREG, 1);
814 write_fdreg(FDC_CS, F_READ|hbit);
815 fd_cmd = F_READ;
816 }
817 else {
818 /* Issue the command */
819 st_dmacomm(DMA_WRBIT | DMA_FDC | DMA_SCREG, 1);
820 write_fdreg(DMA_WRBIT | FDC_CS, F_WRITE|hbit|EBIT|PBIT);
821 fd_cmd = F_WRITE;
822 }
823 timeout((FPV)fdmotoroff, (void*)sc, FLP_XFERDELAY);
824 }
825
826 /* return values of fd_xfer_ok(): */
827 #define X_OK 0
828 #define X_AGAIN 1
829 #define X_ERROR 2
830 #define X_FAIL 3
831
832 /*
833 * Hardware interrupt function.
834 */
835 static void
836 fdcint(sc)
837 struct fd_softc *sc;
838 {
839 struct buf *bp;
840
841 #ifdef FLP_DEBUG
842 printf("fdcint: unit = %d\n", sc->unit);
843 #endif
844
845 /*
846 * Cancel timeout (we made it, didn't we)
847 */
848 untimeout((FPV)fdmotoroff, (void*)sc);
849
850 switch(fd_xfer_ok(sc)) {
851 case X_ERROR :
852 if(++(sc->errcnt) < MAX_ERRORS) {
853 /*
854 * Command failed but still retries left.
855 */
856 break;
857 }
858 /* FALL THROUGH */
859 case X_FAIL :
860 /*
861 * Non recoverable error. Fall back to motor-on
862 * idle-state.
863 */
864 if(fd_state == FLP_STAT) {
865 sc->flags |= FLPF_EMPTY;
866 sc->flags &= ~FLPF_GETSTAT;
867 wakeup((caddr_t)sc);
868 fddone(sc);
869 return;
870 }
871
872 bp = sc->bufq.b_actf;
873
874 bp->b_error = EIO;
875 bp->b_flags |= B_ERROR;
876 fd_state = FLP_MON;
877 if(fd_error != NULL) {
878 printf("Floppy error: %s\n", fd_error);
879 fd_error = NULL;
880 }
881
882 break;
883 case X_AGAIN:
884 /*
885 * Start next part of state machine.
886 */
887 break;
888 case X_OK:
889 /*
890 * Command ok and finished. Reset error-counter.
891 * If there are no more bytes to transfer fall back
892 * to motor-on idle state.
893 */
894 sc->errcnt = 0;
895
896 if(fd_state == FLP_STAT) {
897 sc->flags &= ~FLPF_GETSTAT;
898 wakeup((caddr_t)sc);
899 fddone(sc);
900 return;
901 }
902
903 if((sc->flags & FLPF_BOUNCE) && (sc->io_dir == B_READ))
904 bcopy(sc->bounceb, sc->io_data, SECTOR_SIZE);
905 sc->flags &= ~FLPF_BOUNCE;
906
907 sc->sector++;
908 sc->io_data += SECTOR_SIZE;
909 sc->io_bytes -= SECTOR_SIZE;
910 if(sc->io_bytes <= 0)
911 fd_state = FLP_MON;
912 }
913 if(fd_state == FLP_MON)
914 fddone(sc);
915 else fd_xfer(sc);
916 }
917
918 /*
919 * Determine status of last command. Should only be called through
920 * 'fdcint()'.
921 * Returns:
922 * X_ERROR : Error on command; might succeed next time.
923 * X_FAIL : Error on command; will never succeed.
924 * X_AGAIN : Part of a command succeeded, call 'fd_xfer()' to complete.
925 * X_OK : Command succeeded and is complete.
926 *
927 * This function only affects sc->curtrk.
928 */
929 static int
930 fd_xfer_ok(sc)
931 register struct fd_softc *sc;
932 {
933 register int status;
934
935 #ifdef FLP_DEBUG
936 printf("fd_xfer_ok: cmd: 0x%x, state: 0x%x\n", fd_cmd, fd_state);
937 #endif
938 switch(fd_cmd) {
939 case IRUPT:
940 /*
941 * Timeout. Force a recalibrate before we try again.
942 */
943 fd_error = "Timeout";
944 sc->curtrk = INV_TRK;
945 return(X_ERROR);
946 case F_READ:
947 /*
948 * Test for DMA error
949 */
950 status = read_dmastat();
951 if(!(status & DMAOK)) {
952 fd_error = "Dma error";
953 return(X_ERROR);
954 }
955 /*
956 * Get controller status and check for errors.
957 */
958 status = read_fdreg(FDC_CS);
959 if(status & (RNF | CRCERR | LD_T00)) {
960 fd_error = "Read error";
961 if(status & RNF)
962 sc->curtrk = INV_TRK;
963 return(X_ERROR);
964 }
965 break;
966 case F_WRITE:
967 /*
968 * Test for DMA error
969 */
970 status = read_dmastat();
971 if(!(status & DMAOK)) {
972 fd_error = "Dma error";
973 return(X_ERROR);
974 }
975 /*
976 * Get controller status and check for errors.
977 */
978 status = read_fdreg(FDC_CS);
979 if(status & WRI_PRO) {
980 fd_error = "Write protected";
981 return(X_FAIL);
982 }
983 if(status & (RNF | CRCERR | LD_T00)) {
984 fd_error = "Write error";
985 sc->curtrk = INV_TRK;
986 return(X_ERROR);
987 }
988 break;
989 case SEEK:
990 status = read_fdreg(FDC_CS);
991 if(status & (RNF | CRCERR)) {
992 fd_error = "Seek error";
993 sc->curtrk = INV_TRK;
994 return(X_ERROR);
995 }
996 return(X_AGAIN);
997 case RESTORE:
998 /*
999 * Determine if the recalibration succeeded.
1000 */
1001 status = read_fdreg(FDC_CS);
1002 if(status & RNF) {
1003 fd_error = "Recalibrate error";
1004 /* reset controller */
1005 write_fdreg(FDC_CS, IRUPT);
1006 sc->curtrk = INV_TRK;
1007 return(X_ERROR);
1008 }
1009 sc->curtrk = 0;
1010 if(fd_state == FLP_STAT) {
1011 if(status & WRI_PRO)
1012 sc->flags |= FLPF_WRTPROT;
1013 break;
1014 }
1015 return(X_AGAIN);
1016 default:
1017 fd_error = "Driver error: fd_xfer_ok : Unknown state";
1018 return(X_FAIL);
1019 }
1020 return(X_OK);
1021 }
1022
1023 /*
1024 * All timeouts will call this function.
1025 */
1026 static void
1027 fdmotoroff(sc)
1028 struct fd_softc *sc;
1029 {
1030 int sps, wrbit;
1031
1032 /*
1033 * Get at harware interrupt level
1034 */
1035 sps = splbio();
1036
1037 #if FLP_DEBUG
1038 printf("fdmotoroff, state = 0x%x\n", fd_state);
1039 #endif
1040
1041 switch(fd_state) {
1042 case FLP_STAT :
1043 case FLP_XFER :
1044 /*
1045 * Timeout during a transfer; cancel transaction
1046 * set command to 'IRUPT'.
1047 * A drive-interrupt is simulated to trigger the state
1048 * machine.
1049 */
1050 /*
1051 * Cancel current transaction
1052 */
1053 wrbit = (fd_cmd == F_WRITE) ? DMA_WRBIT : 0;
1054 fd_cmd = IRUPT;
1055 write_fdreg(FDC_CS, wrbit|IRUPT);
1056
1057 /*
1058 * Simulate floppy interrupt.
1059 */
1060 fdcint(sc);
1061 return;
1062 case FLP_MON :
1063 /*
1064 * Turn motor off.
1065 */
1066 if(selected) {
1067 SOUND->sd_selr = YM_IOA;
1068 SOUND->sd_wdat = SOUND->sd_rdat | 0x07;
1069 motoron = selected = 0;
1070 }
1071 fd_state = FLP_IDLE;
1072 break;
1073 }
1074 splx(sps);
1075 }
1076
1077 /*
1078 * min byte count to whats left of the track in question
1079 */
1080 static int
1081 fdminphys(bp)
1082 struct buf *bp;
1083 {
1084 struct fd_softc *sc;
1085 int sec, toff, tsz;
1086
1087 if((sc = getsoftc(fdcd, DISKUNIT(bp->b_dev))) == NULL)
1088 return(ENXIO);
1089
1090 sec = bp->b_blkno % (sc->nsectors * sc->nheads);
1091 toff = sec * SECTOR_SIZE;
1092 tsz = sc->nsectors * sc->nheads * SECTOR_SIZE;
1093
1094 #ifdef FLP_DEBUG
1095 printf("fdminphys: before %d", bp->b_bcount);
1096 #endif
1097
1098 bp->b_bcount = min(bp->b_bcount, tsz - toff);
1099
1100 #ifdef FLP_DEBUG
1101 printf(" after %d\n", bp->b_bcount);
1102 #endif
1103
1104 return(bp->b_bcount);
1105 }
1106
1107 /*
1108 * Used to find out wich drives are actually connected. We do this by issueing
1109 * is 'RESTORE' command and check if the 'track-0' bit is set. This also works
1110 * if the drive is present but no floppy is inserted.
1111 */
1112 static void
1113 fdtestdrv(fdsoftc)
1114 struct fd_softc *fdsoftc;
1115 {
1116 int i, status;
1117
1118 /*
1119 * Select the right unit and head.
1120 */
1121 i = fdsoftc->unit ? PA_FLOP1 : PA_FLOP0;
1122 if(i != selected) {
1123 selected = i;
1124 SOUND->sd_selr = YM_IOA;
1125 SOUND->sd_wdat = (SOUND->sd_rdat & 0xF8) | (i ^ 0x07);
1126 }
1127
1128 write_fdreg(FDC_CS, RESTORE|VBIT|HBIT);
1129
1130 /*
1131 * Wait for about 2 seconds.
1132 */
1133 delay(2000000);
1134
1135 status = read_fdreg(FDC_CS);
1136 if(status & (RNF|BUSY))
1137 write_fdreg(FDC_CS, IRUPT); /* reset controller */
1138
1139 if(!(status & LD_T00))
1140 fdsoftc->flags |= FLPF_NOTRESP;
1141 }
1142
1143 /*
1144 * Build disk label. For now we only create a label from what we know
1145 * from 'sc'.
1146 */
1147 static int
1148 fdgetdisklabel(sc, dev)
1149 struct fd_softc *sc;
1150 dev_t dev;
1151 {
1152 struct disklabel *lp, *dlp;
1153 int part;
1154
1155 /*
1156 * If we already got one, get out.
1157 */
1158 if(sc->flags & FLPF_HAVELAB)
1159 return(0);
1160
1161 #ifdef FLP_DEBUG
1162 printf("fdgetdisklabel()\n");
1163 #endif
1164
1165 part = DISKPART(dev);
1166 lp = &sc->dkdev.dk_label;
1167 bzero(lp, sizeof(struct disklabel));
1168
1169 lp->d_secsize = SECTOR_SIZE;
1170 lp->d_ntracks = sc->nheads;
1171 lp->d_nsectors = sc->nsectors;
1172 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1173 lp->d_ncylinders = sc->nblocks / lp->d_secpercyl;
1174 lp->d_secperunit = sc->nblocks;
1175
1176 lp->d_type = DTYPE_FLOPPY;
1177 lp->d_rpm = 300; /* good guess I suppose. */
1178 lp->d_interleave = 1; /* FIXME: is this OK? */
1179 lp->d_bbsize = 0;
1180 lp->d_sbsize = 0;
1181 lp->d_npartitions = part + 1;
1182 lp->d_trkseek = STEP_DELAY;
1183 lp->d_magic = DISKMAGIC;
1184 lp->d_magic2 = DISKMAGIC;
1185 lp->d_checksum = dkcksum(lp);
1186 lp->d_partitions[part].p_size = lp->d_secperunit;
1187 lp->d_partitions[part].p_fstype = FS_UNUSED;
1188 lp->d_partitions[part].p_fsize = 1024;
1189 lp->d_partitions[part].p_frag = 8;
1190 sc->flags |= FLPF_HAVELAB;
1191
1192 return(0);
1193 }
1194