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