fd.c revision 1.37.2.1 1 /* $NetBSD: fd.c,v 1.37.2.1 2001/10/10 11:55:59 fvdl 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 *, struct vnode *));
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(devvp, cmd, addr, flag, p)
410 struct vnode *devvp;
411 u_long cmd;
412 int flag;
413 caddr_t addr;
414 struct proc *p;
415 {
416 struct fd_softc *sc;
417
418 sc = vdev_privdata(devvp);
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(devvp, flags, devtype, proc)
461 struct vnode *devvp;
462 int flags, devtype;
463 struct proc *proc;
464 {
465 struct fd_softc *sc;
466 int sps;
467 dev_t dev;
468
469 dev = vdev_rdev(devvp);
470
471 #ifdef FLP_DEBUG
472 printf("fdopen dev=0x%x\n", dev);
473 #endif
474
475 if(FLP_TYPE(dev) >= NR_TYPES)
476 return(ENXIO);
477
478 if((sc = getsoftc(fd_cd, DISKUNIT(dev))) == NULL)
479 return(ENXIO);
480
481 /*
482 * If no floppy currently open, reset the controller and select
483 * floppy type.
484 */
485 if(!nopens) {
486
487 #ifdef FLP_DEBUG
488 printf("fdopen device not yet open\n");
489 #endif
490 nopens++;
491 write_fdreg(FDC_CS, IRUPT);
492 delay(40);
493 }
494
495 vdev_setprivdata(devvp, sc);
496
497 /*
498 * Sleep while other process is opening the device
499 */
500 sps = splbio();
501 while(sc->flags & FLPF_INOPEN)
502 tsleep((caddr_t)sc, PRIBIO, "fdopen", 0);
503 splx(sps);
504
505 if(!(sc->flags & FLPF_ISOPEN)) {
506 /*
507 * Initialise some driver values.
508 */
509 int type;
510 void *addr;
511
512 type = FLP_TYPE(dev);
513
514 BUFQ_INIT(&sc->bufq);
515 sc->unit = DISKUNIT(dev);
516 sc->part = RAW_PART;
517 sc->nheads = fdtypes[type].nheads;
518 sc->nsectors = fdtypes[type].nsectors;
519 sc->nblocks = fdtypes[type].nblocks;
520 sc->density = fdtypes[type].density;
521 sc->curtrk = INV_TRK;
522 sc->sector = 0;
523 sc->errcnt = 0;
524 sc->bounceb = (u_char*)alloc_stmem(SECTOR_SIZE, &addr);
525 if(sc->bounceb == NULL)
526 return(ENOMEM); /* XXX */
527
528 /*
529 * Go get write protect + loaded status
530 */
531 sc->flags |= FLPF_INOPEN|FLPF_GETSTAT;
532 sps = splbio();
533 st_dmagrab((dma_farg)fdcint, (dma_farg)fdstatus, sc,
534 &lock_stat, 0);
535 while(sc->flags & FLPF_GETSTAT)
536 tsleep((caddr_t)sc, PRIBIO, "fdopen", 0);
537 splx(sps);
538 wakeup((caddr_t)sc);
539
540 if((sc->flags & FLPF_WRTPROT) && (flags & FWRITE)) {
541 sc->flags = 0;
542 return(EPERM);
543 }
544 if(sc->flags & FLPF_EMPTY) {
545 sc->flags = 0;
546 return(ENXIO);
547 }
548 sc->flags &= ~(FLPF_INOPEN|FLPF_GETSTAT);
549 sc->flags |= FLPF_ISOPEN;
550 }
551 else {
552 /*
553 * Multiply opens are granted when accessing the same type of
554 * floppy (eq. the same partition).
555 */
556 if(sc->density != fdtypes[DISKPART(dev)].density)
557 return(ENXIO); /* XXX temporarely out of business */
558 }
559 fdgetdisklabel(sc, devvp);
560 #ifdef FLP_DEBUG
561 printf("fdopen open succeeded on type %d\n", sc->part);
562 #endif
563 return (0);
564 }
565
566 int
567 fdclose(devvp, flags, devtype, proc)
568 struct vnode *devvp;
569 int flags, devtype;
570 struct proc *proc;
571 {
572 struct fd_softc *sc;
573
574 sc = vdev_privdata(devvp);
575
576 free_stmem(sc->bounceb);
577 sc->flags = 0;
578 nopens--;
579
580 #ifdef FLP_DEBUG
581 printf("Closed floppy device -- nopens: %d\n", nopens);
582 #endif
583 return(0);
584 }
585
586 void
587 fdstrategy(bp)
588 struct buf *bp;
589 {
590 struct fd_softc *sc;
591 struct disklabel *lp;
592 int sps, sz;
593
594 sc = vdev_privdata(bp->b_devvp);
595
596 #ifdef FLP_DEBUG
597 printf("fdstrategy: %p, b_bcount: %ld\n", bp, bp->b_bcount);
598 #endif
599
600 /*
601 * check for valid partition and bounds
602 */
603 lp = sc->dkdev.dk_label;
604 if ((sc->flags & FLPF_HAVELAB) == 0) {
605 bp->b_error = EIO;
606 goto bad;
607 }
608 if (bp->b_blkno < 0 || (bp->b_bcount % SECTOR_SIZE)) {
609 bp->b_error = EINVAL;
610 goto bad;
611 }
612 if (bp->b_bcount == 0)
613 goto done;
614
615 sz = howmany(bp->b_bcount, SECTOR_SIZE);
616
617 if (bp->b_blkno + sz > sc->nblocks) {
618 sz = sc->nblocks - bp->b_blkno;
619 if (sz == 0) /* Exactly at EndOfDisk */
620 goto done;
621 if (sz < 0) { /* Past EndOfDisk */
622 bp->b_error = EINVAL;
623 goto bad;
624 }
625 /* Trucate it */
626 if (bp->b_flags & B_RAW)
627 bp->b_bcount = sz << DEV_BSHIFT;
628 else bp->b_bcount = sz * lp->d_secsize;
629 }
630
631 /* No partition translation. */
632 bp->b_rawblkno = bp->b_blkno;
633
634 /*
635 * queue the buf and kick the low level code
636 */
637 sps = splbio();
638 disksort_blkno(&sc->bufq, bp); /* XXX disksort_cylinder */
639 if (!lock_stat) {
640 if (fd_state & FLP_MON)
641 callout_stop(&sc->sc_motor_ch);
642 fd_state = FLP_IDLE;
643 st_dmagrab((dma_farg)fdcint, (dma_farg)fdstart, sc,
644 &lock_stat, 0);
645 }
646 splx(sps);
647
648 return;
649 bad:
650 bp->b_flags |= B_ERROR;
651 done:
652 bp->b_resid = bp->b_bcount;
653 biodone(bp);
654 }
655
656 /*
657 * no dumps to floppy disks thank you.
658 */
659 int
660 fddump(dev, blkno, va, size)
661 dev_t dev;
662 daddr_t blkno;
663 caddr_t va;
664 size_t size;
665 {
666 return(ENXIO);
667 }
668
669 /*
670 * no dumps to floppy disks thank you.
671 */
672 int
673 fdsize(dev)
674 dev_t dev;
675 {
676 return(-1);
677 }
678
679 int
680 fdread(devvp, uio, flags)
681 struct vnode *devvp;
682 struct uio *uio;
683 int flags;
684 {
685 return(physio(fdstrategy, NULL, devvp, B_READ, fdminphys, uio));
686 }
687
688 int
689 fdwrite(devvp, uio, flags)
690 struct vnode *devvp;
691 struct uio *uio;
692 int flags;
693 {
694 return(physio(fdstrategy, NULL, devvp, B_WRITE, fdminphys, uio));
695 }
696
697 /*
698 * Called through DMA-dispatcher, get status.
699 */
700 static void
701 fdstatus(sc)
702 struct fd_softc *sc;
703 {
704 #ifdef FLP_DEBUG
705 printf("fdstatus\n");
706 #endif
707 sc->errcnt = 0;
708 fd_state = FLP_STAT;
709 fd_xfer(sc);
710 }
711
712 /*
713 * Called through the dma-dispatcher. So we know we are the only ones
714 * messing with the floppy-controler.
715 * Initialize some fields in the fdsoftc for the state-machine and get
716 * it going.
717 */
718 static void
719 fdstart(sc)
720 struct fd_softc *sc;
721 {
722 struct buf *bp;
723
724 bp = BUFQ_FIRST(&sc->bufq);
725 sc->sector = bp->b_blkno; /* Start sector for I/O */
726 sc->io_data = bp->b_data; /* KVA base for I/O */
727 sc->io_bytes = bp->b_bcount; /* Transfer size in bytes */
728 sc->io_dir = bp->b_flags & B_READ;/* Direction of transfer */
729 sc->errcnt = 0; /* No errors yet */
730 fd_state = FLP_XFER; /* Yes, we're going to transfer */
731
732 /* Instrumentation. */
733 disk_busy(&sc->dkdev);
734
735 fd_xfer(sc);
736 }
737
738 /*
739 * The current transaction is finished (for good or bad). Let go of
740 * the dma-resources. Call biodone() to finish the transaction.
741 * Find a new transaction to work on.
742 */
743 static void
744 fddone(sc)
745 register struct fd_softc *sc;
746 {
747 struct buf *bp;
748 struct fd_softc *sc1;
749 int i, sps;
750
751 /*
752 * Give others a chance to use the dma.
753 */
754 st_dmafree(sc, &lock_stat);
755
756
757 if(fd_state != FLP_STAT) {
758 /*
759 * Finish current transaction.
760 */
761 sps = splbio();
762 bp = BUFQ_FIRST(&sc->bufq);
763 if (bp == NULL)
764 panic("fddone");
765 BUFQ_REMOVE(&sc->bufq, bp);
766 splx(sps);
767
768 #ifdef FLP_DEBUG
769 printf("fddone: unit: %d, buf: %p, resid: %d\n",sc->unit,bp,
770 sc->io_bytes);
771 #endif
772 bp->b_resid = sc->io_bytes;
773
774 disk_unbusy(&sc->dkdev, (bp->b_bcount - bp->b_resid));
775
776 biodone(bp);
777 }
778 fd_state = FLP_MON;
779
780 if(lock_stat)
781 return; /* XXX Is this possible? */
782
783 /*
784 * Find a new transaction on round-robin basis.
785 */
786 for(i = sc->unit + 1; ;i++) {
787 if(i >= fd_cd.cd_ndevs)
788 i = 0;
789 if((sc1 = fd_cd.cd_devs[i]) == NULL)
790 continue;
791 if (BUFQ_FIRST(&sc1->bufq) != NULL)
792 break;
793 if(i == sc->unit) {
794 callout_reset(&sc->sc_motor_ch, FLP_MONDELAY,
795 (FPV)fdmotoroff, sc);
796 #ifdef FLP_DEBUG
797 printf("fddone: Nothing to do\n");
798 #endif
799 return; /* No work */
800 }
801 }
802 fd_state = FLP_IDLE;
803 #ifdef FLP_DEBUG
804 printf("fddone: Staring job on unit %d\n", sc1->unit);
805 #endif
806 st_dmagrab((dma_farg)fdcint, (dma_farg)fdstart, sc1, &lock_stat, 0);
807 }
808
809 static int
810 fdselect(drive, head, dense)
811 int drive, head, dense;
812 {
813 int i, spinning;
814 #ifdef FLP_DEBUG
815 printf("fdselect: drive=%d, head=%d, dense=%d\n", drive, head, dense);
816 #endif
817 i = ((drive == 1) ? PA_FLOP1 : PA_FLOP0) | head;
818 spinning = motoron;
819 motoron = 1;
820
821 switch(dense) {
822 case FLP_DD:
823 DMA->dma_drvmode = 0;
824 break;
825 case FLP_HD:
826 DMA->dma_drvmode = (FDC_HDSET|FDC_HDSIG);
827 break;
828 default:
829 panic("fdselect: unknown density code\n");
830 }
831 if(i != selected) {
832 selected = i;
833 ym2149_fd_select((i ^ PA_FDSEL));
834 }
835 return(spinning);
836 }
837
838 static void
839 fddeselect()
840 {
841 ym2149_fd_select(PA_FDSEL);
842 motoron = selected = 0;
843 DMA->dma_drvmode = 0;
844 }
845
846 /****************************************************************************
847 * The following functions assume to be running as a result of a *
848 * disk-interrupt (e.q. spl = splbio). *
849 * They form the finit-state machine, the actual driver. *
850 * *
851 * fdstart()/ --> fd_xfer() -> activate hardware *
852 * fdopen() ^ *
853 * | *
854 * +-- not ready -<------------+ *
855 * | *
856 * fdmotoroff()/ --> fdcint() -> fd_xfer_ok() ---+ *
857 * h/w interrupt | *
858 * \|/ *
859 * finished ---> fdone() *
860 * *
861 ****************************************************************************/
862 static void
863 fd_xfer(sc)
864 struct fd_softc *sc;
865 {
866 register int head;
867 register int track, sector, hbit;
868 u_long phys_addr;
869
870 head = track = 0;
871 switch(fd_state) {
872 case FLP_XFER:
873 /*
874 * Calculate head/track values
875 */
876 track = sc->sector / sc->nsectors;
877 head = track % sc->nheads;
878 track = track / sc->nheads;
879 #ifdef FLP_DEBUG
880 printf("fd_xfer: sector:%d,head:%d,track:%d\n", sc->sector,head,
881 track);
882 #endif
883 break;
884
885 case FLP_STAT:
886 /*
887 * FLP_STAT only wants to recalibrate
888 */
889 sc->curtrk = INV_TRK;
890 break;
891 default:
892 panic("fd_xfer: wrong state (0x%x)", fd_state);
893 }
894
895 /*
896 * Select the drive.
897 */
898 hbit = fdselect(sc->unit, head, sc->density) ? HBIT : 0;
899
900 if(sc->curtrk == INV_TRK) {
901 /*
902 * Recalibrate, since we lost track of head positioning.
903 * The floppy disk controller has no way of determining its
904 * absolute arm position (track). Instead, it steps the
905 * arm a track at a time and keeps track of where it
906 * thinks it is (in software). However, after a SEEK, the
907 * hardware reads information from the diskette telling
908 * where the arm actually is. If the arm is in the wrong place,
909 * a recalibration is done, which forces the arm to track 0.
910 * This way the controller can get back into sync with reality.
911 */
912 fd_cmd = RESTORE;
913 write_fdreg(FDC_CS, RESTORE|VBIT|hbit);
914 callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
915 (FPV)fdmotoroff, sc);
916
917 #ifdef FLP_DEBUG
918 printf("fd_xfer:Recalibrating drive %d\n", sc->unit);
919 #endif
920 return;
921 }
922
923 write_fdreg(FDC_TR, sc->curtrk);
924
925 /*
926 * Issue a SEEK command on the indicated drive unless the arm is
927 * already positioned on the correct track.
928 */
929 if(track != sc->curtrk) {
930 sc->curtrk = track; /* be optimistic */
931 write_fdreg(FDC_DR, track);
932 write_fdreg(FDC_CS, SEEK|RATE6|VBIT|hbit);
933 callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
934 (FPV)fdmotoroff, sc);
935 fd_cmd = SEEK;
936 #ifdef FLP_DEBUG
937 printf("fd_xfer:Seek to track %d on drive %d\n",track,sc->unit);
938 #endif
939 return;
940 }
941
942 /*
943 * The drive is now on the proper track. Read or write 1 block.
944 */
945 sector = sc->sector % sc->nsectors;
946 sector++; /* start numbering at 1 */
947
948 write_fdreg(FDC_SR, sector);
949
950 phys_addr = (u_long)kvtop(sc->io_data);
951 if(phys_addr >= FDC_MAX_DMA_AD) {
952 /*
953 * We _must_ bounce this address
954 */
955 phys_addr = (u_long)kvtop(sc->bounceb);
956 if(sc->io_dir == B_WRITE)
957 bcopy(sc->io_data, sc->bounceb, SECTOR_SIZE);
958 sc->flags |= FLPF_BOUNCE;
959 }
960 st_dmaaddr_set((caddr_t)phys_addr); /* DMA address setup */
961
962 #ifdef FLP_DEBUG
963 printf("fd_xfer:Start io (io_addr:%lx)\n", (u_long)kvtop(sc->io_data));
964 #endif
965
966 if(sc->io_dir == B_READ) {
967 /* Issue the command */
968 st_dmacomm(DMA_FDC | DMA_SCREG, 1);
969 write_fdreg(FDC_CS, F_READ|hbit);
970 fd_cmd = F_READ;
971 }
972 else {
973 /* Issue the command */
974 st_dmacomm(DMA_WRBIT | DMA_FDC | DMA_SCREG, 1);
975 write_fdreg(DMA_WRBIT | FDC_CS, F_WRITE|hbit|EBIT|PBIT);
976 fd_cmd = F_WRITE;
977 }
978 callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY, (FPV)fdmotoroff, sc);
979 }
980
981 /* return values of fd_xfer_ok(): */
982 #define X_OK 0
983 #define X_AGAIN 1
984 #define X_ERROR 2
985 #define X_FAIL 3
986
987 /*
988 * Hardware interrupt function.
989 */
990 static void
991 fdcint(sc)
992 struct fd_softc *sc;
993 {
994 struct buf *bp;
995
996 #ifdef FLP_DEBUG
997 printf("fdcint: unit = %d\n", sc->unit);
998 #endif
999
1000 /*
1001 * Cancel timeout (we made it, didn't we)
1002 */
1003 callout_stop(&sc->sc_motor_ch);
1004
1005 switch(fd_xfer_ok(sc)) {
1006 case X_ERROR :
1007 if(++(sc->errcnt) < MAX_ERRORS) {
1008 /*
1009 * Command failed but still retries left.
1010 */
1011 break;
1012 }
1013 /* FALL THROUGH */
1014 case X_FAIL :
1015 /*
1016 * Non recoverable error. Fall back to motor-on
1017 * idle-state.
1018 */
1019 if(fd_error != NULL) {
1020 printf("Floppy error: %s\n", fd_error);
1021 fd_error = NULL;
1022 }
1023
1024 if(fd_state == FLP_STAT) {
1025 sc->flags |= FLPF_EMPTY;
1026 sc->flags &= ~FLPF_GETSTAT;
1027 wakeup((caddr_t)sc);
1028 fddone(sc);
1029 return;
1030 }
1031
1032 bp = BUFQ_FIRST(&sc->bufq);
1033
1034 bp->b_error = EIO;
1035 bp->b_flags |= B_ERROR;
1036 fd_state = FLP_MON;
1037
1038 break;
1039 case X_AGAIN:
1040 /*
1041 * Start next part of state machine.
1042 */
1043 break;
1044 case X_OK:
1045 /*
1046 * Command ok and finished. Reset error-counter.
1047 * If there are no more bytes to transfer fall back
1048 * to motor-on idle state.
1049 */
1050 sc->errcnt = 0;
1051
1052 if(fd_state == FLP_STAT) {
1053 sc->flags &= ~FLPF_GETSTAT;
1054 wakeup((caddr_t)sc);
1055 fddone(sc);
1056 return;
1057 }
1058
1059 if((sc->flags & FLPF_BOUNCE) && (sc->io_dir == B_READ))
1060 bcopy(sc->bounceb, sc->io_data, SECTOR_SIZE);
1061 sc->flags &= ~FLPF_BOUNCE;
1062
1063 sc->sector++;
1064 sc->io_data += SECTOR_SIZE;
1065 sc->io_bytes -= SECTOR_SIZE;
1066 if(sc->io_bytes <= 0)
1067 fd_state = FLP_MON;
1068 }
1069 if(fd_state == FLP_MON)
1070 fddone(sc);
1071 else fd_xfer(sc);
1072 }
1073
1074 /*
1075 * Determine status of last command. Should only be called through
1076 * 'fdcint()'.
1077 * Returns:
1078 * X_ERROR : Error on command; might succeed next time.
1079 * X_FAIL : Error on command; will never succeed.
1080 * X_AGAIN : Part of a command succeeded, call 'fd_xfer()' to complete.
1081 * X_OK : Command succeeded and is complete.
1082 *
1083 * This function only affects sc->curtrk.
1084 */
1085 static int
1086 fd_xfer_ok(sc)
1087 register struct fd_softc *sc;
1088 {
1089 register int status;
1090
1091 #ifdef FLP_DEBUG
1092 printf("fd_xfer_ok: cmd: 0x%x, state: 0x%x\n", fd_cmd, fd_state);
1093 #endif
1094 switch(fd_cmd) {
1095 case IRUPT:
1096 /*
1097 * Timeout. Force a recalibrate before we try again.
1098 */
1099 status = read_fdreg(FDC_CS);
1100
1101 fd_error = "Timeout";
1102 sc->curtrk = INV_TRK;
1103 return(X_ERROR);
1104 case F_READ:
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 & (RNF | CRCERR | LD_T00)) {
1118 fd_error = "Read error";
1119 if(status & RNF)
1120 sc->curtrk = INV_TRK;
1121 return(X_ERROR);
1122 }
1123 break;
1124 case F_WRITE:
1125 /*
1126 * Test for DMA error
1127 */
1128 status = read_dmastat();
1129 if(!(status & DMAOK)) {
1130 fd_error = "Dma error";
1131 return(X_ERROR);
1132 }
1133 /*
1134 * Get controller status and check for errors.
1135 */
1136 status = read_fdreg(FDC_CS);
1137 if(status & WRI_PRO) {
1138 fd_error = "Write protected";
1139 return(X_FAIL);
1140 }
1141 if(status & (RNF | CRCERR | LD_T00)) {
1142 fd_error = "Write error";
1143 sc->curtrk = INV_TRK;
1144 return(X_ERROR);
1145 }
1146 break;
1147 case SEEK:
1148 status = read_fdreg(FDC_CS);
1149 if(status & (RNF | CRCERR)) {
1150 fd_error = "Seek error";
1151 sc->curtrk = INV_TRK;
1152 return(X_ERROR);
1153 }
1154 return(X_AGAIN);
1155 case RESTORE:
1156 /*
1157 * Determine if the recalibration succeeded.
1158 */
1159 status = read_fdreg(FDC_CS);
1160 if(status & RNF) {
1161 fd_error = "Recalibrate error";
1162 /* reset controller */
1163 write_fdreg(FDC_CS, IRUPT);
1164 sc->curtrk = INV_TRK;
1165 return(X_ERROR);
1166 }
1167 sc->curtrk = 0;
1168 if(fd_state == FLP_STAT) {
1169 if(status & WRI_PRO)
1170 sc->flags |= FLPF_WRTPROT;
1171 break;
1172 }
1173 return(X_AGAIN);
1174 default:
1175 fd_error = "Driver error: fd_xfer_ok : Unknown state";
1176 return(X_FAIL);
1177 }
1178 return(X_OK);
1179 }
1180
1181 /*
1182 * All timeouts will call this function.
1183 */
1184 static void
1185 fdmotoroff(sc)
1186 struct fd_softc *sc;
1187 {
1188 int sps;
1189
1190 /*
1191 * Get at harware interrupt level
1192 */
1193 sps = splbio();
1194
1195 #if FLP_DEBUG
1196 printf("fdmotoroff, state = 0x%x\n", fd_state);
1197 #endif
1198
1199 switch(fd_state) {
1200 case FLP_STAT :
1201 case FLP_XFER :
1202 /*
1203 * Timeout during a transfer; cancel transaction
1204 * set command to 'IRUPT'.
1205 * A drive-interrupt is simulated to trigger the state
1206 * machine.
1207 */
1208 /*
1209 * Cancel current transaction
1210 */
1211 fd_cmd = IRUPT;
1212 write_fdreg(FDC_CS, IRUPT);
1213 delay(20);
1214 (void)read_fdreg(FDC_CS);
1215 write_fdreg(FDC_CS, RESTORE);
1216 break;
1217
1218 case FLP_MON :
1219 /*
1220 * Turn motor off.
1221 */
1222 if(selected) {
1223 int tmp;
1224
1225 st_dmagrab((dma_farg)fdcint, (dma_farg)fdmoff,
1226 sc, &tmp, 0);
1227 }
1228 else fd_state = FLP_IDLE;
1229 break;
1230 }
1231 splx(sps);
1232 }
1233
1234 /*
1235 * min byte count to whats left of the track in question
1236 */
1237 static void
1238 fdminphys(bp)
1239 struct buf *bp;
1240 {
1241 struct fd_softc *sc;
1242 int sec, toff, tsz;
1243
1244 if((sc = getsoftc(fd_cd, DISKUNIT(bp->b_dev))) == NULL)
1245 panic("fdminphys: couldn't get softc");
1246
1247 sec = bp->b_blkno % (sc->nsectors * sc->nheads);
1248 toff = sec * SECTOR_SIZE;
1249 tsz = sc->nsectors * sc->nheads * SECTOR_SIZE;
1250
1251 #ifdef FLP_DEBUG
1252 printf("fdminphys: before %ld", bp->b_bcount);
1253 #endif
1254
1255 bp->b_bcount = min(bp->b_bcount, tsz - toff);
1256
1257 #ifdef FLP_DEBUG
1258 printf(" after %ld\n", bp->b_bcount);
1259 #endif
1260
1261 minphys(bp);
1262 }
1263
1264 /*
1265 * Called from fdmotoroff to turn the motor actually off....
1266 * This can't be done in fdmotoroff itself, because exclusive access to the
1267 * DMA controller is needed to read the FDC-status register. The function
1268 * 'fdmoff()' always runs as the result of a 'dmagrab()'.
1269 * We need to test the status-register because we want to be sure that the
1270 * drive motor is really off before deselecting the drive. The FDC only
1271 * turns off the drive motor after having seen 10 index-pulses. You only
1272 * get index-pulses when a drive is selected....This means that if the
1273 * drive is deselected when the motor is still spinning, it will continue
1274 * to spin _even_ when you insert a floppy later on...
1275 */
1276 static void
1277 fdmoff(fdsoftc)
1278 struct fd_softc *fdsoftc;
1279 {
1280 int tmp;
1281
1282 if ((fd_state == FLP_MON) && selected) {
1283 tmp = read_fdreg(FDC_CS);
1284 if (!(tmp & MOTORON)) {
1285 fddeselect();
1286 fd_state = FLP_IDLE;
1287 }
1288 else
1289 callout_reset(&fdsoftc->sc_motor_ch, 10*FLP_MONDELAY,
1290 (FPV)fdmotoroff, fdsoftc);
1291 }
1292 st_dmafree(fdsoftc, &tmp);
1293 }
1294
1295 /*
1296 * Used to find out wich drives are actually connected. We do this by issuing
1297 * is 'RESTORE' command and check if the 'track-0' bit is set. This also works
1298 * if the drive is present but no floppy is inserted.
1299 */
1300 static void
1301 fdtestdrv(fdsoftc)
1302 struct fd_softc *fdsoftc;
1303 {
1304 int status;
1305
1306 /*
1307 * Select the right unit and head.
1308 */
1309 fdselect(fdsoftc->unit, 0, FLP_DD);
1310
1311 write_fdreg(FDC_CS, RESTORE|HBIT);
1312
1313 /*
1314 * Wait for about 2 seconds.
1315 */
1316 delay(2000000);
1317
1318 status = read_fdreg(FDC_CS);
1319 if(status & (RNF|BUSY)) {
1320 write_fdreg(FDC_CS, IRUPT); /* reset controller */
1321 delay(40);
1322 }
1323
1324 if(!(status & LD_T00))
1325 fdsoftc->flags |= FLPF_NOTRESP;
1326
1327 fddeselect();
1328 }
1329
1330 static void
1331 fdgetdefaultlabel(sc, lp, part)
1332 struct fd_softc *sc;
1333 struct disklabel *lp;
1334 int part;
1335 {
1336
1337 bzero(lp, sizeof(struct disklabel));
1338
1339 lp->d_secsize = SECTOR_SIZE;
1340 lp->d_ntracks = sc->nheads;
1341 lp->d_nsectors = sc->nsectors;
1342 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1343 lp->d_ncylinders = sc->nblocks / lp->d_secpercyl;
1344 lp->d_secperunit = sc->nblocks;
1345
1346 lp->d_type = DTYPE_FLOPPY;
1347 lp->d_rpm = 300; /* good guess I suppose. */
1348 lp->d_interleave = 1; /* FIXME: is this OK? */
1349 lp->d_bbsize = 0;
1350 lp->d_sbsize = 0;
1351 lp->d_npartitions = part + 1;
1352 lp->d_trkseek = STEP_DELAY;
1353 lp->d_magic = DISKMAGIC;
1354 lp->d_magic2 = DISKMAGIC;
1355 lp->d_checksum = dkcksum(lp);
1356 lp->d_partitions[part].p_size = lp->d_secperunit;
1357 lp->d_partitions[part].p_fstype = FS_UNUSED;
1358 lp->d_partitions[part].p_fsize = 1024;
1359 lp->d_partitions[part].p_frag = 8;
1360 }
1361
1362 /*
1363 * Build disk label. For now we only create a label from what we know
1364 * from 'sc'.
1365 */
1366 static int
1367 fdgetdisklabel(sc, devvp)
1368 struct fd_softc *sc;
1369 struct vnode *devvp;
1370 {
1371 struct disklabel *lp;
1372 int part;
1373
1374 /*
1375 * If we already got one, get out.
1376 */
1377 if(sc->flags & FLPF_HAVELAB)
1378 return(0);
1379
1380 #ifdef FLP_DEBUG
1381 printf("fdgetdisklabel()\n");
1382 #endif
1383
1384 part = RAW_PART;
1385 lp = sc->dkdev.dk_label;
1386 fdgetdefaultlabel(sc, lp, part);
1387 sc->flags |= FLPF_HAVELAB;
1388
1389 return(0);
1390 }
1391