^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #define pr_fmt(fmt) "OF: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/fwnode.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/logic_pio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/pci_regs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sizes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/dma-direct.h> /* for bus_dma_region */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "of_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* Max address size we deal with */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define OF_MAX_ADDR_CELLS 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static struct of_bus *of_match_bus(struct device_node *np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int __of_address_to_resource(struct device_node *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) const __be32 *addrp, u64 size, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) const char *name, struct resource *r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* Debug utility */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static void of_dump_addr(const char *s, const __be32 *addr, int na)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) pr_debug("%s", s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) while (na--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) pr_cont(" %08x", be32_to_cpu(*(addr++)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) pr_cont("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* Callbacks for bus specific translators */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct of_bus {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) const char *addresses;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int (*match)(struct device_node *parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) void (*count_cells)(struct device_node *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int *addrc, int *sizec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) u64 (*map)(__be32 *addr, const __be32 *range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int na, int ns, int pna);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int (*translate)(__be32 *addr, u64 offset, int na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) bool has_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned int (*get_flags)(const __be32 *addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * Default translator (generic bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static void of_bus_default_count_cells(struct device_node *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int *addrc, int *sizec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (addrc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *addrc = of_n_addr_cells(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (sizec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *sizec = of_n_size_cells(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int na, int ns, int pna)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) u64 cp, s, da;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) cp = of_read_number(range, na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) s = of_read_number(range + na + pna, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) da = of_read_number(addr, na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) pr_debug("default map, cp=%llx, s=%llx, da=%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) (unsigned long long)cp, (unsigned long long)s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) (unsigned long long)da);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (da < cp || da >= (cp + s))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return OF_BAD_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return da - cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) u64 a = of_read_number(addr, na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) memset(addr, 0, na * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) a += offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (na > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) addr[na - 2] = cpu_to_be32(a >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static unsigned int of_bus_default_get_flags(const __be32 *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return IORESOURCE_MEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #ifdef CONFIG_PCI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static unsigned int of_bus_pci_get_flags(const __be32 *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) unsigned int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) u32 w = be32_to_cpup(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (!IS_ENABLED(CONFIG_PCI))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) switch((w >> 24) & 0x03) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) case 0x01:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) flags |= IORESOURCE_IO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) case 0x02: /* 32 bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) case 0x03: /* 64 bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) flags |= IORESOURCE_MEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (w & 0x40000000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) flags |= IORESOURCE_PREFETCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * PCI bus specific translator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static bool of_node_is_pcie(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) bool is_pcie = of_node_name_eq(np, "pcie");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (is_pcie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) pr_warn_once("%pOF: Missing device_type\n", np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return is_pcie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int of_bus_pci_match(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * "pciex" is PCI Express
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * "ht" is hypertransport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * If none of the device_type match, and that the node name is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * "pcie", accept the device as PCI (with a warning).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) of_node_is_type(np, "vci") || of_node_is_type(np, "ht") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) of_node_is_pcie(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static void of_bus_pci_count_cells(struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) int *addrc, int *sizec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (addrc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) *addrc = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (sizec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) *sizec = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int pna)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) u64 cp, s, da;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) unsigned int af, rf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) af = of_bus_pci_get_flags(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) rf = of_bus_pci_get_flags(range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /* Check address type match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return OF_BAD_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* Read address values, skipping high cell */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) cp = of_read_number(range + 1, na - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) s = of_read_number(range + na + pna, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) da = of_read_number(addr + 1, na - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) pr_debug("PCI map, cp=%llx, s=%llx, da=%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) (unsigned long long)cp, (unsigned long long)s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) (unsigned long long)da);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (da < cp || da >= (cp + s))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return OF_BAD_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return da - cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int of_bus_pci_translate(__be32 *addr, u64 offset, int na)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return of_bus_default_translate(addr + 1, offset, na - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) unsigned int *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) const __be32 *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) unsigned int psize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct device_node *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct of_bus *bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) int onesize, i, na, ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /* Get parent & match bus type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) parent = of_get_parent(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (parent == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) bus = of_match_bus(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (strcmp(bus->name, "pci")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) of_node_put(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) bus->count_cells(dev, &na, &ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) of_node_put(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (!OF_CHECK_ADDR_COUNT(na))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /* Get "reg" or "assigned-addresses" property */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) prop = of_get_property(dev, bus->addresses, &psize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (prop == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) psize /= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) onesize = na + ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) u32 val = be32_to_cpu(prop[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) *size = of_read_number(prop + na, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) *flags = bus->get_flags(prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) EXPORT_SYMBOL(of_get_pci_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) int of_pci_address_to_resource(struct device_node *dev, int bar,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct resource *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) const __be32 *addrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) u64 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) addrp = of_get_pci_address(dev, bar, &size, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (addrp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return __of_address_to_resource(dev, addrp, size, flags, NULL, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * of_pci_range_to_resource - Create a resource from an of_pci_range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * @range: the PCI range that describes the resource
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * @np: device node where the range belongs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * @res: pointer to a valid resource that will be updated to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * reflect the values contained in the range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * Returns EINVAL if the range cannot be converted to resource.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * Note that if the range is an IO range, the resource will be converted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * using pci_address_to_pio() which can fail if it is called too early or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * if the range cannot be matched to any host bridge IO space (our case here).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * To guard against that we try to register the IO range first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * If that fails we know that pci_address_to_pio() will do too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) int of_pci_range_to_resource(struct of_pci_range *range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct device_node *np, struct resource *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) res->flags = range->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) res->parent = res->child = res->sibling = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) res->name = np->full_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (res->flags & IORESOURCE_IO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) unsigned long port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) err = pci_register_io_range(&np->fwnode, range->cpu_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) range->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) goto invalid_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) port = pci_address_to_pio(range->cpu_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (port == (unsigned long)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) goto invalid_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) res->start = port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if ((sizeof(resource_size_t) < 8) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) upper_32_bits(range->cpu_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) goto invalid_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) res->start = range->cpu_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) res->end = res->start + range->size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) invalid_range:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) res->start = (resource_size_t)OF_BAD_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) res->end = (resource_size_t)OF_BAD_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) EXPORT_SYMBOL(of_pci_range_to_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) #endif /* CONFIG_PCI */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * ISA bus specific translator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static int of_bus_isa_match(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return of_node_name_eq(np, "isa");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static void of_bus_isa_count_cells(struct device_node *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) int *addrc, int *sizec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (addrc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) *addrc = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (sizec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) *sizec = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) int pna)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) u64 cp, s, da;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* Check address type match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if ((addr[0] ^ range[0]) & cpu_to_be32(1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return OF_BAD_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) /* Read address values, skipping high cell */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) cp = of_read_number(range + 1, na - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) s = of_read_number(range + na + pna, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) da = of_read_number(addr + 1, na - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) pr_debug("ISA map, cp=%llx, s=%llx, da=%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) (unsigned long long)cp, (unsigned long long)s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) (unsigned long long)da);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (da < cp || da >= (cp + s))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return OF_BAD_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return da - cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static int of_bus_isa_translate(__be32 *addr, u64 offset, int na)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return of_bus_default_translate(addr + 1, offset, na - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static unsigned int of_bus_isa_get_flags(const __be32 *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) unsigned int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) u32 w = be32_to_cpup(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (w & 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) flags |= IORESOURCE_IO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) flags |= IORESOURCE_MEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * Array of bus specific translators
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static struct of_bus of_busses[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) #ifdef CONFIG_PCI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /* PCI */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) .name = "pci",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) .addresses = "assigned-addresses",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) .match = of_bus_pci_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) .count_cells = of_bus_pci_count_cells,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) .map = of_bus_pci_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) .translate = of_bus_pci_translate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) .has_flags = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) .get_flags = of_bus_pci_get_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) #endif /* CONFIG_PCI */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /* ISA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) .name = "isa",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) .addresses = "reg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) .match = of_bus_isa_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) .count_cells = of_bus_isa_count_cells,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) .map = of_bus_isa_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) .translate = of_bus_isa_translate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) .has_flags = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) .get_flags = of_bus_isa_get_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /* Default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) .name = "default",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) .addresses = "reg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) .match = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) .count_cells = of_bus_default_count_cells,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) .map = of_bus_default_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) .translate = of_bus_default_translate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) .get_flags = of_bus_default_get_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) static struct of_bus *of_match_bus(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) for (i = 0; i < ARRAY_SIZE(of_busses); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (!of_busses[i].match || of_busses[i].match(np))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return &of_busses[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) static int of_empty_ranges_quirk(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (IS_ENABLED(CONFIG_PPC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) /* To save cycles, we cache the result for global "Mac" setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) static int quirk_state = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) /* PA-SEMI sdc DT bug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (of_device_is_compatible(np, "1682m-sdc"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) /* Make quirk cached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (quirk_state < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) quirk_state =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) of_machine_is_compatible("Power Macintosh") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) of_machine_is_compatible("MacRISC");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) return quirk_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) static int of_translate_one(struct device_node *parent, struct of_bus *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) struct of_bus *pbus, __be32 *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) int na, int ns, int pna, const char *rprop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) const __be32 *ranges;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) unsigned int rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) int rone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) u64 offset = OF_BAD_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * Normally, an absence of a "ranges" property means we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * crossing a non-translatable boundary, and thus the addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * below the current cannot be converted to CPU physical ones.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * Unfortunately, while this is very clear in the spec, it's not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * what Apple understood, and they do have things like /uni-n or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * /ht nodes with no "ranges" property and a lot of perfectly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * useable mapped devices below them. Thus we treat the absence of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * "ranges" as equivalent to an empty "ranges" property which means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * a 1:1 translation at that level. It's up to the caller not to try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * to translate addresses that aren't supposed to be translated in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * the first place. --BenH.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * As far as we know, this damage only exists on Apple machines, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * This code is only enabled on powerpc. --gcl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * This quirk also applies for 'dma-ranges' which frequently exist in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * child nodes without 'dma-ranges' in the parent nodes. --RobH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) ranges = of_get_property(parent, rprop, &rlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) strcmp(rprop, "dma-ranges")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) pr_debug("no ranges; cannot translate\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if (ranges == NULL || rlen == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) offset = of_read_number(addr, na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) memset(addr, 0, pna * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) pr_debug("empty ranges; 1:1 translation\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) goto finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) pr_debug("walking ranges...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) /* Now walk through the ranges */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) rlen /= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) rone = na + pna + ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) for (; rlen >= rone; rlen -= rone, ranges += rone) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) offset = bus->map(addr, ranges, na, ns, pna);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (offset != OF_BAD_ADDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (offset == OF_BAD_ADDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) pr_debug("not found !\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) memcpy(addr, ranges + na, 4 * pna);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) finish:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) of_dump_addr("parent translation for:", addr, pna);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) pr_debug("with offset: %llx\n", (unsigned long long)offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) /* Translate it into parent bus space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return pbus->translate(addr, offset, pna);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * Translate an address from the device-tree into a CPU physical address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * this walks up the tree and applies the various bus mappings on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * Note: We consider that crossing any level with #size-cells == 0 to mean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) * that translation is impossible (that is we are not dealing with a value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) * that can be mapped to a cpu physical address). This is not really specified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * that way, but this is traditionally the way IBM at least do things
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) * Whenever the translation fails, the *host pointer will be set to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) * device that had registered logical PIO mapping, and the return code is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) * relative to that node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static u64 __of_translate_address(struct device_node *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct device_node *(*get_parent)(const struct device_node *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) const __be32 *in_addr, const char *rprop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) struct device_node **host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) struct device_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) struct of_bus *bus, *pbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) __be32 addr[OF_MAX_ADDR_CELLS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) int na, ns, pna, pns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) u64 result = OF_BAD_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) pr_debug("** translation for device %pOF **\n", dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) /* Increase refcount at current level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) of_node_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) *host = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) /* Get parent & match bus type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) parent = get_parent(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (parent == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) bus = of_match_bus(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) /* Count address cells & copy address locally */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) bus->count_cells(dev, &na, &ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (!OF_CHECK_COUNTS(na, ns)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) pr_debug("Bad cell count for %pOF\n", dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) memcpy(addr, in_addr, na * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) bus->name, na, ns, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) of_dump_addr("translating address:", addr, na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) /* Translate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) struct logic_pio_hwaddr *iorange;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) /* Switch to parent bus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) of_node_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) dev = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) parent = get_parent(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) /* If root, we have finished */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (parent == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) pr_debug("reached root node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) result = of_read_number(addr, na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) * For indirectIO device which has no ranges property, get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * the address from reg directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) iorange = find_io_range_by_fwnode(&dev->fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) result = of_read_number(addr + 1, na - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) pr_debug("indirectIO matched(%pOF) 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) dev, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) *host = of_node_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) /* Get new parent bus and counts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) pbus = of_match_bus(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) pbus->count_cells(dev, &pna, &pns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (!OF_CHECK_COUNTS(pna, pns)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) pr_err("Bad cell count for %pOF\n", dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) pbus->name, pna, pns, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) /* Apply bus translation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) /* Complete the move up one level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) na = pna;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) ns = pns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) bus = pbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) of_dump_addr("one level translation:", addr, na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) of_node_put(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) of_node_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) struct device_node *host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) u64 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) ret = __of_translate_address(dev, of_get_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) in_addr, "ranges", &host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) if (host) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) of_node_put(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return OF_BAD_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) EXPORT_SYMBOL(of_translate_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) static struct device_node *__of_get_dma_parent(const struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) struct of_phandle_args args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) int ret, index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) index = of_property_match_string(np, "interconnect-names", "dma-mem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) if (index < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) return of_get_parent(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) ret = of_parse_phandle_with_args(np, "interconnects",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) "#interconnect-cells",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) index, &args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) return of_get_parent(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) return of_node_get(args.np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) static struct device_node *of_get_next_dma_parent(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) struct device_node *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) parent = __of_get_dma_parent(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) struct device_node *host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) u64 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) ret = __of_translate_address(dev, __of_get_dma_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) in_addr, "dma-ranges", &host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) if (host) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) of_node_put(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) return OF_BAD_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) EXPORT_SYMBOL(of_translate_dma_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) unsigned int *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) const __be32 *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) unsigned int psize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) struct device_node *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) struct of_bus *bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) int onesize, i, na, ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) /* Get parent & match bus type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) parent = of_get_parent(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (parent == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) bus = of_match_bus(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) bus->count_cells(dev, &na, &ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) of_node_put(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) if (!OF_CHECK_ADDR_COUNT(na))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) /* Get "reg" or "assigned-addresses" property */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) prop = of_get_property(dev, bus->addresses, &psize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) if (prop == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) psize /= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) onesize = na + ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (i == index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) *size = of_read_number(prop + na, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) *flags = bus->get_flags(prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) return prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) EXPORT_SYMBOL(of_get_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) static int parser_init(struct of_pci_range_parser *parser,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) struct device_node *node, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) int rlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) parser->node = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) parser->pna = of_n_addr_cells(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) parser->na = of_bus_n_addr_cells(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) parser->ns = of_bus_n_size_cells(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) parser->dma = !strcmp(name, "dma-ranges");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) parser->bus = of_match_bus(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) parser->range = of_get_property(node, name, &rlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) if (parser->range == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) parser->end = parser->range + rlen / sizeof(__be32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) int of_pci_range_parser_init(struct of_pci_range_parser *parser,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) struct device_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) return parser_init(parser, node, "ranges");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) struct device_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) return parser_init(parser, node, "dma-ranges");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) #define of_dma_range_parser_init of_pci_dma_range_parser_init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) struct of_pci_range *range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) int na = parser->na;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) int ns = parser->ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) int np = parser->pna + na + ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) int busflag_na = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (!range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) if (!parser->range || parser->range + np > parser->end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) range->flags = parser->bus->get_flags(parser->range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) /* A extra cell for resource flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) if (parser->bus->has_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) busflag_na = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) if (parser->dma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) range->cpu_addr = of_translate_dma_address(parser->node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) parser->range + na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) range->cpu_addr = of_translate_address(parser->node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) parser->range + na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) range->size = of_read_number(parser->range + parser->pna + na, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) parser->range += np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) /* Now consume following elements while they are contiguous */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) while (parser->range + np <= parser->end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) u32 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) u64 bus_addr, cpu_addr, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) flags = parser->bus->get_flags(parser->range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) if (parser->dma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) cpu_addr = of_translate_dma_address(parser->node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) parser->range + na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) cpu_addr = of_translate_address(parser->node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) parser->range + na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) size = of_read_number(parser->range + parser->pna + na, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) if (flags != range->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) if (bus_addr != range->bus_addr + range->size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) cpu_addr != range->cpu_addr + range->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) range->size += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) parser->range += np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) return range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) u64 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) u64 taddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) unsigned long port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) struct device_node *host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) taddr = __of_translate_address(dev, of_get_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) in_addr, "ranges", &host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (host) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) /* host-specific port access */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) of_node_put(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) /* memory-mapped I/O range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) port = pci_address_to_pio(taddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) if (port == (unsigned long)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) return OF_BAD_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) return port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) static int __of_address_to_resource(struct device_node *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) const __be32 *addrp, u64 size, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) const char *name, struct resource *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) u64 taddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) if (flags & IORESOURCE_MEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) taddr = of_translate_address(dev, addrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) else if (flags & IORESOURCE_IO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) taddr = of_translate_ioport(dev, addrp, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) if (taddr == OF_BAD_ADDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) memset(r, 0, sizeof(struct resource));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) r->start = taddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) r->end = taddr + size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) r->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) r->name = name ? name : dev->full_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) * of_address_to_resource - Translate device tree address and return as resource
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) * Note that if your address is a PIO address, the conversion will fail if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) * the physical address can't be internally converted to an IO token with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) * pci_address_to_pio(), that is because it's either called too early or it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) * can't be matched to any host bridge IO space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) int of_address_to_resource(struct device_node *dev, int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) struct resource *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) const __be32 *addrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) u64 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) const char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) addrp = of_get_address(dev, index, &size, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) if (addrp == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) /* Get optional "reg-names" property to add a name to a resource */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) of_property_read_string_index(dev, "reg-names", index, &name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) return __of_address_to_resource(dev, addrp, size, flags, name, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) EXPORT_SYMBOL_GPL(of_address_to_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * of_iomap - Maps the memory mapped IO for a given device_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * @np: the device whose io range will be mapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) * @index: index of the io range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) * Returns a pointer to the mapped memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) void __iomem *of_iomap(struct device_node *np, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) struct resource res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) if (of_address_to_resource(np, index, &res))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) return ioremap(res.start, resource_size(&res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) EXPORT_SYMBOL(of_iomap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) * of_io_request_and_map - Requests a resource and maps the memory mapped IO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) * for a given device_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) * @device: the device whose io range will be mapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) * @index: index of the io range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) * @name: name "override" for the memory region request or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) * error code on failure. Usage example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) * base = of_io_request_and_map(node, 0, "foo");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) * if (IS_ERR(base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) * return PTR_ERR(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) void __iomem *of_io_request_and_map(struct device_node *np, int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) struct resource res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) void __iomem *mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (of_address_to_resource(np, index, &res))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) return IOMEM_ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) name = res.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) if (!request_mem_region(res.start, resource_size(&res), name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) return IOMEM_ERR_PTR(-EBUSY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) mem = ioremap(res.start, resource_size(&res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) if (!mem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) release_mem_region(res.start, resource_size(&res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) return IOMEM_ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) return mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) EXPORT_SYMBOL(of_io_request_and_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) #ifdef CONFIG_HAS_DMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) * of_dma_get_range - Get DMA range info and put it into a map array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) * @np: device node to get DMA range info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) * @map: dma range structure to return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) * Look in bottom up direction for the first "dma-ranges" property
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) * and parse it. Put the information into a DMA offset map array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) * dma-ranges format:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) * DMA addr (dma_addr) : naddr cells
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) * CPU addr (phys_addr_t) : pna cells
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) * size : nsize cells
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) * It returns -ENODEV if "dma-ranges" property was not found for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) * device in the DT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) struct device_node *node = of_node_get(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) const __be32 *ranges = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) bool found_dma_ranges = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) struct of_range_parser parser;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) struct of_range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) struct bus_dma_region *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) int len, num_ranges = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) ranges = of_get_property(node, "dma-ranges", &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) /* Ignore empty ranges, they imply no translation required */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) if (ranges && len > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) /* Once we find 'dma-ranges', then a missing one is an error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) if (found_dma_ranges && !ranges) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) found_dma_ranges = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) node = of_get_next_dma_parent(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) if (!node || !ranges) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) pr_debug("no dma-ranges found for node(%pOF)\n", np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) of_dma_range_parser_init(&parser, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) for_each_of_range(&parser, &range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) num_ranges++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) if (!r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) * Record all info in the generic DMA ranges array for struct device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) *map = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) of_dma_range_parser_init(&parser, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) for_each_of_range(&parser, &range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) range.bus_addr, range.cpu_addr, range.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) if (range.cpu_addr == OF_BAD_ADDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) range.bus_addr, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) r->cpu_start = range.cpu_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) r->dma_start = range.bus_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) r->size = range.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) r->offset = range.cpu_addr - range.bus_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) r++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) of_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) #endif /* CONFIG_HAS_DMA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) * @np: The node to start searching from or NULL to start from the root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) * Gets the highest CPU physical address that is addressable by all DMA masters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) * DMA constrained device is found, it returns PHYS_ADDR_MAX.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) phys_addr_t max_cpu_addr = PHYS_ADDR_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) struct of_range_parser parser;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) phys_addr_t subtree_max_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) struct of_range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) const __be32 *ranges;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) u64 cpu_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) if (!np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) np = of_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) ranges = of_get_property(np, "dma-ranges", &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) if (ranges && len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) of_dma_range_parser_init(&parser, np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) for_each_of_range(&parser, &range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) if (range.cpu_addr + range.size > cpu_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) cpu_end = range.cpu_addr + range.size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) if (max_cpu_addr > cpu_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) max_cpu_addr = cpu_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) for_each_available_child_of_node(np, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) subtree_max_addr = of_dma_get_max_cpu_address(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) if (max_cpu_addr > subtree_max_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) max_cpu_addr = subtree_max_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) return max_cpu_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) * of_dma_is_coherent - Check if device is coherent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) * @np: device node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) * It returns true if "dma-coherent" property was found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) * for this device in the DT, or if DMA is coherent by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) * default for OF devices on the current platform.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) bool of_dma_is_coherent(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) struct device_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) if (IS_ENABLED(CONFIG_OF_DMA_DEFAULT_COHERENT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) node = of_node_get(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) if (of_property_read_bool(node, "dma-coherent")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) of_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) node = of_get_next_dma_parent(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) of_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) EXPORT_SYMBOL_GPL(of_dma_is_coherent);