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