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 | |
| 103 | printf("ENET%d: local-mac-address <-" |
| 104 | " %02x:%02x:%02x:%02x:%02x:%02x\n\r", index, |
| 105 | addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); |
| 106 | |
| 107 | if (devp) |
| 108 | setprop(devp, "local-mac-address", addr, 6); |
| 109 | |
| 110 | index++; |
| 111 | } |
| 112 | va_end(ap); |
| 113 | } |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 114 | |
| 115 | #define MAX_ADDR_CELLS 4 |
| 116 | #define MAX_RANGES 8 |
| 117 | |
| 118 | static void get_reg_format(void *node, u32 *naddr, u32 *nsize) |
| 119 | { |
| 120 | if (getprop(node, "#address-cells", naddr, 4) != 4) |
| 121 | *naddr = 2; |
| 122 | if (getprop(node, "#size-cells", nsize, 4) != 4) |
| 123 | *nsize = 1; |
| 124 | } |
| 125 | |
| 126 | static void copy_val(u32 *dest, u32 *src, int naddr) |
| 127 | { |
Scott Wood | e4bb688 | 2007-04-27 03:08:13 +1000 | [diff] [blame] | 128 | int pad = MAX_ADDR_CELLS - naddr; |
| 129 | |
| 130 | memset(dest, 0, pad * 4); |
| 131 | memcpy(dest + pad, src, naddr * 4); |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static int sub_reg(u32 *reg, u32 *sub) |
| 135 | { |
| 136 | int i, borrow = 0; |
| 137 | |
Scott Wood | e4bb688 | 2007-04-27 03:08:13 +1000 | [diff] [blame] | 138 | for (i = MAX_ADDR_CELLS - 1; i >= 0; i--) { |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 139 | int prev_borrow = borrow; |
| 140 | borrow = reg[i] < sub[i] + prev_borrow; |
| 141 | reg[i] -= sub[i] + prev_borrow; |
| 142 | } |
| 143 | |
| 144 | return !borrow; |
| 145 | } |
| 146 | |
Scott Wood | e4bb688 | 2007-04-27 03:08:13 +1000 | [diff] [blame] | 147 | static int add_reg(u32 *reg, u32 *add, int naddr) |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 148 | { |
| 149 | int i, carry = 0; |
| 150 | |
Scott Wood | e4bb688 | 2007-04-27 03:08:13 +1000 | [diff] [blame] | 151 | for (i = MAX_ADDR_CELLS - 1; i >= MAX_ADDR_CELLS - naddr; i--) { |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 152 | u64 tmp = (u64)reg[i] + add[i] + carry; |
| 153 | carry = tmp >> 32; |
| 154 | reg[i] = (u32)tmp; |
| 155 | } |
| 156 | |
| 157 | return !carry; |
| 158 | } |
| 159 | |
| 160 | /* It is assumed that if the first byte of reg fits in a |
| 161 | * range, then the whole reg block fits. |
| 162 | */ |
| 163 | static int compare_reg(u32 *reg, u32 *range, u32 *rangesize) |
| 164 | { |
| 165 | int i; |
| 166 | u32 end; |
| 167 | |
| 168 | for (i = 0; i < MAX_ADDR_CELLS; i++) { |
| 169 | if (reg[i] < range[i]) |
| 170 | return 0; |
| 171 | if (reg[i] > range[i]) |
| 172 | break; |
| 173 | } |
| 174 | |
| 175 | for (i = 0; i < MAX_ADDR_CELLS; i++) { |
| 176 | end = range[i] + rangesize[i]; |
| 177 | |
| 178 | if (reg[i] < end) |
| 179 | break; |
| 180 | if (reg[i] > end) |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | return reg[i] != end; |
| 185 | } |
| 186 | |
| 187 | /* reg must be MAX_ADDR_CELLS */ |
| 188 | static int find_range(u32 *reg, u32 *ranges, int nregaddr, |
| 189 | int naddr, int nsize, int buflen) |
| 190 | { |
| 191 | int nrange = nregaddr + naddr + nsize; |
| 192 | int i; |
| 193 | |
| 194 | for (i = 0; i + nrange <= buflen; i += nrange) { |
| 195 | u32 range_addr[MAX_ADDR_CELLS]; |
| 196 | u32 range_size[MAX_ADDR_CELLS]; |
| 197 | |
| 198 | copy_val(range_addr, ranges + i, naddr); |
| 199 | copy_val(range_size, ranges + i + nregaddr + naddr, nsize); |
| 200 | |
| 201 | if (compare_reg(reg, range_addr, range_size)) |
| 202 | return i; |
| 203 | } |
| 204 | |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | /* Currently only generic buses without special encodings are supported. |
| 209 | * In particular, PCI is not supported. Also, only the beginning of the |
| 210 | * reg block is tracked; size is ignored except in ranges. |
| 211 | */ |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 212 | static u32 dt_xlate_buf[MAX_ADDR_CELLS * MAX_RANGES * 3]; |
| 213 | |
| 214 | static int dt_xlate(void *node, int res, int reglen, unsigned long *addr, |
| 215 | unsigned long *size) |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 216 | { |
| 217 | u32 last_addr[MAX_ADDR_CELLS]; |
| 218 | u32 this_addr[MAX_ADDR_CELLS]; |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 219 | void *parent; |
| 220 | u64 ret_addr, ret_size; |
Scott Wood | 0602801 | 2007-08-21 03:39:46 +1000 | [diff] [blame^] | 221 | u32 naddr, nsize, prev_naddr, prev_nsize; |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 222 | int buflen, offset; |
| 223 | |
| 224 | parent = get_parent(node); |
| 225 | if (!parent) |
| 226 | return 0; |
| 227 | |
| 228 | get_reg_format(parent, &naddr, &nsize); |
| 229 | |
| 230 | if (nsize > 2) |
| 231 | return 0; |
| 232 | |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 233 | offset = (naddr + nsize) * res; |
| 234 | |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 235 | if (reglen < offset + naddr + nsize || |
Scott Wood | 0602801 | 2007-08-21 03:39:46 +1000 | [diff] [blame^] | 236 | sizeof(dt_xlate_buf) < (offset + naddr + nsize) * 4) |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 237 | return 0; |
| 238 | |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 239 | copy_val(last_addr, dt_xlate_buf + offset, naddr); |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 240 | |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 241 | ret_size = dt_xlate_buf[offset + naddr]; |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 242 | if (nsize == 2) { |
| 243 | ret_size <<= 32; |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 244 | ret_size |= dt_xlate_buf[offset + naddr + 1]; |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 245 | } |
| 246 | |
Scott Wood | 0602801 | 2007-08-21 03:39:46 +1000 | [diff] [blame^] | 247 | for (;;) { |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 248 | prev_naddr = naddr; |
Scott Wood | 0602801 | 2007-08-21 03:39:46 +1000 | [diff] [blame^] | 249 | prev_nsize = nsize; |
| 250 | node = parent; |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 251 | |
Scott Wood | 0602801 | 2007-08-21 03:39:46 +1000 | [diff] [blame^] | 252 | parent = get_parent(node); |
| 253 | if (!parent) |
| 254 | break; |
| 255 | |
| 256 | get_reg_format(parent, &naddr, &nsize); |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 257 | |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 258 | buflen = getprop(node, "ranges", dt_xlate_buf, |
| 259 | sizeof(dt_xlate_buf)); |
Scott Wood | 0602801 | 2007-08-21 03:39:46 +1000 | [diff] [blame^] | 260 | if (buflen == 0) |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 261 | continue; |
Scott Wood | 0602801 | 2007-08-21 03:39:46 +1000 | [diff] [blame^] | 262 | if (buflen < 0 || buflen > sizeof(dt_xlate_buf)) |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 263 | return 0; |
| 264 | |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 265 | offset = find_range(last_addr, dt_xlate_buf, prev_naddr, |
Scott Wood | 0602801 | 2007-08-21 03:39:46 +1000 | [diff] [blame^] | 266 | naddr, prev_nsize, buflen / 4); |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 267 | |
| 268 | if (offset < 0) |
| 269 | return 0; |
| 270 | |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 271 | copy_val(this_addr, dt_xlate_buf + offset, prev_naddr); |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 272 | |
| 273 | if (!sub_reg(last_addr, this_addr)) |
| 274 | return 0; |
| 275 | |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 276 | copy_val(this_addr, dt_xlate_buf + offset + prev_naddr, naddr); |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 277 | |
Scott Wood | e4bb688 | 2007-04-27 03:08:13 +1000 | [diff] [blame] | 278 | if (!add_reg(last_addr, this_addr, naddr)) |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | if (naddr > 2) |
| 283 | return 0; |
| 284 | |
Scott Wood | e4bb688 | 2007-04-27 03:08:13 +1000 | [diff] [blame] | 285 | ret_addr = ((u64)last_addr[2] << 32) | last_addr[3]; |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 286 | |
| 287 | if (sizeof(void *) == 4 && |
| 288 | (ret_addr >= 0x100000000ULL || ret_size > 0x100000000ULL || |
| 289 | ret_addr + ret_size > 0x100000000ULL)) |
| 290 | return 0; |
| 291 | |
| 292 | *addr = ret_addr; |
| 293 | if (size) |
| 294 | *size = ret_size; |
| 295 | |
| 296 | return 1; |
| 297 | } |
Mark A. Greer | 8895ea4 | 2007-04-28 06:48:24 +1000 | [diff] [blame] | 298 | |
| 299 | int dt_xlate_reg(void *node, int res, unsigned long *addr, unsigned long *size) |
| 300 | { |
| 301 | int reglen; |
| 302 | |
| 303 | reglen = getprop(node, "reg", dt_xlate_buf, sizeof(dt_xlate_buf)) / 4; |
| 304 | return dt_xlate(node, res, reglen, addr, size); |
| 305 | } |
| 306 | |
| 307 | int dt_xlate_addr(void *node, u32 *buf, int buflen, unsigned long *xlated_addr) |
| 308 | { |
| 309 | |
| 310 | if (buflen > sizeof(dt_xlate_buf)) |
| 311 | return 0; |
| 312 | |
| 313 | memcpy(dt_xlate_buf, buf, buflen); |
| 314 | return dt_xlate(node, 0, buflen / 4, xlated_addr, NULL); |
| 315 | } |