]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/arm/xscale/pxa/pxa_obio.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / arm / xscale / pxa / pxa_obio.c
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
41 static void     pxa_identify(driver_t *, device_t);
42 static int      pxa_probe(device_t);
43 static int      pxa_attach(device_t);
44
45 static int      pxa_print_child(device_t, device_t);
46
47 static int      pxa_setup_intr(device_t, device_t, struct resource *, int,
48                     driver_filter_t *, driver_intr_t *, void *, void **);
49 static int      pxa_read_ivar(device_t, device_t, int, uintptr_t *);
50
51 static struct resource_list *   pxa_get_resource_list(device_t, device_t);
52 static struct resource *        pxa_alloc_resource(device_t, device_t, int,
53                                     int *, u_long, u_long, u_long, u_int);
54 static int                      pxa_release_resource(device_t, device_t, int,
55                                     int, struct resource *);
56 static int                      pxa_activate_resource(device_t, device_t,
57                                     int, int, struct resource *);
58
59 static struct resource *        pxa_alloc_gpio_irq(device_t, device_t, int,
60                                     int *, u_long, u_long, u_long, u_int);
61
62 struct 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
70 static 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
83 void
84 pxa_identify(driver_t *driver, device_t parent)
85 {
86
87         BUS_ADD_CHILD(parent, 0, "pxa", 0);
88 }
89
90 int
91 pxa_probe(device_t dev)
92 {
93
94         device_set_desc(dev, "XScale PXA On-board IO");
95         return (0);
96 }
97
98 int
99 pxa_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
147 static int
148 pxa_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
171 static int
172 pxa_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
177         sc = (struct obio_softc *)device_get_softc(dev);
178
179         BUS_SETUP_INTR(device_get_parent(dev), child, irq, flags, filter,
180             ithread, arg, cookiep);
181         arm_unmask_irq(rman_get_start(irq));
182         return (0);
183 }
184
185 static int
186 pxa_teardown_intr(device_t dev, device_t child, struct resource *ires,
187     void *cookie)
188 {
189         return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, ires, cookie));}
190
191 static int
192 pxa_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
193 {
194         struct  obio_device *od;
195
196         od = (struct obio_device *)device_get_ivars(child);
197
198         switch (which) {
199         case PXA_IVAR_BASE:
200                 *((u_long *)result) = od->od_base;
201                 break;
202
203         default:
204                 return (ENOENT);
205         }
206
207         return (0);
208 }
209
210 static struct resource_list *
211 pxa_get_resource_list(device_t dev, device_t child)
212 {
213         struct  obio_device *od;
214
215         od = (struct obio_device *)device_get_ivars(child);
216
217         if (od == NULL)
218                 return (NULL);
219
220         return (&od->od_resources);
221 }
222
223 static struct resource *
224 pxa_alloc_resource(device_t dev, device_t child, int type, int *rid,
225     u_long start, u_long end, u_long count, u_int flags)
226 {
227         struct  obio_softc *sc;
228         struct  obio_device *od;
229         struct  resource *rv;
230         struct  resource_list *rl;
231         struct  resource_list_entry *rle;
232         struct  rman *rm;
233         int     needactivate;
234
235         sc = (struct obio_softc *)device_get_softc(dev);
236         od = (struct obio_device *)device_get_ivars(child);
237         rl = &od->od_resources;
238
239         rle = resource_list_find(rl, type, *rid);
240         if (rle == NULL) {
241                 /* We can allocate GPIO-based IRQs lazily. */
242                 if (type == SYS_RES_IRQ)
243                         return (pxa_alloc_gpio_irq(dev, child, type, rid,
244                             start, end, count, flags));
245                 return (NULL);
246         }
247         if (rle->res != NULL)
248                 panic("pxa_alloc_resource: resource is busy");
249
250         switch (type) {
251         case SYS_RES_IRQ:
252                 rm = &sc->obio_irq;
253                 break;
254
255         case SYS_RES_MEMORY:
256                 rm = &sc->obio_mem;
257                 break;
258
259         default:
260                 return (NULL);
261         }
262
263         needactivate = flags & RF_ACTIVE;
264         flags &= ~RF_ACTIVE;
265         rv = rman_reserve_resource(rm, rle->start, rle->end, rle->count, flags,
266             child);
267         if (rv == NULL)
268                 return (NULL);
269         rle->res = rv;
270         rman_set_rid(rv, *rid);
271         if (type == SYS_RES_MEMORY) {
272                 rman_set_bustag(rv, sc->obio_bst);
273                 rman_set_bushandle(rv, rle->start);
274         }
275
276         if (needactivate) {
277                 if (bus_activate_resource(child, type, *rid, rv)) {
278                         rman_release_resource(rv);
279                         return (NULL);
280                 }
281         }
282
283         return (rv);
284 }
285
286 static int
287 pxa_release_resource(device_t dev, device_t child, int type, int rid,
288     struct resource *r)
289 {
290         struct  obio_device *od;
291         struct  resource_list *rl;
292         struct  resource_list_entry *rle;
293
294         od = (struct obio_device *)device_get_ivars(child);
295         rl = &od->od_resources;
296
297         if (type == SYS_RES_IOPORT)
298                 type = SYS_RES_MEMORY;
299
300         rle = resource_list_find(rl, type, rid);
301
302         if (!rle)
303                 panic("pxa_release_resource: can't find resource");
304         if (!rle->res)
305                 panic("pxa_release_resource: resource entry is not busy");
306
307         rman_release_resource(rle->res);
308         rle->res = NULL;
309
310         return (0);
311 }
312
313 static int
314 pxa_activate_resource(device_t dev, device_t child, int type, int rid,
315     struct resource *r)
316 {
317
318         return (rman_activate_resource(r));
319 }
320
321 static device_method_t pxa_methods[] = {
322         DEVMETHOD(device_identify,      pxa_identify),
323         DEVMETHOD(device_probe,         pxa_probe),
324         DEVMETHOD(device_attach,        pxa_attach),
325
326         DEVMETHOD(bus_print_child,      pxa_print_child),
327
328         DEVMETHOD(bus_read_ivar,        pxa_read_ivar),
329         DEVMETHOD(bus_setup_intr,       pxa_setup_intr),
330         DEVMETHOD(bus_teardown_intr,    pxa_teardown_intr),
331
332         DEVMETHOD(bus_get_resource_list,        pxa_get_resource_list),
333         DEVMETHOD(bus_alloc_resource,           pxa_alloc_resource),
334         DEVMETHOD(bus_release_resource,         pxa_release_resource),
335         DEVMETHOD(bus_activate_resource,        pxa_activate_resource),
336
337         {0, 0}
338 };
339
340 static driver_t pxa_driver = {
341         "pxa",
342         pxa_methods,
343         sizeof(struct obio_softc),
344 };
345
346 static devclass_t pxa_devclass;
347
348 DRIVER_MODULE(pxa, nexus, pxa_driver, pxa_devclass, 0, 0);
349
350 static struct resource *
351 pxa_alloc_gpio_irq(device_t dev, device_t child, int type, int *rid,
352     u_long start, u_long end, u_long count, u_int flags)
353 {
354         struct  obio_softc *sc;
355         struct  obio_device *od;
356         struct  resource_list *rl;
357         struct  resource_list_entry *rle;
358         struct  resource *rv;
359         struct  rman *rm;
360         int     needactivate;
361
362         sc = device_get_softc(dev);
363         od = device_get_ivars(child);
364         rl = &od->od_resources;
365         rm = &sc->obio_irq;
366
367         needactivate = flags & RF_ACTIVE;
368         flags &= ~RF_ACTIVE;
369         rv = rman_reserve_resource(rm, start, end, count, flags, child);
370         if (rv == NULL)
371                 return (NULL);
372
373         resource_list_add(rl, type, *rid, start, end, count);
374         rle = resource_list_find(rl, type, *rid);
375         if (rle == NULL)
376                 panic("pxa_alloc_gpio_irq: unexpectedly can't find resource");
377
378         rle->res = rv;
379         rle->start = rman_get_start(rv);
380         rle->end = rman_get_end(rv);
381         rle->count = count;
382
383         if (needactivate) {
384                 if (bus_activate_resource(child, type, *rid, rv)) {
385                         rman_release_resource(rv);
386                         return (NULL);
387                 }
388         }
389
390         if (bootverbose)
391                 device_printf(dev, "lazy allocation of irq %ld for %s\n",
392                     start, device_get_nameunit(child));
393
394         return (rv);
395 }