bl2_plat_setup.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /*
  2. * Copyright (c) 2020-2021, 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 <arch_helpers.h>
  10. #include <bl1/bl1.h>
  11. #include <common/bl_common.h>
  12. #include <common/debug.h>
  13. #include <common/desc_image_load.h>
  14. #include <drivers/console.h>
  15. #include <drivers/io/io_driver.h>
  16. #include <drivers/io/io_storage.h>
  17. #include <libfdt.h>
  18. #include <lib/mmio.h>
  19. #include <lib/xlat_tables/xlat_tables_defs.h>
  20. #include <platform_def.h>
  21. #include <plat/common/platform.h>
  22. #include "avs_driver.h"
  23. #include "board.h"
  24. #include "boot_init_dram.h"
  25. #include "cpg_registers.h"
  26. #include "emmc_def.h"
  27. #include "emmc_hal.h"
  28. #include "emmc_std.h"
  29. #include "io_common.h"
  30. #include "io_rcar.h"
  31. #include "qos_init.h"
  32. #include "rcar_def.h"
  33. #include "rcar_private.h"
  34. #include "rcar_version.h"
  35. #include "rom_api.h"
  36. #define MAX_DRAM_CHANNELS 4
  37. /*
  38. * DDR ch0 has a shadow area mapped in 32bit address space.
  39. * Physical address 0x4_0000_0000 - 0x4_7fff_ffff in 64bit space
  40. * is mapped to 0x4000_0000 - 0xbfff_ffff in 32bit space.
  41. */
  42. #define MAX_DRAM_SIZE_CH0_32BIT_ADDR_SPACE 0x80000000ULL
  43. #if RCAR_BL2_DCACHE == 1
  44. /*
  45. * Following symbols are only used during plat_arch_setup() only
  46. * when RCAR_BL2_DCACHE is enabled.
  47. */
  48. static const uint64_t BL2_RO_BASE = BL_CODE_BASE;
  49. static const uint64_t BL2_RO_LIMIT = BL_CODE_END;
  50. #if USE_COHERENT_MEM
  51. static const uint64_t BL2_COHERENT_RAM_BASE = BL_COHERENT_RAM_BASE;
  52. static const uint64_t BL2_COHERENT_RAM_LIMIT = BL_COHERENT_RAM_END;
  53. #endif /* USE_COHERENT_MEM */
  54. #endif /* RCAR_BL2_DCACHE */
  55. extern void plat_rcar_gic_driver_init(void);
  56. extern void plat_rcar_gic_init(void);
  57. extern void bl2_enter_bl31(const struct entry_point_info *bl_ep_info);
  58. extern void bl2_system_cpg_init(void);
  59. extern void bl2_secure_setting(void);
  60. extern void bl2_cpg_init(void);
  61. extern void rcar_io_emmc_setup(void);
  62. extern void rcar_io_setup(void);
  63. extern void rcar_swdt_release(void);
  64. extern void rcar_swdt_init(void);
  65. extern void rcar_rpc_init(void);
  66. extern void rcar_dma_init(void);
  67. extern void rzg_pfc_init(void);
  68. static void bl2_init_generic_timer(void);
  69. /* RZ/G2 product check */
  70. #if RCAR_LSI == RZ_G2M
  71. #define TARGET_PRODUCT PRR_PRODUCT_M3
  72. #define TARGET_NAME "RZ/G2M"
  73. #elif RCAR_LSI == RZ_G2H
  74. #define TARGET_PRODUCT PRR_PRODUCT_H3
  75. #define TARGET_NAME "RZ/G2H"
  76. #elif RCAR_LSI == RZ_G2N
  77. #define TARGET_PRODUCT PRR_PRODUCT_M3N
  78. #define TARGET_NAME "RZ/G2N"
  79. #elif RCAR_LSI == RZ_G2E
  80. #define TARGET_PRODUCT PRR_PRODUCT_E3
  81. #define TARGET_NAME "RZ/G2E"
  82. #elif RCAR_LSI == RCAR_AUTO
  83. #define TARGET_NAME "RZ/G2M"
  84. #endif /* RCAR_LSI == RZ_G2M */
  85. #if (RCAR_LSI == RZ_G2E)
  86. #define GPIO_INDT (GPIO_INDT6)
  87. #define GPIO_BKUP_TRG_SHIFT ((uint32_t)1U << 13U)
  88. #else
  89. #define GPIO_INDT (GPIO_INDT1)
  90. #define GPIO_BKUP_TRG_SHIFT (1U << 8U)
  91. #endif /* RCAR_LSI == RZ_G2E */
  92. CASSERT((PARAMS_BASE + sizeof(bl2_to_bl31_params_mem_t) + 0x100)
  93. < (RCAR_SHARED_MEM_BASE + RCAR_SHARED_MEM_SIZE),
  94. assert_bl31_params_do_not_fit_in_shared_memory);
  95. static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE);
  96. /* FDT with DRAM configuration */
  97. uint64_t fdt_blob[PAGE_SIZE_4KB / sizeof(uint64_t)];
  98. static void *fdt = (void *)fdt_blob;
  99. static void unsigned_num_print(uint64_t unum, unsigned int radix, char *string)
  100. {
  101. /* Just need enough space to store 64 bit decimal integer */
  102. char num_buf[20];
  103. int i = 0;
  104. unsigned int rem;
  105. do {
  106. rem = unum % radix;
  107. if (rem < 0xaU) {
  108. num_buf[i] = '0' + rem;
  109. } else {
  110. num_buf[i] = 'a' + (rem - 0xaU);
  111. }
  112. i++;
  113. unum /= radix;
  114. } while (unum > 0U);
  115. while (--i >= 0) {
  116. *string++ = num_buf[i];
  117. }
  118. *string = 0;
  119. }
  120. #if RCAR_LOSSY_ENABLE == 1
  121. typedef struct bl2_lossy_info {
  122. uint32_t magic;
  123. uint32_t a0;
  124. uint32_t b0;
  125. } bl2_lossy_info_t;
  126. static void bl2_lossy_gen_fdt(uint32_t no, uint64_t start_addr,
  127. uint64_t end_addr, uint32_t format,
  128. uint32_t enable, int fcnlnode)
  129. {
  130. const uint64_t fcnlsize = cpu_to_fdt64(end_addr - start_addr);
  131. char nodename[40] = { 0 };
  132. int ret, node;
  133. /* Ignore undefined addresses */
  134. if (start_addr == 0UL && end_addr == 0UL) {
  135. return;
  136. }
  137. snprintf(nodename, sizeof(nodename), "lossy-decompression@");
  138. unsigned_num_print(start_addr, 16, nodename + strlen(nodename));
  139. node = ret = fdt_add_subnode(fdt, fcnlnode, nodename);
  140. if (ret < 0) {
  141. NOTICE("BL2: Cannot create FCNL node (ret=%i)\n", ret);
  142. panic();
  143. }
  144. ret = fdt_setprop_string(fdt, node, "compatible",
  145. "renesas,lossy-decompression");
  146. if (ret < 0) {
  147. NOTICE("BL2: Cannot add FCNL compat string %s (ret=%i)\n",
  148. "renesas,lossy-decompression", ret);
  149. panic();
  150. }
  151. ret = fdt_appendprop_string(fdt, node, "compatible",
  152. "shared-dma-pool");
  153. if (ret < 0) {
  154. NOTICE("BL2: Cannot append FCNL compat string %s (ret=%i)\n",
  155. "shared-dma-pool", 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 + 0x8U * no, reg);
  188. mmio_write_32(AXI_DCMPAREACRB0 + 0x8U * no, end_addr >> 20);
  189. mmio_write_32(AXI_DCMPAREACRA0 + 0x8U * no, reg | enable);
  190. info.magic = 0x12345678U;
  191. info.a0 = mmio_read_32(AXI_DCMPAREACRA0 + 0x8U * no);
  192. info.b0 = mmio_read_32(AXI_DCMPAREACRB0 + 0x8U * no);
  193. mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no, info.magic);
  194. mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no + 0x4U, info.a0);
  195. mmio_write_32(LOSSY_PARAMS_BASE + sizeof(info) * no + 0x8U, info.b0);
  196. NOTICE(" Entry %d: DCMPAREACRAx:0x%x DCMPAREACRBx:0x%x\n", no,
  197. mmio_read_32(AXI_DCMPAREACRA0 + 0x8U * no),
  198. mmio_read_32(AXI_DCMPAREACRB0 + 0x8U * no));
  199. }
  200. #endif /* RCAR_LOSSY_ENABLE == 1 */
  201. void bl2_plat_flush_bl31_params(void)
  202. {
  203. uint32_t product_cut, product, cut;
  204. uint32_t boot_dev, boot_cpu;
  205. uint32_t reg;
  206. reg = mmio_read_32(RCAR_MODEMR);
  207. boot_dev = reg & MODEMR_BOOT_DEV_MASK;
  208. if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
  209. boot_dev == MODEMR_BOOT_DEV_EMMC_50X8) {
  210. emmc_terminate();
  211. }
  212. if ((reg & MODEMR_BOOT_CPU_MASK) != MODEMR_BOOT_CPU_CR7) {
  213. bl2_secure_setting();
  214. }
  215. reg = mmio_read_32(RCAR_PRR);
  216. product_cut = reg & (PRR_PRODUCT_MASK | PRR_CUT_MASK);
  217. product = reg & PRR_PRODUCT_MASK;
  218. cut = reg & PRR_CUT_MASK;
  219. if (!((product == PRR_PRODUCT_M3 && cut < PRR_PRODUCT_30) ||
  220. (product == PRR_PRODUCT_H3 && cut < PRR_PRODUCT_20))) {
  221. /* Disable MFIS write protection */
  222. mmio_write_32(MFISWPCNTR, MFISWPCNTR_PASSWORD | 1U);
  223. }
  224. reg = mmio_read_32(RCAR_MODEMR);
  225. boot_cpu = reg & MODEMR_BOOT_CPU_MASK;
  226. if (boot_cpu == MODEMR_BOOT_CPU_CA57 ||
  227. boot_cpu == MODEMR_BOOT_CPU_CA53) {
  228. if (product_cut == PRR_PRODUCT_H3_CUT20) {
  229. mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
  230. mmio_write_32(IPMMUVI1_IMSCTLR, IMSCTLR_DISCACHE);
  231. mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
  232. mmio_write_32(IPMMUPV1_IMSCTLR, IMSCTLR_DISCACHE);
  233. mmio_write_32(IPMMUPV2_IMSCTLR, IMSCTLR_DISCACHE);
  234. mmio_write_32(IPMMUPV3_IMSCTLR, IMSCTLR_DISCACHE);
  235. } else if (product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_10) ||
  236. product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_11)) {
  237. mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
  238. mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
  239. } else if ((product_cut == (PRR_PRODUCT_E3 | PRR_PRODUCT_10)) ||
  240. (product_cut == (PRR_PRODUCT_E3 | PRR_PRODUCT_11))) {
  241. mmio_write_32(IPMMUVI0_IMSCTLR, IMSCTLR_DISCACHE);
  242. mmio_write_32(IPMMUVP0_IMSCTLR, IMSCTLR_DISCACHE);
  243. mmio_write_32(IPMMUPV0_IMSCTLR, IMSCTLR_DISCACHE);
  244. }
  245. if (product_cut == (PRR_PRODUCT_H3_CUT20) ||
  246. product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_10) ||
  247. product_cut == (PRR_PRODUCT_M3N | PRR_PRODUCT_11) ||
  248. product_cut == (PRR_PRODUCT_E3 | PRR_PRODUCT_10)) {
  249. mmio_write_32(IPMMUHC_IMSCTLR, IMSCTLR_DISCACHE);
  250. mmio_write_32(IPMMURT_IMSCTLR, IMSCTLR_DISCACHE);
  251. mmio_write_32(IPMMUMP_IMSCTLR, IMSCTLR_DISCACHE);
  252. mmio_write_32(IPMMUDS0_IMSCTLR, IMSCTLR_DISCACHE);
  253. mmio_write_32(IPMMUDS1_IMSCTLR, IMSCTLR_DISCACHE);
  254. }
  255. }
  256. mmio_write_32(IPMMUMM_IMSCTLR, IPMMUMM_IMSCTLR_ENABLE);
  257. mmio_write_32(IPMMUMM_IMAUXCTLR, IPMMUMM_IMAUXCTLR_NMERGE40_BIT);
  258. rcar_swdt_release();
  259. bl2_system_cpg_init();
  260. #if RCAR_BL2_DCACHE == 1
  261. /* Disable data cache (clean and invalidate) */
  262. disable_mmu_el3();
  263. #endif /* RCAR_BL2_DCACHE == 1 */
  264. }
  265. static uint32_t is_ddr_backup_mode(void)
  266. {
  267. #if RCAR_SYSTEM_SUSPEND
  268. static uint32_t reason = RCAR_COLD_BOOT;
  269. static uint32_t once;
  270. if (once != 0U) {
  271. return reason;
  272. }
  273. once = 1;
  274. if ((mmio_read_32(GPIO_INDT) & GPIO_BKUP_TRG_SHIFT) == 0U) {
  275. return reason;
  276. }
  277. reason = RCAR_WARM_BOOT;
  278. return reason;
  279. #else /* RCAR_SYSTEM_SUSPEND */
  280. return RCAR_COLD_BOOT;
  281. #endif /* RCAR_SYSTEM_SUSPEND */
  282. }
  283. int bl2_plat_handle_pre_image_load(unsigned int image_id)
  284. {
  285. u_register_t *boot_kind = (void *)BOOT_KIND_BASE;
  286. bl_mem_params_node_t *bl_mem_params;
  287. if (image_id != BL31_IMAGE_ID) {
  288. return 0;
  289. }
  290. bl_mem_params = get_bl_mem_params_node(image_id);
  291. if (is_ddr_backup_mode() != RCAR_COLD_BOOT) {
  292. *boot_kind = RCAR_WARM_BOOT;
  293. flush_dcache_range(BOOT_KIND_BASE, sizeof(*boot_kind));
  294. console_flush();
  295. bl2_plat_flush_bl31_params();
  296. /* will not return */
  297. bl2_enter_bl31(&bl_mem_params->ep_info);
  298. }
  299. *boot_kind = RCAR_COLD_BOOT;
  300. flush_dcache_range(BOOT_KIND_BASE, sizeof(*boot_kind));
  301. return 0;
  302. }
  303. static uint64_t rzg_get_dest_addr_from_cert(uint32_t certid, uintptr_t *dest)
  304. {
  305. uint32_t cert, len;
  306. int err;
  307. err = rcar_get_certificate(certid, &cert);
  308. if (err != 0) {
  309. ERROR("%s : cert file load error", __func__);
  310. return 1U;
  311. }
  312. rcar_read_certificate((uint64_t)cert, &len, dest);
  313. return 0U;
  314. }
  315. int bl2_plat_handle_post_image_load(unsigned int image_id)
  316. {
  317. static bl2_to_bl31_params_mem_t *params;
  318. bl_mem_params_node_t *bl_mem_params;
  319. uintptr_t dest;
  320. uint64_t ret;
  321. if (params == NULL) {
  322. params = (bl2_to_bl31_params_mem_t *)PARAMS_BASE;
  323. memset((void *)PARAMS_BASE, 0, sizeof(*params));
  324. }
  325. bl_mem_params = get_bl_mem_params_node(image_id);
  326. switch (image_id) {
  327. case BL31_IMAGE_ID:
  328. ret = rzg_get_dest_addr_from_cert(SOC_FW_CONTENT_CERT_ID,
  329. &dest);
  330. if (ret == 0U) {
  331. bl_mem_params->image_info.image_base = dest;
  332. }
  333. break;
  334. case BL32_IMAGE_ID:
  335. ret = rzg_get_dest_addr_from_cert(TRUSTED_OS_FW_CONTENT_CERT_ID,
  336. &dest);
  337. if (ret == 0U) {
  338. bl_mem_params->image_info.image_base = dest;
  339. }
  340. memcpy(&params->bl32_ep_info, &bl_mem_params->ep_info,
  341. sizeof(entry_point_info_t));
  342. break;
  343. case BL33_IMAGE_ID:
  344. memcpy(&params->bl33_ep_info, &bl_mem_params->ep_info,
  345. sizeof(entry_point_info_t));
  346. break;
  347. default:
  348. break;
  349. }
  350. return 0;
  351. }
  352. struct meminfo *bl2_plat_sec_mem_layout(void)
  353. {
  354. return &bl2_tzram_layout;
  355. }
  356. static void bl2_populate_compatible_string(void *dt)
  357. {
  358. uint32_t board_type;
  359. uint32_t board_rev;
  360. uint32_t reg;
  361. int ret;
  362. fdt_setprop_u32(dt, 0, "#address-cells", 2);
  363. fdt_setprop_u32(dt, 0, "#size-cells", 2);
  364. /* Populate compatible string */
  365. rzg_get_board_type(&board_type, &board_rev);
  366. switch (board_type) {
  367. case BOARD_HIHOPE_RZ_G2M:
  368. ret = fdt_setprop_string(dt, 0, "compatible",
  369. "hoperun,hihope-rzg2m");
  370. break;
  371. case BOARD_HIHOPE_RZ_G2H:
  372. ret = fdt_setprop_string(dt, 0, "compatible",
  373. "hoperun,hihope-rzg2h");
  374. break;
  375. case BOARD_HIHOPE_RZ_G2N:
  376. ret = fdt_setprop_string(dt, 0, "compatible",
  377. "hoperun,hihope-rzg2n");
  378. break;
  379. case BOARD_EK874_RZ_G2E:
  380. ret = fdt_setprop_string(dt, 0, "compatible",
  381. "si-linux,cat874");
  382. break;
  383. default:
  384. NOTICE("BL2: Cannot set compatible string, board unsupported\n");
  385. panic();
  386. break;
  387. }
  388. if (ret < 0) {
  389. NOTICE("BL2: Cannot set compatible string (ret=%i)\n", ret);
  390. panic();
  391. }
  392. reg = mmio_read_32(RCAR_PRR);
  393. switch (reg & PRR_PRODUCT_MASK) {
  394. case PRR_PRODUCT_M3:
  395. ret = fdt_appendprop_string(dt, 0, "compatible",
  396. "renesas,r8a774a1");
  397. break;
  398. case PRR_PRODUCT_H3:
  399. ret = fdt_appendprop_string(dt, 0, "compatible",
  400. "renesas,r8a774e1");
  401. break;
  402. case PRR_PRODUCT_M3N:
  403. ret = fdt_appendprop_string(dt, 0, "compatible",
  404. "renesas,r8a774b1");
  405. break;
  406. case PRR_PRODUCT_E3:
  407. ret = fdt_appendprop_string(dt, 0, "compatible",
  408. "renesas,r8a774c0");
  409. break;
  410. default:
  411. NOTICE("BL2: Cannot set compatible string, SoC unsupported\n");
  412. panic();
  413. break;
  414. }
  415. if (ret < 0) {
  416. NOTICE("BL2: Cannot set compatible string (ret=%i)\n", ret);
  417. panic();
  418. }
  419. }
  420. static int bl2_add_memory_node(uint64_t start, uint64_t size)
  421. {
  422. char nodename[32] = { 0 };
  423. uint64_t fdtsize;
  424. int ret, node;
  425. fdtsize = cpu_to_fdt64(size);
  426. snprintf(nodename, sizeof(nodename), "memory@");
  427. unsigned_num_print(start, 16, nodename + strlen(nodename));
  428. node = ret = fdt_add_subnode(fdt, 0, nodename);
  429. if (ret < 0) {
  430. return ret;
  431. }
  432. ret = fdt_setprop_string(fdt, node, "device_type", "memory");
  433. if (ret < 0) {
  434. return ret;
  435. }
  436. ret = fdt_setprop_u64(fdt, node, "reg", start);
  437. if (ret < 0) {
  438. return ret;
  439. }
  440. return fdt_appendprop(fdt, node, "reg", &fdtsize, sizeof(fdtsize));
  441. }
  442. static void bl2_advertise_dram_entries(uint64_t dram_config[8])
  443. {
  444. uint64_t start, size;
  445. int ret, chan;
  446. for (chan = 0; chan < MAX_DRAM_CHANNELS; chan++) {
  447. start = dram_config[2 * chan];
  448. size = dram_config[2 * chan + 1];
  449. if (size == 0U) {
  450. continue;
  451. }
  452. NOTICE("BL2: CH%d: %" PRIx64 " - %" PRIx64 ", %" PRId64 " %siB\n",
  453. chan, start, start + size - 1U,
  454. (size >> 30) ? : size >> 20,
  455. (size >> 30) ? "G" : "M");
  456. }
  457. /*
  458. * We add the DT nodes in reverse order here. The fdt_add_subnode()
  459. * adds the DT node before the first existing DT node, so we have
  460. * to add them in reverse order to get nodes sorted by address in
  461. * the resulting DT.
  462. */
  463. for (chan = MAX_DRAM_CHANNELS - 1; chan >= 0; chan--) {
  464. start = dram_config[2 * chan];
  465. size = dram_config[2 * chan + 1];
  466. if (size == 0U) {
  467. continue;
  468. }
  469. /*
  470. * Channel 0 is mapped in 32bit space and the first
  471. * 128 MiB are reserved
  472. */
  473. if (chan == 0) {
  474. /*
  475. * Maximum DDR size in Channel 0 for 32 bit space is 2GB, Add DT node
  476. * for remaining region in 64 bit address space
  477. */
  478. if (size > MAX_DRAM_SIZE_CH0_32BIT_ADDR_SPACE) {
  479. start = dram_config[chan] + MAX_DRAM_SIZE_CH0_32BIT_ADDR_SPACE;
  480. size -= MAX_DRAM_SIZE_CH0_32BIT_ADDR_SPACE;
  481. ret = bl2_add_memory_node(start, size);
  482. if (ret < 0) {
  483. goto err;
  484. }
  485. }
  486. start = 0x48000000U;
  487. size -= 0x8000000U;
  488. }
  489. ret = bl2_add_memory_node(start, size);
  490. if (ret < 0) {
  491. goto err;
  492. }
  493. }
  494. return;
  495. err:
  496. NOTICE("BL2: Cannot add memory node to FDT (ret=%i)\n", ret);
  497. panic();
  498. }
  499. static void bl2_advertise_dram_size(uint32_t product)
  500. {
  501. uint64_t dram_config[8] = {
  502. [0] = 0x400000000ULL,
  503. [2] = 0x500000000ULL,
  504. [4] = 0x600000000ULL,
  505. [6] = 0x700000000ULL,
  506. };
  507. switch (product) {
  508. case PRR_PRODUCT_M3:
  509. /* 4GB(2GBx2 2ch split) */
  510. dram_config[1] = 0x80000000ULL;
  511. dram_config[5] = 0x80000000ULL;
  512. break;
  513. case PRR_PRODUCT_H3:
  514. #if (RCAR_DRAM_LPDDR4_MEMCONF == 0)
  515. /* 4GB(1GBx4) */
  516. dram_config[1] = 0x40000000ULL;
  517. dram_config[3] = 0x40000000ULL;
  518. dram_config[5] = 0x40000000ULL;
  519. dram_config[7] = 0x40000000ULL;
  520. #elif (RCAR_DRAM_LPDDR4_MEMCONF == 1) && (RCAR_DRAM_CHANNEL == 5) && \
  521. (RCAR_DRAM_SPLIT == 2)
  522. /* 4GB(2GBx2 2ch split) */
  523. dram_config[1] = 0x80000000ULL;
  524. dram_config[3] = 0x80000000ULL;
  525. #elif (RCAR_DRAM_LPDDR4_MEMCONF == 1) && (RCAR_DRAM_CHANNEL == 15)
  526. /* 8GB(2GBx4: default) */
  527. dram_config[1] = 0x80000000ULL;
  528. dram_config[3] = 0x80000000ULL;
  529. dram_config[5] = 0x80000000ULL;
  530. dram_config[7] = 0x80000000ULL;
  531. #endif /* RCAR_DRAM_LPDDR4_MEMCONF == 0 */
  532. break;
  533. case PRR_PRODUCT_M3N:
  534. /* 4GB(4GBx1) */
  535. dram_config[1] = 0x100000000ULL;
  536. break;
  537. case PRR_PRODUCT_E3:
  538. #if (RCAR_DRAM_DDR3L_MEMCONF == 0)
  539. /* 1GB(512MBx2) */
  540. dram_config[1] = 0x40000000ULL;
  541. #elif (RCAR_DRAM_DDR3L_MEMCONF == 1)
  542. /* 2GB(512MBx4) */
  543. dram_config[1] = 0x80000000ULL;
  544. #elif (RCAR_DRAM_DDR3L_MEMCONF == 2)
  545. /* 4GB(1GBx4) */
  546. dram_config[1] = 0x100000000ULL;
  547. #endif /* RCAR_DRAM_DDR3L_MEMCONF == 0 */
  548. break;
  549. default:
  550. NOTICE("BL2: Detected invalid DRAM entries\n");
  551. break;
  552. }
  553. bl2_advertise_dram_entries(dram_config);
  554. }
  555. void bl2_el3_early_platform_setup(u_register_t arg1, u_register_t arg2,
  556. u_register_t arg3, u_register_t arg4)
  557. {
  558. uint32_t reg, midr, boot_dev, boot_cpu, type, rev;
  559. uint32_t product, product_cut, major, minor;
  560. int32_t ret;
  561. const char *str;
  562. const char *unknown = "unknown";
  563. const char *cpu_ca57 = "CA57";
  564. const char *cpu_ca53 = "CA53";
  565. const char *product_g2e = "G2E";
  566. const char *product_g2h = "G2H";
  567. const char *product_g2m = "G2M";
  568. const char *product_g2n = "G2N";
  569. const char *boot_hyper80 = "HyperFlash(80MHz)";
  570. const char *boot_qspi40 = "QSPI Flash(40MHz)";
  571. const char *boot_qspi80 = "QSPI Flash(80MHz)";
  572. const char *boot_emmc25x1 = "eMMC(25MHz x1)";
  573. const char *boot_emmc50x8 = "eMMC(50MHz x8)";
  574. #if (RCAR_LSI == RZ_G2E)
  575. uint32_t sscg;
  576. const char *sscg_on = "PLL1 SSCG Clock select";
  577. const char *sscg_off = "PLL1 nonSSCG Clock select";
  578. const char *boot_hyper160 = "HyperFlash(150MHz)";
  579. #else
  580. const char *boot_hyper160 = "HyperFlash(160MHz)";
  581. #endif /* RCAR_LSI == RZ_G2E */
  582. #if RZG_LCS_STATE_DETECTION_ENABLE
  583. uint32_t lcs;
  584. const char *lcs_secure = "SE";
  585. const char *lcs_cm = "CM";
  586. const char *lcs_dm = "DM";
  587. const char *lcs_sd = "SD";
  588. const char *lcs_fa = "FA";
  589. #endif /* RZG_LCS_STATE_DETECTION_ENABLE */
  590. #if (RCAR_LOSSY_ENABLE == 1)
  591. int fcnlnode;
  592. #endif /* (RCAR_LOSSY_ENABLE == 1) */
  593. bl2_init_generic_timer();
  594. reg = mmio_read_32(RCAR_MODEMR);
  595. boot_dev = reg & MODEMR_BOOT_DEV_MASK;
  596. boot_cpu = reg & MODEMR_BOOT_CPU_MASK;
  597. bl2_cpg_init();
  598. if (boot_cpu == MODEMR_BOOT_CPU_CA57 ||
  599. boot_cpu == MODEMR_BOOT_CPU_CA53) {
  600. rzg_pfc_init();
  601. rcar_console_boot_init();
  602. }
  603. plat_rcar_gic_driver_init();
  604. plat_rcar_gic_init();
  605. rcar_swdt_init();
  606. /* FIQ interrupts are taken to EL3 */
  607. write_scr_el3(read_scr_el3() | SCR_FIQ_BIT);
  608. write_daifclr(DAIF_FIQ_BIT);
  609. reg = read_midr();
  610. midr = reg & (MIDR_PN_MASK << MIDR_PN_SHIFT);
  611. switch (midr) {
  612. case MIDR_CA57:
  613. str = cpu_ca57;
  614. break;
  615. case MIDR_CA53:
  616. str = cpu_ca53;
  617. break;
  618. default:
  619. str = unknown;
  620. break;
  621. }
  622. NOTICE("BL2: RZ/G2 Initial Program Loader(%s) Rev.%s\n", str,
  623. version_of_renesas);
  624. reg = mmio_read_32(RCAR_PRR);
  625. product_cut = reg & (PRR_PRODUCT_MASK | PRR_CUT_MASK);
  626. product = reg & PRR_PRODUCT_MASK;
  627. switch (product) {
  628. case PRR_PRODUCT_M3:
  629. str = product_g2m;
  630. break;
  631. case PRR_PRODUCT_H3:
  632. str = product_g2h;
  633. break;
  634. case PRR_PRODUCT_M3N:
  635. str = product_g2n;
  636. break;
  637. case PRR_PRODUCT_E3:
  638. str = product_g2e;
  639. break;
  640. default:
  641. str = unknown;
  642. break;
  643. }
  644. if ((product == PRR_PRODUCT_M3) &&
  645. ((reg & RCAR_MAJOR_MASK) == PRR_PRODUCT_20)) {
  646. if ((reg & PRR_CUT_MASK) == RCAR_M3_CUT_VER11) {
  647. /* M3 Ver.1.1 or Ver.1.2 */
  648. NOTICE("BL2: PRR is RZ/%s Ver.1.1 / Ver.1.2\n", str);
  649. } else {
  650. NOTICE("BL2: PRR is RZ/%s Ver.1.%d\n", str,
  651. (reg & RCAR_MINOR_MASK) + RCAR_M3_MINOR_OFFSET);
  652. }
  653. } else {
  654. major = (reg & RCAR_MAJOR_MASK) >> RCAR_MAJOR_SHIFT;
  655. major = major + RCAR_MAJOR_OFFSET;
  656. minor = reg & RCAR_MINOR_MASK;
  657. NOTICE("BL2: PRR is RZ/%s Ver.%d.%d\n", str, major, minor);
  658. }
  659. #if (RCAR_LSI == RZ_G2E)
  660. if (product == PRR_PRODUCT_E3) {
  661. reg = mmio_read_32(RCAR_MODEMR);
  662. sscg = reg & RCAR_SSCG_MASK;
  663. str = sscg == RCAR_SSCG_ENABLE ? sscg_on : sscg_off;
  664. NOTICE("BL2: %s\n", str);
  665. }
  666. #endif /* RCAR_LSI == RZ_G2E */
  667. rzg_get_board_type(&type, &rev);
  668. switch (type) {
  669. case BOARD_HIHOPE_RZ_G2M:
  670. case BOARD_HIHOPE_RZ_G2H:
  671. case BOARD_HIHOPE_RZ_G2N:
  672. case BOARD_EK874_RZ_G2E:
  673. break;
  674. default:
  675. type = BOARD_UNKNOWN;
  676. break;
  677. }
  678. if (type == BOARD_UNKNOWN || rev == BOARD_REV_UNKNOWN) {
  679. NOTICE("BL2: Board is %s Rev.---\n", GET_BOARD_NAME(type));
  680. } else {
  681. NOTICE("BL2: Board is %s Rev.%d.%d\n",
  682. GET_BOARD_NAME(type),
  683. GET_BOARD_MAJOR(rev), GET_BOARD_MINOR(rev));
  684. }
  685. #if RCAR_LSI != RCAR_AUTO
  686. if (product != TARGET_PRODUCT) {
  687. ERROR("BL2: IPL was been built for the %s.\n", TARGET_NAME);
  688. ERROR("BL2: Please write the correct IPL to flash memory.\n");
  689. panic();
  690. }
  691. #endif /* RCAR_LSI != RCAR_AUTO */
  692. rcar_avs_init();
  693. rcar_avs_setting();
  694. switch (boot_dev) {
  695. case MODEMR_BOOT_DEV_HYPERFLASH160:
  696. str = boot_hyper160;
  697. break;
  698. case MODEMR_BOOT_DEV_HYPERFLASH80:
  699. str = boot_hyper80;
  700. break;
  701. case MODEMR_BOOT_DEV_QSPI_FLASH40:
  702. str = boot_qspi40;
  703. break;
  704. case MODEMR_BOOT_DEV_QSPI_FLASH80:
  705. str = boot_qspi80;
  706. break;
  707. case MODEMR_BOOT_DEV_EMMC_25X1:
  708. str = boot_emmc25x1;
  709. break;
  710. case MODEMR_BOOT_DEV_EMMC_50X8:
  711. str = boot_emmc50x8;
  712. break;
  713. default:
  714. str = unknown;
  715. break;
  716. }
  717. NOTICE("BL2: Boot device is %s\n", str);
  718. rcar_avs_setting();
  719. #if RZG_LCS_STATE_DETECTION_ENABLE
  720. reg = rcar_rom_get_lcs(&lcs);
  721. if (reg != 0U) {
  722. str = unknown;
  723. goto lcm_state;
  724. }
  725. switch (lcs) {
  726. case LCS_CM:
  727. str = lcs_cm;
  728. break;
  729. case LCS_DM:
  730. str = lcs_dm;
  731. break;
  732. case LCS_SD:
  733. str = lcs_sd;
  734. break;
  735. case LCS_SE:
  736. str = lcs_secure;
  737. break;
  738. case LCS_FA:
  739. str = lcs_fa;
  740. break;
  741. default:
  742. str = unknown;
  743. break;
  744. }
  745. lcm_state:
  746. NOTICE("BL2: LCM state is %s\n", str);
  747. #endif /* RZG_LCS_STATE_DETECTION_ENABLE */
  748. rcar_avs_end();
  749. is_ddr_backup_mode();
  750. bl2_tzram_layout.total_base = BL31_BASE;
  751. bl2_tzram_layout.total_size = BL31_LIMIT - BL31_BASE;
  752. if (boot_cpu == MODEMR_BOOT_CPU_CA57 ||
  753. boot_cpu == MODEMR_BOOT_CPU_CA53) {
  754. ret = rcar_dram_init();
  755. if (ret != 0) {
  756. NOTICE("BL2: Failed to DRAM initialize (%d).\n", ret);
  757. panic();
  758. }
  759. rzg_qos_init();
  760. }
  761. /* Set up FDT */
  762. ret = fdt_create_empty_tree(fdt, sizeof(fdt_blob));
  763. if (ret != 0) {
  764. NOTICE("BL2: Cannot allocate FDT for U-Boot (ret=%i)\n", ret);
  765. panic();
  766. }
  767. /* Add platform compatible string */
  768. bl2_populate_compatible_string(fdt);
  769. /* Print DRAM layout */
  770. bl2_advertise_dram_size(product);
  771. if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
  772. boot_dev == MODEMR_BOOT_DEV_EMMC_50X8) {
  773. if (rcar_emmc_init() != EMMC_SUCCESS) {
  774. NOTICE("BL2: Failed to eMMC driver initialize.\n");
  775. panic();
  776. }
  777. rcar_emmc_memcard_power(EMMC_POWER_ON);
  778. if (rcar_emmc_mount() != EMMC_SUCCESS) {
  779. NOTICE("BL2: Failed to eMMC mount operation.\n");
  780. panic();
  781. }
  782. } else {
  783. rcar_rpc_init();
  784. rcar_dma_init();
  785. }
  786. reg = mmio_read_32(RST_WDTRSTCR);
  787. reg &= ~WDTRSTCR_RWDT_RSTMSK;
  788. reg |= WDTRSTCR_PASSWORD;
  789. mmio_write_32(RST_WDTRSTCR, reg);
  790. mmio_write_32(CPG_CPGWPR, CPGWPR_PASSWORD);
  791. mmio_write_32(CPG_CPGWPCR, CPGWPCR_PASSWORD);
  792. reg = mmio_read_32(RCAR_PRR);
  793. if ((reg & RCAR_CPU_MASK_CA57) == RCAR_CPU_HAVE_CA57) {
  794. mmio_write_32(CPG_CA57DBGRCR,
  795. DBGCPUPREN | mmio_read_32(CPG_CA57DBGRCR));
  796. }
  797. if ((reg & RCAR_CPU_MASK_CA53) == RCAR_CPU_HAVE_CA53) {
  798. mmio_write_32(CPG_CA53DBGRCR,
  799. DBGCPUPREN | mmio_read_32(CPG_CA53DBGRCR));
  800. }
  801. if (product_cut == PRR_PRODUCT_H3_CUT10) {
  802. reg = mmio_read_32(CPG_PLL2CR);
  803. reg &= ~((uint32_t)1 << 5);
  804. mmio_write_32(CPG_PLL2CR, reg);
  805. reg = mmio_read_32(CPG_PLL4CR);
  806. reg &= ~((uint32_t)1 << 5);
  807. mmio_write_32(CPG_PLL4CR, reg);
  808. reg = mmio_read_32(CPG_PLL0CR);
  809. reg &= ~((uint32_t)1 << 12);
  810. mmio_write_32(CPG_PLL0CR, reg);
  811. }
  812. #if (RCAR_LOSSY_ENABLE == 1)
  813. NOTICE("BL2: Lossy Decomp areas\n");
  814. fcnlnode = fdt_add_subnode(fdt, 0, "reserved-memory");
  815. if (fcnlnode < 0) {
  816. NOTICE("BL2: Cannot create reserved mem node (ret=%i)\n",
  817. fcnlnode);
  818. panic();
  819. }
  820. bl2_lossy_setting(0, LOSSY_ST_ADDR0, LOSSY_END_ADDR0,
  821. LOSSY_FMT0, LOSSY_ENA_DIS0, fcnlnode);
  822. bl2_lossy_setting(1, LOSSY_ST_ADDR1, LOSSY_END_ADDR1,
  823. LOSSY_FMT1, LOSSY_ENA_DIS1, fcnlnode);
  824. bl2_lossy_setting(2, LOSSY_ST_ADDR2, LOSSY_END_ADDR2,
  825. LOSSY_FMT2, LOSSY_ENA_DIS2, fcnlnode);
  826. #endif /* RCAR_LOSSY_ENABLE */
  827. fdt_pack(fdt);
  828. NOTICE("BL2: FDT at %p\n", fdt);
  829. if (boot_dev == MODEMR_BOOT_DEV_EMMC_25X1 ||
  830. boot_dev == MODEMR_BOOT_DEV_EMMC_50X8) {
  831. rcar_io_emmc_setup();
  832. } else {
  833. rcar_io_setup();
  834. }
  835. }
  836. void bl2_el3_plat_arch_setup(void)
  837. {
  838. #if RCAR_BL2_DCACHE == 1
  839. NOTICE("BL2: D-Cache enable\n");
  840. rcar_configure_mmu_el3(BL2_BASE,
  841. BL2_END - BL2_BASE,
  842. BL2_RO_BASE, BL2_RO_LIMIT
  843. #if USE_COHERENT_MEM
  844. , BL2_COHERENT_RAM_BASE, BL2_COHERENT_RAM_LIMIT
  845. #endif /* USE_COHERENT_MEM */
  846. );
  847. #endif /* RCAR_BL2_DCACHE == 1 */
  848. }
  849. void bl2_platform_setup(void)
  850. {
  851. /*
  852. * Place holder for performing any platform initialization specific
  853. * to BL2.
  854. */
  855. }
  856. static void bl2_init_generic_timer(void)
  857. {
  858. #if RCAR_LSI == RZ_G2E
  859. uint32_t reg_cntfid = EXTAL_EBISU;
  860. #else
  861. uint32_t reg_cntfid;
  862. uint32_t modemr;
  863. uint32_t modemr_pll;
  864. uint32_t pll_table[] = {
  865. EXTAL_MD14_MD13_TYPE_0, /* MD14/MD13 : 0b00 */
  866. EXTAL_MD14_MD13_TYPE_1, /* MD14/MD13 : 0b01 */
  867. EXTAL_MD14_MD13_TYPE_2, /* MD14/MD13 : 0b10 */
  868. EXTAL_MD14_MD13_TYPE_3 /* MD14/MD13 : 0b11 */
  869. };
  870. modemr = mmio_read_32(RCAR_MODEMR);
  871. modemr_pll = (modemr & MODEMR_BOOT_PLL_MASK);
  872. /* Set frequency data in CNTFID0 */
  873. reg_cntfid = pll_table[modemr_pll >> MODEMR_BOOT_PLL_SHIFT];
  874. #endif /* RCAR_LSI == RZ_G2E */
  875. /* Update memory mapped and register based frequency */
  876. write_cntfrq_el0((u_register_t)reg_cntfid);
  877. mmio_write_32(ARM_SYS_CNTCTL_BASE + (uintptr_t)CNTFID_OFF, reg_cntfid);
  878. /* Enable counter */
  879. mmio_setbits_32(RCAR_CNTC_BASE + (uintptr_t)CNTCR_OFF,
  880. (uint32_t)CNTCR_EN);
  881. }