fdt.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * (C) Copyright 2007
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /* define DEBUG for debugging output (obviously ;-)) */
  24. #if 0
  25. #define DEBUG
  26. #endif
  27. #include <common.h>
  28. #include <watchdog.h>
  29. #include <command.h>
  30. #include <asm/cache.h>
  31. #include <ppc4xx.h>
  32. #if defined(CONFIG_OF_LIBFDT)
  33. #include <libfdt.h>
  34. #include <libfdt_env.h>
  35. DECLARE_GLOBAL_DATA_PTR;
  36. static void do_fixup(void *fdt, const char *node, const char *prop,
  37. const void *val, int len, int create)
  38. {
  39. #if defined(DEBUG)
  40. int i;
  41. debug("Updating property '%s/%s' = ", node, prop);
  42. for (i = 0; i < len; i++)
  43. debug(" %.2x", *(u8*)(val+i));
  44. debug("(%d)\n", *(u32 *)val);
  45. #endif
  46. int rc = fdt_find_and_setprop(fdt, node, prop, val, len, create);
  47. if (rc)
  48. printf("Unable to update property %s:%s, err=%s\n",
  49. node, prop, fdt_strerror(rc));
  50. }
  51. static void do_fixup_macaddr(void *fdt, int offset, const void *val, int i)
  52. {
  53. int rc;
  54. debug("Updating node EMAC%d\n", i);
  55. rc = fdt_setprop(fdt, offset, "mac-address", val, 6);
  56. if (rc)
  57. printf("Unable to update property %s, err=%s\n",
  58. "mac-address", fdt_strerror(rc));
  59. rc = fdt_setprop(fdt, offset, "local-mac-address", val, 6);
  60. if (rc)
  61. printf("Unable to update property %s, err=%s\n",
  62. "local-mac-address", fdt_strerror(rc));
  63. }
  64. static void do_fixup_u32(void *fdt, const char *node, const char *prop,
  65. u32 val, int create)
  66. {
  67. val = cpu_to_fdt32(val);
  68. do_fixup(fdt, node, prop, &val, sizeof(val), create);
  69. }
  70. static void do_fixup_uart(void *fdt, int offset, int i, bd_t *bd)
  71. {
  72. int rc;
  73. u32 val;
  74. PPC4xx_SYS_INFO sys_info;
  75. get_sys_info(&sys_info);
  76. debug("Updating node UART%d: clock-frequency=%d\n", i, gd->uart_clk);
  77. val = cpu_to_fdt32(gd->uart_clk);
  78. rc = fdt_setprop(fdt, offset, "clock-frequency", &val, 4);
  79. if (rc)
  80. printf("Unable to update node UART, err=%s\n", fdt_strerror(rc));
  81. val = cpu_to_fdt32(bd->bi_baudrate);
  82. rc = fdt_setprop(fdt, offset, "current-speed", &val, 4);
  83. if (rc)
  84. printf("Unable to update node UART, err=%s\n", fdt_strerror(rc));
  85. }
  86. void ft_cpu_setup(void *blob, bd_t *bd)
  87. {
  88. char * cpu_path = "/cpus/" OF_CPU;
  89. sys_info_t sys_info;
  90. int offset;
  91. int i;
  92. int tmp[2];
  93. get_sys_info (&sys_info);
  94. do_fixup_u32(blob, cpu_path, "timebase-frequency", bd->bi_intfreq, 1);
  95. do_fixup_u32(blob, cpu_path, "clock-frequency", bd->bi_intfreq, 1);
  96. do_fixup_u32(blob, "/plb", "clock-frequency", sys_info.freqPLB, 1);
  97. do_fixup_u32(blob, "/plb/opb", "clock-frequency", sys_info.freqOPB, 1);
  98. do_fixup_u32(blob, "/plb/opb/ebc", "clock-frequency", sys_info.freqEBC, 1);
  99. /* update, or add and update /memory node */
  100. offset = fdt_find_node_by_path(blob, "/memory");
  101. if (offset < 0) {
  102. offset = fdt_add_subnode(blob, 0, "memory");
  103. if (offset < 0)
  104. debug("failed to add /memory node: %s\n",
  105. fdt_strerror(offset));
  106. }
  107. if (offset >= 0) {
  108. fdt_setprop(blob, offset, "device_type",
  109. "memory", sizeof("memory"));
  110. tmp[0] = cpu_to_fdt32(bd->bi_memstart);
  111. tmp[1] = cpu_to_fdt32(bd->bi_memsize);
  112. fdt_setprop(blob, offset, "reg", tmp, sizeof(tmp));
  113. debug("Updating /memory node to %d:%d\n",
  114. bd->bi_memstart, bd->bi_memsize);
  115. }
  116. /*
  117. * Setup all baudrates for the UARTs
  118. */
  119. offset = 0;
  120. for (i = 0; i < 4; i++) {
  121. offset = fdt_find_node_by_type(blob, offset, "serial");
  122. if (offset < 0)
  123. break;
  124. do_fixup_uart(blob, offset, i, bd);
  125. }
  126. /*
  127. * Setup all MAC addresses in fdt
  128. */
  129. offset = 0;
  130. for (i = 0; i < 4; i++) {
  131. offset = fdt_find_node_by_type(blob, offset, "network");
  132. if (offset < 0)
  133. break;
  134. switch (i) {
  135. case 0:
  136. do_fixup_macaddr(blob, offset, bd->bi_enetaddr, 0);
  137. break;
  138. #ifdef CONFIG_HAS_ETH1
  139. case 1:
  140. do_fixup_macaddr(blob, offset, bd->bi_enet1addr, 1);
  141. break;
  142. #endif
  143. #ifdef CONFIG_HAS_ETH2
  144. case 2:
  145. do_fixup_macaddr(blob, offset, bd->bi_enet2addr, 2);
  146. break;
  147. #endif
  148. #ifdef CONFIG_HAS_ETH3
  149. case 3:
  150. do_fixup_macaddr(blob, offset, bd->bi_enet3addr, 3);
  151. break;
  152. #endif
  153. }
  154. }
  155. }
  156. #endif /* CONFIG_OF_LIBFDT */