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