10bdf1a55SNate Lawson /*- 26d6fa4fdSWarner Losh * Copyright (c) 2004 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 96d6fa4fdSWarner Losh * notice, this list of conditions, and the following disclaimer, 106d6fa4fdSWarner Losh * without modification, immediately at the beginning of the file. 116d6fa4fdSWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 126d6fa4fdSWarner Losh * notice, this list of conditions and the following disclaimer in 136d6fa4fdSWarner Losh * the documentation and/or other materials provided with the 146d6fa4fdSWarner Losh * distribution. 156d6fa4fdSWarner Losh * 166d6fa4fdSWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 176d6fa4fdSWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 186d6fa4fdSWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 196d6fa4fdSWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 206d6fa4fdSWarner Losh * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 216d6fa4fdSWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 226d6fa4fdSWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 236d6fa4fdSWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 246d6fa4fdSWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 256d6fa4fdSWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 266d6fa4fdSWarner Losh * SUCH DAMAGE. 276d6fa4fdSWarner Losh */ 286d6fa4fdSWarner Losh 296d6fa4fdSWarner Losh #include <sys/cdefs.h> 306d6fa4fdSWarner Losh __FBSDID("$FreeBSD$"); 316d6fa4fdSWarner Losh 326d6fa4fdSWarner Losh #include <sys/param.h> 336d6fa4fdSWarner Losh #include <sys/bio.h> 346d6fa4fdSWarner Losh #include <sys/bus.h> 356d6fa4fdSWarner Losh #include <sys/kernel.h> 361b67be7bSPoul-Henning Kamp #include <sys/lock.h> 376d6fa4fdSWarner Losh #include <sys/module.h> 381b67be7bSPoul-Henning Kamp #include <sys/mutex.h> 396d6fa4fdSWarner Losh #include <sys/rman.h> 406d6fa4fdSWarner Losh #include <sys/systm.h> 416d6fa4fdSWarner Losh 42c3ae4c40SWarner Losh #include <machine/bus.h> 43c3ae4c40SWarner Losh 446d6fa4fdSWarner Losh #include <dev/fdc/fdcvar.h> 456d6fa4fdSWarner Losh 466d6fa4fdSWarner Losh #include <isa/isavar.h> 476d6fa4fdSWarner Losh #include <isa/isareg.h> 486d6fa4fdSWarner Losh 496d6fa4fdSWarner Losh static int fdc_isa_probe(device_t); 506d6fa4fdSWarner Losh 516d6fa4fdSWarner Losh static struct isa_pnp_id fdc_ids[] = { 526d6fa4fdSWarner Losh {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */ 536d6fa4fdSWarner Losh {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */ 546d6fa4fdSWarner Losh {0} 556d6fa4fdSWarner Losh }; 566d6fa4fdSWarner Losh 570bdf1a55SNate Lawson int 58aad64165SWarner Losh fdc_isa_alloc_resources(device_t dev, struct fdc_data *fdc) 59aad64165SWarner Losh { 60b4046cd7SWarner Losh int nports = 6; 61aad64165SWarner Losh 6216629bd9SWarner Losh fdc->fdc_dev = dev; 63aad64165SWarner Losh fdc->rid_ioport = 0; 64aad64165SWarner Losh fdc->rid_irq = 0; 65aad64165SWarner Losh fdc->rid_drq = 0; 66aad64165SWarner Losh fdc->rid_ctl = 1; 67aad64165SWarner Losh 68aad64165SWarner Losh /* 69aad64165SWarner Losh * On standard ISA, we don't just use an 8 port range 70aad64165SWarner Losh * (e.g. 0x3f0-0x3f7) since that covers an IDE control 71b4046cd7SWarner Losh * register at 0x3f6. So, on older hardware, we use 72b4046cd7SWarner Losh * 0x3f0-0x3f5 and 0x3f7. However, some BIOSs omit the 73b4046cd7SWarner Losh * control port, while others start at 0x3f2. Of the latter, 74b4046cd7SWarner Losh * sometimes we have two resources, other times we have one. 75b4046cd7SWarner Losh * We have to deal with the following cases: 76aad64165SWarner Losh * 77b4046cd7SWarner Losh * 1: 0x3f0-0x3f5 # very rare 78b4046cd7SWarner Losh * 2: 0x3f0 # hints -> 0x3f0-0x3f5,0x3f7 79b4046cd7SWarner Losh * 3: 0x3f0-0x3f5,0x3f7 # Most common 80b4046cd7SWarner Losh * 4: 0x3f2-0x3f5,0x3f7 # Second most common 81b4046cd7SWarner Losh * 5: 0x3f2-0x3f5 # implies 0x3f7 too. 82b4046cd7SWarner Losh * 6: 0x3f2-0x3f3,0x3f4-0x3f5,0x3f7 # becoming common 83b4046cd7SWarner Losh * 7: 0x3f2-0x3f3,0x3f4-0x3f5 # rare 84b4046cd7SWarner Losh * 85b4046cd7SWarner Losh * The following code is generic for any value of 0x3fx :-) 86aad64165SWarner Losh */ 87aad64165SWarner Losh 88aad64165SWarner Losh /* 89b4046cd7SWarner Losh * First, allocated the main range of ports. In the best of 90b4046cd7SWarner Losh * worlds, this is 4 or 6 ports. In others, well, that's 91b4046cd7SWarner Losh * why this function is so complicated. 92aad64165SWarner Losh */ 93aad64165SWarner Losh fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT, 94aad64165SWarner Losh &fdc->rid_ioport, 0ul, ~0ul, nports, RF_ACTIVE); 95aad64165SWarner Losh if (fdc->res_ioport == 0) { 96b4046cd7SWarner Losh device_printf(dev, "cannot allocate I/O port (%d ports)\n", 97aad64165SWarner Losh nports); 98b4046cd7SWarner Losh return (ENXIO); 99aad64165SWarner Losh } 100aad64165SWarner Losh fdc->portt = rman_get_bustag(fdc->res_ioport); 101aad64165SWarner Losh fdc->porth = rman_get_bushandle(fdc->res_ioport); 102aad64165SWarner Losh 103aad64165SWarner Losh /* 104b4046cd7SWarner Losh * Handle cases 4-7 above 105aad64165SWarner Losh */ 106b4046cd7SWarner Losh fdc->port_off = -(fdc->porth & 0x7); 107aad64165SWarner Losh 108aad64165SWarner Losh /* 109b4046cd7SWarner Losh * Deal with case 6 and 7: FDSTS and FDSATA are in rid 1. 110aad64165SWarner Losh */ 111b4046cd7SWarner Losh if (rman_get_size(fdc->res_ioport) == 2) { 112b4046cd7SWarner Losh fdc->rid_sts = 1; 113b4046cd7SWarner Losh fdc->res_sts = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 114b4046cd7SWarner Losh &fdc->rid_sts, RF_ACTIVE); 115b4046cd7SWarner Losh if (fdc->res_sts == NULL) { 116b4046cd7SWarner Losh device_printf(dev, "Can't alloc rid 1"); 117b4046cd7SWarner Losh fdc_release_resources(fdc); 118b4046cd7SWarner Losh return (ENXIO); 119b4046cd7SWarner Losh } 120b4046cd7SWarner Losh fdc->rid_ctl++; 121b4046cd7SWarner Losh fdc->sts_off = -4; 122b4046cd7SWarner Losh fdc->stst = rman_get_bustag(fdc->res_sts); 123b4046cd7SWarner Losh fdc->stsh = rman_get_bushandle(fdc->res_sts); 124b4046cd7SWarner Losh } else { 125b4046cd7SWarner Losh fdc->res_sts = NULL; 126b4046cd7SWarner Losh fdc->sts_off = fdc->port_off; 127b4046cd7SWarner Losh fdc->stst = fdc->portt; 128b4046cd7SWarner Losh fdc->stsh = fdc->porth; 129aad64165SWarner Losh } 130aad64165SWarner Losh 131aad64165SWarner Losh /* 132b4046cd7SWarner Losh * allocate the control port. For cases 1, 2, 5 and 7, we 133b4046cd7SWarner Losh * fake it from the ioports resource. XXX IS THIS THE RIGHT THING 134b4046cd7SWarner Losh * TO DO, OR SHOULD WE CREATE A NEW RID? (I think we need a new rid) 135aad64165SWarner Losh */ 136aad64165SWarner Losh fdc->res_ctl = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 137aad64165SWarner Losh &fdc->rid_ctl, RF_ACTIVE); 138b4046cd7SWarner Losh if (fdc->res_ctl == NULL) { 139b4046cd7SWarner Losh fdc->ctl_off = 7 + fdc->port_off; 140b4046cd7SWarner Losh fdc->res_ctl = NULL; 141b4046cd7SWarner Losh fdc->ctlt = fdc->portt; 142b4046cd7SWarner Losh fdc->ctlh = fdc->porth; 143b4046cd7SWarner Losh } else { 144b4046cd7SWarner Losh fdc->ctl_off = 0; 145aad64165SWarner Losh fdc->ctlt = rman_get_bustag(fdc->res_ctl); 146aad64165SWarner Losh fdc->ctlh = rman_get_bushandle(fdc->res_ctl); 147b4046cd7SWarner Losh } 148aad64165SWarner Losh 149aad64165SWarner Losh fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq, 150aad64165SWarner Losh RF_ACTIVE | RF_SHAREABLE); 151aad64165SWarner Losh if (fdc->res_irq == 0) { 152aad64165SWarner Losh device_printf(dev, "cannot reserve interrupt line\n"); 153b4046cd7SWarner Losh return (ENXIO); 154aad64165SWarner Losh } 155aad64165SWarner Losh 156aad64165SWarner Losh if ((fdc->flags & FDC_NODMA) == 0) { 157aad64165SWarner Losh fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ, 158aad64165SWarner Losh &fdc->rid_drq, RF_ACTIVE | RF_SHAREABLE); 159aad64165SWarner Losh if (fdc->res_drq == 0) { 160aad64165SWarner Losh device_printf(dev, "cannot reserve DMA request line\n"); 161b4046cd7SWarner Losh /* This is broken and doesn't work for ISA case */ 162aad64165SWarner Losh fdc->flags |= FDC_NODMA; 163aad64165SWarner Losh } else 164aad64165SWarner Losh fdc->dmachan = rman_get_start(fdc->res_drq); 165aad64165SWarner Losh } 166aad64165SWarner Losh 167b4046cd7SWarner Losh return (0); 168aad64165SWarner Losh } 169aad64165SWarner Losh 170aad64165SWarner Losh static int 1716d6fa4fdSWarner Losh fdc_isa_probe(device_t dev) 1726d6fa4fdSWarner Losh { 1731b67be7bSPoul-Henning Kamp int error; 1746d6fa4fdSWarner Losh struct fdc_data *fdc; 1756d6fa4fdSWarner Losh 1766d6fa4fdSWarner Losh fdc = device_get_softc(dev); 1776d6fa4fdSWarner Losh fdc->fdc_dev = dev; 1786d6fa4fdSWarner Losh 1796d6fa4fdSWarner Losh /* Check pnp ids */ 1806d6fa4fdSWarner Losh error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 1816d6fa4fdSWarner Losh if (error == ENXIO) 182aad64165SWarner Losh return (ENXIO); 1836d6fa4fdSWarner Losh 1846d6fa4fdSWarner Losh /* Attempt to allocate our resources for the duration of the probe */ 185aad64165SWarner Losh error = fdc_isa_alloc_resources(dev, fdc); 1861b67be7bSPoul-Henning Kamp if (error == 0) 1871b67be7bSPoul-Henning Kamp error = fdc_initial_reset(dev, fdc); 1886d6fa4fdSWarner Losh 1896d6fa4fdSWarner Losh fdc_release_resources(fdc); 1906d6fa4fdSWarner Losh return (error); 1916d6fa4fdSWarner Losh } 1926d6fa4fdSWarner Losh 193aad64165SWarner Losh static int 194aad64165SWarner Losh fdc_isa_attach(device_t dev) 195aad64165SWarner Losh { 196aad64165SWarner Losh struct fdc_data *fdc; 197aad64165SWarner Losh int error; 198aad64165SWarner Losh 199aad64165SWarner Losh fdc = device_get_softc(dev); 200aad64165SWarner Losh error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 201aad64165SWarner Losh if (error == 0) 202aad64165SWarner Losh fdc->flags |= FDC_ISPNP; 2030bdf1a55SNate Lawson 2040bdf1a55SNate Lawson error = fdc_isa_alloc_resources(dev, fdc); 2051b67be7bSPoul-Henning Kamp if (error == 0) 2060bdf1a55SNate Lawson error = fdc_attach(dev); 2071b67be7bSPoul-Henning Kamp if (error == 0) 2080bdf1a55SNate Lawson error = fdc_hints_probe(dev); 2090bdf1a55SNate Lawson if (error) 2100bdf1a55SNate Lawson fdc_release_resources(fdc); 2110bdf1a55SNate Lawson return (error); 212aad64165SWarner Losh } 213aad64165SWarner Losh 2146d6fa4fdSWarner Losh static device_method_t fdc_methods[] = { 2156d6fa4fdSWarner Losh /* Device interface */ 2166d6fa4fdSWarner Losh DEVMETHOD(device_probe, fdc_isa_probe), 217aad64165SWarner Losh DEVMETHOD(device_attach, fdc_isa_attach), 2186d6fa4fdSWarner Losh DEVMETHOD(device_detach, fdc_detach), 2196d6fa4fdSWarner Losh DEVMETHOD(device_shutdown, bus_generic_shutdown), 2206d6fa4fdSWarner Losh DEVMETHOD(device_suspend, bus_generic_suspend), 2216d6fa4fdSWarner Losh DEVMETHOD(device_resume, bus_generic_resume), 2226d6fa4fdSWarner Losh 2236d6fa4fdSWarner Losh /* Bus interface */ 2246d6fa4fdSWarner Losh DEVMETHOD(bus_print_child, fdc_print_child), 2256d6fa4fdSWarner Losh DEVMETHOD(bus_read_ivar, fdc_read_ivar), 226752d4735SNate Lawson DEVMETHOD(bus_write_ivar, fdc_write_ivar), 2276d6fa4fdSWarner Losh /* Our children never use any other bus interface methods. */ 2286d6fa4fdSWarner Losh 2296d6fa4fdSWarner Losh { 0, 0 } 2306d6fa4fdSWarner Losh }; 2316d6fa4fdSWarner Losh 2326d6fa4fdSWarner Losh static driver_t fdc_driver = { 2336d6fa4fdSWarner Losh "fdc", 2346d6fa4fdSWarner Losh fdc_methods, 2356d6fa4fdSWarner Losh sizeof(struct fdc_data) 2366d6fa4fdSWarner Losh }; 2376d6fa4fdSWarner Losh 2386d6fa4fdSWarner Losh DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0); 239