1 /* 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Don Ahn. 7 * 8 * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu) 9 * aided by the Linux floppy driver modifications from David Bateman 10 * (dbateman@eng.uts.edu.au). 11 * 12 * Copyright (c) 1993, 1994 by 13 * jc@irbs.UUCP (John Capo) 14 * vak@zebub.msk.su (Serge Vakulenko) 15 * ache@astral.msk.su (Andrew A. Chernov) 16 * 17 * Copyright (c) 1993, 1994, 1995 by 18 * joerg_wunsch@uriah.sax.de (Joerg Wunsch) 19 * dufault@hda.com (Peter Dufault) 20 * 21 * Redistribution and use in source and binary forms, with or without 22 * modification, are permitted provided that the following conditions 23 * are met: 24 * 1. Redistributions of source code must retain the above copyright 25 * notice, this list of conditions and the following disclaimer. 26 * 2. Redistributions in binary form must reproduce the above copyright 27 * notice, this list of conditions and the following disclaimer in the 28 * documentation and/or other materials provided with the distribution. 29 * 3. All advertising materials mentioning features or use of this software 30 * must display the following acknowledgement: 31 * This product includes software developed by the University of 32 * California, Berkeley and its contributors. 33 * 4. Neither the name of the University nor the names of its contributors 34 * may be used to endorse or promote products derived from this software 35 * without specific prior written permission. 36 * 37 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 40 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47 * SUCH DAMAGE. 48 * 49 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91 50 * $Id: fd.c,v 1.151 1999/07/29 11:27:33 joerg Exp $ 51 * 52 */ 53 54 #include "fd.h" 55 #include "opt_devfs.h" 56 #include "opt_fdc.h" 57 58 #if NFDC > 0 59 60 #include <sys/param.h> 61 #include <sys/systm.h> 62 #include <sys/kernel.h> 63 #include <sys/buf.h> 64 #include <sys/bus.h> 65 #include <sys/conf.h> 66 #include <sys/disklabel.h> 67 #include <sys/devicestat.h> 68 #include <sys/fcntl.h> 69 #include <sys/malloc.h> 70 #include <sys/module.h> 71 #include <sys/proc.h> 72 #include <sys/syslog.h> 73 74 #include <sys/bus.h> 75 #include <machine/bus.h> 76 #include <sys/rman.h> 77 78 #include <machine/clock.h> 79 #include <machine/ioctl_fd.h> 80 #include <machine/resource.h> 81 #include <machine/stdarg.h> 82 83 #ifdef DEVFS 84 #include <sys/devfsext.h> 85 #endif /* DEVFS */ 86 87 #include <isa/isavar.h> 88 #include <isa/isareg.h> 89 #include <isa/fdreg.h> 90 #include <isa/fdc.h> 91 #include <isa/rtc.h> 92 93 #ifdef FDC_YE 94 #undef FDC_YE 95 #warning "fix FDC_YE! - newbus casualty" 96 #endif 97 98 /* misuse a flag to identify format operation */ 99 #define B_FORMAT B_XXX 100 101 /* configuration flags */ 102 #define FDC_PRETEND_D0 (1 << 0) /* pretend drive 0 to be there */ 103 #ifdef FDC_YE 104 #define FDC_IS_PCMCIA (1 << 1) /* if successful probe, then it's 105 a PCMCIA device */ 106 #endif 107 108 /* internally used only, not really from CMOS: */ 109 #define RTCFDT_144M_PRETENDED 0x1000 110 111 /* error returns for fd_cmd() */ 112 #define FD_FAILED -1 113 #define FD_NOT_VALID -2 114 #define FDC_ERRMAX 100 /* do not log more */ 115 116 #define NUMTYPES 14 117 #define NUMDENS (NUMTYPES - 6) 118 119 /* These defines (-1) must match index for fd_types */ 120 #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */ 121 #define NO_TYPE 0 /* must match NO_TYPE in ft.c */ 122 #define FD_1720 1 123 #define FD_1480 2 124 #define FD_1440 3 125 #define FD_1200 4 126 #define FD_820 5 127 #define FD_800 6 128 #define FD_720 7 129 #define FD_360 8 130 131 #define FD_1480in5_25 9 132 #define FD_1440in5_25 10 133 #define FD_820in5_25 11 134 #define FD_800in5_25 12 135 #define FD_720in5_25 13 136 #define FD_360in5_25 14 137 138 139 static struct fd_type fd_types[NUMTYPES] = 140 { 141 { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */ 142 { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */ 143 { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */ 144 { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */ 145 { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */ 146 { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */ 147 { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */ 148 { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */ 149 150 { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */ 151 { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */ 152 { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */ 153 { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */ 154 { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */ 155 { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */ 156 }; 157 158 #define DRVS_PER_CTLR 2 /* 2 floppies */ 159 160 /***********************************************************************\ 161 * Per controller structure. * 162 \***********************************************************************/ 163 static devclass_t fdc_devclass; 164 165 /***********************************************************************\ 166 * Per drive structure. * 167 * N per controller (DRVS_PER_CTLR) * 168 \***********************************************************************/ 169 struct fd_data { 170 struct fdc_data *fdc; /* pointer to controller structure */ 171 int fdsu; /* this units number on this controller */ 172 int type; /* Drive type (FD_1440...) */ 173 struct fd_type *ft; /* pointer to the type descriptor */ 174 int flags; 175 #define FD_OPEN 0x01 /* it's open */ 176 #define FD_ACTIVE 0x02 /* it's active */ 177 #define FD_MOTOR 0x04 /* motor should be on */ 178 #define FD_MOTOR_WAIT 0x08 /* motor coming up */ 179 int skip; 180 int hddrv; 181 #define FD_NO_TRACK -2 182 int track; /* where we think the head is */ 183 int options; /* user configurable options, see ioctl_fd.h */ 184 struct callout_handle toffhandle; 185 struct callout_handle tohandle; 186 struct devstat device_stats; 187 #ifdef DEVFS 188 void *bdevs[1 + NUMDENS + MAXPARTITIONS]; 189 void *cdevs[1 + NUMDENS + MAXPARTITIONS]; 190 #endif 191 device_t dev; 192 fdu_t fdu; 193 }; 194 static devclass_t fd_devclass; 195 196 /***********************************************************************\ 197 * Throughout this file the following conventions will be used: * 198 * fd is a pointer to the fd_data struct for the drive in question * 199 * fdc is a pointer to the fdc_data struct for the controller * 200 * fdu is the floppy drive unit number * 201 * fdcu is the floppy controller unit number * 202 * fdsu is the floppy drive unit number on that controller. (sub-unit) * 203 \***********************************************************************/ 204 205 #ifdef FDC_YE 206 #include "card.h" 207 static int yeattach(struct isa_device *); 208 #endif 209 210 /* needed for ft driver, thus exported */ 211 int in_fdc(struct fdc_data *); 212 int out_fdc(struct fdc_data *, int); 213 214 /* internal functions */ 215 static void fdc_add_device(device_t, const char *, int); 216 static void fdc_intr(void *); 217 static void set_motor(struct fdc_data *, int, int); 218 # define TURNON 1 219 # define TURNOFF 0 220 static timeout_t fd_turnoff; 221 static timeout_t fd_motor_on; 222 static void fd_turnon(struct fd_data *); 223 static void fdc_reset(fdc_p); 224 static int fd_in(struct fdc_data *, int *); 225 static void fdstart(struct fdc_data *); 226 static timeout_t fd_iotimeout; 227 static timeout_t fd_pseudointr; 228 static int fdstate(struct fdc_data *); 229 static int retrier(struct fdc_data *); 230 static int fdformat(dev_t, struct fd_formb *, struct proc *); 231 232 static int enable_fifo(fdc_p fdc); 233 234 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */ 235 236 237 #define DEVIDLE 0 238 #define FINDWORK 1 239 #define DOSEEK 2 240 #define SEEKCOMPLETE 3 241 #define IOCOMPLETE 4 242 #define RECALCOMPLETE 5 243 #define STARTRECAL 6 244 #define RESETCTLR 7 245 #define SEEKWAIT 8 246 #define RECALWAIT 9 247 #define MOTORWAIT 10 248 #define IOTIMEDOUT 11 249 #define RESETCOMPLETE 12 250 #ifdef FDC_YE 251 #define PIOREAD 13 252 #endif 253 254 #ifdef FDC_DEBUG 255 static char const * const fdstates[] = 256 { 257 "DEVIDLE", 258 "FINDWORK", 259 "DOSEEK", 260 "SEEKCOMPLETE", 261 "IOCOMPLETE", 262 "RECALCOMPLETE", 263 "STARTRECAL", 264 "RESETCTLR", 265 "SEEKWAIT", 266 "RECALWAIT", 267 "MOTORWAIT", 268 "IOTIMEDOUT", 269 "RESETCOMPLETE", 270 #ifdef FDC_YE 271 "PIOREAD", 272 #endif 273 }; 274 275 /* CAUTION: fd_debug causes huge amounts of logging output */ 276 static int volatile fd_debug = 0; 277 #define TRACE0(arg) if(fd_debug) printf(arg) 278 #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2) 279 #else /* FDC_DEBUG */ 280 #define TRACE0(arg) 281 #define TRACE1(arg1, arg2) 282 #endif /* FDC_DEBUG */ 283 284 #ifdef FDC_YE 285 #if NCARD > 0 286 #include <sys/select.h> 287 #include <sys/module.h> 288 #include <pccard/cardinfo.h> 289 #include <pccard/driver.h> 290 #include <pccard/slot.h> 291 292 /* 293 * PC-Card (PCMCIA) specific code. 294 */ 295 static int yeinit(struct pccard_devinfo *); /* init device */ 296 static void yeunload(struct pccard_devinfo *); /* Disable driver */ 297 static int yeintr(struct pccard_devinfo *); /* Interrupt handler */ 298 299 PCCARD_MODULE(fdc, yeinit, yeunload, yeintr, 0, bio_imask); 300 301 /* 302 * this is the secret PIO data port (offset from base) 303 */ 304 #define FDC_YE_DATAPORT 6 305 306 /* 307 * Initialize the device - called from Slot manager. 308 */ 309 static int yeinit(struct pccard_devinfo *devi) 310 { 311 fdc_p fdc = &fdc_data[devi->isahd.id_unit]; 312 313 /* validate unit number. */ 314 if (devi->isahd.id_unit >= NFDC) 315 return(ENODEV); 316 fdc->baseport = devi->isahd.id_iobase; 317 /* 318 * reset controller 319 */ 320 outb(fdc->baseport+FDOUT, 0); 321 DELAY(100); 322 outb(fdc->baseport+FDOUT, FDO_FRST); 323 324 /* 325 * wire into system 326 */ 327 if (yeattach(&devi->isahd) == 0) 328 return(ENXIO); 329 330 return(0); 331 } 332 333 /* 334 * yeunload - unload the driver and clear the table. 335 * XXX TODO: 336 * This is usually called when the card is ejected, but 337 * can be caused by a modunload of a controller driver. 338 * The idea is to reset the driver's view of the device 339 * and ensure that any driver entry points such as 340 * read and write do not hang. 341 */ 342 static void yeunload(struct pccard_devinfo *devi) 343 { 344 if (fd_data[devi->isahd.id_unit].type == NO_TYPE) 345 return; 346 347 /* 348 * this prevents Fdopen() and fdstrategy() from attempting 349 * to access unloaded controller 350 */ 351 fd_data[devi->isahd.id_unit].type = NO_TYPE; 352 353 printf("fdc%d: unload\n", devi->isahd.id_unit); 354 } 355 356 /* 357 * yeintr - Shared interrupt called from 358 * front end of PC-Card handler. 359 */ 360 static int yeintr(struct pccard_devinfo *devi) 361 { 362 fdintr((fdcu_t)devi->isahd.id_unit); 363 return(1); 364 } 365 #endif /* NCARD > 0 */ 366 #endif /* FDC_YE */ 367 368 static d_open_t Fdopen; /* NOTE, not fdopen */ 369 static d_close_t fdclose; 370 static d_ioctl_t fdioctl; 371 static d_strategy_t fdstrategy; 372 373 #define CDEV_MAJOR 9 374 #define BDEV_MAJOR 2 375 376 static struct cdevsw fd_cdevsw = { 377 /* open */ Fdopen, 378 /* close */ fdclose, 379 /* read */ physread, 380 /* write */ physwrite, 381 /* ioctl */ fdioctl, 382 /* stop */ nostop, 383 /* reset */ noreset, 384 /* devtotty */ nodevtotty, 385 /* poll */ nopoll, 386 /* mmap */ nommap, 387 /* strategy */ fdstrategy, 388 /* name */ "fd", 389 /* parms */ noparms, 390 /* maj */ CDEV_MAJOR, 391 /* dump */ nodump, 392 /* psize */ nopsize, 393 /* flags */ D_DISK, 394 /* maxio */ 0, 395 /* bmaj */ BDEV_MAJOR 396 }; 397 398 static int 399 fdc_err(struct fdc_data *fdc, const char *s) 400 { 401 fdc->fdc_errs++; 402 if (s) { 403 if (fdc->fdc_errs < FDC_ERRMAX) { 404 device_print_prettyname(fdc->fdc_dev); 405 printf("%s", s); 406 } else if (fdc->fdc_errs == FDC_ERRMAX) { 407 device_print_prettyname(fdc->fdc_dev); 408 printf("too many errors, not logging any more\n"); 409 } 410 } 411 412 return FD_FAILED; 413 } 414 415 /* 416 * fd_cmd: Send a command to the chip. Takes a varargs with this structure: 417 * Unit number, 418 * # of output bytes, output bytes as ints ..., 419 * # of input bytes, input bytes as ints ... 420 */ 421 static int 422 fd_cmd(struct fdc_data *fdc, int n_out, ...) 423 { 424 u_char cmd; 425 int n_in; 426 int n; 427 va_list ap; 428 429 va_start(ap, n_out); 430 cmd = (u_char)(va_arg(ap, int)); 431 va_end(ap); 432 va_start(ap, n_out); 433 for (n = 0; n < n_out; n++) 434 { 435 if (out_fdc(fdc, va_arg(ap, int)) < 0) 436 { 437 char msg[50]; 438 snprintf(msg, sizeof(msg), 439 "cmd %x failed at out byte %d of %d\n", 440 cmd, n + 1, n_out); 441 return fdc_err(fdc, msg); 442 } 443 } 444 n_in = va_arg(ap, int); 445 for (n = 0; n < n_in; n++) 446 { 447 int *ptr = va_arg(ap, int *); 448 if (fd_in(fdc, ptr) < 0) 449 { 450 char msg[50]; 451 snprintf(msg, sizeof(msg), 452 "cmd %02x failed at in byte %d of %d\n", 453 cmd, n + 1, n_in); 454 return fdc_err(fdc, msg); 455 } 456 } 457 458 return 0; 459 } 460 461 static int 462 enable_fifo(fdc_p fdc) 463 { 464 int i, j; 465 466 if ((fdc->flags & FDC_HAS_FIFO) == 0) { 467 468 /* 469 * XXX: 470 * Cannot use fd_cmd the normal way here, since 471 * this might be an invalid command. Thus we send the 472 * first byte, and check for an early turn of data directon. 473 */ 474 475 if (out_fdc(fdc, I8207X_CONFIGURE) < 0) 476 return fdc_err(fdc, "Enable FIFO failed\n"); 477 478 /* If command is invalid, return */ 479 j = 100000; 480 while ((i = inb(fdc->baseport + FDSTS) & (NE7_DIO | NE7_RQM)) 481 != NE7_RQM && j-- > 0) 482 if (i == (NE7_DIO | NE7_RQM)) { 483 fdc_reset(fdc); 484 return FD_FAILED; 485 } 486 if (j<0 || 487 fd_cmd(fdc, 3, 488 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) { 489 fdc_reset(fdc); 490 return fdc_err(fdc, "Enable FIFO failed\n"); 491 } 492 fdc->flags |= FDC_HAS_FIFO; 493 return 0; 494 } 495 if (fd_cmd(fdc, 4, 496 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) 497 return fdc_err(fdc, "Re-enable FIFO failed\n"); 498 return 0; 499 } 500 501 static int 502 fd_sense_drive_status(fdc_p fdc, int *st3p) 503 { 504 int st3; 505 506 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) 507 { 508 return fdc_err(fdc, "Sense Drive Status failed\n"); 509 } 510 if (st3p) 511 *st3p = st3; 512 513 return 0; 514 } 515 516 static int 517 fd_sense_int(fdc_p fdc, int *st0p, int *cylp) 518 { 519 int cyl, st0, ret; 520 521 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0); 522 if (ret) { 523 (void)fdc_err(fdc, 524 "sense intr err reading stat reg 0\n"); 525 return ret; 526 } 527 528 if (st0p) 529 *st0p = st0; 530 531 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) { 532 /* 533 * There doesn't seem to have been an interrupt. 534 */ 535 return FD_NOT_VALID; 536 } 537 538 if (fd_in(fdc, &cyl) < 0) { 539 return fdc_err(fdc, "can't get cyl num\n"); 540 } 541 542 if (cylp) 543 *cylp = cyl; 544 545 return 0; 546 } 547 548 549 static int 550 fd_read_status(fdc_p fdc, int fdsu) 551 { 552 int i, ret; 553 554 for (i = 0; i < 7; i++) { 555 /* 556 * XXX types are poorly chosen. Only bytes can by read 557 * from the hardware, but fdc->status[] wants u_ints and 558 * fd_in() gives ints. 559 */ 560 int status; 561 562 ret = fd_in(fdc, &status); 563 fdc->status[i] = status; 564 if (ret != 0) 565 break; 566 } 567 568 if (ret == 0) 569 fdc->flags |= FDC_STAT_VALID; 570 else 571 fdc->flags &= ~FDC_STAT_VALID; 572 573 return ret; 574 } 575 576 /****************************************************************************/ 577 /* autoconfiguration stuff */ 578 /****************************************************************************/ 579 580 static int 581 fdc_probe(device_t dev) 582 { 583 int error, i, ic_type; 584 struct fdc_data *fdc; 585 char myname[8]; /* better be long enough */ 586 587 /* No pnp support */ 588 if (isa_get_vendorid(dev)) 589 return (ENXIO); 590 591 fdc = device_get_softc(dev); 592 bzero(fdc, sizeof *fdc); 593 fdc->fdc_dev = dev; 594 fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0; 595 fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0; 596 597 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT, 598 &fdc->rid_ioport, 0ul, ~0ul, 599 IO_FDCSIZE, RF_ACTIVE); 600 if (fdc->res_ioport == 0) { 601 device_print_prettyname(dev); 602 printf("cannot reserve I/O port range\n"); 603 error = ENXIO; 604 goto out; 605 } 606 fdc->baseport = fdc->res_ioport->r_start; 607 608 fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, 609 &fdc->rid_irq, 0ul, ~0ul, 1, 610 RF_ACTIVE); 611 if (fdc->res_irq == 0) { 612 device_print_prettyname(dev); 613 printf("cannot reserve interrupt line\n"); 614 error = ENXIO; 615 goto out; 616 } 617 fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ, 618 &fdc->rid_drq, 0ul, ~0ul, 1, 619 RF_ACTIVE); 620 if (fdc->res_drq == 0) { 621 device_print_prettyname(dev); 622 printf("cannot reserve DMA request line\n"); 623 error = ENXIO; 624 goto out; 625 } 626 fdc->dmachan = fdc->res_drq->r_start; 627 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq, 628 INTR_TYPE_BIO, fdc_intr, fdc, &fdc->fdc_intr); 629 630 /* First - lets reset the floppy controller */ 631 outb(fdc->baseport + FDOUT, 0); 632 DELAY(100); 633 outb(fdc->baseport + FDOUT, FDO_FRST); 634 635 /* see if it can handle a command */ 636 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 637 NE7_SPEC_2(2, 0), 0)) { 638 error = ENXIO; 639 goto out; 640 } 641 642 if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { 643 ic_type = (u_char)ic_type; 644 switch (ic_type) { 645 case 0x80: 646 device_set_desc(dev, "NEC 765 or clone"); 647 fdc->fdct = FDC_NE765; 648 break; 649 case 0x81: 650 device_set_desc(dev, "Intel 82077 or clone"); 651 fdc->fdct = FDC_I82077; 652 break; 653 case 0x90: 654 device_set_desc(dev, "NEC 72065B or clone"); 655 fdc->fdct = FDC_NE72065; 656 break; 657 default: 658 device_set_desc(dev, "generic floppy controller"); 659 fdc->fdct = FDC_UNKNOWN; 660 break; 661 } 662 } 663 664 snprintf(myname, sizeof(myname), "%s%d", device_get_name(dev), 665 device_get_unit(dev)); 666 for (i = resource_query_string(-1, "at", myname); i != -1; 667 i = resource_query_string(i, "at", myname)) 668 fdc_add_device(dev, resource_query_name(i), 669 resource_query_unit(i)); 670 #ifdef FDC_YE 671 /* 672 * don't succeed on probe; wait 673 * for PCCARD subsystem to do it 674 */ 675 if (dev->id_flags & FDC_IS_PCMCIA) 676 return(0); 677 #endif 678 return (0); 679 680 out: 681 if (fdc->fdc_intr) 682 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq, 683 fdc->fdc_intr); 684 if (fdc->res_irq != 0) { 685 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 686 fdc->res_irq); 687 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 688 fdc->res_irq); 689 } 690 if (fdc->res_ioport != 0) { 691 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 692 fdc->res_ioport); 693 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 694 fdc->res_ioport); 695 } 696 if (fdc->res_drq != 0) { 697 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 698 fdc->res_drq); 699 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 700 fdc->res_drq); 701 } 702 return (error); 703 } 704 705 /* 706 * Aped dfr@freebsd.org's isa_add_device(). 707 */ 708 static void 709 fdc_add_device(device_t dev, const char *name, int unit) 710 { 711 int disabled, *ivar; 712 device_t child; 713 714 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT); 715 if (ivar == 0) 716 return; 717 if (resource_int_value(name, unit, "drive", ivar) != 0) 718 *ivar = 0; 719 child = device_add_child(dev, name, unit, ivar); 720 if (child == 0) 721 return; 722 if (resource_int_value(name, unit, "disabled", &disabled) == 0 723 && disabled != 0) 724 device_disable(child); 725 } 726 727 static int 728 fdc_attach(device_t dev) 729 { 730 struct fdc_data *fdc = device_get_softc(dev); 731 fdcu_t fdcu = device_get_unit(dev); 732 733 fdc->fdcu = fdcu; 734 fdc->flags |= FDC_ATTACHED; 735 736 /* Acquire the DMA channel forever, The driver will do the rest */ 737 /* XXX should integrate with rman */ 738 isa_dma_acquire(fdc->dmachan); 739 isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */); 740 fdc->state = DEVIDLE; 741 742 /* reset controller, turn motor off, clear fdout mirror reg */ 743 outb(fdc->baseport + FDOUT, ((fdc->fdout = 0))); 744 bufq_init(&fdc->head); 745 746 #ifdef FIFO_BEFORE_MOTORON 747 /* Hmm, this doesn't work here - is set_motor() magic? -Peter */ 748 if (fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN 749 && enable_fifo(fdc) == 0) { 750 device_print_prettyname(dev); 751 printf("FIFO enabled, %d bytes threshold\n", fifo_threshold); 752 } 753 #endif 754 /* 755 * Probe and attach any children as were configured above. 756 */ 757 return (bus_generic_attach(dev)); 758 } 759 760 static int 761 fdc_print_child(device_t me, device_t child) 762 { 763 int retval = 0; 764 765 retval += bus_print_child_header(me, child); 766 retval += printf(" on %s drive %d\n", device_get_nameunit(me), 767 *(int *)device_get_ivars(child)); 768 769 return (retval); 770 } 771 772 static int 773 fd_probe(device_t dev) 774 { 775 int i; 776 u_int fdt, st0, st3; 777 struct fd_data *fd; 778 struct fdc_data *fdc; 779 fdsu_t fdsu; 780 #ifndef FIFO_BEFORE_MOTORON 781 static int fd_fifo = 0; 782 #endif 783 784 fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */ 785 fd = device_get_softc(dev); 786 fdc = device_get_softc(device_get_parent(dev)); 787 788 bzero(fd, sizeof *fd); 789 fd->dev = dev; 790 fd->fdc = fdc; 791 fd->fdsu = fdsu; 792 fd->fdu = device_get_unit(dev); 793 794 #ifdef __i386__ 795 /* look up what bios thinks we have */ 796 switch (fd->fdu) { 797 case 0: 798 if (isa_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0) 799 fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED; 800 else 801 fdt = (rtcin(RTC_FDISKETTE) & 0xf0); 802 break; 803 case 1: 804 fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0); 805 break; 806 default: 807 fdt = RTCFDT_NONE; 808 break; 809 } 810 #else 811 fdt = RTCFDT_144M; /* XXX probably */ 812 #endif 813 814 /* is there a unit? */ 815 if (fdt == RTCFDT_NONE) 816 return (ENXIO); 817 818 /* select it */ 819 set_motor(fdc, fdsu, TURNON); 820 DELAY(1000000); /* 1 sec */ 821 822 #ifndef FIFO_BEFORE_MOTORON 823 if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN 824 && enable_fifo(fdc) == 0) { 825 device_print_prettyname(device_get_parent(dev)); 826 printf("FIFO enabled, %d bytes threshold\n", fifo_threshold); 827 } 828 fd_fifo = 1; 829 #endif 830 831 if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) 832 && (st3 & NE7_ST3_T0)) { 833 /* if at track 0, first seek inwards */ 834 /* seek some steps: */ 835 fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0); 836 DELAY(300000); /* ...wait a moment... */ 837 fd_sense_int(fdc, 0, 0); /* make ctrlr happy */ 838 } 839 840 /* If we're at track 0 first seek inwards. */ 841 if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) { 842 /* Seek some steps... */ 843 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 844 /* ...wait a moment... */ 845 DELAY(300000); 846 /* make ctrlr happy: */ 847 fd_sense_int(fdc, 0, 0); 848 } 849 } 850 851 for (i = 0; i < 2; i++) { 852 /* 853 * we must recalibrate twice, just in case the 854 * heads have been beyond cylinder 76, since most 855 * FDCs still barf when attempting to recalibrate 856 * more than 77 steps 857 */ 858 /* go back to 0: */ 859 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 860 /* a second being enough for full stroke seek*/ 861 DELAY(i == 0 ? 1000000 : 300000); 862 863 /* anything responding? */ 864 if (fd_sense_int(fdc, &st0, 0) == 0 && 865 (st0 & NE7_ST0_EC) == 0) 866 break; /* already probed succesfully */ 867 } 868 } 869 870 set_motor(fdc, fdsu, TURNOFF); 871 872 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */ 873 return (ENXIO); 874 875 fd->track = FD_NO_TRACK; 876 fd->fdc = fdc; 877 fd->fdsu = fdsu; 878 fd->options = 0; 879 callout_handle_init(&fd->toffhandle); 880 callout_handle_init(&fd->tohandle); 881 882 switch (fdt) { 883 case RTCFDT_12M: 884 device_set_desc(dev, "1200-KB 5.25\" drive"); 885 fd->type = FD_1200; 886 break; 887 case RTCFDT_144M | RTCFDT_144M_PRETENDED: 888 device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive"); 889 fdt = RTCFDT_144M; 890 fd->type = FD_1440; 891 case RTCFDT_144M: 892 device_set_desc(dev, "1440-KB 3.5\" drive"); 893 fd->type = FD_1440; 894 break; 895 case RTCFDT_288M: 896 case RTCFDT_288M_1: 897 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)"); 898 fd->type = FD_1440; 899 break; 900 case RTCFDT_360K: 901 device_set_desc(dev, "360-KB 5.25\" drive"); 902 fd->type = FD_360; 903 break; 904 case RTCFDT_720K: 905 printf("720-KB 3.5\" drive"); 906 fd->type = FD_720; 907 break; 908 default: 909 return (ENXIO); 910 } 911 return (0); 912 } 913 914 static int 915 fd_attach(device_t dev) 916 { 917 struct fd_data *fd; 918 #ifdef DEVFS 919 int i; 920 int mynor; 921 int typemynor; 922 int typesize; 923 #endif 924 925 fd = device_get_softc(dev); 926 927 #ifdef DEVFS /* XXX bitrot */ 928 mynor = fd->fdu << 6; 929 fd->bdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_BLK, 930 UID_ROOT, GID_OPERATOR, 0640, 931 "fd%d", fd->fdu); 932 fd->cdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_CHR, 933 UID_ROOT, GID_OPERATOR, 0640, 934 "rfd%d", fd->fdu); 935 for (i = 1; i < 1 + NUMDENS; i++) { 936 /* 937 * XXX this and the lookup in Fdopen() should be 938 * data driven. 939 */ 940 switch (fd->type) { 941 case FD_360: 942 if (i != FD_360) 943 continue; 944 break; 945 case FD_720: 946 if (i != FD_720 && i != FD_800 && i != FD_820) 947 continue; 948 break; 949 case FD_1200: 950 if (i != FD_360 && i != FD_720 && i != FD_800 951 && i != FD_820 && i != FD_1200 952 && i != FD_1440 && i != FD_1480) 953 continue; 954 break; 955 case FD_1440: 956 if (i != FD_720 && i != FD_800 && i != FD_820 957 && i != FD_1200 && i != FD_1440 958 && i != FD_1480 && i != FD_1720) 959 continue; 960 break; 961 } 962 typesize = fd_types[i - 1].size / 2; 963 /* 964 * XXX all these conversions give bloated code and 965 * confusing names. 966 */ 967 if (typesize == 1476) 968 typesize = 1480; 969 if (typesize == 1722) 970 typesize = 1720; 971 typemynor = mynor | i; 972 fd->bdevs[i] = 973 devfs_add_devswf(&fd_cdevsw, typemynor, DV_BLK, 974 UID_ROOT, GID_OPERATOR, 0640, 975 "fd%d.%d", fd->fdu, typesize); 976 fd->cdevs[i] = 977 devfs_add_devswf(&fd_cdevsw, typemynor, DV_CHR, 978 UID_ROOT, GID_OPERATOR, 0640, 979 "rfd%d.%d", fd->fdu, typesize); 980 } 981 982 for (i = 0; i < MAXPARTITIONS; i++) { 983 fd->bdevs[1 + NUMDENS + i] = devfs_makelink(fd->bdevs[0], 984 "fd%d%c", fd->fdu, 'a' + i); 985 fd->cdevs[1 + NUMDENS + i] = 986 devfs_makelink(fd->cdevs[0], 987 "rfd%d%c", fd->fdu, 'a' + i); 988 } 989 #endif /* DEVFS */ 990 /* 991 * Export the drive to the devstat interface. 992 */ 993 devstat_add_entry(&fd->device_stats, device_get_name(dev), 994 device_get_unit(dev), 512, DEVSTAT_NO_ORDERED_TAGS, 995 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER, 996 DEVSTAT_PRIORITY_FD); 997 return (0); 998 } 999 1000 #ifdef FDC_YE 1001 /* 1002 * this is a subset of fdattach() optimized for the Y-E Data 1003 * PCMCIA floppy drive. 1004 */ 1005 static int yeattach(struct isa_device *dev) 1006 { 1007 fdcu_t fdcu = dev->id_unit; 1008 fdc_p fdc = fdc_data + fdcu; 1009 fdsu_t fdsu = 0; /* assume 1 drive per YE controller */ 1010 fdu_t fdu; 1011 fd_p fd; 1012 int st0, st3, i; 1013 #ifdef DEVFS 1014 int mynor; 1015 int typemynor; 1016 int typesize; 1017 #endif 1018 fdc->fdcu = fdcu; 1019 /* 1020 * the FDC_PCMCIA flag is used to to indicate special PIO is used 1021 * instead of DMA 1022 */ 1023 fdc->flags = FDC_ATTACHED|FDC_PCMCIA; 1024 fdc->state = DEVIDLE; 1025 /* reset controller, turn motor off, clear fdout mirror reg */ 1026 outb(fdc->baseport + FDOUT, ((fdc->fdout = 0))); 1027 bufq_init(&fdc->head); 1028 /* 1029 * assume 2 drives/ "normal" controller 1030 */ 1031 fdu = fdcu * 2; 1032 if (fdu >= NFD) { 1033 printf("fdu %d >= NFD\n",fdu); 1034 return(0); 1035 }; 1036 fd = &fd_data[fdu]; 1037 1038 set_motor(fdcu, fdsu, TURNON); 1039 DELAY(1000000); /* 1 sec */ 1040 fdc->fdct = FDC_NE765; 1041 1042 if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) && 1043 (st3 & NE7_ST3_T0)) { 1044 /* if at track 0, first seek inwards */ 1045 /* seek some steps: */ 1046 (void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0); 1047 DELAY(300000); /* ...wait a moment... */ 1048 (void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */ 1049 } 1050 1051 /* If we're at track 0 first seek inwards. */ 1052 if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) { 1053 /* Seek some steps... */ 1054 if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 1055 /* ...wait a moment... */ 1056 DELAY(300000); 1057 /* make ctrlr happy: */ 1058 (void)fd_sense_int(fdc, 0, 0); 1059 } 1060 } 1061 1062 for(i = 0; i < 2; i++) { 1063 /* 1064 * we must recalibrate twice, just in case the 1065 * heads have been beyond cylinder 76, since most 1066 * FDCs still barf when attempting to recalibrate 1067 * more than 77 steps 1068 */ 1069 /* go back to 0: */ 1070 if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 1071 /* a second being enough for full stroke seek*/ 1072 DELAY(i == 0? 1000000: 300000); 1073 1074 /* anything responding? */ 1075 if (fd_sense_int(fdc, &st0, 0) == 0 && 1076 (st0 & NE7_ST0_EC) == 0) 1077 break; /* already probed succesfully */ 1078 } 1079 } 1080 1081 set_motor(fdcu, fdsu, TURNOFF); 1082 1083 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */ 1084 return(0); 1085 1086 fd->track = FD_NO_TRACK; 1087 fd->fdc = fdc; 1088 fd->fdsu = fdsu; 1089 fd->options = 0; 1090 printf("fdc%d: 1.44MB 3.5in PCMCIA\n", fdcu); 1091 fd->type = FD_1440; 1092 1093 #ifdef DEVFS 1094 mynor = fdcu << 6; 1095 fd->bdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_BLK, 1096 UID_ROOT, GID_OPERATOR, 0640, 1097 "fd%d", fdu); 1098 fd->cdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_CHR, 1099 UID_ROOT, GID_OPERATOR, 0640, 1100 "rfd%d", fdu); 1101 /* 1102 * XXX this and the lookup in Fdopen() should be 1103 * data driven. 1104 */ 1105 typemynor = mynor | FD_1440; 1106 typesize = fd_types[FD_1440 - 1].size / 2; 1107 /* 1108 * XXX all these conversions give bloated code and 1109 * confusing names. 1110 */ 1111 if (typesize == 1476) 1112 typesize = 1480; 1113 if (typesize == 1722) 1114 typesize = 1720; 1115 fd->bdevs[FD_1440] = devfs_add_devswf(&fd_cdevsw, typemynor, 1116 DV_BLK, UID_ROOT, GID_OPERATOR, 1117 0640, "fd%d.%d", fdu, typesize); 1118 fd->cdevs[FD_1440] = devfs_add_devswf(&fd_cdevsw, typemynor, 1119 DV_CHR, UID_ROOT, GID_OPERATOR, 1120 0640,"rfd%d.%d", fdu, typesize); 1121 for (i = 0; i < MAXPARTITIONS; i++) { 1122 fd->bdevs[1 + NUMDENS + i] = devfs_makelink(fd->bdevs[0], 1123 "fd%d%c", fdu, 'a' + i); 1124 fd->cdevs[1 + NUMDENS + i] = devfs_makelink(fd->cdevs[0], 1125 "rfd%d%c", fdu, 'a' + i); 1126 } 1127 #endif /* DEVFS */ 1128 return (1); 1129 } 1130 #endif 1131 1132 /****************************************************************************/ 1133 /* motor control stuff */ 1134 /* remember to not deselect the drive we're working on */ 1135 /****************************************************************************/ 1136 static void 1137 set_motor(struct fdc_data *fdc, int fdsu, int turnon) 1138 { 1139 int fdout = fdc->fdout; 1140 int needspecify = 0; 1141 1142 if(turnon) { 1143 fdout &= ~FDO_FDSEL; 1144 fdout |= (FDO_MOEN0 << fdsu) + fdsu; 1145 } else 1146 fdout &= ~(FDO_MOEN0 << fdsu); 1147 1148 if(!turnon 1149 && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0) 1150 /* gonna turn off the last drive, put FDC to bed */ 1151 fdout &= ~ (FDO_FRST|FDO_FDMAEN); 1152 else { 1153 /* make sure controller is selected and specified */ 1154 if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0) 1155 needspecify = 1; 1156 fdout |= (FDO_FRST|FDO_FDMAEN); 1157 } 1158 1159 outb(fdc->baseport+FDOUT, fdout); 1160 fdc->fdout = fdout; 1161 TRACE1("[0x%x->FDOUT]", fdout); 1162 1163 if (needspecify) { 1164 /* 1165 * XXX 1166 * special case: since we have just woken up the FDC 1167 * from its sleep, we silently assume the command will 1168 * be accepted, and do not test for a timeout 1169 */ 1170 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1171 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1172 0); 1173 if (fdc->flags & FDC_HAS_FIFO) 1174 (void) enable_fifo(fdc); 1175 } 1176 } 1177 1178 static void 1179 fd_turnoff(void *xfd) 1180 { 1181 int s; 1182 fd_p fd = xfd; 1183 1184 TRACE1("[fd%d: turnoff]", fd->fdu); 1185 1186 /* 1187 * Don't turn off the motor yet if the drive is active. 1188 * XXX shouldn't even schedule turnoff until drive is inactive 1189 * and nothing is queued on it. 1190 */ 1191 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) { 1192 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz); 1193 return; 1194 } 1195 1196 s = splbio(); 1197 fd->flags &= ~FD_MOTOR; 1198 set_motor(fd->fdc, fd->fdsu, TURNOFF); 1199 splx(s); 1200 } 1201 1202 static void 1203 fd_motor_on(void *xfd) 1204 { 1205 int s; 1206 fd_p fd = xfd; 1207 1208 s = splbio(); 1209 fd->flags &= ~FD_MOTOR_WAIT; 1210 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) 1211 { 1212 fdc_intr(fd->fdc); 1213 } 1214 splx(s); 1215 } 1216 1217 static void 1218 fd_turnon(fd_p fd) 1219 { 1220 if(!(fd->flags & FD_MOTOR)) 1221 { 1222 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); 1223 set_motor(fd->fdc, fd->fdsu, TURNON); 1224 timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */ 1225 } 1226 } 1227 1228 static void 1229 fdc_reset(fdc_p fdc) 1230 { 1231 /* Try a reset, keep motor on */ 1232 outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 1233 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 1234 DELAY(100); 1235 /* enable FDC, but defer interrupts a moment */ 1236 outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN); 1237 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); 1238 DELAY(100); 1239 outb(fdc->baseport + FDOUT, fdc->fdout); 1240 TRACE1("[0x%x->FDOUT]", fdc->fdout); 1241 1242 /* XXX after a reset, silently believe the FDC will accept commands */ 1243 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1244 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1245 0); 1246 if (fdc->flags & FDC_HAS_FIFO) 1247 (void) enable_fifo(fdc); 1248 } 1249 1250 /****************************************************************************/ 1251 /* fdc in/out */ 1252 /****************************************************************************/ 1253 int 1254 in_fdc(struct fdc_data *fdc) 1255 { 1256 int baseport = fdc->baseport; 1257 int i, j = 100000; 1258 while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM)) 1259 != (NE7_DIO|NE7_RQM) && j-- > 0) 1260 if (i == NE7_RQM) 1261 return fdc_err(fdc, "ready for output in input\n"); 1262 if (j <= 0) 1263 return fdc_err(fdc, bootverbose? "input ready timeout\n": 0); 1264 #ifdef FDC_DEBUG 1265 i = inb(baseport+FDDATA); 1266 TRACE1("[FDDATA->0x%x]", (unsigned char)i); 1267 return(i); 1268 #else /* !FDC_DEBUG */ 1269 return inb(baseport+FDDATA); 1270 #endif /* FDC_DEBUG */ 1271 } 1272 1273 /* 1274 * fd_in: Like in_fdc, but allows you to see if it worked. 1275 */ 1276 static int 1277 fd_in(struct fdc_data *fdc, int *ptr) 1278 { 1279 int baseport = fdc->baseport; 1280 int i, j = 100000; 1281 while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM)) 1282 != (NE7_DIO|NE7_RQM) && j-- > 0) 1283 if (i == NE7_RQM) 1284 return fdc_err(fdc, "ready for output in input\n"); 1285 if (j <= 0) 1286 return fdc_err(fdc, bootverbose? "input ready timeout\n": 0); 1287 #ifdef FDC_DEBUG 1288 i = inb(baseport+FDDATA); 1289 TRACE1("[FDDATA->0x%x]", (unsigned char)i); 1290 *ptr = i; 1291 return 0; 1292 #else /* !FDC_DEBUG */ 1293 i = inb(baseport+FDDATA); 1294 if (ptr) 1295 *ptr = i; 1296 return 0; 1297 #endif /* FDC_DEBUG */ 1298 } 1299 1300 int 1301 out_fdc(struct fdc_data *fdc, int x) 1302 { 1303 int baseport = fdc->baseport; 1304 int i; 1305 1306 /* Check that the direction bit is set */ 1307 i = 100000; 1308 while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0); 1309 if (i <= 0) return fdc_err(fdc, "direction bit not set\n"); 1310 1311 /* Check that the floppy controller is ready for a command */ 1312 i = 100000; 1313 while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0); 1314 if (i <= 0) 1315 return fdc_err(fdc, bootverbose? "output ready timeout\n": 0); 1316 1317 /* Send the command and return */ 1318 outb(baseport+FDDATA, x); 1319 TRACE1("[0x%x->FDDATA]", x); 1320 return (0); 1321 } 1322 1323 /****************************************************************************/ 1324 /* fdopen/fdclose */ 1325 /****************************************************************************/ 1326 int 1327 Fdopen(dev_t dev, int flags, int mode, struct proc *p) 1328 { 1329 fdu_t fdu = FDUNIT(minor(dev)); 1330 int type = FDTYPE(minor(dev)); 1331 fd_p fd; 1332 fdc_p fdc; 1333 1334 /* check bounds */ 1335 if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0) 1336 return (ENXIO); 1337 fdc = fd->fdc; 1338 if ((fdc == NULL) || (fd->type == NO_TYPE)) 1339 return (ENXIO); 1340 if (type > NUMDENS) 1341 return (ENXIO); 1342 if (type == 0) 1343 type = fd->type; 1344 else { 1345 /* 1346 * For each type of basic drive, make sure we are trying 1347 * to open a type it can do, 1348 */ 1349 if (type != fd->type) { 1350 switch (fd->type) { 1351 case FD_360: 1352 return (ENXIO); 1353 case FD_720: 1354 if ( type != FD_820 1355 && type != FD_800 1356 ) 1357 return (ENXIO); 1358 break; 1359 case FD_1200: 1360 switch (type) { 1361 case FD_1480: 1362 type = FD_1480in5_25; 1363 break; 1364 case FD_1440: 1365 type = FD_1440in5_25; 1366 break; 1367 case FD_820: 1368 type = FD_820in5_25; 1369 break; 1370 case FD_800: 1371 type = FD_800in5_25; 1372 break; 1373 case FD_720: 1374 type = FD_720in5_25; 1375 break; 1376 case FD_360: 1377 type = FD_360in5_25; 1378 break; 1379 default: 1380 return(ENXIO); 1381 } 1382 break; 1383 case FD_1440: 1384 if ( type != FD_1720 1385 && type != FD_1480 1386 && type != FD_1200 1387 && type != FD_820 1388 && type != FD_800 1389 && type != FD_720 1390 ) 1391 return(ENXIO); 1392 break; 1393 } 1394 } 1395 } 1396 fd->ft = fd_types + type - 1; 1397 fd->flags |= FD_OPEN; 1398 device_busy(fd->dev); 1399 device_busy(fd->fdc->fdc_dev); 1400 return 0; 1401 } 1402 1403 int 1404 fdclose(dev_t dev, int flags, int mode, struct proc *p) 1405 { 1406 fdu_t fdu = FDUNIT(minor(dev)); 1407 struct fd_data *fd; 1408 1409 fd = devclass_get_softc(fd_devclass, fdu); 1410 fd->flags &= ~FD_OPEN; 1411 fd->options &= ~FDOPT_NORETRY; 1412 1413 return (0); 1414 } 1415 1416 /****************************************************************************/ 1417 /* fdstrategy */ 1418 /****************************************************************************/ 1419 void 1420 fdstrategy(struct buf *bp) 1421 { 1422 unsigned nblocks, blknum, cando; 1423 int s; 1424 fdu_t fdu; 1425 fdc_p fdc; 1426 fd_p fd; 1427 size_t fdblk; 1428 1429 fdu = FDUNIT(minor(bp->b_dev)); 1430 fd = devclass_get_softc(fd_devclass, fdu); 1431 if (fd == 0) 1432 panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)", 1433 (u_long)major(bp->b_dev), (u_long)minor(bp->b_dev)); 1434 fdc = fd->fdc; 1435 #ifdef FDC_YE 1436 if (fd->type == NO_TYPE) { 1437 bp->b_error = ENXIO; 1438 bp->b_flags |= B_ERROR; 1439 /* 1440 * I _refuse_ to use a goto 1441 */ 1442 biodone(bp); 1443 return; 1444 }; 1445 #endif 1446 1447 fdblk = 128 << (fd->ft->secsize); 1448 if (!(bp->b_flags & B_FORMAT)) { 1449 if (bp->b_blkno < 0) { 1450 printf( 1451 "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n", 1452 fdu, (u_long)bp->b_blkno, bp->b_bcount); 1453 bp->b_error = EINVAL; 1454 bp->b_flags |= B_ERROR; 1455 goto bad; 1456 } 1457 if ((bp->b_bcount % fdblk) != 0) { 1458 bp->b_error = EINVAL; 1459 bp->b_flags |= B_ERROR; 1460 goto bad; 1461 } 1462 } 1463 1464 /* 1465 * Set up block calculations. 1466 */ 1467 if (bp->b_blkno > 20000000) { 1468 /* 1469 * Reject unreasonably high block number, prevent the 1470 * multiplication below from overflowing. 1471 */ 1472 bp->b_error = EINVAL; 1473 bp->b_flags |= B_ERROR; 1474 goto bad; 1475 } 1476 blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk; 1477 nblocks = fd->ft->size; 1478 bp->b_resid = 0; 1479 if (blknum + (bp->b_bcount / fdblk) > nblocks) { 1480 if (blknum <= nblocks) { 1481 cando = (nblocks - blknum) * fdblk; 1482 bp->b_resid = bp->b_bcount - cando; 1483 if (cando == 0) 1484 goto bad; /* not actually bad but EOF */ 1485 } else { 1486 bp->b_error = EINVAL; 1487 bp->b_flags |= B_ERROR; 1488 goto bad; 1489 } 1490 } 1491 bp->b_pblkno = bp->b_blkno; 1492 s = splbio(); 1493 bufqdisksort(&fdc->head, bp); 1494 untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */ 1495 1496 /* Tell devstat we are starting on the transaction */ 1497 devstat_start_transaction(&fd->device_stats); 1498 1499 fdstart(fdc); 1500 splx(s); 1501 return; 1502 1503 bad: 1504 biodone(bp); 1505 } 1506 1507 /***************************************************************\ 1508 * fdstart * 1509 * We have just queued something.. if the controller is not busy * 1510 * then simulate the case where it has just finished a command * 1511 * So that it (the interrupt routine) looks on the queue for more* 1512 * work to do and picks up what we just added. * 1513 * If the controller is already busy, we need do nothing, as it * 1514 * will pick up our work when the present work completes * 1515 \***************************************************************/ 1516 static void 1517 fdstart(struct fdc_data *fdc) 1518 { 1519 int s; 1520 1521 s = splbio(); 1522 if(fdc->state == DEVIDLE) 1523 { 1524 fdc_intr(fdc); 1525 } 1526 splx(s); 1527 } 1528 1529 static void 1530 fd_iotimeout(void *xfdc) 1531 { 1532 fdc_p fdc; 1533 int s; 1534 1535 fdc = xfdc; 1536 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu); 1537 1538 /* 1539 * Due to IBM's brain-dead design, the FDC has a faked ready 1540 * signal, hardwired to ready == true. Thus, any command 1541 * issued if there's no diskette in the drive will _never_ 1542 * complete, and must be aborted by resetting the FDC. 1543 * Many thanks, Big Blue! 1544 * The FDC must not be reset directly, since that would 1545 * interfere with the state machine. Instead, pretend that 1546 * the command completed but was invalid. The state machine 1547 * will reset the FDC and retry once. 1548 */ 1549 s = splbio(); 1550 fdc->status[0] = NE7_ST0_IC_IV; 1551 fdc->flags &= ~FDC_STAT_VALID; 1552 fdc->state = IOTIMEDOUT; 1553 fdc_intr(fdc); 1554 splx(s); 1555 } 1556 1557 /* just ensure it has the right spl */ 1558 static void 1559 fd_pseudointr(void *xfdc) 1560 { 1561 int s; 1562 1563 s = splbio(); 1564 fdc_intr(xfdc); 1565 splx(s); 1566 } 1567 1568 /***********************************************************************\ 1569 * fdintr * 1570 * keep calling the state machine until it returns a 0 * 1571 * ALWAYS called at SPLBIO * 1572 \***********************************************************************/ 1573 static void 1574 fdc_intr(void *xfdc) 1575 { 1576 fdc_p fdc = xfdc; 1577 while(fdstate(fdc)) 1578 ; 1579 } 1580 1581 #ifdef FDC_YE 1582 /* 1583 * magic pseudo-DMA initialization for YE FDC. Sets count and 1584 * direction 1585 */ 1586 #define SET_BCDR(wr,cnt,port) outb(port,(((cnt)-1) & 0xff)); \ 1587 outb(port+1,((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f))) 1588 1589 /* 1590 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy 1591 */ 1592 static int fdcpio(fdcu_t fdcu, long flags, caddr_t addr, u_int count) 1593 { 1594 u_char *cptr = (u_char *)addr; 1595 fdc_p fdc = &fdc_data[fdcu]; 1596 int io = fdc->baseport; 1597 1598 if (flags & B_READ) { 1599 if (fdc->state != PIOREAD) { 1600 fdc->state = PIOREAD; 1601 return(0); 1602 }; 1603 SET_BCDR(0,count,io); 1604 insb(io+FDC_YE_DATAPORT,cptr,count); 1605 } else { 1606 outsb(io+FDC_YE_DATAPORT,cptr,count); 1607 SET_BCDR(0,count,io); 1608 }; 1609 return(1); 1610 } 1611 #endif /* FDC_YE */ 1612 1613 /***********************************************************************\ 1614 * The controller state machine. * 1615 * if it returns a non zero value, it should be called again immediatly * 1616 \***********************************************************************/ 1617 static int 1618 fdstate(fdc_p fdc) 1619 { 1620 int read, format, head, i, sec = 0, sectrac, st0, cyl, st3; 1621 unsigned blknum = 0, b_cylinder = 0; 1622 fdu_t fdu = fdc->fdu; 1623 fd_p fd; 1624 register struct buf *bp; 1625 struct fd_formb *finfo = NULL; 1626 size_t fdblk; 1627 1628 bp = fdc->bp; 1629 if (bp == NULL) { 1630 bp = bufq_first(&fdc->head); 1631 if (bp != NULL) { 1632 bufq_remove(&fdc->head, bp); 1633 fdc->bp = bp; 1634 } 1635 } 1636 if (bp == NULL) { 1637 /***********************************************\ 1638 * nothing left for this controller to do * 1639 * Force into the IDLE state, * 1640 \***********************************************/ 1641 fdc->state = DEVIDLE; 1642 if (fdc->fd) { 1643 device_print_prettyname(fdc->fdc_dev); 1644 printf("unexpected valid fd pointer\n"); 1645 fdc->fd = (fd_p) 0; 1646 fdc->fdu = -1; 1647 } 1648 TRACE1("[fdc%d IDLE]", fdc->fdcu); 1649 return (0); 1650 } 1651 fdu = FDUNIT(minor(bp->b_dev)); 1652 fd = devclass_get_softc(fd_devclass, fdu); 1653 fdblk = 128 << fd->ft->secsize; 1654 if (fdc->fd && (fd != fdc->fd)) { 1655 device_print_prettyname(fd->dev); 1656 printf("confused fd pointers\n"); 1657 } 1658 read = bp->b_flags & B_READ; 1659 format = bp->b_flags & B_FORMAT; 1660 if (format) { 1661 finfo = (struct fd_formb *)bp->b_data; 1662 fd->skip = (char *)&(finfo->fd_formb_cylno(0)) 1663 - (char *)finfo; 1664 } 1665 if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) { 1666 blknum = (unsigned) bp->b_pblkno * DEV_BSIZE/fdblk + 1667 fd->skip/fdblk; 1668 b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 1669 } 1670 TRACE1("fd%d", fdu); 1671 TRACE1("[%s]", fdstates[fdc->state]); 1672 TRACE1("(0x%x)", fd->flags); 1673 untimeout(fd_turnoff, fd, fd->toffhandle); 1674 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz); 1675 switch (fdc->state) 1676 { 1677 case DEVIDLE: 1678 case FINDWORK: /* we have found new work */ 1679 fdc->retry = 0; 1680 fd->skip = 0; 1681 fdc->fd = fd; 1682 fdc->fdu = fdu; 1683 outb(fdc->baseport+FDCTL, fd->ft->trans); 1684 TRACE1("[0x%x->FDCTL]", fd->ft->trans); 1685 /*******************************************************\ 1686 * If the next drive has a motor startup pending, then * 1687 * it will start up in its own good time * 1688 \*******************************************************/ 1689 if(fd->flags & FD_MOTOR_WAIT) { 1690 fdc->state = MOTORWAIT; 1691 return (0); /* come back later */ 1692 } 1693 /*******************************************************\ 1694 * Maybe if it's not starting, it SHOULD be starting * 1695 \*******************************************************/ 1696 if (!(fd->flags & FD_MOTOR)) 1697 { 1698 fdc->state = MOTORWAIT; 1699 fd_turnon(fd); 1700 return (0); 1701 } 1702 else /* at least make sure we are selected */ 1703 { 1704 set_motor(fdc, fd->fdsu, TURNON); 1705 } 1706 if (fdc->flags & FDC_NEEDS_RESET) { 1707 fdc->state = RESETCTLR; 1708 fdc->flags &= ~FDC_NEEDS_RESET; 1709 } else 1710 fdc->state = DOSEEK; 1711 break; 1712 case DOSEEK: 1713 if (b_cylinder == (unsigned)fd->track) 1714 { 1715 fdc->state = SEEKCOMPLETE; 1716 break; 1717 } 1718 if (fd_cmd(fdc, 3, NE7CMD_SEEK, 1719 fd->fdsu, b_cylinder * fd->ft->steptrac, 1720 0)) 1721 { 1722 /* 1723 * seek command not accepted, looks like 1724 * the FDC went off to the Saints... 1725 */ 1726 fdc->retry = 6; /* try a reset */ 1727 return(retrier(fdc)); 1728 } 1729 fd->track = FD_NO_TRACK; 1730 fdc->state = SEEKWAIT; 1731 return(0); /* will return later */ 1732 case SEEKWAIT: 1733 /* allow heads to settle */ 1734 timeout(fd_pseudointr, fdc, hz / 16); 1735 fdc->state = SEEKCOMPLETE; 1736 return(0); /* will return later */ 1737 case SEEKCOMPLETE : /* SEEK DONE, START DMA */ 1738 /* Make sure seek really happened*/ 1739 if(fd->track == FD_NO_TRACK) { 1740 int descyl = b_cylinder * fd->ft->steptrac; 1741 do { 1742 /* 1743 * This might be a "ready changed" interrupt, 1744 * which cannot really happen since the 1745 * RDY pin is hardwired to + 5 volts. This 1746 * generally indicates a "bouncing" intr 1747 * line, so do one of the following: 1748 * 1749 * When running on an enhanced FDC that is 1750 * known to not go stuck after responding 1751 * with INVALID, fetch all interrupt states 1752 * until seeing either an INVALID or a 1753 * real interrupt condition. 1754 * 1755 * When running on a dumb old NE765, give 1756 * up immediately. The controller will 1757 * provide up to four dummy RC interrupt 1758 * conditions right after reset (for the 1759 * corresponding four drives), so this is 1760 * our only chance to get notice that it 1761 * was not the FDC that caused the interrupt. 1762 */ 1763 if (fd_sense_int(fdc, &st0, &cyl) 1764 == FD_NOT_VALID) 1765 return 0; 1766 if(fdc->fdct == FDC_NE765 1767 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 1768 return 0; /* hope for a real intr */ 1769 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 1770 1771 if (0 == descyl) { 1772 int failed = 0; 1773 /* 1774 * seek to cyl 0 requested; make sure we are 1775 * really there 1776 */ 1777 if (fd_sense_drive_status(fdc, &st3)) 1778 failed = 1; 1779 if ((st3 & NE7_ST3_T0) == 0) { 1780 printf( 1781 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", 1782 fdu, st3, NE7_ST3BITS); 1783 failed = 1; 1784 } 1785 1786 if (failed) { 1787 if(fdc->retry < 3) 1788 fdc->retry = 3; 1789 return (retrier(fdc)); 1790 } 1791 } 1792 1793 if (cyl != descyl) { 1794 printf( 1795 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", 1796 fdu, descyl, cyl, st0); 1797 if (fdc->retry < 3) 1798 fdc->retry = 3; 1799 return (retrier(fdc)); 1800 } 1801 } 1802 1803 fd->track = b_cylinder; 1804 #ifdef FDC_YE 1805 if (!(fdc->flags & FDC_PCMCIA)) 1806 #endif 1807 isa_dmastart(bp->b_flags, bp->b_data+fd->skip, 1808 format ? bp->b_bcount : fdblk, fdc->dmachan); 1809 sectrac = fd->ft->sectrac; 1810 sec = blknum % (sectrac * fd->ft->heads); 1811 head = sec / sectrac; 1812 sec = sec % sectrac + 1; 1813 fd->hddrv = ((head&1)<<2)+fdu; 1814 1815 if(format || !read) 1816 { 1817 /* make sure the drive is writable */ 1818 if(fd_sense_drive_status(fdc, &st3) != 0) 1819 { 1820 /* stuck controller? */ 1821 isa_dmadone(bp->b_flags, bp->b_data + fd->skip, 1822 format ? bp->b_bcount : fdblk, 1823 fdc->dmachan); 1824 fdc->retry = 6; /* reset the beast */ 1825 return (retrier(fdc)); 1826 } 1827 if(st3 & NE7_ST3_WP) 1828 { 1829 /* 1830 * XXX YES! this is ugly. 1831 * in order to force the current operation 1832 * to fail, we will have to fake an FDC 1833 * error - all error handling is done 1834 * by the retrier() 1835 */ 1836 fdc->status[0] = NE7_ST0_IC_AT; 1837 fdc->status[1] = NE7_ST1_NW; 1838 fdc->status[2] = 0; 1839 fdc->status[3] = fd->track; 1840 fdc->status[4] = head; 1841 fdc->status[5] = sec; 1842 fdc->retry = 8; /* break out immediately */ 1843 fdc->state = IOTIMEDOUT; /* not really... */ 1844 return (1); 1845 } 1846 } 1847 1848 if (format) { 1849 #ifdef FDC_YE 1850 if (fdc->flags & FDC_PCMCIA) 1851 (void)fdcpio(fdcu,bp->b_flags, 1852 bp->b_data+fd->skip, 1853 bp->b_bcount); 1854 #endif 1855 /* formatting */ 1856 if(fd_cmd(fdc, 6, NE7CMD_FORMAT, head << 2 | fdu, 1857 finfo->fd_formb_secshift, 1858 finfo->fd_formb_nsecs, 1859 finfo->fd_formb_gaplen, 1860 finfo->fd_formb_fillbyte, 0)) { 1861 /* controller fell over */ 1862 isa_dmadone(bp->b_flags, bp->b_data + fd->skip, 1863 format ? bp->b_bcount : fdblk, 1864 fdc->dmachan); 1865 fdc->retry = 6; 1866 return (retrier(fdc)); 1867 } 1868 } else { 1869 #ifdef FDC_YE 1870 if (fdc->flags & FDC_PCMCIA) { 1871 /* 1872 * this seems to be necessary even when 1873 * reading data 1874 */ 1875 SET_BCDR(1,fdblk,fdc->baseport); 1876 1877 /* 1878 * perform the write pseudo-DMA before 1879 * the WRITE command is sent 1880 */ 1881 if (!read) 1882 (void)fdcpio(fdcu,bp->b_flags, 1883 bp->b_data+fd->skip, 1884 fdblk); 1885 } 1886 #endif 1887 if (fd_cmd(fdc, 9, 1888 (read ? NE7CMD_READ : NE7CMD_WRITE), 1889 head << 2 | fdu, /* head & unit */ 1890 fd->track, /* track */ 1891 head, 1892 sec, /* sector + 1 */ 1893 fd->ft->secsize, /* sector size */ 1894 sectrac, /* sectors/track */ 1895 fd->ft->gap, /* gap size */ 1896 fd->ft->datalen, /* data length */ 1897 0)) { 1898 /* the beast is sleeping again */ 1899 isa_dmadone(bp->b_flags, bp->b_data + fd->skip, 1900 format ? bp->b_bcount : fdblk, 1901 fdc->dmachan); 1902 fdc->retry = 6; 1903 return (retrier(fdc)); 1904 } 1905 } 1906 #ifdef FDC_YE 1907 if (fdc->flags & FDC_PCMCIA) 1908 /* 1909 * if this is a read, then simply await interrupt 1910 * before performing PIO 1911 */ 1912 if (read && !fdcpio(fdcu,bp->b_flags, 1913 bp->b_data+fd->skip,fdblk)) { 1914 fd->tohandle = timeout(fd_iotimeout, 1915 (caddr_t)fdcu, hz); 1916 return(0); /* will return later */ 1917 }; 1918 1919 /* 1920 * write (or format) operation will fall through and 1921 * await completion interrupt 1922 */ 1923 #endif 1924 fdc->state = IOCOMPLETE; 1925 fd->tohandle = timeout(fd_iotimeout, fdc, hz); 1926 return (0); /* will return later */ 1927 #ifdef FDC_YE 1928 case PIOREAD: 1929 /* 1930 * actually perform the PIO read. The IOCOMPLETE case 1931 * removes the timeout for us. 1932 */ 1933 (void)fdcpio(fdcu,bp->b_flags,bp->b_data+fd->skip,fdblk); 1934 fdc->state = IOCOMPLETE; 1935 /* FALLTHROUGH */ 1936 #endif 1937 case IOCOMPLETE: /* IO DONE, post-analyze */ 1938 untimeout(fd_iotimeout, fdc, fd->tohandle); 1939 1940 if (fd_read_status(fdc, fd->fdsu)) { 1941 isa_dmadone(bp->b_flags, bp->b_data + fd->skip, 1942 format ? bp->b_bcount : fdblk, 1943 fdc->dmachan); 1944 if (fdc->retry < 6) 1945 fdc->retry = 6; /* force a reset */ 1946 return (retrier(fdc)); 1947 } 1948 1949 fdc->state = IOTIMEDOUT; 1950 1951 /* FALLTHROUGH */ 1952 1953 case IOTIMEDOUT: 1954 #ifdef FDC_YE 1955 if (!(fdc->flags & FDC_PCMCIA)) 1956 #endif 1957 isa_dmadone(bp->b_flags, bp->b_data + fd->skip, 1958 format ? bp->b_bcount : fdblk, fdc->dmachan); 1959 if (fdc->status[0] & NE7_ST0_IC) { 1960 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 1961 && fdc->status[1] & NE7_ST1_OR) { 1962 /* 1963 * DMA overrun. Someone hogged the bus 1964 * and didn't release it in time for the 1965 * next FDC transfer. 1966 * Just restart it, don't increment retry 1967 * count. (vak) 1968 */ 1969 fdc->state = SEEKCOMPLETE; 1970 return (1); 1971 } 1972 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV 1973 && fdc->retry < 6) 1974 fdc->retry = 6; /* force a reset */ 1975 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 1976 && fdc->status[2] & NE7_ST2_WC 1977 && fdc->retry < 3) 1978 fdc->retry = 3; /* force recalibrate */ 1979 return (retrier(fdc)); 1980 } 1981 /* All OK */ 1982 fd->skip += fdblk; 1983 if (!format && fd->skip < bp->b_bcount - bp->b_resid) { 1984 /* set up next transfer */ 1985 fdc->state = DOSEEK; 1986 } else { 1987 /* ALL DONE */ 1988 fd->skip = 0; 1989 fdc->bp = NULL; 1990 /* Tell devstat we have finished with the transaction */ 1991 devstat_end_transaction(&fd->device_stats, 1992 bp->b_bcount - bp->b_resid, 1993 DEVSTAT_TAG_NONE, 1994 (bp->b_flags & B_READ) ? 1995 DEVSTAT_READ : DEVSTAT_WRITE); 1996 biodone(bp); 1997 fdc->fd = (fd_p) 0; 1998 fdc->fdu = -1; 1999 fdc->state = FINDWORK; 2000 } 2001 return (1); 2002 case RESETCTLR: 2003 fdc_reset(fdc); 2004 fdc->retry++; 2005 fdc->state = RESETCOMPLETE; 2006 return (0); 2007 case RESETCOMPLETE: 2008 /* 2009 * Discard all the results from the reset so that they 2010 * can't cause an unexpected interrupt later. 2011 */ 2012 for (i = 0; i < 4; i++) 2013 (void)fd_sense_int(fdc, &st0, &cyl); 2014 fdc->state = STARTRECAL; 2015 /* Fall through. */ 2016 case STARTRECAL: 2017 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) { 2018 /* arrgl */ 2019 fdc->retry = 6; 2020 return (retrier(fdc)); 2021 } 2022 fdc->state = RECALWAIT; 2023 return (0); /* will return later */ 2024 case RECALWAIT: 2025 /* allow heads to settle */ 2026 timeout(fd_pseudointr, fdc, hz / 8); 2027 fdc->state = RECALCOMPLETE; 2028 return (0); /* will return later */ 2029 case RECALCOMPLETE: 2030 do { 2031 /* 2032 * See SEEKCOMPLETE for a comment on this: 2033 */ 2034 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 2035 return 0; 2036 if(fdc->fdct == FDC_NE765 2037 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 2038 return 0; /* hope for a real intr */ 2039 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 2040 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) 2041 { 2042 if(fdc->retry > 3) 2043 /* 2044 * a recalibrate from beyond cylinder 77 2045 * will "fail" due to the FDC limitations; 2046 * since people used to complain much about 2047 * the failure message, try not logging 2048 * this one if it seems to be the first 2049 * time in a line 2050 */ 2051 printf("fd%d: recal failed ST0 %b cyl %d\n", 2052 fdu, st0, NE7_ST0BITS, cyl); 2053 if(fdc->retry < 3) fdc->retry = 3; 2054 return (retrier(fdc)); 2055 } 2056 fd->track = 0; 2057 /* Seek (probably) necessary */ 2058 fdc->state = DOSEEK; 2059 return (1); /* will return immediatly */ 2060 case MOTORWAIT: 2061 if(fd->flags & FD_MOTOR_WAIT) 2062 { 2063 return (0); /* time's not up yet */ 2064 } 2065 if (fdc->flags & FDC_NEEDS_RESET) { 2066 fdc->state = RESETCTLR; 2067 fdc->flags &= ~FDC_NEEDS_RESET; 2068 } else { 2069 /* 2070 * If all motors were off, then the controller was 2071 * reset, so it has lost track of the current 2072 * cylinder. Recalibrate to handle this case. 2073 * But first, discard the results of the reset. 2074 */ 2075 fdc->state = RESETCOMPLETE; 2076 } 2077 return (1); /* will return immediatly */ 2078 default: 2079 device_print_prettyname(fdc->fdc_dev); 2080 printf("unexpected FD int->"); 2081 if (fd_read_status(fdc, fd->fdsu) == 0) 2082 printf("FDC status :%x %x %x %x %x %x %x ", 2083 fdc->status[0], 2084 fdc->status[1], 2085 fdc->status[2], 2086 fdc->status[3], 2087 fdc->status[4], 2088 fdc->status[5], 2089 fdc->status[6] ); 2090 else 2091 printf("No status available "); 2092 if (fd_sense_int(fdc, &st0, &cyl) != 0) 2093 { 2094 printf("[controller is dead now]\n"); 2095 return (0); 2096 } 2097 printf("ST0 = %x, PCN = %x\n", st0, cyl); 2098 return (0); 2099 } 2100 /*XXX confusing: some branches return immediately, others end up here*/ 2101 return (1); /* Come back immediatly to new state */ 2102 } 2103 2104 static int 2105 retrier(struct fdc_data *fdc) 2106 { 2107 register struct buf *bp; 2108 struct fd_data *fd; 2109 int fdu; 2110 2111 bp = fdc->bp; 2112 2113 /* XXX shouldn't this be cached somewhere? */ 2114 fdu = FDUNIT(minor(bp->b_dev)); 2115 fd = devclass_get_softc(fd_devclass, fdu); 2116 if (fd->options & FDOPT_NORETRY) 2117 goto fail; 2118 2119 switch (fdc->retry) { 2120 case 0: case 1: case 2: 2121 fdc->state = SEEKCOMPLETE; 2122 break; 2123 case 3: case 4: case 5: 2124 fdc->state = STARTRECAL; 2125 break; 2126 case 6: 2127 fdc->state = RESETCTLR; 2128 break; 2129 case 7: 2130 break; 2131 default: 2132 fail: 2133 { 2134 dev_t sav_b_dev = bp->b_dev; 2135 /* Trick diskerr */ 2136 bp->b_dev = makedev(major(bp->b_dev), 2137 (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART); 2138 diskerr(bp, "fd", "hard error", LOG_PRINTF, 2139 fdc->fd->skip / DEV_BSIZE, 2140 (struct disklabel *)NULL); 2141 bp->b_dev = sav_b_dev; 2142 if (fdc->flags & FDC_STAT_VALID) 2143 { 2144 printf( 2145 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n", 2146 fdc->status[0], NE7_ST0BITS, 2147 fdc->status[1], NE7_ST1BITS, 2148 fdc->status[2], NE7_ST2BITS, 2149 fdc->status[3], fdc->status[4], 2150 fdc->status[5]); 2151 } 2152 else 2153 printf(" (No status)\n"); 2154 } 2155 bp->b_flags |= B_ERROR; 2156 bp->b_error = EIO; 2157 bp->b_resid += bp->b_bcount - fdc->fd->skip; 2158 fdc->bp = NULL; 2159 2160 /* Tell devstat we have finished with the transaction */ 2161 devstat_end_transaction(&fdc->fd->device_stats, 2162 bp->b_bcount - bp->b_resid, 2163 DEVSTAT_TAG_NONE, 2164 (bp->b_flags & B_READ) ? DEVSTAT_READ : 2165 DEVSTAT_WRITE); 2166 fdc->fd->skip = 0; 2167 biodone(bp); 2168 fdc->state = FINDWORK; 2169 fdc->flags |= FDC_NEEDS_RESET; 2170 fdc->fd = (fd_p) 0; 2171 fdc->fdu = -1; 2172 return (1); 2173 } 2174 fdc->retry++; 2175 return (1); 2176 } 2177 2178 static int 2179 fdformat(dev, finfo, p) 2180 dev_t dev; 2181 struct fd_formb *finfo; 2182 struct proc *p; 2183 { 2184 fdu_t fdu; 2185 fd_p fd; 2186 2187 struct buf *bp; 2188 int rv = 0, s; 2189 size_t fdblk; 2190 2191 fdu = FDUNIT(minor(dev)); 2192 fd = devclass_get_softc(fd_devclass, fdu); 2193 fdblk = 128 << fd->ft->secsize; 2194 2195 /* set up a buffer header for fdstrategy() */ 2196 bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT); 2197 if(bp == 0) 2198 return ENOBUFS; 2199 /* 2200 * keep the process from being swapped 2201 */ 2202 PHOLD(p); 2203 bzero((void *)bp, sizeof(struct buf)); 2204 BUF_LOCKINIT(bp); 2205 BUF_LOCK(bp, LK_EXCLUSIVE); 2206 bp->b_flags = B_PHYS | B_FORMAT; 2207 2208 /* 2209 * calculate a fake blkno, so fdstrategy() would initiate a 2210 * seek to the requested cylinder 2211 */ 2212 bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) 2213 + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE; 2214 2215 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs; 2216 bp->b_data = (caddr_t)finfo; 2217 2218 /* now do the format */ 2219 bp->b_dev = dev; 2220 BUF_STRATEGY(bp, 0); 2221 2222 /* ...and wait for it to complete */ 2223 s = splbio(); 2224 while(!(bp->b_flags & B_DONE)) { 2225 rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz); 2226 if (rv == EWOULDBLOCK) 2227 break; 2228 } 2229 splx(s); 2230 2231 if (rv == EWOULDBLOCK) { 2232 /* timed out */ 2233 rv = EIO; 2234 biodone(bp); 2235 } 2236 if (bp->b_flags & B_ERROR) 2237 rv = bp->b_error; 2238 /* 2239 * allow the process to be swapped 2240 */ 2241 PRELE(p); 2242 BUF_UNLOCK(bp); 2243 BUF_LOCKFREE(bp); 2244 free(bp, M_TEMP); 2245 return rv; 2246 } 2247 2248 /* 2249 * TODO: don't allocate buffer on stack. 2250 */ 2251 2252 static int 2253 fdioctl(dev, cmd, addr, flag, p) 2254 dev_t dev; 2255 u_long cmd; 2256 caddr_t addr; 2257 int flag; 2258 struct proc *p; 2259 { 2260 fdu_t fdu = FDUNIT(minor(dev)); 2261 fd_p fd = devclass_get_softc(fd_devclass, fdu); 2262 size_t fdblk; 2263 2264 struct fd_type *fdt; 2265 struct disklabel *dl; 2266 char buffer[DEV_BSIZE]; 2267 int error = 0; 2268 2269 fdblk = 128 << fd->ft->secsize; 2270 2271 switch (cmd) { 2272 case DIOCGDINFO: 2273 bzero(buffer, sizeof (buffer)); 2274 dl = (struct disklabel *)buffer; 2275 dl->d_secsize = fdblk; 2276 fdt = fd->ft; 2277 dl->d_secpercyl = fdt->size / fdt->tracks; 2278 dl->d_type = DTYPE_FLOPPY; 2279 2280 if (readdisklabel(dkmodpart(dev, RAW_PART), dl) 2281 == NULL) 2282 error = 0; 2283 else 2284 error = EINVAL; 2285 2286 *(struct disklabel *)addr = *dl; 2287 break; 2288 2289 case DIOCSDINFO: 2290 if ((flag & FWRITE) == 0) 2291 error = EBADF; 2292 break; 2293 2294 case DIOCWLABEL: 2295 if ((flag & FWRITE) == 0) 2296 error = EBADF; 2297 break; 2298 2299 case DIOCWDINFO: 2300 if ((flag & FWRITE) == 0) { 2301 error = EBADF; 2302 break; 2303 } 2304 2305 dl = (struct disklabel *)addr; 2306 2307 if ((error = setdisklabel((struct disklabel *)buffer, dl, 2308 (u_long)0)) != 0) 2309 break; 2310 2311 error = writedisklabel(dev, (struct disklabel *)buffer); 2312 break; 2313 case FD_FORM: 2314 if ((flag & FWRITE) == 0) 2315 error = EBADF; /* must be opened for writing */ 2316 else if (((struct fd_formb *)addr)->format_version != 2317 FD_FORMAT_VERSION) 2318 error = EINVAL; /* wrong version of formatting prog */ 2319 else 2320 error = fdformat(dev, (struct fd_formb *)addr, p); 2321 break; 2322 2323 case FD_GTYPE: /* get drive type */ 2324 *(struct fd_type *)addr = *fd->ft; 2325 break; 2326 2327 case FD_STYPE: /* set drive type */ 2328 /* this is considered harmful; only allow for superuser */ 2329 if (suser(p) != 0) 2330 return EPERM; 2331 *fd->ft = *(struct fd_type *)addr; 2332 break; 2333 2334 case FD_GOPTS: /* get drive options */ 2335 *(int *)addr = fd->options; 2336 break; 2337 2338 case FD_SOPTS: /* set drive options */ 2339 fd->options = *(int *)addr; 2340 break; 2341 2342 default: 2343 error = ENOTTY; 2344 break; 2345 } 2346 return (error); 2347 } 2348 2349 static device_method_t fdc_methods[] = { 2350 /* Device interface */ 2351 DEVMETHOD(device_probe, fdc_probe), 2352 DEVMETHOD(device_attach, fdc_attach), 2353 DEVMETHOD(device_detach, bus_generic_detach), 2354 DEVMETHOD(device_shutdown, bus_generic_shutdown), 2355 DEVMETHOD(device_suspend, bus_generic_suspend), 2356 DEVMETHOD(device_resume, bus_generic_resume), 2357 2358 /* Bus interface */ 2359 DEVMETHOD(bus_print_child, fdc_print_child), 2360 /* Our children never use any other bus interface methods. */ 2361 2362 { 0, 0 } 2363 }; 2364 2365 static driver_t fdc_driver = { 2366 "fdc", 2367 fdc_methods, 2368 sizeof(struct fdc_data) 2369 }; 2370 2371 DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0); 2372 2373 static device_method_t fd_methods[] = { 2374 /* Device interface */ 2375 DEVMETHOD(device_probe, fd_probe), 2376 DEVMETHOD(device_attach, fd_attach), 2377 DEVMETHOD(device_detach, bus_generic_detach), 2378 DEVMETHOD(device_shutdown, bus_generic_shutdown), 2379 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */ 2380 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */ 2381 2382 { 0, 0 } 2383 }; 2384 2385 static driver_t fd_driver = { 2386 "fd", 2387 fd_methods, 2388 sizeof(struct fd_data) 2389 }; 2390 2391 DEV_DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, fd_cdevsw, 0, 0); 2392 2393 #endif /* NFDC > 0 */ 2394 2395 /* 2396 * Hello emacs, these are the 2397 * Local Variables: 2398 * c-indent-level: 8 2399 * c-continued-statement-offset: 8 2400 * c-continued-brace-offset: 0 2401 * c-brace-offset: -8 2402 * c-brace-imaginary-offset: 0 2403 * c-argdecl-indent: 8 2404 * c-label-offset: -8 2405 * c++-hanging-braces: 1 2406 * c++-access-specifier-offset: -8 2407 * c++-empty-arglist-indent: 8 2408 * c++-friend-offset: 0 2409 * End: 2410 */ 2411