10bdf1a55SNate Lawson /*- 2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3718cf2ccSPedro F. Giffuni * 4973bfe6cSWarner Losh * Copyright (c) 2004-2005 M. Warner Losh. 56d6fa4fdSWarner Losh * All rights reserved. 66d6fa4fdSWarner Losh * 76d6fa4fdSWarner Losh * Redistribution and use in source and binary forms, with or without 86d6fa4fdSWarner Losh * modification, are permitted provided that the following conditions 96d6fa4fdSWarner Losh * are met: 106d6fa4fdSWarner Losh * 1. Redistributions of source code must retain the above copyright 11ac673f9aSWarner Losh * notice, this list of conditions and the following disclaimer. 126d6fa4fdSWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 13ac673f9aSWarner Losh * notice, this list of conditions and the following disclaimer in the 14ac673f9aSWarner Losh * documentation and/or other materials provided with the 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 19ac673f9aSWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20ac673f9aSWarner Losh * FOR 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[] = { 52973bfe6cSWarner Losh {0x0007d041, "PC standard floppy controller"}, /* PNP0700 */ 536d6fa4fdSWarner Losh {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */ 546d6fa4fdSWarner Losh {0} 556d6fa4fdSWarner Losh }; 566d6fa4fdSWarner Losh 57aad64165SWarner Losh /* 58aad64165SWarner Losh * On standard ISA, we don't just use an 8 port range 59973bfe6cSWarner Losh * (e.g. 0x3f0-0x3f7) since that covers an IDE control register at 60973bfe6cSWarner Losh * 0x3f6. So, on older hardware, we use 0x3f0-0x3f5 and 0x3f7. 61973bfe6cSWarner Losh * However, some BIOSs omit the control port, while others start at 62973bfe6cSWarner Losh * 0x3f2. Of the latter, sometimes we have two resources, other times 63973bfe6cSWarner Losh * we have one. We have to deal with the following cases: 64aad64165SWarner Losh * 65b4046cd7SWarner Losh * 1: 0x3f0-0x3f5 # very rare 66b4046cd7SWarner Losh * 2: 0x3f0 # hints -> 0x3f0-0x3f5,0x3f7 67b4046cd7SWarner Losh * 3: 0x3f0-0x3f5,0x3f7 # Most common 68b4046cd7SWarner Losh * 4: 0x3f2-0x3f5,0x3f7 # Second most common 69b4046cd7SWarner Losh * 5: 0x3f2-0x3f5 # implies 0x3f7 too. 70b4046cd7SWarner Losh * 6: 0x3f2-0x3f3,0x3f4-0x3f5,0x3f7 # becoming common 71b4046cd7SWarner Losh * 7: 0x3f2-0x3f3,0x3f4-0x3f5 # rare 72083ba097SWarner Losh * 8: 0x3f0-0x3f1,0x3f2-0x3f3,0x3f4-0x3f5,0x3f7 73534e7194SWarner Losh * 9: 0x3f0-0x3f3,0x3f4-0x3f5,0x3f7 74b4046cd7SWarner Losh * 75973bfe6cSWarner Losh * The following code is generic for any value of 0x3fx. It is also 76973bfe6cSWarner Losh * generic for all the above cases, as well as cases where things are 77973bfe6cSWarner Losh * even weirder. 78aad64165SWarner Losh */ 79973bfe6cSWarner Losh int 80973bfe6cSWarner Losh fdc_isa_alloc_resources(device_t dev, struct fdc_data *fdc) 81973bfe6cSWarner Losh { 82973bfe6cSWarner Losh struct resource *res; 83530a6924SWarner Losh int i, j, rid, newrid, nport; 84530a6924SWarner Losh u_long port; 85aad64165SWarner Losh 86973bfe6cSWarner Losh fdc->fdc_dev = dev; 87973bfe6cSWarner Losh rid = 0; 88973bfe6cSWarner Losh for (i = 0; i < FDC_MAXREG; i++) 89973bfe6cSWarner Losh fdc->resio[i] = NULL; 90973bfe6cSWarner Losh 91530a6924SWarner Losh nport = isa_get_logicalid(dev) ? 1 : 6; 92973bfe6cSWarner Losh for (rid = 0; ; rid++) { 93973bfe6cSWarner Losh newrid = rid; 94c47476d7SJustin Hibbits res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &newrid, 95c47476d7SJustin Hibbits rid == 0 ? nport : 1, RF_ACTIVE); 96973bfe6cSWarner Losh if (res == NULL) 97973bfe6cSWarner Losh break; 98787a196bSWarner Losh /* 99787a196bSWarner Losh * Mask off the upper bits of the register, and sanity 100787a196bSWarner Losh * check resource ranges. 101787a196bSWarner Losh */ 102787a196bSWarner Losh i = rman_get_start(res) & 0x7; 103356eadcdSWarner Losh if (i + rman_get_size(res) - 1 > FDC_MAXREG) { 104356eadcdSWarner Losh bus_release_resource(dev, SYS_RES_IOPORT, newrid, res); 105787a196bSWarner Losh return (ENXIO); 106356eadcdSWarner Losh } 107973bfe6cSWarner Losh for (j = 0; j < rman_get_size(res); j++) { 108973bfe6cSWarner Losh fdc->resio[i + j] = res; 109973bfe6cSWarner Losh fdc->ridio[i + j] = newrid; 110973bfe6cSWarner Losh fdc->ioff[i + j] = j; 111973bfe6cSWarner Losh fdc->ioh[i + j] = rman_get_bushandle(res); 112973bfe6cSWarner Losh } 113973bfe6cSWarner Losh } 114530a6924SWarner Losh if (fdc->resio[2] == NULL) { 115530a6924SWarner Losh device_printf(dev, "No FDOUT register!\n"); 116b4046cd7SWarner Losh return (ENXIO); 117530a6924SWarner Losh } 118973bfe6cSWarner Losh fdc->iot = rman_get_bustag(fdc->resio[2]); 119973bfe6cSWarner Losh if (fdc->resio[7] == NULL) { 120530a6924SWarner Losh port = (rman_get_start(fdc->resio[2]) & ~0x7) + 7; 121530a6924SWarner Losh newrid = rid; 122530a6924SWarner Losh res = bus_alloc_resource(dev, SYS_RES_IOPORT, &newrid, port, 123530a6924SWarner Losh port, 1, RF_ACTIVE); 124530a6924SWarner Losh if (res == NULL) { 125530a6924SWarner Losh device_printf(dev, "Faking up FDCTL\n"); 126973bfe6cSWarner Losh fdc->resio[7] = fdc->resio[2]; 127973bfe6cSWarner Losh fdc->ridio[7] = fdc->ridio[2]; 128973bfe6cSWarner Losh fdc->ioff[7] = fdc->ioff[2] + 5; 129973bfe6cSWarner Losh fdc->ioh[7] = fdc->ioh[2]; 130530a6924SWarner Losh } else { 131530a6924SWarner Losh fdc->resio[7] = res; 132530a6924SWarner Losh fdc->ridio[7] = newrid; 133530a6924SWarner Losh fdc->ioff[7] = rman_get_start(res) & 7; 134530a6924SWarner Losh fdc->ioh[7] = rman_get_bushandle(res); 135530a6924SWarner Losh } 136b4046cd7SWarner Losh } 137aad64165SWarner Losh 138aad64165SWarner Losh fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq, 139aad64165SWarner Losh RF_ACTIVE | RF_SHAREABLE); 140973bfe6cSWarner Losh if (fdc->res_irq == NULL) { 141aad64165SWarner Losh device_printf(dev, "cannot reserve interrupt line\n"); 142b4046cd7SWarner Losh return (ENXIO); 143aad64165SWarner Losh } 144aad64165SWarner Losh 145aad64165SWarner Losh if ((fdc->flags & FDC_NODMA) == 0) { 146aad64165SWarner Losh fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ, 147aad64165SWarner Losh &fdc->rid_drq, RF_ACTIVE | RF_SHAREABLE); 148973bfe6cSWarner Losh if (fdc->res_drq == NULL) { 149aad64165SWarner Losh device_printf(dev, "cannot reserve DMA request line\n"); 150b4046cd7SWarner Losh /* This is broken and doesn't work for ISA case */ 151aad64165SWarner Losh fdc->flags |= FDC_NODMA; 152aad64165SWarner Losh } else 153aad64165SWarner Losh fdc->dmachan = rman_get_start(fdc->res_drq); 154aad64165SWarner Losh } 155aad64165SWarner Losh 156b4046cd7SWarner Losh return (0); 157aad64165SWarner Losh } 158aad64165SWarner Losh 159aad64165SWarner Losh static int 1606d6fa4fdSWarner Losh fdc_isa_probe(device_t dev) 1616d6fa4fdSWarner Losh { 1621b67be7bSPoul-Henning Kamp int error; 1636d6fa4fdSWarner Losh struct fdc_data *fdc; 1646d6fa4fdSWarner Losh 1656d6fa4fdSWarner Losh fdc = device_get_softc(dev); 1666d6fa4fdSWarner Losh fdc->fdc_dev = dev; 1676d6fa4fdSWarner Losh 1686d6fa4fdSWarner Losh /* Check pnp ids */ 1696d6fa4fdSWarner Losh error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 1706d6fa4fdSWarner Losh if (error == ENXIO) 171aad64165SWarner Losh return (ENXIO); 1726d6fa4fdSWarner Losh 1736d6fa4fdSWarner Losh /* Attempt to allocate our resources for the duration of the probe */ 174aad64165SWarner Losh error = fdc_isa_alloc_resources(dev, fdc); 1751b67be7bSPoul-Henning Kamp if (error == 0) 1761b67be7bSPoul-Henning Kamp error = fdc_initial_reset(dev, fdc); 1776d6fa4fdSWarner Losh 1786d6fa4fdSWarner Losh fdc_release_resources(fdc); 1796d6fa4fdSWarner Losh return (error); 1806d6fa4fdSWarner Losh } 1816d6fa4fdSWarner Losh 182aad64165SWarner Losh static int 183aad64165SWarner Losh fdc_isa_attach(device_t dev) 184aad64165SWarner Losh { 185aad64165SWarner Losh struct fdc_data *fdc; 186aad64165SWarner Losh int error; 187aad64165SWarner Losh 188aad64165SWarner Losh fdc = device_get_softc(dev); 189787a196bSWarner Losh fdc->fdc_dev = dev; 1900bdf1a55SNate Lawson error = fdc_isa_alloc_resources(dev, fdc); 1911b67be7bSPoul-Henning Kamp if (error == 0) 1920bdf1a55SNate Lawson error = fdc_attach(dev); 1931b67be7bSPoul-Henning Kamp if (error == 0) 1940bdf1a55SNate Lawson error = fdc_hints_probe(dev); 195fed2c48aSJohn Baldwin if (error == 0) 196fed2c48aSJohn Baldwin fdc_start_worker(dev); 197fed2c48aSJohn Baldwin else 1980bdf1a55SNate Lawson fdc_release_resources(fdc); 1990bdf1a55SNate Lawson return (error); 200aad64165SWarner Losh } 201aad64165SWarner Losh 2026d6fa4fdSWarner Losh static device_method_t fdc_methods[] = { 2036d6fa4fdSWarner Losh /* Device interface */ 2046d6fa4fdSWarner Losh DEVMETHOD(device_probe, fdc_isa_probe), 205aad64165SWarner Losh DEVMETHOD(device_attach, fdc_isa_attach), 2066d6fa4fdSWarner Losh DEVMETHOD(device_detach, fdc_detach), 2076d6fa4fdSWarner Losh DEVMETHOD(device_shutdown, bus_generic_shutdown), 2086d6fa4fdSWarner Losh DEVMETHOD(device_suspend, bus_generic_suspend), 2096d6fa4fdSWarner Losh DEVMETHOD(device_resume, bus_generic_resume), 2106d6fa4fdSWarner Losh 2116d6fa4fdSWarner Losh /* Bus interface */ 2126d6fa4fdSWarner Losh DEVMETHOD(bus_print_child, fdc_print_child), 2136d6fa4fdSWarner Losh DEVMETHOD(bus_read_ivar, fdc_read_ivar), 214752d4735SNate Lawson DEVMETHOD(bus_write_ivar, fdc_write_ivar), 2156d6fa4fdSWarner Losh /* Our children never use any other bus interface methods. */ 2166d6fa4fdSWarner Losh 2176d6fa4fdSWarner Losh { 0, 0 } 2186d6fa4fdSWarner Losh }; 2196d6fa4fdSWarner Losh 2206d6fa4fdSWarner Losh static driver_t fdc_driver = { 2216d6fa4fdSWarner Losh "fdc", 2226d6fa4fdSWarner Losh fdc_methods, 2236d6fa4fdSWarner Losh sizeof(struct fdc_data) 2246d6fa4fdSWarner Losh }; 2256d6fa4fdSWarner Losh 2266d6fa4fdSWarner Losh DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0); 227*5fe4723cSWarner Losh ISA_PNP_INFO(fdc_ids); 228