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> 366d6fa4fdSWarner Losh #include <sys/module.h> 376d6fa4fdSWarner Losh #include <sys/rman.h> 386d6fa4fdSWarner Losh #include <sys/systm.h> 39aad64165SWarner Losh #include <machine/bus.h> 406d6fa4fdSWarner Losh 41c3ae4c40SWarner Losh #include <machine/bus.h> 42c3ae4c40SWarner Losh 436d6fa4fdSWarner Losh #include <dev/fdc/fdcvar.h> 446d6fa4fdSWarner Losh #include <dev/fdc/fdcreg.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 static void fdctl_wr_isa(fdc_p, u_int8_t); 516d6fa4fdSWarner Losh 526d6fa4fdSWarner Losh static struct isa_pnp_id fdc_ids[] = { 536d6fa4fdSWarner Losh {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */ 546d6fa4fdSWarner Losh {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */ 556d6fa4fdSWarner Losh {0} 566d6fa4fdSWarner Losh }; 576d6fa4fdSWarner Losh 586d6fa4fdSWarner Losh static void 596d6fa4fdSWarner Losh fdctl_wr_isa(fdc_p fdc, u_int8_t v) 606d6fa4fdSWarner Losh { 616d6fa4fdSWarner Losh bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v); 626d6fa4fdSWarner Losh } 636d6fa4fdSWarner Losh 640bdf1a55SNate Lawson int 65aad64165SWarner Losh fdc_isa_alloc_resources(device_t dev, struct fdc_data *fdc) 66aad64165SWarner Losh { 67aad64165SWarner Losh int ispnp, nports; 68aad64165SWarner Losh 69aad64165SWarner Losh ispnp = (fdc->flags & FDC_ISPNP) != 0; 7016629bd9SWarner Losh fdc->fdc_dev = dev; 71aad64165SWarner Losh fdc->rid_ioport = 0; 72aad64165SWarner Losh fdc->rid_irq = 0; 73aad64165SWarner Losh fdc->rid_drq = 0; 74aad64165SWarner Losh fdc->rid_ctl = 1; 75aad64165SWarner Losh 76aad64165SWarner Losh /* 77aad64165SWarner Losh * On standard ISA, we don't just use an 8 port range 78aad64165SWarner Losh * (e.g. 0x3f0-0x3f7) since that covers an IDE control 79aad64165SWarner Losh * register at 0x3f6. 80aad64165SWarner Losh * 81aad64165SWarner Losh * Isn't PC hardware wonderful. 82aad64165SWarner Losh */ 83aad64165SWarner Losh nports = ispnp ? 1 : 6; 84aad64165SWarner Losh 85aad64165SWarner Losh /* 86aad64165SWarner Losh * Some ACPI BIOSen have _CRS objects for the floppy device that 87aad64165SWarner Losh * split the I/O port resource into several resources. We detect 88aad64165SWarner Losh * this case by checking if there are more than 2 IOPORT resources. 89aad64165SWarner Losh * If so, we use the resource with the smallest start address as 90aad64165SWarner Losh * the port RID and the largest start address as the control RID. 91aad64165SWarner Losh */ 92aad64165SWarner Losh if (bus_get_resource_count(dev, SYS_RES_IOPORT, 2) != 0) { 93aad64165SWarner Losh u_long min_start, max_start, tmp; 94aad64165SWarner Losh int i; 95aad64165SWarner Losh 96aad64165SWarner Losh /* Find the min/max start addresses and their RIDs. */ 97aad64165SWarner Losh max_start = 0ul; 98aad64165SWarner Losh min_start = ~0ul; 99aad64165SWarner Losh for (i = 0; bus_get_resource_count(dev, SYS_RES_IOPORT, i) > 0; 100aad64165SWarner Losh i++) { 101aad64165SWarner Losh tmp = bus_get_resource_start(dev, SYS_RES_IOPORT, i); 102aad64165SWarner Losh KASSERT(tmp != 0, ("bogus resource")); 103aad64165SWarner Losh if (tmp < min_start) { 104aad64165SWarner Losh min_start = tmp; 105aad64165SWarner Losh fdc->rid_ioport = i; 106aad64165SWarner Losh } 107aad64165SWarner Losh if (tmp > max_start) { 108aad64165SWarner Losh max_start = tmp; 109aad64165SWarner Losh fdc->rid_ctl = i; 110aad64165SWarner Losh } 111aad64165SWarner Losh } 112aad64165SWarner Losh if (min_start + 7 != max_start) { 113aad64165SWarner Losh device_printf(dev, "I/O to control range incorrect\n"); 114aad64165SWarner Losh return (ENXIO); 115aad64165SWarner Losh } 116aad64165SWarner Losh } 117aad64165SWarner Losh 118aad64165SWarner Losh fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT, 119aad64165SWarner Losh &fdc->rid_ioport, 0ul, ~0ul, nports, RF_ACTIVE); 120aad64165SWarner Losh if (fdc->res_ioport == 0) { 121aad64165SWarner Losh device_printf(dev, "cannot reserve I/O port range (%d ports)\n", 122aad64165SWarner Losh nports); 123aad64165SWarner Losh return ENXIO; 124aad64165SWarner Losh } 125aad64165SWarner Losh fdc->portt = rman_get_bustag(fdc->res_ioport); 126aad64165SWarner Losh fdc->porth = rman_get_bushandle(fdc->res_ioport); 127aad64165SWarner Losh 128aad64165SWarner Losh /* 129aad64165SWarner Losh * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7 130aad64165SWarner Losh * and some at 0x3f0-0x3f5,0x3f7. We detect the former 131aad64165SWarner Losh * by checking the size and adjust the port address 132aad64165SWarner Losh * accordingly. 133aad64165SWarner Losh */ 134aad64165SWarner Losh if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4) 135aad64165SWarner Losh fdc->port_off = -2; 136aad64165SWarner Losh 137aad64165SWarner Losh /* 138aad64165SWarner Losh * Register the control port range as rid 1 if it 139aad64165SWarner Losh * isn't there already. Most PnP BIOSen will have 140aad64165SWarner Losh * already done this but non-PnP configurations don't. 141aad64165SWarner Losh * 142aad64165SWarner Losh * And some (!!) report 0x3f2-0x3f5 and completely 143aad64165SWarner Losh * leave out the control register! It seems that some 144aad64165SWarner Losh * non-antique controller chips have a different 145aad64165SWarner Losh * method of programming the transfer speed which 146aad64165SWarner Losh * doesn't require the control register, but it's 147aad64165SWarner Losh * mighty bogus as the chip still responds to the 148aad64165SWarner Losh * address for the control register. 149aad64165SWarner Losh */ 150aad64165SWarner Losh if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) { 151aad64165SWarner Losh u_long ctlstart; 152aad64165SWarner Losh /* Find the control port, usually 0x3f7 */ 153aad64165SWarner Losh ctlstart = rman_get_start(fdc->res_ioport) + fdc->port_off + 7; 154aad64165SWarner Losh bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1); 155aad64165SWarner Losh } 156aad64165SWarner Losh 157aad64165SWarner Losh /* 158aad64165SWarner Losh * Now (finally!) allocate the control port. 159aad64165SWarner Losh */ 160aad64165SWarner Losh fdc->res_ctl = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 161aad64165SWarner Losh &fdc->rid_ctl, RF_ACTIVE); 162aad64165SWarner Losh if (fdc->res_ctl == 0) { 163aad64165SWarner Losh device_printf(dev, 164aad64165SWarner Losh "cannot reserve control I/O port range (control port)\n"); 165aad64165SWarner Losh return ENXIO; 166aad64165SWarner Losh } 167aad64165SWarner Losh fdc->ctlt = rman_get_bustag(fdc->res_ctl); 168aad64165SWarner Losh fdc->ctlh = rman_get_bushandle(fdc->res_ctl); 169aad64165SWarner Losh 170aad64165SWarner Losh fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq, 171aad64165SWarner Losh RF_ACTIVE | RF_SHAREABLE); 172aad64165SWarner Losh if (fdc->res_irq == 0) { 173aad64165SWarner Losh device_printf(dev, "cannot reserve interrupt line\n"); 174aad64165SWarner Losh return ENXIO; 175aad64165SWarner Losh } 176aad64165SWarner Losh 177aad64165SWarner Losh if ((fdc->flags & FDC_NODMA) == 0) { 178aad64165SWarner Losh fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ, 179aad64165SWarner Losh &fdc->rid_drq, RF_ACTIVE | RF_SHAREABLE); 180aad64165SWarner Losh if (fdc->res_drq == 0) { 181aad64165SWarner Losh device_printf(dev, "cannot reserve DMA request line\n"); 182aad64165SWarner Losh fdc->flags |= FDC_NODMA; 183aad64165SWarner Losh } else 184aad64165SWarner Losh fdc->dmachan = rman_get_start(fdc->res_drq); 185aad64165SWarner Losh } 186aad64165SWarner Losh 187aad64165SWarner Losh return 0; 188aad64165SWarner Losh } 189aad64165SWarner Losh 190aad64165SWarner Losh static int 1916d6fa4fdSWarner Losh fdc_isa_probe(device_t dev) 1926d6fa4fdSWarner Losh { 1936d6fa4fdSWarner Losh int error, ic_type; 1946d6fa4fdSWarner Losh struct fdc_data *fdc; 1956d6fa4fdSWarner Losh 1966d6fa4fdSWarner Losh fdc = device_get_softc(dev); 1976d6fa4fdSWarner Losh fdc->fdc_dev = dev; 1986d6fa4fdSWarner Losh fdc->fdctl_wr = fdctl_wr_isa; 1996d6fa4fdSWarner Losh 2006d6fa4fdSWarner Losh /* Check pnp ids */ 2016d6fa4fdSWarner Losh error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 2026d6fa4fdSWarner Losh if (error == ENXIO) 203aad64165SWarner Losh return (ENXIO); 2046d6fa4fdSWarner Losh 2056d6fa4fdSWarner Losh /* Attempt to allocate our resources for the duration of the probe */ 206aad64165SWarner Losh error = fdc_isa_alloc_resources(dev, fdc); 2076d6fa4fdSWarner Losh if (error) 2086d6fa4fdSWarner Losh goto out; 2096d6fa4fdSWarner Losh 210752d4735SNate Lawson /* Check that the controller is working. */ 211752d4735SNate Lawson error = fdc_initial_reset(fdc); 212752d4735SNate Lawson if (error) 2136d6fa4fdSWarner Losh goto out; 2146d6fa4fdSWarner Losh 215752d4735SNate Lawson /* Try to determine a more specific device type. */ 2166d6fa4fdSWarner Losh if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { 217aad64165SWarner Losh switch (ic_type & 0xff) { 2186d6fa4fdSWarner Losh case 0x80: 2196d6fa4fdSWarner Losh device_set_desc(dev, "NEC 765 or clone"); 2206d6fa4fdSWarner Losh break; 2216d6fa4fdSWarner Losh case 0x81: /* not mentioned in any hardware doc */ 2226d6fa4fdSWarner Losh case 0x90: 2236d6fa4fdSWarner Losh device_set_desc(dev, 2246d6fa4fdSWarner Losh "Enhanced floppy controller (i82077, NE72065 or clone)"); 2256d6fa4fdSWarner Losh break; 2266d6fa4fdSWarner Losh default: 2276d6fa4fdSWarner Losh device_set_desc(dev, "Generic floppy controller"); 2286d6fa4fdSWarner Losh break; 2296d6fa4fdSWarner Losh } 2306d6fa4fdSWarner Losh } 2316d6fa4fdSWarner Losh 2326d6fa4fdSWarner Losh out: 2336d6fa4fdSWarner Losh fdc_release_resources(fdc); 2346d6fa4fdSWarner Losh return (error); 2356d6fa4fdSWarner Losh } 2366d6fa4fdSWarner Losh 237aad64165SWarner Losh static int 238aad64165SWarner Losh fdc_isa_attach(device_t dev) 239aad64165SWarner Losh { 240aad64165SWarner Losh int ic_type; 241aad64165SWarner Losh struct fdc_data *fdc; 242aad64165SWarner Losh int error; 243aad64165SWarner Losh 244aad64165SWarner Losh fdc = device_get_softc(dev); 245aad64165SWarner Losh fdc->fdctl_wr = fdctl_wr_isa; 246aad64165SWarner Losh error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 247aad64165SWarner Losh if (error == 0) 248aad64165SWarner Losh fdc->flags |= FDC_ISPNP; 249aad64165SWarner Losh if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { 250aad64165SWarner Losh ic_type = (u_char)ic_type; 251aad64165SWarner Losh switch (ic_type) { 252aad64165SWarner Losh case 0x80: 253aad64165SWarner Losh fdc->fdct = FDC_NE765; 254aad64165SWarner Losh break; 255aad64165SWarner Losh case 0x81: /* not mentioned in any hardware doc */ 256aad64165SWarner Losh case 0x90: 257aad64165SWarner Losh fdc->fdct = FDC_ENHANCED; 258aad64165SWarner Losh break; 259aad64165SWarner Losh default: 260aad64165SWarner Losh fdc->fdct = FDC_UNKNOWN; 261aad64165SWarner Losh break; 262aad64165SWarner Losh } 263aad64165SWarner Losh } 2640bdf1a55SNate Lawson 2650bdf1a55SNate Lawson error = fdc_isa_alloc_resources(dev, fdc); 2660bdf1a55SNate Lawson if (error) 2670bdf1a55SNate Lawson goto out; 2680bdf1a55SNate Lawson error = fdc_attach(dev); 2690bdf1a55SNate Lawson if (error) 2700bdf1a55SNate Lawson goto out; 2710bdf1a55SNate Lawson error = fdc_hints_probe(dev); 2720bdf1a55SNate Lawson if (error) 2730bdf1a55SNate Lawson goto out; 2740bdf1a55SNate Lawson out: 2750bdf1a55SNate Lawson if (error) 2760bdf1a55SNate Lawson fdc_release_resources(fdc); 2770bdf1a55SNate Lawson return (error); 278aad64165SWarner Losh } 279aad64165SWarner Losh 2806d6fa4fdSWarner Losh static device_method_t fdc_methods[] = { 2816d6fa4fdSWarner Losh /* Device interface */ 2826d6fa4fdSWarner Losh DEVMETHOD(device_probe, fdc_isa_probe), 283aad64165SWarner Losh DEVMETHOD(device_attach, fdc_isa_attach), 2846d6fa4fdSWarner Losh DEVMETHOD(device_detach, fdc_detach), 2856d6fa4fdSWarner Losh DEVMETHOD(device_shutdown, bus_generic_shutdown), 2866d6fa4fdSWarner Losh DEVMETHOD(device_suspend, bus_generic_suspend), 2876d6fa4fdSWarner Losh DEVMETHOD(device_resume, bus_generic_resume), 2886d6fa4fdSWarner Losh 2896d6fa4fdSWarner Losh /* Bus interface */ 2906d6fa4fdSWarner Losh DEVMETHOD(bus_print_child, fdc_print_child), 2916d6fa4fdSWarner Losh DEVMETHOD(bus_read_ivar, fdc_read_ivar), 292752d4735SNate Lawson DEVMETHOD(bus_write_ivar, fdc_write_ivar), 2936d6fa4fdSWarner Losh /* Our children never use any other bus interface methods. */ 2946d6fa4fdSWarner Losh 2956d6fa4fdSWarner Losh { 0, 0 } 2966d6fa4fdSWarner Losh }; 2976d6fa4fdSWarner Losh 2986d6fa4fdSWarner Losh static driver_t fdc_driver = { 2996d6fa4fdSWarner Losh "fdc", 3006d6fa4fdSWarner Losh fdc_methods, 3016d6fa4fdSWarner Losh sizeof(struct fdc_data) 3026d6fa4fdSWarner Losh }; 3036d6fa4fdSWarner Losh 3046d6fa4fdSWarner Losh DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0); 305