fdt.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright 2009-2011 Freescale Semiconductor, Inc.
  3. *
  4. * This file is derived from arch/powerpc/cpu/mpc85xx/cpu.c and
  5. * arch/powerpc/cpu/mpc86xx/cpu.c. Basically this file contains
  6. * cpu specific common code for 85xx/86xx processors.
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <libfdt.h>
  27. #include <fdt_support.h>
  28. #include <asm/mp.h>
  29. #include <asm/fsl_serdes.h>
  30. #include <phy.h>
  31. #include <hwconfig.h>
  32. #ifdef CONFIG_HAS_FSL_DR_USB
  33. #include <usb.h>
  34. #endif
  35. #if defined(CONFIG_MP) && (defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx))
  36. static int ft_del_cpuhandle(void *blob, int cpuhandle)
  37. {
  38. int off, ret = -FDT_ERR_NOTFOUND;
  39. /* if we find a match, we'll delete at it which point the offsets are
  40. * invalid so we start over from the beginning
  41. */
  42. off = fdt_node_offset_by_prop_value(blob, -1, "cpu-handle",
  43. &cpuhandle, 4);
  44. while (off != -FDT_ERR_NOTFOUND) {
  45. fdt_delprop(blob, off, "cpu-handle");
  46. ret = 1;
  47. off = fdt_node_offset_by_prop_value(blob, -1, "cpu-handle",
  48. &cpuhandle, 4);
  49. }
  50. return ret;
  51. }
  52. void ft_fixup_num_cores(void *blob) {
  53. int off, num_cores, del_cores;
  54. del_cores = 0;
  55. num_cores = cpu_numcores();
  56. off = fdt_node_offset_by_prop_value(blob, -1, "device_type", "cpu", 4);
  57. while (off != -FDT_ERR_NOTFOUND) {
  58. u32 *reg = (u32 *)fdt_getprop(blob, off, "reg", 0);
  59. if ((*reg > num_cores-1) || (is_core_disabled(*reg))) {
  60. int ph = fdt_get_phandle(blob, off);
  61. /* Delete the cpu node once there are no cpu handles */
  62. if (-FDT_ERR_NOTFOUND == ft_del_cpuhandle(blob, ph)) {
  63. fdt_del_node(blob, off);
  64. del_cores++;
  65. }
  66. /* either we deleted some cpu handles or the cpu node
  67. * so we reset the offset back to the start since we
  68. * can't trust the offsets anymore
  69. */
  70. off = -1;
  71. }
  72. off = fdt_node_offset_by_prop_value(blob, off,
  73. "device_type", "cpu", 4);
  74. }
  75. debug ("%x core system found\n", num_cores);
  76. debug ("deleted %d extra core entry entries from device tree\n",
  77. del_cores);
  78. }
  79. #endif /* defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx) */
  80. #ifdef CONFIG_HAS_FSL_DR_USB
  81. static void fdt_fixup_usb_mode_phy_type(void *blob, const char *mode,
  82. const char *phy_type)
  83. {
  84. const char *compat = "fsl-usb2-dr";
  85. const char *prop_mode = "dr_mode";
  86. const char *prop_type = "phy_type";
  87. static int start_offset = -1;
  88. int node_offset;
  89. int err;
  90. node_offset = fdt_node_offset_by_compatible(blob,
  91. start_offset, compat);
  92. if (node_offset < 0) {
  93. printf("WARNING: could not find compatible node %s: %s.\n",
  94. compat, fdt_strerror(node_offset));
  95. return;
  96. }
  97. if (mode) {
  98. err = fdt_setprop(blob, node_offset, prop_mode, mode,
  99. strlen(mode) + 1);
  100. if (err < 0)
  101. printf("WARNING: could not set %s for %s: %s.\n",
  102. prop_mode, compat, fdt_strerror(err));
  103. }
  104. if (phy_type) {
  105. err = fdt_setprop(blob, node_offset, prop_type, phy_type,
  106. strlen(phy_type) + 1);
  107. if (err < 0)
  108. printf("WARNING: could not set %s for %s: %s.\n",
  109. prop_type, compat, fdt_strerror(err));
  110. }
  111. start_offset = node_offset;
  112. }
  113. void fdt_fixup_dr_usb(void *blob, bd_t *bd)
  114. {
  115. const char *modes[] = { "host", "peripheral", "otg" };
  116. const char *phys[] = { "ulpi", "umti" };
  117. const char *mode = NULL;
  118. const char *phy_type = NULL;
  119. char usb1_defined = 0;
  120. char str[5];
  121. int i, j;
  122. for (i = 1; i <= USB_MAX_DEVICE; i++) {
  123. int mode_idx = -1, phy_idx = -1;
  124. sprintf(str, "%s%d", "usb", i);
  125. if (hwconfig(str)) {
  126. for (j = 0; j < sizeof(modes); j++) {
  127. if (hwconfig_subarg_cmp(str, "dr_mode",
  128. modes[j])) {
  129. mode_idx = j;
  130. break;
  131. }
  132. }
  133. for (j = 0; j < sizeof(phys); j++) {
  134. if (hwconfig_subarg_cmp(str, "phy_type",
  135. phys[j])) {
  136. phy_idx = j;
  137. break;
  138. }
  139. }
  140. if (mode_idx >= 0)
  141. fdt_fixup_usb_mode_phy_type(blob,
  142. modes[mode_idx], NULL);
  143. if (phy_idx >= 0)
  144. fdt_fixup_usb_mode_phy_type(blob,
  145. NULL, phys[phy_idx]);
  146. if (!strcmp(str, "usb1"))
  147. usb1_defined = 1;
  148. if (mode_idx < 0 && phy_idx < 0)
  149. printf("WARNING: invalid phy or mode\n");
  150. } else {
  151. break;
  152. }
  153. }
  154. if (!usb1_defined) {
  155. mode = getenv("usb_dr_mode");
  156. phy_type = getenv("usb_phy_type");
  157. if (!mode && !phy_type)
  158. return;
  159. fdt_fixup_usb_mode_phy_type(blob, mode, phy_type);
  160. }
  161. }
  162. #endif /* CONFIG_HAS_FSL_DR_USB */
  163. /*
  164. * update crypto node properties to a specified revision of the SEC
  165. * called with sec_rev == 0 if not on an E processor
  166. */
  167. #if CONFIG_SYS_FSL_SEC_COMPAT == 2 /* SEC 2.x/3.x */
  168. void fdt_fixup_crypto_node(void *blob, int sec_rev)
  169. {
  170. const struct sec_rev_prop {
  171. u32 sec_rev;
  172. u32 num_channels;
  173. u32 channel_fifo_len;
  174. u32 exec_units_mask;
  175. u32 descriptor_types_mask;
  176. } sec_rev_prop_list [] = {
  177. { 0x0200, 4, 24, 0x07e, 0x01010ebf }, /* SEC 2.0 */
  178. { 0x0201, 4, 24, 0x0fe, 0x012b0ebf }, /* SEC 2.1 */
  179. { 0x0202, 1, 24, 0x04c, 0x0122003f }, /* SEC 2.2 */
  180. { 0x0204, 4, 24, 0x07e, 0x012b0ebf }, /* SEC 2.4 */
  181. { 0x0300, 4, 24, 0x9fe, 0x03ab0ebf }, /* SEC 3.0 */
  182. { 0x0301, 4, 24, 0xbfe, 0x03ab0ebf }, /* SEC 3.1 */
  183. { 0x0303, 4, 24, 0x97c, 0x03a30abf }, /* SEC 3.3 */
  184. };
  185. char compat_strlist[ARRAY_SIZE(sec_rev_prop_list) *
  186. sizeof("fsl,secX.Y")];
  187. int crypto_node, sec_idx, err;
  188. char *p;
  189. u32 val;
  190. /* locate crypto node based on lowest common compatible */
  191. crypto_node = fdt_node_offset_by_compatible(blob, -1, "fsl,sec2.0");
  192. if (crypto_node == -FDT_ERR_NOTFOUND)
  193. return;
  194. /* delete it if not on an E-processor */
  195. if (crypto_node > 0 && !sec_rev) {
  196. fdt_del_node(blob, crypto_node);
  197. return;
  198. }
  199. /* else we got called for possible uprev */
  200. for (sec_idx = 0; sec_idx < ARRAY_SIZE(sec_rev_prop_list); sec_idx++)
  201. if (sec_rev_prop_list[sec_idx].sec_rev == sec_rev)
  202. break;
  203. if (sec_idx == ARRAY_SIZE(sec_rev_prop_list)) {
  204. puts("warning: unknown SEC revision number\n");
  205. return;
  206. }
  207. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].num_channels);
  208. err = fdt_setprop(blob, crypto_node, "fsl,num-channels", &val, 4);
  209. if (err < 0)
  210. printf("WARNING: could not set crypto property: %s\n",
  211. fdt_strerror(err));
  212. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].descriptor_types_mask);
  213. err = fdt_setprop(blob, crypto_node, "fsl,descriptor-types-mask", &val, 4);
  214. if (err < 0)
  215. printf("WARNING: could not set crypto property: %s\n",
  216. fdt_strerror(err));
  217. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].exec_units_mask);
  218. err = fdt_setprop(blob, crypto_node, "fsl,exec-units-mask", &val, 4);
  219. if (err < 0)
  220. printf("WARNING: could not set crypto property: %s\n",
  221. fdt_strerror(err));
  222. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].channel_fifo_len);
  223. err = fdt_setprop(blob, crypto_node, "fsl,channel-fifo-len", &val, 4);
  224. if (err < 0)
  225. printf("WARNING: could not set crypto property: %s\n",
  226. fdt_strerror(err));
  227. val = 0;
  228. while (sec_idx >= 0) {
  229. p = compat_strlist + val;
  230. val += sprintf(p, "fsl,sec%d.%d",
  231. (sec_rev_prop_list[sec_idx].sec_rev & 0xff00) >> 8,
  232. sec_rev_prop_list[sec_idx].sec_rev & 0x00ff) + 1;
  233. sec_idx--;
  234. }
  235. err = fdt_setprop(blob, crypto_node, "compatible", &compat_strlist, val);
  236. if (err < 0)
  237. printf("WARNING: could not set crypto property: %s\n",
  238. fdt_strerror(err));
  239. }
  240. #elif CONFIG_SYS_FSL_SEC_COMPAT >= 4 /* SEC4 */
  241. void fdt_fixup_crypto_node(void *blob, int sec_rev)
  242. {
  243. if (!sec_rev)
  244. fdt_del_node_and_alias(blob, "crypto");
  245. }
  246. #endif
  247. int fdt_fixup_phy_connection(void *blob, int offset, phy_interface_t phyc)
  248. {
  249. return fdt_setprop_string(blob, offset, "phy-connection-type",
  250. phy_string_for_interface(phyc));
  251. }
  252. #ifdef CONFIG_SYS_SRIO
  253. void ft_srio_setup(void *blob)
  254. {
  255. #ifdef CONFIG_SRIO1
  256. if (!is_serdes_configured(SRIO1)) {
  257. fdt_del_node_and_alias(blob, "rio0");
  258. }
  259. #else
  260. fdt_del_node_and_alias(blob, "rio0");
  261. #endif
  262. #ifdef CONFIG_SRIO2
  263. if (!is_serdes_configured(SRIO2)) {
  264. fdt_del_node_and_alias(blob, "rio1");
  265. }
  266. #else
  267. fdt_del_node_and_alias(blob, "rio1");
  268. #endif
  269. }
  270. #endif