3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/pci_regs.h>
6 #include <linux/module.h>
10 #define DBG(fmt...) do { printk(fmt); } while(0)
12 #define DBG(fmt...) do { } while(0)
21 /* Max address size we deal with */
22 #define OF_MAX_ADDR_CELLS 4
23 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
28 static void of_dump_addr(const char *s, u32 *addr, int na)
32 printk(" %08x", *(addr++));
36 static void of_dump_addr(const char *s, u32 *addr, int na) { }
39 /* Read a big address */
40 static inline u64 of_read_addr(u32 *cell, int size)
44 r = (r << 32) | *(cell++);
48 /* Callbacks for bus specific translators */
51 const char *addresses;
52 int (*match)(struct device_node *parent);
53 void (*count_cells)(struct device_node *child,
54 int *addrc, int *sizec);
55 u64 (*map)(u32 *addr, u32 *range, int na, int ns, int pna);
56 int (*translate)(u32 *addr, u64 offset, int na);
61 * Default translator (generic bus)
64 static void of_default_count_cells(struct device_node *dev,
65 int *addrc, int *sizec)
68 *addrc = prom_n_addr_cells(dev);
70 *sizec = prom_n_size_cells(dev);
73 static u64 of_default_map(u32 *addr, u32 *range, int na, int ns, int pna)
77 cp = of_read_addr(range, na);
78 s = of_read_addr(range + na + pna, ns);
79 da = of_read_addr(addr, na);
81 DBG("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
84 if (da < cp || da >= (cp + s))
89 static int of_default_translate(u32 *addr, u64 offset, int na)
91 u64 a = of_read_addr(addr, na);
92 memset(addr, 0, na * 4);
95 addr[na - 2] = a >> 32;
96 addr[na - 1] = a & 0xffffffffu;
103 * PCI bus specific translator
106 static int of_bus_pci_match(struct device_node *np)
108 return !strcmp(np->type, "pci");
111 static void of_bus_pci_count_cells(struct device_node *np,
112 int *addrc, int *sizec)
120 static u64 of_bus_pci_map(u32 *addr, u32 *range, int na, int ns, int pna)
124 /* Check address type match */
125 if ((addr[0] ^ range[0]) & 0x03000000)
128 /* Read address values, skipping high cell */
129 cp = of_read_addr(range + 1, na - 1);
130 s = of_read_addr(range + na + pna, ns);
131 da = of_read_addr(addr + 1, na - 1);
133 DBG("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
135 if (da < cp || da >= (cp + s))
140 static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
142 return of_default_translate(addr + 1, offset, na - 1);
146 * ISA bus specific translator
149 static int of_bus_isa_match(struct device_node *np)
151 return !strcmp(np->name, "isa");
154 static void of_bus_isa_count_cells(struct device_node *child,
155 int *addrc, int *sizec)
163 static u64 of_bus_isa_map(u32 *addr, u32 *range, int na, int ns, int pna)
167 /* Check address type match */
168 if ((addr[0] ^ range[0]) & 0x00000001)
171 /* Read address values, skipping high cell */
172 cp = of_read_addr(range + 1, na - 1);
173 s = of_read_addr(range + na + pna, ns);
174 da = of_read_addr(addr + 1, na - 1);
176 DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
178 if (da < cp || da >= (cp + s))
183 static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
185 return of_default_translate(addr + 1, offset, na - 1);
189 * Array of bus specific translators
192 static struct of_bus of_busses[] = {
196 .addresses = "assigned-addresses",
197 .match = of_bus_pci_match,
198 .count_cells = of_bus_pci_count_cells,
199 .map = of_bus_pci_map,
200 .translate = of_bus_pci_translate,
206 .match = of_bus_isa_match,
207 .count_cells = of_bus_isa_count_cells,
208 .map = of_bus_isa_map,
209 .translate = of_bus_isa_translate,
216 .count_cells = of_default_count_cells,
217 .map = of_default_map,
218 .translate = of_default_translate,
222 static struct of_bus *of_match_bus(struct device_node *np)
226 for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
227 if (!of_busses[i].match || of_busses[i].match(np))
228 return &of_busses[i];
233 static int of_translate_one(struct device_node *parent, struct of_bus *bus,
234 struct of_bus *pbus, u32 *addr,
235 int na, int ns, int pna)
240 u64 offset = OF_BAD_ADDR;
242 /* Normally, an absence of a "ranges" property means we are
243 * crossing a non-translatable boundary, and thus the addresses
244 * below the current not cannot be converted to CPU physical ones.
245 * Unfortunately, while this is very clear in the spec, it's not
246 * what Apple understood, and they do have things like /uni-n or
247 * /ht nodes with no "ranges" property and a lot of perfectly
248 * useable mapped devices below them. Thus we treat the absence of
249 * "ranges" as equivalent to an empty "ranges" property which means
250 * a 1:1 translation at that level. It's up to the caller not to try
251 * to translate addresses that aren't supposed to be translated in
252 * the first place. --BenH.
254 ranges = (u32 *)get_property(parent, "ranges", &rlen);
255 if (ranges == NULL || rlen == 0) {
256 offset = of_read_addr(addr, na);
257 memset(addr, 0, pna);
261 DBG("OF: walking ranges...\n");
263 /* Now walk through the ranges */
265 rone = na + pna + ns;
266 for (; rlen >= rone; rlen -= rone, ranges += rone) {
267 offset = bus->map(addr, ranges, na, ns, pna);
268 if (offset != OF_BAD_ADDR)
271 if (offset == OF_BAD_ADDR) {
272 DBG("OF: not found !\n");
275 memcpy(addr, ranges + na, 4 * pna);
278 of_dump_addr("OF: parent translation for:", addr, pna);
279 DBG("OF: with offset: "PRu64"\n", offset);
281 /* Translate it into parent bus space */
282 return pbus->translate(addr, offset, pna);
287 * Translate an address from the device-tree into a CPU physical address,
288 * this walks up the tree and applies the various bus mappings on the
291 * Note: We consider that crossing any level with #size-cells == 0 to mean
292 * that translation is impossible (that is we are not dealing with a value
293 * that can be mapped to a cpu physical address). This is not really specified
294 * that way, but this is traditionally the way IBM at least do things
296 u64 of_translate_address(struct device_node *dev, u32 *in_addr)
298 struct device_node *parent = NULL;
299 struct of_bus *bus, *pbus;
300 u32 addr[OF_MAX_ADDR_CELLS];
301 int na, ns, pna, pns;
302 u64 result = OF_BAD_ADDR;
304 DBG("OF: ** translation for device %s **\n", dev->full_name);
306 /* Increase refcount at current level */
309 /* Get parent & match bus type */
310 parent = of_get_parent(dev);
313 bus = of_match_bus(parent);
315 /* Cound address cells & copy address locally */
316 bus->count_cells(dev, &na, &ns);
317 if (!OF_CHECK_COUNTS(na, ns)) {
318 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
322 memcpy(addr, in_addr, na * 4);
324 DBG("OF: bus is %s (na=%d, ns=%d) on %s\n",
325 bus->name, na, ns, parent->full_name);
326 of_dump_addr("OF: translating address:", addr, na);
330 /* Switch to parent bus */
333 parent = of_get_parent(dev);
335 /* If root, we have finished */
336 if (parent == NULL) {
337 DBG("OF: reached root node\n");
338 result = of_read_addr(addr, na);
342 /* Get new parent bus and counts */
343 pbus = of_match_bus(parent);
344 pbus->count_cells(dev, &pna, &pns);
345 if (!OF_CHECK_COUNTS(pna, pns)) {
346 printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
351 DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
352 pbus->name, pna, pns, parent->full_name);
354 /* Apply bus translation */
355 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna))
358 /* Complete the move up one level */
363 of_dump_addr("OF: one level translation:", addr, na);
371 EXPORT_SYMBOL(of_translate_address);
373 u32 *of_get_address(struct device_node *dev, int index, u64 *size)
377 struct device_node *parent;
379 int onesize, i, na, ns;
381 /* Get parent & match bus type */
382 parent = of_get_parent(dev);
385 bus = of_match_bus(parent);
386 bus->count_cells(dev, &na, &ns);
388 if (!OF_CHECK_COUNTS(na, ns))
391 /* Get "reg" or "assigned-addresses" property */
392 prop = (u32 *)get_property(dev, bus->addresses, &psize);
398 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
401 *size = of_read_addr(prop + na, ns);
406 EXPORT_SYMBOL(of_get_address);
408 u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size)
413 for (index = 0; (addr = of_get_address(dev, index, size)) != NULL;
415 if ((addr[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))
420 EXPORT_SYMBOL(of_get_pci_address);