The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/arm/mv/mv_localbus.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 7bc273e474d7ffa72c2e2187565eb1bf


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.