1331c488dSSøren Schmidt /*- 29f82379cSSøren Schmidt * Copyright (c) 1998 - 2008 S�ren Schmidt <sos@FreeBSD.org> 3331c488dSSøren Schmidt * All rights reserved. 4331c488dSSøren Schmidt * 5331c488dSSøren Schmidt * Redistribution and use in source and binary forms, with or without 6331c488dSSøren Schmidt * modification, are permitted provided that the following conditions 7331c488dSSøren Schmidt * are met: 8331c488dSSøren Schmidt * 1. Redistributions of source code must retain the above copyright 9331c488dSSøren Schmidt * notice, this list of conditions and the following disclaimer, 10331c488dSSøren Schmidt * without modification, immediately at the beginning of the file. 11331c488dSSøren Schmidt * 2. Redistributions in binary form must reproduce the above copyright 12331c488dSSøren Schmidt * notice, this list of conditions and the following disclaimer in the 13331c488dSSøren Schmidt * documentation and/or other materials provided with the distribution. 14331c488dSSøren Schmidt * 15331c488dSSøren Schmidt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16331c488dSSøren Schmidt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17331c488dSSøren Schmidt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18331c488dSSøren Schmidt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19331c488dSSøren Schmidt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20331c488dSSøren Schmidt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21331c488dSSøren Schmidt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22331c488dSSøren Schmidt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23331c488dSSøren Schmidt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24331c488dSSøren Schmidt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25331c488dSSøren Schmidt */ 26331c488dSSøren Schmidt 27aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 28aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 29aad970f1SDavid E. O'Brien 30b5d0be89SPeter Wemm #include "opt_ata.h" 31331c488dSSøren Schmidt #include <sys/param.h> 32331c488dSSøren Schmidt #include <sys/systm.h> 33c55607ceSSøren Schmidt #include <sys/ata.h> 34331c488dSSøren Schmidt #include <sys/kernel.h> 35331c488dSSøren Schmidt #include <sys/module.h> 36331c488dSSøren Schmidt #include <sys/bus.h> 37331c488dSSøren Schmidt #include <sys/malloc.h> 38a7a120f6SSøren Schmidt #include <sys/sema.h> 395fdbb0d2SSøren Schmidt #include <sys/taskqueue.h> 405df3ca78SSøren Schmidt #include <vm/uma.h> 41331c488dSSøren Schmidt #include <machine/stdarg.h> 42331c488dSSøren Schmidt #include <machine/resource.h> 43331c488dSSøren Schmidt #include <machine/bus.h> 44331c488dSSøren Schmidt #include <sys/rman.h> 45331c488dSSøren Schmidt #include <isa/isavar.h> 46331c488dSSøren Schmidt #include <dev/ata/ata-all.h> 47d5514ba3SSøren Schmidt #include <ata_if.h> 48331c488dSSøren Schmidt 49331c488dSSøren Schmidt /* local vars */ 50331c488dSSøren Schmidt static struct isa_pnp_id ata_ids[] = { 51331c488dSSøren Schmidt {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */ 52331c488dSSøren Schmidt {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */ 53331c488dSSøren Schmidt {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */ 54331c488dSSøren Schmidt {0x0306d041, "Generic ATA"}, /* PNP0603 */ 55bb5bdd38SSøren Schmidt /* PNP0680 */ 56bb5bdd38SSøren Schmidt {0x8006d041, "Standard bus mastering IDE hard disk controller"}, 57331c488dSSøren Schmidt {0} 58331c488dSSøren Schmidt }; 59331c488dSSøren Schmidt 606192895dSSøren Schmidt static int 61331c488dSSøren Schmidt ata_isa_probe(device_t dev) 62331c488dSSøren Schmidt { 630e1eb682SSøren Schmidt struct resource *io = NULL, *ctlio = NULL; 64331c488dSSøren Schmidt u_long tmp; 656a03316aSAlexander Motin int rid; 66331c488dSSøren Schmidt 67331c488dSSøren Schmidt /* check isapnp ids */ 68331c488dSSøren Schmidt if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO) 69331c488dSSøren Schmidt return ENXIO; 70331c488dSSøren Schmidt 718ba4488cSSøren Schmidt /* allocate the io port range */ 72331c488dSSøren Schmidt rid = ATA_IOADDR_RID; 73ecd6c15dSSøren Schmidt if (!(io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 74ecd6c15dSSøren Schmidt ATA_IOSIZE, RF_ACTIVE))) 75566cf07aSSøren Schmidt return ENXIO; 76331c488dSSøren Schmidt 77331c488dSSøren Schmidt /* set the altport range */ 780e1eb682SSøren Schmidt if (bus_get_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, &tmp, &tmp)) { 790e1eb682SSøren Schmidt bus_set_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, 800e1eb682SSøren Schmidt rman_get_start(io) + ATA_CTLOFFSET, ATA_CTLIOSIZE); 81331c488dSSøren Schmidt } 82331c488dSSøren Schmidt 83566cf07aSSøren Schmidt /* allocate the altport range */ 840e1eb682SSøren Schmidt rid = ATA_CTLADDR_RID; 85ecd6c15dSSøren Schmidt if (!(ctlio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 86ecd6c15dSSøren Schmidt ATA_CTLIOSIZE, RF_ACTIVE))) { 87566cf07aSSøren Schmidt bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 88566cf07aSSøren Schmidt return ENXIO; 89566cf07aSSøren Schmidt } 90566cf07aSSøren Schmidt 916a03316aSAlexander Motin /* Release resources to reallocate on attach. */ 926a03316aSAlexander Motin bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlio); 936a03316aSAlexander Motin bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 946a03316aSAlexander Motin 956a03316aSAlexander Motin return (ata_probe(dev)); 966a03316aSAlexander Motin } 976a03316aSAlexander Motin 986a03316aSAlexander Motin static int 996a03316aSAlexander Motin ata_isa_attach(device_t dev) 1006a03316aSAlexander Motin { 1016a03316aSAlexander Motin struct ata_channel *ch = device_get_softc(dev); 1026a03316aSAlexander Motin struct resource *io = NULL, *ctlio = NULL; 1036a03316aSAlexander Motin u_long tmp; 1046a03316aSAlexander Motin int i, rid; 1056a03316aSAlexander Motin 106b50bb79cSAlexander Motin if (ch->attached) 107b50bb79cSAlexander Motin return (0); 108b50bb79cSAlexander Motin ch->attached = 1; 109b50bb79cSAlexander Motin 1106a03316aSAlexander Motin /* allocate the io port range */ 1116a03316aSAlexander Motin rid = ATA_IOADDR_RID; 1126a03316aSAlexander Motin if (!(io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1136a03316aSAlexander Motin ATA_IOSIZE, RF_ACTIVE))) 1146a03316aSAlexander Motin return ENXIO; 1156a03316aSAlexander Motin 1166a03316aSAlexander Motin /* set the altport range */ 1176a03316aSAlexander Motin if (bus_get_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, &tmp, &tmp)) { 1186a03316aSAlexander Motin bus_set_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, 1196a03316aSAlexander Motin rman_get_start(io) + ATA_CTLOFFSET, ATA_CTLIOSIZE); 1206a03316aSAlexander Motin } 1216a03316aSAlexander Motin 1226a03316aSAlexander Motin /* allocate the altport range */ 1236a03316aSAlexander Motin rid = ATA_CTLADDR_RID; 1246a03316aSAlexander Motin if (!(ctlio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1256a03316aSAlexander Motin ATA_CTLIOSIZE, RF_ACTIVE))) { 1266a03316aSAlexander Motin bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 1276a03316aSAlexander Motin return ENXIO; 1286a03316aSAlexander Motin } 1296a03316aSAlexander Motin 130566cf07aSSøren Schmidt /* setup the resource vectors */ 1310e1eb682SSøren Schmidt for (i = ATA_DATA; i <= ATA_COMMAND; i++) { 132566cf07aSSøren Schmidt ch->r_io[i].res = io; 133566cf07aSSøren Schmidt ch->r_io[i].offset = i; 134566cf07aSSøren Schmidt } 1350e1eb682SSøren Schmidt ch->r_io[ATA_CONTROL].res = ctlio; 1360e1eb682SSøren Schmidt ch->r_io[ATA_CONTROL].offset = 0; 1370e1eb682SSøren Schmidt ch->r_io[ATA_IDX_ADDR].res = io; 1380068f98fSSøren Schmidt ata_default_registers(dev); 139566cf07aSSøren Schmidt 140566cf07aSSøren Schmidt /* initialize softc for this channel */ 1416ddce903SSøren Schmidt ch->unit = 0; 1426ddce903SSøren Schmidt ch->flags |= ATA_USE_16BIT; 1430068f98fSSøren Schmidt ata_generic_hw(dev); 1446a03316aSAlexander Motin return ata_attach(dev); 1456a03316aSAlexander Motin } 1466a03316aSAlexander Motin 1476a03316aSAlexander Motin static int 1486a03316aSAlexander Motin ata_isa_detach(device_t dev) 1496a03316aSAlexander Motin { 1506a03316aSAlexander Motin struct ata_channel *ch = device_get_softc(dev); 1516a03316aSAlexander Motin int error; 1526a03316aSAlexander Motin 153b50bb79cSAlexander Motin if (!ch->attached) 154b50bb79cSAlexander Motin return (0); 155b50bb79cSAlexander Motin ch->attached = 0; 156b50bb79cSAlexander Motin 1576a03316aSAlexander Motin error = ata_detach(dev); 1586a03316aSAlexander Motin 1596a03316aSAlexander Motin bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, 1606a03316aSAlexander Motin ch->r_io[ATA_CONTROL].res); 1616a03316aSAlexander Motin bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, 1626a03316aSAlexander Motin ch->r_io[ATA_IDX_ADDR].res); 1636a03316aSAlexander Motin return (error); 164331c488dSSøren Schmidt } 165331c488dSSøren Schmidt 16679ca9100SAlexander Motin static int 16779ca9100SAlexander Motin ata_isa_suspend(device_t dev) 16879ca9100SAlexander Motin { 16979ca9100SAlexander Motin struct ata_channel *ch = device_get_softc(dev); 17079ca9100SAlexander Motin 17179ca9100SAlexander Motin if (!ch->attached) 17279ca9100SAlexander Motin return (0); 17379ca9100SAlexander Motin 17479ca9100SAlexander Motin return ata_suspend(dev); 17579ca9100SAlexander Motin } 17679ca9100SAlexander Motin 17779ca9100SAlexander Motin static int 17879ca9100SAlexander Motin ata_isa_resume(device_t dev) 17979ca9100SAlexander Motin { 18079ca9100SAlexander Motin struct ata_channel *ch = device_get_softc(dev); 18179ca9100SAlexander Motin 18279ca9100SAlexander Motin if (!ch->attached) 18379ca9100SAlexander Motin return (0); 18479ca9100SAlexander Motin 18579ca9100SAlexander Motin return ata_resume(dev); 18679ca9100SAlexander Motin } 18779ca9100SAlexander Motin 18879ca9100SAlexander Motin 189331c488dSSøren Schmidt static device_method_t ata_isa_methods[] = { 190331c488dSSøren Schmidt /* device interface */ 191331c488dSSøren Schmidt DEVMETHOD(device_probe, ata_isa_probe), 1926a03316aSAlexander Motin DEVMETHOD(device_attach, ata_isa_attach), 1936a03316aSAlexander Motin DEVMETHOD(device_detach, ata_isa_detach), 19479ca9100SAlexander Motin DEVMETHOD(device_suspend, ata_isa_suspend), 19579ca9100SAlexander Motin DEVMETHOD(device_resume, ata_isa_resume), 196d5514ba3SSøren Schmidt 197331c488dSSøren Schmidt { 0, 0 } 198331c488dSSøren Schmidt }; 199331c488dSSøren Schmidt 200331c488dSSøren Schmidt static driver_t ata_isa_driver = { 201331c488dSSøren Schmidt "ata", 202331c488dSSøren Schmidt ata_isa_methods, 2036ddce903SSøren Schmidt sizeof(struct ata_channel), 204331c488dSSøren Schmidt }; 205331c488dSSøren Schmidt 206331c488dSSøren Schmidt DRIVER_MODULE(ata, isa, ata_isa_driver, ata_devclass, 0, 0); 2078ca4df32SSøren Schmidt MODULE_DEPEND(ata, ata, 1, 1, 1); 208