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 | } |
| 77 | } |
| 78 | |
| 79 | void dt_fixup_clock(const char *path, u32 freq) |
| 80 | { |
| 81 | void *devp = finddevice(path); |
| 82 | |
| 83 | if (devp) { |
| 84 | printf("%s: clock-frequency <- %x (%dMHz)\n\r", path, freq, MHZ(freq)); |
| 85 | setprop_val(devp, "clock-frequency", freq); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void __dt_fixup_mac_addresses(u32 startindex, ...) |
| 90 | { |
| 91 | va_list ap; |
| 92 | u32 index = startindex; |
| 93 | void *devp; |
| 94 | const u8 *addr; |
| 95 | |
| 96 | va_start(ap, startindex); |
| 97 | while ((addr = va_arg(ap, const u8 *))) { |
| 98 | devp = find_node_by_prop_value(NULL, "linux,network-index", |
| 99 | (void*)&index, sizeof(index)); |
| 100 | |
| 101 | printf("ENET%d: local-mac-address <-" |
| 102 | " %02x:%02x:%02x:%02x:%02x:%02x\n\r", index, |
| 103 | addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); |
| 104 | |
| 105 | if (devp) |
| 106 | setprop(devp, "local-mac-address", addr, 6); |
| 107 | |
| 108 | index++; |
| 109 | } |
| 110 | va_end(ap); |
| 111 | } |
Scott Wood | 6e1af38 | 2007-03-26 15:52:24 -0500 | [diff] [blame] | 112 | |
| 113 | #define MAX_ADDR_CELLS 4 |
| 114 | #define MAX_RANGES 8 |
| 115 | |
| 116 | static void get_reg_format(void *node, u32 *naddr, u32 *nsize) |
| 117 | { |
| 118 | if (getprop(node, "#address-cells", naddr, 4) != 4) |
| 119 | *naddr = 2; |
| 120 | if (getprop(node, "#size-cells", nsize, 4) != 4) |
| 121 | *nsize = 1; |
| 122 | } |
| 123 | |
| 124 | static void copy_val(u32 *dest, u32 *src, int naddr) |
| 125 | { |
| 126 | memset(dest, 0, (MAX_ADDR_CELLS - naddr) * 4); |
| 127 | memcpy(dest, src, naddr * 4); |
| 128 | } |
| 129 | |
| 130 | static int sub_reg(u32 *reg, u32 *sub) |
| 131 | { |
| 132 | int i, borrow = 0; |
| 133 | |
| 134 | for (i = 0; i < MAX_ADDR_CELLS; i++) { |
| 135 | int prev_borrow = borrow; |
| 136 | borrow = reg[i] < sub[i] + prev_borrow; |
| 137 | reg[i] -= sub[i] + prev_borrow; |
| 138 | } |
| 139 | |
| 140 | return !borrow; |
| 141 | } |
| 142 | |
| 143 | static int add_reg(u32 *reg, u32 *add) |
| 144 | { |
| 145 | int i, carry = 0; |
| 146 | |
| 147 | for (i = 0; i < MAX_ADDR_CELLS; i++) { |
| 148 | u64 tmp = (u64)reg[i] + add[i] + carry; |
| 149 | carry = tmp >> 32; |
| 150 | reg[i] = (u32)tmp; |
| 151 | } |
| 152 | |
| 153 | return !carry; |
| 154 | } |
| 155 | |
| 156 | /* It is assumed that if the first byte of reg fits in a |
| 157 | * range, then the whole reg block fits. |
| 158 | */ |
| 159 | static int compare_reg(u32 *reg, u32 *range, u32 *rangesize) |
| 160 | { |
| 161 | int i; |
| 162 | u32 end; |
| 163 | |
| 164 | for (i = 0; i < MAX_ADDR_CELLS; i++) { |
| 165 | if (reg[i] < range[i]) |
| 166 | return 0; |
| 167 | if (reg[i] > range[i]) |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | for (i = 0; i < MAX_ADDR_CELLS; i++) { |
| 172 | end = range[i] + rangesize[i]; |
| 173 | |
| 174 | if (reg[i] < end) |
| 175 | break; |
| 176 | if (reg[i] > end) |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | return reg[i] != end; |
| 181 | } |
| 182 | |
| 183 | /* reg must be MAX_ADDR_CELLS */ |
| 184 | static int find_range(u32 *reg, u32 *ranges, int nregaddr, |
| 185 | int naddr, int nsize, int buflen) |
| 186 | { |
| 187 | int nrange = nregaddr + naddr + nsize; |
| 188 | int i; |
| 189 | |
| 190 | for (i = 0; i + nrange <= buflen; i += nrange) { |
| 191 | u32 range_addr[MAX_ADDR_CELLS]; |
| 192 | u32 range_size[MAX_ADDR_CELLS]; |
| 193 | |
| 194 | copy_val(range_addr, ranges + i, naddr); |
| 195 | copy_val(range_size, ranges + i + nregaddr + naddr, nsize); |
| 196 | |
| 197 | if (compare_reg(reg, range_addr, range_size)) |
| 198 | return i; |
| 199 | } |
| 200 | |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | /* Currently only generic buses without special encodings are supported. |
| 205 | * In particular, PCI is not supported. Also, only the beginning of the |
| 206 | * reg block is tracked; size is ignored except in ranges. |
| 207 | */ |
| 208 | int dt_xlate_reg(void *node, int res, unsigned long *addr, |
| 209 | unsigned long *size) |
| 210 | { |
| 211 | u32 last_addr[MAX_ADDR_CELLS]; |
| 212 | u32 this_addr[MAX_ADDR_CELLS]; |
| 213 | u32 buf[MAX_ADDR_CELLS * MAX_RANGES * 3]; |
| 214 | void *parent; |
| 215 | u64 ret_addr, ret_size; |
| 216 | u32 naddr, nsize, prev_naddr; |
| 217 | int buflen, offset; |
| 218 | |
| 219 | parent = get_parent(node); |
| 220 | if (!parent) |
| 221 | return 0; |
| 222 | |
| 223 | get_reg_format(parent, &naddr, &nsize); |
| 224 | |
| 225 | if (nsize > 2) |
| 226 | return 0; |
| 227 | |
| 228 | buflen = getprop(node, "reg", buf, sizeof(buf)) / 4; |
| 229 | offset = (naddr + nsize) * res; |
| 230 | |
| 231 | if (buflen < offset + naddr + nsize) |
| 232 | return 0; |
| 233 | |
| 234 | copy_val(last_addr, buf + offset, naddr); |
| 235 | |
| 236 | ret_size = buf[offset + naddr]; |
| 237 | if (nsize == 2) { |
| 238 | ret_size <<= 32; |
| 239 | ret_size |= buf[offset + naddr + 1]; |
| 240 | } |
| 241 | |
| 242 | while ((node = get_parent(node))) { |
| 243 | prev_naddr = naddr; |
| 244 | |
| 245 | get_reg_format(node, &naddr, &nsize); |
| 246 | |
| 247 | buflen = getprop(node, "ranges", buf, sizeof(buf)); |
| 248 | if (buflen < 0) |
| 249 | continue; |
| 250 | if (buflen > sizeof(buf)) |
| 251 | return 0; |
| 252 | |
| 253 | offset = find_range(last_addr, buf, prev_naddr, |
| 254 | naddr, nsize, buflen / 4); |
| 255 | |
| 256 | if (offset < 0) |
| 257 | return 0; |
| 258 | |
| 259 | copy_val(this_addr, buf + offset, prev_naddr); |
| 260 | |
| 261 | if (!sub_reg(last_addr, this_addr)) |
| 262 | return 0; |
| 263 | |
| 264 | copy_val(this_addr, buf + offset + prev_naddr, naddr); |
| 265 | |
| 266 | if (!add_reg(last_addr, this_addr)) |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | if (naddr > 2) |
| 271 | return 0; |
| 272 | |
| 273 | ret_addr = last_addr[0]; |
| 274 | if (naddr == 2) { |
| 275 | ret_addr <<= 32; |
| 276 | ret_addr |= last_addr[1]; |
| 277 | } |
| 278 | |
| 279 | if (sizeof(void *) == 4 && |
| 280 | (ret_addr >= 0x100000000ULL || ret_size > 0x100000000ULL || |
| 281 | ret_addr + ret_size > 0x100000000ULL)) |
| 282 | return 0; |
| 283 | |
| 284 | *addr = ret_addr; |
| 285 | if (size) |
| 286 | *size = ret_size; |
| 287 | |
| 288 | return 1; |
| 289 | } |