xref: /freebsd/sys/dev/ata/ata-isa.c (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/ata.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/malloc.h>
36 #include <sys/sema.h>
37 #include <sys/taskqueue.h>
38 #include <vm/uma.h>
39 #include <machine/stdarg.h>
40 #include <machine/resource.h>
41 #include <machine/bus.h>
42 #include <sys/rman.h>
43 #include <isa/isavar.h>
44 #include <dev/ata/ata-all.h>
45 #include <ata_if.h>
46 
47 /* local vars */
48 static struct isa_pnp_id ata_ids[] = {
49     {0x0006d041,        "Generic ESDI/IDE/ATA controller"},     /* PNP0600 */
50     {0x0106d041,        "Plus Hardcard II"},                    /* PNP0601 */
51     {0x0206d041,        "Plus Hardcard IIXL/EZ"},               /* PNP0602 */
52     {0x0306d041,        "Generic ATA"},                         /* PNP0603 */
53 								/* PNP0680 */
54     {0x8006d041,        "Standard bus mastering IDE hard disk controller"},
55     {0}
56 };
57 
58 static int
59 ata_isa_probe(device_t dev)
60 {
61     struct resource *io = NULL, *ctlio = NULL;
62     rman_res_t tmp;
63     int rid;
64 
65     /* check isapnp ids */
66     if (ISA_PNP_PROBE(device_get_parent(dev), dev, ata_ids) == ENXIO)
67 	return ENXIO;
68 
69     /* allocate the io port range */
70     rid = ATA_IOADDR_RID;
71     if (!(io = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
72 					   ATA_IOSIZE, RF_ACTIVE)))
73 	return ENXIO;
74 
75     /* set the altport range */
76     if (bus_get_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, &tmp, &tmp)) {
77 	bus_set_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
78 			 rman_get_start(io) + ATA_CTLOFFSET, ATA_CTLIOSIZE);
79     }
80 
81     /* allocate the altport range */
82     rid = ATA_CTLADDR_RID;
83     if (!(ctlio = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
84 					      ATA_CTLIOSIZE, RF_ACTIVE))) {
85 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
86 	return ENXIO;
87     }
88 
89     /* Release resources to reallocate on attach. */
90     bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlio);
91     bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
92 
93     device_set_desc(dev, "ATA channel");
94     return (ata_probe(dev));
95 }
96 
97 static int
98 ata_isa_attach(device_t dev)
99 {
100     struct ata_channel *ch = device_get_softc(dev);
101     struct resource *io = NULL, *ctlio = NULL;
102     rman_res_t tmp;
103     int i, rid;
104 
105     if (ch->attached)
106 	return (0);
107     ch->attached = 1;
108 
109     /* allocate the io port range */
110     rid = ATA_IOADDR_RID;
111     if (!(io = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
112 					   ATA_IOSIZE, RF_ACTIVE)))
113 	return ENXIO;
114 
115     /* set the altport range */
116     if (bus_get_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, &tmp, &tmp)) {
117 	bus_set_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
118 			 rman_get_start(io) + ATA_CTLOFFSET, ATA_CTLIOSIZE);
119     }
120 
121     /* allocate the altport range */
122     rid = ATA_CTLADDR_RID;
123     if (!(ctlio = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
124 					      ATA_CTLIOSIZE, RF_ACTIVE))) {
125 	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
126 	return ENXIO;
127     }
128 
129     /* setup the resource vectors */
130     for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
131 	ch->r_io[i].res = io;
132 	ch->r_io[i].offset = i;
133     }
134     ch->r_io[ATA_CONTROL].res = ctlio;
135     ch->r_io[ATA_CONTROL].offset = 0;
136     ch->r_io[ATA_IDX_ADDR].res = io;
137     ata_default_registers(dev);
138 
139     /* initialize softc for this channel */
140     ch->unit = 0;
141     ch->flags |= ATA_USE_16BIT;
142     ata_generic_hw(dev);
143     return ata_attach(dev);
144 }
145 
146 static int
147 ata_isa_detach(device_t dev)
148 {
149     struct ata_channel *ch = device_get_softc(dev);
150     int error;
151 
152     if (!ch->attached)
153 	return (0);
154     ch->attached = 0;
155 
156     error = ata_detach(dev);
157 
158     bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
159 	ch->r_io[ATA_CONTROL].res);
160     bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID,
161 	ch->r_io[ATA_IDX_ADDR].res);
162     return (error);
163 }
164 
165 static int
166 ata_isa_suspend(device_t dev)
167 {
168     struct ata_channel *ch = device_get_softc(dev);
169 
170     if (!ch->attached)
171 	return (0);
172 
173     return ata_suspend(dev);
174 }
175 
176 static int
177 ata_isa_resume(device_t dev)
178 {
179     struct ata_channel *ch = device_get_softc(dev);
180 
181     if (!ch->attached)
182 	return (0);
183 
184     return ata_resume(dev);
185 }
186 
187 static device_method_t ata_isa_methods[] = {
188     /* device interface */
189     DEVMETHOD(device_probe,     ata_isa_probe),
190     DEVMETHOD(device_attach,    ata_isa_attach),
191     DEVMETHOD(device_detach,    ata_isa_detach),
192     DEVMETHOD(device_suspend,   ata_isa_suspend),
193     DEVMETHOD(device_resume,    ata_isa_resume),
194 
195     DEVMETHOD_END
196 };
197 
198 static driver_t ata_isa_driver = {
199     "ata",
200     ata_isa_methods,
201     sizeof(struct ata_channel),
202 };
203 
204 DRIVER_MODULE(ata, isa, ata_isa_driver, NULL, NULL);
205 MODULE_DEPEND(ata, ata, 1, 1, 1);
206 ISA_PNP_INFO(ata_ids);
207