warmboot.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2010 - 2011
  4. * NVIDIA Corporation <www.nvidia.com>
  5. */
  6. #include <common.h>
  7. #include <asm/io.h>
  8. #include <linux/errno.h>
  9. #include <asm/arch/clock.h>
  10. #include <asm/arch/emc.h>
  11. #include <asm/arch/gp_padctrl.h>
  12. #include <asm/arch/pinmux.h>
  13. #include <asm/arch/sdram_param.h>
  14. #include <asm/arch/tegra.h>
  15. #include <asm/arch-tegra/ap.h>
  16. #include <asm/arch-tegra/apb_misc.h>
  17. #include <asm/arch-tegra/clk_rst.h>
  18. #include <asm/arch-tegra/pmc.h>
  19. #include <asm/arch-tegra/fuse.h>
  20. #include <asm/arch-tegra/warmboot.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. #ifndef CONFIG_TEGRA_CLOCK_SCALING
  23. #error "You must enable CONFIG_TEGRA_CLOCK_SCALING to use CONFIG_TEGRA_LP0"
  24. #endif
  25. /*
  26. * This is the place in SRAM where the SDRAM parameters are stored. There
  27. * are 4 blocks, one for each RAM code
  28. */
  29. #define SDRAM_PARAMS_BASE (NV_PA_BASE_SRAM + 0x188)
  30. /* TODO: If we later add support for the Misc GP controller, refactor this */
  31. union xm2cfga_reg {
  32. struct {
  33. u32 reserved0:2;
  34. u32 hsm_en:1;
  35. u32 reserved1:2;
  36. u32 preemp_en:1;
  37. u32 vref_en:1;
  38. u32 reserved2:5;
  39. u32 cal_drvdn:5;
  40. u32 reserved3:3;
  41. u32 cal_drvup:5;
  42. u32 reserved4:3;
  43. u32 cal_drvdn_slwr:2;
  44. u32 cal_drvup_slwf:2;
  45. };
  46. u32 word;
  47. };
  48. union xm2cfgd_reg {
  49. struct {
  50. u32 reserved0:2;
  51. u32 hsm_en:1;
  52. u32 schmt_en:1;
  53. u32 lpmd:2;
  54. u32 vref_en:1;
  55. u32 reserved1:5;
  56. u32 cal_drvdn:5;
  57. u32 reserved2:3;
  58. u32 cal_drvup:5;
  59. u32 reserved3:3;
  60. u32 cal_drvdn_slwr:2;
  61. u32 cal_drvup_slwf:2;
  62. };
  63. u32 word;
  64. };
  65. /*
  66. * TODO: This register is not documented in the TRM yet. We could move this
  67. * into the EMC and give it a proper interface, but not while it is
  68. * undocumented.
  69. */
  70. union fbio_spare_reg {
  71. struct {
  72. u32 reserved:24;
  73. u32 cfg_wb0:8;
  74. };
  75. u32 word;
  76. };
  77. /* We pack the resume information into these unions for later */
  78. union scratch2_reg {
  79. struct {
  80. u32 pllm_base_divm:5;
  81. u32 pllm_base_divn:10;
  82. u32 pllm_base_divp:3;
  83. u32 pllm_misc_lfcon:4;
  84. u32 pllm_misc_cpcon:4;
  85. u32 gp_xm2cfga_padctrl_preemp:1;
  86. u32 gp_xm2cfgd_padctrl_schmt:1;
  87. u32 osc_ctrl_xobp:1;
  88. u32 memory_type:3;
  89. };
  90. u32 word;
  91. };
  92. union scratch4_reg {
  93. struct {
  94. u32 emc_clock_divider:8;
  95. u32 pllm_stable_time:8;
  96. u32 pllx_stable_time:8;
  97. u32 emc_fbio_spare_cfg_wb0:8;
  98. };
  99. u32 word;
  100. };
  101. union scratch24_reg {
  102. struct {
  103. u32 emc_auto_cal_wait:8;
  104. u32 emc_pin_program_wait:8;
  105. u32 warmboot_wait:8;
  106. u32 reserved:8;
  107. };
  108. u32 word;
  109. };
  110. int warmboot_save_sdram_params(void)
  111. {
  112. u32 ram_code;
  113. struct sdram_params sdram;
  114. struct apb_misc_pp_ctlr *apb_misc =
  115. (struct apb_misc_pp_ctlr *)NV_PA_APB_MISC_BASE;
  116. struct pmc_ctlr *pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
  117. struct apb_misc_gp_ctlr *gp =
  118. (struct apb_misc_gp_ctlr *)NV_PA_APB_MISC_GP_BASE;
  119. struct emc_ctlr *emc = emc_get_controller(gd->fdt_blob);
  120. union scratch2_reg scratch2;
  121. union scratch4_reg scratch4;
  122. union scratch24_reg scratch24;
  123. union xm2cfga_reg xm2cfga;
  124. union xm2cfgd_reg xm2cfgd;
  125. union fbio_spare_reg fbio_spare;
  126. /* get ram code that is used as index to array sdram_params in BCT */
  127. ram_code = (readl(&apb_misc->strapping_opt_a) >>
  128. STRAP_OPT_A_RAM_CODE_SHIFT) & 3;
  129. memcpy(&sdram,
  130. (char *)((struct sdram_params *)SDRAM_PARAMS_BASE + ram_code),
  131. sizeof(sdram));
  132. xm2cfga.word = readl(&gp->xm2cfga);
  133. xm2cfgd.word = readl(&gp->xm2cfgd);
  134. scratch2.word = 0;
  135. scratch2.osc_ctrl_xobp = clock_get_osc_bypass();
  136. /* Get the memory PLL settings */
  137. {
  138. u32 divm, divn, divp, cpcon, lfcon;
  139. if (clock_ll_read_pll(CLOCK_ID_MEMORY, &divm, &divn, &divp,
  140. &cpcon, &lfcon))
  141. return -1;
  142. scratch2.pllm_base_divm = divm;
  143. scratch2.pllm_base_divn = divn;
  144. scratch2.pllm_base_divp = divp;
  145. scratch2.pllm_misc_cpcon = cpcon;
  146. scratch2.pllm_misc_lfcon = lfcon;
  147. }
  148. scratch2.gp_xm2cfga_padctrl_preemp = xm2cfga.preemp_en;
  149. scratch2.gp_xm2cfgd_padctrl_schmt = xm2cfgd.schmt_en;
  150. scratch2.memory_type = sdram.memory_type;
  151. writel(scratch2.word, &pmc->pmc_scratch2);
  152. /* collect data from various sources for pmc_scratch4 */
  153. fbio_spare.word = readl(&emc->fbio_spare);
  154. scratch4.word = 0;
  155. scratch4.emc_fbio_spare_cfg_wb0 = fbio_spare.cfg_wb0;
  156. scratch4.emc_clock_divider = sdram.emc_clock_divider;
  157. scratch4.pllm_stable_time = -1;
  158. scratch4.pllx_stable_time = -1;
  159. writel(scratch4.word, &pmc->pmc_scratch4);
  160. /* collect various data from sdram for pmc_scratch24 */
  161. scratch24.word = 0;
  162. scratch24.emc_pin_program_wait = sdram.emc_pin_program_wait;
  163. scratch24.emc_auto_cal_wait = sdram.emc_auto_cal_wait;
  164. scratch24.warmboot_wait = sdram.warm_boot_wait;
  165. writel(scratch24.word, &pmc->pmc_scratch24);
  166. return 0;
  167. }
  168. static u32 get_major_version(void)
  169. {
  170. u32 major_id;
  171. struct apb_misc_gp_ctlr *gp =
  172. (struct apb_misc_gp_ctlr *)NV_PA_APB_MISC_GP_BASE;
  173. major_id = (readl(&gp->hidrev) & HIDREV_MAJORPREV_MASK) >>
  174. HIDREV_MAJORPREV_SHIFT;
  175. return major_id;
  176. }
  177. static int is_production_mode_fuse_set(struct fuse_regs *fuse)
  178. {
  179. return readl(&fuse->production_mode);
  180. }
  181. static int is_odm_production_mode_fuse_set(struct fuse_regs *fuse)
  182. {
  183. return readl(&fuse->security_mode);
  184. }
  185. static int is_failure_analysis_mode(struct fuse_regs *fuse)
  186. {
  187. return readl(&fuse->fa);
  188. }
  189. static int ap20_is_odm_production_mode(void)
  190. {
  191. struct fuse_regs *fuse = (struct fuse_regs *)NV_PA_FUSE_BASE;
  192. if (!is_failure_analysis_mode(fuse) &&
  193. is_odm_production_mode_fuse_set(fuse))
  194. return 1;
  195. else
  196. return 0;
  197. }
  198. static int ap20_is_production_mode(void)
  199. {
  200. struct fuse_regs *fuse = (struct fuse_regs *)NV_PA_FUSE_BASE;
  201. if (get_major_version() == 0)
  202. return 1;
  203. if (!is_failure_analysis_mode(fuse) &&
  204. is_production_mode_fuse_set(fuse) &&
  205. !is_odm_production_mode_fuse_set(fuse))
  206. return 1;
  207. else
  208. return 0;
  209. }
  210. static enum fuse_operating_mode fuse_get_operation_mode(void)
  211. {
  212. u32 chip_id;
  213. struct apb_misc_gp_ctlr *gp =
  214. (struct apb_misc_gp_ctlr *)NV_PA_APB_MISC_GP_BASE;
  215. chip_id = (readl(&gp->hidrev) & HIDREV_CHIPID_MASK) >>
  216. HIDREV_CHIPID_SHIFT;
  217. if (chip_id == CHIPID_TEGRA20) {
  218. if (ap20_is_odm_production_mode()) {
  219. printf("!! odm_production_mode is not supported !!\n");
  220. return MODE_UNDEFINED;
  221. } else
  222. if (ap20_is_production_mode())
  223. return MODE_PRODUCTION;
  224. else
  225. return MODE_UNDEFINED;
  226. }
  227. return MODE_UNDEFINED;
  228. }
  229. static void determine_crypto_options(int *is_encrypted, int *is_signed,
  230. int *use_zero_key)
  231. {
  232. switch (fuse_get_operation_mode()) {
  233. case MODE_PRODUCTION:
  234. *is_encrypted = 0;
  235. *is_signed = 1;
  236. *use_zero_key = 1;
  237. break;
  238. case MODE_UNDEFINED:
  239. default:
  240. *is_encrypted = 0;
  241. *is_signed = 0;
  242. *use_zero_key = 0;
  243. break;
  244. }
  245. }
  246. static int sign_wb_code(u32 start, u32 length, int use_zero_key)
  247. {
  248. int err;
  249. u8 *source; /* Pointer to source */
  250. u8 *hash;
  251. /* Calculate AES block parameters. */
  252. source = (u8 *)(start + offsetof(struct wb_header, random_aes_block));
  253. length -= offsetof(struct wb_header, random_aes_block);
  254. hash = (u8 *)(start + offsetof(struct wb_header, hash));
  255. err = sign_data_block(source, length, hash);
  256. return err;
  257. }
  258. int warmboot_prepare_code(u32 seg_address, u32 seg_length)
  259. {
  260. int err = 0;
  261. u32 length; /* length of the signed/encrypt code */
  262. struct wb_header *dst_header; /* Pointer to dest WB header */
  263. int is_encrypted; /* Segment is encrypted */
  264. int is_signed; /* Segment is signed */
  265. int use_zero_key; /* Use key of all zeros */
  266. /* Determine crypto options. */
  267. determine_crypto_options(&is_encrypted, &is_signed, &use_zero_key);
  268. /* Get the actual code limits. */
  269. length = roundup(((u32)wb_end - (u32)wb_start), 16);
  270. /*
  271. * The region specified by seg_address must be in SDRAM and must be
  272. * nonzero in length.
  273. */
  274. if (seg_length == 0 || seg_address < NV_PA_SDRAM_BASE ||
  275. seg_address + seg_length >= NV_PA_SDRAM_BASE + gd->ram_size) {
  276. err = -EFAULT;
  277. goto fail;
  278. }
  279. /* Things must be 16-byte aligned. */
  280. if ((seg_length & 0xF) || (seg_address & 0xF)) {
  281. err = -EINVAL;
  282. goto fail;
  283. }
  284. /* Will the code fit? (destination includes wb_header + wb code) */
  285. if (seg_length < (length + sizeof(struct wb_header))) {
  286. err = -EINVAL;
  287. goto fail;
  288. }
  289. dst_header = (struct wb_header *)seg_address;
  290. memset((char *)dst_header, 0, sizeof(struct wb_header));
  291. /* Populate the random_aes_block as requested. */
  292. {
  293. u32 *aes_block = (u32 *)&(dst_header->random_aes_block);
  294. u32 *end = (u32 *)(((u32)aes_block) +
  295. sizeof(dst_header->random_aes_block));
  296. do {
  297. *aes_block++ = 0;
  298. } while (aes_block < end);
  299. }
  300. /* Populate the header. */
  301. dst_header->length_insecure = length + sizeof(struct wb_header);
  302. dst_header->length_secure = length + sizeof(struct wb_header);
  303. dst_header->destination = NV_WB_RUN_ADDRESS;
  304. dst_header->entry_point = NV_WB_RUN_ADDRESS;
  305. dst_header->code_length = length;
  306. if (is_encrypted) {
  307. printf("!!!! Encryption is not supported !!!!\n");
  308. dst_header->length_insecure = 0;
  309. err = -EACCES;
  310. goto fail;
  311. } else
  312. /* copy the wb code directly following dst_header. */
  313. memcpy((char *)(dst_header+1), (char *)wb_start, length);
  314. if (is_signed)
  315. err = sign_wb_code(seg_address, dst_header->length_insecure,
  316. use_zero_key);
  317. fail:
  318. if (err)
  319. printf("Warning: warmboot code copy failed (error=%d)\n", err);
  320. return err;
  321. }