David Gibson | 27fbaa9 | 2007-03-22 17:02:21 +1100 | [diff] [blame] | 1 | /* |
| 2 | * devtree.c - convenience functions for device tree manipulation |
| 3 | * Copyright 2007 David Gibson, IBM Corporation. |
| 4 | * Copyright (c) 2007 Freescale Semiconductor, Inc. |
| 5 | * |
| 6 | * Authors: David Gibson <david@gibson.dropbear.id.au> |
| 7 | * Scott Wood <scottwood@freescale.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation; either version |
| 12 | * 2 of the License, or (at your option) any later version. |
| 13 | */ |
| 14 | #include <stdarg.h> |
| 15 | #include <stddef.h> |
| 16 | #include "types.h" |
| 17 | #include "string.h" |
| 18 | #include "stdio.h" |
| 19 | #include "ops.h" |
| 20 | |
| 21 | void dt_fixup_memory(u64 start, u64 size) |
| 22 | { |
| 23 | void *root, *memory; |
| 24 | int naddr, nsize, i; |
| 25 | u32 memreg[4]; |
| 26 | |
| 27 | root = finddevice("/"); |
| 28 | if (getprop(root, "#address-cells", &naddr, sizeof(naddr)) < 0) |
| 29 | naddr = 2; |
| 30 | if (naddr < 1 || naddr > 2) |
| 31 | fatal("Can't cope with #address-cells == %d in /\n\r", naddr); |
| 32 | |
| 33 | if (getprop(root, "#size-cells", &nsize, sizeof(nsize)) < 0) |
| 34 | nsize = 1; |
| 35 | if (nsize < 1 || nsize > 2) |
| 36 | fatal("Can't cope with #size-cells == %d in /\n\r", nsize); |
| 37 | |
| 38 | i = 0; |
| 39 | if (naddr == 2) |
| 40 | memreg[i++] = start >> 32; |
| 41 | memreg[i++] = start & 0xffffffff; |
| 42 | if (nsize == 2) |
| 43 | memreg[i++] = size >> 32; |
| 44 | memreg[i++] = size & 0xffffffff; |
| 45 | |
| 46 | memory = finddevice("/memory"); |
| 47 | if (! memory) { |
| 48 | memory = create_node(NULL, "memory"); |
| 49 | setprop_str(memory, "device_type", "memory"); |
| 50 | } |
| 51 | |
| 52 | printf("Memory <- <0x%x", memreg[0]); |
| 53 | for (i = 1; i < (naddr + nsize); i++) |
| 54 | printf(" 0x%x", memreg[i]); |
| 55 | printf("> (%ldMB)\n\r", (unsigned long)(size >> 20)); |
| 56 | |
| 57 | setprop(memory, "reg", memreg, (naddr + nsize)*sizeof(u32)); |
| 58 | } |
| 59 | |
| 60 | #define MHZ(x) ((x + 500000) / 1000000) |
| 61 | |
| 62 | void dt_fixup_cpu_clocks(u32 cpu, u32 tb, u32 bus) |
| 63 | { |
| 64 | void *devp = NULL; |
| 65 | |
| 66 | printf("CPU clock-frequency <- 0x%x (%dMHz)\n\r", cpu, MHZ(cpu)); |
| 67 | printf("CPU timebase-frequency <- 0x%x (%dMHz)\n\r", tb, MHZ(tb)); |
| 68 | if (bus > 0) |
| 69 | printf("CPU bus-frequency <- 0x%x (%dMHz)\n\r", bus, MHZ(bus)); |
| 70 | |
| 71 | while ((devp = find_node_by_devtype(devp, "cpu"))) { |
| 72 | setprop_val(devp, "clock-frequency", cpu); |
| 73 | setprop_val(devp, "timebase-frequency", tb); |
| 74 | if (bus > 0) |
| 75 | setprop_val(devp, "bus-frequency", bus); |
| 76 | } |
Scott Wood | 643d3c1 | 2007-08-21 03:39:45 +1000 | [diff] [blame] | 77 | |
| 78 | timebase_period_ns = 1000000000 / tb; |
David Gibson | 27fbaa9 | 2007-03-22 17:02:21 +1100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void dt_fixup_clock(const char *path, u32 freq) |
| 82 | { |
| 83 | void *devp = finddevice(path); |
| 84 | |
| 85 | if (devp) { |
| 86 | printf("%s: clock-frequency <- %x (%dMHz)\n\r", path, freq, MHZ(freq)); |
| 87 | setprop_val(devp, "clock-frequency", freq); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void __dt_fixup_mac_addresses(u32 startindex, ...) |
| 92 | { |
| 93 | va_list ap; |
| 94 | u32 index = startindex; |
| 95 | void *devp; |
| 96 | const u8 *addr; |
| 97 | |
| 98 | va_start(ap, startindex); |
| 99 | while ((addr = va_arg(ap, const u8 *))) { |
| 100 | devp = find_node_by_prop_value(NULL, "linux,network-index", |
| 101 | (void*)&index, sizeof(index)); |
| 102 | |
Scott Wood | 96ebc3b | 2007-09-06 05:21:18 +1000 | [diff] [blame] | 103 | if (devp) { |
| 104 | printf("ENET%d: local-mac-address <-" |
| 105 | " %02x:%02x:%02x:%02x:%02x:%02x\n\r", index, |
| 106 | addr[0], addr[1], addr[2], |
| 107 | addr[3], addr[4], addr[5]); |
David Gibson | 27fbaa9 | 2007-03-22 17:02:21 +1100 | [diff] [blame] | 108 | |
David Gibson | 27fbaa9 | 2007-03-22 17:02:21 +1100 | [diff] [blame] | 109 | setprop(devp, "local-mac-address", addr, 6); |
Scott Wood | 96ebc3b | 2007-09-06 05:21:18 +1000 | [diff] [blame] | 110 | } |
David Gibson | 27fbaa9 | 2007-03-22 17:02:21 +1100 | [diff] [blame] | 111 | |
| 112 | index++; |
| 113 | } |
| 114 | va_end(ap); |
| 115 | } |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 116 | |
| 117 | #define MAX_ADDR_CELLS 4 |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 118 | |
Scott Wood | e5d8d54 | 2007-08-21 03:40:02 +1000 | [diff] [blame] | 119 | void dt_get_reg_format(void *node, u32 *naddr, u32 *nsize) |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 120 | { |
| 121 | if (getprop(node, "#address-cells", naddr, 4) != 4) |
| 122 | *naddr = 2; |
| 123 | if (getprop(node, "#size-cells", nsize, 4) != 4) |
| 124 | *nsize = 1; |
| 125 | } |
| 126 | |
| 127 | static void copy_val(u32 *dest, u32 *src, int naddr) |
| 128 | { |
Scott Wood | e4bb688 | 2007-04-27 03:08:13 +1000 | [diff] [blame] | 129 | int pad = MAX_ADDR_CELLS - naddr; |
| 130 | |
| 131 | memset(dest, 0, pad * 4); |
| 132 | memcpy(dest + pad, src, naddr * 4); |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static int sub_reg(u32 *reg, u32 *sub) |
| 136 | { |
| 137 | int i, borrow = 0; |
| 138 | |
Scott Wood | e4bb688 | 2007-04-27 03:08:13 +1000 | [diff] [blame] | 139 | for (i = MAX_ADDR_CELLS - 1; i >= 0; i--) { |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 140 | int prev_borrow = borrow; |
| 141 | borrow = reg[i] < sub[i] + prev_borrow; |
| 142 | reg[i] -= sub[i] + prev_borrow; |
| 143 | } |
| 144 | |
| 145 | return !borrow; |
| 146 | } |
| 147 | |
Scott Wood | e4bb688 | 2007-04-27 03:08:13 +1000 | [diff] [blame] | 148 | static int add_reg(u32 *reg, u32 *add, int naddr) |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 149 | { |
| 150 | int i, carry = 0; |
| 151 | |
Scott Wood | e4bb688 | 2007-04-27 03:08:13 +1000 | [diff] [blame] | 152 | for (i = MAX_ADDR_CELLS - 1; i >= MAX_ADDR_CELLS - naddr; i--) { |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 153 | u64 tmp = (u64)reg[i] + add[i] + carry; |
| 154 | carry = tmp >> 32; |
| 155 | reg[i] = (u32)tmp; |
| 156 | } |
| 157 | |
| 158 | return !carry; |
| 159 | } |
| 160 | |
| 161 | /* It is assumed that if the first byte of reg fits in a |
| 162 | * range, then the whole reg block fits. |
| 163 | */ |
| 164 | static int compare_reg(u32 *reg, u32 *range, u32 *rangesize) |
| 165 | { |
| 166 | int i; |
| 167 | u32 end; |
| 168 | |
| 169 | for (i = 0; i < MAX_ADDR_CELLS; i++) { |
| 170 | if (reg[i] < range[i]) |
| 171 | return 0; |
| 172 | if (reg[i] > range[i]) |
| 173 | break; |
| 174 | } |
| 175 | |
| 176 | for (i = 0; i < MAX_ADDR_CELLS; i++) { |
| 177 | end = range[i] + rangesize[i]; |
| 178 | |
| 179 | if (reg[i] < end) |
| 180 | break; |
| 181 | if (reg[i] > end) |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | return reg[i] != end; |
| 186 | } |
| 187 | |
| 188 | /* reg must be MAX_ADDR_CELLS */ |
| 189 | static int find_range(u32 *reg, u32 *ranges, int nregaddr, |
| 190 | int naddr, int nsize, int buflen) |
| 191 | { |
| 192 | int nrange = nregaddr + naddr + nsize; |
| 193 | int i; |
| 194 | |
| 195 | for (i = 0; i + nrange <= buflen; i += nrange) { |
| 196 | u32 range_addr[MAX_ADDR_CELLS]; |
| 197 | u32 range_size[MAX_ADDR_CELLS]; |
| 198 | |
| 199 | copy_val(range_addr, ranges + i, naddr); |
| 200 | copy_val(range_size, ranges + i + nregaddr + naddr, nsize); |
| 201 | |
| 202 | if (compare_reg(reg, range_addr, range_size)) |
| 203 | return i; |
| 204 | } |
| 205 | |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | /* Currently only generic buses without special encodings are supported. |
| 210 | * In particular, PCI is not supported. Also, only the beginning of the |
| 211 | * reg block is tracked; size is ignored except in ranges. |
| 212 | */ |
Scott Wood | a73ac50 | 2007-08-21 03:39:48 +1000 | [diff] [blame] | 213 | static u32 prop_buf[MAX_PROP_LEN / 4]; |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 214 | |
| 215 | static int dt_xlate(void *node, int res, int reglen, unsigned long *addr, |
| 216 | unsigned long *size) |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 217 | { |
| 218 | u32 last_addr[MAX_ADDR_CELLS]; |
| 219 | u32 this_addr[MAX_ADDR_CELLS]; |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 220 | void *parent; |
| 221 | u64 ret_addr, ret_size; |
Scott Wood | 0602801 | 2007-08-21 03:39:46 +1000 | [diff] [blame] | 222 | u32 naddr, nsize, prev_naddr, prev_nsize; |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 223 | int buflen, offset; |
| 224 | |
| 225 | parent = get_parent(node); |
| 226 | if (!parent) |
| 227 | return 0; |
| 228 | |
Scott Wood | e5d8d54 | 2007-08-21 03:40:02 +1000 | [diff] [blame] | 229 | dt_get_reg_format(parent, &naddr, &nsize); |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 230 | |
| 231 | if (nsize > 2) |
| 232 | return 0; |
| 233 | |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 234 | offset = (naddr + nsize) * res; |
| 235 | |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 236 | if (reglen < offset + naddr + nsize || |
Scott Wood | a73ac50 | 2007-08-21 03:39:48 +1000 | [diff] [blame] | 237 | MAX_PROP_LEN < (offset + naddr + nsize) * 4) |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 238 | return 0; |
| 239 | |
Scott Wood | a73ac50 | 2007-08-21 03:39:48 +1000 | [diff] [blame] | 240 | copy_val(last_addr, prop_buf + offset, naddr); |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 241 | |
Scott Wood | a73ac50 | 2007-08-21 03:39:48 +1000 | [diff] [blame] | 242 | ret_size = prop_buf[offset + naddr]; |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 243 | if (nsize == 2) { |
| 244 | ret_size <<= 32; |
Scott Wood | a73ac50 | 2007-08-21 03:39:48 +1000 | [diff] [blame] | 245 | ret_size |= prop_buf[offset + naddr + 1]; |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 246 | } |
| 247 | |
Scott Wood | 0602801 | 2007-08-21 03:39:46 +1000 | [diff] [blame] | 248 | for (;;) { |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 249 | prev_naddr = naddr; |
Scott Wood | 0602801 | 2007-08-21 03:39:46 +1000 | [diff] [blame] | 250 | prev_nsize = nsize; |
| 251 | node = parent; |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 252 | |
Scott Wood | 0602801 | 2007-08-21 03:39:46 +1000 | [diff] [blame] | 253 | parent = get_parent(node); |
| 254 | if (!parent) |
| 255 | break; |
| 256 | |
Scott Wood | e5d8d54 | 2007-08-21 03:40:02 +1000 | [diff] [blame] | 257 | dt_get_reg_format(parent, &naddr, &nsize); |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 258 | |
Scott Wood | a73ac50 | 2007-08-21 03:39:48 +1000 | [diff] [blame] | 259 | buflen = getprop(node, "ranges", prop_buf, |
| 260 | sizeof(prop_buf)); |
Scott Wood | 0602801 | 2007-08-21 03:39:46 +1000 | [diff] [blame] | 261 | if (buflen == 0) |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 262 | continue; |
Scott Wood | a73ac50 | 2007-08-21 03:39:48 +1000 | [diff] [blame] | 263 | if (buflen < 0 || buflen > sizeof(prop_buf)) |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 264 | return 0; |
| 265 | |
Scott Wood | a73ac50 | 2007-08-21 03:39:48 +1000 | [diff] [blame] | 266 | offset = find_range(last_addr, prop_buf, prev_naddr, |
Scott Wood | 0602801 | 2007-08-21 03:39:46 +1000 | [diff] [blame] | 267 | naddr, prev_nsize, buflen / 4); |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 268 | |
| 269 | if (offset < 0) |
| 270 | return 0; |
| 271 | |
Scott Wood | a73ac50 | 2007-08-21 03:39:48 +1000 | [diff] [blame] | 272 | copy_val(this_addr, prop_buf + offset, prev_naddr); |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 273 | |
| 274 | if (!sub_reg(last_addr, this_addr)) |
| 275 | return 0; |
| 276 | |
Scott Wood | a73ac50 | 2007-08-21 03:39:48 +1000 | [diff] [blame] | 277 | copy_val(this_addr, prop_buf + offset + prev_naddr, naddr); |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 278 | |
Scott Wood | e4bb688 | 2007-04-27 03:08:13 +1000 | [diff] [blame] | 279 | if (!add_reg(last_addr, this_addr, naddr)) |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | if (naddr > 2) |
| 284 | return 0; |
| 285 | |
Scott Wood | e4bb688 | 2007-04-27 03:08:13 +1000 | [diff] [blame] | 286 | ret_addr = ((u64)last_addr[2] << 32) | last_addr[3]; |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 287 | |
| 288 | if (sizeof(void *) == 4 && |
| 289 | (ret_addr >= 0x100000000ULL || ret_size > 0x100000000ULL || |
| 290 | ret_addr + ret_size > 0x100000000ULL)) |
| 291 | return 0; |
| 292 | |
| 293 | *addr = ret_addr; |
| 294 | if (size) |
| 295 | *size = ret_size; |
| 296 | |
| 297 | return 1; |
| 298 | } |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 299 | |
| 300 | int dt_xlate_reg(void *node, int res, unsigned long *addr, unsigned long *size) |
| 301 | { |
| 302 | int reglen; |
| 303 | |
Scott Wood | a73ac50 | 2007-08-21 03:39:48 +1000 | [diff] [blame] | 304 | reglen = getprop(node, "reg", prop_buf, sizeof(prop_buf)) / 4; |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 305 | return dt_xlate(node, res, reglen, addr, size); |
| 306 | } |
| 307 | |
| 308 | int dt_xlate_addr(void *node, u32 *buf, int buflen, unsigned long *xlated_addr) |
| 309 | { |
| 310 | |
Scott Wood | a73ac50 | 2007-08-21 03:39:48 +1000 | [diff] [blame] | 311 | if (buflen > sizeof(prop_buf)) |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 312 | return 0; |
| 313 | |
Scott Wood | a73ac50 | 2007-08-21 03:39:48 +1000 | [diff] [blame] | 314 | memcpy(prop_buf, buf, buflen); |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 315 | return dt_xlate(node, 0, buflen / 4, xlated_addr, NULL); |
| 316 | } |
Scott Wood | a73ac50 | 2007-08-21 03:39:48 +1000 | [diff] [blame] | 317 | |
| 318 | int dt_is_compatible(void *node, const char *compat) |
| 319 | { |
| 320 | char *buf = (char *)prop_buf; |
| 321 | int len, pos; |
| 322 | |
| 323 | len = getprop(node, "compatible", buf, MAX_PROP_LEN); |
| 324 | if (len < 0) |
| 325 | return 0; |
| 326 | |
| 327 | for (pos = 0; pos < len; pos++) { |
| 328 | if (!strcmp(buf + pos, compat)) |
| 329 | return 1; |
| 330 | |
| 331 | pos += strnlen(&buf[pos], len - pos); |
| 332 | } |
| 333 | |
| 334 | return 0; |
| 335 | } |