spl_boot.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2012 Samsung Electronics
  4. */
  5. #include <common.h>
  6. #include <config.h>
  7. #include <asm/arch/clock.h>
  8. #include <asm/arch/clk.h>
  9. #include <asm/arch/dmc.h>
  10. #include <asm/arch/periph.h>
  11. #include <asm/arch/pinmux.h>
  12. #include <asm/arch/power.h>
  13. #include <asm/arch/spl.h>
  14. #include <asm/arch/spi.h>
  15. #include "common_setup.h"
  16. #include "clock_init.h"
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /* Index into irom ptr table */
  19. enum index {
  20. MMC_INDEX,
  21. EMMC44_INDEX,
  22. EMMC44_END_INDEX,
  23. SPI_INDEX,
  24. USB_INDEX,
  25. };
  26. /* IROM Function Pointers Table */
  27. u32 irom_ptr_table[] = {
  28. [MMC_INDEX] = 0x02020030, /* iROM Function Pointer-SDMMC boot */
  29. [EMMC44_INDEX] = 0x02020044, /* iROM Function Pointer-EMMC4.4 boot*/
  30. [EMMC44_END_INDEX] = 0x02020048,/* iROM Function Pointer
  31. -EMMC4.4 end boot operation */
  32. [SPI_INDEX] = 0x02020058, /* iROM Function Pointer-SPI boot */
  33. [USB_INDEX] = 0x02020070, /* iROM Function Pointer-USB boot*/
  34. };
  35. void *get_irom_func(int index)
  36. {
  37. return (void *)*(u32 *)irom_ptr_table[index];
  38. }
  39. #ifdef CONFIG_USB_BOOTING
  40. /*
  41. * Set/clear program flow prediction and return the previous state.
  42. */
  43. static int config_branch_prediction(int set_cr_z)
  44. {
  45. unsigned int cr;
  46. /* System Control Register: 11th bit Z Branch prediction enable */
  47. cr = get_cr();
  48. set_cr(set_cr_z ? cr | CR_Z : cr & ~CR_Z);
  49. return cr & CR_Z;
  50. }
  51. #endif
  52. #ifdef CONFIG_SPI_BOOTING
  53. static void spi_rx_tx(struct exynos_spi *regs, int todo,
  54. void *dinp, void const *doutp, int i)
  55. {
  56. uint *rxp = (uint *)(dinp + (i * (32 * 1024)));
  57. int rx_lvl, tx_lvl;
  58. uint out_bytes, in_bytes;
  59. out_bytes = todo;
  60. in_bytes = todo;
  61. setbits_le32(&regs->ch_cfg, SPI_CH_RST);
  62. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  63. writel(((todo * 8) / 32) | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
  64. while (in_bytes) {
  65. uint32_t spi_sts;
  66. int temp;
  67. spi_sts = readl(&regs->spi_sts);
  68. rx_lvl = ((spi_sts >> 15) & 0x7f);
  69. tx_lvl = ((spi_sts >> 6) & 0x7f);
  70. while (tx_lvl < 32 && out_bytes) {
  71. temp = 0xffffffff;
  72. writel(temp, &regs->tx_data);
  73. out_bytes -= 4;
  74. tx_lvl += 4;
  75. }
  76. while (rx_lvl >= 4 && in_bytes) {
  77. temp = readl(&regs->rx_data);
  78. if (rxp)
  79. *rxp++ = temp;
  80. in_bytes -= 4;
  81. rx_lvl -= 4;
  82. }
  83. }
  84. }
  85. /*
  86. * Copy uboot from spi flash to RAM
  87. *
  88. * @parma uboot_size size of u-boot to copy
  89. * @param uboot_addr address in u-boot to copy
  90. */
  91. static void exynos_spi_copy(unsigned int uboot_size, unsigned int uboot_addr)
  92. {
  93. int upto, todo;
  94. int i, timeout = 100;
  95. struct exynos_spi *regs = (struct exynos_spi *)CONFIG_ENV_SPI_BASE;
  96. set_spi_clk(PERIPH_ID_SPI1, 50000000); /* set spi clock to 50Mhz */
  97. /* set the spi1 GPIO */
  98. exynos_pinmux_config(PERIPH_ID_SPI1, PINMUX_FLAG_NONE);
  99. /* set pktcnt and enable it */
  100. writel(4 | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
  101. /* set FB_CLK_SEL */
  102. writel(SPI_FB_DELAY_180, &regs->fb_clk);
  103. /* set CH_WIDTH and BUS_WIDTH as word */
  104. setbits_le32(&regs->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
  105. SPI_MODE_BUS_WIDTH_WORD);
  106. clrbits_le32(&regs->ch_cfg, SPI_CH_CPOL_L); /* CPOL: active high */
  107. /* clear rx and tx channel if set priveously */
  108. clrbits_le32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON);
  109. setbits_le32(&regs->swap_cfg, SPI_RX_SWAP_EN |
  110. SPI_RX_BYTE_SWAP |
  111. SPI_RX_HWORD_SWAP);
  112. /* do a soft reset */
  113. setbits_le32(&regs->ch_cfg, SPI_CH_RST);
  114. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  115. /* now set rx and tx channel ON */
  116. setbits_le32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON | SPI_CH_HS_EN);
  117. clrbits_le32(&regs->cs_reg, SPI_SLAVE_SIG_INACT); /* CS low */
  118. /* Send read instruction (0x3h) followed by a 24 bit addr */
  119. writel((SF_READ_DATA_CMD << 24) | SPI_FLASH_UBOOT_POS, &regs->tx_data);
  120. /* waiting for TX done */
  121. while (!(readl(&regs->spi_sts) & SPI_ST_TX_DONE)) {
  122. if (!timeout) {
  123. debug("SPI TIMEOUT\n");
  124. break;
  125. }
  126. timeout--;
  127. }
  128. for (upto = 0, i = 0; upto < uboot_size; upto += todo, i++) {
  129. todo = min(uboot_size - upto, (unsigned int)(1 << 15));
  130. spi_rx_tx(regs, todo, (void *)(uboot_addr),
  131. (void *)(SPI_FLASH_UBOOT_POS), i);
  132. }
  133. setbits_le32(&regs->cs_reg, SPI_SLAVE_SIG_INACT);/* make the CS high */
  134. /*
  135. * Let put controller mode to BYTE as
  136. * SPI driver does not support WORD mode yet
  137. */
  138. clrbits_le32(&regs->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
  139. SPI_MODE_BUS_WIDTH_WORD);
  140. writel(0, &regs->swap_cfg);
  141. /*
  142. * Flush spi tx, rx fifos and reset the SPI controller
  143. * and clear rx/tx channel
  144. */
  145. clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
  146. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  147. clrbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
  148. }
  149. #endif
  150. /*
  151. * Copy U-Boot from mmc to RAM:
  152. * COPY_BL2_FNPTR_ADDR: Address in iRAM, which Contains
  153. * Pointer to API (Data transfer from mmc to ram)
  154. */
  155. void copy_uboot_to_ram(void)
  156. {
  157. unsigned int bootmode = BOOT_MODE_OM;
  158. u32 (*copy_bl2)(u32 offset, u32 nblock, u32 dst) = NULL;
  159. u32 offset = 0, size = 0;
  160. #ifdef CONFIG_SPI_BOOTING
  161. struct spl_machine_param *param = spl_get_machine_params();
  162. #endif
  163. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  164. u32 (*copy_bl2_from_emmc)(u32 nblock, u32 dst);
  165. void (*end_bootop_from_emmc)(void);
  166. #endif
  167. #ifdef CONFIG_USB_BOOTING
  168. int is_cr_z_set;
  169. unsigned int sec_boot_check;
  170. /*
  171. * Note that older hardware (before Exynos5800) does not expect any
  172. * arguments, but it does not hurt to pass them, so a common function
  173. * prototype is used.
  174. */
  175. u32 (*usb_copy)(u32 num_of_block, u32 *dst);
  176. /* Read iRAM location to check for secondary USB boot mode */
  177. sec_boot_check = readl(EXYNOS_IRAM_SECONDARY_BASE);
  178. if (sec_boot_check == EXYNOS_USB_SECONDARY_BOOT)
  179. bootmode = BOOT_MODE_USB;
  180. #endif
  181. if (bootmode == BOOT_MODE_OM)
  182. bootmode = get_boot_mode();
  183. switch (bootmode) {
  184. #ifdef CONFIG_SPI_BOOTING
  185. case BOOT_MODE_SERIAL:
  186. /* Customised function to copy u-boot from SF */
  187. exynos_spi_copy(param->uboot_size, CONFIG_SYS_TEXT_BASE);
  188. break;
  189. #endif
  190. case BOOT_MODE_SD:
  191. offset = BL2_START_OFFSET;
  192. size = BL2_SIZE_BLOC_COUNT;
  193. copy_bl2 = get_irom_func(MMC_INDEX);
  194. break;
  195. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  196. case BOOT_MODE_EMMC:
  197. /* Set the FSYS1 clock divisor value for EMMC boot */
  198. emmc_boot_clk_div_set();
  199. copy_bl2_from_emmc = get_irom_func(EMMC44_INDEX);
  200. end_bootop_from_emmc = get_irom_func(EMMC44_END_INDEX);
  201. copy_bl2_from_emmc(BL2_SIZE_BLOC_COUNT, CONFIG_SYS_TEXT_BASE);
  202. end_bootop_from_emmc();
  203. break;
  204. #endif
  205. #ifdef CONFIG_USB_BOOTING
  206. case BOOT_MODE_USB:
  207. /*
  208. * iROM needs program flow prediction to be disabled
  209. * before copy from USB device to RAM
  210. */
  211. is_cr_z_set = config_branch_prediction(0);
  212. usb_copy = get_irom_func(USB_INDEX);
  213. usb_copy(0, (u32 *)CONFIG_SYS_TEXT_BASE);
  214. config_branch_prediction(is_cr_z_set);
  215. break;
  216. #endif
  217. default:
  218. break;
  219. }
  220. if (copy_bl2)
  221. copy_bl2(offset, size, CONFIG_SYS_TEXT_BASE);
  222. }
  223. void memzero(void *s, size_t n)
  224. {
  225. char *ptr = s;
  226. size_t i;
  227. for (i = 0; i < n; i++)
  228. *ptr++ = '\0';
  229. }
  230. /**
  231. * Set up the U-Boot global_data pointer
  232. *
  233. * This sets the address of the global data, and sets up basic values.
  234. *
  235. * @param gdp Value to give to gd
  236. */
  237. static void setup_global_data(gd_t *gdp)
  238. {
  239. gd = gdp;
  240. memzero((void *)gd, sizeof(gd_t));
  241. gd->flags |= GD_FLG_RELOC;
  242. gd->baudrate = CONFIG_BAUDRATE;
  243. gd->have_console = 1;
  244. }
  245. void board_init_f(unsigned long bootflag)
  246. {
  247. __aligned(8) gd_t local_gd;
  248. __attribute__((noreturn)) void (*uboot)(void);
  249. setup_global_data(&local_gd);
  250. if (do_lowlevel_init())
  251. power_exit_wakeup();
  252. copy_uboot_to_ram();
  253. /* Jump to U-Boot image */
  254. uboot = (void *)CONFIG_SYS_TEXT_BASE;
  255. (*uboot)();
  256. /* Never returns Here */
  257. }
  258. /* Place Holders */
  259. void board_init_r(gd_t *id, ulong dest_addr)
  260. {
  261. /* Function attribute is no-return */
  262. /* This Function never executes */
  263. while (1)
  264. ;
  265. }