xref: /freebsd/sys/dev/ata/ata-isa.c (revision fdafd315)
1331c488dSSøren Schmidt /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 
29331c488dSSøren Schmidt #include <sys/param.h>
30331c488dSSøren Schmidt #include <sys/systm.h>
31c55607ceSSøren Schmidt #include <sys/ata.h>
32331c488dSSøren Schmidt #include <sys/kernel.h>
33331c488dSSøren Schmidt #include <sys/module.h>
34331c488dSSøren Schmidt #include <sys/bus.h>
35331c488dSSøren Schmidt #include <sys/malloc.h>
36a7a120f6SSøren Schmidt #include <sys/sema.h>
375fdbb0d2SSøren Schmidt #include <sys/taskqueue.h>
385df3ca78SSøren Schmidt #include <vm/uma.h>
39331c488dSSøren Schmidt #include <machine/stdarg.h>
40331c488dSSøren Schmidt #include <machine/resource.h>
41331c488dSSøren Schmidt #include <machine/bus.h>
42331c488dSSøren Schmidt #include <sys/rman.h>
43331c488dSSøren Schmidt #include <isa/isavar.h>
44331c488dSSøren Schmidt #include <dev/ata/ata-all.h>
45d5514ba3SSøren Schmidt #include <ata_if.h>
46331c488dSSøren Schmidt 
47331c488dSSøren Schmidt /* local vars */
48331c488dSSøren Schmidt static struct isa_pnp_id ata_ids[] = {
49331c488dSSøren Schmidt     {0x0006d041,        "Generic ESDI/IDE/ATA controller"},     /* PNP0600 */
50331c488dSSøren Schmidt     {0x0106d041,        "Plus Hardcard II"},                    /* PNP0601 */
51331c488dSSøren Schmidt     {0x0206d041,        "Plus Hardcard IIXL/EZ"},               /* PNP0602 */
52331c488dSSøren Schmidt     {0x0306d041,        "Generic ATA"},                         /* PNP0603 */
53bb5bdd38SSøren Schmidt 								/* PNP0680 */
54bb5bdd38SSøren Schmidt     {0x8006d041,        "Standard bus mastering IDE hard disk controller"},
55331c488dSSøren Schmidt     {0}
56331c488dSSøren Schmidt };
57331c488dSSøren Schmidt 
586192895dSSøren Schmidt static int
ata_isa_probe(device_t dev)59331c488dSSøren Schmidt ata_isa_probe(device_t dev)
60331c488dSSøren Schmidt {
610e1eb682SSøren Schmidt     struct resource *io = NULL, *ctlio = NULL;
622dd1bdf1SJustin Hibbits     rman_res_t tmp;
636a03316aSAlexander Motin     int rid;
64331c488dSSøren Schmidt 
65331c488dSSøren Schmidt     /* check isapnp ids */
66331c488dSSøren Schmidt     if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO)
67331c488dSSøren Schmidt 	return ENXIO;
68331c488dSSøren Schmidt 
698ba4488cSSøren Schmidt     /* allocate the io port range */
70331c488dSSøren Schmidt     rid = ATA_IOADDR_RID;
71c47476d7SJustin Hibbits     if (!(io = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
72ecd6c15dSSøren Schmidt 					   ATA_IOSIZE, RF_ACTIVE)))
73566cf07aSSøren Schmidt 	return ENXIO;
74331c488dSSøren Schmidt 
75331c488dSSøren Schmidt     /* set the altport range */
760e1eb682SSøren Schmidt     if (bus_get_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, &tmp, &tmp)) {
770e1eb682SSøren Schmidt 	bus_set_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
780e1eb682SSøren Schmidt 			 rman_get_start(io) + ATA_CTLOFFSET, ATA_CTLIOSIZE);
79331c488dSSøren Schmidt     }
80331c488dSSøren Schmidt 
81566cf07aSSøren Schmidt     /* allocate the altport range */
820e1eb682SSøren Schmidt     rid = ATA_CTLADDR_RID;
83c47476d7SJustin Hibbits     if (!(ctlio = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
84ecd6c15dSSøren Schmidt 					      ATA_CTLIOSIZE, RF_ACTIVE))) {
85566cf07aSSøren Schmidt 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
86566cf07aSSøren Schmidt 	return ENXIO;
87566cf07aSSøren Schmidt     }
88566cf07aSSøren Schmidt 
896a03316aSAlexander Motin     /* Release resources to reallocate on attach. */
906a03316aSAlexander Motin     bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlio);
916a03316aSAlexander Motin     bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
926a03316aSAlexander Motin 
93c779dc14SAlexander Motin     device_set_desc(dev, "ATA channel");
946a03316aSAlexander Motin     return (ata_probe(dev));
956a03316aSAlexander Motin }
966a03316aSAlexander Motin 
976a03316aSAlexander Motin static int
ata_isa_attach(device_t dev)986a03316aSAlexander Motin ata_isa_attach(device_t dev)
996a03316aSAlexander Motin {
1006a03316aSAlexander Motin     struct ata_channel *ch = device_get_softc(dev);
1016a03316aSAlexander Motin     struct resource *io = NULL, *ctlio = NULL;
1022dd1bdf1SJustin Hibbits     rman_res_t tmp;
1036a03316aSAlexander Motin     int i, rid;
1046a03316aSAlexander Motin 
105b50bb79cSAlexander Motin     if (ch->attached)
106b50bb79cSAlexander Motin 	return (0);
107b50bb79cSAlexander Motin     ch->attached = 1;
108b50bb79cSAlexander Motin 
1096a03316aSAlexander Motin     /* allocate the io port range */
1106a03316aSAlexander Motin     rid = ATA_IOADDR_RID;
111c47476d7SJustin Hibbits     if (!(io = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
1126a03316aSAlexander Motin 					   ATA_IOSIZE, RF_ACTIVE)))
1136a03316aSAlexander Motin 	return ENXIO;
1146a03316aSAlexander Motin 
1156a03316aSAlexander Motin     /* set the altport range */
1166a03316aSAlexander Motin     if (bus_get_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, &tmp, &tmp)) {
1176a03316aSAlexander Motin 	bus_set_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
1186a03316aSAlexander Motin 			 rman_get_start(io) + ATA_CTLOFFSET, ATA_CTLIOSIZE);
1196a03316aSAlexander Motin     }
1206a03316aSAlexander Motin 
1216a03316aSAlexander Motin     /* allocate the altport range */
1226a03316aSAlexander Motin     rid = ATA_CTLADDR_RID;
123c47476d7SJustin Hibbits     if (!(ctlio = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
1246a03316aSAlexander Motin 					      ATA_CTLIOSIZE, RF_ACTIVE))) {
1256a03316aSAlexander Motin 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
1266a03316aSAlexander Motin 	return ENXIO;
1276a03316aSAlexander Motin     }
1286a03316aSAlexander Motin 
129566cf07aSSøren Schmidt     /* setup the resource vectors */
1300e1eb682SSøren Schmidt     for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
131566cf07aSSøren Schmidt 	ch->r_io[i].res = io;
132566cf07aSSøren Schmidt 	ch->r_io[i].offset = i;
133566cf07aSSøren Schmidt     }
1340e1eb682SSøren Schmidt     ch->r_io[ATA_CONTROL].res = ctlio;
1350e1eb682SSøren Schmidt     ch->r_io[ATA_CONTROL].offset = 0;
1360e1eb682SSøren Schmidt     ch->r_io[ATA_IDX_ADDR].res = io;
1370068f98fSSøren Schmidt     ata_default_registers(dev);
138566cf07aSSøren Schmidt 
139566cf07aSSøren Schmidt     /* initialize softc for this channel */
1406ddce903SSøren Schmidt     ch->unit = 0;
1416ddce903SSøren Schmidt     ch->flags |= ATA_USE_16BIT;
1420068f98fSSøren Schmidt     ata_generic_hw(dev);
1436a03316aSAlexander Motin     return ata_attach(dev);
1446a03316aSAlexander Motin }
1456a03316aSAlexander Motin 
1466a03316aSAlexander Motin static int
ata_isa_detach(device_t dev)1476a03316aSAlexander Motin ata_isa_detach(device_t dev)
1486a03316aSAlexander Motin {
1496a03316aSAlexander Motin     struct ata_channel *ch = device_get_softc(dev);
1506a03316aSAlexander Motin     int error;
1516a03316aSAlexander Motin 
152b50bb79cSAlexander Motin     if (!ch->attached)
153b50bb79cSAlexander Motin 	return (0);
154b50bb79cSAlexander Motin     ch->attached = 0;
155b50bb79cSAlexander Motin 
1566a03316aSAlexander Motin     error = ata_detach(dev);
1576a03316aSAlexander Motin 
1586a03316aSAlexander Motin     bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
1596a03316aSAlexander Motin 	ch->r_io[ATA_CONTROL].res);
1606a03316aSAlexander Motin     bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID,
1616a03316aSAlexander Motin 	ch->r_io[ATA_IDX_ADDR].res);
1626a03316aSAlexander Motin     return (error);
163331c488dSSøren Schmidt }
164331c488dSSøren Schmidt 
16579ca9100SAlexander Motin static int
ata_isa_suspend(device_t dev)16679ca9100SAlexander Motin ata_isa_suspend(device_t dev)
16779ca9100SAlexander Motin {
16879ca9100SAlexander Motin     struct ata_channel *ch = device_get_softc(dev);
16979ca9100SAlexander Motin 
17079ca9100SAlexander Motin     if (!ch->attached)
17179ca9100SAlexander Motin 	return (0);
17279ca9100SAlexander Motin 
17379ca9100SAlexander Motin     return ata_suspend(dev);
17479ca9100SAlexander Motin }
17579ca9100SAlexander Motin 
17679ca9100SAlexander Motin static int
ata_isa_resume(device_t dev)17779ca9100SAlexander Motin ata_isa_resume(device_t dev)
17879ca9100SAlexander Motin {
17979ca9100SAlexander Motin     struct ata_channel *ch = device_get_softc(dev);
18079ca9100SAlexander Motin 
18179ca9100SAlexander Motin     if (!ch->attached)
18279ca9100SAlexander Motin 	return (0);
18379ca9100SAlexander Motin 
18479ca9100SAlexander Motin     return ata_resume(dev);
18579ca9100SAlexander Motin }
18679ca9100SAlexander Motin 
187331c488dSSøren Schmidt static device_method_t ata_isa_methods[] = {
188331c488dSSøren Schmidt     /* device interface */
189331c488dSSøren Schmidt     DEVMETHOD(device_probe,     ata_isa_probe),
1906a03316aSAlexander Motin     DEVMETHOD(device_attach,    ata_isa_attach),
1916a03316aSAlexander Motin     DEVMETHOD(device_detach,    ata_isa_detach),
19279ca9100SAlexander Motin     DEVMETHOD(device_suspend,   ata_isa_suspend),
19379ca9100SAlexander Motin     DEVMETHOD(device_resume,    ata_isa_resume),
194d5514ba3SSøren Schmidt 
195d2ce15bdSMarius Strobl     DEVMETHOD_END
196331c488dSSøren Schmidt };
197331c488dSSøren Schmidt 
198331c488dSSøren Schmidt static driver_t ata_isa_driver = {
199331c488dSSøren Schmidt     "ata",
200331c488dSSøren Schmidt     ata_isa_methods,
2016ddce903SSøren Schmidt     sizeof(struct ata_channel),
202331c488dSSøren Schmidt };
203331c488dSSøren Schmidt 
204d5a7306cSJohn Baldwin DRIVER_MODULE(ata, isa, ata_isa_driver, NULL, NULL);
2058ca4df32SSøren Schmidt MODULE_DEPEND(ata, ata, 1, 1, 1);
206d6b66397SWarner Losh ISA_PNP_INFO(ata_ids);
207