]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/arm/mv/obio.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / arm / mv / 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  * from: FreeBSD: //depot/projects/arm/src/sys/arm/xscale/pxa2x0/pxa2x0_obio.c, rev 1
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/mv/mvvar.h>
41 #include <arm/mv/mvreg.h>
42
43 static void mbus_identify(driver_t *, device_t);
44 static int mbus_probe(device_t);
45 static int mbus_attach(device_t);
46
47 static void
48 mbus_identify(driver_t *driver, device_t parent)
49 {
50
51         BUS_ADD_CHILD(parent, 0, "mbus", 0);
52 }
53
54 static int
55 mbus_probe(device_t dev)
56 {
57
58         device_set_desc(dev, "Marvell Internal Bus (Mbus)");
59         return (0);
60 }
61
62 static int
63 mbus_attach(device_t dev)
64 {
65         struct obio_softc *sc;
66         struct obio_device *od;
67         int i;
68         device_t child;
69
70         sc = device_get_softc(dev);
71
72         sc->obio_bst = obio_tag;
73
74         sc->obio_mem.rm_type = RMAN_ARRAY;
75         sc->obio_mem.rm_descr = "Marvell OBIO Memory";
76         if (rman_init(&sc->obio_mem) != 0)
77                 panic("mbus_attach: failed to init obio mem rman");
78         if (rman_manage_region(&sc->obio_mem, 0, ~0) != 0)
79                 panic("mbus_attach: failed to set up obio mem rman");
80
81         sc->obio_irq.rm_type = RMAN_ARRAY;
82         sc->obio_irq.rm_descr = "Marvell OBIO IRQ";
83         if (rman_init(&sc->obio_irq) != 0)
84                 panic("mbus_attach: failed to init obio irq rman");
85         if (rman_manage_region(&sc->obio_irq, 0, NIRQ - 1) != 0)
86                 panic("mbus_attach: failed to set up obio irq rman");
87
88         sc->obio_gpio.rm_type = RMAN_ARRAY;
89         sc->obio_gpio.rm_descr = "Marvell GPIO";
90         if (rman_init(&sc->obio_gpio) != 0)
91                 panic("mbus_attach: failed to init gpio rman");
92         if (rman_manage_region(&sc->obio_gpio, 0, MV_GPIO_MAX_NPINS - 1) != 0)
93                 panic("mbus_attach: failed to set up gpio rman");
94
95         for (od = obio_devices; od->od_name != NULL; od++) {
96                 if (soc_power_ctrl_get(od->od_pwr_mask) != od->od_pwr_mask)
97                         continue;
98
99                 resource_list_init(&od->od_resources);
100
101                 resource_list_add(&od->od_resources, SYS_RES_MEMORY, 0,
102                     od->od_base, od->od_base + od->od_size - 1, od->od_size);
103
104                 for (i = 0; od->od_irqs[i] != -1; i++) {
105                         resource_list_add(&od->od_resources, SYS_RES_IRQ, i,
106                             od->od_irqs[i], od->od_irqs[i], 1);
107                 }
108
109                 for (i = 0; od->od_gpio[i] != -1; i++) {
110                         resource_list_add(&od->od_resources, SYS_RES_GPIO, i,
111                             od->od_gpio[i], od->od_gpio[i], 1);
112                 }
113
114                 child = device_add_child(dev, od->od_name, -1);
115                 device_set_ivars(child, od);
116         }
117
118         bus_generic_probe(dev);
119         bus_generic_attach(dev);
120
121         return (0);
122 }
123
124 static int
125 mbus_print_child(device_t dev, device_t child)
126 {
127         struct obio_device *od;
128         int rv;
129
130         od = (struct obio_device *)device_get_ivars(child);
131         if (od == NULL)
132                 panic("Unknown device on %s", device_get_nameunit(dev));
133
134         rv = bus_print_child_header(dev, child);
135
136         rv += resource_list_print_type(&od->od_resources, "at mem",
137             SYS_RES_MEMORY, "0x%08lx");
138         rv += resource_list_print_type(&od->od_resources, "irq",
139             SYS_RES_IRQ, "%ld");
140         rv += resource_list_print_type(&od->od_resources, "gpio",
141             SYS_RES_GPIO, "%ld");
142
143         if (device_get_flags(child))
144                 rv += printf(" flags %#x", device_get_flags(child));
145         rv += bus_print_child_footer(dev, child);
146
147         return (rv);
148 }
149
150 static int
151 mbus_setup_intr(device_t dev, device_t child, struct resource *ires, int flags,
152     driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
153 {
154         struct obio_softc *sc;
155         int error;
156
157         sc = (struct obio_softc *)device_get_softc(dev);
158
159         if (rman_is_region_manager(ires, &sc->obio_gpio)) {
160                 error = mv_gpio_setup_intrhandler(
161                     device_get_nameunit(child), filt, intr, arg,
162                     rman_get_start(ires), flags, cookiep);
163
164                 if (error != 0)
165                         return (error);
166
167                 mv_gpio_intr_unmask(rman_get_start(ires));
168                 return (0);
169         }
170
171         BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags, filt, intr, arg,
172             cookiep);
173         arm_unmask_irq(rman_get_start(ires));
174         return (0);
175 }
176
177 static int
178 mbus_teardown_intr(device_t dev, device_t child, struct resource *ires, void *cookie)
179 {
180
181         return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, ires, cookie));
182 }
183
184 static int
185 mbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
186 {
187         struct obio_device *od;
188
189         od = (struct obio_device *)device_get_ivars(child);
190
191         switch (which) {
192         case MBUS_IVAR_BASE:
193                 *((u_long *)result) = od->od_base;
194                 break;
195
196         default:
197                 return (ENOENT);
198         }
199
200         return (0);
201 }
202
203 static struct resource_list *
204 mbus_get_resource_list(device_t dev, device_t child)
205 {
206         struct obio_device *od;
207
208         od = (struct obio_device *)device_get_ivars(child);
209
210         if (od == NULL)
211                 return (NULL);
212
213         return (&od->od_resources);
214 }
215
216 static struct resource *
217 mbus_alloc_resource(device_t dev, device_t child, int type, int *rid,
218     u_long start, u_long end, u_long count, u_int flags)
219 {
220         struct obio_softc *sc;
221         struct obio_device *od;
222         struct resource *rv;
223         struct resource_list *rl;
224         struct resource_list_entry *rle = NULL;
225         struct rman *rm;
226         int needactivate;
227
228         sc = (struct obio_softc *)device_get_softc(dev);
229
230         if (type == SYS_RES_IRQ && IS_GPIO_IRQ(start)) {
231                 type = SYS_RES_GPIO;
232                 start = IRQ2GPIO(start);
233         }
234
235         switch (type) {
236         case SYS_RES_IRQ:
237                 rm = &sc->obio_irq;
238                 break;
239         case SYS_RES_MEMORY:
240                 rm = &sc->obio_mem;
241                 break;
242         case SYS_RES_GPIO:
243                 rm = &sc->obio_gpio;
244                 break;
245         default:
246                 return (NULL);
247         }
248
249         if (device_get_parent(child) == dev) {
250                 od = (struct obio_device *)device_get_ivars(child);
251                 rl = &od->od_resources;
252                 rle = resource_list_find(rl, type, *rid);
253
254                 if (rle->res != NULL)
255                         panic("mbus_alloc_resource: resource is busy");
256         }
257
258         if (rle) {
259                 start = rle->start;
260                 end = rle->end;
261                 count = rle->count;
262         }
263
264         needactivate = flags & RF_ACTIVE;
265         flags &= ~RF_ACTIVE;
266         rv = rman_reserve_resource(rm, start, end, count, flags, child);
267
268         if (rv == NULL)
269                 return (NULL);
270
271         if (rle)
272                 rle->res = rv;
273
274         rman_set_rid(rv, *rid);
275
276         if (type == SYS_RES_MEMORY) {
277                 rman_set_bustag(rv, sc->obio_bst);
278                 rman_set_bushandle(rv, start);
279         }
280
281         if (needactivate)
282                 if (bus_activate_resource(child, type, *rid, rv)) {
283                         rman_release_resource(rv);
284                         return (NULL);
285                 }
286
287         return (rv);
288 }
289
290 static int
291 mbus_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         if (device_get_parent(child) == dev) {
299                 od = (struct obio_device *)device_get_ivars(child);
300                 rl = &od->od_resources;
301                 rle = resource_list_find(rl, type, rid);
302
303                 if (!rle)
304                         panic("mbus_release_resource: can't find resource");
305
306                 if (!rle->res)
307                         panic("mbus_release_resource: resource entry is not busy");
308
309                 r = rle->res;
310                 rle->res = NULL;
311         }
312
313         rman_release_resource(r);
314
315         return (0);
316 }
317
318 static int
319 mbus_activate_resource(device_t dev, device_t child, int type, int rid,
320     struct resource *r)
321 {
322
323         return (rman_activate_resource(r));
324 }
325
326 static device_t
327 mbus_add_child(device_t bus, int order, const char *name, int unit)
328 {
329         struct obio_device *od;
330         device_t child;
331
332         od = malloc(sizeof(struct obio_device), M_DEVBUF, M_NOWAIT | M_ZERO);
333         if (!od)
334                 return (0);
335
336         resource_list_init(&od->od_resources);
337         od->od_name = name;
338
339         child = device_add_child_ordered(bus, order, name, unit);
340         device_set_ivars(child, od);
341
342         return (child);
343 }
344
345 static device_method_t mbus_methods[] = {
346         DEVMETHOD(device_identify,      mbus_identify),
347         DEVMETHOD(device_probe,         mbus_probe),
348         DEVMETHOD(device_attach,        mbus_attach),
349
350         DEVMETHOD(bus_add_child,        mbus_add_child),
351         DEVMETHOD(bus_print_child,      mbus_print_child),
352
353         DEVMETHOD(bus_read_ivar,        mbus_read_ivar),
354         DEVMETHOD(bus_setup_intr,       mbus_setup_intr),
355         DEVMETHOD(bus_teardown_intr,    mbus_teardown_intr),
356
357         DEVMETHOD(bus_set_resource,             bus_generic_rl_set_resource),
358         DEVMETHOD(bus_get_resource_list,        mbus_get_resource_list),
359         DEVMETHOD(bus_alloc_resource,           mbus_alloc_resource),
360         DEVMETHOD(bus_release_resource,         mbus_release_resource),
361         DEVMETHOD(bus_activate_resource,        mbus_activate_resource),
362
363         {0, 0}
364 };
365
366 static driver_t mbus_driver = {
367         "mbus",
368         mbus_methods,
369         sizeof(struct obio_softc),
370 };
371
372 static devclass_t mbus_devclass;
373
374 DRIVER_MODULE(mbus, nexus, mbus_driver, mbus_devclass, 0, 0);