fd.c revision 1.14 1 /* $NetBSD: fd.c,v 1.14 1996/02/02 18:05:52 mycroft 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 * The density codes
105 */
106 #define FLP_DD 0 /* Double density */
107 #define FLP_HD 1 /* High density */
108
109
110 #define b_block b_resid /* FIXME: this is not the place */
111
112 /*
113 * Global data for all physical floppy devices
114 */
115 static short selected = 0; /* drive/head currently selected*/
116 static short motoron = 0; /* motor is spinning */
117 static short nopens = 0; /* Number of opens executed */
118
119 static short fd_state = FLP_IDLE; /* Current driver state */
120 static int lock_stat= 0; /* dma locking status */
121 static short fd_cmd = 0; /* command being executed */
122 static char *fd_error= NULL; /* error from fd_xfer_ok() */
123
124 /*
125 * Private per device data
126 */
127 struct fd_softc {
128 struct device sc_dv; /* generic device info */
129 struct disk dkdev; /* generic disk info */
130 struct buf bufq; /* queue of buf's */
131 int unit; /* unit for atari controlling hw*/
132 int nheads; /* number of heads in use */
133 int nsectors; /* number of sectors/track */
134 int density; /* density code */
135 int nblocks; /* number of blocks on disk */
136 int curtrk; /* track head positioned on */
137 short flags; /* misc flags */
138 short part; /* Current open partition */
139 int sector; /* logical sector for I/O */
140 caddr_t io_data; /* KVA for data transfer */
141 int io_bytes; /* bytes left for I/O */
142 int io_dir; /* B_READ/B_WRITE */
143 int errcnt; /* current error count */
144 u_char *bounceb; /* Bounce buffer */
145
146 };
147
148 /*
149 * Flags in fd_softc:
150 */
151 #define FLPF_NOTRESP 0x001 /* Unit not responding */
152 #define FLPF_ISOPEN 0x002 /* Unit is open */
153 #define FLPF_SPARE 0x004 /* Not used */
154 #define FLPF_HAVELAB 0x008 /* We have a valid label */
155 #define FLPF_BOUNCE 0x010 /* Now using the bounce buffer */
156 #define FLPF_WRTPROT 0x020 /* Unit is write-protected */
157 #define FLPF_EMPTY 0x040 /* Unit is empty */
158 #define FLPF_INOPEN 0x080 /* Currently being opened */
159 #define FLPF_GETSTAT 0x100 /* Getting unit status */
160
161 struct fd_types {
162 int nheads; /* Heads in use */
163 int nsectors; /* sectors per track */
164 int nblocks; /* number of blocks */
165 int density; /* density code */
166 } fdtypes[NR_TYPES] = {
167 { 1, 9, 720 , FLP_DD }, /* 360 Kb */
168 { 2, 9, 1440 , FLP_DD }, /* 720 Kb */
169 { 2, 18, 2880 , FLP_HD }, /* 1.44 Mb */
170 };
171
172 typedef void (*FPV)();
173
174 /*
175 * Private drive functions....
176 */
177 static void fdstart __P((struct fd_softc *));
178 static void fddone __P((struct fd_softc *));
179 static void fdstatus __P((struct fd_softc *));
180 static void fd_xfer __P((struct fd_softc *));
181 static void fdcint __P((struct fd_softc *));
182 static int fd_xfer_ok __P((struct fd_softc *));
183 static void fdmotoroff __P((struct fd_softc *));
184 static void fdminphys __P((struct buf *));
185 static void fdtestdrv __P((struct fd_softc *));
186 static int fdgetdisklabel __P((struct fd_softc *, dev_t));
187 static int fdselect __P((int, int, int));
188 static void fddeselect __P((void));
189 static void fdmoff __P((struct fd_softc *));
190
191 extern __inline__ u_char read_fdreg(u_short regno)
192 {
193 DMA->dma_mode = regno;
194 return(DMA->dma_data);
195 }
196
197 extern __inline__ void write_fdreg(u_short regno, u_short val)
198 {
199 DMA->dma_mode = regno;
200 DMA->dma_data = val;
201 }
202
203 extern __inline__ u_char read_dmastat(void)
204 {
205 DMA->dma_mode = FDC_CS | DMA_SCREG;
206 return(DMA->dma_stat);
207 }
208
209 /*
210 * Autoconfig stuff....
211 */
212 static int fdcmatch __P((struct device *, struct cfdata *, void *));
213 static int fdcprint __P((void *, char *));
214 static void fdcattach __P((struct device *, struct device *, void *));
215
216 struct cfdriver fdccd = {
217 NULL, "fdc", (cfmatch_t)fdcmatch, fdcattach, DV_DULL,
218 sizeof(struct device), NULL, 0 };
219
220 static int
221 fdcmatch(pdp, cfp, auxp)
222 struct device *pdp;
223 struct cfdata *cfp;
224 void *auxp;
225 {
226 if(strcmp("fdc", auxp) || cfp->cf_unit != 0)
227 return(0);
228 return(1);
229 }
230
231 static void
232 fdcattach(pdp, dp, auxp)
233 struct device *pdp, *dp;
234 void *auxp;
235 {
236 extern struct cfdriver fdcd;
237
238 struct fd_softc fdsoftc;
239 int i, nfound, first_found = 0;
240
241 printf("\n");
242 fddeselect();
243 for(i = 0; i < NR_DRIVES; i++) {
244
245 /*
246 * Test if unit is present
247 */
248 fdsoftc.unit = i;
249 fdsoftc.flags = 0;
250 st_dmagrab(fdcint, fdtestdrv, &fdsoftc, &lock_stat, 0);
251 st_dmafree(&fdsoftc, &lock_stat);
252
253 if(!(fdsoftc.flags & FLPF_NOTRESP)) {
254 if(!nfound)
255 first_found = i;
256 nfound++;
257 config_found(dp, (void*)i, fdcprint);
258 }
259 }
260
261 if(nfound) {
262
263 /*
264 * Make sure motor will be turned of when a floppy is
265 * inserted in the first selected drive.
266 */
267 fdselect(first_found, 0, FLP_DD);
268 fd_state = FLP_MON;
269 timeout((FPV)fdmotoroff, (void*)getsoftc(fdcd, first_found),
270 FLP_MONDELAY);
271
272 /*
273 * enable disk related interrupts
274 */
275 MFP->mf_ierb |= IB_DINT;
276 MFP->mf_iprb &= ~IB_DINT;
277 MFP->mf_imrb |= IB_DINT;
278 }
279 }
280
281 static int
282 fdcprint(auxp, pnp)
283 void *auxp;
284 char *pnp;
285 {
286 return(UNCONF);
287 }
288
289 static int fdmatch __P((struct device *, struct cfdata *, void *));
290 static void fdattach __P((struct device *, struct device *, void *));
291 void fdstrategy __P((struct buf *));
292 struct dkdriver fddkdriver = { fdstrategy };
293
294 struct cfdriver fdcd = {
295 NULL, "fd", (cfmatch_t)fdmatch, fdattach, DV_DISK,
296 sizeof(struct fd_softc), NULL, 0 };
297
298 static int
299 fdmatch(pdp, cfp, auxp)
300 struct device *pdp;
301 struct cfdata *cfp;
302 void *auxp;
303 {
304 int unit = (int)auxp;
305 return(1);
306 }
307
308 static void
309 fdattach(pdp, dp, auxp)
310 struct device *pdp, *dp;
311 void *auxp;
312 {
313 struct fd_softc *sc;
314
315 sc = (struct fd_softc *)dp;
316
317 printf("\n");
318
319 /*
320 * Initialize and attach the disk structure.
321 */
322 sc->dkdev.dk_name = sc->sc_dv.dv_xname;
323 sc->dkdev.dk_driver = &fddkdriver;
324 disk_attach(&sc->dkdev);
325 }
326
327 fdioctl(dev, cmd, addr, flag, p)
328 dev_t dev;
329 u_long cmd;
330 int flag;
331 caddr_t addr;
332 struct proc *p;
333 {
334 struct fd_softc *sc;
335 void *data;
336
337 sc = getsoftc(fdcd, DISKUNIT(dev));
338
339 if((sc->flags & FLPF_HAVELAB) == 0)
340 return(EBADF);
341
342 switch(cmd) {
343 case DIOCSBAD:
344 return(EINVAL);
345 case DIOCGDINFO:
346 *(struct disklabel *)addr = *(sc->dkdev.dk_label);
347 return(0);
348 case DIOCGPART:
349 ((struct partinfo *)addr)->disklab =
350 sc->dkdev.dk_label;
351 ((struct partinfo *)addr)->part =
352 &sc->dkdev.dk_label->d_partitions[DISKPART(dev)];
353 return(0);
354 #ifdef notyet /* XXX LWP */
355 case DIOCSRETRIES:
356 case DIOCSSTEP:
357 case DIOCSDINFO:
358 case DIOCWDINFO:
359 case DIOCWLABEL:
360 #endif /* notyet */
361 default:
362 return(ENOTTY);
363 }
364 }
365
366 /*
367 * Open the device. If this is the first open on both the floppy devices,
368 * intialize the controller.
369 * Note that partition info on the floppy device is used to distinguise
370 * between 780Kb and 360Kb floppy's.
371 * partition 0: 360Kb
372 * partition 1: 780Kb
373 */
374 Fdopen(dev, flags, devtype, proc)
375 dev_t dev;
376 int flags, devtype;
377 struct proc *proc;
378 {
379 struct fd_softc *sc;
380 int sps;
381
382 #ifdef FLP_DEBUG
383 printf("Fdopen dev=0x%x\n", dev);
384 #endif
385
386 if(DISKPART(dev) >= NR_TYPES)
387 return(ENXIO);
388
389 if((sc = getsoftc(fdcd, DISKUNIT(dev))) == NULL)
390 return(ENXIO);
391
392 /*
393 * If no floppy currently open, reset the controller and select
394 * floppy type.
395 */
396 if(!nopens) {
397
398 #ifdef FLP_DEBUG
399 printf("Fdopen device not yet open\n");
400 #endif
401 nopens++;
402 write_fdreg(FDC_CS, IRUPT);
403 delay(40);
404 }
405
406 /*
407 * Sleep while other process is opening the device
408 */
409 sps = splbio();
410 while(sc->flags & FLPF_INOPEN)
411 tsleep((caddr_t)sc, PRIBIO, "Fdopen", 0);
412 splx(sps);
413
414 if(!(sc->flags & FLPF_ISOPEN)) {
415 /*
416 * Initialise some driver values.
417 */
418 int part = DISKPART(dev);
419 void *addr;
420
421 sc->bufq.b_actf = NULL;
422 sc->unit = DISKUNIT(dev);
423 sc->part = part;
424 sc->nheads = fdtypes[part].nheads;
425 sc->nsectors = fdtypes[part].nsectors;
426 sc->nblocks = fdtypes[part].nblocks;
427 sc->density = fdtypes[part].density;
428 sc->curtrk = INV_TRK;
429 sc->sector = 0;
430 sc->errcnt = 0;
431 sc->bounceb = (u_char*)alloc_stmem(SECTOR_SIZE, &addr);
432 if(sc->bounceb == NULL)
433 return(ENOMEM); /* XXX */
434
435 /*
436 * Go get write protect + loaded status
437 */
438 sc->flags |= FLPF_INOPEN|FLPF_GETSTAT;
439 sps = splbio();
440 st_dmagrab(fdcint, fdstatus, sc, &lock_stat, 0);
441 while(sc->flags & FLPF_GETSTAT)
442 tsleep((caddr_t)sc, PRIBIO, "Fdopen", 0);
443 splx(sps);
444 wakeup((caddr_t)sc);
445
446 if((sc->flags & FLPF_WRTPROT) && (flags & FWRITE)) {
447 sc->flags = 0;
448 return(EPERM);
449 }
450 if(sc->flags & FLPF_EMPTY) {
451 sc->flags = 0;
452 return(ENXIO);
453 }
454 sc->flags &= ~(FLPF_INOPEN|FLPF_GETSTAT);
455 sc->flags |= FLPF_ISOPEN;
456 }
457 else {
458 /*
459 * Multiply opens are granted when accessing the same type of
460 * floppy (eq. the same partition).
461 */
462 if(sc->part != DISKPART(dev))
463 return(ENXIO); /* XXX temporarely out of business */
464 }
465 fdgetdisklabel(sc, dev);
466 #ifdef FLP_DEBUG
467 printf("Fdopen open succeeded on type %d\n", sc->part);
468 #endif
469 }
470
471 fdclose(dev, flags, devtype, proc)
472 dev_t dev;
473 int flags, devtype;
474 struct proc *proc;
475 {
476 struct fd_softc *sc;
477
478 sc = getsoftc(fdcd, DISKUNIT(dev));
479 free_stmem(sc->bounceb);
480 sc->flags = 0;
481 nopens--;
482
483 #ifdef FLP_DEBUG
484 printf("Closed floppy device -- nopens: %d\n", nopens);
485 #endif
486 return(0);
487 }
488
489 void
490 fdstrategy(bp)
491 struct buf *bp;
492 {
493 struct fd_softc *sc;
494 struct disklabel *lp;
495 int sps, nblocks;
496
497 sc = getsoftc(fdcd, DISKUNIT(bp->b_dev));
498
499 #ifdef FLP_DEBUG
500 printf("fdstrategy: 0x%x\n", bp);
501 #endif
502
503 /*
504 * check for valid partition and bounds
505 */
506 lp = sc->dkdev.dk_label;
507 if ((sc->flags & FLPF_HAVELAB) == 0) {
508 bp->b_error = EIO;
509 goto bad;
510 }
511 if (bounds_check_with_label(bp, lp, 0) <= 0)
512 goto done;
513
514 if (bp->b_bcount == 0)
515 goto done;
516
517 /*
518 * queue the buf and kick the low level code
519 */
520 sps = splbio();
521 disksort(&sc->bufq, bp);
522 if (!lock_stat) {
523 if (fd_state & FLP_MON)
524 untimeout((FPV)fdmotoroff, (void*)sc);
525 fd_state = FLP_IDLE;
526 st_dmagrab(fdcint, fdstart, sc, &lock_stat, 0);
527 }
528 splx(sps);
529
530 return;
531 bad:
532 bp->b_flags |= B_ERROR;
533 done:
534 bp->b_resid = bp->b_bcount;
535 biodone(bp);
536 }
537
538 /*
539 * no dumps to floppy disks thank you.
540 */
541 int
542 fddump(dev_t dev)
543 {
544 return(ENXIO);
545 }
546
547 /*
548 * no dumps to floppy disks thank you.
549 */
550 int
551 fdsize(dev)
552 dev_t dev;
553 {
554 return(-1);
555 }
556
557 int
558 fdread(dev, uio)
559 dev_t dev;
560 struct uio *uio;
561 {
562 return(physio(fdstrategy, NULL, dev, B_READ, fdminphys, uio));
563 }
564
565 int
566 fdwrite(dev, uio)
567 dev_t dev;
568 struct uio *uio;
569 {
570 return(physio(fdstrategy, NULL, dev, B_WRITE, fdminphys, uio));
571 }
572
573 /*
574 * Called through DMA-dispatcher, get status.
575 */
576 static void
577 fdstatus(sc)
578 struct fd_softc *sc;
579 {
580 #ifdef FLP_DEBUG
581 printf("fdstatus\n");
582 #endif
583 sc->errcnt = 0;
584 fd_state = FLP_STAT;
585 fd_xfer(sc);
586 }
587
588 /*
589 * Called through the dma-dispatcher. So we know we are the only ones
590 * messing with the floppy-controler.
591 * Initialize some fields in the fdsoftc for the state-machine and get
592 * it going.
593 */
594 static void
595 fdstart(sc)
596 struct fd_softc *sc;
597 {
598 struct buf *bp;
599
600 bp = sc->bufq.b_actf;
601 sc->sector = bp->b_blkno; /* Start sector for I/O */
602 sc->io_data = bp->b_data; /* KVA base for I/O */
603 sc->io_bytes = bp->b_bcount; /* Transfer size in bytes */
604 sc->io_dir = bp->b_flags & B_READ;/* Direction of transfer */
605 sc->errcnt = 0; /* No errors yet */
606 fd_state = FLP_XFER; /* Yes, we're going to transfer */
607
608 /* Instrumentation. */
609 disk_busy(&sc->dkdev);
610
611 fd_xfer(sc);
612 }
613
614 /*
615 * The current transaction is finished (for good or bad). Let go of
616 * the the dma-resources. Call biodone() to finish the transaction.
617 * Find a new transaction to work on.
618 */
619 static void
620 fddone(sc)
621 register struct fd_softc *sc;
622 {
623 struct buf *bp, *dp;
624 struct fd_softc *sc1;
625 int i, sps;
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
651 disk_unbusy(&sc->dkdev, (bp->b_bcount - bp->b_resid));
652
653 biodone(bp);
654 }
655 fd_state = FLP_MON;
656
657 if(lock_stat)
658 return; /* XXX Is this possible? */
659
660 /*
661 * Find a new transaction on round-robin basis.
662 */
663 for(i = sc->unit + 1; ;i++) {
664 if(i >= fdcd.cd_ndevs)
665 i = 0;
666 if((sc1 = fdcd.cd_devs[i]) == NULL)
667 continue;
668 if(sc1->bufq.b_actf)
669 break;
670 if(i == sc->unit) {
671 timeout((FPV)fdmotoroff, (void*)sc, FLP_MONDELAY);
672 #ifdef FLP_DEBUG
673 printf("fddone: Nothing to do\n");
674 #endif
675 return; /* No work */
676 }
677 }
678 fd_state = FLP_IDLE;
679 #ifdef FLP_DEBUG
680 printf("fddone: Staring job on unit %d\n", sc1->unit);
681 #endif
682 st_dmagrab(fdcint, fdstart, sc1, &lock_stat, 0);
683 }
684
685 static int
686 fdselect(drive, head, dense)
687 int drive, head, dense;
688 {
689 int i, sps, spinning;
690 #ifdef FLP_DEBUG
691 printf("fdselect: drive=%d, head=%d, dense=%d\n", drive, head, dense);
692 #endif
693 i = ((drive == 1) ? PA_FLOP1 : PA_FLOP0) | head;
694 spinning = motoron;
695 motoron = 1;
696
697 switch(dense) {
698 case FLP_DD:
699 DMA->dma_drvmode = 0;
700 break;
701 case FLP_HD:
702 DMA->dma_drvmode = (FDC_HDSET|FDC_HDSIG);
703 break;
704 default:
705 panic("fdselect: unknown density code\n");
706 }
707 if(i != selected) {
708 sps = splhigh();
709
710 selected = i;
711 SOUND->sd_selr = YM_IOA;
712 SOUND->sd_wdat = (SOUND->sd_rdat & 0x78) | (i ^ 0x07);
713 splx(sps);
714 }
715 return(spinning);
716 }
717
718 static void
719 fddeselect()
720 {
721 int sps;
722
723 sps = splhigh();
724 SOUND->sd_selr = YM_IOA;
725 SOUND->sd_wdat = SOUND->sd_rdat | 0x07;
726 splx(sps);
727
728 motoron = selected = 0;
729 DMA->dma_drvmode = 0;
730 }
731
732 /****************************************************************************
733 * The following functions assume to be running as a result of a *
734 * disk-interrupt (e.q. spl = splbio). *
735 * They form the finit-state machine, the actual driver. *
736 * *
737 * fdstart()/ --> fd_xfer() -> activate hardware *
738 * fdopen() ^ *
739 * | *
740 * +-- not ready -<------------+ *
741 * | *
742 * fdmotoroff()/ --> fdcint() -> fd_xfer_ok() ---+ *
743 * h/w interrupt | *
744 * \|/ *
745 * finished ---> fdone() *
746 * *
747 ****************************************************************************/
748 static void
749 fd_xfer(sc)
750 struct fd_softc *sc;
751 {
752 register int head = 0;
753 register int track, sector, hbit;
754 int i;
755 u_long phys_addr;
756
757 switch(fd_state) {
758 case FLP_XFER:
759 /*
760 * Calculate head/track values
761 */
762 track = sc->sector / sc->nsectors;
763 head = track % sc->nheads;
764 track = track / sc->nheads;
765 #ifdef FLP_DEBUG
766 printf("fd_xfer: sector:%d,head:%d,track:%d\n", sc->sector,head,
767 track);
768 #endif
769 break;
770
771 case FLP_STAT:
772 /*
773 * FLP_STAT only wants to recalibrate
774 */
775 sc->curtrk = INV_TRK;
776 break;
777 default:
778 panic("fd_xfer: wrong state (0x%x)", fd_state);
779 }
780
781 /*
782 * Select the drive.
783 */
784 hbit = fdselect(sc->unit, head, sc->density) ? HBIT : 0;
785
786 if(sc->curtrk == INV_TRK) {
787 /*
788 * Recalibrate, since we lost track of head positioning.
789 * The floppy disk controller has no way of determining its
790 * absolute arm position (track). Instead, it steps the
791 * arm a track at a time and keeps track of where it
792 * thinks it is (in software). However, after a SEEK, the
793 * hardware reads information from the diskette telling
794 * where the arm actually is. If the arm is in the wrong place,
795 * a recalibration is done, which forces the arm to track 0.
796 * This way the controller can get back into sync with reality.
797 */
798 fd_cmd = RESTORE;
799 write_fdreg(FDC_CS, RESTORE|VBIT|hbit);
800 timeout((FPV)fdmotoroff, (void*)sc, FLP_XFERDELAY);
801
802 #ifdef FLP_DEBUG
803 printf("fd_xfer:Recalibrating drive %d\n", sc->unit);
804 #endif
805 return;
806 }
807
808 write_fdreg(FDC_TR, sc->curtrk);
809
810 /*
811 * Issue a SEEK command on the indicated drive unless the arm is
812 * already positioned on the correct track.
813 */
814 if(track != sc->curtrk) {
815 sc->curtrk = track; /* be optimistic */
816 write_fdreg(FDC_DR, track);
817 write_fdreg(FDC_CS, SEEK|RATE6|VBIT|hbit);
818 timeout((FPV)fdmotoroff, (void*)sc, FLP_XFERDELAY);
819 fd_cmd = SEEK;
820 #ifdef FLP_DEBUG
821 printf("fd_xfer:Seek to track %d on drive %d\n",track,sc->unit);
822 #endif
823 return;
824 }
825
826 /*
827 * The drive is now on the proper track. Read or write 1 block.
828 */
829 sector = sc->sector % sc->nsectors;
830 sector++; /* start numbering at 1 */
831
832 write_fdreg(FDC_SR, sector);
833
834 phys_addr = (u_long)kvtop(sc->io_data);
835 if(phys_addr >= FDC_MAX_DMA_AD) {
836 /*
837 * We _must_ bounce this address
838 */
839 phys_addr = (u_long)kvtop(sc->bounceb);
840 if(sc->io_dir == B_WRITE)
841 bcopy(sc->io_data, sc->bounceb, SECTOR_SIZE);
842 sc->flags |= FLPF_BOUNCE;
843 }
844 st_dmaaddr_set((caddr_t)phys_addr); /* DMA address setup */
845
846 #ifdef FLP_DEBUG
847 printf("fd_xfer:Start io (io_addr:%x)\n", kvtop(sc->io_data));
848 #endif
849
850 if(sc->io_dir == B_READ) {
851 /* Issue the command */
852 st_dmacomm(DMA_FDC | DMA_SCREG, 1);
853 write_fdreg(FDC_CS, F_READ|hbit);
854 fd_cmd = F_READ;
855 }
856 else {
857 /* Issue the command */
858 st_dmacomm(DMA_WRBIT | DMA_FDC | DMA_SCREG, 1);
859 write_fdreg(DMA_WRBIT | FDC_CS, F_WRITE|hbit|EBIT|PBIT);
860 fd_cmd = F_WRITE;
861 }
862 timeout((FPV)fdmotoroff, (void*)sc, FLP_XFERDELAY);
863 }
864
865 /* return values of fd_xfer_ok(): */
866 #define X_OK 0
867 #define X_AGAIN 1
868 #define X_ERROR 2
869 #define X_FAIL 3
870
871 /*
872 * Hardware interrupt function.
873 */
874 static void
875 fdcint(sc)
876 struct fd_softc *sc;
877 {
878 struct buf *bp;
879
880 #ifdef FLP_DEBUG
881 printf("fdcint: unit = %d\n", sc->unit);
882 #endif
883
884 /*
885 * Cancel timeout (we made it, didn't we)
886 */
887 untimeout((FPV)fdmotoroff, (void*)sc);
888
889 switch(fd_xfer_ok(sc)) {
890 case X_ERROR :
891 if(++(sc->errcnt) < MAX_ERRORS) {
892 /*
893 * Command failed but still retries left.
894 */
895 break;
896 }
897 /* FALL THROUGH */
898 case X_FAIL :
899 /*
900 * Non recoverable error. Fall back to motor-on
901 * idle-state.
902 */
903 if(fd_error != NULL) {
904 printf("Floppy error: %s\n", fd_error);
905 fd_error = NULL;
906 }
907
908 if(fd_state == FLP_STAT) {
909 sc->flags |= FLPF_EMPTY;
910 sc->flags &= ~FLPF_GETSTAT;
911 wakeup((caddr_t)sc);
912 fddone(sc);
913 return;
914 }
915
916 bp = sc->bufq.b_actf;
917
918 bp->b_error = EIO;
919 bp->b_flags |= B_ERROR;
920 fd_state = FLP_MON;
921
922 break;
923 case X_AGAIN:
924 /*
925 * Start next part of state machine.
926 */
927 break;
928 case X_OK:
929 /*
930 * Command ok and finished. Reset error-counter.
931 * If there are no more bytes to transfer fall back
932 * to motor-on idle state.
933 */
934 sc->errcnt = 0;
935
936 if(fd_state == FLP_STAT) {
937 sc->flags &= ~FLPF_GETSTAT;
938 wakeup((caddr_t)sc);
939 fddone(sc);
940 return;
941 }
942
943 if((sc->flags & FLPF_BOUNCE) && (sc->io_dir == B_READ))
944 bcopy(sc->bounceb, sc->io_data, SECTOR_SIZE);
945 sc->flags &= ~FLPF_BOUNCE;
946
947 sc->sector++;
948 sc->io_data += SECTOR_SIZE;
949 sc->io_bytes -= SECTOR_SIZE;
950 if(sc->io_bytes <= 0)
951 fd_state = FLP_MON;
952 }
953 if(fd_state == FLP_MON)
954 fddone(sc);
955 else fd_xfer(sc);
956 }
957
958 /*
959 * Determine status of last command. Should only be called through
960 * 'fdcint()'.
961 * Returns:
962 * X_ERROR : Error on command; might succeed next time.
963 * X_FAIL : Error on command; will never succeed.
964 * X_AGAIN : Part of a command succeeded, call 'fd_xfer()' to complete.
965 * X_OK : Command succeeded and is complete.
966 *
967 * This function only affects sc->curtrk.
968 */
969 static int
970 fd_xfer_ok(sc)
971 register struct fd_softc *sc;
972 {
973 register int status;
974
975 #ifdef FLP_DEBUG
976 printf("fd_xfer_ok: cmd: 0x%x, state: 0x%x\n", fd_cmd, fd_state);
977 #endif
978 switch(fd_cmd) {
979 case IRUPT:
980 /*
981 * Timeout. Force a recalibrate before we try again.
982 */
983 status = read_fdreg(FDC_CS);
984
985 fd_error = "Timeout";
986 sc->curtrk = INV_TRK;
987 return(X_ERROR);
988 case F_READ:
989 /*
990 * Test for DMA error
991 */
992 status = read_dmastat();
993 if(!(status & DMAOK)) {
994 fd_error = "Dma error";
995 return(X_ERROR);
996 }
997 /*
998 * Get controller status and check for errors.
999 */
1000 status = read_fdreg(FDC_CS);
1001 if(status & (RNF | CRCERR | LD_T00)) {
1002 fd_error = "Read error";
1003 if(status & RNF)
1004 sc->curtrk = INV_TRK;
1005 return(X_ERROR);
1006 }
1007 break;
1008 case F_WRITE:
1009 /*
1010 * Test for DMA error
1011 */
1012 status = read_dmastat();
1013 if(!(status & DMAOK)) {
1014 fd_error = "Dma error";
1015 return(X_ERROR);
1016 }
1017 /*
1018 * Get controller status and check for errors.
1019 */
1020 status = read_fdreg(FDC_CS);
1021 if(status & WRI_PRO) {
1022 fd_error = "Write protected";
1023 return(X_FAIL);
1024 }
1025 if(status & (RNF | CRCERR | LD_T00)) {
1026 fd_error = "Write error";
1027 sc->curtrk = INV_TRK;
1028 return(X_ERROR);
1029 }
1030 break;
1031 case SEEK:
1032 status = read_fdreg(FDC_CS);
1033 if(status & (RNF | CRCERR)) {
1034 fd_error = "Seek error";
1035 sc->curtrk = INV_TRK;
1036 return(X_ERROR);
1037 }
1038 return(X_AGAIN);
1039 case RESTORE:
1040 /*
1041 * Determine if the recalibration succeeded.
1042 */
1043 status = read_fdreg(FDC_CS);
1044 if(status & RNF) {
1045 fd_error = "Recalibrate error";
1046 /* reset controller */
1047 write_fdreg(FDC_CS, IRUPT);
1048 sc->curtrk = INV_TRK;
1049 return(X_ERROR);
1050 }
1051 sc->curtrk = 0;
1052 if(fd_state == FLP_STAT) {
1053 if(status & WRI_PRO)
1054 sc->flags |= FLPF_WRTPROT;
1055 break;
1056 }
1057 return(X_AGAIN);
1058 default:
1059 fd_error = "Driver error: fd_xfer_ok : Unknown state";
1060 return(X_FAIL);
1061 }
1062 return(X_OK);
1063 }
1064
1065 /*
1066 * All timeouts will call this function.
1067 */
1068 static void
1069 fdmotoroff(sc)
1070 struct fd_softc *sc;
1071 {
1072 int sps;
1073
1074 /*
1075 * Get at harware interrupt level
1076 */
1077 sps = splbio();
1078
1079 #if FLP_DEBUG
1080 printf("fdmotoroff, state = 0x%x\n", fd_state);
1081 #endif
1082
1083 switch(fd_state) {
1084 case FLP_STAT :
1085 case FLP_XFER :
1086 /*
1087 * Timeout during a transfer; cancel transaction
1088 * set command to 'IRUPT'.
1089 * A drive-interrupt is simulated to trigger the state
1090 * machine.
1091 */
1092 /*
1093 * Cancel current transaction
1094 */
1095 fd_cmd = IRUPT;
1096 write_fdreg(FDC_CS, IRUPT);
1097 delay(20);
1098 (void)read_fdreg(FDC_CS);
1099 write_fdreg(FDC_CS, RESTORE);
1100 break;
1101
1102 case FLP_MON :
1103 /*
1104 * Turn motor off.
1105 */
1106 if(selected) {
1107 int tmp;
1108
1109 st_dmagrab(fdcint, fdmoff, sc, &tmp, 0);
1110 }
1111 else fd_state = FLP_IDLE;
1112 break;
1113 }
1114 splx(sps);
1115 }
1116
1117 /*
1118 * min byte count to whats left of the track in question
1119 */
1120 static void
1121 fdminphys(bp)
1122 struct buf *bp;
1123 {
1124 struct fd_softc *sc;
1125 int sec, toff, tsz;
1126
1127 if((sc = getsoftc(fdcd, DISKUNIT(bp->b_dev))) == NULL)
1128 panic("fdminphys: couldn't get softc");
1129
1130 sec = bp->b_blkno % (sc->nsectors * sc->nheads);
1131 toff = sec * SECTOR_SIZE;
1132 tsz = sc->nsectors * sc->nheads * SECTOR_SIZE;
1133
1134 #ifdef FLP_DEBUG
1135 printf("fdminphys: before %d", bp->b_bcount);
1136 #endif
1137
1138 bp->b_bcount = min(bp->b_bcount, tsz - toff);
1139
1140 #ifdef FLP_DEBUG
1141 printf(" after %d\n", bp->b_bcount);
1142 #endif
1143
1144 minphys(bp);
1145 }
1146
1147 /*
1148 * Called from fdmotoroff to turn the motor actually off....
1149 * This can't be done in fdmotoroff itself, because exclusive access to the
1150 * DMA controller is needed to read the FDC-status register. The function
1151 * 'fdmoff()' always runs as the result of a 'dmagrab()'.
1152 * We need to test the status-register because we want to be sure that the
1153 * drive motor is really off before deselecting the drive. The FDC only
1154 * turns off the drive motor after having seen 10 index-pulses. You only
1155 * get index-pulses when a drive is selected....This means that if the
1156 * drive is deselected when the motor is still spinning, it will continue
1157 * to spin _even_ when you insert a floppy later on...
1158 */
1159 static void
1160 fdmoff(fdsoftc)
1161 struct fd_softc *fdsoftc;
1162 {
1163 int tmp;
1164
1165 if ((fd_state == FLP_MON) && selected) {
1166 tmp = read_fdreg(FDC_CS);
1167 if (!(tmp & MOTORON)) {
1168 fddeselect();
1169 fd_state = FLP_IDLE;
1170 }
1171 else timeout((FPV)fdmotoroff, (void*)fdsoftc, 10*FLP_MONDELAY);
1172 }
1173 st_dmafree(fdsoftc, &tmp);
1174 }
1175
1176 /*
1177 * Used to find out wich drives are actually connected. We do this by issueing
1178 * is 'RESTORE' command and check if the 'track-0' bit is set. This also works
1179 * if the drive is present but no floppy is inserted.
1180 */
1181 static void
1182 fdtestdrv(fdsoftc)
1183 struct fd_softc *fdsoftc;
1184 {
1185 int i, status;
1186
1187 /*
1188 * Select the right unit and head.
1189 */
1190 fdselect(fdsoftc->unit, 0, FLP_DD);
1191
1192 write_fdreg(FDC_CS, RESTORE|HBIT);
1193
1194 /*
1195 * Wait for about 2 seconds.
1196 */
1197 delay(2000000);
1198
1199 status = read_fdreg(FDC_CS);
1200 if(status & (RNF|BUSY)) {
1201 write_fdreg(FDC_CS, IRUPT); /* reset controller */
1202 delay(40);
1203 }
1204
1205 if(!(status & LD_T00))
1206 fdsoftc->flags |= FLPF_NOTRESP;
1207
1208 fddeselect();
1209 }
1210
1211 /*
1212 * Build disk label. For now we only create a label from what we know
1213 * from 'sc'.
1214 */
1215 static int
1216 fdgetdisklabel(sc, dev)
1217 struct fd_softc *sc;
1218 dev_t dev;
1219 {
1220 struct disklabel *lp, *dlp;
1221 int part;
1222
1223 /*
1224 * If we already got one, get out.
1225 */
1226 if(sc->flags & FLPF_HAVELAB)
1227 return(0);
1228
1229 #ifdef FLP_DEBUG
1230 printf("fdgetdisklabel()\n");
1231 #endif
1232
1233 part = DISKPART(dev);
1234 lp = sc->dkdev.dk_label;
1235 bzero(lp, sizeof(struct disklabel));
1236
1237 lp->d_secsize = SECTOR_SIZE;
1238 lp->d_ntracks = sc->nheads;
1239 lp->d_nsectors = sc->nsectors;
1240 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1241 lp->d_ncylinders = sc->nblocks / lp->d_secpercyl;
1242 lp->d_secperunit = sc->nblocks;
1243
1244 lp->d_type = DTYPE_FLOPPY;
1245 lp->d_rpm = 300; /* good guess I suppose. */
1246 lp->d_interleave = 1; /* FIXME: is this OK? */
1247 lp->d_bbsize = 0;
1248 lp->d_sbsize = 0;
1249 lp->d_npartitions = part + 1;
1250 lp->d_trkseek = STEP_DELAY;
1251 lp->d_magic = DISKMAGIC;
1252 lp->d_magic2 = DISKMAGIC;
1253 lp->d_checksum = dkcksum(lp);
1254 lp->d_partitions[part].p_size = lp->d_secperunit;
1255 lp->d_partitions[part].p_fstype = FS_UNUSED;
1256 lp->d_partitions[part].p_fsize = 1024;
1257 lp->d_partitions[part].p_frag = 8;
1258 sc->flags |= FLPF_HAVELAB;
1259
1260 return(0);
1261 }
1262