10bdf1a55SNate Lawson /*- 2973bfe6cSWarner Losh * Copyright (c) 2004-2005 M. Warner Losh. 36d6fa4fdSWarner Losh * All rights reserved. 46d6fa4fdSWarner Losh * 56d6fa4fdSWarner Losh * Redistribution and use in source and binary forms, with or without 66d6fa4fdSWarner Losh * modification, are permitted provided that the following conditions 76d6fa4fdSWarner Losh * are met: 86d6fa4fdSWarner Losh * 1. Redistributions of source code must retain the above copyright 9ac673f9aSWarner Losh * notice, this list of conditions and the following disclaimer. 106d6fa4fdSWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 11ac673f9aSWarner Losh * notice, this list of conditions and the following disclaimer in the 12ac673f9aSWarner Losh * documentation and/or other materials provided with the distribution. 136d6fa4fdSWarner Losh * 146d6fa4fdSWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 156d6fa4fdSWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 166d6fa4fdSWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17ac673f9aSWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18ac673f9aSWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 196d6fa4fdSWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 206d6fa4fdSWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 216d6fa4fdSWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 226d6fa4fdSWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 236d6fa4fdSWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 246d6fa4fdSWarner Losh * SUCH DAMAGE. 256d6fa4fdSWarner Losh */ 266d6fa4fdSWarner Losh 276d6fa4fdSWarner Losh #include <sys/cdefs.h> 286d6fa4fdSWarner Losh __FBSDID("$FreeBSD$"); 296d6fa4fdSWarner Losh 306d6fa4fdSWarner Losh #include <sys/param.h> 316d6fa4fdSWarner Losh #include <sys/bio.h> 326d6fa4fdSWarner Losh #include <sys/bus.h> 336d6fa4fdSWarner Losh #include <sys/kernel.h> 341b67be7bSPoul-Henning Kamp #include <sys/lock.h> 356d6fa4fdSWarner Losh #include <sys/module.h> 361b67be7bSPoul-Henning Kamp #include <sys/mutex.h> 376d6fa4fdSWarner Losh #include <sys/rman.h> 386d6fa4fdSWarner Losh #include <sys/systm.h> 396d6fa4fdSWarner Losh 40c3ae4c40SWarner Losh #include <machine/bus.h> 41c3ae4c40SWarner Losh 426d6fa4fdSWarner Losh #include <dev/fdc/fdcvar.h> 436d6fa4fdSWarner Losh 446d6fa4fdSWarner Losh #include <isa/isavar.h> 456d6fa4fdSWarner Losh #include <isa/isareg.h> 466d6fa4fdSWarner Losh 476d6fa4fdSWarner Losh static int fdc_isa_probe(device_t); 486d6fa4fdSWarner Losh 496d6fa4fdSWarner Losh static struct isa_pnp_id fdc_ids[] = { 50973bfe6cSWarner Losh {0x0007d041, "PC standard floppy controller"}, /* PNP0700 */ 516d6fa4fdSWarner Losh {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */ 526d6fa4fdSWarner Losh {0} 536d6fa4fdSWarner Losh }; 546d6fa4fdSWarner Losh 55aad64165SWarner Losh /* 56aad64165SWarner Losh * On standard ISA, we don't just use an 8 port range 57973bfe6cSWarner Losh * (e.g. 0x3f0-0x3f7) since that covers an IDE control register at 58973bfe6cSWarner Losh * 0x3f6. So, on older hardware, we use 0x3f0-0x3f5 and 0x3f7. 59973bfe6cSWarner Losh * However, some BIOSs omit the control port, while others start at 60973bfe6cSWarner Losh * 0x3f2. Of the latter, sometimes we have two resources, other times 61973bfe6cSWarner Losh * we have one. We have to deal with the following cases: 62aad64165SWarner Losh * 63b4046cd7SWarner Losh * 1: 0x3f0-0x3f5 # very rare 64b4046cd7SWarner Losh * 2: 0x3f0 # hints -> 0x3f0-0x3f5,0x3f7 65b4046cd7SWarner Losh * 3: 0x3f0-0x3f5,0x3f7 # Most common 66b4046cd7SWarner Losh * 4: 0x3f2-0x3f5,0x3f7 # Second most common 67b4046cd7SWarner Losh * 5: 0x3f2-0x3f5 # implies 0x3f7 too. 68b4046cd7SWarner Losh * 6: 0x3f2-0x3f3,0x3f4-0x3f5,0x3f7 # becoming common 69b4046cd7SWarner Losh * 7: 0x3f2-0x3f3,0x3f4-0x3f5 # rare 70083ba097SWarner Losh * 8: 0x3f0-0x3f1,0x3f2-0x3f3,0x3f4-0x3f5,0x3f7 71534e7194SWarner Losh * 9: 0x3f0-0x3f3,0x3f4-0x3f5,0x3f7 72b4046cd7SWarner Losh * 73973bfe6cSWarner Losh * The following code is generic for any value of 0x3fx. It is also 74973bfe6cSWarner Losh * generic for all the above cases, as well as cases where things are 75973bfe6cSWarner Losh * even weirder. 76aad64165SWarner Losh */ 77973bfe6cSWarner Losh int 78973bfe6cSWarner Losh fdc_isa_alloc_resources(device_t dev, struct fdc_data *fdc) 79973bfe6cSWarner Losh { 80973bfe6cSWarner Losh struct resource *res; 81973bfe6cSWarner Losh int nports = 6; 82973bfe6cSWarner Losh int i, j, rid, newrid; 83aad64165SWarner Losh 84973bfe6cSWarner Losh fdc->fdc_dev = dev; 85973bfe6cSWarner Losh rid = 0; 86973bfe6cSWarner Losh for (i = 0; i < FDC_MAXREG; i++) 87973bfe6cSWarner Losh fdc->resio[i] = NULL; 88973bfe6cSWarner Losh 89973bfe6cSWarner Losh for (rid = 0; ; rid++) { 90973bfe6cSWarner Losh newrid = rid; 91787a196bSWarner Losh res = bus_alloc_resource(dev, SYS_RES_IOPORT, &newrid, 92787a196bSWarner Losh 0ul, ~0ul, nports, RF_ACTIVE); 93973bfe6cSWarner Losh if (res == NULL) 94973bfe6cSWarner Losh break; 95787a196bSWarner Losh /* 96787a196bSWarner Losh * Mask off the upper bits of the register, and sanity 97787a196bSWarner Losh * check resource ranges. 98787a196bSWarner Losh */ 99787a196bSWarner Losh i = rman_get_start(res) & 0x7; 100787a196bSWarner Losh if (i + rman_get_size(res) - 1 > FDC_MAXREG) 101787a196bSWarner Losh return (ENXIO); 102973bfe6cSWarner Losh for (j = 0; j < rman_get_size(res); j++) { 103973bfe6cSWarner Losh fdc->resio[i + j] = res; 104973bfe6cSWarner Losh fdc->ridio[i + j] = newrid; 105973bfe6cSWarner Losh fdc->ioff[i + j] = j; 106973bfe6cSWarner Losh fdc->ioh[i + j] = rman_get_bushandle(res); 107973bfe6cSWarner Losh } 108973bfe6cSWarner Losh } 109973bfe6cSWarner Losh if (fdc->resio[2] == NULL) 110b4046cd7SWarner Losh return (ENXIO); 111973bfe6cSWarner Losh fdc->iot = rman_get_bustag(fdc->resio[2]); 112973bfe6cSWarner Losh if (fdc->resio[7] == NULL) { 113973bfe6cSWarner Losh /* XXX allocate */ 114973bfe6cSWarner Losh fdc->resio[7] = fdc->resio[2]; 115973bfe6cSWarner Losh fdc->ridio[7] = fdc->ridio[2]; 116973bfe6cSWarner Losh fdc->ioff[7] = fdc->ioff[2] + 5; 117973bfe6cSWarner Losh fdc->ioh[7] = fdc->ioh[2]; 118b4046cd7SWarner Losh } 119aad64165SWarner Losh 120aad64165SWarner Losh fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq, 121aad64165SWarner Losh RF_ACTIVE | RF_SHAREABLE); 122973bfe6cSWarner Losh if (fdc->res_irq == NULL) { 123aad64165SWarner Losh device_printf(dev, "cannot reserve interrupt line\n"); 124b4046cd7SWarner Losh return (ENXIO); 125aad64165SWarner Losh } 126aad64165SWarner Losh 127aad64165SWarner Losh if ((fdc->flags & FDC_NODMA) == 0) { 128aad64165SWarner Losh fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ, 129aad64165SWarner Losh &fdc->rid_drq, RF_ACTIVE | RF_SHAREABLE); 130973bfe6cSWarner Losh if (fdc->res_drq == NULL) { 131aad64165SWarner Losh device_printf(dev, "cannot reserve DMA request line\n"); 132b4046cd7SWarner Losh /* This is broken and doesn't work for ISA case */ 133aad64165SWarner Losh fdc->flags |= FDC_NODMA; 134aad64165SWarner Losh } else 135aad64165SWarner Losh fdc->dmachan = rman_get_start(fdc->res_drq); 136aad64165SWarner Losh } 137aad64165SWarner Losh 138b4046cd7SWarner Losh return (0); 139aad64165SWarner Losh } 140aad64165SWarner Losh 141aad64165SWarner Losh static int 1426d6fa4fdSWarner Losh fdc_isa_probe(device_t dev) 1436d6fa4fdSWarner Losh { 1441b67be7bSPoul-Henning Kamp int error; 1456d6fa4fdSWarner Losh struct fdc_data *fdc; 1466d6fa4fdSWarner Losh 1476d6fa4fdSWarner Losh fdc = device_get_softc(dev); 1486d6fa4fdSWarner Losh fdc->fdc_dev = dev; 1496d6fa4fdSWarner Losh 1506d6fa4fdSWarner Losh /* Check pnp ids */ 1516d6fa4fdSWarner Losh error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 1526d6fa4fdSWarner Losh if (error == ENXIO) 153aad64165SWarner Losh return (ENXIO); 1546d6fa4fdSWarner Losh 1556d6fa4fdSWarner Losh /* Attempt to allocate our resources for the duration of the probe */ 156aad64165SWarner Losh error = fdc_isa_alloc_resources(dev, fdc); 1571b67be7bSPoul-Henning Kamp if (error == 0) 1581b67be7bSPoul-Henning Kamp error = fdc_initial_reset(dev, fdc); 1596d6fa4fdSWarner Losh 1606d6fa4fdSWarner Losh fdc_release_resources(fdc); 1616d6fa4fdSWarner Losh return (error); 1626d6fa4fdSWarner Losh } 1636d6fa4fdSWarner Losh 164aad64165SWarner Losh static int 165aad64165SWarner Losh fdc_isa_attach(device_t dev) 166aad64165SWarner Losh { 167aad64165SWarner Losh struct fdc_data *fdc; 168aad64165SWarner Losh int error; 169aad64165SWarner Losh 170aad64165SWarner Losh fdc = device_get_softc(dev); 171787a196bSWarner Losh fdc->fdc_dev = dev; 1720bdf1a55SNate Lawson error = fdc_isa_alloc_resources(dev, fdc); 1731b67be7bSPoul-Henning Kamp if (error == 0) 1740bdf1a55SNate Lawson error = fdc_attach(dev); 1751b67be7bSPoul-Henning Kamp if (error == 0) 1760bdf1a55SNate Lawson error = fdc_hints_probe(dev); 1770bdf1a55SNate Lawson if (error) 1780bdf1a55SNate Lawson fdc_release_resources(fdc); 1790bdf1a55SNate Lawson return (error); 180aad64165SWarner Losh } 181aad64165SWarner Losh 1826d6fa4fdSWarner Losh static device_method_t fdc_methods[] = { 1836d6fa4fdSWarner Losh /* Device interface */ 1846d6fa4fdSWarner Losh DEVMETHOD(device_probe, fdc_isa_probe), 185aad64165SWarner Losh DEVMETHOD(device_attach, fdc_isa_attach), 1866d6fa4fdSWarner Losh DEVMETHOD(device_detach, fdc_detach), 1876d6fa4fdSWarner Losh DEVMETHOD(device_shutdown, bus_generic_shutdown), 1886d6fa4fdSWarner Losh DEVMETHOD(device_suspend, bus_generic_suspend), 1896d6fa4fdSWarner Losh DEVMETHOD(device_resume, bus_generic_resume), 1906d6fa4fdSWarner Losh 1916d6fa4fdSWarner Losh /* Bus interface */ 1926d6fa4fdSWarner Losh DEVMETHOD(bus_print_child, fdc_print_child), 1936d6fa4fdSWarner Losh DEVMETHOD(bus_read_ivar, fdc_read_ivar), 194752d4735SNate Lawson DEVMETHOD(bus_write_ivar, fdc_write_ivar), 1956d6fa4fdSWarner Losh /* Our children never use any other bus interface methods. */ 1966d6fa4fdSWarner Losh 1976d6fa4fdSWarner Losh { 0, 0 } 1986d6fa4fdSWarner Losh }; 1996d6fa4fdSWarner Losh 2006d6fa4fdSWarner Losh static driver_t fdc_driver = { 2016d6fa4fdSWarner Losh "fdc", 2026d6fa4fdSWarner Losh fdc_methods, 2036d6fa4fdSWarner Losh sizeof(struct fdc_data) 2046d6fa4fdSWarner Losh }; 2056d6fa4fdSWarner Losh 2066d6fa4fdSWarner Losh DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0); 207