devtree.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. void dt_fixup_memory(u64 start, u64 size)
  21. {
  22. void *root, *memory;
  23. int naddr, nsize, i;
  24. u32 memreg[4];
  25. root = finddevice("/");
  26. if (getprop(root, "#address-cells", &naddr, sizeof(naddr)) < 0)
  27. naddr = 2;
  28. if (naddr < 1 || naddr > 2)
  29. fatal("Can't cope with #address-cells == %d in /\n\r", naddr);
  30. if (getprop(root, "#size-cells", &nsize, sizeof(nsize)) < 0)
  31. nsize = 1;
  32. if (nsize < 1 || nsize > 2)
  33. fatal("Can't cope with #size-cells == %d in /\n\r", nsize);
  34. i = 0;
  35. if (naddr == 2)
  36. memreg[i++] = start >> 32;
  37. memreg[i++] = start & 0xffffffff;
  38. if (nsize == 2)
  39. memreg[i++] = size >> 32;
  40. memreg[i++] = size & 0xffffffff;
  41. memory = finddevice("/memory");
  42. if (! memory) {
  43. memory = create_node(NULL, "memory");
  44. setprop_str(memory, "device_type", "memory");
  45. }
  46. printf("Memory <- <0x%x", memreg[0]);
  47. for (i = 1; i < (naddr + nsize); i++)
  48. printf(" 0x%x", memreg[i]);
  49. printf("> (%ldMB)\n\r", (unsigned long)(size >> 20));
  50. setprop(memory, "reg", memreg, (naddr + nsize)*sizeof(u32));
  51. }
  52. #define MHZ(x) ((x + 500000) / 1000000)
  53. void dt_fixup_cpu_clocks(u32 cpu, u32 tb, u32 bus)
  54. {
  55. void *devp = NULL;
  56. printf("CPU clock-frequency <- 0x%x (%dMHz)\n\r", cpu, MHZ(cpu));
  57. printf("CPU timebase-frequency <- 0x%x (%dMHz)\n\r", tb, MHZ(tb));
  58. if (bus > 0)
  59. printf("CPU bus-frequency <- 0x%x (%dMHz)\n\r", bus, MHZ(bus));
  60. while ((devp = find_node_by_devtype(devp, "cpu"))) {
  61. setprop_val(devp, "clock-frequency", cpu);
  62. setprop_val(devp, "timebase-frequency", tb);
  63. if (bus > 0)
  64. setprop_val(devp, "bus-frequency", bus);
  65. }
  66. timebase_period_ns = 1000000000 / tb;
  67. }
  68. void dt_fixup_clock(const char *path, u32 freq)
  69. {
  70. void *devp = finddevice(path);
  71. if (devp) {
  72. printf("%s: clock-frequency <- %x (%dMHz)\n\r", path, freq, MHZ(freq));
  73. setprop_val(devp, "clock-frequency", freq);
  74. }
  75. }
  76. void dt_fixup_mac_address_by_alias(const char *alias, const u8 *addr)
  77. {
  78. void *devp = find_node_by_alias(alias);
  79. if (devp) {
  80. printf("%s: local-mac-address <-"
  81. " %02x:%02x:%02x:%02x:%02x:%02x\n\r", alias,
  82. addr[0], addr[1], addr[2],
  83. addr[3], addr[4], addr[5]);
  84. setprop(devp, "local-mac-address", addr, 6);
  85. }
  86. }
  87. void dt_fixup_mac_address(u32 index, const u8 *addr)
  88. {
  89. void *devp = find_node_by_prop_value(NULL, "linux,network-index",
  90. (void*)&index, sizeof(index));
  91. if (devp) {
  92. printf("ENET%d: local-mac-address <-"
  93. " %02x:%02x:%02x:%02x:%02x:%02x\n\r", index,
  94. addr[0], addr[1], addr[2],
  95. addr[3], addr[4], addr[5]);
  96. setprop(devp, "local-mac-address", addr, 6);
  97. }
  98. }
  99. void __dt_fixup_mac_addresses(u32 startindex, ...)
  100. {
  101. va_list ap;
  102. u32 index = startindex;
  103. const u8 *addr;
  104. va_start(ap, startindex);
  105. while ((addr = va_arg(ap, const u8 *)))
  106. dt_fixup_mac_address(index++, addr);
  107. va_end(ap);
  108. }
  109. #define MAX_ADDR_CELLS 4
  110. void dt_get_reg_format(void *node, u32 *naddr, u32 *nsize)
  111. {
  112. if (getprop(node, "#address-cells", naddr, 4) != 4)
  113. *naddr = 2;
  114. if (getprop(node, "#size-cells", nsize, 4) != 4)
  115. *nsize = 1;
  116. }
  117. static void copy_val(u32 *dest, u32 *src, int naddr)
  118. {
  119. int pad = MAX_ADDR_CELLS - naddr;
  120. memset(dest, 0, pad * 4);
  121. memcpy(dest + pad, src, naddr * 4);
  122. }
  123. static int sub_reg(u32 *reg, u32 *sub)
  124. {
  125. int i, borrow = 0;
  126. for (i = MAX_ADDR_CELLS - 1; i >= 0; i--) {
  127. int prev_borrow = borrow;
  128. borrow = reg[i] < sub[i] + prev_borrow;
  129. reg[i] -= sub[i] + prev_borrow;
  130. }
  131. return !borrow;
  132. }
  133. static int add_reg(u32 *reg, u32 *add, int naddr)
  134. {
  135. int i, carry = 0;
  136. for (i = MAX_ADDR_CELLS - 1; i >= MAX_ADDR_CELLS - naddr; i--) {
  137. u64 tmp = (u64)reg[i] + add[i] + carry;
  138. carry = tmp >> 32;
  139. reg[i] = (u32)tmp;
  140. }
  141. return !carry;
  142. }
  143. /* It is assumed that if the first byte of reg fits in a
  144. * range, then the whole reg block fits.
  145. */
  146. static int compare_reg(u32 *reg, u32 *range, u32 *rangesize)
  147. {
  148. int i;
  149. u32 end;
  150. for (i = 0; i < MAX_ADDR_CELLS; i++) {
  151. if (reg[i] < range[i])
  152. return 0;
  153. if (reg[i] > range[i])
  154. break;
  155. }
  156. for (i = 0; i < MAX_ADDR_CELLS; i++) {
  157. end = range[i] + rangesize[i];
  158. if (reg[i] < end)
  159. break;
  160. if (reg[i] > end)
  161. return 0;
  162. }
  163. return reg[i] != end;
  164. }
  165. /* reg must be MAX_ADDR_CELLS */
  166. static int find_range(u32 *reg, u32 *ranges, int nregaddr,
  167. int naddr, int nsize, int buflen)
  168. {
  169. int nrange = nregaddr + naddr + nsize;
  170. int i;
  171. for (i = 0; i + nrange <= buflen; i += nrange) {
  172. u32 range_addr[MAX_ADDR_CELLS];
  173. u32 range_size[MAX_ADDR_CELLS];
  174. copy_val(range_addr, ranges + i, nregaddr);
  175. copy_val(range_size, ranges + i + nregaddr + naddr, nsize);
  176. if (compare_reg(reg, range_addr, range_size))
  177. return i;
  178. }
  179. return -1;
  180. }
  181. /* Currently only generic buses without special encodings are supported.
  182. * In particular, PCI is not supported. Also, only the beginning of the
  183. * reg block is tracked; size is ignored except in ranges.
  184. */
  185. static u32 prop_buf[MAX_PROP_LEN / 4];
  186. static int dt_xlate(void *node, int res, int reglen, unsigned long *addr,
  187. unsigned long *size)
  188. {
  189. u32 last_addr[MAX_ADDR_CELLS];
  190. u32 this_addr[MAX_ADDR_CELLS];
  191. void *parent;
  192. u64 ret_addr, ret_size;
  193. u32 naddr, nsize, prev_naddr, prev_nsize;
  194. int buflen, offset;
  195. parent = get_parent(node);
  196. if (!parent)
  197. return 0;
  198. dt_get_reg_format(parent, &naddr, &nsize);
  199. if (nsize > 2)
  200. return 0;
  201. offset = (naddr + nsize) * res;
  202. if (reglen < offset + naddr + nsize ||
  203. MAX_PROP_LEN < (offset + naddr + nsize) * 4)
  204. return 0;
  205. copy_val(last_addr, prop_buf + offset, naddr);
  206. ret_size = prop_buf[offset + naddr];
  207. if (nsize == 2) {
  208. ret_size <<= 32;
  209. ret_size |= prop_buf[offset + naddr + 1];
  210. }
  211. for (;;) {
  212. prev_naddr = naddr;
  213. prev_nsize = nsize;
  214. node = parent;
  215. parent = get_parent(node);
  216. if (!parent)
  217. break;
  218. dt_get_reg_format(parent, &naddr, &nsize);
  219. buflen = getprop(node, "ranges", prop_buf,
  220. sizeof(prop_buf));
  221. if (buflen == 0)
  222. continue;
  223. if (buflen < 0 || buflen > sizeof(prop_buf))
  224. return 0;
  225. offset = find_range(last_addr, prop_buf, prev_naddr,
  226. naddr, prev_nsize, buflen / 4);
  227. if (offset < 0)
  228. return 0;
  229. copy_val(this_addr, prop_buf + offset, prev_naddr);
  230. if (!sub_reg(last_addr, this_addr))
  231. return 0;
  232. copy_val(this_addr, prop_buf + offset + prev_naddr, naddr);
  233. if (!add_reg(last_addr, this_addr, naddr))
  234. return 0;
  235. }
  236. if (naddr > 2)
  237. return 0;
  238. ret_addr = ((u64)last_addr[2] << 32) | last_addr[3];
  239. if (sizeof(void *) == 4 &&
  240. (ret_addr >= 0x100000000ULL || ret_size > 0x100000000ULL ||
  241. ret_addr + ret_size > 0x100000000ULL))
  242. return 0;
  243. *addr = ret_addr;
  244. if (size)
  245. *size = ret_size;
  246. return 1;
  247. }
  248. int dt_xlate_reg(void *node, int res, unsigned long *addr, unsigned long *size)
  249. {
  250. int reglen;
  251. reglen = getprop(node, "reg", prop_buf, sizeof(prop_buf)) / 4;
  252. return dt_xlate(node, res, reglen, addr, size);
  253. }
  254. int dt_xlate_addr(void *node, u32 *buf, int buflen, unsigned long *xlated_addr)
  255. {
  256. if (buflen > sizeof(prop_buf))
  257. return 0;
  258. memcpy(prop_buf, buf, buflen);
  259. return dt_xlate(node, 0, buflen / 4, xlated_addr, NULL);
  260. }
  261. int dt_is_compatible(void *node, const char *compat)
  262. {
  263. char *buf = (char *)prop_buf;
  264. int len, pos;
  265. len = getprop(node, "compatible", buf, MAX_PROP_LEN);
  266. if (len < 0)
  267. return 0;
  268. for (pos = 0; pos < len; pos++) {
  269. if (!strcmp(buf + pos, compat))
  270. return 1;
  271. pos += strnlen(&buf[pos], len - pos);
  272. }
  273. return 0;
  274. }
  275. int dt_get_virtual_reg(void *node, void **addr, int nres)
  276. {
  277. unsigned long xaddr;
  278. int n;
  279. n = getprop(node, "virtual-reg", addr, nres * 4);
  280. if (n > 0)
  281. return n / 4;
  282. for (n = 0; n < nres; n++) {
  283. if (!dt_xlate_reg(node, n, &xaddr, NULL))
  284. break;
  285. addr[n] = (void *)xaddr;
  286. }
  287. return n;
  288. }