fdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright 2009-2012 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. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <libfdt.h>
  11. #include <fdt_support.h>
  12. #include <asm/mp.h>
  13. #include <asm/fsl_serdes.h>
  14. #include <phy.h>
  15. #include <hwconfig.h>
  16. #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
  17. #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
  18. #endif
  19. #if defined(CONFIG_MP) && (defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx))
  20. static int ft_del_cpuhandle(void *blob, int cpuhandle)
  21. {
  22. int off, ret = -FDT_ERR_NOTFOUND;
  23. /* if we find a match, we'll delete at it which point the offsets are
  24. * invalid so we start over from the beginning
  25. */
  26. off = fdt_node_offset_by_prop_value(blob, -1, "cpu-handle",
  27. &cpuhandle, 4);
  28. while (off != -FDT_ERR_NOTFOUND) {
  29. fdt_delprop(blob, off, "cpu-handle");
  30. ret = 1;
  31. off = fdt_node_offset_by_prop_value(blob, -1, "cpu-handle",
  32. &cpuhandle, 4);
  33. }
  34. return ret;
  35. }
  36. void ft_fixup_num_cores(void *blob) {
  37. int off, num_cores, del_cores;
  38. del_cores = 0;
  39. num_cores = cpu_numcores();
  40. off = fdt_node_offset_by_prop_value(blob, -1, "device_type", "cpu", 4);
  41. while (off != -FDT_ERR_NOTFOUND) {
  42. u32 *reg = (u32 *)fdt_getprop(blob, off, "reg", 0);
  43. u32 phys_cpu_id = thread_to_core(*reg);
  44. if (!is_core_valid(phys_cpu_id) || is_core_disabled(phys_cpu_id)) {
  45. int ph = fdt_get_phandle(blob, off);
  46. /* Delete the cpu node once there are no cpu handles */
  47. if (-FDT_ERR_NOTFOUND == ft_del_cpuhandle(blob, ph)) {
  48. fdt_del_node(blob, off);
  49. del_cores++;
  50. }
  51. /* either we deleted some cpu handles or the cpu node
  52. * so we reset the offset back to the start since we
  53. * can't trust the offsets anymore
  54. */
  55. off = -1;
  56. }
  57. off = fdt_node_offset_by_prop_value(blob, off,
  58. "device_type", "cpu", 4);
  59. }
  60. debug ("%x core system found\n", num_cores);
  61. debug ("deleted %d extra core entry entries from device tree\n",
  62. del_cores);
  63. }
  64. #endif /* defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx) */
  65. #if defined(CONFIG_HAS_FSL_DR_USB) || defined(CONFIG_HAS_FSL_MPH_USB)
  66. static int fdt_fixup_usb_mode_phy_type(void *blob, const char *mode,
  67. const char *phy_type, int start_offset)
  68. {
  69. const char *compat_dr = "fsl-usb2-dr";
  70. const char *compat_mph = "fsl-usb2-mph";
  71. const char *prop_mode = "dr_mode";
  72. const char *prop_type = "phy_type";
  73. const char *node_type = NULL;
  74. int node_offset;
  75. int err;
  76. node_offset = fdt_node_offset_by_compatible(blob,
  77. start_offset, compat_mph);
  78. if (node_offset < 0) {
  79. node_offset = fdt_node_offset_by_compatible(blob,
  80. start_offset, compat_dr);
  81. if (node_offset < 0) {
  82. printf("WARNING: could not find compatible"
  83. " node %s or %s: %s.\n", compat_mph,
  84. compat_dr, fdt_strerror(node_offset));
  85. return -1;
  86. } else
  87. node_type = compat_dr;
  88. } else
  89. node_type = compat_mph;
  90. if (mode) {
  91. err = fdt_setprop(blob, node_offset, prop_mode, mode,
  92. strlen(mode) + 1);
  93. if (err < 0)
  94. printf("WARNING: could not set %s for %s: %s.\n",
  95. prop_mode, node_type, fdt_strerror(err));
  96. }
  97. if (phy_type) {
  98. err = fdt_setprop(blob, node_offset, prop_type, phy_type,
  99. strlen(phy_type) + 1);
  100. if (err < 0)
  101. printf("WARNING: could not set %s for %s: %s.\n",
  102. prop_type, node_type, fdt_strerror(err));
  103. }
  104. return node_offset;
  105. }
  106. void fdt_fixup_dr_usb(void *blob, bd_t *bd)
  107. {
  108. const char *modes[] = { "host", "peripheral", "otg" };
  109. const char *phys[] = { "ulpi", "utmi" };
  110. const char *dr_mode_type = NULL;
  111. const char *dr_phy_type = NULL;
  112. int usb_mode_off = -1;
  113. int usb_phy_off = -1;
  114. char str[5];
  115. int i, j;
  116. for (i = 1; i <= CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
  117. int mode_idx = -1, phy_idx = -1;
  118. snprintf(str, 5, "%s%d", "usb", i);
  119. if (hwconfig(str)) {
  120. for (j = 0; j < ARRAY_SIZE(modes); j++) {
  121. if (hwconfig_subarg_cmp(str, "dr_mode",
  122. modes[j])) {
  123. mode_idx = j;
  124. break;
  125. }
  126. }
  127. for (j = 0; j < ARRAY_SIZE(phys); j++) {
  128. if (hwconfig_subarg_cmp(str, "phy_type",
  129. phys[j])) {
  130. phy_idx = j;
  131. break;
  132. }
  133. }
  134. if (mode_idx < 0 || phy_idx < 0) {
  135. puts("ERROR: wrong usb mode/phy defined!!\n");
  136. return;
  137. }
  138. dr_mode_type = modes[mode_idx];
  139. dr_phy_type = phys[phy_idx];
  140. if (mode_idx < 0 && phy_idx < 0) {
  141. printf("WARNING: invalid phy or mode\n");
  142. return;
  143. }
  144. }
  145. usb_mode_off = fdt_fixup_usb_mode_phy_type(blob,
  146. dr_mode_type, NULL, usb_mode_off);
  147. if (usb_mode_off < 0)
  148. return;
  149. usb_phy_off = fdt_fixup_usb_mode_phy_type(blob,
  150. NULL, dr_phy_type, usb_phy_off);
  151. if (usb_phy_off < 0)
  152. return;
  153. }
  154. }
  155. #endif /* defined(CONFIG_HAS_FSL_DR_USB) || defined(CONFIG_HAS_FSL_MPH_USB) */
  156. /*
  157. * update crypto node properties to a specified revision of the SEC
  158. * called with sec_rev == 0 if not on an E processor
  159. */
  160. #if CONFIG_SYS_FSL_SEC_COMPAT == 2 /* SEC 2.x/3.x */
  161. void fdt_fixup_crypto_node(void *blob, int sec_rev)
  162. {
  163. static const struct sec_rev_prop {
  164. u32 sec_rev;
  165. u32 num_channels;
  166. u32 channel_fifo_len;
  167. u32 exec_units_mask;
  168. u32 descriptor_types_mask;
  169. } sec_rev_prop_list [] = {
  170. { 0x0200, 4, 24, 0x07e, 0x01010ebf }, /* SEC 2.0 */
  171. { 0x0201, 4, 24, 0x0fe, 0x012b0ebf }, /* SEC 2.1 */
  172. { 0x0202, 1, 24, 0x04c, 0x0122003f }, /* SEC 2.2 */
  173. { 0x0204, 4, 24, 0x07e, 0x012b0ebf }, /* SEC 2.4 */
  174. { 0x0300, 4, 24, 0x9fe, 0x03ab0ebf }, /* SEC 3.0 */
  175. { 0x0301, 4, 24, 0xbfe, 0x03ab0ebf }, /* SEC 3.1 */
  176. { 0x0303, 4, 24, 0x97c, 0x03a30abf }, /* SEC 3.3 */
  177. };
  178. static char compat_strlist[ARRAY_SIZE(sec_rev_prop_list) *
  179. sizeof("fsl,secX.Y")];
  180. int crypto_node, sec_idx, err;
  181. char *p;
  182. u32 val;
  183. /* locate crypto node based on lowest common compatible */
  184. crypto_node = fdt_node_offset_by_compatible(blob, -1, "fsl,sec2.0");
  185. if (crypto_node == -FDT_ERR_NOTFOUND)
  186. return;
  187. /* delete it if not on an E-processor */
  188. if (crypto_node > 0 && !sec_rev) {
  189. fdt_del_node(blob, crypto_node);
  190. return;
  191. }
  192. /* else we got called for possible uprev */
  193. for (sec_idx = 0; sec_idx < ARRAY_SIZE(sec_rev_prop_list); sec_idx++)
  194. if (sec_rev_prop_list[sec_idx].sec_rev == sec_rev)
  195. break;
  196. if (sec_idx == ARRAY_SIZE(sec_rev_prop_list)) {
  197. puts("warning: unknown SEC revision number\n");
  198. return;
  199. }
  200. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].num_channels);
  201. err = fdt_setprop(blob, crypto_node, "fsl,num-channels", &val, 4);
  202. if (err < 0)
  203. printf("WARNING: could not set crypto property: %s\n",
  204. fdt_strerror(err));
  205. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].descriptor_types_mask);
  206. err = fdt_setprop(blob, crypto_node, "fsl,descriptor-types-mask", &val, 4);
  207. if (err < 0)
  208. printf("WARNING: could not set crypto property: %s\n",
  209. fdt_strerror(err));
  210. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].exec_units_mask);
  211. err = fdt_setprop(blob, crypto_node, "fsl,exec-units-mask", &val, 4);
  212. if (err < 0)
  213. printf("WARNING: could not set crypto property: %s\n",
  214. fdt_strerror(err));
  215. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].channel_fifo_len);
  216. err = fdt_setprop(blob, crypto_node, "fsl,channel-fifo-len", &val, 4);
  217. if (err < 0)
  218. printf("WARNING: could not set crypto property: %s\n",
  219. fdt_strerror(err));
  220. val = 0;
  221. while (sec_idx >= 0) {
  222. p = compat_strlist + val;
  223. val += sprintf(p, "fsl,sec%d.%d",
  224. (sec_rev_prop_list[sec_idx].sec_rev & 0xff00) >> 8,
  225. sec_rev_prop_list[sec_idx].sec_rev & 0x00ff) + 1;
  226. sec_idx--;
  227. }
  228. err = fdt_setprop(blob, crypto_node, "compatible", &compat_strlist, val);
  229. if (err < 0)
  230. printf("WARNING: could not set crypto property: %s\n",
  231. fdt_strerror(err));
  232. }
  233. #elif CONFIG_SYS_FSL_SEC_COMPAT >= 4 /* SEC4 */
  234. static u8 caam_get_era(void)
  235. {
  236. static const struct {
  237. u16 ip_id;
  238. u8 maj_rev;
  239. u8 era;
  240. } caam_eras[] = {
  241. {0x0A10, 1, 1},
  242. {0x0A10, 2, 2},
  243. {0x0A12, 1, 3},
  244. {0x0A14, 1, 3},
  245. {0x0A14, 2, 4},
  246. {0x0A16, 1, 4},
  247. {0x0A10, 3, 4},
  248. {0x0A11, 1, 4},
  249. {0x0A18, 1, 4},
  250. {0x0A11, 2, 5},
  251. {0x0A12, 2, 5},
  252. {0x0A13, 1, 5},
  253. {0x0A1C, 1, 5}
  254. };
  255. ccsr_sec_t __iomem *sec = (void __iomem *)CONFIG_SYS_FSL_SEC_ADDR;
  256. u32 secvid_ms = in_be32(&sec->secvid_ms);
  257. u32 ccbvid = in_be32(&sec->ccbvid);
  258. u16 ip_id = (secvid_ms & SEC_SECVID_MS_IPID_MASK) >>
  259. SEC_SECVID_MS_IPID_SHIFT;
  260. u8 maj_rev = (secvid_ms & SEC_SECVID_MS_MAJ_REV_MASK) >>
  261. SEC_SECVID_MS_MAJ_REV_SHIFT;
  262. u8 era = (ccbvid & SEC_CCBVID_ERA_MASK) >> SEC_CCBVID_ERA_SHIFT;
  263. int i;
  264. if (era) /* This is '0' prior to CAAM ERA-6 */
  265. return era;
  266. for (i = 0; i < ARRAY_SIZE(caam_eras); i++)
  267. if (caam_eras[i].ip_id == ip_id &&
  268. caam_eras[i].maj_rev == maj_rev)
  269. return caam_eras[i].era;
  270. return 0;
  271. }
  272. static void fdt_fixup_crypto_era(void *blob, u32 era)
  273. {
  274. int err;
  275. int crypto_node;
  276. crypto_node = fdt_path_offset(blob, "crypto");
  277. if (crypto_node < 0) {
  278. printf("WARNING: Missing crypto node\n");
  279. return;
  280. }
  281. err = fdt_setprop(blob, crypto_node, "fsl,sec-era", &era,
  282. sizeof(era));
  283. if (err < 0) {
  284. printf("ERROR: could not set fsl,sec-era property: %s\n",
  285. fdt_strerror(err));
  286. }
  287. }
  288. void fdt_fixup_crypto_node(void *blob, int sec_rev)
  289. {
  290. u8 era;
  291. if (!sec_rev) {
  292. fdt_del_node_and_alias(blob, "crypto");
  293. return;
  294. }
  295. /* Add SEC ERA information in compatible */
  296. era = caam_get_era();
  297. if (era) {
  298. fdt_fixup_crypto_era(blob, era);
  299. } else {
  300. printf("WARNING: Unable to get ERA for CAAM rev: %d\n",
  301. sec_rev);
  302. }
  303. }
  304. #endif
  305. int fdt_fixup_phy_connection(void *blob, int offset, phy_interface_t phyc)
  306. {
  307. return fdt_setprop_string(blob, offset, "phy-connection-type",
  308. phy_string_for_interface(phyc));
  309. }
  310. #ifdef CONFIG_SYS_SRIO
  311. static inline void ft_disable_srio_port(void *blob, int srio_off, int port)
  312. {
  313. int off = fdt_node_offset_by_prop_value(blob, srio_off,
  314. "cell-index", &port, 4);
  315. if (off >= 0) {
  316. off = fdt_setprop_string(blob, off, "status", "disabled");
  317. if (off > 0)
  318. printf("WARNING unable to set status for fsl,srio "
  319. "port %d: %s\n", port, fdt_strerror(off));
  320. }
  321. }
  322. static inline void ft_disable_rman(void *blob)
  323. {
  324. int off = fdt_node_offset_by_compatible(blob, -1, "fsl,rman");
  325. if (off >= 0) {
  326. off = fdt_setprop_string(blob, off, "status", "disabled");
  327. if (off > 0)
  328. printf("WARNING unable to set status for fsl,rman %s\n",
  329. fdt_strerror(off));
  330. }
  331. }
  332. static inline void ft_disable_rmu(void *blob)
  333. {
  334. int off = fdt_node_offset_by_compatible(blob, -1, "fsl,srio-rmu");
  335. if (off >= 0) {
  336. off = fdt_setprop_string(blob, off, "status", "disabled");
  337. if (off > 0)
  338. printf("WARNING unable to set status for "
  339. "fsl,srio-rmu %s\n", fdt_strerror(off));
  340. }
  341. }
  342. void ft_srio_setup(void *blob)
  343. {
  344. int srio1_used = 0, srio2_used = 0;
  345. int srio_off;
  346. /* search for srio node, if doesn't exist just return - nothing todo */
  347. srio_off = fdt_node_offset_by_compatible(blob, -1, "fsl,srio");
  348. if (srio_off < 0)
  349. return ;
  350. #ifdef CONFIG_SRIO1
  351. if (is_serdes_configured(SRIO1))
  352. srio1_used = 1;
  353. #endif
  354. #ifdef CONFIG_SRIO2
  355. if (is_serdes_configured(SRIO2))
  356. srio2_used = 1;
  357. #endif
  358. /* mark port1 disabled */
  359. if (!srio1_used)
  360. ft_disable_srio_port(blob, srio_off, 1);
  361. /* mark port2 disabled */
  362. if (!srio2_used)
  363. ft_disable_srio_port(blob, srio_off, 2);
  364. /* if both ports not used, disable controller, rmu and rman */
  365. if (!srio1_used && !srio2_used) {
  366. fdt_setprop_string(blob, srio_off, "status", "disabled");
  367. ft_disable_rman(blob);
  368. ft_disable_rmu(blob);
  369. }
  370. }
  371. #endif