blob: bd5a06447b9c8977e06da9f26068921c0e7ac4bb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Chander Kashyap1633dd12012-02-05 23:01:48 +00002/*
3 * Copyright (C) 2012 Samsung Electronics
Chander Kashyap1633dd12012-02-05 23:01:48 +00004 */
5
Vasili Galka36ac1b52014-06-10 16:06:52 +03006#include <config.h>
Simon Glass97589732020-05-10 11:40:02 -06007#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Chander Kashyap1633dd12012-02-05 23:01:48 +000010
Simon Glass274e0b02020-05-10 11:39:56 -060011#include <asm/cache.h>
Amare33add82013-04-27 11:42:59 +053012#include <asm/arch/clock.h>
13#include <asm/arch/clk.h>
Rajeshwari Shindebed24422013-07-04 12:29:17 +053014#include <asm/arch/dmc.h>
Rajeshwari Shinde507f8922013-10-08 18:42:22 +053015#include <asm/arch/periph.h>
16#include <asm/arch/pinmux.h>
Rajeshwari Shindebed24422013-07-04 12:29:17 +053017#include <asm/arch/power.h>
Rajeshwari Shindebff6d0a2013-06-25 19:17:06 +053018#include <asm/arch/spl.h>
Rajeshwari Shinde507f8922013-10-08 18:42:22 +053019#include <asm/arch/spi.h>
Amare33add82013-04-27 11:42:59 +053020
Rajeshwari Shindebed24422013-07-04 12:29:17 +053021#include "common_setup.h"
Amare33add82013-04-27 11:42:59 +053022#include "clock_init.h"
23
Tom Rini0bbed0c2022-02-23 12:28:16 -050024#ifdef CONFIG_ARCH_EXYNOS5
25#define SECURE_BL1_ONLY
26
27/* Secure FW size configuration */
28#ifdef SECURE_BL1_ONLY
29#define SEC_FW_SIZE (8 << 10) /* 8KB */
30#else
31#define SEC_FW_SIZE 0
32#endif
33
34/* Configuration of BL1, BL2, ENV Blocks on mmc */
35#define RES_BLOCK_SIZE (512)
36#define BL1_SIZE (16 << 10) /*16 K reserved for BL1*/
37#define BL2_SIZE (512UL << 10UL) /* 512 KB */
38
39#define BL1_OFFSET (RES_BLOCK_SIZE + SEC_FW_SIZE)
40#define BL2_OFFSET (BL1_OFFSET + BL1_SIZE)
41
42/* U-Boot copy size from boot Media to DRAM.*/
43#define BL2_START_OFFSET (BL2_OFFSET/512)
44#define BL2_SIZE_BLOC_COUNT (BL2_SIZE/512)
45
46#define EXYNOS_COPY_SPI_FNPTR_ADDR 0x02020058
47#define SPI_FLASH_UBOOT_POS (SEC_FW_SIZE + BL1_SIZE)
48#elif defined(CONFIG_ARCH_EXYNOS4)
49#define COPY_BL2_SIZE 0x80000
50#define BL2_START_OFFSET ((CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE)/512)
51#define BL2_SIZE_BLOC_COUNT (COPY_BL2_SIZE/512)
52#endif
53
Rajeshwari Shindebed24422013-07-04 12:29:17 +053054DECLARE_GLOBAL_DATA_PTR;
Rajeshwari Shindebed24422013-07-04 12:29:17 +053055
Amare33add82013-04-27 11:42:59 +053056/* Index into irom ptr table */
57enum index {
58 MMC_INDEX,
59 EMMC44_INDEX,
60 EMMC44_END_INDEX,
61 SPI_INDEX,
62 USB_INDEX,
63};
64
65/* IROM Function Pointers Table */
66u32 irom_ptr_table[] = {
67 [MMC_INDEX] = 0x02020030, /* iROM Function Pointer-SDMMC boot */
68 [EMMC44_INDEX] = 0x02020044, /* iROM Function Pointer-EMMC4.4 boot*/
69 [EMMC44_END_INDEX] = 0x02020048,/* iROM Function Pointer
70 -EMMC4.4 end boot operation */
71 [SPI_INDEX] = 0x02020058, /* iROM Function Pointer-SPI boot */
72 [USB_INDEX] = 0x02020070, /* iROM Function Pointer-USB boot*/
73 };
74
Amare33add82013-04-27 11:42:59 +053075void *get_irom_func(int index)
76{
77 return (void *)*(u32 *)irom_ptr_table[index];
78}
Rajeshwari Shinde9cb48e82012-11-02 01:15:38 +000079
Rajeshwari Shindebed24422013-07-04 12:29:17 +053080#ifdef CONFIG_USB_BOOTING
Chander Kashyap1633dd12012-02-05 23:01:48 +000081/*
Vivek Gautam681dd832013-01-28 00:39:59 +000082 * Set/clear program flow prediction and return the previous state.
83 */
84static int config_branch_prediction(int set_cr_z)
85{
86 unsigned int cr;
87
88 /* System Control Register: 11th bit Z Branch prediction enable */
89 cr = get_cr();
90 set_cr(set_cr_z ? cr | CR_Z : cr & ~CR_Z);
91
92 return cr & CR_Z;
93}
Rajeshwari Shindebed24422013-07-04 12:29:17 +053094#endif
Vivek Gautam681dd832013-01-28 00:39:59 +000095
Minkyu Kang1609f512013-12-06 19:04:03 +090096#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde507f8922013-10-08 18:42:22 +053097static void spi_rx_tx(struct exynos_spi *regs, int todo,
98 void *dinp, void const *doutp, int i)
99{
100 uint *rxp = (uint *)(dinp + (i * (32 * 1024)));
101 int rx_lvl, tx_lvl;
102 uint out_bytes, in_bytes;
103
104 out_bytes = todo;
105 in_bytes = todo;
106 setbits_le32(&regs->ch_cfg, SPI_CH_RST);
107 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
108 writel(((todo * 8) / 32) | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
109
110 while (in_bytes) {
111 uint32_t spi_sts;
112 int temp;
113
114 spi_sts = readl(&regs->spi_sts);
115 rx_lvl = ((spi_sts >> 15) & 0x7f);
116 tx_lvl = ((spi_sts >> 6) & 0x7f);
117 while (tx_lvl < 32 && out_bytes) {
118 temp = 0xffffffff;
119 writel(temp, &regs->tx_data);
120 out_bytes -= 4;
121 tx_lvl += 4;
122 }
123 while (rx_lvl >= 4 && in_bytes) {
124 temp = readl(&regs->rx_data);
125 if (rxp)
126 *rxp++ = temp;
127 in_bytes -= 4;
128 rx_lvl -= 4;
129 }
130 }
131}
132
133/*
134 * Copy uboot from spi flash to RAM
135 *
136 * @parma uboot_size size of u-boot to copy
137 * @param uboot_addr address in u-boot to copy
138 */
139static void exynos_spi_copy(unsigned int uboot_size, unsigned int uboot_addr)
140{
141 int upto, todo;
142 int i, timeout = 100;
Tom Rini6a5dccc2022-11-16 13:10:41 -0500143 struct exynos_spi *regs = (struct exynos_spi *)CFG_SYS_SPI_BASE;
Rajeshwari Shinde507f8922013-10-08 18:42:22 +0530144
145 set_spi_clk(PERIPH_ID_SPI1, 50000000); /* set spi clock to 50Mhz */
146 /* set the spi1 GPIO */
147 exynos_pinmux_config(PERIPH_ID_SPI1, PINMUX_FLAG_NONE);
148
149 /* set pktcnt and enable it */
150 writel(4 | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
151 /* set FB_CLK_SEL */
152 writel(SPI_FB_DELAY_180, &regs->fb_clk);
153 /* set CH_WIDTH and BUS_WIDTH as word */
154 setbits_le32(&regs->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
155 SPI_MODE_BUS_WIDTH_WORD);
156 clrbits_le32(&regs->ch_cfg, SPI_CH_CPOL_L); /* CPOL: active high */
157
158 /* clear rx and tx channel if set priveously */
159 clrbits_le32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON);
160
161 setbits_le32(&regs->swap_cfg, SPI_RX_SWAP_EN |
162 SPI_RX_BYTE_SWAP |
163 SPI_RX_HWORD_SWAP);
164
165 /* do a soft reset */
166 setbits_le32(&regs->ch_cfg, SPI_CH_RST);
167 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
168
169 /* now set rx and tx channel ON */
170 setbits_le32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON | SPI_CH_HS_EN);
171 clrbits_le32(&regs->cs_reg, SPI_SLAVE_SIG_INACT); /* CS low */
172
173 /* Send read instruction (0x3h) followed by a 24 bit addr */
174 writel((SF_READ_DATA_CMD << 24) | SPI_FLASH_UBOOT_POS, &regs->tx_data);
175
176 /* waiting for TX done */
177 while (!(readl(&regs->spi_sts) & SPI_ST_TX_DONE)) {
178 if (!timeout) {
179 debug("SPI TIMEOUT\n");
180 break;
181 }
182 timeout--;
183 }
184
185 for (upto = 0, i = 0; upto < uboot_size; upto += todo, i++) {
Masahiro Yamadadb204642014-11-07 03:03:31 +0900186 todo = min(uboot_size - upto, (unsigned int)(1 << 15));
Rajeshwari Shinde507f8922013-10-08 18:42:22 +0530187 spi_rx_tx(regs, todo, (void *)(uboot_addr),
188 (void *)(SPI_FLASH_UBOOT_POS), i);
189 }
190
191 setbits_le32(&regs->cs_reg, SPI_SLAVE_SIG_INACT);/* make the CS high */
192
193 /*
194 * Let put controller mode to BYTE as
195 * SPI driver does not support WORD mode yet
196 */
197 clrbits_le32(&regs->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
198 SPI_MODE_BUS_WIDTH_WORD);
199 writel(0, &regs->swap_cfg);
200
201 /*
202 * Flush spi tx, rx fifos and reset the SPI controller
203 * and clear rx/tx channel
204 */
205 clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
206 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
207 clrbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
208}
Minkyu Kang1609f512013-12-06 19:04:03 +0900209#endif
Rajeshwari Shinde507f8922013-10-08 18:42:22 +0530210
Vivek Gautam681dd832013-01-28 00:39:59 +0000211/*
Bin Meng75574052016-02-05 19:30:11 -0800212* Copy U-Boot from mmc to RAM:
Chander Kashyap1633dd12012-02-05 23:01:48 +0000213* COPY_BL2_FNPTR_ADDR: Address in iRAM, which Contains
214* Pointer to API (Data transfer from mmc to ram)
215*/
216void copy_uboot_to_ram(void)
217{
Przemyslaw Marczak93976f72014-09-01 13:50:44 +0200218 unsigned int bootmode = BOOT_MODE_OM;
Amare33add82013-04-27 11:42:59 +0530219
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530220 u32 (*copy_bl2)(u32 offset, u32 nblock, u32 dst) = NULL;
221 u32 offset = 0, size = 0;
Minkyu Kang1609f512013-12-06 19:04:03 +0900222#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde507f8922013-10-08 18:42:22 +0530223 struct spl_machine_param *param = spl_get_machine_params();
Minkyu Kang1609f512013-12-06 19:04:03 +0900224#endif
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530225#ifdef CONFIG_SUPPORT_EMMC_BOOT
Amare33add82013-04-27 11:42:59 +0530226 u32 (*copy_bl2_from_emmc)(u32 nblock, u32 dst);
227 void (*end_bootop_from_emmc)(void);
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530228#endif
229#ifdef CONFIG_USB_BOOTING
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530230 int is_cr_z_set;
231 unsigned int sec_boot_check;
Rajeshwari Shinde9cb48e82012-11-02 01:15:38 +0000232
Vadim Bendeburyc12efec2014-11-13 22:38:21 +0530233 /*
234 * Note that older hardware (before Exynos5800) does not expect any
235 * arguments, but it does not hurt to pass them, so a common function
236 * prototype is used.
237 */
238 u32 (*usb_copy)(u32 num_of_block, u32 *dst);
239
Vivek Gautam681dd832013-01-28 00:39:59 +0000240 /* Read iRAM location to check for secondary USB boot mode */
241 sec_boot_check = readl(EXYNOS_IRAM_SECONDARY_BASE);
242 if (sec_boot_check == EXYNOS_USB_SECONDARY_BOOT)
243 bootmode = BOOT_MODE_USB;
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530244#endif
Vivek Gautam681dd832013-01-28 00:39:59 +0000245
246 if (bootmode == BOOT_MODE_OM)
Przemyslaw Marczak93976f72014-09-01 13:50:44 +0200247 bootmode = get_boot_mode();
Chander Kashyap1633dd12012-02-05 23:01:48 +0000248
Rajeshwari Shinde9cb48e82012-11-02 01:15:38 +0000249 switch (bootmode) {
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530250#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde9cb48e82012-11-02 01:15:38 +0000251 case BOOT_MODE_SERIAL:
Rajeshwari Shinde507f8922013-10-08 18:42:22 +0530252 /* Customised function to copy u-boot from SF */
Simon Glass72cc5382022-10-20 18:22:39 -0600253 exynos_spi_copy(param->uboot_size, CONFIG_TEXT_BASE);
Rajeshwari Shinde9cb48e82012-11-02 01:15:38 +0000254 break;
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530255#endif
Przemyslaw Marczak93976f72014-09-01 13:50:44 +0200256 case BOOT_MODE_SD:
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530257 offset = BL2_START_OFFSET;
258 size = BL2_SIZE_BLOC_COUNT;
Amare33add82013-04-27 11:42:59 +0530259 copy_bl2 = get_irom_func(MMC_INDEX);
Amare33add82013-04-27 11:42:59 +0530260 break;
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530261#ifdef CONFIG_SUPPORT_EMMC_BOOT
Amare33add82013-04-27 11:42:59 +0530262 case BOOT_MODE_EMMC:
263 /* Set the FSYS1 clock divisor value for EMMC boot */
264 emmc_boot_clk_div_set();
265
266 copy_bl2_from_emmc = get_irom_func(EMMC44_INDEX);
267 end_bootop_from_emmc = get_irom_func(EMMC44_END_INDEX);
268
Simon Glass72cc5382022-10-20 18:22:39 -0600269 copy_bl2_from_emmc(BL2_SIZE_BLOC_COUNT, CONFIG_TEXT_BASE);
Amare33add82013-04-27 11:42:59 +0530270 end_bootop_from_emmc();
Rajeshwari Shinde9cb48e82012-11-02 01:15:38 +0000271 break;
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530272#endif
273#ifdef CONFIG_USB_BOOTING
Vivek Gautam681dd832013-01-28 00:39:59 +0000274 case BOOT_MODE_USB:
275 /*
276 * iROM needs program flow prediction to be disabled
277 * before copy from USB device to RAM
278 */
279 is_cr_z_set = config_branch_prediction(0);
Amare33add82013-04-27 11:42:59 +0530280 usb_copy = get_irom_func(USB_INDEX);
Simon Glass72cc5382022-10-20 18:22:39 -0600281 usb_copy(0, (u32 *)CONFIG_TEXT_BASE);
Vivek Gautam681dd832013-01-28 00:39:59 +0000282 config_branch_prediction(is_cr_z_set);
283 break;
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530284#endif
Rajeshwari Shinde9cb48e82012-11-02 01:15:38 +0000285 default:
286 break;
287 }
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530288
289 if (copy_bl2)
Simon Glass72cc5382022-10-20 18:22:39 -0600290 copy_bl2(offset, size, CONFIG_TEXT_BASE);
Chander Kashyap1633dd12012-02-05 23:01:48 +0000291}
292
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530293void memzero(void *s, size_t n)
294{
295 char *ptr = s;
296 size_t i;
297
298 for (i = 0; i < n; i++)
299 *ptr++ = '\0';
300}
301
302/**
303 * Set up the U-Boot global_data pointer
304 *
305 * This sets the address of the global data, and sets up basic values.
306 *
307 * @param gdp Value to give to gd
308 */
309static void setup_global_data(gd_t *gdp)
310{
Marek BehĂșna8cf1552021-05-20 13:24:10 +0200311 set_gd(gdp);
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530312 memzero((void *)gd, sizeof(gd_t));
313 gd->flags |= GD_FLG_RELOC;
314 gd->baudrate = CONFIG_BAUDRATE;
315 gd->have_console = 1;
316}
317
Chander Kashyap1633dd12012-02-05 23:01:48 +0000318void board_init_f(unsigned long bootflag)
319{
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530320 __aligned(8) gd_t local_gd;
Chander Kashyap1633dd12012-02-05 23:01:48 +0000321 __attribute__((noreturn)) void (*uboot)(void);
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530322
323 setup_global_data(&local_gd);
324
325 if (do_lowlevel_init())
326 power_exit_wakeup();
327
Chander Kashyap1633dd12012-02-05 23:01:48 +0000328 copy_uboot_to_ram();
329
330 /* Jump to U-Boot image */
Simon Glass72cc5382022-10-20 18:22:39 -0600331 uboot = (void *)CONFIG_TEXT_BASE;
Chander Kashyap1633dd12012-02-05 23:01:48 +0000332 (*uboot)();
333 /* Never returns Here */
334}
335
336/* Place Holders */
337void board_init_r(gd_t *id, ulong dest_addr)
338{
339 /* Function attribute is no-return */
340 /* This Function never executes */
341 while (1)
342 ;
343}