fd.c revision 1.38 1 /* $NetBSD: fd.c,v 1.38 2002/08/10 21:49:14 hannken 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 bufq_state 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_alloc(&sc->bufq, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
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 BUFQ_PUT(&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_PEEK(&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_GET(&sc->bufq);
757 if (bp == NULL)
758 panic("fddone");
759 splx(sps);
760
761 #ifdef FLP_DEBUG
762 printf("fddone: unit: %d, buf: %p, resid: %d\n",sc->unit,bp,
763 sc->io_bytes);
764 #endif
765 bp->b_resid = sc->io_bytes;
766
767 disk_unbusy(&sc->dkdev, (bp->b_bcount - bp->b_resid));
768
769 biodone(bp);
770 }
771 fd_state = FLP_MON;
772
773 if(lock_stat)
774 return; /* XXX Is this possible? */
775
776 /*
777 * Find a new transaction on round-robin basis.
778 */
779 for(i = sc->unit + 1; ;i++) {
780 if(i >= fd_cd.cd_ndevs)
781 i = 0;
782 if((sc1 = fd_cd.cd_devs[i]) == NULL)
783 continue;
784 if (BUFQ_PEEK(&sc1->bufq) != NULL)
785 break;
786 if(i == sc->unit) {
787 callout_reset(&sc->sc_motor_ch, FLP_MONDELAY,
788 (FPV)fdmotoroff, sc);
789 #ifdef FLP_DEBUG
790 printf("fddone: Nothing to do\n");
791 #endif
792 return; /* No work */
793 }
794 }
795 fd_state = FLP_IDLE;
796 #ifdef FLP_DEBUG
797 printf("fddone: Staring job on unit %d\n", sc1->unit);
798 #endif
799 st_dmagrab((dma_farg)fdcint, (dma_farg)fdstart, sc1, &lock_stat, 0);
800 }
801
802 static int
803 fdselect(drive, head, dense)
804 int drive, head, dense;
805 {
806 int i, spinning;
807 #ifdef FLP_DEBUG
808 printf("fdselect: drive=%d, head=%d, dense=%d\n", drive, head, dense);
809 #endif
810 i = ((drive == 1) ? PA_FLOP1 : PA_FLOP0) | head;
811 spinning = motoron;
812 motoron = 1;
813
814 switch(dense) {
815 case FLP_DD:
816 DMA->dma_drvmode = 0;
817 break;
818 case FLP_HD:
819 DMA->dma_drvmode = (FDC_HDSET|FDC_HDSIG);
820 break;
821 default:
822 panic("fdselect: unknown density code\n");
823 }
824 if(i != selected) {
825 selected = i;
826 ym2149_fd_select((i ^ PA_FDSEL));
827 }
828 return(spinning);
829 }
830
831 static void
832 fddeselect()
833 {
834 ym2149_fd_select(PA_FDSEL);
835 motoron = selected = 0;
836 DMA->dma_drvmode = 0;
837 }
838
839 /****************************************************************************
840 * The following functions assume to be running as a result of a *
841 * disk-interrupt (e.q. spl = splbio). *
842 * They form the finit-state machine, the actual driver. *
843 * *
844 * fdstart()/ --> fd_xfer() -> activate hardware *
845 * fdopen() ^ *
846 * | *
847 * +-- not ready -<------------+ *
848 * | *
849 * fdmotoroff()/ --> fdcint() -> fd_xfer_ok() ---+ *
850 * h/w interrupt | *
851 * \|/ *
852 * finished ---> fdone() *
853 * *
854 ****************************************************************************/
855 static void
856 fd_xfer(sc)
857 struct fd_softc *sc;
858 {
859 register int head;
860 register int track, sector, hbit;
861 u_long phys_addr;
862
863 head = track = 0;
864 switch(fd_state) {
865 case FLP_XFER:
866 /*
867 * Calculate head/track values
868 */
869 track = sc->sector / sc->nsectors;
870 head = track % sc->nheads;
871 track = track / sc->nheads;
872 #ifdef FLP_DEBUG
873 printf("fd_xfer: sector:%d,head:%d,track:%d\n", sc->sector,head,
874 track);
875 #endif
876 break;
877
878 case FLP_STAT:
879 /*
880 * FLP_STAT only wants to recalibrate
881 */
882 sc->curtrk = INV_TRK;
883 break;
884 default:
885 panic("fd_xfer: wrong state (0x%x)", fd_state);
886 }
887
888 /*
889 * Select the drive.
890 */
891 hbit = fdselect(sc->unit, head, sc->density) ? HBIT : 0;
892
893 if(sc->curtrk == INV_TRK) {
894 /*
895 * Recalibrate, since we lost track of head positioning.
896 * The floppy disk controller has no way of determining its
897 * absolute arm position (track). Instead, it steps the
898 * arm a track at a time and keeps track of where it
899 * thinks it is (in software). However, after a SEEK, the
900 * hardware reads information from the diskette telling
901 * where the arm actually is. If the arm is in the wrong place,
902 * a recalibration is done, which forces the arm to track 0.
903 * This way the controller can get back into sync with reality.
904 */
905 fd_cmd = RESTORE;
906 write_fdreg(FDC_CS, RESTORE|VBIT|hbit);
907 callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
908 (FPV)fdmotoroff, sc);
909
910 #ifdef FLP_DEBUG
911 printf("fd_xfer:Recalibrating drive %d\n", sc->unit);
912 #endif
913 return;
914 }
915
916 write_fdreg(FDC_TR, sc->curtrk);
917
918 /*
919 * Issue a SEEK command on the indicated drive unless the arm is
920 * already positioned on the correct track.
921 */
922 if(track != sc->curtrk) {
923 sc->curtrk = track; /* be optimistic */
924 write_fdreg(FDC_DR, track);
925 write_fdreg(FDC_CS, SEEK|RATE6|VBIT|hbit);
926 callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
927 (FPV)fdmotoroff, sc);
928 fd_cmd = SEEK;
929 #ifdef FLP_DEBUG
930 printf("fd_xfer:Seek to track %d on drive %d\n",track,sc->unit);
931 #endif
932 return;
933 }
934
935 /*
936 * The drive is now on the proper track. Read or write 1 block.
937 */
938 sector = sc->sector % sc->nsectors;
939 sector++; /* start numbering at 1 */
940
941 write_fdreg(FDC_SR, sector);
942
943 phys_addr = (u_long)kvtop(sc->io_data);
944 if(phys_addr >= FDC_MAX_DMA_AD) {
945 /*
946 * We _must_ bounce this address
947 */
948 phys_addr = (u_long)kvtop(sc->bounceb);
949 if(sc->io_dir == B_WRITE)
950 bcopy(sc->io_data, sc->bounceb, SECTOR_SIZE);
951 sc->flags |= FLPF_BOUNCE;
952 }
953 st_dmaaddr_set((caddr_t)phys_addr); /* DMA address setup */
954
955 #ifdef FLP_DEBUG
956 printf("fd_xfer:Start io (io_addr:%lx)\n", (u_long)kvtop(sc->io_data));
957 #endif
958
959 if(sc->io_dir == B_READ) {
960 /* Issue the command */
961 st_dmacomm(DMA_FDC | DMA_SCREG, 1);
962 write_fdreg(FDC_CS, F_READ|hbit);
963 fd_cmd = F_READ;
964 }
965 else {
966 /* Issue the command */
967 st_dmacomm(DMA_WRBIT | DMA_FDC | DMA_SCREG, 1);
968 write_fdreg(DMA_WRBIT | FDC_CS, F_WRITE|hbit|EBIT|PBIT);
969 fd_cmd = F_WRITE;
970 }
971 callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY, (FPV)fdmotoroff, sc);
972 }
973
974 /* return values of fd_xfer_ok(): */
975 #define X_OK 0
976 #define X_AGAIN 1
977 #define X_ERROR 2
978 #define X_FAIL 3
979
980 /*
981 * Hardware interrupt function.
982 */
983 static void
984 fdcint(sc)
985 struct fd_softc *sc;
986 {
987 struct buf *bp;
988
989 #ifdef FLP_DEBUG
990 printf("fdcint: unit = %d\n", sc->unit);
991 #endif
992
993 /*
994 * Cancel timeout (we made it, didn't we)
995 */
996 callout_stop(&sc->sc_motor_ch);
997
998 switch(fd_xfer_ok(sc)) {
999 case X_ERROR :
1000 if(++(sc->errcnt) < MAX_ERRORS) {
1001 /*
1002 * Command failed but still retries left.
1003 */
1004 break;
1005 }
1006 /* FALL THROUGH */
1007 case X_FAIL :
1008 /*
1009 * Non recoverable error. Fall back to motor-on
1010 * idle-state.
1011 */
1012 if(fd_error != NULL) {
1013 printf("Floppy error: %s\n", fd_error);
1014 fd_error = NULL;
1015 }
1016
1017 if(fd_state == FLP_STAT) {
1018 sc->flags |= FLPF_EMPTY;
1019 sc->flags &= ~FLPF_GETSTAT;
1020 wakeup((caddr_t)sc);
1021 fddone(sc);
1022 return;
1023 }
1024
1025 bp = BUFQ_PEEK(&sc->bufq);
1026
1027 bp->b_error = EIO;
1028 bp->b_flags |= B_ERROR;
1029 fd_state = FLP_MON;
1030
1031 break;
1032 case X_AGAIN:
1033 /*
1034 * Start next part of state machine.
1035 */
1036 break;
1037 case X_OK:
1038 /*
1039 * Command ok and finished. Reset error-counter.
1040 * If there are no more bytes to transfer fall back
1041 * to motor-on idle state.
1042 */
1043 sc->errcnt = 0;
1044
1045 if(fd_state == FLP_STAT) {
1046 sc->flags &= ~FLPF_GETSTAT;
1047 wakeup((caddr_t)sc);
1048 fddone(sc);
1049 return;
1050 }
1051
1052 if((sc->flags & FLPF_BOUNCE) && (sc->io_dir == B_READ))
1053 bcopy(sc->bounceb, sc->io_data, SECTOR_SIZE);
1054 sc->flags &= ~FLPF_BOUNCE;
1055
1056 sc->sector++;
1057 sc->io_data += SECTOR_SIZE;
1058 sc->io_bytes -= SECTOR_SIZE;
1059 if(sc->io_bytes <= 0)
1060 fd_state = FLP_MON;
1061 }
1062 if(fd_state == FLP_MON)
1063 fddone(sc);
1064 else fd_xfer(sc);
1065 }
1066
1067 /*
1068 * Determine status of last command. Should only be called through
1069 * 'fdcint()'.
1070 * Returns:
1071 * X_ERROR : Error on command; might succeed next time.
1072 * X_FAIL : Error on command; will never succeed.
1073 * X_AGAIN : Part of a command succeeded, call 'fd_xfer()' to complete.
1074 * X_OK : Command succeeded and is complete.
1075 *
1076 * This function only affects sc->curtrk.
1077 */
1078 static int
1079 fd_xfer_ok(sc)
1080 register struct fd_softc *sc;
1081 {
1082 register int status;
1083
1084 #ifdef FLP_DEBUG
1085 printf("fd_xfer_ok: cmd: 0x%x, state: 0x%x\n", fd_cmd, fd_state);
1086 #endif
1087 switch(fd_cmd) {
1088 case IRUPT:
1089 /*
1090 * Timeout. Force a recalibrate before we try again.
1091 */
1092 status = read_fdreg(FDC_CS);
1093
1094 fd_error = "Timeout";
1095 sc->curtrk = INV_TRK;
1096 return(X_ERROR);
1097 case F_READ:
1098 /*
1099 * Test for DMA error
1100 */
1101 status = read_dmastat();
1102 if(!(status & DMAOK)) {
1103 fd_error = "Dma error";
1104 return(X_ERROR);
1105 }
1106 /*
1107 * Get controller status and check for errors.
1108 */
1109 status = read_fdreg(FDC_CS);
1110 if(status & (RNF | CRCERR | LD_T00)) {
1111 fd_error = "Read error";
1112 if(status & RNF)
1113 sc->curtrk = INV_TRK;
1114 return(X_ERROR);
1115 }
1116 break;
1117 case F_WRITE:
1118 /*
1119 * Test for DMA error
1120 */
1121 status = read_dmastat();
1122 if(!(status & DMAOK)) {
1123 fd_error = "Dma error";
1124 return(X_ERROR);
1125 }
1126 /*
1127 * Get controller status and check for errors.
1128 */
1129 status = read_fdreg(FDC_CS);
1130 if(status & WRI_PRO) {
1131 fd_error = "Write protected";
1132 return(X_FAIL);
1133 }
1134 if(status & (RNF | CRCERR | LD_T00)) {
1135 fd_error = "Write error";
1136 sc->curtrk = INV_TRK;
1137 return(X_ERROR);
1138 }
1139 break;
1140 case SEEK:
1141 status = read_fdreg(FDC_CS);
1142 if(status & (RNF | CRCERR)) {
1143 fd_error = "Seek error";
1144 sc->curtrk = INV_TRK;
1145 return(X_ERROR);
1146 }
1147 return(X_AGAIN);
1148 case RESTORE:
1149 /*
1150 * Determine if the recalibration succeeded.
1151 */
1152 status = read_fdreg(FDC_CS);
1153 if(status & RNF) {
1154 fd_error = "Recalibrate error";
1155 /* reset controller */
1156 write_fdreg(FDC_CS, IRUPT);
1157 sc->curtrk = INV_TRK;
1158 return(X_ERROR);
1159 }
1160 sc->curtrk = 0;
1161 if(fd_state == FLP_STAT) {
1162 if(status & WRI_PRO)
1163 sc->flags |= FLPF_WRTPROT;
1164 break;
1165 }
1166 return(X_AGAIN);
1167 default:
1168 fd_error = "Driver error: fd_xfer_ok : Unknown state";
1169 return(X_FAIL);
1170 }
1171 return(X_OK);
1172 }
1173
1174 /*
1175 * All timeouts will call this function.
1176 */
1177 static void
1178 fdmotoroff(sc)
1179 struct fd_softc *sc;
1180 {
1181 int sps;
1182
1183 /*
1184 * Get at harware interrupt level
1185 */
1186 sps = splbio();
1187
1188 #if FLP_DEBUG
1189 printf("fdmotoroff, state = 0x%x\n", fd_state);
1190 #endif
1191
1192 switch(fd_state) {
1193 case FLP_STAT :
1194 case FLP_XFER :
1195 /*
1196 * Timeout during a transfer; cancel transaction
1197 * set command to 'IRUPT'.
1198 * A drive-interrupt is simulated to trigger the state
1199 * machine.
1200 */
1201 /*
1202 * Cancel current transaction
1203 */
1204 fd_cmd = IRUPT;
1205 write_fdreg(FDC_CS, IRUPT);
1206 delay(20);
1207 (void)read_fdreg(FDC_CS);
1208 write_fdreg(FDC_CS, RESTORE);
1209 break;
1210
1211 case FLP_MON :
1212 /*
1213 * Turn motor off.
1214 */
1215 if(selected) {
1216 int tmp;
1217
1218 st_dmagrab((dma_farg)fdcint, (dma_farg)fdmoff,
1219 sc, &tmp, 0);
1220 }
1221 else fd_state = FLP_IDLE;
1222 break;
1223 }
1224 splx(sps);
1225 }
1226
1227 /*
1228 * min byte count to whats left of the track in question
1229 */
1230 static void
1231 fdminphys(bp)
1232 struct buf *bp;
1233 {
1234 struct fd_softc *sc;
1235 int sec, toff, tsz;
1236
1237 if((sc = getsoftc(fd_cd, DISKUNIT(bp->b_dev))) == NULL)
1238 panic("fdminphys: couldn't get softc");
1239
1240 sec = bp->b_blkno % (sc->nsectors * sc->nheads);
1241 toff = sec * SECTOR_SIZE;
1242 tsz = sc->nsectors * sc->nheads * SECTOR_SIZE;
1243
1244 #ifdef FLP_DEBUG
1245 printf("fdminphys: before %ld", bp->b_bcount);
1246 #endif
1247
1248 bp->b_bcount = min(bp->b_bcount, tsz - toff);
1249
1250 #ifdef FLP_DEBUG
1251 printf(" after %ld\n", bp->b_bcount);
1252 #endif
1253
1254 minphys(bp);
1255 }
1256
1257 /*
1258 * Called from fdmotoroff to turn the motor actually off....
1259 * This can't be done in fdmotoroff itself, because exclusive access to the
1260 * DMA controller is needed to read the FDC-status register. The function
1261 * 'fdmoff()' always runs as the result of a 'dmagrab()'.
1262 * We need to test the status-register because we want to be sure that the
1263 * drive motor is really off before deselecting the drive. The FDC only
1264 * turns off the drive motor after having seen 10 index-pulses. You only
1265 * get index-pulses when a drive is selected....This means that if the
1266 * drive is deselected when the motor is still spinning, it will continue
1267 * to spin _even_ when you insert a floppy later on...
1268 */
1269 static void
1270 fdmoff(fdsoftc)
1271 struct fd_softc *fdsoftc;
1272 {
1273 int tmp;
1274
1275 if ((fd_state == FLP_MON) && selected) {
1276 tmp = read_fdreg(FDC_CS);
1277 if (!(tmp & MOTORON)) {
1278 fddeselect();
1279 fd_state = FLP_IDLE;
1280 }
1281 else
1282 callout_reset(&fdsoftc->sc_motor_ch, 10*FLP_MONDELAY,
1283 (FPV)fdmotoroff, fdsoftc);
1284 }
1285 st_dmafree(fdsoftc, &tmp);
1286 }
1287
1288 /*
1289 * Used to find out wich drives are actually connected. We do this by issuing
1290 * is 'RESTORE' command and check if the 'track-0' bit is set. This also works
1291 * if the drive is present but no floppy is inserted.
1292 */
1293 static void
1294 fdtestdrv(fdsoftc)
1295 struct fd_softc *fdsoftc;
1296 {
1297 int status;
1298
1299 /*
1300 * Select the right unit and head.
1301 */
1302 fdselect(fdsoftc->unit, 0, FLP_DD);
1303
1304 write_fdreg(FDC_CS, RESTORE|HBIT);
1305
1306 /*
1307 * Wait for about 2 seconds.
1308 */
1309 delay(2000000);
1310
1311 status = read_fdreg(FDC_CS);
1312 if(status & (RNF|BUSY)) {
1313 write_fdreg(FDC_CS, IRUPT); /* reset controller */
1314 delay(40);
1315 }
1316
1317 if(!(status & LD_T00))
1318 fdsoftc->flags |= FLPF_NOTRESP;
1319
1320 fddeselect();
1321 }
1322
1323 static void
1324 fdgetdefaultlabel(sc, lp, part)
1325 struct fd_softc *sc;
1326 struct disklabel *lp;
1327 int part;
1328 {
1329
1330 bzero(lp, sizeof(struct disklabel));
1331
1332 lp->d_secsize = SECTOR_SIZE;
1333 lp->d_ntracks = sc->nheads;
1334 lp->d_nsectors = sc->nsectors;
1335 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1336 lp->d_ncylinders = sc->nblocks / lp->d_secpercyl;
1337 lp->d_secperunit = sc->nblocks;
1338
1339 lp->d_type = DTYPE_FLOPPY;
1340 lp->d_rpm = 300; /* good guess I suppose. */
1341 lp->d_interleave = 1; /* FIXME: is this OK? */
1342 lp->d_bbsize = 0;
1343 lp->d_sbsize = 0;
1344 lp->d_npartitions = part + 1;
1345 lp->d_trkseek = STEP_DELAY;
1346 lp->d_magic = DISKMAGIC;
1347 lp->d_magic2 = DISKMAGIC;
1348 lp->d_checksum = dkcksum(lp);
1349 lp->d_partitions[part].p_size = lp->d_secperunit;
1350 lp->d_partitions[part].p_fstype = FS_UNUSED;
1351 lp->d_partitions[part].p_fsize = 1024;
1352 lp->d_partitions[part].p_frag = 8;
1353 }
1354
1355 /*
1356 * Build disk label. For now we only create a label from what we know
1357 * from 'sc'.
1358 */
1359 static int
1360 fdgetdisklabel(sc, dev)
1361 struct fd_softc *sc;
1362 dev_t dev;
1363 {
1364 struct disklabel *lp;
1365 int part;
1366
1367 /*
1368 * If we already got one, get out.
1369 */
1370 if(sc->flags & FLPF_HAVELAB)
1371 return(0);
1372
1373 #ifdef FLP_DEBUG
1374 printf("fdgetdisklabel()\n");
1375 #endif
1376
1377 part = RAW_PART;
1378 lp = sc->dkdev.dk_label;
1379 fdgetdefaultlabel(sc, lp, part);
1380 sc->flags |= FLPF_HAVELAB;
1381
1382 return(0);
1383 }
1384