blob: 44e4d4435bed78032b1b7a0d9935e577ec196538 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
David S. Millera2bd4fd2006-06-23 01:44:10 -07002#include <linux/string.h>
3#include <linux/kernel.h>
Stephen Rothwellf85ff302007-05-01 16:40:36 +10004#include <linux/of.h>
David S. Millera2bd4fd2006-06-23 01:44:10 -07005#include <linux/init.h>
Paul Gortmaker066bcac2011-07-22 13:18:16 -04006#include <linux/export.h>
David S. Millera2bd4fd2006-06-23 01:44:10 -07007#include <linux/mod_devicetable.h>
8#include <linux/slab.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +10009#include <linux/errno.h>
David S. Millerc1b1a5f12008-03-19 04:52:48 -070010#include <linux/irq.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100011#include <linux/of_device.h>
12#include <linux/of_platform.h>
Paul Gortmakerc2068da2011-08-01 13:42:48 -040013#include <asm/spitfire.h>
David S. Millera2bd4fd2006-06-23 01:44:10 -070014
Robert Reifc9f5b7e2009-06-04 02:00:02 -070015#include "of_device_common.h"
16
David S. Miller3ca9fab2006-06-29 14:35:33 -070017void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
18{
19 unsigned long ret = res->start + offset;
David S. Miller6bda5732006-10-18 23:00:35 -070020 struct resource *r;
David S. Miller3ca9fab2006-06-29 14:35:33 -070021
David S. Miller6bda5732006-10-18 23:00:35 -070022 if (res->flags & IORESOURCE_MEM)
23 r = request_mem_region(ret, size, name);
24 else
25 r = request_region(ret, size, name);
26 if (!r)
David S. Miller3ca9fab2006-06-29 14:35:33 -070027 ret = 0;
28
29 return (void __iomem *) ret;
30}
31EXPORT_SYMBOL(of_ioremap);
32
David S. Millere3a411a32006-12-28 21:01:32 -080033void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
David S. Miller3ca9fab2006-06-29 14:35:33 -070034{
David S. Millere3a411a32006-12-28 21:01:32 -080035 if (res->flags & IORESOURCE_MEM)
36 release_mem_region((unsigned long) base, size);
37 else
38 release_region((unsigned long) base, size);
David S. Miller3ca9fab2006-06-29 14:35:33 -070039}
40EXPORT_SYMBOL(of_iounmap);
41
David S. Millercf44bbc2006-06-29 14:34:50 -070042/*
43 * PCI bus specific translator
44 */
45
46static int of_bus_pci_match(struct device_node *np)
47{
David S. Miller7ee766d2008-09-20 22:00:40 -070048 if (!strcmp(np->name, "pci")) {
David S. Millera165b422007-03-29 01:50:16 -070049 const char *model = of_get_property(np, "model", NULL);
David S. Miller01f94c42007-03-04 12:53:19 -080050
51 if (model && !strcmp(model, "SUNW,simba"))
52 return 0;
53
David S. Millera83f9822006-07-12 23:19:31 -070054 /* Do not do PCI specific frobbing if the
55 * PCI bridge lacks a ranges property. We
56 * want to pass it through up to the next
57 * parent as-is, not with the PCI translate
58 * method which chops off the top address cell.
59 */
60 if (!of_find_property(np, "ranges", NULL))
61 return 0;
62
63 return 1;
64 }
65
66 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -070067}
68
David S. Miller01f94c42007-03-04 12:53:19 -080069static int of_bus_simba_match(struct device_node *np)
70{
David S. Millera165b422007-03-29 01:50:16 -070071 const char *model = of_get_property(np, "model", NULL);
David S. Miller01f94c42007-03-04 12:53:19 -080072
73 if (model && !strcmp(model, "SUNW,simba"))
74 return 1;
David S. Miller8c2786c2007-06-07 21:59:44 -070075
76 /* Treat PCI busses lacking ranges property just like
77 * simba.
78 */
David S. Miller7ee766d2008-09-20 22:00:40 -070079 if (!strcmp(np->name, "pci")) {
David S. Miller8c2786c2007-06-07 21:59:44 -070080 if (!of_find_property(np, "ranges", NULL))
81 return 1;
82 }
83
David S. Miller01f94c42007-03-04 12:53:19 -080084 return 0;
85}
86
87static int of_bus_simba_map(u32 *addr, const u32 *range,
88 int na, int ns, int pna)
89{
90 return 0;
91}
92
David S. Millercf44bbc2006-06-29 14:34:50 -070093static void of_bus_pci_count_cells(struct device_node *np,
94 int *addrc, int *sizec)
95{
96 if (addrc)
97 *addrc = 3;
98 if (sizec)
99 *sizec = 2;
100}
101
David S. Millera83f9822006-07-12 23:19:31 -0700102static int of_bus_pci_map(u32 *addr, const u32 *range,
103 int na, int ns, int pna)
David S. Millercf44bbc2006-06-29 14:34:50 -0700104{
David S. Millera83f9822006-07-12 23:19:31 -0700105 u32 result[OF_MAX_ADDR_CELLS];
106 int i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700107
108 /* Check address type match */
David S. Miller4230fa32009-12-09 01:39:09 -0800109 if (!((addr[0] ^ range[0]) & 0x03000000))
110 goto type_match;
David S. Millercf44bbc2006-06-29 14:34:50 -0700111
David S. Miller4230fa32009-12-09 01:39:09 -0800112 /* Special exception, we can map a 64-bit address into
113 * a 32-bit range.
114 */
115 if ((addr[0] & 0x03000000) == 0x03000000 &&
116 (range[0] & 0x03000000) == 0x02000000)
117 goto type_match;
118
119 return -EINVAL;
120
121type_match:
David S. Millera83f9822006-07-12 23:19:31 -0700122 if (of_out_of_range(addr + 1, range + 1, range + na + pna,
123 na - 1, ns))
124 return -EINVAL;
David S. Millercf44bbc2006-06-29 14:34:50 -0700125
David S. Millera83f9822006-07-12 23:19:31 -0700126 /* Start with the parent range base. */
127 memcpy(result, range + na, pna * 4);
David S. Millercf44bbc2006-06-29 14:34:50 -0700128
David S. Millera83f9822006-07-12 23:19:31 -0700129 /* Add in the child address offset, skipping high cell. */
130 for (i = 0; i < na - 1; i++)
131 result[pna - 1 - i] +=
132 (addr[na - 1 - i] -
133 range[na - 1 - i]);
134
135 memcpy(addr, result, pna * 4);
136
137 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700138}
139
David S. Millere3c71a32008-08-28 21:02:58 -0700140static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
David S. Millercf44bbc2006-06-29 14:34:50 -0700141{
David S. Millercf44bbc2006-06-29 14:34:50 -0700142 u32 w = addr[0];
143
David S. Millere3c71a32008-08-28 21:02:58 -0700144 /* For PCI, we override whatever child busses may have used. */
145 flags = 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700146 switch((w >> 24) & 0x03) {
147 case 0x01:
148 flags |= IORESOURCE_IO;
David S. Millere3c71a32008-08-28 21:02:58 -0700149 break;
150
David S. Millercf44bbc2006-06-29 14:34:50 -0700151 case 0x02: /* 32 bits */
152 case 0x03: /* 64 bits */
153 flags |= IORESOURCE_MEM;
David S. Millere3c71a32008-08-28 21:02:58 -0700154 break;
David S. Millercf44bbc2006-06-29 14:34:50 -0700155 }
156 if (w & 0x40000000)
157 flags |= IORESOURCE_PREFETCH;
158 return flags;
159}
160
161/*
David S. Miller4130a4b2006-10-25 22:31:06 -0700162 * FHC/Central bus specific translator.
163 *
164 * This is just needed to hard-code the address and size cell
165 * counts. 'fhc' and 'central' nodes lack the #address-cells and
166 * #size-cells properties, and if you walk to the root on such
167 * Enterprise boxes all you'll get is a #size-cells of 2 which is
168 * not what we want to use.
169 */
170static int of_bus_fhc_match(struct device_node *np)
David S. Millercf44bbc2006-06-29 14:34:50 -0700171{
David S. Miller4130a4b2006-10-25 22:31:06 -0700172 return !strcmp(np->name, "fhc") ||
173 !strcmp(np->name, "central");
David S. Millercf44bbc2006-06-29 14:34:50 -0700174}
175
David S. Miller4130a4b2006-10-25 22:31:06 -0700176#define of_bus_fhc_count_cells of_bus_sbus_count_cells
David S. Millercf44bbc2006-06-29 14:34:50 -0700177
178/*
179 * Array of bus specific translators
180 */
181
182static struct of_bus of_busses[] = {
183 /* PCI */
184 {
185 .name = "pci",
186 .addr_prop_name = "assigned-addresses",
187 .match = of_bus_pci_match,
188 .count_cells = of_bus_pci_count_cells,
189 .map = of_bus_pci_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700190 .get_flags = of_bus_pci_get_flags,
191 },
David S. Miller01f94c42007-03-04 12:53:19 -0800192 /* SIMBA */
193 {
194 .name = "simba",
195 .addr_prop_name = "assigned-addresses",
196 .match = of_bus_simba_match,
197 .count_cells = of_bus_pci_count_cells,
198 .map = of_bus_simba_map,
199 .get_flags = of_bus_pci_get_flags,
200 },
David S. Millercf44bbc2006-06-29 14:34:50 -0700201 /* SBUS */
202 {
203 .name = "sbus",
204 .addr_prop_name = "reg",
205 .match = of_bus_sbus_match,
206 .count_cells = of_bus_sbus_count_cells,
David S. Miller4130a4b2006-10-25 22:31:06 -0700207 .map = of_bus_default_map,
208 .get_flags = of_bus_default_get_flags,
209 },
210 /* FHC */
211 {
212 .name = "fhc",
213 .addr_prop_name = "reg",
214 .match = of_bus_fhc_match,
215 .count_cells = of_bus_fhc_count_cells,
216 .map = of_bus_default_map,
217 .get_flags = of_bus_default_get_flags,
David S. Millercf44bbc2006-06-29 14:34:50 -0700218 },
219 /* Default */
220 {
221 .name = "default",
222 .addr_prop_name = "reg",
223 .match = NULL,
224 .count_cells = of_bus_default_count_cells,
225 .map = of_bus_default_map,
David S. Millercf44bbc2006-06-29 14:34:50 -0700226 .get_flags = of_bus_default_get_flags,
227 },
228};
229
230static struct of_bus *of_match_bus(struct device_node *np)
231{
232 int i;
233
234 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
235 if (!of_busses[i].match || of_busses[i].match(np))
236 return &of_busses[i];
237 BUG();
238 return NULL;
239}
240
241static int __init build_one_resource(struct device_node *parent,
242 struct of_bus *bus,
243 struct of_bus *pbus,
244 u32 *addr,
245 int na, int ns, int pna)
246{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700247 const u32 *ranges;
David S. Miller21cd8832008-09-11 23:53:41 -0700248 int rone, rlen;
David S. Millercf44bbc2006-06-29 14:34:50 -0700249
250 ranges = of_get_property(parent, "ranges", &rlen);
251 if (ranges == NULL || rlen == 0) {
David S. Millera83f9822006-07-12 23:19:31 -0700252 u32 result[OF_MAX_ADDR_CELLS];
253 int i;
254
255 memset(result, 0, pna * 4);
256 for (i = 0; i < na; i++)
257 result[pna - 1 - i] =
258 addr[na - 1 - i];
259
260 memcpy(addr, result, pna * 4);
261 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700262 }
263
264 /* Now walk through the ranges */
265 rlen /= 4;
266 rone = na + pna + ns;
267 for (; rlen >= rone; rlen -= rone, ranges += rone) {
David S. Millera83f9822006-07-12 23:19:31 -0700268 if (!bus->map(addr, ranges, na, ns, pna))
269 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700270 }
David S. Millera83f9822006-07-12 23:19:31 -0700271
David S. Miller49d23cf2007-05-13 22:01:18 -0700272 /* When we miss an I/O space match on PCI, just pass it up
273 * to the next PCI bridge and/or controller.
274 */
275 if (!strcmp(bus->name, "pci") &&
276 (addr[0] & 0x03000000) == 0x01000000)
277 return 0;
278
David S. Millera83f9822006-07-12 23:19:31 -0700279 return 1;
280}
281
282static int __init use_1to1_mapping(struct device_node *pp)
283{
David S. Millera83f9822006-07-12 23:19:31 -0700284 /* If we have a ranges property in the parent, use it. */
285 if (of_find_property(pp, "ranges", NULL) != NULL)
286 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700287
David S. Millera83f9822006-07-12 23:19:31 -0700288 /* If the parent is the dma node of an ISA bus, pass
289 * the translation up to the root.
David S. Miller52802672008-09-03 02:04:41 -0700290 *
291 * Some SBUS devices use intermediate nodes to express
292 * hierarchy within the device itself. These aren't
293 * real bus nodes, and don't have a 'ranges' property.
294 * But, we should still pass the translation work up
295 * to the SBUS itself.
David S. Millera83f9822006-07-12 23:19:31 -0700296 */
David S. Miller52802672008-09-03 02:04:41 -0700297 if (!strcmp(pp->name, "dma") ||
298 !strcmp(pp->name, "espdma") ||
299 !strcmp(pp->name, "ledma") ||
300 !strcmp(pp->name, "lebuffer"))
David S. Millera83f9822006-07-12 23:19:31 -0700301 return 0;
302
David S. Miller8c2786c2007-06-07 21:59:44 -0700303 /* Similarly for all PCI bridges, if we get this far
304 * it lacks a ranges property, and this will include
305 * cases like Simba.
306 */
David S. Miller7ee766d2008-09-20 22:00:40 -0700307 if (!strcmp(pp->name, "pci"))
David S. Millera83f9822006-07-12 23:19:31 -0700308 return 0;
309
310 return 1;
David S. Millercf44bbc2006-06-29 14:34:50 -0700311}
312
David S. Millera83f9822006-07-12 23:19:31 -0700313static int of_resource_verbose;
314
Grant Likelycd4cd732010-07-22 16:04:30 -0600315static void __init build_device_resources(struct platform_device *op,
David S. Millercf44bbc2006-06-29 14:34:50 -0700316 struct device *parent)
317{
Grant Likelycd4cd732010-07-22 16:04:30 -0600318 struct platform_device *p_op;
David S. Millercf44bbc2006-06-29 14:34:50 -0700319 struct of_bus *bus;
320 int na, ns;
321 int index, num_reg;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700322 const void *preg;
David S. Millercf44bbc2006-06-29 14:34:50 -0700323
324 if (!parent)
325 return;
326
Grant Likelycd4cd732010-07-22 16:04:30 -0600327 p_op = to_platform_device(parent);
Grant Likely61c7a082010-04-13 16:12:29 -0700328 bus = of_match_bus(p_op->dev.of_node);
329 bus->count_cells(op->dev.of_node, &na, &ns);
David S. Millercf44bbc2006-06-29 14:34:50 -0700330
Grant Likely61c7a082010-04-13 16:12:29 -0700331 preg = of_get_property(op->dev.of_node, bus->addr_prop_name, &num_reg);
David S. Millercf44bbc2006-06-29 14:34:50 -0700332 if (!preg || num_reg == 0)
333 return;
334
335 /* Convert to num-cells. */
336 num_reg /= 4;
337
David S. Miller46ba6d72006-07-16 22:10:44 -0700338 /* Convert to num-entries. */
David S. Millercf44bbc2006-06-29 14:34:50 -0700339 num_reg /= na + ns;
340
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700341 /* Prevent overrunning the op->resources[] array. */
David S. Miller46ba6d72006-07-16 22:10:44 -0700342 if (num_reg > PROMREG_MAX) {
343 printk(KERN_WARNING "%s: Too many regs (%d), "
344 "limiting to %d.\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700345 op->dev.of_node->full_name, num_reg, PROMREG_MAX);
David S. Miller46ba6d72006-07-16 22:10:44 -0700346 num_reg = PROMREG_MAX;
347 }
348
Grant Likely1636f8a2010-06-18 11:09:58 -0600349 op->resource = op->archdata.resource;
350 op->num_resources = num_reg;
David S. Millercf44bbc2006-06-29 14:34:50 -0700351 for (index = 0; index < num_reg; index++) {
352 struct resource *r = &op->resource[index];
353 u32 addr[OF_MAX_ADDR_CELLS];
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700354 const u32 *reg = (preg + (index * ((na + ns) * 4)));
Grant Likely61c7a082010-04-13 16:12:29 -0700355 struct device_node *dp = op->dev.of_node;
356 struct device_node *pp = p_op->dev.of_node;
David S. Millerb85cdd42007-02-28 23:20:12 -0800357 struct of_bus *pbus, *dbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700358 u64 size, result = OF_BAD_ADDR;
359 unsigned long flags;
360 int dna, dns;
361 int pna, pns;
362
363 size = of_read_addr(reg + na, ns);
David S. Millercf44bbc2006-06-29 14:34:50 -0700364 memcpy(addr, reg, na * 4);
365
David S. Millere3c71a32008-08-28 21:02:58 -0700366 flags = bus->get_flags(addr, 0);
367
David S. Millera83f9822006-07-12 23:19:31 -0700368 if (use_1to1_mapping(pp)) {
David S. Millercf44bbc2006-06-29 14:34:50 -0700369 result = of_read_addr(addr, na);
370 goto build_res;
371 }
372
373 dna = na;
374 dns = ns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800375 dbus = bus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700376
377 while (1) {
378 dp = pp;
379 pp = dp->parent;
380 if (!pp) {
381 result = of_read_addr(addr, dna);
382 break;
383 }
384
385 pbus = of_match_bus(pp);
386 pbus->count_cells(dp, &pna, &pns);
387
David S. Millerb85cdd42007-02-28 23:20:12 -0800388 if (build_one_resource(dp, dbus, pbus, addr,
David S. Millera83f9822006-07-12 23:19:31 -0700389 dna, dns, pna))
David S. Millercf44bbc2006-06-29 14:34:50 -0700390 break;
391
David S. Millere3c71a32008-08-28 21:02:58 -0700392 flags = pbus->get_flags(addr, flags);
393
David S. Millercf44bbc2006-06-29 14:34:50 -0700394 dna = pna;
395 dns = pns;
David S. Millerb85cdd42007-02-28 23:20:12 -0800396 dbus = pbus;
David S. Millercf44bbc2006-06-29 14:34:50 -0700397 }
398
399 build_res:
400 memset(r, 0, sizeof(*r));
David S. Millera83f9822006-07-12 23:19:31 -0700401
402 if (of_resource_verbose)
Sam Ravnborg90181132009-01-06 13:19:28 -0800403 printk("%s reg[%d] -> %llx\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700404 op->dev.of_node->full_name, index,
David S. Millera83f9822006-07-12 23:19:31 -0700405 result);
406
David S. Millercf44bbc2006-06-29 14:34:50 -0700407 if (result != OF_BAD_ADDR) {
David S. Miller1815aed2006-06-29 19:58:28 -0700408 if (tlb_type == hypervisor)
409 result &= 0x0fffffffffffffffUL;
410
David S. Millercf44bbc2006-06-29 14:34:50 -0700411 r->start = result;
412 r->end = result + size - 1;
413 r->flags = flags;
David S. Millercf44bbc2006-06-29 14:34:50 -0700414 }
Grant Likely61c7a082010-04-13 16:12:29 -0700415 r->name = op->dev.of_node->name;
David S. Millercf44bbc2006-06-29 14:34:50 -0700416 }
417}
418
David S. Miller2b1e5972006-06-29 15:07:37 -0700419static struct device_node * __init
420apply_interrupt_map(struct device_node *dp, struct device_node *pp,
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700421 const u32 *imap, int imlen, const u32 *imask,
David S. Miller2b1e5972006-06-29 15:07:37 -0700422 unsigned int *irq_p)
423{
424 struct device_node *cp;
425 unsigned int irq = *irq_p;
426 struct of_bus *bus;
427 phandle handle;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700428 const u32 *reg;
David S. Miller2b1e5972006-06-29 15:07:37 -0700429 int na, num_reg, i;
430
431 bus = of_match_bus(pp);
432 bus->count_cells(dp, &na, NULL);
433
434 reg = of_get_property(dp, "reg", &num_reg);
435 if (!reg || !num_reg)
436 return NULL;
437
438 imlen /= ((na + 3) * 4);
439 handle = 0;
440 for (i = 0; i < imlen; i++) {
441 int j;
442
443 for (j = 0; j < na; j++) {
444 if ((reg[j] & imask[j]) != imap[j])
445 goto next;
446 }
447 if (imap[na] == irq) {
448 handle = imap[na + 1];
449 irq = imap[na + 2];
450 break;
451 }
452
453 next:
454 imap += (na + 3);
455 }
David S. Miller46ba6d72006-07-16 22:10:44 -0700456 if (i == imlen) {
457 /* Psycho and Sabre PCI controllers can have 'interrupt-map'
458 * properties that do not include the on-board device
459 * interrupts. Instead, the device's 'interrupts' property
460 * is already a fully specified INO value.
461 *
462 * Handle this by deciding that, if we didn't get a
463 * match in the parent's 'interrupt-map', and the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300464 * parent is an IRQ translator, then use the parent as
David S. Miller46ba6d72006-07-16 22:10:44 -0700465 * our IRQ controller.
466 */
467 if (pp->irq_trans)
468 return pp;
469
David S. Miller2b1e5972006-06-29 15:07:37 -0700470 return NULL;
David S. Miller46ba6d72006-07-16 22:10:44 -0700471 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700472
473 *irq_p = irq;
474 cp = of_find_node_by_phandle(handle);
475
476 return cp;
477}
478
479static unsigned int __init pci_irq_swizzle(struct device_node *dp,
480 struct device_node *pp,
481 unsigned int irq)
482{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700483 const struct linux_prom_pci_registers *regs;
David S. Millerbb4c18c2007-02-26 14:55:06 -0800484 unsigned int bus, devfn, slot, ret;
David S. Miller2b1e5972006-06-29 15:07:37 -0700485
486 if (irq < 1 || irq > 4)
487 return irq;
488
489 regs = of_get_property(dp, "reg", NULL);
490 if (!regs)
491 return irq;
492
David S. Millerbb4c18c2007-02-26 14:55:06 -0800493 bus = (regs->phys_hi >> 16) & 0xff;
David S. Miller2b1e5972006-06-29 15:07:37 -0700494 devfn = (regs->phys_hi >> 8) & 0xff;
495 slot = (devfn >> 3) & 0x1f;
496
David S. Millerbb4c18c2007-02-26 14:55:06 -0800497 if (pp->irq_trans) {
498 /* Derived from Table 8-3, U2P User's Manual. This branch
499 * is handling a PCI controller that lacks a proper set of
500 * interrupt-map and interrupt-map-mask properties. The
501 * Ultra-E450 is one example.
502 *
503 * The bit layout is BSSLL, where:
504 * B: 0 on bus A, 1 on bus B
505 * D: 2-bit slot number, derived from PCI device number as
506 * (dev - 1) for bus A, or (dev - 2) for bus B
507 * L: 2-bit line number
David S. Millerbb4c18c2007-02-26 14:55:06 -0800508 */
509 if (bus & 0x80) {
510 /* PBM-A */
511 bus = 0x00;
512 slot = (slot - 1) << 2;
513 } else {
514 /* PBM-B */
515 bus = 0x10;
516 slot = (slot - 2) << 2;
517 }
518 irq -= 1;
519
520 ret = (bus | slot | irq);
521 } else {
522 /* Going through a PCI-PCI bridge that lacks a set of
523 * interrupt-map and interrupt-map-mask properties.
524 */
525 ret = ((irq - 1 + (slot & 3)) & 3) + 1;
526 }
David S. Miller2b1e5972006-06-29 15:07:37 -0700527
528 return ret;
529}
530
David S. Millera83f9822006-07-12 23:19:31 -0700531static int of_irq_verbose;
532
Grant Likelycd4cd732010-07-22 16:04:30 -0600533static unsigned int __init build_one_device_irq(struct platform_device *op,
David S. Miller2b1e5972006-06-29 15:07:37 -0700534 struct device *parent,
535 unsigned int irq)
536{
Grant Likely61c7a082010-04-13 16:12:29 -0700537 struct device_node *dp = op->dev.of_node;
David S. Miller2b1e5972006-06-29 15:07:37 -0700538 struct device_node *pp, *ip;
539 unsigned int orig_irq = irq;
David S. Millerc1b1a5f12008-03-19 04:52:48 -0700540 int nid;
David S. Miller2b1e5972006-06-29 15:07:37 -0700541
542 if (irq == 0xffffffff)
543 return irq;
544
545 if (dp->irq_trans) {
546 irq = dp->irq_trans->irq_build(dp, irq,
547 dp->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700548
549 if (of_irq_verbose)
550 printk("%s: direct translate %x --> %x\n",
551 dp->full_name, orig_irq, irq);
552
David S. Millerc1b1a5f12008-03-19 04:52:48 -0700553 goto out;
David S. Miller2b1e5972006-06-29 15:07:37 -0700554 }
555
556 /* Something more complicated. Walk up to the root, applying
557 * interrupt-map or bus specific translations, until we hit
558 * an IRQ translator.
559 *
560 * If we hit a bus type or situation we cannot handle, we
561 * stop and assume that the original IRQ number was in a
562 * format which has special meaning to it's immediate parent.
563 */
564 pp = dp->parent;
565 ip = NULL;
566 while (pp) {
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700567 const void *imap, *imsk;
David S. Miller2b1e5972006-06-29 15:07:37 -0700568 int imlen;
569
570 imap = of_get_property(pp, "interrupt-map", &imlen);
571 imsk = of_get_property(pp, "interrupt-map-mask", NULL);
572 if (imap && imsk) {
573 struct device_node *iret;
574 int this_orig_irq = irq;
575
576 iret = apply_interrupt_map(dp, pp,
577 imap, imlen, imsk,
578 &irq);
David S. Millera83f9822006-07-12 23:19:31 -0700579
580 if (of_irq_verbose)
581 printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700582 op->dev.of_node->full_name,
David S. Millera83f9822006-07-12 23:19:31 -0700583 pp->full_name, this_orig_irq,
Grant Likely74a7f082012-06-15 11:50:25 -0600584 of_node_full_name(iret), irq);
David S. Millera83f9822006-07-12 23:19:31 -0700585
David S. Miller2b1e5972006-06-29 15:07:37 -0700586 if (!iret)
587 break;
588
589 if (iret->irq_trans) {
590 ip = iret;
591 break;
592 }
593 } else {
David S. Miller7ee766d2008-09-20 22:00:40 -0700594 if (!strcmp(pp->name, "pci")) {
David S. Miller2b1e5972006-06-29 15:07:37 -0700595 unsigned int this_orig_irq = irq;
596
597 irq = pci_irq_swizzle(dp, pp, irq);
David S. Millera83f9822006-07-12 23:19:31 -0700598 if (of_irq_verbose)
599 printk("%s: PCI swizzle [%s] "
600 "%x --> %x\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700601 op->dev.of_node->full_name,
David S. Millera83f9822006-07-12 23:19:31 -0700602 pp->full_name, this_orig_irq,
603 irq);
604
David S. Miller2b1e5972006-06-29 15:07:37 -0700605 }
606
607 if (pp->irq_trans) {
608 ip = pp;
609 break;
610 }
611 }
612 dp = pp;
613 pp = pp->parent;
614 }
615 if (!ip)
616 return orig_irq;
617
Grant Likely61c7a082010-04-13 16:12:29 -0700618 irq = ip->irq_trans->irq_build(op->dev.of_node, irq,
David S. Miller2b1e5972006-06-29 15:07:37 -0700619 ip->irq_trans->data);
David S. Millera83f9822006-07-12 23:19:31 -0700620 if (of_irq_verbose)
621 printk("%s: Apply IRQ trans [%s] %x --> %x\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700622 op->dev.of_node->full_name, ip->full_name, orig_irq, irq);
David S. Miller2b1e5972006-06-29 15:07:37 -0700623
David S. Millerc1b1a5f12008-03-19 04:52:48 -0700624out:
625 nid = of_node_to_nid(dp);
626 if (nid != -1) {
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700627 cpumask_t numa_mask;
David S. Millerc1b1a5f12008-03-19 04:52:48 -0700628
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700629 cpumask_copy(&numa_mask, cpumask_of_node(nid));
Rusty Russell0de26522008-12-13 21:20:26 +1030630 irq_set_affinity(irq, &numa_mask);
David S. Millerc1b1a5f12008-03-19 04:52:48 -0700631 }
632
David S. Miller2b1e5972006-06-29 15:07:37 -0700633 return irq;
634}
635
Grant Likelycd4cd732010-07-22 16:04:30 -0600636static struct platform_device * __init scan_one_device(struct device_node *dp,
David S. Millercf44bbc2006-06-29 14:34:50 -0700637 struct device *parent)
638{
Grant Likelycd4cd732010-07-22 16:04:30 -0600639 struct platform_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700640 const unsigned int *irq;
David S. Miller3d6e4702007-07-18 22:03:25 -0700641 struct dev_archdata *sd;
David S. Miller2b1e5972006-06-29 15:07:37 -0700642 int len, i;
David S. Millercf44bbc2006-06-29 14:34:50 -0700643
644 if (!op)
645 return NULL;
646
David S. Miller3d6e4702007-07-18 22:03:25 -0700647 sd = &op->dev.archdata;
David S. Miller3d6e4702007-07-18 22:03:25 -0700648 sd->op = op;
649
Grant Likelyd706c1b2010-04-13 16:12:28 -0700650 op->dev.of_node = dp;
David S. Millercf44bbc2006-06-29 14:34:50 -0700651
David S. Millercf44bbc2006-06-29 14:34:50 -0700652 irq = of_get_property(dp, "interrupts", &len);
David S. Miller2b1e5972006-06-29 15:07:37 -0700653 if (irq) {
Grant Likely1636f8a2010-06-18 11:09:58 -0600654 op->archdata.num_irqs = len / 4;
Robert Reif92d90912008-12-26 15:39:11 -0800655
656 /* Prevent overrunning the op->irqs[] array. */
Grant Likely1636f8a2010-06-18 11:09:58 -0600657 if (op->archdata.num_irqs > PROMINTR_MAX) {
Robert Reif92d90912008-12-26 15:39:11 -0800658 printk(KERN_WARNING "%s: Too many irqs (%d), "
659 "limiting to %d.\n",
Grant Likely1636f8a2010-06-18 11:09:58 -0600660 dp->full_name, op->archdata.num_irqs, PROMINTR_MAX);
661 op->archdata.num_irqs = PROMINTR_MAX;
Robert Reif92d90912008-12-26 15:39:11 -0800662 }
Grant Likely1636f8a2010-06-18 11:09:58 -0600663 memcpy(op->archdata.irqs, irq, op->archdata.num_irqs * 4);
David S. Miller2b1e5972006-06-29 15:07:37 -0700664 } else {
Grant Likely1636f8a2010-06-18 11:09:58 -0600665 op->archdata.num_irqs = 0;
David S. Miller2b1e5972006-06-29 15:07:37 -0700666 }
David S. Millercf44bbc2006-06-29 14:34:50 -0700667
668 build_device_resources(op, parent);
Grant Likely1636f8a2010-06-18 11:09:58 -0600669 for (i = 0; i < op->archdata.num_irqs; i++)
670 op->archdata.irqs[i] = build_one_device_irq(op, parent, op->archdata.irqs[i]);
David S. Millercf44bbc2006-06-29 14:34:50 -0700671
672 op->dev.parent = parent;
Grant Likelyeca39302010-06-08 07:48:21 -0600673 op->dev.bus = &platform_bus_type;
David S. Millercf44bbc2006-06-29 14:34:50 -0700674 if (!parent)
Greg Kroah-Hartman2222c312008-05-02 06:02:41 +0200675 dev_set_name(&op->dev, "root");
David S. Millercf44bbc2006-06-29 14:34:50 -0700676 else
Grant Likely6016a362010-01-28 14:06:53 -0700677 dev_set_name(&op->dev, "%08x", dp->phandle);
David S. Millercf44bbc2006-06-29 14:34:50 -0700678
679 if (of_device_register(op)) {
680 printk("%s: Could not register of device.\n",
681 dp->full_name);
682 kfree(op);
683 op = NULL;
684 }
685
686 return op;
687}
688
689static void __init scan_tree(struct device_node *dp, struct device *parent)
690{
691 while (dp) {
Grant Likelycd4cd732010-07-22 16:04:30 -0600692 struct platform_device *op = scan_one_device(dp, parent);
David S. Millercf44bbc2006-06-29 14:34:50 -0700693
694 if (op)
695 scan_tree(dp->child, &op->dev);
696
697 dp = dp->sibling;
698 }
699}
700
Grant Likelyeca39302010-06-08 07:48:21 -0600701static int __init scan_of_devices(void)
David S. Millercf44bbc2006-06-29 14:34:50 -0700702{
703 struct device_node *root = of_find_node_by_path("/");
Grant Likelycd4cd732010-07-22 16:04:30 -0600704 struct platform_device *parent;
David S. Millercf44bbc2006-06-29 14:34:50 -0700705
706 parent = scan_one_device(root, NULL);
707 if (!parent)
Grant Likelyeca39302010-06-08 07:48:21 -0600708 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700709
710 scan_tree(root->child, &parent->dev);
Grant Likelyeca39302010-06-08 07:48:21 -0600711 return 0;
David S. Millercf44bbc2006-06-29 14:34:50 -0700712}
Grant Likelyeca39302010-06-08 07:48:21 -0600713postcore_initcall(scan_of_devices);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700714
David S. Millera83f9822006-07-12 23:19:31 -0700715static int __init of_debug(char *str)
716{
717 int val = 0;
718
719 get_option(&str, &val);
720 if (val & 1)
721 of_resource_verbose = 1;
722 if (val & 2)
723 of_irq_verbose = 1;
724 return 1;
725}
726
727__setup("of_debug=", of_debug);