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$");
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 (BUS_PROBE_NOWILDCARD);
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	return (0);
185}
186
187static int
188pxa_teardown_intr(device_t dev, device_t child, struct resource *ires,
189    void *cookie)
190{
191	return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, ires, cookie));}
192
193static int
194pxa_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
195{
196	struct	obio_device *od;
197
198	od = (struct obio_device *)device_get_ivars(child);
199
200	switch (which) {
201	case PXA_IVAR_BASE:
202		*((u_long *)result) = od->od_base;
203		break;
204
205	default:
206		return (ENOENT);
207	}
208
209	return (0);
210}
211
212static struct resource_list *
213pxa_get_resource_list(device_t dev, device_t child)
214{
215	struct	obio_device *od;
216
217	od = (struct obio_device *)device_get_ivars(child);
218
219	if (od == NULL)
220		return (NULL);
221
222	return (&od->od_resources);
223}
224
225static struct resource *
226pxa_alloc_resource(device_t dev, device_t child, int type, int *rid,
227    u_long start, u_long end, u_long count, u_int flags)
228{
229	struct	obio_softc *sc;
230	struct	obio_device *od;
231	struct	resource *rv;
232	struct	resource_list *rl;
233	struct	resource_list_entry *rle;
234	struct	rman *rm;
235	int	needactivate;
236
237	sc = (struct obio_softc *)device_get_softc(dev);
238	od = (struct obio_device *)device_get_ivars(child);
239	rl = &od->od_resources;
240
241	rle = resource_list_find(rl, type, *rid);
242	if (rle == NULL) {
243		/* We can allocate GPIO-based IRQs lazily. */
244		if (type == SYS_RES_IRQ)
245			return (pxa_alloc_gpio_irq(dev, child, type, rid,
246			    start, end, count, flags));
247		return (NULL);
248	}
249	if (rle->res != NULL)
250		panic("pxa_alloc_resource: resource is busy");
251
252	switch (type) {
253	case SYS_RES_IRQ:
254		rm = &sc->obio_irq;
255		break;
256
257	case SYS_RES_MEMORY:
258		rm = &sc->obio_mem;
259		break;
260
261	default:
262		return (NULL);
263	}
264
265	needactivate = flags & RF_ACTIVE;
266	flags &= ~RF_ACTIVE;
267	rv = rman_reserve_resource(rm, rle->start, rle->end, rle->count, flags,
268	    child);
269	if (rv == NULL)
270		return (NULL);
271	rle->res = rv;
272	rman_set_rid(rv, *rid);
273	if (type == SYS_RES_MEMORY) {
274		rman_set_bustag(rv, sc->obio_bst);
275		rman_set_bushandle(rv, rle->start);
276	}
277
278	if (needactivate) {
279		if (bus_activate_resource(child, type, *rid, rv)) {
280			rman_release_resource(rv);
281			return (NULL);
282		}
283	}
284
285	return (rv);
286}
287
288static int
289pxa_release_resource(device_t dev, device_t child, int type, int rid,
290    struct resource *r)
291{
292	struct	obio_device *od;
293	struct	resource_list *rl;
294	struct	resource_list_entry *rle;
295
296	od = (struct obio_device *)device_get_ivars(child);
297	rl = &od->od_resources;
298
299	if (type == SYS_RES_IOPORT)
300		type = SYS_RES_MEMORY;
301
302	rle = resource_list_find(rl, type, rid);
303
304	if (!rle)
305		panic("pxa_release_resource: can't find resource");
306	if (!rle->res)
307		panic("pxa_release_resource: resource entry is not busy");
308
309	rman_release_resource(rle->res);
310	rle->res = NULL;
311
312	return (0);
313}
314
315static int
316pxa_activate_resource(device_t dev, device_t child, int type, int rid,
317    struct resource *r)
318{
319
320	return (rman_activate_resource(r));
321}
322
323static device_method_t pxa_methods[] = {
324	DEVMETHOD(device_identify,	pxa_identify),
325	DEVMETHOD(device_probe,		pxa_probe),
326	DEVMETHOD(device_attach,	pxa_attach),
327
328	DEVMETHOD(bus_print_child,	pxa_print_child),
329
330	DEVMETHOD(bus_read_ivar,	pxa_read_ivar),
331	DEVMETHOD(bus_setup_intr,	pxa_setup_intr),
332	DEVMETHOD(bus_teardown_intr,	pxa_teardown_intr),
333
334	DEVMETHOD(bus_get_resource_list,	pxa_get_resource_list),
335	DEVMETHOD(bus_alloc_resource,		pxa_alloc_resource),
336	DEVMETHOD(bus_release_resource,		pxa_release_resource),
337	DEVMETHOD(bus_activate_resource,	pxa_activate_resource),
338
339	{0, 0}
340};
341
342static driver_t pxa_driver = {
343	"pxa",
344	pxa_methods,
345	sizeof(struct obio_softc),
346};
347
348static devclass_t pxa_devclass;
349
350DRIVER_MODULE(pxa, nexus, pxa_driver, pxa_devclass, 0, 0);
351
352static struct resource *
353pxa_alloc_gpio_irq(device_t dev, device_t child, int type, int *rid,
354    u_long start, u_long end, u_long count, u_int flags)
355{
356	struct	obio_softc *sc;
357	struct	obio_device *od;
358	struct	resource_list *rl;
359	struct	resource_list_entry *rle;
360	struct	resource *rv;
361	struct	rman *rm;
362	int	needactivate;
363
364	sc = device_get_softc(dev);
365	od = device_get_ivars(child);
366	rl = &od->od_resources;
367	rm = &sc->obio_irq;
368
369	needactivate = flags & RF_ACTIVE;
370	flags &= ~RF_ACTIVE;
371	rv = rman_reserve_resource(rm, start, end, count, flags, child);
372	if (rv == NULL)
373		return (NULL);
374
375	resource_list_add(rl, type, *rid, start, end, count);
376	rle = resource_list_find(rl, type, *rid);
377	if (rle == NULL)
378		panic("pxa_alloc_gpio_irq: unexpectedly can't find resource");
379
380	rle->res = rv;
381	rle->start = rman_get_start(rv);
382	rle->end = rman_get_end(rv);
383	rle->count = count;
384
385	if (needactivate) {
386		if (bus_activate_resource(child, type, *rid, rv)) {
387			rman_release_resource(rv);
388			return (NULL);
389		}
390	}
391
392	if (bootverbose)
393		device_printf(dev, "lazy allocation of irq %ld for %s\n",
394		    start, device_get_nameunit(child));
395
396	return (rv);
397}
398