olpc_dt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_pdt.h>
  22. #include <asm/olpc_ofw.h>
  23. static phandle __init olpc_dt_getsibling(phandle node)
  24. {
  25. const void *args[] = { (void *)node };
  26. void *res[] = { &node };
  27. if ((s32)node == -1)
  28. return 0;
  29. if (olpc_ofw("peer", args, res) || (s32)node == -1)
  30. return 0;
  31. return node;
  32. }
  33. static phandle __init olpc_dt_getchild(phandle node)
  34. {
  35. const void *args[] = { (void *)node };
  36. void *res[] = { &node };
  37. if ((s32)node == -1)
  38. return 0;
  39. if (olpc_ofw("child", args, res) || (s32)node == -1) {
  40. pr_err("PROM: %s: fetching child failed!\n", __func__);
  41. return 0;
  42. }
  43. return node;
  44. }
  45. static int __init olpc_dt_getproplen(phandle node, const char *prop)
  46. {
  47. const void *args[] = { (void *)node, prop };
  48. int len;
  49. void *res[] = { &len };
  50. if ((s32)node == -1)
  51. return -1;
  52. if (olpc_ofw("getproplen", args, res)) {
  53. pr_err("PROM: %s: getproplen failed!\n", __func__);
  54. return -1;
  55. }
  56. return len;
  57. }
  58. static int __init olpc_dt_getproperty(phandle node, const char *prop,
  59. char *buf, int bufsize)
  60. {
  61. int plen;
  62. plen = olpc_dt_getproplen(node, prop);
  63. if (plen > bufsize || plen < 1) {
  64. return -1;
  65. } else {
  66. const void *args[] = { (void *)node, prop, buf, (void *)plen };
  67. void *res[] = { &plen };
  68. if (olpc_ofw("getprop", args, res)) {
  69. pr_err("PROM: %s: getprop failed!\n", __func__);
  70. return -1;
  71. }
  72. }
  73. return plen;
  74. }
  75. static int __init olpc_dt_nextprop(phandle node, char *prev, char *buf)
  76. {
  77. const void *args[] = { (void *)node, prev, buf };
  78. int success;
  79. void *res[] = { &success };
  80. buf[0] = '\0';
  81. if ((s32)node == -1)
  82. return -1;
  83. if (olpc_ofw("nextprop", args, res) || success != 1)
  84. return -1;
  85. return 0;
  86. }
  87. static int __init olpc_dt_pkg2path(phandle node, char *buf,
  88. const int buflen, int *len)
  89. {
  90. const void *args[] = { (void *)node, buf, (void *)buflen };
  91. void *res[] = { len };
  92. if ((s32)node == -1)
  93. return -1;
  94. if (olpc_ofw("package-to-path", args, res) || *len < 1)
  95. return -1;
  96. return 0;
  97. }
  98. static unsigned int prom_early_allocated __initdata;
  99. void * __init prom_early_alloc(unsigned long size)
  100. {
  101. static u8 *mem;
  102. static size_t free_mem;
  103. void *res;
  104. if (free_mem < size) {
  105. const size_t chunk_size = max(PAGE_SIZE, size);
  106. /*
  107. * To mimimize the number of allocations, grab at least
  108. * PAGE_SIZE of memory (that's an arbitrary choice that's
  109. * fast enough on the platforms we care about while minimizing
  110. * wasted bootmem) and hand off chunks of it to callers.
  111. */
  112. res = alloc_bootmem(chunk_size);
  113. if (!res)
  114. return NULL;
  115. prom_early_allocated += chunk_size;
  116. memset(res, 0, chunk_size);
  117. free_mem = chunk_size;
  118. mem = res;
  119. }
  120. /* allocate from the local cache */
  121. free_mem -= size;
  122. res = mem;
  123. mem += size;
  124. return res;
  125. }
  126. static struct of_pdt_ops prom_olpc_ops __initdata = {
  127. .nextprop = olpc_dt_nextprop,
  128. .getproplen = olpc_dt_getproplen,
  129. .getproperty = olpc_dt_getproperty,
  130. .getchild = olpc_dt_getchild,
  131. .getsibling = olpc_dt_getsibling,
  132. .pkg2path = olpc_dt_pkg2path,
  133. };
  134. void __init olpc_dt_build_devicetree(void)
  135. {
  136. phandle root;
  137. if (!olpc_ofw_is_installed())
  138. return;
  139. root = olpc_dt_getsibling(0);
  140. if (!root) {
  141. pr_err("PROM: unable to get root node from OFW!\n");
  142. return;
  143. }
  144. of_pdt_build_devicetree(root, &prom_olpc_ops);
  145. pr_info("PROM DT: Built device tree with %u bytes of memory.\n",
  146. prom_early_allocated);
  147. }