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