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