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