fdt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. static void do_fixup(void *fdt, const char *node, const char *prop,
  36. const void *val, int len, int create)
  37. {
  38. #if defined(DEBUG)
  39. int i;
  40. debug("Updating property '%s/%s' = ", node, prop);
  41. for (i = 0; i < len; i++)
  42. debug(" %.2x", *(u8*)(val+i));
  43. debug("\n");
  44. #endif
  45. int rc = fdt_find_and_setprop(fdt, node, prop, val, len, create);
  46. if (rc)
  47. printf("Unable to update property %s:%s, err=%s\n",
  48. node, prop, fdt_strerror(rc));
  49. }
  50. static void do_fixup_macaddr(void *fdt, int offset, const void *val, int i)
  51. {
  52. int rc;
  53. debug("Updating node EMAC%d\n", i);
  54. rc = fdt_setprop(fdt, offset, "mac-address", val, 6);
  55. if (rc)
  56. printf("Unable to update property %s, err=%s\n",
  57. "mac-address", fdt_strerror(rc));
  58. rc = fdt_setprop(fdt, offset, "local-mac-address", val, 6);
  59. if (rc)
  60. printf("Unable to update property %s, err=%s\n",
  61. "local-mac-address", fdt_strerror(rc));
  62. }
  63. static void do_fixup_u32(void *fdt, const char *node, const char *prop,
  64. u32 val, int create)
  65. {
  66. val = cpu_to_fdt32(val);
  67. do_fixup(fdt, node, prop, &val, sizeof(val), create);
  68. }
  69. static void do_fixup_uart(void *fdt, int offset, int i, bd_t *bd)
  70. {
  71. int rc;
  72. u32 val;
  73. PPC4xx_SYS_INFO sys_info;
  74. get_sys_info(&sys_info);
  75. debug("Updating node UART%d\n", i);
  76. val = cpu_to_fdt32(sys_info.freqUART);
  77. rc = fdt_setprop(fdt, offset, "clock-frequency", &val, 4);
  78. if (rc)
  79. printf("Unable to update node UART, err=%s\n", fdt_strerror(rc));
  80. val = cpu_to_fdt32(bd->bi_baudrate);
  81. rc = fdt_setprop(fdt, offset, "current-speed", &val, 4);
  82. if (rc)
  83. printf("Unable to update node UART, err=%s\n", fdt_strerror(rc));
  84. }
  85. void ft_cpu_setup(void *blob, bd_t *bd)
  86. {
  87. char * cpu_path = "/cpus/" OF_CPU;
  88. sys_info_t sys_info;
  89. int offset;
  90. int i;
  91. int tmp[2];
  92. get_sys_info (&sys_info);
  93. do_fixup_u32(blob, cpu_path, "timebase-frequency", bd->bi_intfreq, 1);
  94. do_fixup_u32(blob, cpu_path, "clock-frequency", bd->bi_intfreq, 1);
  95. do_fixup_u32(blob, "/plb", "clock-frequency", sys_info.freqPLB, 1);
  96. do_fixup_u32(blob, "/plb/opb", "clock-frequency", sys_info.freqOPB, 1);
  97. do_fixup_u32(blob, "/plb/opb/ebc", "clock-frequency", sys_info.freqEBC, 1);
  98. /* update, or add and update /memory node */
  99. offset = fdt_find_node_by_path(blob, "/memory");
  100. if (offset < 0) {
  101. offset = fdt_add_subnode(blob, 0, "memory");
  102. if (offset < 0)
  103. debug("failed to add /memory node: %s\n",
  104. fdt_strerror(offset));
  105. }
  106. if (offset >= 0) {
  107. fdt_setprop(blob, offset, "device_type",
  108. "memory", sizeof("memory"));
  109. tmp[0] = cpu_to_fdt32(bd->bi_memstart);
  110. tmp[1] = cpu_to_fdt32(bd->bi_memsize);
  111. fdt_setprop(blob, offset, "reg", tmp, sizeof(tmp));
  112. debug("Updating /memory node to %d:%d\n",
  113. bd->bi_memstart, bd->bi_memsize);
  114. }
  115. /*
  116. * Setup all baudrates for the UARTs
  117. */
  118. offset = 0;
  119. for (i = 0; i < 4; i++) {
  120. offset = fdt_find_node_by_type(blob, offset, "serial");
  121. if (offset < 0)
  122. break;
  123. do_fixup_uart(blob, offset, i, bd);
  124. }
  125. /*
  126. * Setup all MAC addresses in fdt
  127. */
  128. offset = 0;
  129. for (i = 0; i < 4; i++) {
  130. offset = fdt_find_node_by_type(blob, offset, "network");
  131. if (offset < 0)
  132. break;
  133. switch (i) {
  134. case 0:
  135. do_fixup_macaddr(blob, offset, bd->bi_enetaddr, 0);
  136. break;
  137. #ifdef CONFIG_HAS_ETH1
  138. case 1:
  139. do_fixup_macaddr(blob, offset, bd->bi_enet1addr, 1);
  140. break;
  141. #endif
  142. #ifdef CONFIG_HAS_ETH2
  143. case 2:
  144. do_fixup_macaddr(blob, offset, bd->bi_enet2addr, 2);
  145. break;
  146. #endif
  147. #ifdef CONFIG_HAS_ETH3
  148. case 3:
  149. do_fixup_macaddr(blob, offset, bd->bi_enet3addr, 3);
  150. break;
  151. #endif
  152. }
  153. }
  154. }
  155. #endif /* CONFIG_OF_LIBFDT */