olpc_dt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. void __init olpc_dt_build_devicetree(void)
  136. {
  137. phandle root;
  138. if (!olpc_ofw_is_installed())
  139. return;
  140. root = olpc_dt_getsibling(0);
  141. if (!root) {
  142. pr_err("PROM: unable to get root node from OFW!\n");
  143. return;
  144. }
  145. of_pdt_build_devicetree(root, &prom_olpc_ops);
  146. pr_info("PROM DT: Built device tree with %u bytes of memory.\n",
  147. prom_early_allocated);
  148. }
  149. /* A list of DT node/bus matches that we want to expose as platform devices */
  150. static struct of_device_id __initdata of_ids[] = {
  151. { .compatible = "olpc,xo1-battery" },
  152. { .compatible = "olpc,xo1-dcon" },
  153. { .compatible = "olpc,xo1-rtc" },
  154. {},
  155. };
  156. static int __init olpc_create_platform_devices(void)
  157. {
  158. if (machine_is_olpc())
  159. return of_platform_bus_probe(NULL, of_ids, NULL);
  160. else
  161. return 0;
  162. }
  163. device_initcall(olpc_create_platform_devices);