bl2_plat_setup.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /*
  2. * Copyright (c) 2018-2023, Renesas Electronics Corporation. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <inttypes.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <libfdt.h>
  10. #include <platform_def.h>
  11. #include <arch_helpers.h>
  12. #include <bl1/bl1.h>
  13. #include <common/bl_common.h>
  14. #include <common/debug.h>
  15. #include <common/desc_image_load.h>
  16. #include <common/image_decompress.h>
  17. #include <drivers/console.h>
  18. #include <drivers/io/io_driver.h>
  19. #include <drivers/io/io_storage.h>
  20. #include <lib/mmio.h>
  21. #include <lib/xlat_tables/xlat_tables_defs.h>
  22. #include <plat/common/platform.h>
  23. #if RCAR_GEN3_BL33_GZIP == 1
  24. #include <tf_gunzip.h>
  25. #endif
  26. #include "avs_driver.h"
  27. #include "boot_init_dram.h"
  28. #include "cpg_registers.h"
  29. #include "board.h"
  30. #include "emmc_def.h"
  31. #include "emmc_hal.h"
  32. #include "emmc_std.h"
  33. #if PMIC_ROHM_BD9571 && RCAR_SYSTEM_RESET_KEEPON_DDR
  34. #include "iic_dvfs.h"
  35. #endif
  36. #include "io_common.h"
  37. #include "io_rcar.h"
  38. #include "qos_init.h"
  39. #include "rcar_def.h"
  40. #include "rcar_private.h"
  41. #include "rcar_version.h"
  42. #include "rom_api.h"
  43. /*
  44. * Following symbols are only used during plat_arch_setup()
  45. */
  46. static const uint64_t BL2_RO_BASE = BL_CODE_BASE;
  47. static const uint64_t BL2_RO_LIMIT = BL_CODE_END;
  48. #if USE_COHERENT_MEM
  49. static const uint64_t BL2_COHERENT_RAM_BASE = BL_COHERENT_RAM_BASE;
  50. static const uint64_t BL2_COHERENT_RAM_LIMIT = BL_COHERENT_RAM_END;
  51. #endif
  52. extern void plat_rcar_gic_driver_init(void);
  53. extern void plat_rcar_gic_init(void);
  54. extern void bl2_enter_bl31(const struct entry_point_info *bl_ep_info);
  55. extern void bl2_system_cpg_init(void);
  56. extern void bl2_secure_setting(void);
  57. extern void bl2_ram_security_setting_finish(void);
  58. extern void bl2_cpg_init(void);
  59. extern void rcar_io_emmc_setup(void);
  60. extern void rcar_io_setup(void);
  61. extern void rcar_swdt_release(void);
  62. extern void rcar_swdt_init(void);
  63. extern void rcar_rpc_init(void);
  64. extern void rcar_pfc_init(void);
  65. extern void rcar_dma_init(void);
  66. static void bl2_init_generic_timer(void);
  67. /* R-Car Gen3 product check */
  68. #if (RCAR_LSI == RCAR_H3) || (RCAR_LSI == RCAR_H3N)
  69. #define TARGET_PRODUCT PRR_PRODUCT_H3
  70. #define TARGET_NAME "R-Car H3"
  71. #elif RCAR_LSI == RCAR_M3
  72. #define TARGET_PRODUCT PRR_PRODUCT_M3
  73. #define TARGET_NAME "R-Car M3"
  74. #elif RCAR_LSI == RCAR_M3N
  75. #define TARGET_PRODUCT PRR_PRODUCT_M3N
  76. #define TARGET_NAME "R-Car M3N"
  77. #elif RCAR_LSI == RCAR_V3M
  78. #define TARGET_PRODUCT PRR_PRODUCT_V3M
  79. #define TARGET_NAME "R-Car V3M"
  80. #elif RCAR_LSI == RCAR_E3
  81. #define TARGET_PRODUCT PRR_PRODUCT_E3
  82. #define TARGET_NAME "R-Car E3"
  83. #elif RCAR_LSI == RCAR_D3
  84. #define TARGET_PRODUCT PRR_PRODUCT_D3
  85. #define TARGET_NAME "R-Car D3"
  86. #elif RCAR_LSI == RCAR_AUTO
  87. #define TARGET_NAME "R-Car H3/M3/M3N/V3M"
  88. #endif
  89. #if (RCAR_LSI == RCAR_E3)
  90. #define GPIO_INDT (GPIO_INDT6)
  91. #define GPIO_BKUP_TRG_SHIFT ((uint32_t)1U<<13U)
  92. #else
  93. #define GPIO_INDT (GPIO_INDT1)
  94. #define GPIO_BKUP_TRG_SHIFT ((uint32_t)1U<<8U)
  95. #endif
  96. CASSERT((PARAMS_BASE + sizeof(bl2_to_bl31_params_mem_t) + 0x100)
  97. < (RCAR_SHARED_MEM_BASE + RCAR_SHARED_MEM_SIZE),
  98. assert_bl31_params_do_not_fit_in_shared_memory);
  99. static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
  100. /* FDT with DRAM configuration */
  101. uint64_t fdt_blob[PAGE_SIZE_4KB / sizeof(uint64_t)];
  102. static void *fdt = (void *)fdt_blob;
  103. static void unsigned_num_print(unsigned long long int unum, unsigned int radix,
  104. char *string)
  105. {
  106. /* Just need enough space to store 64 bit decimal integer */
  107. char num_buf[20];
  108. int i = 0;
  109. unsigned int rem;
  110. do {
  111. rem = unum % radix;
  112. if (rem < 0xa)
  113. num_buf[i] = '0' + rem;
  114. else
  115. num_buf[i] = 'a' + (rem - 0xa);
  116. i++;
  117. unum /= radix;
  118. } while (unum > 0U);
  119. while (--i >= 0)
  120. *string++ = num_buf[i];
  121. *string = 0;
  122. }
  123. #if (RCAR_LOSSY_ENABLE == 1)
  124. typedef struct bl2_lossy_info {
  125. uint32_t magic;
  126. uint32_t a0;
  127. uint32_t b0;
  128. } bl2_lossy_info_t;
  129. static void bl2_lossy_gen_fdt(uint32_t no, uint64_t start_addr,
  130. uint64_t end_addr, uint32_t format,
  131. uint32_t enable, int fcnlnode)
  132. {
  133. const uint64_t fcnlsize = cpu_to_fdt64(end_addr - start_addr);
  134. char nodename[40] = { 0 };
  135. int ret, node;
  136. /* Ignore undefined addresses */
  137. if (start_addr == 0 && end_addr == 0)
  138. return;
  139. snprintf(nodename, sizeof(nodename), "lossy-decompression@");
  140. unsigned_num_print(start_addr, 16, nodename + strlen(nodename));
  141. node = ret = fdt_add_subnode(fdt, fcnlnode, nodename);
  142. if (ret < 0) {
  143. NOTICE("BL2: Cannot create FCNL node (ret=%i)\n", ret);
  144. panic();
  145. }
  146. ret = fdt_setprop_string(fdt, node, "compatible",
  147. "renesas,lossy-decompression");
  148. if (ret < 0) {
  149. NOTICE("BL2: Cannot add FCNL compat string (ret=%i)\n", ret);
  150. panic();
  151. }
  152. ret = fdt_appendprop_string(fdt, node, "compatible",
  153. "shared-dma-pool");
  154. if (ret < 0) {
  155. NOTICE("BL2: Cannot append FCNL compat string (ret=%i)\n", ret);
  156. panic();
  157. }
  158. ret = fdt_setprop_u64(fdt, node, "reg", start_addr);
  159. if (ret < 0) {
  160. NOTICE("BL2: Cannot add FCNL reg prop (ret=%i)\n", ret);
  161. panic();
  162. }
  163. ret = fdt_appendprop(fdt, node, "reg", &fcnlsize, sizeof(fcnlsize));
  164. if (ret < 0) {
  165. NOTICE("BL2: Cannot append FCNL reg size prop (ret=%i)\n", ret);
  166. panic();
  167. }
  168. ret = fdt_setprop(fdt, node, "no-map", NULL, 0);
  169. if (ret < 0) {
  170. NOTICE("BL2: Cannot add FCNL no-map prop (ret=%i)\n", ret);
  171. panic();
  172. }
  173. ret = fdt_setprop_u32(fdt, node, "renesas,formats", format);
  174. if (ret < 0) {
  175. NOTICE("BL2: Cannot add FCNL formats prop (ret=%i)\n", ret);
  176. panic();
  177. }
  178. }
  179. static void bl2_lossy_setting(uint32_t no, uint64_t start_addr,
  180. uint64_t end_addr, uint32_t format,
  181. uint32_t enable, int fcnlnode)
  182. {
  183. bl2_lossy_info_t info;
  184. uint32_t reg;
  185. bl2_lossy_gen_fdt(no, start_addr, end_addr, format, enable, fcnlnode);
  186. reg = format | (start_addr >> 20);
  187. mmio_write_32(AXI_DCMPAREACRA0 + 0x8 * no, reg);
  188. mmio_write_32(AXI_DCMPAREACRB0 + 0x8 * no, end_addr >> 20);
  189. mmio_write_32(AXI_DCMPAREACRA0 + 0x8 * no, reg | enable);
  190. info.magic = 0x12345678U;
  191. info.a0 = mmio_read_32(AXI_DCMPAREACRA0 + 0x8 * no);
  192. info.b0 = mmio_read_32(AXI_DCMPAREACRB0 + 0x8 * no);
  193. mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no, info.magic);
  194. mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no + 0x4, info.a0);
  195. mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no + 0x8, info.b0);
  196. NOTICE(" Entry %d: DCMPAREACRAx:0x%x DCMPAREACRBx:0x%x\n", no,
  197. mmio_read_32(AXI_DCMPAREACRA0 + 0x8 * no),
  198. mmio_read_32(AXI_DCMPAREACRB0 + 0x8 * no));
  199. }
  200. static int bl2_create_reserved_memory(void)
  201. {
  202. int ret;
  203. int fcnlnode = fdt_add_subnode(fdt, 0, "reserved-memory");
  204. if (fcnlnode < 0) {
  205. NOTICE("BL2: Cannot create reserved mem node (ret=%i)\n",
  206. fcnlnode);
  207. panic();
  208. }
  209. ret = fdt_setprop(fdt, fcnlnode, "ranges", NULL, 0);
  210. if (ret < 0) {
  211. NOTICE("BL2: Cannot add FCNL ranges prop (ret=%i)\n", ret);
  212. panic();
  213. }
  214. ret = fdt_setprop_u32(fdt, fcnlnode, "#address-cells", 2);
  215. if (ret < 0) {
  216. NOTICE("BL2: Cannot add FCNL #address-cells prop (ret=%i)\n", ret);
  217. panic();
  218. }
  219. ret = fdt_setprop_u32(fdt, fcnlnode, "#size-cells", 2);
  220. if (ret < 0) {
  221. NOTICE("BL2: Cannot add FCNL #size-cells prop (ret=%i)\n", ret);
  222. panic();
  223. }
  224. return fcnlnode;
  225. }
  226. static void bl2_create_fcnl_reserved_memory(void)
  227. {
  228. int fcnlnode;
  229. NOTICE("BL2: Lossy Decomp areas\n");
  230. fcnlnode = bl2_create_reserved_memory();
  231. bl2_lossy_setting(0, LOSSY_ST_ADDR0, LOSSY_END_ADDR0,
  232. LOSSY_FMT0, LOSSY_ENA_DIS0, fcnlnode);
  233. bl2_lossy_setting(1, LOSSY_ST_ADDR1, LOSSY_END_ADDR1,
  234. LOSSY_FMT1, LOSSY_ENA_DIS1, fcnlnode);
  235. bl2_lossy_setting(2, LOSSY_ST_ADDR2, LOSSY_END_ADDR2,
  236. LOSSY_FMT2, LOSSY_ENA_DIS2, fcnlnode);
  237. }
  238. #else
  239. static void bl2_create_fcnl_reserved_memory(void) {}
  240. #endif
  241. void bl2_plat_flush_bl31_params(void)
  242. {
  243. uint32_t product_cut, product, cut;
  244. uint32_t boot_dev, boot_cpu;
  245. uint32_t lcs, reg, val;
  246. reg = mmio_read_32(RCAR_MODEMR);
  247. boot_dev = reg & MODEMR_BOOT_DEV_MASK;
  248. if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
  249. boot_dev == MODEMR_BOOT_DEV_EMMC_50X8)
  250. emmc_terminate();
  251. if ((reg & MODEMR_BOOT_CPU_MASK) != MODEMR_BOOT_CPU_CR7)
  252. bl2_secure_setting();
  253. reg = mmio_read_32(RCAR_PRR);
  254. product_cut = reg & (PRR_PRODUCT_MASK | PRR_CUT_MASK);
  255. product = reg & PRR_PRODUCT_MASK;
  256. cut = reg & PRR_CUT_MASK;
  257. if (product == PRR_PRODUCT_M3 && PRR_PRODUCT_30 > cut)
  258. goto tlb;
  259. if (product == PRR_PRODUCT_H3 && PRR_PRODUCT_20 > cut)
  260. goto tlb;
  261. /* Disable MFIS write protection */
  262. mmio_write_32(MFISWPCNTR, MFISWPCNTR_PASSWORD | 1);
  263. tlb:
  264. reg = mmio_read_32(RCAR_MODEMR);
  265. boot_cpu = reg & MODEMR_BOOT_CPU_MASK;
  266. if (boot_cpu != MODEMR_BOOT_CPU_CA57 &&
  267. boot_cpu != MODEMR_BOOT_CPU_CA53)
  268. goto mmu;
  269. if (product_cut == PRR_PRODUCT_H3_CUT20) {
  270. mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
  271. mmio_write_32(IPMMUVI1_IMSCTLR, IMSCTLR_DISCACHE);
  272. mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
  273. mmio_write_32(IPMMUPV1_IMSCTLR, IMSCTLR_DISCACHE);
  274. mmio_write_32(IPMMUPV2_IMSCTLR, IMSCTLR_DISCACHE);
  275. mmio_write_32(IPMMUPV3_IMSCTLR, IMSCTLR_DISCACHE);
  276. } else if (product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_10) ||
  277. product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_11)) {
  278. mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
  279. mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
  280. } else if ((product_cut == (PRR_PRODUCT_E3 | PRR_PRODUCT_10)) ||
  281. (product_cut == (PRR_PRODUCT_E3 | PRR_PRODUCT_11))) {
  282. mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
  283. mmio_write_32(IPMMUVP0_IMSCTLR, IMSCTLR_DISCACHE);
  284. mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
  285. }
  286. if (product_cut == (PRR_PRODUCT_H3_CUT20) ||
  287. product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_10) ||
  288. product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_11) ||
  289. product_cut == (PRR_PRODUCT_E3 | PRR_PRODUCT_10)) {
  290. mmio_write_32(IPMMUHC_IMSCTLR, IMSCTLR_DISCACHE);
  291. mmio_write_32(IPMMURT_IMSCTLR, IMSCTLR_DISCACHE);
  292. mmio_write_32(IPMMUMP_IMSCTLR, IMSCTLR_DISCACHE);
  293. mmio_write_32(IPMMUDS0_IMSCTLR, IMSCTLR_DISCACHE);
  294. mmio_write_32(IPMMUDS1_IMSCTLR, IMSCTLR_DISCACHE);
  295. }
  296. mmu:
  297. mmio_write_32(IPMMUMM_IMSCTLR, IPMMUMM_IMSCTLR_ENABLE);
  298. mmio_write_32(IPMMUMM_IMAUXCTLR, IPMMUMM_IMAUXCTLR_NMERGE40_BIT);
  299. val = rcar_rom_get_lcs(&lcs);
  300. if (val) {
  301. ERROR("BL2: Failed to get the LCS. (%d)\n", val);
  302. panic();
  303. }
  304. if (lcs == LCS_SE)
  305. mmio_clrbits_32(P_ARMREG_SEC_CTRL, P_ARMREG_SEC_CTRL_PROT);
  306. rcar_swdt_release();
  307. bl2_system_cpg_init();
  308. /* Disable data cache (clean and invalidate) */
  309. disable_mmu_el3();
  310. #if RCAR_BL2_DCACHE == 1
  311. dcsw_op_all(DCCISW);
  312. #endif
  313. tlbialle3();
  314. disable_mmu_icache_el3();
  315. plat_invalidate_icache();
  316. dsbsy();
  317. isb();
  318. }
  319. static uint32_t is_ddr_backup_mode(void)
  320. {
  321. #if RCAR_SYSTEM_SUSPEND
  322. static uint32_t reason = RCAR_COLD_BOOT;
  323. static uint32_t once;
  324. #if PMIC_ROHM_BD9571 && RCAR_SYSTEM_RESET_KEEPON_DDR
  325. uint8_t data;
  326. #endif
  327. if (once)
  328. return reason;
  329. once = 1;
  330. if ((mmio_read_32(GPIO_INDT) & GPIO_BKUP_TRG_SHIFT) == 0)
  331. return reason;
  332. #if PMIC_ROHM_BD9571 && RCAR_SYSTEM_RESET_KEEPON_DDR
  333. if (rcar_iic_dvfs_receive(PMIC, REG_KEEP10, &data)) {
  334. ERROR("BL2: REG Keep10 READ ERROR.\n");
  335. panic();
  336. }
  337. if (KEEP10_MAGIC != data)
  338. reason = RCAR_WARM_BOOT;
  339. #else
  340. reason = RCAR_WARM_BOOT;
  341. #endif
  342. return reason;
  343. #else
  344. return RCAR_COLD_BOOT;
  345. #endif
  346. }
  347. #if RCAR_GEN3_BL33_GZIP == 1
  348. void bl2_plat_preload_setup(void)
  349. {
  350. image_decompress_init(BL33_COMP_BASE, BL33_COMP_SIZE, gunzip);
  351. }
  352. #endif
  353. static uint64_t check_secure_load_area(uintptr_t base, uint32_t size,
  354. uintptr_t dest, uint32_t len)
  355. {
  356. uintptr_t free_end, requested_end;
  357. /*
  358. * Handle corner cases first.
  359. *
  360. * The order of the 2 tests is important, because if there's no space
  361. * left (i.e. free_size == 0) but we don't ask for any memory
  362. * (i.e. size == 0) then we should report that the memory is free.
  363. */
  364. if (len == 0U) {
  365. WARN("BL2: load data size is zero\n");
  366. return 0; /* A zero-byte region is always free */
  367. }
  368. if (size == 0U) {
  369. goto err;
  370. }
  371. /*
  372. * Check that the end addresses don't overflow.
  373. * If they do, consider that this memory region is not free, as this
  374. * is an invalid scenario.
  375. */
  376. if (check_uptr_overflow(base, size - 1U)) {
  377. goto err;
  378. }
  379. free_end = base + (size - 1U);
  380. if (check_uptr_overflow(dest, len - 1U)) {
  381. goto err;
  382. }
  383. requested_end = dest + (len - 1U);
  384. /*
  385. * Finally, check that the requested memory region lies within the free
  386. * region.
  387. */
  388. if ((dest < base) || (requested_end > free_end)) {
  389. goto err;
  390. }
  391. return 0;
  392. err:
  393. ERROR("BL2: load data is outside the loadable area.\n");
  394. ERROR("BL2: dst=0x%lx, len=%d(0x%x)\n", dest, len, len);
  395. return 1;
  396. }
  397. static uint64_t rcar_get_dest_addr_from_cert(uint32_t certid, uintptr_t *dest,
  398. uint32_t *len)
  399. {
  400. uint32_t cert;
  401. int ret;
  402. ret = rcar_get_certificate(certid, &cert);
  403. if (ret) {
  404. ERROR("%s : cert file load error", __func__);
  405. return 1;
  406. }
  407. rcar_read_certificate((uint64_t) cert, len, dest);
  408. return 0;
  409. }
  410. int bl2_plat_handle_pre_image_load(unsigned int image_id)
  411. {
  412. u_register_t *boot_kind = (void *) BOOT_KIND_BASE;
  413. bl_mem_params_node_t *bl_mem_params;
  414. uintptr_t dev_handle;
  415. uintptr_t image_spec;
  416. uintptr_t dest;
  417. uint32_t len;
  418. uint64_t ui64_ret;
  419. int iret;
  420. bl_mem_params = get_bl_mem_params_node(image_id);
  421. if (bl_mem_params == NULL) {
  422. ERROR("BL2: Failed to get loading parameter.\n");
  423. return 1;
  424. }
  425. switch (image_id) {
  426. case BL31_IMAGE_ID:
  427. if (is_ddr_backup_mode() == RCAR_COLD_BOOT) {
  428. iret = plat_get_image_source(image_id, &dev_handle,
  429. &image_spec);
  430. if (iret != 0) {
  431. return 1;
  432. }
  433. ui64_ret = rcar_get_dest_addr_from_cert(
  434. SOC_FW_CONTENT_CERT_ID, &dest, &len);
  435. if (ui64_ret != 0U) {
  436. return 1;
  437. }
  438. ui64_ret = check_secure_load_area(
  439. BL31_BASE, BL31_LIMIT - BL31_BASE,
  440. dest, len);
  441. if (ui64_ret != 0U) {
  442. return 1;
  443. }
  444. *boot_kind = RCAR_COLD_BOOT;
  445. flush_dcache_range(BOOT_KIND_BASE, sizeof(*boot_kind));
  446. bl_mem_params->image_info.image_base = dest;
  447. bl_mem_params->image_info.image_size = len;
  448. } else {
  449. *boot_kind = RCAR_WARM_BOOT;
  450. flush_dcache_range(BOOT_KIND_BASE, sizeof(*boot_kind));
  451. console_flush();
  452. bl2_plat_flush_bl31_params();
  453. /* will not return */
  454. bl2_enter_bl31(&bl_mem_params->ep_info);
  455. }
  456. return 0;
  457. #ifndef SPD_NONE
  458. case BL32_IMAGE_ID:
  459. ui64_ret = rcar_get_dest_addr_from_cert(
  460. TRUSTED_OS_FW_CONTENT_CERT_ID, &dest, &len);
  461. if (ui64_ret != 0U) {
  462. return 1;
  463. }
  464. ui64_ret = check_secure_load_area(
  465. BL32_BASE, BL32_LIMIT - BL32_BASE, dest, len);
  466. if (ui64_ret != 0U) {
  467. return 1;
  468. }
  469. bl_mem_params->image_info.image_base = dest;
  470. bl_mem_params->image_info.image_size = len;
  471. return 0;
  472. #endif
  473. case BL33_IMAGE_ID:
  474. /* case of image_id == BL33_IMAGE_ID */
  475. ui64_ret = rcar_get_dest_addr_from_cert(
  476. NON_TRUSTED_FW_CONTENT_CERT_ID,
  477. &dest, &len);
  478. if (ui64_ret != 0U) {
  479. return 1;
  480. }
  481. #if RCAR_GEN3_BL33_GZIP == 1
  482. image_decompress_prepare(&bl_mem_params->image_info);
  483. #endif
  484. return 0;
  485. default:
  486. return 1;
  487. }
  488. return 0;
  489. }
  490. int bl2_plat_handle_post_image_load(unsigned int image_id)
  491. {
  492. static bl2_to_bl31_params_mem_t *params;
  493. bl_mem_params_node_t *bl_mem_params;
  494. if (!params) {
  495. params = (bl2_to_bl31_params_mem_t *) PARAMS_BASE;
  496. memset((void *)PARAMS_BASE, 0, sizeof(*params));
  497. }
  498. bl_mem_params = get_bl_mem_params_node(image_id);
  499. if (!bl_mem_params) {
  500. ERROR("BL2: Failed to get loading parameter.\n");
  501. return 1;
  502. }
  503. switch (image_id) {
  504. case BL31_IMAGE_ID:
  505. bl_mem_params->ep_info.pc = bl_mem_params->image_info.image_base;
  506. return 0;
  507. case BL32_IMAGE_ID:
  508. bl_mem_params->ep_info.pc = bl_mem_params->image_info.image_base;
  509. memcpy(&params->bl32_ep_info, &bl_mem_params->ep_info,
  510. sizeof(entry_point_info_t));
  511. return 0;
  512. case BL33_IMAGE_ID:
  513. #if RCAR_GEN3_BL33_GZIP == 1
  514. int ret;
  515. if ((mmio_read_32(BL33_COMP_BASE) & 0xffff) == 0x8b1f) {
  516. /* decompress gzip-compressed image */
  517. ret = image_decompress(&bl_mem_params->image_info);
  518. if (ret != 0) {
  519. return ret;
  520. }
  521. } else {
  522. /* plain image, copy it in place */
  523. memcpy((void *)BL33_BASE, (void *)BL33_COMP_BASE,
  524. bl_mem_params->image_info.image_size);
  525. }
  526. #endif
  527. memcpy(&params->bl33_ep_info, &bl_mem_params->ep_info,
  528. sizeof(entry_point_info_t));
  529. return 0;
  530. default:
  531. return 1;
  532. }
  533. return 0;
  534. }
  535. struct meminfo *bl2_plat_sec_mem_layout(void)
  536. {
  537. return &bl2_tzram_layout;
  538. }
  539. static void bl2_populate_compatible_string(void *dt)
  540. {
  541. uint32_t board_type;
  542. uint32_t board_rev;
  543. uint32_t reg;
  544. int ret;
  545. fdt_setprop_u32(dt, 0, "#address-cells", 2);
  546. fdt_setprop_u32(dt, 0, "#size-cells", 2);
  547. /* Populate compatible string */
  548. rcar_get_board_type(&board_type, &board_rev);
  549. switch (board_type) {
  550. case BOARD_SALVATOR_X:
  551. ret = fdt_setprop_string(dt, 0, "compatible",
  552. "renesas,salvator-x");
  553. break;
  554. case BOARD_SALVATOR_XS:
  555. ret = fdt_setprop_string(dt, 0, "compatible",
  556. "renesas,salvator-xs");
  557. break;
  558. case BOARD_STARTER_KIT:
  559. ret = fdt_setprop_string(dt, 0, "compatible",
  560. "renesas,m3ulcb");
  561. break;
  562. case BOARD_STARTER_KIT_PRE:
  563. ret = fdt_setprop_string(dt, 0, "compatible",
  564. "renesas,h3ulcb");
  565. break;
  566. case BOARD_EAGLE:
  567. ret = fdt_setprop_string(dt, 0, "compatible",
  568. "renesas,eagle");
  569. break;
  570. case BOARD_EBISU:
  571. case BOARD_EBISU_4D:
  572. ret = fdt_setprop_string(dt, 0, "compatible",
  573. "renesas,ebisu");
  574. break;
  575. case BOARD_DRAAK:
  576. ret = fdt_setprop_string(dt, 0, "compatible",
  577. "renesas,draak");
  578. break;
  579. default:
  580. NOTICE("BL2: Cannot set compatible string, board unsupported\n");
  581. panic();
  582. }
  583. if (ret < 0) {
  584. NOTICE("BL2: Cannot set compatible string (ret=%i)\n", ret);
  585. panic();
  586. }
  587. reg = mmio_read_32(RCAR_PRR);
  588. switch (reg & PRR_PRODUCT_MASK) {
  589. case PRR_PRODUCT_H3:
  590. ret = fdt_appendprop_string(dt, 0, "compatible",
  591. "renesas,r8a7795");
  592. break;
  593. case PRR_PRODUCT_M3:
  594. ret = fdt_appendprop_string(dt, 0, "compatible",
  595. "renesas,r8a7796");
  596. break;
  597. case PRR_PRODUCT_M3N:
  598. ret = fdt_appendprop_string(dt, 0, "compatible",
  599. "renesas,r8a77965");
  600. break;
  601. case PRR_PRODUCT_V3M:
  602. ret = fdt_appendprop_string(dt, 0, "compatible",
  603. "renesas,r8a77970");
  604. break;
  605. case PRR_PRODUCT_E3:
  606. ret = fdt_appendprop_string(dt, 0, "compatible",
  607. "renesas,r8a77990");
  608. break;
  609. case PRR_PRODUCT_D3:
  610. ret = fdt_appendprop_string(dt, 0, "compatible",
  611. "renesas,r8a77995");
  612. break;
  613. default:
  614. NOTICE("BL2: Cannot set compatible string, SoC unsupported\n");
  615. panic();
  616. }
  617. if (ret < 0) {
  618. NOTICE("BL2: Cannot set compatible string (ret=%i)\n", ret);
  619. panic();
  620. }
  621. }
  622. static void bl2_add_rpc_node(void)
  623. {
  624. #if (RCAR_RPC_HYPERFLASH_LOCKED == 0)
  625. int ret, node;
  626. node = ret = fdt_add_subnode(fdt, 0, "soc");
  627. if (ret < 0) {
  628. goto err;
  629. }
  630. node = ret = fdt_add_subnode(fdt, node, "spi@ee200000");
  631. if (ret < 0) {
  632. goto err;
  633. }
  634. ret = fdt_setprop_string(fdt, node, "status", "okay");
  635. if (ret < 0) {
  636. goto err;
  637. }
  638. return;
  639. err:
  640. NOTICE("BL2: Cannot add RPC node to FDT (ret=%i)\n", ret);
  641. panic();
  642. #endif
  643. }
  644. static void bl2_add_kaslr_seed(void)
  645. {
  646. uint32_t cnt, isr, prr;
  647. uint64_t seed;
  648. int ret, node;
  649. /* SCEG is only available on H3/M3-W/M3-N */
  650. prr = mmio_read_32(RCAR_PRR);
  651. switch (prr & PRR_PRODUCT_MASK) {
  652. case PRR_PRODUCT_H3:
  653. case PRR_PRODUCT_M3:
  654. case PRR_PRODUCT_M3N:
  655. break;
  656. default:
  657. return;
  658. }
  659. mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_SW_RESET_REG_ADDR,
  660. CC63_TRNG_SW_RESET_REG_SET);
  661. do {
  662. mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_CLK_ENABLE_REG_ADDR,
  663. CC63_TRNG_CLK_ENABLE_REG_SET);
  664. mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_SAMPLE_CNT1_REG_ADDR,
  665. CC63_TRNG_SAMPLE_CNT1_REG_SAMPLE_COUNT);
  666. cnt = mmio_read_32(RCAR_CC63_BASE + CC63_TRNG_SAMPLE_CNT1_REG_ADDR);
  667. } while (cnt != CC63_TRNG_SAMPLE_CNT1_REG_SAMPLE_COUNT);
  668. mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_CONFIG_REG_ADDR,
  669. CC63_TRNG_CONFIG_REG_ROSC_MAX_LENGTH);
  670. mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_DEBUG_CONTROL_REG_ADDR,
  671. CC63_TRNG_DEBUG_CONTROL_REG_80090B);
  672. mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_SOURCE_ENABLE_REG_ADDR,
  673. CC63_TRNG_SOURCE_ENABLE_REG_SET);
  674. do {
  675. isr = mmio_read_32(RCAR_CC63_BASE + CC63_TRNG_ISR_REG_ADDR);
  676. if ((isr & CC63_TRNG_ISR_REG_AUTOCORR_ERR) != 0U) {
  677. panic();
  678. }
  679. } while ((isr & CC63_TRNG_ISR_REG_EHR_VALID) == 0U);
  680. mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_ICR_REG_ADDR, UINT32_MAX);
  681. seed = mmio_read_64(RCAR_CC63_BASE + CC63_TRNG_EHR_DATA_ADDR_0_REG_ADDR);
  682. mmio_write_32(RCAR_CC63_BASE + CC63_TRNG_SOURCE_ENABLE_REG_ADDR,
  683. CC63_TRNG_SOURCE_ENABLE_REG_CLR);
  684. node = ret = fdt_add_subnode(fdt, 0, "chosen");
  685. if (ret < 0) {
  686. goto err;
  687. }
  688. ret = fdt_setprop_u64(fdt, node, "kaslr-seed", seed);
  689. if (ret < 0) {
  690. goto err;
  691. }
  692. return;
  693. err:
  694. NOTICE("BL2: Cannot add KASLR seed to FDT (ret=%i)\n", ret);
  695. panic();
  696. }
  697. static void bl2_add_dram_entry(uint64_t start, uint64_t size)
  698. {
  699. char nodename[32] = { 0 };
  700. uint64_t fdtsize;
  701. int ret, node;
  702. fdtsize = cpu_to_fdt64(size);
  703. snprintf(nodename, sizeof(nodename), "memory@");
  704. unsigned_num_print(start, 16, nodename + strlen(nodename));
  705. node = ret = fdt_add_subnode(fdt, 0, nodename);
  706. if (ret < 0) {
  707. goto err;
  708. }
  709. ret = fdt_setprop_string(fdt, node, "device_type", "memory");
  710. if (ret < 0) {
  711. goto err;
  712. }
  713. ret = fdt_setprop_u64(fdt, node, "reg", start);
  714. if (ret < 0) {
  715. goto err;
  716. }
  717. ret = fdt_appendprop(fdt, node, "reg", &fdtsize,
  718. sizeof(fdtsize));
  719. if (ret < 0) {
  720. goto err;
  721. }
  722. return;
  723. err:
  724. NOTICE("BL2: Cannot add memory node [%" PRIx64 " - %" PRIx64 "] to FDT (ret=%i)\n",
  725. start, start + size - 1, ret);
  726. panic();
  727. }
  728. static void bl2_advertise_dram_entries(uint64_t dram_config[8])
  729. {
  730. uint64_t start, size, size32;
  731. int chan;
  732. for (chan = 0; chan < 4; chan++) {
  733. start = dram_config[2 * chan];
  734. size = dram_config[2 * chan + 1];
  735. if (!size)
  736. continue;
  737. NOTICE("BL2: CH%d: %" PRIx64 " - %" PRIx64 ", %" PRId64 " %siB\n",
  738. chan, start, start + size - 1,
  739. (size >> 30) ? : size >> 20,
  740. (size >> 30) ? "G" : "M");
  741. }
  742. /*
  743. * We add the DT nodes in reverse order here. The fdt_add_subnode()
  744. * adds the DT node before the first existing DT node, so we have
  745. * to add them in reverse order to get nodes sorted by address in
  746. * the resulting DT.
  747. */
  748. for (chan = 3; chan >= 0; chan--) {
  749. start = dram_config[2 * chan];
  750. size = dram_config[2 * chan + 1];
  751. if (!size)
  752. continue;
  753. /*
  754. * Channel 0 is mapped in 32bit space and the first
  755. * 128 MiB are reserved and the maximum size is 2GiB.
  756. */
  757. if (chan == 0) {
  758. /* Limit the 32bit entry to 2 GiB - 128 MiB */
  759. size32 = size - 0x8000000U;
  760. if (size32 >= 0x78000000U) {
  761. size32 = 0x78000000U;
  762. }
  763. /* Emit 32bit entry, up to 2 GiB - 128 MiB long. */
  764. bl2_add_dram_entry(0x48000000, size32);
  765. /*
  766. * If channel 0 is less than 2 GiB long, the
  767. * entire memory fits into the 32bit space entry,
  768. * so move on to the next channel.
  769. */
  770. if (size <= 0x80000000U) {
  771. continue;
  772. }
  773. /*
  774. * If channel 0 is more than 2 GiB long, emit
  775. * another entry which covers the rest of the
  776. * memory in channel 0, in the 64bit space.
  777. *
  778. * Start of this new entry is at 2 GiB offset
  779. * from the beginning of the 64bit channel 0
  780. * address, size is 2 GiB shorter than total
  781. * size of the channel.
  782. */
  783. start += 0x80000000U;
  784. size -= 0x80000000U;
  785. }
  786. bl2_add_dram_entry(start, size);
  787. }
  788. }
  789. static void bl2_advertise_dram_size(uint32_t product)
  790. {
  791. uint64_t dram_config[8] = {
  792. [0] = 0x400000000ULL,
  793. [2] = 0x500000000ULL,
  794. [4] = 0x600000000ULL,
  795. [6] = 0x700000000ULL,
  796. };
  797. uint32_t cut = mmio_read_32(RCAR_PRR) & PRR_CUT_MASK;
  798. switch (product) {
  799. case PRR_PRODUCT_H3:
  800. #if (RCAR_DRAM_LPDDR4_MEMCONF == 0)
  801. /* 4GB(1GBx4) */
  802. dram_config[1] = 0x40000000ULL;
  803. dram_config[3] = 0x40000000ULL;
  804. dram_config[5] = 0x40000000ULL;
  805. dram_config[7] = 0x40000000ULL;
  806. #elif (RCAR_DRAM_LPDDR4_MEMCONF == 1) && \
  807. (RCAR_DRAM_CHANNEL == 5) && \
  808. (RCAR_DRAM_SPLIT == 2)
  809. /* 4GB(2GBx2 2ch split) */
  810. dram_config[1] = 0x80000000ULL;
  811. dram_config[3] = 0x80000000ULL;
  812. #elif (RCAR_DRAM_LPDDR4_MEMCONF == 1) && (RCAR_DRAM_CHANNEL == 15)
  813. /* 8GB(2GBx4: default) */
  814. dram_config[1] = 0x80000000ULL;
  815. dram_config[3] = 0x80000000ULL;
  816. dram_config[5] = 0x80000000ULL;
  817. dram_config[7] = 0x80000000ULL;
  818. #endif /* RCAR_DRAM_LPDDR4_MEMCONF == 0 */
  819. break;
  820. case PRR_PRODUCT_M3:
  821. if (cut < PRR_PRODUCT_30) {
  822. #if (RCAR_GEN3_ULCB == 1)
  823. /* 2GB(1GBx2 2ch split) */
  824. dram_config[1] = 0x40000000ULL;
  825. dram_config[5] = 0x40000000ULL;
  826. #else
  827. /* 4GB(2GBx2 2ch split) */
  828. dram_config[1] = 0x80000000ULL;
  829. dram_config[5] = 0x80000000ULL;
  830. #endif
  831. } else {
  832. /* 8GB(2GBx4 2ch split) */
  833. dram_config[1] = 0x100000000ULL;
  834. dram_config[5] = 0x100000000ULL;
  835. }
  836. break;
  837. case PRR_PRODUCT_M3N:
  838. #if (RCAR_DRAM_LPDDR4_MEMCONF == 2)
  839. /* 4GB(4GBx1) */
  840. dram_config[1] = 0x100000000ULL;
  841. #elif (RCAR_DRAM_LPDDR4_MEMCONF == 1)
  842. /* 2GB(1GBx2) */
  843. dram_config[1] = 0x80000000ULL;
  844. #endif
  845. break;
  846. case PRR_PRODUCT_V3M:
  847. /* 1GB(512MBx2) */
  848. dram_config[1] = 0x40000000ULL;
  849. break;
  850. case PRR_PRODUCT_E3:
  851. #if (RCAR_DRAM_DDR3L_MEMCONF == 0)
  852. /* 1GB(512MBx2) */
  853. dram_config[1] = 0x40000000ULL;
  854. #elif (RCAR_DRAM_DDR3L_MEMCONF == 1)
  855. /* 2GB(512MBx4) */
  856. dram_config[1] = 0x80000000ULL;
  857. #elif (RCAR_DRAM_DDR3L_MEMCONF == 2)
  858. /* 4GB(1GBx4) */
  859. dram_config[1] = 0x100000000ULL;
  860. #endif /* RCAR_DRAM_DDR3L_MEMCONF == 0 */
  861. break;
  862. case PRR_PRODUCT_D3:
  863. /* 512MB */
  864. dram_config[1] = 0x20000000ULL;
  865. break;
  866. }
  867. bl2_advertise_dram_entries(dram_config);
  868. }
  869. void bl2_el3_early_platform_setup(u_register_t arg1, u_register_t arg2,
  870. u_register_t arg3, u_register_t arg4)
  871. {
  872. uint32_t reg, midr, lcs, boot_dev, boot_cpu, sscg, type, rev;
  873. uint32_t product, product_cut, major, minor;
  874. int32_t ret;
  875. const char *str;
  876. const char *unknown = "unknown";
  877. const char *cpu_ca57 = "CA57";
  878. const char *cpu_ca53 = "CA53";
  879. const char *product_m3n = "M3N";
  880. const char *product_h3 = "H3";
  881. const char *product_m3 = "M3";
  882. const char *product_e3 = "E3";
  883. const char *product_d3 = "D3";
  884. const char *product_v3m = "V3M";
  885. const char *lcs_secure = "SE";
  886. const char *lcs_cm = "CM";
  887. const char *lcs_dm = "DM";
  888. const char *lcs_sd = "SD";
  889. const char *lcs_fa = "FA";
  890. const char *sscg_off = "PLL1 nonSSCG Clock select";
  891. const char *sscg_on = "PLL1 SSCG Clock select";
  892. const char *boot_hyper80 = "HyperFlash(80MHz)";
  893. const char *boot_qspi40 = "QSPI Flash(40MHz)";
  894. const char *boot_qspi80 = "QSPI Flash(80MHz)";
  895. const char *boot_emmc25x1 = "eMMC(25MHz x1)";
  896. const char *boot_emmc50x8 = "eMMC(50MHz x8)";
  897. #if (RCAR_LSI == RCAR_E3) || (RCAR_LSI == RCAR_D3)
  898. const char *boot_hyper160 = "HyperFlash(150MHz)";
  899. #else
  900. const char *boot_hyper160 = "HyperFlash(160MHz)";
  901. #endif
  902. bl2_init_generic_timer();
  903. reg = mmio_read_32(RCAR_MODEMR);
  904. boot_dev = reg & MODEMR_BOOT_DEV_MASK;
  905. boot_cpu = reg & MODEMR_BOOT_CPU_MASK;
  906. bl2_cpg_init();
  907. if (boot_cpu == MODEMR_BOOT_CPU_CA57 ||
  908. boot_cpu == MODEMR_BOOT_CPU_CA53) {
  909. rcar_pfc_init();
  910. rcar_console_boot_init();
  911. }
  912. plat_rcar_gic_driver_init();
  913. plat_rcar_gic_init();
  914. rcar_swdt_init();
  915. /* FIQ interrupts are taken to EL3 */
  916. write_scr_el3(read_scr_el3() | SCR_FIQ_BIT);
  917. write_daifclr(DAIF_FIQ_BIT);
  918. reg = read_midr();
  919. midr = reg & (MIDR_PN_MASK << MIDR_PN_SHIFT);
  920. switch (midr) {
  921. case MIDR_CA57:
  922. str = cpu_ca57;
  923. break;
  924. case MIDR_CA53:
  925. str = cpu_ca53;
  926. break;
  927. default:
  928. str = unknown;
  929. break;
  930. }
  931. NOTICE("BL2: R-Car Gen3 Initial Program Loader(%s) Rev.%s\n", str,
  932. version_of_renesas);
  933. reg = mmio_read_32(RCAR_PRR);
  934. product_cut = reg & (PRR_PRODUCT_MASK | PRR_CUT_MASK);
  935. product = reg & PRR_PRODUCT_MASK;
  936. switch (product) {
  937. case PRR_PRODUCT_H3:
  938. str = product_h3;
  939. break;
  940. case PRR_PRODUCT_M3:
  941. str = product_m3;
  942. break;
  943. case PRR_PRODUCT_M3N:
  944. str = product_m3n;
  945. break;
  946. case PRR_PRODUCT_V3M:
  947. str = product_v3m;
  948. break;
  949. case PRR_PRODUCT_E3:
  950. str = product_e3;
  951. break;
  952. case PRR_PRODUCT_D3:
  953. str = product_d3;
  954. break;
  955. default:
  956. str = unknown;
  957. break;
  958. }
  959. if ((PRR_PRODUCT_M3 == product) &&
  960. (PRR_PRODUCT_20 == (reg & RCAR_MAJOR_MASK))) {
  961. if (RCAR_M3_CUT_VER11 == (reg & PRR_CUT_MASK)) {
  962. /* M3 Ver.1.1 or Ver.1.2 */
  963. NOTICE("BL2: PRR is R-Car %s Ver.1.1 / Ver.1.2\n",
  964. str);
  965. } else {
  966. NOTICE("BL2: PRR is R-Car %s Ver.1.%d\n",
  967. str,
  968. (reg & RCAR_MINOR_MASK) + RCAR_M3_MINOR_OFFSET);
  969. }
  970. } else if (product == PRR_PRODUCT_D3) {
  971. if (RCAR_D3_CUT_VER10 == (reg & PRR_CUT_MASK)) {
  972. NOTICE("BL2: PRR is R-Car %s Ver.1.0\n", str);
  973. } else if (RCAR_D3_CUT_VER11 == (reg & PRR_CUT_MASK)) {
  974. NOTICE("BL2: PRR is R-Car %s Ver.1.1\n", str);
  975. } else {
  976. NOTICE("BL2: PRR is R-Car %s Ver.X.X\n", str);
  977. }
  978. } else {
  979. major = (reg & RCAR_MAJOR_MASK) >> RCAR_MAJOR_SHIFT;
  980. major = major + RCAR_MAJOR_OFFSET;
  981. minor = reg & RCAR_MINOR_MASK;
  982. NOTICE("BL2: PRR is R-Car %s Ver.%d.%d\n", str, major, minor);
  983. }
  984. if (PRR_PRODUCT_E3 == product || PRR_PRODUCT_D3 == product) {
  985. reg = mmio_read_32(RCAR_MODEMR);
  986. sscg = reg & RCAR_SSCG_MASK;
  987. str = sscg == RCAR_SSCG_ENABLE ? sscg_on : sscg_off;
  988. NOTICE("BL2: %s\n", str);
  989. }
  990. rcar_get_board_type(&type, &rev);
  991. switch (type) {
  992. case BOARD_SALVATOR_X:
  993. case BOARD_KRIEK:
  994. case BOARD_STARTER_KIT:
  995. case BOARD_SALVATOR_XS:
  996. case BOARD_EBISU:
  997. case BOARD_STARTER_KIT_PRE:
  998. case BOARD_EBISU_4D:
  999. case BOARD_DRAAK:
  1000. case BOARD_EAGLE:
  1001. break;
  1002. default:
  1003. type = BOARD_UNKNOWN;
  1004. break;
  1005. }
  1006. if (type == BOARD_UNKNOWN || rev == BOARD_REV_UNKNOWN)
  1007. NOTICE("BL2: Board is %s Rev.---\n", GET_BOARD_NAME(type));
  1008. else {
  1009. NOTICE("BL2: Board is %s Rev.%d.%d\n",
  1010. GET_BOARD_NAME(type),
  1011. GET_BOARD_MAJOR(rev), GET_BOARD_MINOR(rev));
  1012. }
  1013. #if RCAR_LSI != RCAR_AUTO
  1014. if (product != TARGET_PRODUCT) {
  1015. ERROR("BL2: IPL was been built for the %s.\n", TARGET_NAME);
  1016. ERROR("BL2: Please write the correct IPL to flash memory.\n");
  1017. panic();
  1018. }
  1019. #endif
  1020. rcar_avs_init();
  1021. rcar_avs_setting();
  1022. switch (boot_dev) {
  1023. case MODEMR_BOOT_DEV_HYPERFLASH160:
  1024. str = boot_hyper160;
  1025. break;
  1026. case MODEMR_BOOT_DEV_HYPERFLASH80:
  1027. str = boot_hyper80;
  1028. break;
  1029. case MODEMR_BOOT_DEV_QSPI_FLASH40:
  1030. str = boot_qspi40;
  1031. break;
  1032. case MODEMR_BOOT_DEV_QSPI_FLASH80:
  1033. str = boot_qspi80;
  1034. break;
  1035. case MODEMR_BOOT_DEV_EMMC_25X1:
  1036. #if RCAR_LSI == RCAR_D3
  1037. ERROR("BL2: Failed to Initialize. eMMC is not supported.\n");
  1038. panic();
  1039. #endif
  1040. str = boot_emmc25x1;
  1041. break;
  1042. case MODEMR_BOOT_DEV_EMMC_50X8:
  1043. str = boot_emmc50x8;
  1044. break;
  1045. default:
  1046. str = unknown;
  1047. break;
  1048. }
  1049. NOTICE("BL2: Boot device is %s\n", str);
  1050. rcar_avs_setting();
  1051. reg = rcar_rom_get_lcs(&lcs);
  1052. if (reg) {
  1053. str = unknown;
  1054. goto lcm_state;
  1055. }
  1056. switch (lcs) {
  1057. case LCS_CM:
  1058. str = lcs_cm;
  1059. break;
  1060. case LCS_DM:
  1061. str = lcs_dm;
  1062. break;
  1063. case LCS_SD:
  1064. str = lcs_sd;
  1065. break;
  1066. case LCS_SE:
  1067. str = lcs_secure;
  1068. break;
  1069. case LCS_FA:
  1070. str = lcs_fa;
  1071. break;
  1072. default:
  1073. str = unknown;
  1074. break;
  1075. }
  1076. lcm_state:
  1077. NOTICE("BL2: LCM state is %s\n", str);
  1078. rcar_avs_end();
  1079. is_ddr_backup_mode();
  1080. bl2_tzram_layout.total_base = BL31_BASE;
  1081. bl2_tzram_layout.total_size = BL31_LIMIT - BL31_BASE;
  1082. if (boot_cpu == MODEMR_BOOT_CPU_CA57 ||
  1083. boot_cpu == MODEMR_BOOT_CPU_CA53) {
  1084. ret = rcar_dram_init();
  1085. if (ret) {
  1086. NOTICE("BL2: Failed to DRAM initialize (%d).\n", ret);
  1087. panic();
  1088. }
  1089. rcar_qos_init();
  1090. }
  1091. /* Set up FDT */
  1092. ret = fdt_create_empty_tree(fdt, sizeof(fdt_blob));
  1093. if (ret) {
  1094. NOTICE("BL2: Cannot allocate FDT for U-Boot (ret=%i)\n", ret);
  1095. panic();
  1096. }
  1097. /* Add platform compatible string */
  1098. bl2_populate_compatible_string(fdt);
  1099. /* Enable RPC if unlocked */
  1100. bl2_add_rpc_node();
  1101. /* Print DRAM layout */
  1102. bl2_advertise_dram_size(product);
  1103. /* Add KASLR seed */
  1104. bl2_add_kaslr_seed();
  1105. if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
  1106. boot_dev == MODEMR_BOOT_DEV_EMMC_50X8) {
  1107. if (rcar_emmc_init() != EMMC_SUCCESS) {
  1108. NOTICE("BL2: Failed to eMMC driver initialize.\n");
  1109. panic();
  1110. }
  1111. rcar_emmc_memcard_power(EMMC_POWER_ON);
  1112. if (rcar_emmc_mount() != EMMC_SUCCESS) {
  1113. NOTICE("BL2: Failed to eMMC mount operation.\n");
  1114. panic();
  1115. }
  1116. } else {
  1117. rcar_rpc_init();
  1118. rcar_dma_init();
  1119. }
  1120. reg = mmio_read_32(RST_WDTRSTCR);
  1121. reg &= ~WDTRSTCR_RWDT_RSTMSK;
  1122. reg |= WDTRSTCR_PASSWORD;
  1123. mmio_write_32(RST_WDTRSTCR, reg);
  1124. mmio_write_32(CPG_CPGWPR, CPGWPR_PASSWORD);
  1125. mmio_write_32(CPG_CPGWPCR, CPGWPCR_PASSWORD);
  1126. reg = mmio_read_32(RCAR_PRR);
  1127. if ((reg & RCAR_CPU_MASK_CA57) == RCAR_CPU_HAVE_CA57)
  1128. mmio_write_32(CPG_CA57DBGRCR,
  1129. DBGCPUPREN | mmio_read_32(CPG_CA57DBGRCR));
  1130. if ((reg & RCAR_CPU_MASK_CA53) == RCAR_CPU_HAVE_CA53)
  1131. mmio_write_32(CPG_CA53DBGRCR,
  1132. DBGCPUPREN | mmio_read_32(CPG_CA53DBGRCR));
  1133. if (product_cut == PRR_PRODUCT_H3_CUT10) {
  1134. reg = mmio_read_32(CPG_PLL2CR);
  1135. reg &= ~((uint32_t) 1 << 5);
  1136. mmio_write_32(CPG_PLL2CR, reg);
  1137. reg = mmio_read_32(CPG_PLL4CR);
  1138. reg &= ~((uint32_t) 1 << 5);
  1139. mmio_write_32(CPG_PLL4CR, reg);
  1140. reg = mmio_read_32(CPG_PLL0CR);
  1141. reg &= ~((uint32_t) 1 << 12);
  1142. mmio_write_32(CPG_PLL0CR, reg);
  1143. }
  1144. bl2_create_fcnl_reserved_memory();
  1145. fdt_pack(fdt);
  1146. NOTICE("BL2: FDT at %p\n", fdt);
  1147. if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
  1148. boot_dev == MODEMR_BOOT_DEV_EMMC_50X8)
  1149. rcar_io_emmc_setup();
  1150. else
  1151. rcar_io_setup();
  1152. }
  1153. void bl2_el3_plat_arch_setup(void)
  1154. {
  1155. rcar_configure_mmu_el3(BL2_BASE,
  1156. BL2_END - BL2_BASE,
  1157. BL2_RO_BASE, BL2_RO_LIMIT
  1158. #if USE_COHERENT_MEM
  1159. , BL2_COHERENT_RAM_BASE, BL2_COHERENT_RAM_LIMIT
  1160. #endif
  1161. );
  1162. }
  1163. void bl2_el3_plat_prepare_exit(void)
  1164. {
  1165. bl2_ram_security_setting_finish();
  1166. }
  1167. void bl2_platform_setup(void)
  1168. {
  1169. }
  1170. static void bl2_init_generic_timer(void)
  1171. {
  1172. /* FIXME: V3M 16.666 MHz ? */
  1173. #if RCAR_LSI == RCAR_D3
  1174. uint32_t reg_cntfid = EXTAL_DRAAK;
  1175. #elif RCAR_LSI == RCAR_E3
  1176. uint32_t reg_cntfid = EXTAL_EBISU;
  1177. #else /* RCAR_LSI == RCAR_E3 */
  1178. uint32_t reg;
  1179. uint32_t reg_cntfid;
  1180. uint32_t modemr;
  1181. uint32_t modemr_pll;
  1182. uint32_t board_type;
  1183. uint32_t board_rev;
  1184. uint32_t pll_table[] = {
  1185. EXTAL_MD14_MD13_TYPE_0, /* MD14/MD13 : 0b00 */
  1186. EXTAL_MD14_MD13_TYPE_1, /* MD14/MD13 : 0b01 */
  1187. EXTAL_MD14_MD13_TYPE_2, /* MD14/MD13 : 0b10 */
  1188. EXTAL_MD14_MD13_TYPE_3 /* MD14/MD13 : 0b11 */
  1189. };
  1190. modemr = mmio_read_32(RCAR_MODEMR);
  1191. modemr_pll = (modemr & MODEMR_BOOT_PLL_MASK);
  1192. /* Set frequency data in CNTFID0 */
  1193. reg_cntfid = pll_table[modemr_pll >> MODEMR_BOOT_PLL_SHIFT];
  1194. reg = mmio_read_32(RCAR_PRR) & (PRR_PRODUCT_MASK | PRR_CUT_MASK);
  1195. switch (modemr_pll) {
  1196. case MD14_MD13_TYPE_0:
  1197. rcar_get_board_type(&board_type, &board_rev);
  1198. if (BOARD_SALVATOR_XS == board_type) {
  1199. reg_cntfid = EXTAL_SALVATOR_XS;
  1200. }
  1201. break;
  1202. case MD14_MD13_TYPE_3:
  1203. if (PRR_PRODUCT_H3_CUT10 == reg) {
  1204. reg_cntfid = reg_cntfid >> 1U;
  1205. }
  1206. break;
  1207. default:
  1208. /* none */
  1209. break;
  1210. }
  1211. #endif /* RCAR_LSI == RCAR_E3 */
  1212. /* Update memory mapped and register based frequency */
  1213. write_cntfrq_el0((u_register_t )reg_cntfid);
  1214. mmio_write_32(ARM_SYS_CNTCTL_BASE + (uintptr_t)CNTFID_OFF, reg_cntfid);
  1215. /* Enable counter */
  1216. mmio_setbits_32(RCAR_CNTC_BASE + (uintptr_t)CNTCR_OFF,
  1217. (uint32_t)CNTCR_EN);
  1218. }