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