1331c488dSSøren Schmidt /*- 2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3718cf2ccSPedro F. Giffuni * 49a14aa01SUlrich Spörlein * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org> 5331c488dSSøren Schmidt * All rights reserved. 6331c488dSSøren Schmidt * 7331c488dSSøren Schmidt * Redistribution and use in source and binary forms, with or without 8331c488dSSøren Schmidt * modification, are permitted provided that the following conditions 9331c488dSSøren Schmidt * are met: 10331c488dSSøren Schmidt * 1. Redistributions of source code must retain the above copyright 11331c488dSSøren Schmidt * notice, this list of conditions and the following disclaimer, 12331c488dSSøren Schmidt * without modification, immediately at the beginning of the file. 13331c488dSSøren Schmidt * 2. Redistributions in binary form must reproduce the above copyright 14331c488dSSøren Schmidt * notice, this list of conditions and the following disclaimer in the 15331c488dSSøren Schmidt * documentation and/or other materials provided with the distribution. 16331c488dSSøren Schmidt * 17331c488dSSøren Schmidt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18331c488dSSøren Schmidt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19331c488dSSøren Schmidt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20331c488dSSøren Schmidt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21331c488dSSøren Schmidt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22331c488dSSøren Schmidt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23331c488dSSøren Schmidt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24331c488dSSøren Schmidt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25331c488dSSøren Schmidt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26331c488dSSøren Schmidt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27331c488dSSøren Schmidt */ 28331c488dSSøren Schmidt 29aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 30aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 31aad970f1SDavid E. O'Brien 32331c488dSSøren Schmidt #include <sys/param.h> 33331c488dSSøren Schmidt #include <sys/systm.h> 34c55607ceSSøren Schmidt #include <sys/ata.h> 35331c488dSSøren Schmidt #include <sys/kernel.h> 36331c488dSSøren Schmidt #include <sys/module.h> 37331c488dSSøren Schmidt #include <sys/bus.h> 38331c488dSSøren Schmidt #include <sys/malloc.h> 39a7a120f6SSøren Schmidt #include <sys/sema.h> 405fdbb0d2SSøren Schmidt #include <sys/taskqueue.h> 415df3ca78SSøren Schmidt #include <vm/uma.h> 42331c488dSSøren Schmidt #include <machine/stdarg.h> 43331c488dSSøren Schmidt #include <machine/resource.h> 44331c488dSSøren Schmidt #include <machine/bus.h> 45331c488dSSøren Schmidt #include <sys/rman.h> 46331c488dSSøren Schmidt #include <isa/isavar.h> 47331c488dSSøren Schmidt #include <dev/ata/ata-all.h> 48d5514ba3SSøren Schmidt #include <ata_if.h> 49331c488dSSøren Schmidt 50331c488dSSøren Schmidt /* local vars */ 51331c488dSSøren Schmidt static struct isa_pnp_id ata_ids[] = { 52331c488dSSøren Schmidt {0x0006d041, "Generic ESDI/IDE/ATA controller"}, /* PNP0600 */ 53331c488dSSøren Schmidt {0x0106d041, "Plus Hardcard II"}, /* PNP0601 */ 54331c488dSSøren Schmidt {0x0206d041, "Plus Hardcard IIXL/EZ"}, /* PNP0602 */ 55331c488dSSøren Schmidt {0x0306d041, "Generic ATA"}, /* PNP0603 */ 56bb5bdd38SSøren Schmidt /* PNP0680 */ 57bb5bdd38SSøren Schmidt {0x8006d041, "Standard bus mastering IDE hard disk controller"}, 58331c488dSSøren Schmidt {0} 59331c488dSSøren Schmidt }; 60331c488dSSøren Schmidt 616192895dSSøren Schmidt static int 62331c488dSSøren Schmidt ata_isa_probe(device_t dev) 63331c488dSSøren Schmidt { 640e1eb682SSøren Schmidt struct resource *io = NULL, *ctlio = NULL; 652dd1bdf1SJustin Hibbits rman_res_t tmp; 666a03316aSAlexander Motin int rid; 67331c488dSSøren Schmidt 68331c488dSSøren Schmidt /* check isapnp ids */ 69331c488dSSøren Schmidt if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO) 70331c488dSSøren Schmidt return ENXIO; 71331c488dSSøren Schmidt 728ba4488cSSøren Schmidt /* allocate the io port range */ 73331c488dSSøren Schmidt rid = ATA_IOADDR_RID; 74c47476d7SJustin Hibbits if (!(io = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid, 75ecd6c15dSSøren Schmidt ATA_IOSIZE, RF_ACTIVE))) 76566cf07aSSøren Schmidt return ENXIO; 77331c488dSSøren Schmidt 78331c488dSSøren Schmidt /* set the altport range */ 790e1eb682SSøren Schmidt if (bus_get_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, &tmp, &tmp)) { 800e1eb682SSøren Schmidt bus_set_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, 810e1eb682SSøren Schmidt rman_get_start(io) + ATA_CTLOFFSET, ATA_CTLIOSIZE); 82331c488dSSøren Schmidt } 83331c488dSSøren Schmidt 84566cf07aSSøren Schmidt /* allocate the altport range */ 850e1eb682SSøren Schmidt rid = ATA_CTLADDR_RID; 86c47476d7SJustin Hibbits if (!(ctlio = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid, 87ecd6c15dSSøren Schmidt ATA_CTLIOSIZE, RF_ACTIVE))) { 88566cf07aSSøren Schmidt bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 89566cf07aSSøren Schmidt return ENXIO; 90566cf07aSSøren Schmidt } 91566cf07aSSøren Schmidt 926a03316aSAlexander Motin /* Release resources to reallocate on attach. */ 936a03316aSAlexander Motin bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlio); 946a03316aSAlexander Motin bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 956a03316aSAlexander Motin 96c779dc14SAlexander Motin device_set_desc(dev, "ATA channel"); 976a03316aSAlexander Motin return (ata_probe(dev)); 986a03316aSAlexander Motin } 996a03316aSAlexander Motin 1006a03316aSAlexander Motin static int 1016a03316aSAlexander Motin ata_isa_attach(device_t dev) 1026a03316aSAlexander Motin { 1036a03316aSAlexander Motin struct ata_channel *ch = device_get_softc(dev); 1046a03316aSAlexander Motin struct resource *io = NULL, *ctlio = NULL; 1052dd1bdf1SJustin Hibbits rman_res_t tmp; 1066a03316aSAlexander Motin int i, rid; 1076a03316aSAlexander Motin 108b50bb79cSAlexander Motin if (ch->attached) 109b50bb79cSAlexander Motin return (0); 110b50bb79cSAlexander Motin ch->attached = 1; 111b50bb79cSAlexander Motin 1126a03316aSAlexander Motin /* allocate the io port range */ 1136a03316aSAlexander Motin rid = ATA_IOADDR_RID; 114c47476d7SJustin Hibbits if (!(io = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid, 1156a03316aSAlexander Motin ATA_IOSIZE, RF_ACTIVE))) 1166a03316aSAlexander Motin return ENXIO; 1176a03316aSAlexander Motin 1186a03316aSAlexander Motin /* set the altport range */ 1196a03316aSAlexander Motin if (bus_get_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, &tmp, &tmp)) { 1206a03316aSAlexander Motin bus_set_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, 1216a03316aSAlexander Motin rman_get_start(io) + ATA_CTLOFFSET, ATA_CTLIOSIZE); 1226a03316aSAlexander Motin } 1236a03316aSAlexander Motin 1246a03316aSAlexander Motin /* allocate the altport range */ 1256a03316aSAlexander Motin rid = ATA_CTLADDR_RID; 126c47476d7SJustin Hibbits if (!(ctlio = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid, 1276a03316aSAlexander Motin ATA_CTLIOSIZE, RF_ACTIVE))) { 1286a03316aSAlexander Motin bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io); 1296a03316aSAlexander Motin return ENXIO; 1306a03316aSAlexander Motin } 1316a03316aSAlexander Motin 132566cf07aSSøren Schmidt /* setup the resource vectors */ 1330e1eb682SSøren Schmidt for (i = ATA_DATA; i <= ATA_COMMAND; i++) { 134566cf07aSSøren Schmidt ch->r_io[i].res = io; 135566cf07aSSøren Schmidt ch->r_io[i].offset = i; 136566cf07aSSøren Schmidt } 1370e1eb682SSøren Schmidt ch->r_io[ATA_CONTROL].res = ctlio; 1380e1eb682SSøren Schmidt ch->r_io[ATA_CONTROL].offset = 0; 1390e1eb682SSøren Schmidt ch->r_io[ATA_IDX_ADDR].res = io; 1400068f98fSSøren Schmidt ata_default_registers(dev); 141566cf07aSSøren Schmidt 142566cf07aSSøren Schmidt /* initialize softc for this channel */ 1436ddce903SSøren Schmidt ch->unit = 0; 1446ddce903SSøren Schmidt ch->flags |= ATA_USE_16BIT; 1450068f98fSSøren Schmidt ata_generic_hw(dev); 1466a03316aSAlexander Motin return ata_attach(dev); 1476a03316aSAlexander Motin } 1486a03316aSAlexander Motin 1496a03316aSAlexander Motin static int 1506a03316aSAlexander Motin ata_isa_detach(device_t dev) 1516a03316aSAlexander Motin { 1526a03316aSAlexander Motin struct ata_channel *ch = device_get_softc(dev); 1536a03316aSAlexander Motin int error; 1546a03316aSAlexander Motin 155b50bb79cSAlexander Motin if (!ch->attached) 156b50bb79cSAlexander Motin return (0); 157b50bb79cSAlexander Motin ch->attached = 0; 158b50bb79cSAlexander Motin 1596a03316aSAlexander Motin error = ata_detach(dev); 1606a03316aSAlexander Motin 1616a03316aSAlexander Motin bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, 1626a03316aSAlexander Motin ch->r_io[ATA_CONTROL].res); 1636a03316aSAlexander Motin bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, 1646a03316aSAlexander Motin ch->r_io[ATA_IDX_ADDR].res); 1656a03316aSAlexander Motin return (error); 166331c488dSSøren Schmidt } 167331c488dSSøren Schmidt 16879ca9100SAlexander Motin static int 16979ca9100SAlexander Motin ata_isa_suspend(device_t dev) 17079ca9100SAlexander Motin { 17179ca9100SAlexander Motin struct ata_channel *ch = device_get_softc(dev); 17279ca9100SAlexander Motin 17379ca9100SAlexander Motin if (!ch->attached) 17479ca9100SAlexander Motin return (0); 17579ca9100SAlexander Motin 17679ca9100SAlexander Motin return ata_suspend(dev); 17779ca9100SAlexander Motin } 17879ca9100SAlexander Motin 17979ca9100SAlexander Motin static int 18079ca9100SAlexander Motin ata_isa_resume(device_t dev) 18179ca9100SAlexander Motin { 18279ca9100SAlexander Motin struct ata_channel *ch = device_get_softc(dev); 18379ca9100SAlexander Motin 18479ca9100SAlexander Motin if (!ch->attached) 18579ca9100SAlexander Motin return (0); 18679ca9100SAlexander Motin 18779ca9100SAlexander Motin return ata_resume(dev); 18879ca9100SAlexander Motin } 18979ca9100SAlexander Motin 190331c488dSSøren Schmidt static device_method_t ata_isa_methods[] = { 191331c488dSSøren Schmidt /* device interface */ 192331c488dSSøren Schmidt DEVMETHOD(device_probe, ata_isa_probe), 1936a03316aSAlexander Motin DEVMETHOD(device_attach, ata_isa_attach), 1946a03316aSAlexander Motin DEVMETHOD(device_detach, ata_isa_detach), 19579ca9100SAlexander Motin DEVMETHOD(device_suspend, ata_isa_suspend), 19679ca9100SAlexander Motin DEVMETHOD(device_resume, ata_isa_resume), 197d5514ba3SSøren Schmidt 198d2ce15bdSMarius Strobl DEVMETHOD_END 199331c488dSSøren Schmidt }; 200331c488dSSøren Schmidt 201331c488dSSøren Schmidt static driver_t ata_isa_driver = { 202331c488dSSøren Schmidt "ata", 203331c488dSSøren Schmidt ata_isa_methods, 2046ddce903SSøren Schmidt sizeof(struct ata_channel), 205331c488dSSøren Schmidt }; 206331c488dSSøren Schmidt 207d5a7306cSJohn Baldwin DRIVER_MODULE(ata, isa, ata_isa_driver, NULL, NULL); 2088ca4df32SSøren Schmidt MODULE_DEPEND(ata, ata, 1, 1, 1); 209d6b66397SWarner Losh ISA_PNP_INFO(ata_ids); 210