2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2017-05-17 23:18:04 +00:00
|
|
|
/*
|
|
|
|
* Device addresses
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Google, Inc
|
|
|
|
*
|
|
|
|
* (C) Copyright 2012
|
|
|
|
* Pavel Herrmann <morpheus.ibis@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <fdt_support.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2017-05-17 23:18:04 +00:00
|
|
|
#include <asm/io.h>
|
|
|
|
#include <dm/device-internal.h>
|
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2020-01-27 15:49:39 +00:00
|
|
|
fdt_addr_t devfdt_get_addr_index(const struct udevice *dev, int index)
|
2017-05-17 23:18:04 +00:00
|
|
|
{
|
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
|
|
|
|
fdt_addr_t addr;
|
|
|
|
|
|
|
|
if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {
|
|
|
|
const fdt32_t *reg;
|
|
|
|
int len = 0;
|
|
|
|
int na, ns;
|
|
|
|
|
|
|
|
na = fdt_address_cells(gd->fdt_blob,
|
|
|
|
dev_of_offset(dev->parent));
|
|
|
|
if (na < 1) {
|
|
|
|
debug("bad #address-cells\n");
|
|
|
|
return FDT_ADDR_T_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ns = fdt_size_cells(gd->fdt_blob, dev_of_offset(dev->parent));
|
|
|
|
if (ns < 0) {
|
|
|
|
debug("bad #size-cells\n");
|
|
|
|
return FDT_ADDR_T_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
reg = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "reg",
|
|
|
|
&len);
|
|
|
|
if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) {
|
|
|
|
debug("Req index out of range\n");
|
|
|
|
return FDT_ADDR_T_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
reg += index * (na + ns);
|
|
|
|
|
fdt: translate address if #size-cells = <0>
The __of_translate_address routine translates an address from the
device tree into a CPU physical address. A note in the description of
the routine explains that the crossing of any level with
since inherited from IBM. This does not happen for Texas Instruments, or
at least for the beaglebone device tree. Without this patch, in fact,
the translation into physical addresses of the registers contained in the
am33xx-clocks.dtsi nodes would not be possible. They all have a parent
with #size-cells = <0>.
The CONFIG_OF_TRANSLATE_ZERO_SIZE_CELLS symbol makes translation
possible even in the case of crossing levels with #size-cells = <0>.
The patch acts conservatively on address translation, except for
removing a check within the of_translate_one function in the
drivers/core/of_addr.c file:
+
ranges = of_get_property(parent, rprop, &rlen);
- if (ranges == NULL && !of_empty_ranges_quirk(parent)) {
- debug("no ranges; cannot translate\n");
- return 1;
- }
if (ranges == NULL || rlen == 0) {
offset = of_read_number(addr, na);
memset(addr, 0, pna * 4);
debug("empty ranges; 1:1 translation\n");
There are two reasons:
1 The function of_empty_ranges_quirk always returns false, invalidating
the following if statement in case of null ranges. Therefore one of
the two checks is useless.
2 The implementation of the of_translate_one function found in the
common/fdt_support.c file has removed this check while keeping the one
about the 1:1 translation.
The patch adds a test and modifies a check for the correctness of an
address in the case of enabling translation also for zero size cells.
The added test checks translations of addresses generated by nodes of
a device tree similar to those you can find in the files am33xx.dtsi
and am33xx-clocks.dtsi for which the patch was created.
The patch was also tested on a beaglebone black board. The addresses
generated for the registers of the loaded drivers are those specified
by the AM335x reference manual.
Signed-off-by: Dario Binacchi <dariobin@libero.it>
Tested-by: Dario Binacchi <dariobin@libero.it>
Reviewed-by: Simon Glass <sjg@chromium.org>
2020-12-29 23:16:21 +00:00
|
|
|
if (ns || gd_size_cells_0()) {
|
2018-03-12 13:53:33 +00:00
|
|
|
/*
|
|
|
|
* Use the full-fledged translate function for complex
|
|
|
|
* bus setups.
|
|
|
|
*/
|
|
|
|
addr = fdt_translate_address((void *)gd->fdt_blob,
|
|
|
|
dev_of_offset(dev), reg);
|
|
|
|
} else {
|
|
|
|
/* Non translatable if #size-cells == 0 */
|
|
|
|
addr = fdt_read_number(reg, na);
|
|
|
|
}
|
2017-05-17 23:18:04 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Use the "simple" translate function for less complex
|
|
|
|
* bus setups.
|
|
|
|
*/
|
|
|
|
addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
|
|
|
|
dev_of_offset(dev->parent), dev_of_offset(dev),
|
|
|
|
"reg", index, NULL, false);
|
|
|
|
if (CONFIG_IS_ENABLED(SIMPLE_BUS) && addr != FDT_ADDR_T_NONE) {
|
|
|
|
if (device_get_uclass_id(dev->parent) ==
|
|
|
|
UCLASS_SIMPLE_BUS)
|
|
|
|
addr = simple_bus_translate(dev->parent, addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 14:42:28 +00:00
|
|
|
#if defined(CONFIG_TRANSLATION_OFFSET)
|
2017-05-17 23:18:04 +00:00
|
|
|
/*
|
|
|
|
* Some platforms need a special address translation. Those
|
|
|
|
* platforms (e.g. mvebu in SPL) can configure a translation
|
2019-04-12 14:42:28 +00:00
|
|
|
* offset by setting this value in the GD and enaling this
|
|
|
|
* feature via CONFIG_TRANSLATION_OFFSET. This value will
|
|
|
|
* get added to all addresses returned by devfdt_get_addr().
|
2017-05-17 23:18:04 +00:00
|
|
|
*/
|
2019-04-12 14:42:28 +00:00
|
|
|
addr += gd->translation_offset;
|
|
|
|
#endif
|
2017-05-17 23:18:04 +00:00
|
|
|
|
|
|
|
return addr;
|
|
|
|
#else
|
|
|
|
return FDT_ADDR_T_NONE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:49:39 +00:00
|
|
|
fdt_addr_t devfdt_get_addr_size_index(const struct udevice *dev, int index,
|
|
|
|
fdt_size_t *size)
|
2017-05-17 23:18:04 +00:00
|
|
|
{
|
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL)
|
|
|
|
/*
|
|
|
|
* Only get the size in this first call. We'll get the addr in the
|
|
|
|
* next call to the exisiting dev_get_xxx function which handles
|
|
|
|
* all config options.
|
|
|
|
*/
|
|
|
|
fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, dev_of_offset(dev),
|
|
|
|
"reg", index, size, false);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the base address via the existing function which handles
|
|
|
|
* all Kconfig cases
|
|
|
|
*/
|
2017-05-17 23:18:05 +00:00
|
|
|
return devfdt_get_addr_index(dev, index);
|
2017-05-17 23:18:04 +00:00
|
|
|
#else
|
|
|
|
return FDT_ADDR_T_NONE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:49:39 +00:00
|
|
|
fdt_addr_t devfdt_get_addr_name(const struct udevice *dev, const char *name)
|
2017-05-17 23:18:04 +00:00
|
|
|
{
|
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL)
|
|
|
|
int index;
|
|
|
|
|
|
|
|
index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
|
|
|
|
"reg-names", name);
|
|
|
|
if (index < 0)
|
|
|
|
return index;
|
|
|
|
|
2017-05-17 23:18:05 +00:00
|
|
|
return devfdt_get_addr_index(dev, index);
|
2017-05-17 23:18:04 +00:00
|
|
|
#else
|
|
|
|
return FDT_ADDR_T_NONE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:49:39 +00:00
|
|
|
fdt_addr_t devfdt_get_addr_size_name(const struct udevice *dev,
|
|
|
|
const char *name, fdt_size_t *size)
|
2019-08-01 13:42:56 +00:00
|
|
|
{
|
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL)
|
|
|
|
int index;
|
|
|
|
|
|
|
|
index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
|
|
|
|
"reg-names", name);
|
|
|
|
if (index < 0)
|
|
|
|
return index;
|
|
|
|
|
|
|
|
return devfdt_get_addr_size_index(dev, index, size);
|
|
|
|
#else
|
|
|
|
return FDT_ADDR_T_NONE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:49:39 +00:00
|
|
|
fdt_addr_t devfdt_get_addr(const struct udevice *dev)
|
2017-05-17 23:18:04 +00:00
|
|
|
{
|
2017-05-17 23:18:05 +00:00
|
|
|
return devfdt_get_addr_index(dev, 0);
|
2017-05-17 23:18:04 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 15:49:39 +00:00
|
|
|
void *devfdt_get_addr_ptr(const struct udevice *dev)
|
2017-05-17 23:18:04 +00:00
|
|
|
{
|
2020-08-03 19:17:35 +00:00
|
|
|
fdt_addr_t addr = devfdt_get_addr_index(dev, 0);
|
|
|
|
|
|
|
|
return (addr == FDT_ADDR_T_NONE) ? NULL : (void *)(uintptr_t)addr;
|
2017-05-17 23:18:04 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 15:49:39 +00:00
|
|
|
void *devfdt_remap_addr_index(const struct udevice *dev, int index)
|
2018-04-29 19:56:54 +00:00
|
|
|
{
|
2018-08-14 10:48:11 +00:00
|
|
|
fdt_addr_t addr = devfdt_get_addr_index(dev, index);
|
2018-04-29 19:56:54 +00:00
|
|
|
|
|
|
|
if (addr == FDT_ADDR_T_NONE)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return map_physmem(addr, 0, MAP_NOCACHE);
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:49:39 +00:00
|
|
|
void *devfdt_remap_addr_name(const struct udevice *dev, const char *name)
|
2018-12-03 18:37:09 +00:00
|
|
|
{
|
|
|
|
fdt_addr_t addr = devfdt_get_addr_name(dev, name);
|
|
|
|
|
|
|
|
if (addr == FDT_ADDR_T_NONE)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return map_physmem(addr, 0, MAP_NOCACHE);
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:49:39 +00:00
|
|
|
void *devfdt_remap_addr(const struct udevice *dev)
|
2018-04-29 19:56:54 +00:00
|
|
|
{
|
|
|
|
return devfdt_remap_addr_index(dev, 0);
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:49:39 +00:00
|
|
|
void *devfdt_map_physmem(const struct udevice *dev, unsigned long size)
|
2017-05-17 23:18:04 +00:00
|
|
|
{
|
2017-05-17 23:18:05 +00:00
|
|
|
fdt_addr_t addr = devfdt_get_addr(dev);
|
2017-05-17 23:18:04 +00:00
|
|
|
|
|
|
|
if (addr == FDT_ADDR_T_NONE)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return map_physmem(addr, size, MAP_NOCACHE);
|
|
|
|
}
|
2019-09-15 18:08:58 +00:00
|
|
|
|
2020-01-27 15:49:39 +00:00
|
|
|
fdt_addr_t devfdt_get_addr_pci(const struct udevice *dev)
|
2019-09-15 18:08:58 +00:00
|
|
|
{
|
|
|
|
ulong addr;
|
|
|
|
|
|
|
|
addr = devfdt_get_addr(dev);
|
|
|
|
if (CONFIG_IS_ENABLED(PCI) && IS_ENABLED(CONFIG_DM_PCI) &&
|
|
|
|
addr == FDT_ADDR_T_NONE) {
|
|
|
|
struct fdt_pci_addr pci_addr;
|
|
|
|
u32 bar;
|
|
|
|
int ret;
|
|
|
|
|
2019-09-25 14:56:41 +00:00
|
|
|
ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_MEM32,
|
|
|
|
"reg", &pci_addr);
|
2019-09-15 18:08:58 +00:00
|
|
|
if (ret) {
|
|
|
|
/* try if there is any i/o-mapped register */
|
2019-09-25 14:56:41 +00:00
|
|
|
ret = ofnode_read_pci_addr(dev_ofnode(dev),
|
|
|
|
FDT_PCI_SPACE_IO, "reg",
|
|
|
|
&pci_addr);
|
2019-09-15 18:08:58 +00:00
|
|
|
if (ret)
|
|
|
|
return FDT_ADDR_T_NONE;
|
|
|
|
}
|
|
|
|
ret = fdtdec_get_pci_bar32(dev, &pci_addr, &bar);
|
|
|
|
if (ret)
|
|
|
|
return FDT_ADDR_T_NONE;
|
|
|
|
addr = bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|