olpc_dt.c 6.6 KB

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