1 // SPDX-License-Identifier: GPL-2.0+
3 * K3: Common Architecture initialization
5 * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
6 * Lokesh Vutla <lokeshvutla@ti.com>
14 #include <asm/global_data.h>
15 #include <linux/printk.h>
18 #include <remoteproc.h>
19 #include <asm/cache.h>
20 #include <linux/soc/ti/ti_sci_protocol.h>
21 #include <fdt_support.h>
22 #include <asm/hardware.h>
24 #include <fs_loader.h>
30 #if IS_ENABLED(CONFIG_SYS_K3_SPL_ATF)
39 #if CONFIG_IS_ENABLED(FIT_IMAGE_POST_PROCESS)
40 static const char *image_os_match[IMAGE_AMT] = {
41 "arm-trusted-firmware",
48 static struct image_info fit_image_info[IMAGE_AMT];
51 struct ti_sci_handle *get_ti_sci_handle(void)
56 ret = uclass_get_device_by_driver(UCLASS_FIRMWARE,
57 DM_DRIVER_GET(ti_sci), &dev);
59 panic("Failed to get SYSFW (%d)\n", ret);
61 return (struct ti_sci_handle *)ti_sci_get_handle_from_sysfw(dev);
64 void k3_sysfw_print_ver(void)
66 struct ti_sci_handle *ti_sci = get_ti_sci_handle();
67 char fw_desc[sizeof(ti_sci->version.firmware_description) + 1];
70 * Output System Firmware version info. Note that since the
71 * 'firmware_description' field is not guaranteed to be zero-
72 * terminated we manually add a \0 terminator if needed. Further
73 * note that we intentionally no longer rely on the extended
74 * printf() formatter '%.*s' to not having to require a more
75 * full-featured printf() implementation.
77 strncpy(fw_desc, ti_sci->version.firmware_description,
78 sizeof(ti_sci->version.firmware_description));
79 fw_desc[sizeof(fw_desc) - 1] = '\0';
81 printf("SYSFW ABI: %d.%d (firmware rev 0x%04x '%s')\n",
82 ti_sci->version.abi_major, ti_sci->version.abi_minor,
83 ti_sci->version.firmware_revision, fw_desc);
86 void mmr_unlock(uintptr_t base, u32 partition)
88 /* Translate the base address */
89 uintptr_t part_base = base + partition * CTRL_MMR0_PARTITION_SIZE;
91 /* Unlock the requested partition if locked using two-step sequence */
92 writel(CTRLMMR_LOCK_KICK0_UNLOCK_VAL, part_base + CTRLMMR_LOCK_KICK0);
93 writel(CTRLMMR_LOCK_KICK1_UNLOCK_VAL, part_base + CTRLMMR_LOCK_KICK1);
96 bool is_rom_loaded_sysfw(struct rom_extended_boot_data *data)
98 if (strncmp(data->header, K3_ROM_BOOT_HEADER_MAGIC, 7))
101 return data->num_components > 1;
104 DECLARE_GLOBAL_DATA_PTR;
106 #ifdef CONFIG_K3_EARLY_CONS
107 int early_console_init(void)
112 gd->baudrate = CONFIG_BAUDRATE;
114 ret = uclass_get_device_by_seq(UCLASS_SERIAL, CONFIG_K3_EARLY_CONS_IDX,
117 printf("Error getting serial dev for early console! (%d)\n",
122 gd->cur_serial_dev = dev;
123 gd->flags |= GD_FLG_SERIAL_READY;
124 gd->have_console = 1;
130 #if IS_ENABLED(CONFIG_SYS_K3_SPL_ATF)
134 #ifdef CONFIG_SPL_ENV_SUPPORT
139 switch (spl_boot_device()) {
140 case BOOT_DEVICE_MMC2:
141 part = env_get("bootpart");
142 env_set("storage_interface", "mmc");
143 env_set("fw_dev_part", part);
145 case BOOT_DEVICE_SPI:
146 env_set("storage_interface", "ubi");
147 env_set("fw_ubi_mtdpart", "UBI");
148 env_set("fw_ubi_volume", "UBI0");
151 printf("%s from device %u not supported!\n",
152 __func__, spl_boot_device());
158 int load_firmware(char *name_fw, char *name_loadaddr, u32 *loadaddr)
160 struct udevice *fsdev;
164 if (!IS_ENABLED(CONFIG_FS_LOADER))
168 #ifdef CONFIG_SPL_ENV_SUPPORT
169 switch (spl_boot_device()) {
170 case BOOT_DEVICE_MMC2:
171 name = env_get(name_fw);
172 *loadaddr = env_get_hex(name_loadaddr, *loadaddr);
175 printf("Loading rproc fw image from device %u not supported!\n",
183 if (!get_fs_loader(&fsdev)) {
184 size = request_firmware_into_buf(fsdev, name, (void *)*loadaddr,
191 void release_resources_for_core_shutdown(void)
193 struct ti_sci_handle *ti_sci = get_ti_sci_handle();
194 struct ti_sci_dev_ops *dev_ops = &ti_sci->ops.dev_ops;
195 struct ti_sci_proc_ops *proc_ops = &ti_sci->ops.proc_ops;
199 /* Iterate through list of devices to put (shutdown) */
200 for (i = 0; i < ARRAY_SIZE(put_device_ids); i++) {
201 u32 id = put_device_ids[i];
203 ret = dev_ops->put_device(ti_sci, id);
205 panic("Failed to put device %u (%d)\n", id, ret);
208 /* Iterate through list of cores to put (shutdown) */
209 for (i = 0; i < ARRAY_SIZE(put_core_ids); i++) {
210 u32 id = put_core_ids[i];
213 * Queue up the core shutdown request. Note that this call
214 * needs to be followed up by an actual invocation of an WFE
215 * or WFI CPU instruction.
217 ret = proc_ops->proc_shutdown_no_wait(ti_sci, id);
219 panic("Failed sending core %u shutdown message (%d)\n",
224 void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
226 typedef void __noreturn (*image_entry_noargs_t)(void);
227 struct ti_sci_handle *ti_sci = get_ti_sci_handle();
229 int ret, size = 0, shut_cpu = 0;
231 /* Release all the exclusive devices held by SPL before starting ATF */
232 ti_sci->ops.dev_ops.release_exclusive_devices(ti_sci);
236 panic("rproc failed to be initialized (%d)\n", ret);
240 if (!fit_image_info[IMAGE_ID_DM_FW].image_start) {
241 size = load_firmware("name_mcur5f0_0fw", "addr_mcur5f0_0load",
246 * It is assumed that remoteproc device 1 is the corresponding
247 * Cortex-A core which runs ATF. Make sure DT reflects the same.
249 if (!fit_image_info[IMAGE_ID_ATF].image_start)
250 fit_image_info[IMAGE_ID_ATF].image_start =
251 spl_image->entry_point;
253 ret = rproc_load(1, fit_image_info[IMAGE_ID_ATF].image_start, 0x200);
255 panic("%s: ATF failed to load on rproc (%d)\n", __func__, ret);
257 #if (CONFIG_IS_ENABLED(FIT_IMAGE_POST_PROCESS) && IS_ENABLED(CONFIG_SYS_K3_SPL_ATF))
258 /* Authenticate ATF */
259 void *image_addr = (void *)fit_image_info[IMAGE_ID_ATF].image_start;
261 debug("%s: Authenticating image: addr=%lx, size=%ld, os=%s\n", __func__,
262 fit_image_info[IMAGE_ID_ATF].image_start,
263 fit_image_info[IMAGE_ID_ATF].image_len,
264 image_os_match[IMAGE_ID_ATF]);
266 ti_secure_image_post_process(&image_addr,
267 (size_t *)&fit_image_info[IMAGE_ID_ATF].image_len);
269 /* Authenticate OPTEE */
270 image_addr = (void *)fit_image_info[IMAGE_ID_OPTEE].image_start;
272 debug("%s: Authenticating image: addr=%lx, size=%ld, os=%s\n", __func__,
273 fit_image_info[IMAGE_ID_OPTEE].image_start,
274 fit_image_info[IMAGE_ID_OPTEE].image_len,
275 image_os_match[IMAGE_ID_OPTEE]);
277 ti_secure_image_post_process(&image_addr,
278 (size_t *)&fit_image_info[IMAGE_ID_OPTEE].image_len);
282 if (!fit_image_info[IMAGE_ID_DM_FW].image_len &&
283 !(size > 0 && valid_elf_image(loadaddr))) {
288 if (!fit_image_info[IMAGE_ID_DM_FW].image_start) {
289 loadaddr = load_elf_image_phdr(loadaddr);
291 loadaddr = fit_image_info[IMAGE_ID_DM_FW].image_start;
292 if (valid_elf_image(loadaddr))
293 loadaddr = load_elf_image_phdr(loadaddr);
296 debug("%s: jumping to address %x\n", __func__, loadaddr);
299 /* Add an extra newline to differentiate the ATF logs from SPL */
300 printf("Starting ATF on ARM64 core...\n\n");
302 ret = rproc_start(1);
304 panic("%s: ATF failed to start on rproc (%d)\n", __func__, ret);
307 debug("Shutting down...\n");
308 release_resources_for_core_shutdown();
313 image_entry_noargs_t image_entry = (image_entry_noargs_t)loadaddr;
319 #if CONFIG_IS_ENABLED(FIT_IMAGE_POST_PROCESS)
320 void board_fit_image_post_process(const void *fit, int node, void **p_image,
323 #if IS_ENABLED(CONFIG_SYS_K3_SPL_ATF)
329 os = fdt_getprop(fit, node, "os", &len);
330 addr = fdt_getprop_u32_default_node(fit, node, 0, "entry", -1);
332 debug("%s: processing image: addr=%x, size=%d, os=%s\n", __func__,
335 for (i = 0; i < IMAGE_AMT; i++) {
336 if (!strcmp(os, image_os_match[i])) {
337 fit_image_info[i].image_start = addr;
338 fit_image_info[i].image_len = *p_size;
339 debug("%s: matched image for ID %d\n", __func__, i);
344 * Only DM and the DTBs are being authenticated here,
345 * rest will be authenticated when A72 cluster is up
347 if ((i != IMAGE_ID_ATF) && (i != IMAGE_ID_OPTEE))
350 ti_secure_image_check_binary(p_image, p_size);
351 ti_secure_image_post_process(p_image, p_size);
353 #if IS_ENABLED(CONFIG_SYS_K3_SPL_ATF)
355 ti_secure_image_check_binary(p_image, p_size);
360 #ifndef CONFIG_SYSRESET
366 enum k3_device_type get_device_type(void)
368 u32 sys_status = readl(K3_SEC_MGR_SYS_STATUS);
370 u32 sys_dev_type = (sys_status & SYS_STATUS_DEV_TYPE_MASK) >>
371 SYS_STATUS_DEV_TYPE_SHIFT;
373 u32 sys_sub_type = (sys_status & SYS_STATUS_SUB_TYPE_MASK) >>
374 SYS_STATUS_SUB_TYPE_SHIFT;
376 switch (sys_dev_type) {
377 case SYS_STATUS_DEV_TYPE_GP:
378 return K3_DEVICE_TYPE_GP;
379 case SYS_STATUS_DEV_TYPE_TEST:
380 return K3_DEVICE_TYPE_TEST;
381 case SYS_STATUS_DEV_TYPE_EMU:
382 return K3_DEVICE_TYPE_EMU;
383 case SYS_STATUS_DEV_TYPE_HS:
384 if (sys_sub_type == SYS_STATUS_SUB_TYPE_VAL_FS)
385 return K3_DEVICE_TYPE_HS_FS;
387 return K3_DEVICE_TYPE_HS_SE;
389 return K3_DEVICE_TYPE_BAD;
393 #if defined(CONFIG_DISPLAY_CPUINFO)
394 static const char *get_device_type_name(void)
396 enum k3_device_type type = get_device_type();
399 case K3_DEVICE_TYPE_GP:
401 case K3_DEVICE_TYPE_TEST:
403 case K3_DEVICE_TYPE_EMU:
405 case K3_DEVICE_TYPE_HS_FS:
407 case K3_DEVICE_TYPE_HS_SE:
414 int print_cpuinfo(void)
428 ret = soc_get_family(soc, name, 64);
433 ret = soc_get_revision(soc, name, 64);
438 printf("%s\n", get_device_type_name());
445 void board_prep_linux(struct bootm_headers *images)
447 debug("Linux kernel Image start = 0x%lx end = 0x%lx\n",
448 images->os.start, images->os.end);
449 __asm_flush_dcache_range(images->os.start,
450 ROUND(images->os.end,
451 CONFIG_SYS_CACHELINE_SIZE));
455 #ifdef CONFIG_CPU_V7R
456 void disable_linefill_optimization(void)
461 * On K3 devices there are 2 conditions where R5F can deadlock:
462 * 1.When software is performing series of store operations to
463 * cacheable write back/write allocate memory region and later
464 * on software execute barrier operation (DSB or DMB). R5F may
465 * hang at the barrier instruction.
466 * 2.When software is performing a mix of load and store operations
467 * within a tight loop and store operations are all writing to
468 * cacheable write back/write allocates memory regions, R5F may
469 * hang at one of the load instruction.
471 * To avoid the above two conditions disable linefill optimization
474 asm("mrc p15, 0, %0, c1, c0, 1" : "=r" (actlr));
475 actlr |= (1 << 13); /* Set DLFO bit */
476 asm("mcr p15, 0, %0, c1, c0, 1" : : "r" (actlr));
480 static void remove_fwl_regions(struct fwl_data fwl_data, size_t num_regions,
481 enum k3_firewall_region_type fwl_type)
483 struct ti_sci_fwl_ops *fwl_ops;
484 struct ti_sci_handle *ti_sci;
485 struct ti_sci_msg_fwl_region region;
488 ti_sci = get_ti_sci_handle();
489 fwl_ops = &ti_sci->ops.fwl_ops;
491 for (j = 0; j < fwl_data.regions; j++) {
492 region.fwl_id = fwl_data.fwl_id;
494 region.n_permission_regs = 3;
496 fwl_ops->get_fwl_region(ti_sci, ®ion);
498 /* Don't disable the background regions */
499 if (region.control != 0 &&
500 ((region.control >> K3_FIREWALL_BACKGROUND_BIT) & 1) == fwl_type) {
501 pr_debug("Attempting to disable firewall %5d (%25s)\n",
502 region.fwl_id, fwl_data.name);
505 if (fwl_ops->set_fwl_region(ti_sci, ®ion))
506 pr_err("Could not disable firewall %5d (%25s)\n",
507 region.fwl_id, fwl_data.name);
512 void remove_fwl_configs(struct fwl_data *fwl_data, size_t fwl_data_size)
516 for (i = 0; i < fwl_data_size; i++) {
517 remove_fwl_regions(fwl_data[i], fwl_data[i].regions,
518 K3_FIREWALL_REGION_FOREGROUND);
519 remove_fwl_regions(fwl_data[i], fwl_data[i].regions,
520 K3_FIREWALL_REGION_BACKGROUND);
524 void spl_enable_dcache(void)
526 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
527 phys_addr_t ram_top = CFG_SYS_SDRAM_BASE;
531 /* reserve TLB table */
532 gd->arch.tlb_size = PGTABLE_SIZE;
534 ram_top += get_effective_memsize();
535 /* keep ram_top in the 32-bit address space */
536 if (ram_top >= 0x100000000)
537 ram_top = (phys_addr_t) 0x100000000;
539 gd->arch.tlb_addr = ram_top - gd->arch.tlb_size;
540 gd->arch.tlb_addr &= ~(0x10000 - 1);
541 debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
542 gd->arch.tlb_addr + gd->arch.tlb_size);
543 gd->relocaddr = gd->arch.tlb_addr;
549 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
550 void spl_board_prepare_for_boot(void)
555 void spl_board_prepare_for_linux(void)
561 int misc_init_r(void)
563 if (IS_ENABLED(CONFIG_TI_AM65_CPSW_NUSS)) {
567 ret = uclass_get_device_by_driver(UCLASS_MISC,
568 DM_DRIVER_GET(am65_cpsw_nuss),
571 printf("Failed to probe am65_cpsw_nuss driver\n");
574 /* Default FIT boot on HS-SE devices */
575 if (get_device_type() == K3_DEVICE_TYPE_HS_SE)
576 env_set("boot_fit", "1");
582 * do_board_detect() - Detect board description
584 * Function to detect board description. This is expected to be
585 * overridden in the SoC family board file where desired.
587 void __weak do_board_detect(void)