pxa_obio.c revision 226832
1/*-
2 * Copyright (c) 2006 Benno Rice.  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 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include <sys/cdefs.h>
26__FBSDID("$FreeBSD: head/sys/arm/xscale/pxa/pxa_obio.c 226832 2011-10-27 10:21:40Z kevlo $");
27
28#include <sys/param.h>
29#include <sys/systm.h>
30#include <sys/bus.h>
31#include <sys/kernel.h>
32#include <sys/module.h>
33#include <sys/malloc.h>
34#include <sys/rman.h>
35#include <machine/bus.h>
36#include <machine/intr.h>
37
38#include <arm/xscale/pxa/pxavar.h>
39#include <arm/xscale/pxa/pxareg.h>
40
41static void	pxa_identify(driver_t *, device_t);
42static int	pxa_probe(device_t);
43static int	pxa_attach(device_t);
44
45static int	pxa_print_child(device_t, device_t);
46
47static int	pxa_setup_intr(device_t, device_t, struct resource *, int,
48		    driver_filter_t *, driver_intr_t *, void *, void **);
49static int	pxa_read_ivar(device_t, device_t, int, uintptr_t *);
50
51static struct resource_list *	pxa_get_resource_list(device_t, device_t);
52static struct resource *	pxa_alloc_resource(device_t, device_t, int,
53				    int *, u_long, u_long, u_long, u_int);
54static int			pxa_release_resource(device_t, device_t, int,
55				    int, struct resource *);
56static int			pxa_activate_resource(device_t, device_t,
57				    int, int, struct resource *);
58
59static struct resource *	pxa_alloc_gpio_irq(device_t, device_t, int,
60				    int *, u_long, u_long, u_long, u_int);
61
62struct obio_device {
63	const char	*od_name;
64	u_long		od_base;
65	u_long		od_size;
66	u_int		od_irqs[5];
67	struct resource_list od_resources;
68};
69
70static struct obio_device obio_devices[] = {
71	{ "icu", PXA2X0_INTCTL_BASE, PXA2X0_INTCTL_SIZE, { 0 } },
72	{ "timer", PXA2X0_OST_BASE, PXA2X0_OST_SIZE, { PXA2X0_INT_OST0, PXA2X0_INT_OST1, PXA2X0_INT_OST2, PXA2X0_INT_OST3, 0 } },
73	{ "dmac", PXA2X0_DMAC_BASE, PXA2X0_DMAC_SIZE, { PXA2X0_INT_DMA, 0 } },
74	{ "gpio", PXA2X0_GPIO_BASE, PXA250_GPIO_SIZE, { PXA2X0_INT_GPIO0, PXA2X0_INT_GPIO1, PXA2X0_INT_GPION, 0 } },
75	{ "uart", PXA2X0_FFUART_BASE, PXA2X0_FFUART_SIZE, { PXA2X0_INT_FFUART, 0 } },
76	{ "uart", PXA2X0_BTUART_BASE, PXA2X0_BTUART_SIZE, { PXA2X0_INT_BTUART, 0 } },
77	{ "uart", PXA2X0_STUART_BASE, PXA2X0_STUART_SIZE, { PXA2X0_INT_STUART, 0 } },
78	{ "uart", PXA2X0_HWUART_BASE, PXA2X0_HWUART_SIZE, { PXA2X0_INT_HWUART, 0 } },
79	{ "smi", PXA2X0_CS0_START, PXA2X0_CS_SIZE * 6, { 0 } },
80	{ NULL, 0, 0, { 0 } }
81};
82
83void
84pxa_identify(driver_t *driver, device_t parent)
85{
86
87	BUS_ADD_CHILD(parent, 0, "pxa", 0);
88}
89
90int
91pxa_probe(device_t dev)
92{
93
94	device_set_desc(dev, "XScale PXA On-board IO");
95	return (0);
96}
97
98int
99pxa_attach(device_t dev)
100{
101	struct		obio_softc *sc;
102	struct		obio_device *od;
103	int		i;
104	device_t	child;
105
106	sc = device_get_softc(dev);
107
108	sc->obio_bst = obio_tag;
109
110	sc->obio_mem.rm_type = RMAN_ARRAY;
111	sc->obio_mem.rm_descr = "PXA2X0 OBIO Memory";
112	if (rman_init(&sc->obio_mem) != 0)
113		panic("pxa_attach: failed to init obio mem rman");
114	if (rman_manage_region(&sc->obio_mem, 0, PXA250_PERIPH_END) != 0)
115		panic("pxa_attach: failed to set up obio mem rman");
116
117	sc->obio_irq.rm_type = RMAN_ARRAY;
118	sc->obio_irq.rm_descr = "PXA2X0 OBIO IRQ";
119	if (rman_init(&sc->obio_irq) != 0)
120		panic("pxa_attach: failed to init obio irq rman");
121	if (rman_manage_region(&sc->obio_irq, 0, 31) != 0)
122		panic("pxa_attach: failed to set up obio irq rman (main irqs)");
123	if (rman_manage_region(&sc->obio_irq, IRQ_GPIO0, IRQ_GPIO_MAX) != 0)
124		panic("pxa_attach: failed to set up obio irq rman (gpio irqs)");
125
126	for (od = obio_devices; od->od_name != NULL; od++) {
127		resource_list_init(&od->od_resources);
128
129		resource_list_add(&od->od_resources, SYS_RES_MEMORY, 0,
130		    od->od_base, od->od_base + od->od_size, od->od_size);
131
132		for (i = 0; od->od_irqs[i] != 0; i++) {
133			resource_list_add(&od->od_resources, SYS_RES_IRQ, i,
134			    od->od_irqs[i], od->od_irqs[i], 1);
135		}
136
137		child = device_add_child(dev, od->od_name, -1);
138		device_set_ivars(child, od);
139	}
140
141	bus_generic_probe(dev);
142	bus_generic_attach(dev);
143
144	return (0);
145}
146
147static int
148pxa_print_child(device_t dev, device_t child)
149{
150	struct	obio_device *od;
151	int	retval;
152
153	od = (struct obio_device *)device_get_ivars(child);
154	if (od == NULL)
155		panic("Unknown device on pxa0");
156
157	retval = 0;
158
159	retval += bus_print_child_header(dev, child);
160
161	retval += resource_list_print_type(&od->od_resources, "at mem",
162	    SYS_RES_MEMORY, "0x%08lx");
163	retval += resource_list_print_type(&od->od_resources, "irq",
164	    SYS_RES_IRQ, "%ld");
165
166	retval += bus_print_child_footer(dev, child);
167
168	return (retval);
169}
170
171static int
172pxa_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
173    driver_filter_t *filter, driver_intr_t *ithread, void *arg, void **cookiep)
174{
175	struct	obio_softc *sc;
176	int error;
177
178	sc = (struct obio_softc *)device_get_softc(dev);
179
180	error = BUS_SETUP_INTR(device_get_parent(dev), child, irq, flags,
181	    filter, ithread, arg, cookiep);
182	if (error)
183		return (error);
184	arm_unmask_irq(rman_get_start(irq));
185	return (0);
186}
187
188static int
189pxa_teardown_intr(device_t dev, device_t child, struct resource *ires,
190    void *cookie)
191{
192	return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, ires, cookie));}
193
194static int
195pxa_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
196{
197	struct	obio_device *od;
198
199	od = (struct obio_device *)device_get_ivars(child);
200
201	switch (which) {
202	case PXA_IVAR_BASE:
203		*((u_long *)result) = od->od_base;
204		break;
205
206	default:
207		return (ENOENT);
208	}
209
210	return (0);
211}
212
213static struct resource_list *
214pxa_get_resource_list(device_t dev, device_t child)
215{
216	struct	obio_device *od;
217
218	od = (struct obio_device *)device_get_ivars(child);
219
220	if (od == NULL)
221		return (NULL);
222
223	return (&od->od_resources);
224}
225
226static struct resource *
227pxa_alloc_resource(device_t dev, device_t child, int type, int *rid,
228    u_long start, u_long end, u_long count, u_int flags)
229{
230	struct	obio_softc *sc;
231	struct	obio_device *od;
232	struct	resource *rv;
233	struct	resource_list *rl;
234	struct	resource_list_entry *rle;
235	struct	rman *rm;
236	int	needactivate;
237
238	sc = (struct obio_softc *)device_get_softc(dev);
239	od = (struct obio_device *)device_get_ivars(child);
240	rl = &od->od_resources;
241
242	rle = resource_list_find(rl, type, *rid);
243	if (rle == NULL) {
244		/* We can allocate GPIO-based IRQs lazily. */
245		if (type == SYS_RES_IRQ)
246			return (pxa_alloc_gpio_irq(dev, child, type, rid,
247			    start, end, count, flags));
248		return (NULL);
249	}
250	if (rle->res != NULL)
251		panic("pxa_alloc_resource: resource is busy");
252
253	switch (type) {
254	case SYS_RES_IRQ:
255		rm = &sc->obio_irq;
256		break;
257
258	case SYS_RES_MEMORY:
259		rm = &sc->obio_mem;
260		break;
261
262	default:
263		return (NULL);
264	}
265
266	needactivate = flags & RF_ACTIVE;
267	flags &= ~RF_ACTIVE;
268	rv = rman_reserve_resource(rm, rle->start, rle->end, rle->count, flags,
269	    child);
270	if (rv == NULL)
271		return (NULL);
272	rle->res = rv;
273	rman_set_rid(rv, *rid);
274	if (type == SYS_RES_MEMORY) {
275		rman_set_bustag(rv, sc->obio_bst);
276		rman_set_bushandle(rv, rle->start);
277	}
278
279	if (needactivate) {
280		if (bus_activate_resource(child, type, *rid, rv)) {
281			rman_release_resource(rv);
282			return (NULL);
283		}
284	}
285
286	return (rv);
287}
288
289static int
290pxa_release_resource(device_t dev, device_t child, int type, int rid,
291    struct resource *r)
292{
293	struct	obio_device *od;
294	struct	resource_list *rl;
295	struct	resource_list_entry *rle;
296
297	od = (struct obio_device *)device_get_ivars(child);
298	rl = &od->od_resources;
299
300	if (type == SYS_RES_IOPORT)
301		type = SYS_RES_MEMORY;
302
303	rle = resource_list_find(rl, type, rid);
304
305	if (!rle)
306		panic("pxa_release_resource: can't find resource");
307	if (!rle->res)
308		panic("pxa_release_resource: resource entry is not busy");
309
310	rman_release_resource(rle->res);
311	rle->res = NULL;
312
313	return (0);
314}
315
316static int
317pxa_activate_resource(device_t dev, device_t child, int type, int rid,
318    struct resource *r)
319{
320
321	return (rman_activate_resource(r));
322}
323
324static device_method_t pxa_methods[] = {
325	DEVMETHOD(device_identify,	pxa_identify),
326	DEVMETHOD(device_probe,		pxa_probe),
327	DEVMETHOD(device_attach,	pxa_attach),
328
329	DEVMETHOD(bus_print_child,	pxa_print_child),
330
331	DEVMETHOD(bus_read_ivar,	pxa_read_ivar),
332	DEVMETHOD(bus_setup_intr,	pxa_setup_intr),
333	DEVMETHOD(bus_teardown_intr,	pxa_teardown_intr),
334
335	DEVMETHOD(bus_get_resource_list,	pxa_get_resource_list),
336	DEVMETHOD(bus_alloc_resource,		pxa_alloc_resource),
337	DEVMETHOD(bus_release_resource,		pxa_release_resource),
338	DEVMETHOD(bus_activate_resource,	pxa_activate_resource),
339
340	{0, 0}
341};
342
343static driver_t pxa_driver = {
344	"pxa",
345	pxa_methods,
346	sizeof(struct obio_softc),
347};
348
349static devclass_t pxa_devclass;
350
351DRIVER_MODULE(pxa, nexus, pxa_driver, pxa_devclass, 0, 0);
352
353static struct resource *
354pxa_alloc_gpio_irq(device_t dev, device_t child, int type, int *rid,
355    u_long start, u_long end, u_long count, u_int flags)
356{
357	struct	obio_softc *sc;
358	struct	obio_device *od;
359	struct	resource_list *rl;
360	struct	resource_list_entry *rle;
361	struct	resource *rv;
362	struct	rman *rm;
363	int	needactivate;
364
365	sc = device_get_softc(dev);
366	od = device_get_ivars(child);
367	rl = &od->od_resources;
368	rm = &sc->obio_irq;
369
370	needactivate = flags & RF_ACTIVE;
371	flags &= ~RF_ACTIVE;
372	rv = rman_reserve_resource(rm, start, end, count, flags, child);
373	if (rv == NULL)
374		return (NULL);
375
376	resource_list_add(rl, type, *rid, start, end, count);
377	rle = resource_list_find(rl, type, *rid);
378	if (rle == NULL)
379		panic("pxa_alloc_gpio_irq: unexpectedly can't find resource");
380
381	rle->res = rv;
382	rle->start = rman_get_start(rv);
383	rle->end = rman_get_end(rv);
384	rle->count = count;
385
386	if (needactivate) {
387		if (bus_activate_resource(child, type, *rid, rv)) {
388			rman_release_resource(rv);
389			return (NULL);
390		}
391	}
392
393	if (bootverbose)
394		device_printf(dev, "lazy allocation of irq %ld for %s\n",
395		    start, device_get_nameunit(child));
396
397	return (rv);
398}
399