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