2 * Copyright (c) 2006 Benno Rice. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 #include <sys/param.h>
29 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/malloc.h>
35 #include <sys/timetc.h>
36 #include <machine/bus.h>
37 #include <machine/intr.h>
42 #include <arm/xscale/pxa/pxavar.h>
43 #include <arm/xscale/pxa/pxareg.h>
45 static MALLOC_DEFINE(M_PXASMI, "PXA SMI",
46 "Data for static memory interface devices.");
48 struct pxa_smi_softc {
49 struct resource *ps_res[1];
51 bus_space_tag_t ps_bst;
56 struct resource_list smid_resources;
60 static struct resource_spec pxa_smi_spec[] = {
61 { SYS_RES_MEMORY, 0, RF_ACTIVE },
65 static int pxa_smi_probe(device_t);
66 static int pxa_smi_attach(device_t);
68 static int pxa_smi_print_child(device_t, device_t);
70 static int pxa_smi_read_ivar(device_t, device_t, int, uintptr_t *);
72 static struct resource * pxa_smi_alloc_resource(device_t, device_t,
73 int, int *, rman_res_t, rman_res_t, rman_res_t, u_int);
74 static int pxa_smi_release_resource(device_t, device_t,
75 int, int, struct resource *);
76 static int pxa_smi_activate_resource(device_t, device_t,
77 int, int, struct resource *);
79 static void pxa_smi_add_device(device_t, const char *, int);
82 pxa_smi_probe(device_t dev)
85 if (resource_disabled("smi", device_get_unit(dev)))
88 device_set_desc(dev, "Static Memory Interface");
93 pxa_smi_attach(device_t dev)
97 struct pxa_smi_softc *sc;
99 sc = (struct pxa_smi_softc *)device_get_softc(dev);
101 error = bus_alloc_resources(dev, pxa_smi_spec, sc->ps_res);
103 device_printf(dev, "could not allocate resources\n");
107 sc->ps_mem.rm_type = RMAN_ARRAY;
108 sc->ps_mem.rm_descr = device_get_nameunit(dev);
109 if (rman_init(&sc->ps_mem) != 0)
110 panic("pxa_smi_attach: failed to init mem rman");
111 if (rman_manage_region(&sc->ps_mem, 0, PXA2X0_CS_SIZE * 6) != 0)
112 panic("pxa_smi_attach: failed ot set up mem rman");
114 sc->ps_bst = base_tag;
115 sc->ps_base = rman_get_start(sc->ps_res[0]);
118 while (resource_find_match(&i, &dname, &dunit, "at",
119 device_get_nameunit(dev)) == 0) {
120 pxa_smi_add_device(dev, dname, dunit);
123 bus_generic_probe(dev);
124 bus_generic_attach(dev);
130 pxa_smi_print_child(device_t dev, device_t child)
132 struct smi_ivars *smid;
135 smid = (struct smi_ivars *)device_get_ivars(child);
137 device_printf(dev, "unknown device: %s\n",
138 device_get_nameunit(child));
144 retval += bus_print_child_header(dev, child);
146 retval += resource_list_print_type(&smid->smid_resources, "at mem",
147 SYS_RES_MEMORY, "%#jx");
148 retval += resource_list_print_type(&smid->smid_resources, "irq",
151 retval += bus_print_child_footer(dev, child);
157 pxa_smi_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
159 struct pxa_smi_softc *sc;
160 struct smi_ivars *smid;
162 sc = device_get_softc(dev);
163 smid = device_get_ivars(child);
166 case SMI_IVAR_PHYSBASE:
167 *((bus_addr_t *)result) = smid->smid_mem;
177 static struct resource *
178 pxa_smi_alloc_resource(device_t dev, device_t child, int type, int *rid,
179 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
181 struct pxa_smi_softc *sc;
182 struct smi_ivars *smid;
184 struct resource_list *rl;
185 struct resource_list_entry *rle;
188 sc = (struct pxa_smi_softc *)device_get_softc(dev);
189 smid = (struct smi_ivars *)device_get_ivars(child);
190 rl = &smid->smid_resources;
192 if (type == SYS_RES_IOPORT)
193 type = SYS_RES_MEMORY;
195 rle = resource_list_find(rl, type, *rid);
198 if (rle->res != NULL)
199 panic("pxa_smi_alloc_resource: resource is busy");
201 needactivate = flags & RF_ACTIVE;
206 rv = rman_reserve_resource(&sc->ps_mem, rle->start, rle->end,
207 rle->count, flags, child);
211 rman_set_rid(rv, *rid);
212 rman_set_bustag(rv, sc->ps_bst);
213 rman_set_bushandle(rv, rle->start);
215 if (bus_activate_resource(child, type, *rid, rv) != 0) {
216 rman_release_resource(rv);
224 rv = bus_alloc_resource(dev, type, rid, rle->start, rle->end,
229 if (bus_activate_resource(child, type, *rid, rv) != 0) {
230 bus_release_resource(dev, type, *rid, rv);
245 pxa_smi_release_resource(device_t dev, device_t child, int type, int rid,
248 struct smi_ivars *smid;
249 struct resource_list *rl;
250 struct resource_list_entry *rle;
252 if (type == SYS_RES_IRQ)
253 return (bus_release_resource(dev, SYS_RES_IRQ, rid, r));
255 smid = (struct smi_ivars *)device_get_ivars(child);
256 rl = &smid->smid_resources;
258 if (type == SYS_RES_IOPORT)
259 type = SYS_RES_MEMORY;
261 rle = resource_list_find(rl, type, rid);
263 panic("pxa_smi_release_resource: can't find resource");
264 if (rle->res == NULL)
265 panic("pxa_smi_release_resource: resource entry not busy");
267 rman_release_resource(rle->res);
274 pxa_smi_activate_resource(device_t dev, device_t child, int type, int rid,
277 struct pxa_smi_softc *sc;
279 sc = (struct pxa_smi_softc *)device_get_softc(dev);
281 if (type == SYS_RES_IRQ)
282 return (bus_activate_resource(dev, SYS_RES_IRQ, rid, r));
284 rman_set_bushandle(r, (bus_space_handle_t)pmap_mapdev(rman_get_start(r),
286 return (rman_activate_resource(r));
289 static device_method_t pxa_smi_methods[] = {
290 DEVMETHOD(device_probe, pxa_smi_probe),
291 DEVMETHOD(device_attach, pxa_smi_attach),
293 DEVMETHOD(bus_print_child, pxa_smi_print_child),
295 DEVMETHOD(bus_read_ivar, pxa_smi_read_ivar),
297 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
299 DEVMETHOD(bus_alloc_resource, pxa_smi_alloc_resource),
300 DEVMETHOD(bus_release_resource, pxa_smi_release_resource),
301 DEVMETHOD(bus_activate_resource, pxa_smi_activate_resource),
306 static driver_t pxa_smi_driver = {
309 sizeof(struct pxa_smi_softc),
312 static devclass_t pxa_smi_devclass;
314 DRIVER_MODULE(smi, pxa, pxa_smi_driver, pxa_smi_devclass, 0, 0);
317 pxa_smi_add_device(device_t dev, const char *name, int unit)
321 struct smi_ivars *ivars;
323 ivars = (struct smi_ivars *)malloc(
324 sizeof(struct smi_ivars), M_PXASMI, M_WAITOK);
328 child = device_add_child(dev, name, unit);
330 free(ivars, M_PXASMI);
334 device_set_ivars(child, ivars);
335 resource_list_init(&ivars->smid_resources);
339 resource_int_value(name, unit, "mem", &start);
340 resource_int_value(name, unit, "size", &count);
341 if (start > 0 || count > 0) {
342 resource_list_add(&ivars->smid_resources, SYS_RES_MEMORY, 0,
343 start, start + count, count);
344 ivars->smid_mem = (bus_addr_t)start;
349 resource_int_value(name, unit, "irq", &start);
351 resource_list_add(&ivars->smid_resources, SYS_RES_IRQ, 0, start,
354 if (resource_disabled(name, unit))
355 device_disable(child);