olpc_dt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OLPC-specific OFW device tree support code.
  4. *
  5. * Paul Mackerras August 1996.
  6. * Copyright (C) 1996-2005 Paul Mackerras.
  7. *
  8. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  9. * {engebret|bergner}@us.ibm.com
  10. *
  11. * Adapted for sparc by David S. Miller davem@davemloft.net
  12. * Adapted for x86/OLPC by Andres Salomon <dilinger@queued.net>
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/memblock.h>
  16. #include <linux/of.h>
  17. #include <linux/of_pdt.h>
  18. #include <asm/olpc.h>
  19. #include <asm/olpc_ofw.h>
  20. static phandle __init olpc_dt_getsibling(phandle node)
  21. {
  22. const void *args[] = { (void *)node };
  23. void *res[] = { &node };
  24. if ((s32)node == -1)
  25. return 0;
  26. if (olpc_ofw("peer", args, res) || (s32)node == -1)
  27. return 0;
  28. return node;
  29. }
  30. static phandle __init olpc_dt_getchild(phandle node)
  31. {
  32. const void *args[] = { (void *)node };
  33. void *res[] = { &node };
  34. if ((s32)node == -1)
  35. return 0;
  36. if (olpc_ofw("child", args, res) || (s32)node == -1) {
  37. pr_err("PROM: %s: fetching child failed!\n", __func__);
  38. return 0;
  39. }
  40. return node;
  41. }
  42. static int __init olpc_dt_getproplen(phandle node, const char *prop)
  43. {
  44. const void *args[] = { (void *)node, prop };
  45. int len;
  46. void *res[] = { &len };
  47. if ((s32)node == -1)
  48. return -1;
  49. if (olpc_ofw("getproplen", args, res)) {
  50. pr_err("PROM: %s: getproplen failed!\n", __func__);
  51. return -1;
  52. }
  53. return len;
  54. }
  55. static int __init olpc_dt_getproperty(phandle node, const char *prop,
  56. char *buf, int bufsize)
  57. {
  58. int plen;
  59. plen = olpc_dt_getproplen(node, prop);
  60. if (plen > bufsize || plen < 1) {
  61. return -1;
  62. } else {
  63. const void *args[] = { (void *)node, prop, buf, (void *)plen };
  64. void *res[] = { &plen };
  65. if (olpc_ofw("getprop", args, res)) {
  66. pr_err("PROM: %s: getprop failed!\n", __func__);
  67. return -1;
  68. }
  69. }
  70. return plen;
  71. }
  72. static int __init olpc_dt_nextprop(phandle node, char *prev, char *buf)
  73. {
  74. const void *args[] = { (void *)node, prev, buf };
  75. int success;
  76. void *res[] = { &success };
  77. buf[0] = '\0';
  78. if ((s32)node == -1)
  79. return -1;
  80. if (olpc_ofw("nextprop", args, res) || success != 1)
  81. return -1;
  82. return 0;
  83. }
  84. static int __init olpc_dt_pkg2path(phandle node, char *buf,
  85. const int buflen, int *len)
  86. {
  87. const void *args[] = { (void *)node, buf, (void *)buflen };
  88. void *res[] = { len };
  89. if ((s32)node == -1)
  90. return -1;
  91. if (olpc_ofw("package-to-path", args, res) || *len < 1)
  92. return -1;
  93. return 0;
  94. }
  95. static unsigned int prom_early_allocated __initdata;
  96. void * __init prom_early_alloc(unsigned long size)
  97. {
  98. static u8 *mem;
  99. static size_t free_mem;
  100. void *res;
  101. if (free_mem < size) {
  102. const size_t chunk_size = max(PAGE_SIZE, size);
  103. /*
  104. * To minimize the number of allocations, grab at least
  105. * PAGE_SIZE of memory (that's an arbitrary choice that's
  106. * fast enough on the platforms we care about while minimizing
  107. * wasted bootmem) and hand off chunks of it to callers.
  108. */
  109. res = memblock_alloc(chunk_size, SMP_CACHE_BYTES);
  110. if (!res)
  111. panic("%s: Failed to allocate %zu bytes\n", __func__,
  112. chunk_size);
  113. BUG_ON(!res);
  114. prom_early_allocated += chunk_size;
  115. memset(res, 0, chunk_size);
  116. free_mem = chunk_size;
  117. mem = res;
  118. }
  119. /* allocate from the local cache */
  120. free_mem -= size;
  121. res = mem;
  122. mem += size;
  123. return res;
  124. }
  125. static struct of_pdt_ops prom_olpc_ops __initdata = {
  126. .nextprop = olpc_dt_nextprop,
  127. .getproplen = olpc_dt_getproplen,
  128. .getproperty = olpc_dt_getproperty,
  129. .getchild = olpc_dt_getchild,
  130. .getsibling = olpc_dt_getsibling,
  131. .pkg2path = olpc_dt_pkg2path,
  132. };
  133. static phandle __init olpc_dt_finddevice(const char *path)
  134. {
  135. phandle node;
  136. const void *args[] = { path };
  137. void *res[] = { &node };
  138. if (olpc_ofw("finddevice", args, res)) {
  139. pr_err("olpc_dt: finddevice failed!\n");
  140. return 0;
  141. }
  142. if ((s32) node == -1)
  143. return 0;
  144. return node;
  145. }
  146. static int __init olpc_dt_interpret(const char *words)
  147. {
  148. int result;
  149. const void *args[] = { words };
  150. void *res[] = { &result };
  151. if (olpc_ofw("interpret", args, res)) {
  152. pr_err("olpc_dt: interpret failed!\n");
  153. return -1;
  154. }
  155. return result;
  156. }
  157. /*
  158. * Extract board revision directly from OFW device tree.
  159. * We can't use olpc_platform_info because that hasn't been set up yet.
  160. */
  161. static u32 __init olpc_dt_get_board_revision(void)
  162. {
  163. phandle node;
  164. __be32 rev;
  165. int r;
  166. node = olpc_dt_finddevice("/");
  167. if (!node)
  168. return 0;
  169. r = olpc_dt_getproperty(node, "board-revision-int",
  170. (char *) &rev, sizeof(rev));
  171. if (r < 0)
  172. return 0;
  173. return be32_to_cpu(rev);
  174. }
  175. static int __init olpc_dt_compatible_match(phandle node, const char *compat)
  176. {
  177. char buf[64], *p;
  178. int plen, len;
  179. plen = olpc_dt_getproperty(node, "compatible", buf, sizeof(buf));
  180. if (plen <= 0)
  181. return 0;
  182. len = strlen(compat);
  183. for (p = buf; p < buf + plen; p += strlen(p) + 1) {
  184. if (strcmp(p, compat) == 0)
  185. return 1;
  186. }
  187. return 0;
  188. }
  189. static void __init olpc_dt_fixup(void)
  190. {
  191. phandle node;
  192. u32 board_rev;
  193. node = olpc_dt_finddevice("/battery@0");
  194. if (!node)
  195. return;
  196. board_rev = olpc_dt_get_board_revision();
  197. if (!board_rev)
  198. return;
  199. if (board_rev >= olpc_board_pre(0xd0)) {
  200. /* XO-1.5 */
  201. if (olpc_dt_compatible_match(node, "olpc,xo1.5-battery"))
  202. return;
  203. /* Add olpc,xo1.5-battery compatible marker to battery node */
  204. olpc_dt_interpret("\" /battery@0\" find-device");
  205. olpc_dt_interpret(" \" olpc,xo1.5-battery\" +compatible");
  206. olpc_dt_interpret("device-end");
  207. if (olpc_dt_compatible_match(node, "olpc,xo1-battery")) {
  208. /*
  209. * If we have a olpc,xo1-battery compatible, then we're
  210. * running a new enough firmware that already has
  211. * the dcon node.
  212. */
  213. return;
  214. }
  215. /* Add dcon device */
  216. olpc_dt_interpret("\" /pci/display@1\" find-device");
  217. olpc_dt_interpret(" new-device");
  218. olpc_dt_interpret(" \" dcon\" device-name");
  219. olpc_dt_interpret(" \" olpc,xo1-dcon\" +compatible");
  220. olpc_dt_interpret(" finish-device");
  221. olpc_dt_interpret("device-end");
  222. } else {
  223. /* XO-1 */
  224. if (olpc_dt_compatible_match(node, "olpc,xo1-battery")) {
  225. /*
  226. * If we have a olpc,xo1-battery compatible, then we're
  227. * running a new enough firmware that already has
  228. * the dcon and RTC nodes.
  229. */
  230. return;
  231. }
  232. /* Add dcon device, mark RTC as olpc,xo1-rtc */
  233. olpc_dt_interpret("\" /pci/display@1,1\" find-device");
  234. olpc_dt_interpret(" new-device");
  235. olpc_dt_interpret(" \" dcon\" device-name");
  236. olpc_dt_interpret(" \" olpc,xo1-dcon\" +compatible");
  237. olpc_dt_interpret(" finish-device");
  238. olpc_dt_interpret("device-end");
  239. olpc_dt_interpret("\" /rtc\" find-device");
  240. olpc_dt_interpret(" \" olpc,xo1-rtc\" +compatible");
  241. olpc_dt_interpret("device-end");
  242. }
  243. /* Add olpc,xo1-battery compatible marker to battery node */
  244. olpc_dt_interpret("\" /battery@0\" find-device");
  245. olpc_dt_interpret(" \" olpc,xo1-battery\" +compatible");
  246. olpc_dt_interpret("device-end");
  247. }
  248. void __init olpc_dt_build_devicetree(void)
  249. {
  250. phandle root;
  251. if (!olpc_ofw_is_installed())
  252. return;
  253. olpc_dt_fixup();
  254. root = olpc_dt_getsibling(0);
  255. if (!root) {
  256. pr_err("PROM: unable to get root node from OFW!\n");
  257. return;
  258. }
  259. of_pdt_build_devicetree(root, &prom_olpc_ops);
  260. pr_info("PROM DT: Built device tree with %u bytes of memory.\n",
  261. prom_early_allocated);
  262. }