warmboot.c 9.0 KB

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