common.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * K3: Common Architecture initialization
  4. *
  5. * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
  6. * Lokesh Vutla <lokeshvutla@ti.com>
  7. */
  8. #include <common.h>
  9. #include <cpu_func.h>
  10. #include <spl.h>
  11. #include "common.h"
  12. #include <dm.h>
  13. #include <remoteproc.h>
  14. #include <asm/cache.h>
  15. #include <linux/soc/ti/ti_sci_protocol.h>
  16. #include <fdt_support.h>
  17. #include <asm/arch/sys_proto.h>
  18. #include <asm/hardware.h>
  19. #include <asm/io.h>
  20. #include <fs_loader.h>
  21. #include <fs.h>
  22. #include <env.h>
  23. #include <elf.h>
  24. struct ti_sci_handle *get_ti_sci_handle(void)
  25. {
  26. struct udevice *dev;
  27. int ret;
  28. ret = uclass_get_device_by_driver(UCLASS_FIRMWARE,
  29. DM_GET_DRIVER(ti_sci), &dev);
  30. if (ret)
  31. panic("Failed to get SYSFW (%d)\n", ret);
  32. return (struct ti_sci_handle *)ti_sci_get_handle_from_sysfw(dev);
  33. }
  34. void k3_sysfw_print_ver(void)
  35. {
  36. struct ti_sci_handle *ti_sci = get_ti_sci_handle();
  37. char fw_desc[sizeof(ti_sci->version.firmware_description) + 1];
  38. /*
  39. * Output System Firmware version info. Note that since the
  40. * 'firmware_description' field is not guaranteed to be zero-
  41. * terminated we manually add a \0 terminator if needed. Further
  42. * note that we intentionally no longer rely on the extended
  43. * printf() formatter '%.*s' to not having to require a more
  44. * full-featured printf() implementation.
  45. */
  46. strncpy(fw_desc, ti_sci->version.firmware_description,
  47. sizeof(ti_sci->version.firmware_description));
  48. fw_desc[sizeof(fw_desc) - 1] = '\0';
  49. printf("SYSFW ABI: %d.%d (firmware rev 0x%04x '%s')\n",
  50. ti_sci->version.abi_major, ti_sci->version.abi_minor,
  51. ti_sci->version.firmware_revision, fw_desc);
  52. }
  53. DECLARE_GLOBAL_DATA_PTR;
  54. #ifdef CONFIG_K3_EARLY_CONS
  55. int early_console_init(void)
  56. {
  57. struct udevice *dev;
  58. int ret;
  59. gd->baudrate = CONFIG_BAUDRATE;
  60. ret = uclass_get_device_by_seq(UCLASS_SERIAL, CONFIG_K3_EARLY_CONS_IDX,
  61. &dev);
  62. if (ret) {
  63. printf("Error getting serial dev for early console! (%d)\n",
  64. ret);
  65. return ret;
  66. }
  67. gd->cur_serial_dev = dev;
  68. gd->flags |= GD_FLG_SERIAL_READY;
  69. gd->have_console = 1;
  70. return 0;
  71. }
  72. #endif
  73. #ifdef CONFIG_SYS_K3_SPL_ATF
  74. void init_env(void)
  75. {
  76. #ifdef CONFIG_SPL_ENV_SUPPORT
  77. char *part;
  78. env_init();
  79. env_relocate();
  80. switch (spl_boot_device()) {
  81. case BOOT_DEVICE_MMC2:
  82. part = env_get("bootpart");
  83. env_set("storage_interface", "mmc");
  84. env_set("fw_dev_part", part);
  85. break;
  86. case BOOT_DEVICE_SPI:
  87. env_set("storage_interface", "ubi");
  88. env_set("fw_ubi_mtdpart", "UBI");
  89. env_set("fw_ubi_volume", "UBI0");
  90. break;
  91. default:
  92. printf("%s from device %u not supported!\n",
  93. __func__, spl_boot_device());
  94. return;
  95. }
  96. #endif
  97. }
  98. #ifdef CONFIG_FS_LOADER
  99. int load_firmware(char *name_fw, char *name_loadaddr, u32 *loadaddr)
  100. {
  101. struct udevice *fsdev;
  102. char *name = NULL;
  103. int size = 0;
  104. *loadaddr = 0;
  105. #ifdef CONFIG_SPL_ENV_SUPPORT
  106. switch (spl_boot_device()) {
  107. case BOOT_DEVICE_MMC2:
  108. name = env_get(name_fw);
  109. *loadaddr = env_get_hex(name_loadaddr, *loadaddr);
  110. break;
  111. default:
  112. printf("Loading rproc fw image from device %u not supported!\n",
  113. spl_boot_device());
  114. return 0;
  115. }
  116. #endif
  117. if (!*loadaddr)
  118. return 0;
  119. if (!uclass_get_device(UCLASS_FS_FIRMWARE_LOADER, 0, &fsdev)) {
  120. size = request_firmware_into_buf(fsdev, name, (void *)*loadaddr,
  121. 0, 0);
  122. }
  123. return size;
  124. }
  125. #else
  126. int load_firmware(char *name_fw, char *name_loadaddr, u32 *loadaddr)
  127. {
  128. return 0;
  129. }
  130. #endif
  131. __weak void start_non_linux_remote_cores(void)
  132. {
  133. }
  134. void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
  135. {
  136. typedef void __noreturn (*image_entry_noargs_t)(void);
  137. struct ti_sci_handle *ti_sci = get_ti_sci_handle();
  138. u32 loadaddr = 0;
  139. int ret, size;
  140. /* Release all the exclusive devices held by SPL before starting ATF */
  141. ti_sci->ops.dev_ops.release_exclusive_devices(ti_sci);
  142. ret = rproc_init();
  143. if (ret)
  144. panic("rproc failed to be initialized (%d)\n", ret);
  145. init_env();
  146. start_non_linux_remote_cores();
  147. size = load_firmware("name_mcur5f0_0fw", "addr_mcur5f0_0load",
  148. &loadaddr);
  149. /*
  150. * It is assumed that remoteproc device 1 is the corresponding
  151. * Cortex-A core which runs ATF. Make sure DT reflects the same.
  152. */
  153. ret = rproc_load(1, spl_image->entry_point, 0x200);
  154. if (ret)
  155. panic("%s: ATF failed to load on rproc (%d)\n", __func__, ret);
  156. /* Add an extra newline to differentiate the ATF logs from SPL */
  157. printf("Starting ATF on ARM64 core...\n\n");
  158. ret = rproc_start(1);
  159. if (ret)
  160. panic("%s: ATF failed to start on rproc (%d)\n", __func__, ret);
  161. if (!(size > 0 && valid_elf_image(loadaddr))) {
  162. debug("Shutting down...\n");
  163. release_resources_for_core_shutdown();
  164. while (1)
  165. asm volatile("wfe");
  166. }
  167. image_entry_noargs_t image_entry =
  168. (image_entry_noargs_t)load_elf_image_phdr(loadaddr);
  169. image_entry();
  170. }
  171. #endif
  172. #if defined(CONFIG_OF_LIBFDT)
  173. int fdt_fixup_msmc_ram(void *blob, char *parent_path, char *node_name)
  174. {
  175. u64 msmc_start = 0, msmc_end = 0, msmc_size, reg[2];
  176. struct ti_sci_handle *ti_sci = get_ti_sci_handle();
  177. int ret, node, subnode, len, prev_node;
  178. u32 range[4], addr, size;
  179. const fdt32_t *sub_reg;
  180. ti_sci->ops.core_ops.query_msmc(ti_sci, &msmc_start, &msmc_end);
  181. msmc_size = msmc_end - msmc_start + 1;
  182. debug("%s: msmc_start = 0x%llx, msmc_size = 0x%llx\n", __func__,
  183. msmc_start, msmc_size);
  184. /* find or create "msmc_sram node */
  185. ret = fdt_path_offset(blob, parent_path);
  186. if (ret < 0)
  187. return ret;
  188. node = fdt_find_or_add_subnode(blob, ret, node_name);
  189. if (node < 0)
  190. return node;
  191. ret = fdt_setprop_string(blob, node, "compatible", "mmio-sram");
  192. if (ret < 0)
  193. return ret;
  194. reg[0] = cpu_to_fdt64(msmc_start);
  195. reg[1] = cpu_to_fdt64(msmc_size);
  196. ret = fdt_setprop(blob, node, "reg", reg, sizeof(reg));
  197. if (ret < 0)
  198. return ret;
  199. fdt_setprop_cell(blob, node, "#address-cells", 1);
  200. fdt_setprop_cell(blob, node, "#size-cells", 1);
  201. range[0] = 0;
  202. range[1] = cpu_to_fdt32(msmc_start >> 32);
  203. range[2] = cpu_to_fdt32(msmc_start & 0xffffffff);
  204. range[3] = cpu_to_fdt32(msmc_size);
  205. ret = fdt_setprop(blob, node, "ranges", range, sizeof(range));
  206. if (ret < 0)
  207. return ret;
  208. subnode = fdt_first_subnode(blob, node);
  209. prev_node = 0;
  210. /* Look for invalid subnodes and delete them */
  211. while (subnode >= 0) {
  212. sub_reg = fdt_getprop(blob, subnode, "reg", &len);
  213. addr = fdt_read_number(sub_reg, 1);
  214. sub_reg++;
  215. size = fdt_read_number(sub_reg, 1);
  216. debug("%s: subnode = %d, addr = 0x%x. size = 0x%x\n", __func__,
  217. subnode, addr, size);
  218. if (addr + size > msmc_size ||
  219. !strncmp(fdt_get_name(blob, subnode, &len), "sysfw", 5) ||
  220. !strncmp(fdt_get_name(blob, subnode, &len), "l3cache", 7)) {
  221. fdt_del_node(blob, subnode);
  222. debug("%s: deleting subnode %d\n", __func__, subnode);
  223. if (!prev_node)
  224. subnode = fdt_first_subnode(blob, node);
  225. else
  226. subnode = fdt_next_subnode(blob, prev_node);
  227. } else {
  228. prev_node = subnode;
  229. subnode = fdt_next_subnode(blob, prev_node);
  230. }
  231. }
  232. return 0;
  233. }
  234. int fdt_disable_node(void *blob, char *node_path)
  235. {
  236. int offs;
  237. int ret;
  238. offs = fdt_path_offset(blob, node_path);
  239. if (offs < 0) {
  240. printf("Node %s not found.\n", node_path);
  241. return offs;
  242. }
  243. ret = fdt_setprop_string(blob, offs, "status", "disabled");
  244. if (ret < 0) {
  245. printf("Could not add status property to node %s: %s\n",
  246. node_path, fdt_strerror(ret));
  247. return ret;
  248. }
  249. return 0;
  250. }
  251. #endif
  252. #ifndef CONFIG_SYSRESET
  253. void reset_cpu(ulong ignored)
  254. {
  255. }
  256. #endif
  257. #if defined(CONFIG_DISPLAY_CPUINFO)
  258. int print_cpuinfo(void)
  259. {
  260. u32 soc, rev;
  261. char *name;
  262. soc = (readl(CTRLMMR_WKUP_JTAG_ID) &
  263. JTAG_ID_PARTNO_MASK) >> JTAG_ID_PARTNO_SHIFT;
  264. rev = (readl(CTRLMMR_WKUP_JTAG_ID) &
  265. JTAG_ID_VARIANT_MASK) >> JTAG_ID_VARIANT_SHIFT;
  266. printf("SoC: ");
  267. switch (soc) {
  268. case AM65X:
  269. name = "AM65x";
  270. break;
  271. case J721E:
  272. name = "J721E";
  273. break;
  274. default:
  275. name = "Unknown Silicon";
  276. };
  277. printf("%s SR ", name);
  278. switch (rev) {
  279. case REV_PG1_0:
  280. name = "1.0";
  281. break;
  282. case REV_PG2_0:
  283. name = "2.0";
  284. break;
  285. default:
  286. name = "Unknown Revision";
  287. };
  288. printf("%s\n", name);
  289. return 0;
  290. }
  291. #endif
  292. #ifdef CONFIG_ARM64
  293. void board_prep_linux(bootm_headers_t *images)
  294. {
  295. debug("Linux kernel Image start = 0x%lx end = 0x%lx\n",
  296. images->os.start, images->os.end);
  297. __asm_flush_dcache_range(images->os.start,
  298. ROUND(images->os.end,
  299. CONFIG_SYS_CACHELINE_SIZE));
  300. }
  301. #endif
  302. #ifdef CONFIG_CPU_V7R
  303. void disable_linefill_optimization(void)
  304. {
  305. u32 actlr;
  306. /*
  307. * On K3 devices there are 2 conditions where R5F can deadlock:
  308. * 1.When software is performing series of store operations to
  309. * cacheable write back/write allocate memory region and later
  310. * on software execute barrier operation (DSB or DMB). R5F may
  311. * hang at the barrier instruction.
  312. * 2.When software is performing a mix of load and store operations
  313. * within a tight loop and store operations are all writing to
  314. * cacheable write back/write allocates memory regions, R5F may
  315. * hang at one of the load instruction.
  316. *
  317. * To avoid the above two conditions disable linefill optimization
  318. * inside Cortex R5F.
  319. */
  320. asm("mrc p15, 0, %0, c1, c0, 1" : "=r" (actlr));
  321. actlr |= (1 << 13); /* Set DLFO bit */
  322. asm("mcr p15, 0, %0, c1, c0, 1" : : "r" (actlr));
  323. }
  324. #endif
  325. void remove_fwl_configs(struct fwl_data *fwl_data, size_t fwl_data_size)
  326. {
  327. struct ti_sci_msg_fwl_region region;
  328. struct ti_sci_fwl_ops *fwl_ops;
  329. struct ti_sci_handle *ti_sci;
  330. size_t i, j;
  331. ti_sci = get_ti_sci_handle();
  332. fwl_ops = &ti_sci->ops.fwl_ops;
  333. for (i = 0; i < fwl_data_size; i++) {
  334. for (j = 0; j < fwl_data[i].regions; j++) {
  335. region.fwl_id = fwl_data[i].fwl_id;
  336. region.region = j;
  337. region.n_permission_regs = 3;
  338. fwl_ops->get_fwl_region(ti_sci, &region);
  339. if (region.control != 0) {
  340. pr_debug("Attempting to disable firewall %5d (%25s)\n",
  341. region.fwl_id, fwl_data[i].name);
  342. region.control = 0;
  343. if (fwl_ops->set_fwl_region(ti_sci, &region))
  344. pr_err("Could not disable firewall %5d (%25s)\n",
  345. region.fwl_id, fwl_data[i].name);
  346. }
  347. }
  348. }
  349. }