warmboot.c 9.6 KB

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