]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/mv/mv_localbus.c
Import 1.14.3
[FreeBSD/FreeBSD.git] / sys / arm / mv / mv_localbus.c
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$");
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 #include <sys/devmap.h>
40
41 #include <vm/vm.h>
42
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/mvvar.h>
53 #include <arm/mv/mvwin.h>
54
55 #ifdef DEBUG
56 #define debugf(fmt, args...) do { printf("%s(): ", __func__);   \
57     printf(fmt,##args); } while (0)
58 #else
59 #define debugf(fmt, args...)
60 #endif
61
62 #define MV_LOCALBUS_MAX_BANKS           8
63 #define MV_LOCALBUS_MAX_BANK_CELLS      4
64
65 static MALLOC_DEFINE(M_LOCALBUS, "localbus", "localbus devices information");
66
67 struct localbus_bank {
68         vm_offset_t     va;             /* VA of the bank */
69         vm_paddr_t      pa;             /* physical address of the bank */
70         vm_size_t       size;           /* bank size */
71         uint8_t         mapped;         /* device memory has mapping */
72 };
73
74 struct localbus_softc {
75         device_t                sc_dev;
76         bus_space_handle_t      sc_bsh;
77         bus_space_tag_t         sc_bst;
78         int                     sc_rid;
79
80         struct localbus_bank    *sc_banks;
81 };
82
83 struct localbus_devinfo {
84         struct ofw_bus_devinfo  di_ofw;
85         struct resource_list    di_res;
86         int                     di_bank;
87 };
88
89 struct localbus_va_entry {
90         int8_t          bank;
91         vm_offset_t     va;
92         vm_size_t       size;
93 };
94
95 /*
96  * Prototypes.
97  */
98 static int localbus_probe(device_t);
99 static int localbus_attach(device_t);
100 static int localbus_print_child(device_t, device_t);
101
102 static struct resource *localbus_alloc_resource(device_t, device_t, int,
103     int *, rman_res_t, rman_res_t, rman_res_t, u_int);
104 static struct resource_list *localbus_get_resource_list(device_t, device_t);
105
106 static ofw_bus_get_devinfo_t localbus_get_devinfo;
107
108 /*
109  * Bus interface definition.
110  */
111 static device_method_t localbus_methods[] = {
112         /* Device interface */
113         DEVMETHOD(device_probe,         localbus_probe),
114         DEVMETHOD(device_attach,        localbus_attach),
115         DEVMETHOD(device_detach,        bus_generic_detach),
116         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
117         DEVMETHOD(device_suspend,       bus_generic_suspend),
118         DEVMETHOD(device_resume,        bus_generic_resume),
119
120         /* Bus interface */
121         DEVMETHOD(bus_print_child,      localbus_print_child),
122         DEVMETHOD(bus_alloc_resource,   localbus_alloc_resource),
123         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
124         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
125         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
126         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
127         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
128         DEVMETHOD(bus_get_resource_list, localbus_get_resource_list),
129
130         /* OFW bus interface */
131         DEVMETHOD(ofw_bus_get_devinfo,  localbus_get_devinfo),
132         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
133         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
134         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
135         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
136         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
137
138         { 0, 0 }
139 };
140
141 static driver_t localbus_driver = {
142         "localbus",
143         localbus_methods,
144         sizeof(struct localbus_softc)
145 };
146
147 const struct localbus_va_entry localbus_virtmap[] = {
148         {  0, MV_DEV_BOOT_BASE,         MV_DEV_BOOT_SIZE },
149         {  1, MV_DEV_CS0_BASE,          MV_DEV_CS0_SIZE },
150         {  2, MV_DEV_CS1_BASE,          MV_DEV_CS1_SIZE },
151         {  3, MV_DEV_CS2_BASE,          MV_DEV_CS2_SIZE },
152
153         { -1, 0, 0 }
154 };
155
156 static struct localbus_bank localbus_banks[MV_LOCALBUS_MAX_BANKS];
157
158 devclass_t localbus_devclass;
159
160 DRIVER_MODULE(localbus, ofwbus, localbus_driver, localbus_devclass, 0, 0);
161
162 static int
163 fdt_localbus_reg_decode(phandle_t node, struct localbus_softc *sc,
164     struct localbus_devinfo *di)
165 {
166         u_long start, end, count;
167         pcell_t *reg, *regptr;
168         pcell_t addr_cells, size_cells;
169         int tuple_size, tuples;
170         int i, rv, bank;
171
172         if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells) != 0)
173                 return (ENXIO);
174
175         tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
176         tuples = OF_getprop_alloc(node, "reg", tuple_size, (void **)&reg);
177         debugf("addr_cells = %d, size_cells = %d\n", addr_cells, size_cells);
178         debugf("tuples = %d, tuple size = %d\n", tuples, tuple_size);
179         if (tuples <= 0)
180                 /* No 'reg' property in this node. */
181                 return (0);
182
183         regptr = reg;
184         for (i = 0; i < tuples; i++) {
185
186                 bank = fdt_data_get((void *)regptr, 1);
187
188                 if (bank >= MV_LOCALBUS_MAX_BANKS) {
189                         device_printf(sc->sc_dev, "bank number [%d] out of "
190                             "range\n", bank);
191                         continue;
192                 }
193
194                 /*
195                  * If device doesn't have virtual to physical mapping don't add
196                  * resources
197                  */
198                 if (!(sc->sc_banks[bank].mapped)) {
199                         device_printf(sc->sc_dev, "device [%d]: missing memory "
200                             "mapping\n", bank);
201                         continue;
202                 }
203
204                 di->di_bank = bank;
205                 regptr += 1;
206
207                 /* Get address/size. */
208                 rv = fdt_data_to_res(regptr, addr_cells - 1, size_cells, &start,
209                     &count);
210                 if (rv != 0) {
211                         resource_list_free(&di->di_res);
212                         goto out;
213                 }
214
215                 /* Check if enough amount of memory is mapped */
216                 if (sc->sc_banks[bank].size < count) {
217                         device_printf(sc->sc_dev, "device [%d]: not enough "
218                             "memory reserved\n", bank);
219                         continue;
220                 }
221
222                 regptr += addr_cells - 1 + size_cells;
223
224                 /* Calculate address range relative to VA base. */
225                 start = sc->sc_banks[bank].va + start;
226                 end = start + count - 1;
227
228                 debugf("reg addr bank = %d, start = %lx, end = %lx, "
229                     "count = %lx\n", bank, start, end, count);
230
231                 /* Use bank (CS) cell as rid. */
232                 resource_list_add(&di->di_res, SYS_RES_MEMORY, di->di_bank,
233                     start, end, count);
234         }
235         rv = 0;
236 out:
237         OF_prop_free(reg);
238         return (rv);
239 }
240
241 static int
242 localbus_probe(device_t dev)
243 {
244
245         if (!ofw_bus_is_compatible_strict(dev, "mrvl,lbc"))
246                 return (ENXIO);
247
248         device_set_desc(dev, "Marvell device bus");
249
250         return (BUS_PROBE_DEFAULT);
251 }
252
253 static int
254 localbus_attach(device_t dev)
255 {
256         device_t dev_child;
257         struct localbus_softc *sc;
258         struct localbus_devinfo *di;
259         phandle_t dt_node, dt_child;
260
261         sc = device_get_softc(dev);
262         sc->sc_dev = dev;
263         sc->sc_banks = localbus_banks;
264
265         /*
266          * Walk localbus and add direct subordinates as our children.
267          */
268         dt_node = ofw_bus_get_node(dev);
269         for (dt_child = OF_child(dt_node); dt_child != 0;
270             dt_child = OF_peer(dt_child)) {
271
272                 /* Check and process 'status' property. */
273                 if (!(fdt_is_enabled(dt_child)))
274                         continue;
275
276                 if (!(fdt_pm_is_enabled(dt_child)))
277                         continue;
278
279                 di = malloc(sizeof(*di), M_LOCALBUS, M_WAITOK | M_ZERO);
280                 if (ofw_bus_gen_setup_devinfo(&di->di_ofw, dt_child) != 0) {
281                         free(di, M_LOCALBUS);
282                         device_printf(dev, "could not set up devinfo\n");
283                         continue;
284                 }
285
286                 resource_list_init(&di->di_res);
287                 if (fdt_localbus_reg_decode(dt_child, sc, di)) {
288                         device_printf(dev, "could not process 'reg' "
289                             "property\n");
290                         ofw_bus_gen_destroy_devinfo(&di->di_ofw);
291                         free(di, M_LOCALBUS);
292                         continue;
293                 }
294
295                 /* Add newbus device for this FDT node */
296                 dev_child = device_add_child(dev, NULL, -1);
297                 if (dev_child == NULL) {
298                         device_printf(dev, "could not add child: %s\n",
299                             di->di_ofw.obd_name);
300                         resource_list_free(&di->di_res);
301                         ofw_bus_gen_destroy_devinfo(&di->di_ofw);
302                         free(di, M_LOCALBUS);
303                         continue;
304                 }
305 #ifdef DEBUG
306                 device_printf(dev, "added child: %s\n\n", di->di_ofw.obd_name);
307 #endif
308                 device_set_ivars(dev_child, di);
309         }
310
311         return (bus_generic_attach(dev));
312 }
313
314 static int
315 localbus_print_child(device_t dev, device_t child)
316 {
317         struct localbus_devinfo *di;
318         struct resource_list *rl;
319         int rv;
320
321         di = device_get_ivars(child);
322         rl = &di->di_res;
323
324         rv = 0;
325         rv += bus_print_child_header(dev, child);
326         rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
327         rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
328         rv += bus_print_child_footer(dev, child);
329
330         return (rv);
331 }
332
333 static struct resource *
334 localbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
335     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
336 {
337         struct localbus_devinfo *di;
338         struct resource_list_entry *rle;
339
340         /*
341          * Request for the default allocation with a given rid: use resource
342          * list stored in the local device info.
343          */
344         if (RMAN_IS_DEFAULT_RANGE(start, end)) {
345                 if ((di = device_get_ivars(child)) == NULL)
346                         return (NULL);
347
348                 if (type == SYS_RES_IOPORT)
349                         type = SYS_RES_MEMORY;
350
351                 rid = &di->di_bank;
352                 rle = resource_list_find(&di->di_res, type, *rid);
353                 if (rle == NULL) {
354                         device_printf(bus, "no default resources for "
355                             "rid = %d, type = %d\n", *rid, type);
356                         return (NULL);
357                 }
358                 start = rle->start;
359                 end = rle->end;
360                 count = rle->count;
361         }
362
363         return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
364             count, flags));
365 }
366
367
368 static struct resource_list *
369 localbus_get_resource_list(device_t bus, device_t child)
370 {
371         struct localbus_devinfo *di;
372
373         di = device_get_ivars(child);
374         return (&di->di_res);
375 }
376
377 static const struct ofw_bus_devinfo *
378 localbus_get_devinfo(device_t bus, device_t child)
379 {
380         struct localbus_devinfo *di;
381
382         di = device_get_ivars(child);
383         return (&di->di_ofw);
384 }
385
386 int
387 fdt_localbus_devmap(phandle_t dt_node, struct devmap_entry *fdt_devmap,
388     int banks_max_num, int *banks_added)
389 {
390         pcell_t ranges[MV_LOCALBUS_MAX_BANKS * MV_LOCALBUS_MAX_BANK_CELLS];
391         pcell_t *rangesptr;
392         uint32_t tuple_size, bank;
393         vm_paddr_t offset;
394         vm_size_t size;
395         int dev_num, addr_cells, size_cells, par_addr_cells, va_index, i, j, k;
396
397         if ((fdt_addrsize_cells(dt_node, &addr_cells, &size_cells)) != 0)
398                 return (EINVAL);
399
400         par_addr_cells = fdt_parent_addr_cells(dt_node);
401         if (par_addr_cells > 2) {
402                 /*
403                  * Localbus devmap initialization error: unsupported parent
404                  * #addr-cells
405                  */
406                 return (ERANGE);
407         }
408
409         tuple_size = (addr_cells + par_addr_cells + size_cells);
410         if (tuple_size > MV_LOCALBUS_MAX_BANK_CELLS)
411                 return (ERANGE);
412
413         tuple_size *= sizeof(pcell_t);
414
415         dev_num = OF_getprop(dt_node, "ranges", ranges, sizeof(ranges));
416         if (dev_num <= 0)
417                 return (EINVAL);
418
419         /* Calculate number of devices attached to bus */
420         dev_num = dev_num / tuple_size;
421
422         /*
423          * If number of ranges > max number of localbus devices,
424          * additional entries will not be processed
425          */
426         dev_num = MIN(dev_num, banks_max_num);
427
428         rangesptr = &ranges[0];
429         j = 0;
430
431         /* Process data from FDT */
432         for (i = 0; i < dev_num; i++) {
433
434                 /* First field is bank number */
435                 bank = fdt_data_get((void *)rangesptr, 1);
436                 rangesptr += 1;
437
438                 if (bank > MV_LOCALBUS_MAX_BANKS) {
439                         /* Bank out of range */
440                         rangesptr += ((addr_cells - 1) + par_addr_cells +
441                             size_cells);
442                         continue;
443                 }
444
445                 /* Find virtmap entry for this bank */
446                 va_index = -1;
447                 for (k = 0; localbus_virtmap[k].bank >= 0; k++) {
448                         if (localbus_virtmap[k].bank == bank) {
449                                 va_index = k;
450                                 break;
451                         }
452                 }
453
454                 /* Check if virtmap entry was found */
455                 if (va_index == -1) {
456                         rangesptr += ((addr_cells - 1) + par_addr_cells +
457                             size_cells);
458                         continue;
459                 }
460
461                 /* Remaining child's address fields are unused */
462                 rangesptr += (addr_cells - 1);
463
464                 /* Parent address offset */
465                 offset = fdt_data_get((void *)rangesptr, par_addr_cells);
466                 rangesptr += par_addr_cells;
467
468                 /* Last field is size */
469                 size = fdt_data_get((void *)rangesptr, size_cells);
470                 rangesptr += size_cells;
471
472                 if (size > localbus_virtmap[va_index].size) {
473                         /* Not enough space reserved in virtual memory map */
474                         continue;
475                 }
476
477                 fdt_devmap[j].pd_va = localbus_virtmap[va_index].va;
478                 fdt_devmap[j].pd_pa = offset;
479                 fdt_devmap[j].pd_size = size;
480
481                 /* Copy data to structure used by localbus driver */
482                 localbus_banks[bank].va = fdt_devmap[j].pd_va;
483                 localbus_banks[bank].pa = fdt_devmap[j].pd_pa;
484                 localbus_banks[bank].size = fdt_devmap[j].pd_size;
485                 localbus_banks[bank].mapped = 1;
486
487                 j++;
488         }
489
490         *banks_added = j;
491         return (0);
492 }