sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / of / of_pci.c
blob0ee42c3e66a1900db49408577c8ae69e698e7223
1 #define pr_fmt(fmt) "OF: PCI: " fmt
3 #include <linux/kernel.h>
4 #include <linux/export.h>
5 #include <linux/of.h>
6 #include <linux/of_address.h>
7 #include <linux/of_device.h>
8 #include <linux/of_pci.h>
9 #include <linux/slab.h>
11 static inline int __of_pci_pci_compare(struct device_node *node,
12 unsigned int data)
14 int devfn;
16 devfn = of_pci_get_devfn(node);
17 if (devfn < 0)
18 return 0;
20 return devfn == data;
23 struct device_node *of_pci_find_child_device(struct device_node *parent,
24 unsigned int devfn)
26 struct device_node *node, *node2;
28 for_each_child_of_node(parent, node) {
29 if (__of_pci_pci_compare(node, devfn))
30 return node;
32 * Some OFs create a parent node "multifunc-device" as
33 * a fake root for all functions of a multi-function
34 * device we go down them as well.
36 if (!strcmp(node->name, "multifunc-device")) {
37 for_each_child_of_node(node, node2) {
38 if (__of_pci_pci_compare(node2, devfn)) {
39 of_node_put(node);
40 return node2;
45 return NULL;
47 EXPORT_SYMBOL_GPL(of_pci_find_child_device);
49 /**
50 * of_pci_get_devfn() - Get device and function numbers for a device node
51 * @np: device node
53 * Parses a standard 5-cell PCI resource and returns an 8-bit value that can
54 * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device
55 * and function numbers respectively. On error a negative error code is
56 * returned.
58 int of_pci_get_devfn(struct device_node *np)
60 unsigned int size;
61 const __be32 *reg;
63 reg = of_get_property(np, "reg", &size);
65 if (!reg || size < 5 * sizeof(__be32))
66 return -EINVAL;
68 return (be32_to_cpup(reg) >> 8) & 0xff;
70 EXPORT_SYMBOL_GPL(of_pci_get_devfn);
72 /**
73 * of_pci_parse_bus_range() - parse the bus-range property of a PCI device
74 * @node: device node
75 * @res: address to a struct resource to return the bus-range
77 * Returns 0 on success or a negative error-code on failure.
79 int of_pci_parse_bus_range(struct device_node *node, struct resource *res)
81 const __be32 *values;
82 int len;
84 values = of_get_property(node, "bus-range", &len);
85 if (!values || len < sizeof(*values) * 2)
86 return -EINVAL;
88 res->name = node->name;
89 res->start = be32_to_cpup(values++);
90 res->end = be32_to_cpup(values);
91 res->flags = IORESOURCE_BUS;
93 return 0;
95 EXPORT_SYMBOL_GPL(of_pci_parse_bus_range);
97 /**
98 * This function will try to obtain the host bridge domain number by
99 * finding a property called "linux,pci-domain" of the given device node.
101 * @node: device tree node with the domain information
103 * Returns the associated domain number from DT in the range [0-0xffff], or
104 * a negative value if the required property is not found.
106 int of_get_pci_domain_nr(struct device_node *node)
108 const __be32 *value;
109 int len;
110 u16 domain;
112 value = of_get_property(node, "linux,pci-domain", &len);
113 if (!value || len < sizeof(*value))
114 return -EINVAL;
116 domain = (u16)be32_to_cpup(value);
118 return domain;
120 EXPORT_SYMBOL_GPL(of_get_pci_domain_nr);
123 * This function will try to find the limitation of link speed by finding
124 * a property called "max-link-speed" of the given device node.
126 * @node: device tree node with the max link speed information
128 * Returns the associated max link speed from DT, or a negative value if the
129 * required property is not found or is invalid.
131 int of_pci_get_max_link_speed(struct device_node *node)
133 u32 max_link_speed;
135 if (of_property_read_u32(node, "max-link-speed", &max_link_speed) ||
136 max_link_speed > 4)
137 return -EINVAL;
139 return max_link_speed;
141 EXPORT_SYMBOL_GPL(of_pci_get_max_link_speed);
144 * of_pci_check_probe_only - Setup probe only mode if linux,pci-probe-only
145 * is present and valid
147 void of_pci_check_probe_only(void)
149 u32 val;
150 int ret;
152 ret = of_property_read_u32(of_chosen, "linux,pci-probe-only", &val);
153 if (ret) {
154 if (ret == -ENODATA || ret == -EOVERFLOW)
155 pr_warn("linux,pci-probe-only without valid value, ignoring\n");
156 return;
159 if (val)
160 pci_add_flags(PCI_PROBE_ONLY);
161 else
162 pci_clear_flags(PCI_PROBE_ONLY);
164 pr_info("PROBE_ONLY %sabled\n", val ? "en" : "dis");
166 EXPORT_SYMBOL_GPL(of_pci_check_probe_only);
168 #if defined(CONFIG_OF_ADDRESS)
170 * of_pci_get_host_bridge_resources - Parse PCI host bridge resources from DT
171 * @dev: device node of the host bridge having the range property
172 * @busno: bus number associated with the bridge root bus
173 * @bus_max: maximum number of buses for this bridge
174 * @resources: list where the range of resources will be added after DT parsing
175 * @io_base: pointer to a variable that will contain on return the physical
176 * address for the start of the I/O range. Can be NULL if the caller doesn't
177 * expect IO ranges to be present in the device tree.
179 * It is the caller's job to free the @resources list.
181 * This function will parse the "ranges" property of a PCI host bridge device
182 * node and setup the resource mapping based on its content. It is expected
183 * that the property conforms with the Power ePAPR document.
185 * It returns zero if the range parsing has been successful or a standard error
186 * value if it failed.
188 int of_pci_get_host_bridge_resources(struct device_node *dev,
189 unsigned char busno, unsigned char bus_max,
190 struct list_head *resources, resource_size_t *io_base)
192 struct resource_entry *window;
193 struct resource *res;
194 struct resource *bus_range;
195 struct of_pci_range range;
196 struct of_pci_range_parser parser;
197 char range_type[4];
198 int err;
200 if (io_base)
201 *io_base = (resource_size_t)OF_BAD_ADDR;
203 bus_range = kzalloc(sizeof(*bus_range), GFP_KERNEL);
204 if (!bus_range)
205 return -ENOMEM;
207 pr_info("host bridge %s ranges:\n", dev->full_name);
209 err = of_pci_parse_bus_range(dev, bus_range);
210 if (err) {
211 bus_range->start = busno;
212 bus_range->end = bus_max;
213 bus_range->flags = IORESOURCE_BUS;
214 pr_info(" No bus range found for %s, using %pR\n",
215 dev->full_name, bus_range);
216 } else {
217 if (bus_range->end > bus_range->start + bus_max)
218 bus_range->end = bus_range->start + bus_max;
220 pci_add_resource(resources, bus_range);
222 /* Check for ranges property */
223 err = of_pci_range_parser_init(&parser, dev);
224 if (err)
225 goto parse_failed;
227 pr_debug("Parsing ranges property...\n");
228 for_each_of_pci_range(&parser, &range) {
229 /* Read next ranges element */
230 if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
231 snprintf(range_type, 4, " IO");
232 else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
233 snprintf(range_type, 4, "MEM");
234 else
235 snprintf(range_type, 4, "err");
236 pr_info(" %s %#010llx..%#010llx -> %#010llx\n", range_type,
237 range.cpu_addr, range.cpu_addr + range.size - 1,
238 range.pci_addr);
241 * If we failed translation or got a zero-sized region
242 * then skip this range
244 if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
245 continue;
247 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
248 if (!res) {
249 err = -ENOMEM;
250 goto parse_failed;
253 err = of_pci_range_to_resource(&range, dev, res);
254 if (err) {
255 kfree(res);
256 continue;
259 if (resource_type(res) == IORESOURCE_IO) {
260 if (!io_base) {
261 pr_err("I/O range found for %s. Please provide an io_base pointer to save CPU base address\n",
262 dev->full_name);
263 err = -EINVAL;
264 goto conversion_failed;
266 if (*io_base != (resource_size_t)OF_BAD_ADDR)
267 pr_warn("More than one I/O resource converted for %s. CPU base address for old range lost!\n",
268 dev->full_name);
269 *io_base = range.cpu_addr;
272 pci_add_resource_offset(resources, res, res->start - range.pci_addr);
275 return 0;
277 conversion_failed:
278 kfree(res);
279 parse_failed:
280 resource_list_for_each_entry(window, resources)
281 kfree(window->res);
282 pci_free_resource_list(resources);
283 return err;
285 EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources);
286 #endif /* CONFIG_OF_ADDRESS */
288 #ifdef CONFIG_PCI_MSI
290 static LIST_HEAD(of_pci_msi_chip_list);
291 static DEFINE_MUTEX(of_pci_msi_chip_mutex);
293 int of_pci_msi_chip_add(struct msi_controller *chip)
295 if (!of_property_read_bool(chip->of_node, "msi-controller"))
296 return -EINVAL;
298 mutex_lock(&of_pci_msi_chip_mutex);
299 list_add(&chip->list, &of_pci_msi_chip_list);
300 mutex_unlock(&of_pci_msi_chip_mutex);
302 return 0;
304 EXPORT_SYMBOL_GPL(of_pci_msi_chip_add);
306 void of_pci_msi_chip_remove(struct msi_controller *chip)
308 mutex_lock(&of_pci_msi_chip_mutex);
309 list_del(&chip->list);
310 mutex_unlock(&of_pci_msi_chip_mutex);
312 EXPORT_SYMBOL_GPL(of_pci_msi_chip_remove);
314 struct msi_controller *of_pci_find_msi_chip_by_node(struct device_node *of_node)
316 struct msi_controller *c;
318 mutex_lock(&of_pci_msi_chip_mutex);
319 list_for_each_entry(c, &of_pci_msi_chip_list, list) {
320 if (c->of_node == of_node) {
321 mutex_unlock(&of_pci_msi_chip_mutex);
322 return c;
325 mutex_unlock(&of_pci_msi_chip_mutex);
327 return NULL;
329 EXPORT_SYMBOL_GPL(of_pci_find_msi_chip_by_node);
331 #endif /* CONFIG_PCI_MSI */
334 * of_pci_map_rid - Translate a requester ID through a downstream mapping.
335 * @np: root complex device node.
336 * @rid: PCI requester ID to map.
337 * @map_name: property name of the map to use.
338 * @map_mask_name: optional property name of the mask to use.
339 * @target: optional pointer to a target device node.
340 * @id_out: optional pointer to receive the translated ID.
342 * Given a PCI requester ID, look up the appropriate implementation-defined
343 * platform ID and/or the target device which receives transactions on that
344 * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
345 * @id_out may be NULL if only the other is required. If @target points to
346 * a non-NULL device node pointer, only entries targeting that node will be
347 * matched; if it points to a NULL value, it will receive the device node of
348 * the first matching target phandle, with a reference held.
350 * Return: 0 on success or a standard error code on failure.
352 int of_pci_map_rid(struct device_node *np, u32 rid,
353 const char *map_name, const char *map_mask_name,
354 struct device_node **target, u32 *id_out)
356 u32 map_mask, masked_rid;
357 int map_len;
358 const __be32 *map = NULL;
360 if (!np || !map_name || (!target && !id_out))
361 return -EINVAL;
363 map = of_get_property(np, map_name, &map_len);
364 if (!map) {
365 if (target)
366 return -ENODEV;
367 /* Otherwise, no map implies no translation */
368 *id_out = rid;
369 return 0;
372 if (!map_len || map_len % (4 * sizeof(*map))) {
373 pr_err("%s: Error: Bad %s length: %d\n", np->full_name,
374 map_name, map_len);
375 return -EINVAL;
378 /* The default is to select all bits. */
379 map_mask = 0xffffffff;
382 * Can be overridden by "{iommu,msi}-map-mask" property.
383 * If of_property_read_u32() fails, the default is used.
385 if (map_mask_name)
386 of_property_read_u32(np, map_mask_name, &map_mask);
388 masked_rid = map_mask & rid;
389 for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
390 struct device_node *phandle_node;
391 u32 rid_base = be32_to_cpup(map + 0);
392 u32 phandle = be32_to_cpup(map + 1);
393 u32 out_base = be32_to_cpup(map + 2);
394 u32 rid_len = be32_to_cpup(map + 3);
396 if (rid_base & ~map_mask) {
397 pr_err("%s: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
398 np->full_name, map_name, map_name,
399 map_mask, rid_base);
400 return -EFAULT;
403 if (masked_rid < rid_base || masked_rid >= rid_base + rid_len)
404 continue;
406 phandle_node = of_find_node_by_phandle(phandle);
407 if (!phandle_node)
408 return -ENODEV;
410 if (target) {
411 if (*target)
412 of_node_put(phandle_node);
413 else
414 *target = phandle_node;
416 if (*target != phandle_node)
417 continue;
420 if (id_out)
421 *id_out = masked_rid - rid_base + out_base;
423 pr_debug("%s: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
424 np->full_name, map_name, map_mask, rid_base, out_base,
425 rid_len, rid, *id_out);
426 return 0;
429 pr_err("%s: Invalid %s translation - no match for rid 0x%x on %s\n",
430 np->full_name, map_name, rid,
431 target && *target ? (*target)->full_name : "any target");
432 return -EFAULT;