xref: /freebsd/sys/powerpc/powermac/ata_macio.c (revision 6780ab54325a71e7e70112b11657973edde8655e)
1 /*
2  * Copyright 2002 by Peter Grehan. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 /*
31  * Mac-io ATA controller
32  */
33 #include "opt_ata.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/malloc.h>
40 #include <machine/stdarg.h>
41 #include <machine/resource.h>
42 #include <machine/bus.h>
43 #include <sys/rman.h>
44 #include <dev/ata/ata-all.h>
45 
46 #include <dev/ofw/openfirm.h>
47 #include <powerpc/powermac/maciovar.h>
48 
49 struct ata_macio_softc {
50 	struct			resource *sc_memres;
51 	int			sc_memrid;
52 	bus_space_tag_t		sc_bt;
53 	bus_space_handle_t	sc_bh;
54 	struct			rman sc_mem_rman;
55 };
56 
57 /*
58  * Define the macio ata bus attachment. This creates a pseudo-bus that
59  * the ATA device can be attached to
60  */
61 static  int  ata_macio_attach(device_t dev);
62 static  int  ata_macio_probe(device_t dev);
63 static  int  ata_macio_print_child(device_t dev, device_t child);
64 struct resource *ata_macio_alloc_resource(device_t, device_t, int, int *,
65 					  u_long, u_long, u_long, u_int);
66 static int ata_macio_release_resource(device_t, device_t, int, int,
67 				      struct resource *);
68 
69 static device_method_t ata_macio_methods[] = {
70         /* Device interface */
71 	DEVMETHOD(device_probe,		ata_macio_probe),
72 	DEVMETHOD(device_attach,        ata_macio_attach),
73 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
74 	DEVMETHOD(device_suspend,       bus_generic_suspend),
75 	DEVMETHOD(device_resume,        bus_generic_resume),
76 
77 	/* Bus methods */
78 	DEVMETHOD(bus_print_child,          ata_macio_print_child),
79 	DEVMETHOD(bus_alloc_resource,       ata_macio_alloc_resource),
80 	DEVMETHOD(bus_release_resource,     ata_macio_release_resource),
81 	DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
82 	DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
83 	DEVMETHOD(bus_setup_intr,           bus_generic_setup_intr),
84 	DEVMETHOD(bus_teardown_intr,        bus_generic_teardown_intr),
85 
86 	{ 0, 0 }
87 };
88 
89 static driver_t ata_macio_driver = {
90 	"atamacio",
91 	ata_macio_methods,
92 	sizeof(struct ata_macio_softc),
93 };
94 
95 static devclass_t ata_macio_devclass;
96 
97 DRIVER_MODULE(atamacio, macio, ata_macio_driver, ata_macio_devclass, 0, 0);
98 
99 
100 static int
101 ata_macio_probe(device_t dev)
102 {
103 	char *type = macio_get_devtype(dev);
104 
105 	if (strcmp(type, "ata") != 0)
106 		return (ENXIO);
107 
108 	/* Print keylargo/pangea ??? */
109 	device_set_desc(dev, "MacIO ATA Controller");
110 	return (0);
111 }
112 
113 
114 static int
115 ata_macio_attach(device_t dev)
116 {
117 	struct	ata_macio_softc *sc;
118 
119 	sc = device_get_softc(dev);
120 
121 	sc->sc_memrid = 0;
122 	sc->sc_memres = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->sc_memrid,
123 	    0, ~1, 1, RF_ACTIVE | PPC_BUS_SPARSE4);
124 	if (sc->sc_memres == NULL) {
125 		device_printf(dev, "could not allocate memory\n");
126 		return (ENXIO);
127 	}
128 
129 	sc->sc_bt = rman_get_bustag(sc->sc_memres);
130 	sc->sc_bh = rman_get_bushandle(sc->sc_memres);
131 
132 	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
133 	sc->sc_mem_rman.rm_descr = device_get_nameunit(dev);
134 	if (rman_init(&sc->sc_mem_rman) != 0) {
135 		device_printf(dev, "failed to init memory rman\n");
136 		bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid,
137 		    sc->sc_memres);
138 		return (ENXIO);
139 	}
140 	rman_manage_region(&sc->sc_mem_rman, 0, rman_get_size(sc->sc_memres));
141 
142 	/*
143 	 * Add a single child per controller. Should be able
144 	 * to add two
145 	 */
146 	device_add_child(dev, "ata",
147 			 devclass_find_free_unit(ata_devclass, 0));
148 
149 	return (bus_generic_attach(dev));
150 }
151 
152 
153 static int
154 ata_macio_print_child(device_t dev, device_t child)
155 {
156     int retval = 0;
157 
158     retval += bus_print_child_header(dev, child);
159     retval += bus_print_child_footer(dev, child);
160 
161     return (retval);
162 }
163 
164 /* offset to control registers from base */
165 #define ATA_MACIO_ALTOFFSET 0x160
166 
167 struct resource *
168 ata_macio_alloc_resource(device_t dev, device_t child, int type, int *rid,
169 			 u_long start, u_long end, u_long count, u_int flags)
170 {
171 	struct			ata_macio_softc *sc;
172 	struct			resource *res;
173 	int			myrid;
174 	bus_space_handle_t	bh;
175 
176 	sc = device_get_softc(dev);
177 	res = NULL;
178 
179 	/*
180 	 * The offset for the register bank is in the first ofw register,
181 	 * with the base address coming from the parent macio bus (but
182 	 * accessible via the ata-macio child)
183 	 */
184 	if (type == SYS_RES_IOPORT) {
185 		switch (*rid) {
186 		case ATA_IOADDR_RID:
187 			start = 0;
188 			end = (ATA_IOSIZE << 4) - 1;
189 			count = ATA_IOSIZE << 4;
190 			res = rman_reserve_resource(&sc->sc_mem_rman, start,
191 			    end, count, flags | PPC_BUS_SPARSE4, child);
192 			if (res == NULL) {
193 				device_printf(dev, "rman_reserve failed\n");
194 				return (NULL);
195 			}
196 
197 			rman_set_bustag(res, sc->sc_bt);
198 			bus_space_subregion(sc->sc_bt, sc->sc_bh,
199 			    rman_get_start(res), rman_get_size(res), &bh);
200 			rman_set_bushandle(res, bh);
201 			rman_set_rid(res, ATA_IOADDR_RID);
202 
203 			break;
204 
205 		case ATA_ALTADDR_RID:
206 			start = ATA_MACIO_ALTOFFSET;
207 			end = start + (ATA_ALTIOSIZE << 4) - 1;
208 			count = ATA_ALTIOSIZE << 4;
209 
210 			res = rman_reserve_resource(&sc->sc_mem_rman, start,
211 			    end, count, flags | PPC_BUS_SPARSE4, child);
212 			if (res == NULL) {
213 				device_printf(dev, "rman_reserve failed\n");
214 				return (NULL);
215 			}
216 
217 			rman_set_bustag(res, sc->sc_bt);
218 			bus_space_subregion(sc->sc_bt, sc->sc_bh,
219 			    rman_get_start(res), rman_get_size(res), &bh);
220 			rman_set_bushandle(res, bh);
221 			rman_set_rid(res, ATA_ALTADDR_RID);
222 
223 			break;
224 
225 		case ATA_BMADDR_RID:
226 			/* looks difficult to support DBDMA in FreeBSD... */
227 			break;
228 		}
229 		return (res);
230 
231 	} else if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
232 		/*
233 		 * Pass this on to the parent, using the IRQ from the
234 		 * ATA pseudo-bus resource
235 		 */
236 		myrid = 0;
237 		res = bus_generic_rl_alloc_resource(device_get_parent(dev),
238 		   dev, SYS_RES_IRQ, &myrid, 0, ~0, 1, flags);
239 		return (res);
240 
241 	} else {
242 		return (NULL);
243 	}
244 }
245 
246 
247 static int
248 ata_macio_release_resource(device_t dev, device_t child, int type, int rid,
249 			   struct resource *r)
250 {
251 
252 	if (type == SYS_RES_IRQ)
253 		return (bus_release_resource(device_get_parent(dev), type,
254 		    rid, r));
255 
256 	if (type == SYS_RES_IOPORT || type == SYS_RES_MEMORY)
257 		return (rman_release_resource(r));
258 
259 	return (ENXIO);
260 }
261 
262 
263 /*
264  * Define the actual ATA device. This is a sub-bus to the ata-macio layer
265  * to allow the higher layer bus to massage the resource allocation.
266  */
267 
268 static  int  ata_macio_sub_probe(device_t dev);
269 
270 static device_method_t ata_macio_sub_methods[] = {
271 	/* Device interface */
272 	DEVMETHOD(device_probe,     ata_macio_sub_probe),
273 	DEVMETHOD(device_attach,    ata_attach),
274 	DEVMETHOD(device_detach,    ata_detach),
275 	DEVMETHOD(device_resume,    ata_resume),
276 
277 	{ 0, 0 }
278 };
279 
280 static driver_t ata_macio_sub_driver = {
281 	"ata",
282 	ata_macio_sub_methods,
283 	sizeof(struct ata_channel),
284 };
285 
286 DRIVER_MODULE(ata, atamacio, ata_macio_sub_driver, ata_devclass, 0, 0);
287 
288 static int
289 ata_macio_intrnoop(struct ata_channel *ch)
290 {
291 
292 	return (1);
293 }
294 
295 static void
296 ata_macio_locknoop(struct ata_channel *ch, int type)
297 {
298 	/* XXX SMP ? */
299 }
300 
301 static int
302 ata_macio_sub_probe(device_t dev)
303 {
304 	struct	ata_channel *ch;
305 
306 	ch = device_get_softc(dev);
307 
308 	ch->unit = 0;
309 	ch->flags = ATA_USE_16BIT;
310 	ch->intr_func = ata_macio_intrnoop;
311 	ch->lock_func = ata_macio_locknoop;
312 
313 	return (ata_probe(dev));
314 }
315