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