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