1 /* $OpenBSD: fdc.c,v 1.18 2010/11/18 21:13:19 miod 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(parent, match, aux) 97 struct device *parent; 98 void *match, *aux; 99 { 100 register struct isa_attach_args *ia = aux; 101 bus_space_tag_t iot; 102 bus_space_handle_t ioh; 103 bus_space_handle_t ioh_ctl; 104 int rv; 105 106 iot = ia->ia_iot; 107 rv = 0; 108 109 /* Map the i/o space. */ 110 if (bus_space_map(iot, ia->ia_iobase, FDC_NPORT, 0, &ioh)) 111 return 0; 112 if (bus_space_map(iot, ia->ia_iobase + FDCTL_OFFSET, 113 FDCTL_NPORT, 0, &ioh_ctl)) 114 return 0; 115 116 /* reset */ 117 bus_space_write_1(iot, ioh, fdout, 0); 118 delay(100); 119 bus_space_write_1(iot, ioh, fdout, FDO_FRST); 120 121 /* see if it can handle a command */ 122 if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0) 123 goto out; 124 out_fdc(iot, ioh, 0xdf); 125 out_fdc(iot, ioh, 2); 126 127 rv = 1; 128 ia->ia_iosize = FDC_NPORT; 129 ia->ia_msize = 0; 130 131 out: 132 bus_space_unmap(iot, ioh, FDC_NPORT); 133 bus_space_unmap(iot, ioh_ctl, FDCTL_NPORT); 134 return rv; 135 } 136 137 void 138 fdcattach(parent, self, aux) 139 struct device *parent, *self; 140 void *aux; 141 { 142 struct fdc_softc *fdc = (void *)self; 143 bus_space_tag_t iot; 144 bus_space_handle_t ioh; 145 bus_space_handle_t ioh_ctl; 146 struct isa_attach_args *ia = aux; 147 struct fdc_attach_args fa; 148 int type; 149 150 iot = ia->ia_iot; 151 152 /* Re-map the I/O space. */ 153 if (bus_space_map(iot, ia->ia_iobase, FDC_NPORT, 0, &ioh) || 154 bus_space_map(iot, ia->ia_iobase + FDCTL_OFFSET, 155 FDCTL_NPORT, 0, &ioh_ctl)) 156 panic("fdcattach: couldn't map I/O ports"); 157 158 fdc->sc_iot = iot; 159 fdc->sc_ioh = ioh; 160 fdc->sc_ioh_ctl = ioh_ctl; 161 162 fdc->sc_drq = ia->ia_drq; 163 fdc->sc_state = DEVIDLE; 164 TAILQ_INIT(&fdc->sc_link.fdlink.sc_drives); /* XXX */ 165 166 printf("\n"); 167 168 fdc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE, 169 IPL_BIO, fdcintr, fdc, fdc->sc_dev.dv_xname); 170 171 #if defined(__i386__) || defined(__amd64__) 172 /* 173 * The NVRAM info only tells us about the first two disks on the 174 * `primary' floppy controller. 175 */ 176 if (fdc->sc_dev.dv_unit == 0) 177 type = mc146818_read(NULL, NVRAM_DISKETTE); /* XXX softc */ 178 else 179 #endif 180 type = -1; 181 182 timeout_set(&fdc->fdcpseudointr_to, fdcpseudointr, fdc); 183 184 /* physical limit: four drives per controller. */ 185 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) { 186 fa.fa_flags = 0; 187 fa.fa_type = 0; 188 #if NFD > 0 189 if (type >= 0 && fa.fa_drive < 2) 190 fa.fa_deftype = fd_nvtotype(fdc->sc_dev.dv_xname, 191 type, fa.fa_drive); 192 else 193 #endif 194 fa.fa_deftype = NULL; /* unknown */ 195 (void)config_found(self, (void *)&fa, fddprint); 196 } 197 } 198 199 /* 200 * Print the location of a disk/tape drive (called just before attaching the 201 * the drive). If `fdc' is not NULL, the drive was found but was not 202 * in the system config file; print the drive name as well. 203 * Return QUIET (config_find ignores this if the device was configured) to 204 * avoid printing `fdN not configured' messages. 205 */ 206 int 207 fddprint(aux, fdc) 208 void *aux; 209 const char *fdc; 210 { 211 register struct fdc_attach_args *fa = aux; 212 213 if (!fdc) 214 printf(" drive %d", fa->fa_drive); 215 return QUIET; 216 } 217 218 int 219 fdcresult(fdc) 220 struct fdc_softc *fdc; 221 { 222 bus_space_tag_t iot = fdc->sc_iot; 223 bus_space_handle_t ioh = fdc->sc_ioh; 224 u_char i; 225 int j = 100000, n = 0; 226 227 for (; j; j--) { 228 i = bus_space_read_1(iot, ioh, fdsts) & 229 (NE7_DIO | NE7_RQM | NE7_CB); 230 if (i == NE7_RQM) 231 return n; 232 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) { 233 if (n >= sizeof(fdc->sc_status)) { 234 log(LOG_ERR, "fdcresult: overrun\n"); 235 return -1; 236 } 237 fdc->sc_status[n++] = 238 bus_space_read_1(iot, ioh, fddata); 239 } 240 delay(10); 241 } 242 return -1; 243 } 244 245 int 246 out_fdc(iot, ioh, x) 247 bus_space_tag_t iot; 248 bus_space_handle_t ioh; 249 u_char x; 250 { 251 int i = 100000; 252 253 while ((bus_space_read_1(iot, ioh, fdsts) & NE7_DIO) && i-- > 0); 254 if (i <= 0) 255 return -1; 256 while ((bus_space_read_1(iot, ioh, fdsts) & NE7_RQM) == 0 && i-- > 0); 257 if (i <= 0) 258 return -1; 259 bus_space_write_1(iot, ioh, fddata, x); 260 return 0; 261 } 262 263 void 264 fdcstart(fdc) 265 struct fdc_softc *fdc; 266 { 267 268 #ifdef DIAGNOSTIC 269 /* only got here if controller's drive queue was inactive; should 270 be in idle state */ 271 if (fdc->sc_state != DEVIDLE) { 272 printf("fdcstart: not idle\n"); 273 return; 274 } 275 #endif 276 (void) fdcintr(fdc); 277 } 278 279 void 280 fdcstatus(dv, n, s) 281 struct device *dv; 282 int n; 283 char *s; 284 { 285 struct fdc_softc *fdc = (void *)dv->dv_parent; 286 287 if (n == 0) { 288 out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI); 289 (void) fdcresult(fdc); 290 n = 2; 291 } 292 293 printf("%s: %s", dv->dv_xname, s); 294 295 switch (n) { 296 case 0: 297 printf("\n"); 298 break; 299 case 2: 300 printf(" (st0 %b cyl %d)\n", 301 fdc->sc_status[0], NE7_ST0BITS, 302 fdc->sc_status[1]); 303 break; 304 case 7: 305 printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n", 306 fdc->sc_status[0], NE7_ST0BITS, 307 fdc->sc_status[1], NE7_ST1BITS, 308 fdc->sc_status[2], NE7_ST2BITS, 309 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]); 310 break; 311 #ifdef DIAGNOSTIC 312 default: 313 printf("\nfdcstatus: weird size"); 314 break; 315 #endif 316 } 317 } 318 319 void 320 fdcpseudointr(arg) 321 void *arg; 322 { 323 int s; 324 325 /* Just ensure it has the right spl. */ 326 s = splbio(); 327 (void) fdcintr(arg); 328 splx(s); 329 } 330 331 int 332 fdcintr(arg) 333 void *arg; 334 { 335 #if NFD > 0 336 struct fdc_softc *fdc = arg; 337 extern int fdintr(struct fdc_softc *); 338 339 /* Will switch on device type, shortly. */ 340 return (fdintr(fdc)); 341 #else 342 printf("fdcintr: got interrupt, but no devices!\n"); 343 return (1); 344 #endif 345 } 346