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