1 /*- 2 * Copyright (c) 2004-2005 M. Warner Losh. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/bio.h> 32 #include <sys/bus.h> 33 #include <sys/kernel.h> 34 #include <sys/lock.h> 35 #include <sys/module.h> 36 #include <sys/mutex.h> 37 #include <sys/rman.h> 38 #include <sys/systm.h> 39 40 #include <machine/bus.h> 41 42 #include <dev/fdc/fdcvar.h> 43 44 #include <isa/isavar.h> 45 #include <isa/isareg.h> 46 47 static int fdc_isa_probe(device_t); 48 49 static struct isa_pnp_id fdc_ids[] = { 50 {0x0007d041, "PC standard floppy controller"}, /* PNP0700 */ 51 {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */ 52 {0} 53 }; 54 55 /* 56 * On standard ISA, we don't just use an 8 port range 57 * (e.g. 0x3f0-0x3f7) since that covers an IDE control register at 58 * 0x3f6. So, on older hardware, we use 0x3f0-0x3f5 and 0x3f7. 59 * However, some BIOSs omit the control port, while others start at 60 * 0x3f2. Of the latter, sometimes we have two resources, other times 61 * we have one. We have to deal with the following cases: 62 * 63 * 1: 0x3f0-0x3f5 # very rare 64 * 2: 0x3f0 # hints -> 0x3f0-0x3f5,0x3f7 65 * 3: 0x3f0-0x3f5,0x3f7 # Most common 66 * 4: 0x3f2-0x3f5,0x3f7 # Second most common 67 * 5: 0x3f2-0x3f5 # implies 0x3f7 too. 68 * 6: 0x3f2-0x3f3,0x3f4-0x3f5,0x3f7 # becoming common 69 * 7: 0x3f2-0x3f3,0x3f4-0x3f5 # rare 70 * 8: 0x3f0-0x3f1,0x3f2-0x3f3,0x3f4-0x3f5,0x3f7 71 * 9: 0x3f0-0x3f3,0x3f4-0x3f5,0x3f7 72 * 73 * The following code is generic for any value of 0x3fx. It is also 74 * generic for all the above cases, as well as cases where things are 75 * even weirder. 76 */ 77 int 78 fdc_isa_alloc_resources(device_t dev, struct fdc_data *fdc) 79 { 80 struct resource *res; 81 int nports = 6; 82 int i, j, rid, newrid; 83 84 fdc->fdc_dev = dev; 85 rid = 0; 86 for (i = 0; i < FDC_MAXREG; i++) 87 fdc->resio[i] = NULL; 88 89 for (rid = 0; ; rid++) { 90 newrid = rid; 91 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &newrid, 92 0ul, ~0ul, nports, RF_ACTIVE); 93 if (res == NULL) 94 break; 95 /* 96 * Mask off the upper bits of the register, and sanity 97 * check resource ranges. 98 */ 99 i = rman_get_start(res) & 0x7; 100 if (i + rman_get_size(res) - 1 > FDC_MAXREG) 101 return (ENXIO); 102 for (j = 0; j < rman_get_size(res); j++) { 103 fdc->resio[i + j] = res; 104 fdc->ridio[i + j] = newrid; 105 fdc->ioff[i + j] = j; 106 fdc->ioh[i + j] = rman_get_bushandle(res); 107 } 108 } 109 if (fdc->resio[2] == NULL) 110 return (ENXIO); 111 fdc->iot = rman_get_bustag(fdc->resio[2]); 112 if (fdc->resio[7] == NULL) { 113 /* XXX allocate */ 114 fdc->resio[7] = fdc->resio[2]; 115 fdc->ridio[7] = fdc->ridio[2]; 116 fdc->ioff[7] = fdc->ioff[2] + 5; 117 fdc->ioh[7] = fdc->ioh[2]; 118 } 119 120 fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq, 121 RF_ACTIVE | RF_SHAREABLE); 122 if (fdc->res_irq == NULL) { 123 device_printf(dev, "cannot reserve interrupt line\n"); 124 return (ENXIO); 125 } 126 127 if ((fdc->flags & FDC_NODMA) == 0) { 128 fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ, 129 &fdc->rid_drq, RF_ACTIVE | RF_SHAREABLE); 130 if (fdc->res_drq == NULL) { 131 device_printf(dev, "cannot reserve DMA request line\n"); 132 /* This is broken and doesn't work for ISA case */ 133 fdc->flags |= FDC_NODMA; 134 } else 135 fdc->dmachan = rman_get_start(fdc->res_drq); 136 } 137 138 return (0); 139 } 140 141 static int 142 fdc_isa_probe(device_t dev) 143 { 144 int error; 145 struct fdc_data *fdc; 146 147 fdc = device_get_softc(dev); 148 fdc->fdc_dev = dev; 149 150 /* Check pnp ids */ 151 error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 152 if (error == ENXIO) 153 return (ENXIO); 154 155 /* Attempt to allocate our resources for the duration of the probe */ 156 error = fdc_isa_alloc_resources(dev, fdc); 157 if (error == 0) 158 error = fdc_initial_reset(dev, fdc); 159 160 fdc_release_resources(fdc); 161 return (error); 162 } 163 164 static int 165 fdc_isa_attach(device_t dev) 166 { 167 struct fdc_data *fdc; 168 int error; 169 170 fdc = device_get_softc(dev); 171 fdc->fdc_dev = dev; 172 error = fdc_isa_alloc_resources(dev, fdc); 173 if (error == 0) 174 error = fdc_attach(dev); 175 if (error == 0) 176 error = fdc_hints_probe(dev); 177 if (error) 178 fdc_release_resources(fdc); 179 return (error); 180 } 181 182 static device_method_t fdc_methods[] = { 183 /* Device interface */ 184 DEVMETHOD(device_probe, fdc_isa_probe), 185 DEVMETHOD(device_attach, fdc_isa_attach), 186 DEVMETHOD(device_detach, fdc_detach), 187 DEVMETHOD(device_shutdown, bus_generic_shutdown), 188 DEVMETHOD(device_suspend, bus_generic_suspend), 189 DEVMETHOD(device_resume, bus_generic_resume), 190 191 /* Bus interface */ 192 DEVMETHOD(bus_print_child, fdc_print_child), 193 DEVMETHOD(bus_read_ivar, fdc_read_ivar), 194 DEVMETHOD(bus_write_ivar, fdc_write_ivar), 195 /* Our children never use any other bus interface methods. */ 196 197 { 0, 0 } 198 }; 199 200 static driver_t fdc_driver = { 201 "fdc", 202 fdc_methods, 203 sizeof(struct fdc_data) 204 }; 205 206 DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0); 207