1 /*-
2 * Copyright (c) 2012 Semihalf.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: releng/10.1/sys/arm/mv/mv_localbus.c 266386 2014-05-18 00:32:35Z ian $");
29
30 #include "opt_platform.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/ktr.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/rman.h>
38 #include <sys/malloc.h>
39
40 #include <vm/vm.h>
41
42 #include <machine/devmap.h>
43 #include <machine/fdt.h>
44
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/ofw/openfirm.h>
48
49 #include "dev/fdt/fdt_common.h"
50 #include "ofw_bus_if.h"
51
52 #include <arm/mv/mvwin.h>
53
54 #ifdef DEBUG
55 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
56 printf(fmt,##args); } while (0)
57 #else
58 #define debugf(fmt, args...)
59 #endif
60
61 #define MV_LOCALBUS_MAX_BANKS 8
62 #define MV_LOCALBUS_MAX_BANK_CELLS 4
63
64 static MALLOC_DEFINE(M_LOCALBUS, "localbus", "localbus devices information");
65
66 struct localbus_bank {
67 vm_offset_t va; /* VA of the bank */
68 vm_paddr_t pa; /* physical address of the bank */
69 vm_size_t size; /* bank size */
70 uint8_t mapped; /* device memory has mapping */
71 };
72
73 struct localbus_softc {
74 device_t sc_dev;
75 bus_space_handle_t sc_bsh;
76 bus_space_tag_t sc_bst;
77 int sc_rid;
78
79 struct localbus_bank *sc_banks;
80 };
81
82 struct localbus_devinfo {
83 struct ofw_bus_devinfo di_ofw;
84 struct resource_list di_res;
85 int di_bank;
86 };
87
88 struct localbus_va_entry {
89 int8_t bank;
90 vm_offset_t va;
91 vm_size_t size;
92 };
93
94 /*
95 * Prototypes.
96 */
97 static int localbus_probe(device_t);
98 static int localbus_attach(device_t);
99 static int localbus_print_child(device_t, device_t);
100
101 static struct resource *localbus_alloc_resource(device_t, device_t, int,
102 int *, u_long, u_long, u_long, u_int);
103 static struct resource_list *localbus_get_resource_list(device_t, device_t);
104
105 static ofw_bus_get_devinfo_t localbus_get_devinfo;
106
107 /*
108 * Bus interface definition.
109 */
110 static device_method_t localbus_methods[] = {
111 /* Device interface */
112 DEVMETHOD(device_probe, localbus_probe),
113 DEVMETHOD(device_attach, localbus_attach),
114 DEVMETHOD(device_detach, bus_generic_detach),
115 DEVMETHOD(device_shutdown, bus_generic_shutdown),
116 DEVMETHOD(device_suspend, bus_generic_suspend),
117 DEVMETHOD(device_resume, bus_generic_resume),
118
119 /* Bus interface */
120 DEVMETHOD(bus_print_child, localbus_print_child),
121 DEVMETHOD(bus_alloc_resource, localbus_alloc_resource),
122 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
123 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
124 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
125 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
126 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
127 DEVMETHOD(bus_get_resource_list, localbus_get_resource_list),
128
129 /* OFW bus interface */
130 DEVMETHOD(ofw_bus_get_devinfo, localbus_get_devinfo),
131 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
132 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
133 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
134 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
135 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
136
137 { 0, 0 }
138 };
139
140 static driver_t localbus_driver = {
141 "localbus",
142 localbus_methods,
143 sizeof(struct localbus_softc)
144 };
145
146 const struct localbus_va_entry localbus_virtmap[] = {
147 { 0, MV_DEV_BOOT_BASE, MV_DEV_BOOT_SIZE },
148 { 1, MV_DEV_CS0_BASE, MV_DEV_CS0_SIZE },
149 { 2, MV_DEV_CS1_BASE, MV_DEV_CS1_SIZE },
150 { 3, MV_DEV_CS2_BASE, MV_DEV_CS2_SIZE },
151
152 { -1, 0, 0 }
153 };
154
155 static struct localbus_bank localbus_banks[MV_LOCALBUS_MAX_BANKS];
156
157 devclass_t localbus_devclass;
158
159 DRIVER_MODULE(localbus, ofwbus, localbus_driver, localbus_devclass, 0, 0);
160
161 static int
162 fdt_localbus_reg_decode(phandle_t node, struct localbus_softc *sc,
163 struct localbus_devinfo *di)
164 {
165 u_long start, end, count;
166 pcell_t *reg, *regptr;
167 pcell_t addr_cells, size_cells;
168 int tuple_size, tuples;
169 int i, rv, bank;
170
171 if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells) != 0)
172 return (ENXIO);
173
174 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
175 tuples = OF_getprop_alloc(node, "reg", tuple_size, (void **)®);
176 debugf("addr_cells = %d, size_cells = %d\n", addr_cells, size_cells);
177 debugf("tuples = %d, tuple size = %d\n", tuples, tuple_size);
178 if (tuples <= 0)
179 /* No 'reg' property in this node. */
180 return (0);
181
182 regptr = reg;
183 for (i = 0; i < tuples; i++) {
184
185 bank = fdt_data_get((void *)regptr, 1);
186
187 if (bank >= MV_LOCALBUS_MAX_BANKS) {
188 device_printf(sc->sc_dev, "bank number [%d] out of "
189 "range\n", bank);
190 continue;
191 }
192
193 /*
194 * If device doesn't have virtual to physical mapping don't add
195 * resources
196 */
197 if (!(sc->sc_banks[bank].mapped)) {
198 device_printf(sc->sc_dev, "device [%d]: missing memory "
199 "mapping\n", bank);
200 continue;
201 }
202
203 di->di_bank = bank;
204 regptr += 1;
205
206 /* Get address/size. */
207 rv = fdt_data_to_res(regptr, addr_cells - 1, size_cells, &start,
208 &count);
209 if (rv != 0) {
210 resource_list_free(&di->di_res);
211 goto out;
212 }
213
214 /* Check if enough amount of memory is mapped */
215 if (sc->sc_banks[bank].size < count) {
216 device_printf(sc->sc_dev, "device [%d]: not enough "
217 "memory reserved\n", bank);
218 continue;
219 }
220
221 regptr += addr_cells - 1 + size_cells;
222
223 /* Calculate address range relative to VA base. */
224 start = sc->sc_banks[bank].va + start;
225 end = start + count - 1;
226
227 debugf("reg addr bank = %d, start = %lx, end = %lx, "
228 "count = %lx\n", bank, start, end, count);
229
230 /* Use bank (CS) cell as rid. */
231 resource_list_add(&di->di_res, SYS_RES_MEMORY, di->di_bank,
232 start, end, count);
233 }
234 rv = 0;
235 out:
236 free(reg, M_OFWPROP);
237 return (rv);
238 }
239
240 static int
241 localbus_probe(device_t dev)
242 {
243
244 if (!ofw_bus_is_compatible_strict(dev, "mrvl,lbc"))
245 return (ENXIO);
246
247 device_set_desc(dev, "Marvell device bus");
248
249 return (BUS_PROBE_DEFAULT);
250 }
251
252 static int
253 localbus_attach(device_t dev)
254 {
255 device_t dev_child;
256 struct localbus_softc *sc;
257 struct localbus_devinfo *di;
258 phandle_t dt_node, dt_child;
259
260 sc = device_get_softc(dev);
261 sc->sc_dev = dev;
262 sc->sc_banks = localbus_banks;
263
264 /*
265 * Walk localbus and add direct subordinates as our children.
266 */
267 dt_node = ofw_bus_get_node(dev);
268 for (dt_child = OF_child(dt_node); dt_child != 0;
269 dt_child = OF_peer(dt_child)) {
270
271 /* Check and process 'status' property. */
272 if (!(fdt_is_enabled(dt_child)))
273 continue;
274
275 if (!(fdt_pm_is_enabled(dt_child)))
276 continue;
277
278 di = malloc(sizeof(*di), M_LOCALBUS, M_WAITOK | M_ZERO);
279 if (ofw_bus_gen_setup_devinfo(&di->di_ofw, dt_child) != 0) {
280 free(di, M_LOCALBUS);
281 device_printf(dev, "could not set up devinfo\n");
282 continue;
283 }
284
285 resource_list_init(&di->di_res);
286 if (fdt_localbus_reg_decode(dt_child, sc, di)) {
287 device_printf(dev, "could not process 'reg' "
288 "property\n");
289 ofw_bus_gen_destroy_devinfo(&di->di_ofw);
290 free(di, M_LOCALBUS);
291 continue;
292 }
293
294 /* Add newbus device for this FDT node */
295 dev_child = device_add_child(dev, NULL, -1);
296 if (dev_child == NULL) {
297 device_printf(dev, "could not add child: %s\n",
298 di->di_ofw.obd_name);
299 resource_list_free(&di->di_res);
300 ofw_bus_gen_destroy_devinfo(&di->di_ofw);
301 free(di, M_LOCALBUS);
302 continue;
303 }
304 #ifdef DEBUG
305 device_printf(dev, "added child: %s\n\n", di->di_ofw.obd_name);
306 #endif
307 device_set_ivars(dev_child, di);
308 }
309
310 return (bus_generic_attach(dev));
311 }
312
313 static int
314 localbus_print_child(device_t dev, device_t child)
315 {
316 struct localbus_devinfo *di;
317 struct resource_list *rl;
318 int rv;
319
320 di = device_get_ivars(child);
321 rl = &di->di_res;
322
323 rv = 0;
324 rv += bus_print_child_header(dev, child);
325 rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
326 rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
327 rv += bus_print_child_footer(dev, child);
328
329 return (rv);
330 }
331
332 static struct resource *
333 localbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
334 u_long start, u_long end, u_long count, u_int flags)
335 {
336 struct localbus_devinfo *di;
337 struct resource_list_entry *rle;
338
339 /*
340 * Request for the default allocation with a given rid: use resource
341 * list stored in the local device info.
342 */
343 if ((start == 0UL) && (end == ~0UL)) {
344 if ((di = device_get_ivars(child)) == NULL)
345 return (NULL);
346
347 if (type == SYS_RES_IOPORT)
348 type = SYS_RES_MEMORY;
349
350 rid = &di->di_bank;
351 rle = resource_list_find(&di->di_res, type, *rid);
352 if (rle == NULL) {
353 device_printf(bus, "no default resources for "
354 "rid = %d, type = %d\n", *rid, type);
355 return (NULL);
356 }
357 start = rle->start;
358 end = rle->end;
359 count = rle->count;
360 }
361
362 return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
363 count, flags));
364 }
365
366
367 static struct resource_list *
368 localbus_get_resource_list(device_t bus, device_t child)
369 {
370 struct localbus_devinfo *di;
371
372 di = device_get_ivars(child);
373 return (&di->di_res);
374 }
375
376 static const struct ofw_bus_devinfo *
377 localbus_get_devinfo(device_t bus, device_t child)
378 {
379 struct localbus_devinfo *di;
380
381 di = device_get_ivars(child);
382 return (&di->di_ofw);
383 }
384
385 int
386 fdt_localbus_devmap(phandle_t dt_node, struct arm_devmap_entry *fdt_devmap,
387 int banks_max_num, int *banks_added)
388 {
389 pcell_t ranges[MV_LOCALBUS_MAX_BANKS * MV_LOCALBUS_MAX_BANK_CELLS];
390 pcell_t *rangesptr;
391 uint32_t tuple_size, bank;
392 vm_paddr_t offset;
393 vm_size_t size;
394 int dev_num, addr_cells, size_cells, par_addr_cells, va_index, i, j, k;
395
396 if ((fdt_addrsize_cells(dt_node, &addr_cells, &size_cells)) != 0)
397 return (EINVAL);
398
399 par_addr_cells = fdt_parent_addr_cells(dt_node);
400 if (par_addr_cells > 2) {
401 /*
402 * Localbus devmap initialization error: unsupported parent
403 * #addr-cells
404 */
405 return (ERANGE);
406 }
407
408 tuple_size = (addr_cells + par_addr_cells + size_cells);
409 if (tuple_size > MV_LOCALBUS_MAX_BANK_CELLS)
410 return (ERANGE);
411
412 tuple_size *= sizeof(pcell_t);
413
414 dev_num = OF_getprop(dt_node, "ranges", ranges, sizeof(ranges));
415 if (dev_num <= 0)
416 return (EINVAL);
417
418 /* Calculate number of devices attached to bus */
419 dev_num = dev_num / tuple_size;
420
421 /*
422 * If number of ranges > max number of localbus devices,
423 * additional entries will not be processed
424 */
425 dev_num = MIN(dev_num, banks_max_num);
426
427 rangesptr = &ranges[0];
428 j = 0;
429
430 /* Process data from FDT */
431 for (i = 0; i < dev_num; i++) {
432
433 /* First field is bank number */
434 bank = fdt_data_get((void *)rangesptr, 1);
435 rangesptr += 1;
436
437 if (bank < 0 || bank > MV_LOCALBUS_MAX_BANKS) {
438 /* Bank out of range */
439 rangesptr += ((addr_cells - 1) + par_addr_cells +
440 size_cells);
441 continue;
442 }
443
444 /* Find virtmap entry for this bank */
445 va_index = -1;
446 for (k = 0; localbus_virtmap[k].bank >= 0; k++) {
447 if (localbus_virtmap[k].bank == bank) {
448 va_index = k;
449 break;
450 }
451 }
452
453 /* Check if virtmap entry was found */
454 if (va_index == -1) {
455 rangesptr += ((addr_cells - 1) + par_addr_cells +
456 size_cells);
457 continue;
458 }
459
460 /* Remaining child's address fields are unused */
461 rangesptr += (addr_cells - 1);
462
463 /* Parent address offset */
464 offset = fdt_data_get((void *)rangesptr, par_addr_cells);
465 rangesptr += par_addr_cells;
466
467 /* Last field is size */
468 size = fdt_data_get((void *)rangesptr, size_cells);
469 rangesptr += size_cells;
470
471 if (size > localbus_virtmap[va_index].size) {
472 /* Not enough space reserved in virtual memory map */
473 continue;
474 }
475
476 fdt_devmap[j].pd_va = localbus_virtmap[va_index].va;
477 fdt_devmap[j].pd_pa = offset;
478 fdt_devmap[j].pd_size = size;
479 fdt_devmap[j].pd_prot = VM_PROT_READ | VM_PROT_WRITE;
480 fdt_devmap[j].pd_cache = PTE_DEVICE;
481
482 /* Copy data to structure used by localbus driver */
483 localbus_banks[bank].va = fdt_devmap[j].pd_va;
484 localbus_banks[bank].pa = fdt_devmap[j].pd_pa;
485 localbus_banks[bank].size = fdt_devmap[j].pd_size;
486 localbus_banks[bank].mapped = 1;
487
488 j++;
489 }
490
491 *banks_added = j;
492 return (0);
493 }
Cache object: 1ff4f6f6b7a44ba98548b7ebd03f03c0
|