1 /* $OpenBSD: fdc.c,v 1.19 2011/07/04 05:41:48 matthew Exp $ */ 2 /* $NetBSD: fd.c,v 1.90 1996/05/12 23:12:03 mycroft Exp $ */ 3 4 /*- 5 * Copyright (c) 1993, 1994, 1995 Charles Hannum. 6 * Copyright (c) 1990 The Regents of the University of California. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * Don Ahn. 11 * 12 * Portions 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 * joerg_wunsch@uriah.sax.de (Joerg Wunsch) 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 1. Redistributions of source code must retain the above copyright 22 * notice, this list of conditions and the following disclaimer. 23 * 2. Redistributions in binary form must reproduce the above copyright 24 * notice, this list of conditions and the following disclaimer in the 25 * documentation and/or other materials provided with the distribution. 26 * 3. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 * 42 * @(#)fd.c 7.4 (Berkeley) 5/25/91 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/file.h> 49 #include <sys/ioctl.h> 50 #include <sys/device.h> 51 #include <sys/disklabel.h> 52 #include <sys/disk.h> 53 #include <sys/buf.h> 54 #include <sys/malloc.h> 55 #include <sys/uio.h> 56 #include <sys/mtio.h> 57 #include <sys/syslog.h> 58 #include <sys/queue.h> 59 #include <sys/timeout.h> 60 61 #include <machine/cpu.h> 62 #include <machine/bus.h> 63 #include <machine/conf.h> 64 #include <machine/intr.h> 65 #include <machine/ioctl_fd.h> 66 67 #include <dev/isa/isavar.h> 68 #include <dev/isa/isadmavar.h> 69 #include <dev/isa/fdreg.h> 70 71 #if defined(__i386__) || defined(__amd64__) /* XXX */ 72 #include <dev/ic/mc146818reg.h> /* for NVRAM access */ 73 #include <i386/isa/nvram.h> 74 #endif 75 76 #include <dev/isa/fdlink.h> 77 78 #include "fd.h" 79 80 /* controller driver configuration */ 81 int fdcprobe(struct device *, void *, void *); 82 void fdcattach(struct device *, struct device *, void *); 83 84 struct cfattach fdc_ca = { 85 sizeof(struct fdc_softc), fdcprobe, fdcattach 86 }; 87 88 struct cfdriver fdc_cd = { 89 NULL, "fdc", DV_DULL 90 }; 91 92 int fddprint(void *, const char *); 93 int fdcintr(void *); 94 95 int 96 fdcprobe(struct device *parent, void *match, void *aux) 97 { 98 register struct isa_attach_args *ia = aux; 99 bus_space_tag_t iot; 100 bus_space_handle_t ioh; 101 bus_space_handle_t ioh_ctl; 102 int rv; 103 104 iot = ia->ia_iot; 105 rv = 0; 106 107 /* Map the i/o space. */ 108 if (bus_space_map(iot, ia->ia_iobase, FDC_NPORT, 0, &ioh)) 109 return 0; 110 if (bus_space_map(iot, ia->ia_iobase + FDCTL_OFFSET, 111 FDCTL_NPORT, 0, &ioh_ctl)) 112 return 0; 113 114 /* reset */ 115 bus_space_write_1(iot, ioh, fdout, 0); 116 delay(100); 117 bus_space_write_1(iot, ioh, fdout, FDO_FRST); 118 119 /* see if it can handle a command */ 120 if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0) 121 goto out; 122 out_fdc(iot, ioh, 0xdf); 123 out_fdc(iot, ioh, 2); 124 125 rv = 1; 126 ia->ia_iosize = FDC_NPORT; 127 ia->ia_msize = 0; 128 129 out: 130 bus_space_unmap(iot, ioh, FDC_NPORT); 131 bus_space_unmap(iot, ioh_ctl, FDCTL_NPORT); 132 return rv; 133 } 134 135 void 136 fdcattach(struct device *parent, struct device *self, void *aux) 137 { 138 struct fdc_softc *fdc = (void *)self; 139 bus_space_tag_t iot; 140 bus_space_handle_t ioh; 141 bus_space_handle_t ioh_ctl; 142 struct isa_attach_args *ia = aux; 143 struct fdc_attach_args fa; 144 int type; 145 146 iot = ia->ia_iot; 147 148 /* Re-map the I/O space. */ 149 if (bus_space_map(iot, ia->ia_iobase, FDC_NPORT, 0, &ioh) || 150 bus_space_map(iot, ia->ia_iobase + FDCTL_OFFSET, 151 FDCTL_NPORT, 0, &ioh_ctl)) 152 panic("fdcattach: couldn't map I/O ports"); 153 154 fdc->sc_iot = iot; 155 fdc->sc_ioh = ioh; 156 fdc->sc_ioh_ctl = ioh_ctl; 157 158 fdc->sc_drq = ia->ia_drq; 159 fdc->sc_state = DEVIDLE; 160 TAILQ_INIT(&fdc->sc_link.fdlink.sc_drives); /* XXX */ 161 162 printf("\n"); 163 164 fdc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE, 165 IPL_BIO, fdcintr, fdc, fdc->sc_dev.dv_xname); 166 167 #if defined(__i386__) || defined(__amd64__) 168 /* 169 * The NVRAM info only tells us about the first two disks on the 170 * `primary' floppy controller. 171 */ 172 if (fdc->sc_dev.dv_unit == 0) 173 type = mc146818_read(NULL, NVRAM_DISKETTE); /* XXX softc */ 174 else 175 #endif 176 type = -1; 177 178 timeout_set(&fdc->fdcpseudointr_to, fdcpseudointr, fdc); 179 180 /* physical limit: four drives per controller. */ 181 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) { 182 fa.fa_flags = 0; 183 fa.fa_type = 0; 184 #if NFD > 0 185 if (type >= 0 && fa.fa_drive < 2) 186 fa.fa_deftype = fd_nvtotype(fdc->sc_dev.dv_xname, 187 type, fa.fa_drive); 188 else 189 #endif 190 fa.fa_deftype = NULL; /* unknown */ 191 (void)config_found(self, (void *)&fa, fddprint); 192 } 193 } 194 195 /* 196 * Print the location of a disk/tape drive (called just before attaching the 197 * the drive). If `fdc' is not NULL, the drive was found but was not 198 * in the system config file; print the drive name as well. 199 * Return QUIET (config_find ignores this if the device was configured) to 200 * avoid printing `fdN not configured' messages. 201 */ 202 int 203 fddprint(void *aux, const char *fdc) 204 { 205 register struct fdc_attach_args *fa = aux; 206 207 if (!fdc) 208 printf(" drive %d", fa->fa_drive); 209 return QUIET; 210 } 211 212 int 213 fdcresult(struct fdc_softc *fdc) 214 { 215 bus_space_tag_t iot = fdc->sc_iot; 216 bus_space_handle_t ioh = fdc->sc_ioh; 217 u_char i; 218 int j = 100000, n = 0; 219 220 for (; j; j--) { 221 i = bus_space_read_1(iot, ioh, fdsts) & 222 (NE7_DIO | NE7_RQM | NE7_CB); 223 if (i == NE7_RQM) 224 return n; 225 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) { 226 if (n >= sizeof(fdc->sc_status)) { 227 log(LOG_ERR, "fdcresult: overrun\n"); 228 return -1; 229 } 230 fdc->sc_status[n++] = 231 bus_space_read_1(iot, ioh, fddata); 232 } 233 delay(10); 234 } 235 return -1; 236 } 237 238 int 239 out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, u_char x) 240 { 241 int i = 100000; 242 243 while ((bus_space_read_1(iot, ioh, fdsts) & NE7_DIO) && i-- > 0); 244 if (i <= 0) 245 return -1; 246 while ((bus_space_read_1(iot, ioh, fdsts) & NE7_RQM) == 0 && i-- > 0); 247 if (i <= 0) 248 return -1; 249 bus_space_write_1(iot, ioh, fddata, x); 250 return 0; 251 } 252 253 void 254 fdcstart(struct fdc_softc *fdc) 255 { 256 257 #ifdef DIAGNOSTIC 258 /* only got here if controller's drive queue was inactive; should 259 be in idle state */ 260 if (fdc->sc_state != DEVIDLE) { 261 printf("fdcstart: not idle\n"); 262 return; 263 } 264 #endif 265 (void) fdcintr(fdc); 266 } 267 268 void 269 fdcstatus(struct device *dv, int n, char *s) 270 { 271 struct fdc_softc *fdc = (void *)dv->dv_parent; 272 273 if (n == 0) { 274 out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI); 275 (void) fdcresult(fdc); 276 n = 2; 277 } 278 279 printf("%s: %s", dv->dv_xname, s); 280 281 switch (n) { 282 case 0: 283 printf("\n"); 284 break; 285 case 2: 286 printf(" (st0 %b cyl %d)\n", 287 fdc->sc_status[0], NE7_ST0BITS, 288 fdc->sc_status[1]); 289 break; 290 case 7: 291 printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n", 292 fdc->sc_status[0], NE7_ST0BITS, 293 fdc->sc_status[1], NE7_ST1BITS, 294 fdc->sc_status[2], NE7_ST2BITS, 295 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]); 296 break; 297 #ifdef DIAGNOSTIC 298 default: 299 printf("\nfdcstatus: weird size"); 300 break; 301 #endif 302 } 303 } 304 305 void 306 fdcpseudointr(void *arg) 307 { 308 int s; 309 310 /* Just ensure it has the right spl. */ 311 s = splbio(); 312 (void) fdcintr(arg); 313 splx(s); 314 } 315 316 int 317 fdcintr(void *arg) 318 { 319 #if NFD > 0 320 struct fdc_softc *fdc = arg; 321 extern int fdintr(struct fdc_softc *); 322 323 /* Will switch on device type, shortly. */ 324 return (fdintr(fdc)); 325 #else 326 printf("fdcintr: got interrupt, but no devices!\n"); 327 return (1); 328 #endif 329 } 330