]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/xscale/pxa/pxa_smi.c
Revert r336773: it removed too much.
[FreeBSD/FreeBSD.git] / sys / arm / xscale / pxa / pxa_smi.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 <sys/timetc.h>
38 #include <machine/bus.h>
39 #include <machine/intr.h>
40
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43
44 #include <arm/xscale/pxa/pxavar.h>
45 #include <arm/xscale/pxa/pxareg.h>
46
47 static MALLOC_DEFINE(M_PXASMI, "PXA SMI",
48     "Data for static memory interface devices.");
49
50 struct pxa_smi_softc {
51         struct resource *ps_res[1];
52         struct rman     ps_mem;
53         bus_space_tag_t ps_bst;
54         bus_addr_t      ps_base;
55 };
56
57 struct smi_ivars {
58         struct resource_list    smid_resources;
59         bus_addr_t              smid_mem;
60 };
61
62 static struct resource_spec pxa_smi_spec[] = {
63         { SYS_RES_MEMORY,       0,      RF_ACTIVE },
64         { -1, 0 }
65 };
66
67 static int      pxa_smi_probe(device_t);
68 static int      pxa_smi_attach(device_t);
69
70 static int      pxa_smi_print_child(device_t, device_t);
71
72 static int      pxa_smi_read_ivar(device_t, device_t, int, uintptr_t *);
73
74 static struct resource *        pxa_smi_alloc_resource(device_t, device_t,
75                                     int, int *, rman_res_t, rman_res_t, rman_res_t, u_int);
76 static int                      pxa_smi_release_resource(device_t, device_t,
77                                     int, int, struct resource *);
78 static int                      pxa_smi_activate_resource(device_t, device_t,
79                                     int, int, struct resource *);
80
81 static void     pxa_smi_add_device(device_t, const char *, int);
82
83 static int
84 pxa_smi_probe(device_t dev)
85 {
86
87         if (resource_disabled("smi", device_get_unit(dev)))
88                 return (ENXIO);
89
90         device_set_desc(dev, "Static Memory Interface");
91         return (0);
92 }
93
94 static int
95 pxa_smi_attach(device_t dev)
96 {
97         int             error, i, dunit;
98         const char      *dname;
99         struct          pxa_smi_softc *sc;
100
101         sc = (struct pxa_smi_softc *)device_get_softc(dev);
102
103         error = bus_alloc_resources(dev, pxa_smi_spec, sc->ps_res);
104         if (error) {
105                 device_printf(dev, "could not allocate resources\n");
106                 return (ENXIO);
107         }
108
109         sc->ps_mem.rm_type = RMAN_ARRAY;
110         sc->ps_mem.rm_descr = device_get_nameunit(dev);
111         if (rman_init(&sc->ps_mem) != 0)
112                 panic("pxa_smi_attach: failed to init mem rman");
113         if (rman_manage_region(&sc->ps_mem, 0, PXA2X0_CS_SIZE * 6) != 0)
114                 panic("pxa_smi_attach: failed ot set up mem rman");
115
116         sc->ps_bst = base_tag;
117         sc->ps_base = rman_get_start(sc->ps_res[0]);
118
119         i = 0;
120         while (resource_find_match(&i, &dname, &dunit, "at",
121             device_get_nameunit(dev)) == 0) {
122                 pxa_smi_add_device(dev, dname, dunit);
123         }
124
125         bus_generic_probe(dev);
126         bus_generic_attach(dev);
127
128         return (0);
129 }
130
131 static int
132 pxa_smi_print_child(device_t dev, device_t child)
133 {
134         struct  smi_ivars *smid;
135         int     retval;
136
137         smid = (struct smi_ivars *)device_get_ivars(child);
138         if (smid == NULL) {
139                 device_printf(dev, "unknown device: %s\n",
140                     device_get_nameunit(child));
141                 return (0);
142         }
143
144         retval = 0;
145
146         retval += bus_print_child_header(dev, child);
147
148         retval += resource_list_print_type(&smid->smid_resources, "at mem",
149             SYS_RES_MEMORY, "%#jx");
150         retval += resource_list_print_type(&smid->smid_resources, "irq",
151             SYS_RES_IRQ, "%jd");
152
153         retval += bus_print_child_footer(dev, child);
154
155         return (retval);
156 }
157
158 static int
159 pxa_smi_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
160 {
161         struct  pxa_smi_softc *sc;
162         struct  smi_ivars *smid;
163
164         sc = device_get_softc(dev);
165         smid = device_get_ivars(child);
166
167         switch (which) {
168         case SMI_IVAR_PHYSBASE:
169                 *((bus_addr_t *)result) = smid->smid_mem;
170                 break;
171
172         default:
173                 return (ENOENT);
174         }
175
176         return (0);
177 }
178
179 static struct resource *
180 pxa_smi_alloc_resource(device_t dev, device_t child, int type, int *rid,
181     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
182 {
183         struct  pxa_smi_softc *sc;
184         struct  smi_ivars *smid;
185         struct  resource *rv;
186         struct  resource_list *rl;
187         struct  resource_list_entry *rle;
188         int     needactivate;
189
190         sc = (struct pxa_smi_softc *)device_get_softc(dev);
191         smid = (struct smi_ivars *)device_get_ivars(child);
192         rl = &smid->smid_resources;
193
194         if (type == SYS_RES_IOPORT)
195                 type = SYS_RES_MEMORY;
196
197         rle = resource_list_find(rl, type, *rid);
198         if (rle == NULL)
199                 return (NULL);
200         if (rle->res != NULL)
201                 panic("pxa_smi_alloc_resource: resource is busy");
202
203         needactivate = flags & RF_ACTIVE;
204         flags &= ~RF_ACTIVE;
205
206         switch (type) {
207         case SYS_RES_MEMORY:
208                 rv = rman_reserve_resource(&sc->ps_mem, rle->start, rle->end,
209                     rle->count, flags, child);
210                 if (rv == NULL)
211                         return (NULL);
212                 rle->res = rv;
213                 rman_set_rid(rv, *rid);
214                 rman_set_bustag(rv, sc->ps_bst);
215                 rman_set_bushandle(rv, rle->start);
216                 if (needactivate) {
217                         if (bus_activate_resource(child, type, *rid, rv) != 0) {
218                                 rman_release_resource(rv);
219                                 return (NULL);
220                         }
221                 }
222
223                 break;
224
225         case SYS_RES_IRQ:
226                 rv = bus_alloc_resource(dev, type, rid, rle->start, rle->end,
227                     rle->count, flags);
228                 if (rv == NULL)
229                         return (NULL);
230                 if (needactivate) {
231                         if (bus_activate_resource(child, type, *rid, rv) != 0) {
232                                 bus_release_resource(dev, type, *rid, rv);
233                                 return (NULL);
234                         }
235                 }
236
237                 break;
238
239         default:
240                 return (NULL);
241         }
242
243         return (rv);
244 }
245
246 static int
247 pxa_smi_release_resource(device_t dev, device_t child, int type, int rid,
248     struct resource *r)
249 {
250         struct  smi_ivars *smid;
251         struct  resource_list *rl;
252         struct  resource_list_entry *rle;
253
254         if (type == SYS_RES_IRQ)
255                 return (bus_release_resource(dev, SYS_RES_IRQ, rid, r));
256
257         smid = (struct smi_ivars *)device_get_ivars(child);
258         rl = &smid->smid_resources;
259
260         if (type == SYS_RES_IOPORT)
261                 type = SYS_RES_MEMORY;
262
263         rle = resource_list_find(rl, type, rid);
264         if (rle == NULL)
265                 panic("pxa_smi_release_resource: can't find resource");
266         if (rle->res == NULL)
267                 panic("pxa_smi_release_resource: resource entry not busy");
268
269         rman_release_resource(rle->res);
270         rle->res = NULL;
271
272         return (0);
273 }
274
275 static int
276 pxa_smi_activate_resource(device_t dev, device_t child, int type, int rid,
277     struct resource *r)
278 {
279         struct  pxa_smi_softc *sc;
280
281         sc = (struct pxa_smi_softc *)device_get_softc(dev);
282
283         if (type == SYS_RES_IRQ)
284                 return (bus_activate_resource(dev, SYS_RES_IRQ, rid, r));
285
286         rman_set_bushandle(r, (bus_space_handle_t)pmap_mapdev(rman_get_start(r),
287             rman_get_size(r)));
288         return (rman_activate_resource(r));
289 }
290
291 static device_method_t pxa_smi_methods[] = {
292         DEVMETHOD(device_probe, pxa_smi_probe),
293         DEVMETHOD(device_attach, pxa_smi_attach),
294
295         DEVMETHOD(bus_print_child, pxa_smi_print_child),
296
297         DEVMETHOD(bus_read_ivar, pxa_smi_read_ivar),
298
299         DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
300
301         DEVMETHOD(bus_alloc_resource, pxa_smi_alloc_resource),
302         DEVMETHOD(bus_release_resource, pxa_smi_release_resource),
303         DEVMETHOD(bus_activate_resource, pxa_smi_activate_resource),
304
305         {0, 0}
306 };
307
308 static driver_t pxa_smi_driver = {
309         "smi",
310         pxa_smi_methods,
311         sizeof(struct pxa_smi_softc),
312 };
313
314 static devclass_t pxa_smi_devclass;
315
316 DRIVER_MODULE(smi, pxa, pxa_smi_driver, pxa_smi_devclass, 0, 0);
317
318 static void
319 pxa_smi_add_device(device_t dev, const char *name, int unit)
320 {
321         device_t        child;
322         int             start, count;
323         struct          smi_ivars *ivars;
324
325         ivars = (struct smi_ivars *)malloc(
326             sizeof(struct smi_ivars), M_PXASMI, M_WAITOK);
327
328         child = device_add_child(dev, name, unit);
329         if (child == NULL) {
330                 free(ivars, M_PXASMI);
331                 return;
332         }
333
334         device_set_ivars(child, ivars);
335         resource_list_init(&ivars->smid_resources);
336
337         start = 0;
338         count = 0;
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;
345         }
346
347         start = -1;
348         count = 0;
349         resource_int_value(name, unit, "irq", &start);
350         if (start > -1)
351                 resource_list_add(&ivars->smid_resources, SYS_RES_IRQ, 0, start,
352                      start, 1);
353
354         if (resource_disabled(name, unit))
355                 device_disable(child);
356 }