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