pxa_obio.c revision 226832
1179595Sbenno/*-
2179595Sbenno * Copyright (c) 2006 Benno Rice.  All rights reserved.
3179595Sbenno *
4179595Sbenno * Redistribution and use in source and binary forms, with or without
5179595Sbenno * modification, are permitted provided that the following conditions
6179595Sbenno * are met:
7179595Sbenno * 1. Redistributions of source code must retain the above copyright
8179595Sbenno *    notice, this list of conditions and the following disclaimer.
9179595Sbenno * 2. Redistributions in binary form must reproduce the above copyright
10179595Sbenno *    notice, this list of conditions and the following disclaimer in the
11179595Sbenno *    documentation and/or other materials provided with the distribution.
12179595Sbenno *
13179595Sbenno * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14179595Sbenno * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15179595Sbenno * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16179595Sbenno * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17179595Sbenno * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18179595Sbenno * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19179595Sbenno * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20179595Sbenno * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21179595Sbenno * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22179595Sbenno * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23179595Sbenno */
24179595Sbenno
25179595Sbenno#include <sys/cdefs.h>
26179595Sbenno__FBSDID("$FreeBSD: head/sys/arm/xscale/pxa/pxa_obio.c 226832 2011-10-27 10:21:40Z kevlo $");
27179595Sbenno
28179595Sbenno#include <sys/param.h>
29179595Sbenno#include <sys/systm.h>
30179595Sbenno#include <sys/bus.h>
31179595Sbenno#include <sys/kernel.h>
32179595Sbenno#include <sys/module.h>
33179595Sbenno#include <sys/malloc.h>
34179595Sbenno#include <sys/rman.h>
35179595Sbenno#include <machine/bus.h>
36179595Sbenno#include <machine/intr.h>
37179595Sbenno
38179595Sbenno#include <arm/xscale/pxa/pxavar.h>
39179595Sbenno#include <arm/xscale/pxa/pxareg.h>
40179595Sbenno
41179595Sbennostatic void	pxa_identify(driver_t *, device_t);
42179595Sbennostatic int	pxa_probe(device_t);
43179595Sbennostatic int	pxa_attach(device_t);
44179595Sbenno
45179595Sbennostatic int	pxa_print_child(device_t, device_t);
46179595Sbenno
47179595Sbennostatic int	pxa_setup_intr(device_t, device_t, struct resource *, int,
48179595Sbenno		    driver_filter_t *, driver_intr_t *, void *, void **);
49179595Sbennostatic int	pxa_read_ivar(device_t, device_t, int, uintptr_t *);
50179595Sbenno
51179595Sbennostatic struct resource_list *	pxa_get_resource_list(device_t, device_t);
52179595Sbennostatic struct resource *	pxa_alloc_resource(device_t, device_t, int,
53179595Sbenno				    int *, u_long, u_long, u_long, u_int);
54179595Sbennostatic int			pxa_release_resource(device_t, device_t, int,
55179595Sbenno				    int, struct resource *);
56179595Sbennostatic int			pxa_activate_resource(device_t, device_t,
57179595Sbenno				    int, int, struct resource *);
58179595Sbenno
59179595Sbennostatic struct resource *	pxa_alloc_gpio_irq(device_t, device_t, int,
60179595Sbenno				    int *, u_long, u_long, u_long, u_int);
61179595Sbenno
62179595Sbennostruct obio_device {
63179595Sbenno	const char	*od_name;
64179595Sbenno	u_long		od_base;
65179595Sbenno	u_long		od_size;
66179595Sbenno	u_int		od_irqs[5];
67179595Sbenno	struct resource_list od_resources;
68179595Sbenno};
69179595Sbenno
70179595Sbennostatic struct obio_device obio_devices[] = {
71179595Sbenno	{ "icu", PXA2X0_INTCTL_BASE, PXA2X0_INTCTL_SIZE, { 0 } },
72179595Sbenno	{ "timer", PXA2X0_OST_BASE, PXA2X0_OST_SIZE, { PXA2X0_INT_OST0, PXA2X0_INT_OST1, PXA2X0_INT_OST2, PXA2X0_INT_OST3, 0 } },
73179595Sbenno	{ "dmac", PXA2X0_DMAC_BASE, PXA2X0_DMAC_SIZE, { PXA2X0_INT_DMA, 0 } },
74179595Sbenno	{ "gpio", PXA2X0_GPIO_BASE, PXA250_GPIO_SIZE, { PXA2X0_INT_GPIO0, PXA2X0_INT_GPIO1, PXA2X0_INT_GPION, 0 } },
75179595Sbenno	{ "uart", PXA2X0_FFUART_BASE, PXA2X0_FFUART_SIZE, { PXA2X0_INT_FFUART, 0 } },
76179595Sbenno	{ "uart", PXA2X0_BTUART_BASE, PXA2X0_BTUART_SIZE, { PXA2X0_INT_BTUART, 0 } },
77179595Sbenno	{ "uart", PXA2X0_STUART_BASE, PXA2X0_STUART_SIZE, { PXA2X0_INT_STUART, 0 } },
78179595Sbenno	{ "uart", PXA2X0_HWUART_BASE, PXA2X0_HWUART_SIZE, { PXA2X0_INT_HWUART, 0 } },
79179595Sbenno	{ "smi", PXA2X0_CS0_START, PXA2X0_CS_SIZE * 6, { 0 } },
80179595Sbenno	{ NULL, 0, 0, { 0 } }
81179595Sbenno};
82179595Sbenno
83179595Sbennovoid
84179595Sbennopxa_identify(driver_t *driver, device_t parent)
85179595Sbenno{
86179595Sbenno
87179595Sbenno	BUS_ADD_CHILD(parent, 0, "pxa", 0);
88179595Sbenno}
89179595Sbenno
90179595Sbennoint
91179595Sbennopxa_probe(device_t dev)
92179595Sbenno{
93179595Sbenno
94179595Sbenno	device_set_desc(dev, "XScale PXA On-board IO");
95179595Sbenno	return (0);
96179595Sbenno}
97179595Sbenno
98179595Sbennoint
99179595Sbennopxa_attach(device_t dev)
100179595Sbenno{
101179595Sbenno	struct		obio_softc *sc;
102179595Sbenno	struct		obio_device *od;
103179595Sbenno	int		i;
104179595Sbenno	device_t	child;
105179595Sbenno
106179595Sbenno	sc = device_get_softc(dev);
107179595Sbenno
108179595Sbenno	sc->obio_bst = obio_tag;
109179595Sbenno
110179595Sbenno	sc->obio_mem.rm_type = RMAN_ARRAY;
111179595Sbenno	sc->obio_mem.rm_descr = "PXA2X0 OBIO Memory";
112179595Sbenno	if (rman_init(&sc->obio_mem) != 0)
113179595Sbenno		panic("pxa_attach: failed to init obio mem rman");
114179595Sbenno	if (rman_manage_region(&sc->obio_mem, 0, PXA250_PERIPH_END) != 0)
115179595Sbenno		panic("pxa_attach: failed to set up obio mem rman");
116179595Sbenno
117179595Sbenno	sc->obio_irq.rm_type = RMAN_ARRAY;
118179595Sbenno	sc->obio_irq.rm_descr = "PXA2X0 OBIO IRQ";
119179595Sbenno	if (rman_init(&sc->obio_irq) != 0)
120179595Sbenno		panic("pxa_attach: failed to init obio irq rman");
121179595Sbenno	if (rman_manage_region(&sc->obio_irq, 0, 31) != 0)
122179595Sbenno		panic("pxa_attach: failed to set up obio irq rman (main irqs)");
123179595Sbenno	if (rman_manage_region(&sc->obio_irq, IRQ_GPIO0, IRQ_GPIO_MAX) != 0)
124179595Sbenno		panic("pxa_attach: failed to set up obio irq rman (gpio irqs)");
125179595Sbenno
126179595Sbenno	for (od = obio_devices; od->od_name != NULL; od++) {
127179595Sbenno		resource_list_init(&od->od_resources);
128179595Sbenno
129179595Sbenno		resource_list_add(&od->od_resources, SYS_RES_MEMORY, 0,
130179595Sbenno		    od->od_base, od->od_base + od->od_size, od->od_size);
131179595Sbenno
132179595Sbenno		for (i = 0; od->od_irqs[i] != 0; i++) {
133179595Sbenno			resource_list_add(&od->od_resources, SYS_RES_IRQ, i,
134179595Sbenno			    od->od_irqs[i], od->od_irqs[i], 1);
135179595Sbenno		}
136179595Sbenno
137179595Sbenno		child = device_add_child(dev, od->od_name, -1);
138179595Sbenno		device_set_ivars(child, od);
139179595Sbenno	}
140179595Sbenno
141179595Sbenno	bus_generic_probe(dev);
142179595Sbenno	bus_generic_attach(dev);
143179595Sbenno
144179595Sbenno	return (0);
145179595Sbenno}
146179595Sbenno
147179595Sbennostatic int
148179595Sbennopxa_print_child(device_t dev, device_t child)
149179595Sbenno{
150179595Sbenno	struct	obio_device *od;
151179595Sbenno	int	retval;
152179595Sbenno
153179595Sbenno	od = (struct obio_device *)device_get_ivars(child);
154179595Sbenno	if (od == NULL)
155179595Sbenno		panic("Unknown device on pxa0");
156179595Sbenno
157179595Sbenno	retval = 0;
158179595Sbenno
159179595Sbenno	retval += bus_print_child_header(dev, child);
160179595Sbenno
161179595Sbenno	retval += resource_list_print_type(&od->od_resources, "at mem",
162179595Sbenno	    SYS_RES_MEMORY, "0x%08lx");
163179595Sbenno	retval += resource_list_print_type(&od->od_resources, "irq",
164179595Sbenno	    SYS_RES_IRQ, "%ld");
165179595Sbenno
166179595Sbenno	retval += bus_print_child_footer(dev, child);
167179595Sbenno
168179595Sbenno	return (retval);
169179595Sbenno}
170179595Sbenno
171179595Sbennostatic int
172179595Sbennopxa_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
173179595Sbenno    driver_filter_t *filter, driver_intr_t *ithread, void *arg, void **cookiep)
174179595Sbenno{
175179595Sbenno	struct	obio_softc *sc;
176226832Skevlo	int error;
177179595Sbenno
178179595Sbenno	sc = (struct obio_softc *)device_get_softc(dev);
179179595Sbenno
180226832Skevlo	error = BUS_SETUP_INTR(device_get_parent(dev), child, irq, flags,
181226832Skevlo	    filter, ithread, arg, cookiep);
182226832Skevlo	if (error)
183226832Skevlo		return (error);
184179595Sbenno	arm_unmask_irq(rman_get_start(irq));
185179595Sbenno	return (0);
186179595Sbenno}
187179595Sbenno
188179595Sbennostatic int
189179701Skevlopxa_teardown_intr(device_t dev, device_t child, struct resource *ires,
190179701Skevlo    void *cookie)
191179701Skevlo{
192179701Skevlo	return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, ires, cookie));}
193179701Skevlo
194179701Skevlostatic int
195179595Sbennopxa_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
196179595Sbenno{
197179595Sbenno	struct	obio_device *od;
198179595Sbenno
199179595Sbenno	od = (struct obio_device *)device_get_ivars(child);
200179595Sbenno
201179595Sbenno	switch (which) {
202179595Sbenno	case PXA_IVAR_BASE:
203179595Sbenno		*((u_long *)result) = od->od_base;
204179595Sbenno		break;
205179595Sbenno
206179595Sbenno	default:
207179595Sbenno		return (ENOENT);
208179595Sbenno	}
209179595Sbenno
210179595Sbenno	return (0);
211179595Sbenno}
212179595Sbenno
213179595Sbennostatic struct resource_list *
214179595Sbennopxa_get_resource_list(device_t dev, device_t child)
215179595Sbenno{
216179595Sbenno	struct	obio_device *od;
217179595Sbenno
218179595Sbenno	od = (struct obio_device *)device_get_ivars(child);
219179595Sbenno
220179595Sbenno	if (od == NULL)
221179595Sbenno		return (NULL);
222179595Sbenno
223179595Sbenno	return (&od->od_resources);
224179595Sbenno}
225179595Sbenno
226179595Sbennostatic struct resource *
227179595Sbennopxa_alloc_resource(device_t dev, device_t child, int type, int *rid,
228179595Sbenno    u_long start, u_long end, u_long count, u_int flags)
229179595Sbenno{
230179595Sbenno	struct	obio_softc *sc;
231179595Sbenno	struct	obio_device *od;
232179595Sbenno	struct	resource *rv;
233179595Sbenno	struct	resource_list *rl;
234179595Sbenno	struct	resource_list_entry *rle;
235179595Sbenno	struct	rman *rm;
236179595Sbenno	int	needactivate;
237179595Sbenno
238179595Sbenno	sc = (struct obio_softc *)device_get_softc(dev);
239179595Sbenno	od = (struct obio_device *)device_get_ivars(child);
240179595Sbenno	rl = &od->od_resources;
241179595Sbenno
242179595Sbenno	rle = resource_list_find(rl, type, *rid);
243179595Sbenno	if (rle == NULL) {
244179595Sbenno		/* We can allocate GPIO-based IRQs lazily. */
245179595Sbenno		if (type == SYS_RES_IRQ)
246179595Sbenno			return (pxa_alloc_gpio_irq(dev, child, type, rid,
247179595Sbenno			    start, end, count, flags));
248179595Sbenno		return (NULL);
249179595Sbenno	}
250179595Sbenno	if (rle->res != NULL)
251179595Sbenno		panic("pxa_alloc_resource: resource is busy");
252179595Sbenno
253179595Sbenno	switch (type) {
254179595Sbenno	case SYS_RES_IRQ:
255179595Sbenno		rm = &sc->obio_irq;
256179595Sbenno		break;
257179595Sbenno
258179595Sbenno	case SYS_RES_MEMORY:
259179595Sbenno		rm = &sc->obio_mem;
260179595Sbenno		break;
261179595Sbenno
262179595Sbenno	default:
263179595Sbenno		return (NULL);
264179595Sbenno	}
265179595Sbenno
266179595Sbenno	needactivate = flags & RF_ACTIVE;
267179595Sbenno	flags &= ~RF_ACTIVE;
268179595Sbenno	rv = rman_reserve_resource(rm, rle->start, rle->end, rle->count, flags,
269179595Sbenno	    child);
270179595Sbenno	if (rv == NULL)
271179595Sbenno		return (NULL);
272179595Sbenno	rle->res = rv;
273179595Sbenno	rman_set_rid(rv, *rid);
274179595Sbenno	if (type == SYS_RES_MEMORY) {
275179595Sbenno		rman_set_bustag(rv, sc->obio_bst);
276179595Sbenno		rman_set_bushandle(rv, rle->start);
277179595Sbenno	}
278179595Sbenno
279179595Sbenno	if (needactivate) {
280179595Sbenno		if (bus_activate_resource(child, type, *rid, rv)) {
281179595Sbenno			rman_release_resource(rv);
282179595Sbenno			return (NULL);
283179595Sbenno		}
284179595Sbenno	}
285179595Sbenno
286179595Sbenno	return (rv);
287179595Sbenno}
288179595Sbenno
289179595Sbennostatic int
290179595Sbennopxa_release_resource(device_t dev, device_t child, int type, int rid,
291179595Sbenno    struct resource *r)
292179595Sbenno{
293179595Sbenno	struct	obio_device *od;
294179595Sbenno	struct	resource_list *rl;
295179595Sbenno	struct	resource_list_entry *rle;
296179595Sbenno
297179595Sbenno	od = (struct obio_device *)device_get_ivars(child);
298179595Sbenno	rl = &od->od_resources;
299179595Sbenno
300179595Sbenno	if (type == SYS_RES_IOPORT)
301179595Sbenno		type = SYS_RES_MEMORY;
302179595Sbenno
303179595Sbenno	rle = resource_list_find(rl, type, rid);
304179595Sbenno
305179595Sbenno	if (!rle)
306179595Sbenno		panic("pxa_release_resource: can't find resource");
307179595Sbenno	if (!rle->res)
308179595Sbenno		panic("pxa_release_resource: resource entry is not busy");
309179595Sbenno
310179595Sbenno	rman_release_resource(rle->res);
311179595Sbenno	rle->res = NULL;
312179595Sbenno
313179595Sbenno	return (0);
314179595Sbenno}
315179595Sbenno
316179595Sbennostatic int
317179595Sbennopxa_activate_resource(device_t dev, device_t child, int type, int rid,
318179595Sbenno    struct resource *r)
319179595Sbenno{
320179595Sbenno
321179595Sbenno	return (rman_activate_resource(r));
322179595Sbenno}
323179595Sbenno
324179595Sbennostatic device_method_t pxa_methods[] = {
325179701Skevlo	DEVMETHOD(device_identify,	pxa_identify),
326179701Skevlo	DEVMETHOD(device_probe,		pxa_probe),
327179701Skevlo	DEVMETHOD(device_attach,	pxa_attach),
328179595Sbenno
329179701Skevlo	DEVMETHOD(bus_print_child,	pxa_print_child),
330179595Sbenno
331179701Skevlo	DEVMETHOD(bus_read_ivar,	pxa_read_ivar),
332179701Skevlo	DEVMETHOD(bus_setup_intr,	pxa_setup_intr),
333179701Skevlo	DEVMETHOD(bus_teardown_intr,	pxa_teardown_intr),
334179595Sbenno
335179701Skevlo	DEVMETHOD(bus_get_resource_list,	pxa_get_resource_list),
336179701Skevlo	DEVMETHOD(bus_alloc_resource,		pxa_alloc_resource),
337179701Skevlo	DEVMETHOD(bus_release_resource,		pxa_release_resource),
338179701Skevlo	DEVMETHOD(bus_activate_resource,	pxa_activate_resource),
339179595Sbenno
340179595Sbenno	{0, 0}
341179595Sbenno};
342179595Sbenno
343179595Sbennostatic driver_t pxa_driver = {
344179595Sbenno	"pxa",
345179595Sbenno	pxa_methods,
346179595Sbenno	sizeof(struct obio_softc),
347179595Sbenno};
348179595Sbenno
349179595Sbennostatic devclass_t pxa_devclass;
350179595Sbenno
351179595SbennoDRIVER_MODULE(pxa, nexus, pxa_driver, pxa_devclass, 0, 0);
352179595Sbenno
353179595Sbennostatic struct resource *
354179595Sbennopxa_alloc_gpio_irq(device_t dev, device_t child, int type, int *rid,
355179595Sbenno    u_long start, u_long end, u_long count, u_int flags)
356179595Sbenno{
357179595Sbenno	struct	obio_softc *sc;
358179595Sbenno	struct	obio_device *od;
359179595Sbenno	struct	resource_list *rl;
360179595Sbenno	struct	resource_list_entry *rle;
361179595Sbenno	struct	resource *rv;
362179595Sbenno	struct	rman *rm;
363179595Sbenno	int	needactivate;
364179595Sbenno
365179595Sbenno	sc = device_get_softc(dev);
366179595Sbenno	od = device_get_ivars(child);
367179595Sbenno	rl = &od->od_resources;
368179595Sbenno	rm = &sc->obio_irq;
369179595Sbenno
370179595Sbenno	needactivate = flags & RF_ACTIVE;
371179595Sbenno	flags &= ~RF_ACTIVE;
372179595Sbenno	rv = rman_reserve_resource(rm, start, end, count, flags, child);
373179595Sbenno	if (rv == NULL)
374179595Sbenno		return (NULL);
375179595Sbenno
376179595Sbenno	resource_list_add(rl, type, *rid, start, end, count);
377179595Sbenno	rle = resource_list_find(rl, type, *rid);
378179595Sbenno	if (rle == NULL)
379179595Sbenno		panic("pxa_alloc_gpio_irq: unexpectedly can't find resource");
380179595Sbenno
381179595Sbenno	rle->res = rv;
382179595Sbenno	rle->start = rman_get_start(rv);
383179595Sbenno	rle->end = rman_get_end(rv);
384179595Sbenno	rle->count = count;
385179595Sbenno
386179595Sbenno	if (needactivate) {
387179595Sbenno		if (bus_activate_resource(child, type, *rid, rv)) {
388179595Sbenno			rman_release_resource(rv);
389179595Sbenno			return (NULL);
390179595Sbenno		}
391179595Sbenno	}
392179595Sbenno
393179595Sbenno	if (bootverbose)
394179595Sbenno		device_printf(dev, "lazy allocation of irq %ld for %s\n",
395179595Sbenno		    start, device_get_nameunit(child));
396179595Sbenno
397179595Sbenno	return (rv);
398179595Sbenno}
399