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