blob: 93fea9c749a63f8127072ef4e8c5054009a1dd0d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Chander Kashyap81e35202012-02-05 23:01:48 +00002/*
3 * Copyright (C) 2012 Samsung Electronics
Chander Kashyap81e35202012-02-05 23:01:48 +00004 */
5
Vasili Galka4b9ca092014-06-10 16:06:52 +03006#include <common.h>
7#include <config.h>
Simon Glass691d7192020-05-10 11:40:02 -06008#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Chander Kashyap81e35202012-02-05 23:01:48 +000011
Simon Glass90526e92020-05-10 11:39:56 -060012#include <asm/cache.h>
Amarc748be02013-04-27 11:42:59 +053013#include <asm/arch/clock.h>
14#include <asm/arch/clk.h>
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053015#include <asm/arch/dmc.h>
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +053016#include <asm/arch/periph.h>
17#include <asm/arch/pinmux.h>
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053018#include <asm/arch/power.h>
Rajeshwari Shinde493c0732013-06-25 19:17:06 +053019#include <asm/arch/spl.h>
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +053020#include <asm/arch/spi.h>
Amarc748be02013-04-27 11:42:59 +053021
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053022#include "common_setup.h"
Amarc748be02013-04-27 11:42:59 +053023#include "clock_init.h"
24
Tom Rini43ea5642022-02-23 12:28:16 -050025#ifdef CONFIG_ARCH_EXYNOS5
26#define SECURE_BL1_ONLY
27
28/* Secure FW size configuration */
29#ifdef SECURE_BL1_ONLY
30#define SEC_FW_SIZE (8 << 10) /* 8KB */
31#else
32#define SEC_FW_SIZE 0
33#endif
34
35/* Configuration of BL1, BL2, ENV Blocks on mmc */
36#define RES_BLOCK_SIZE (512)
37#define BL1_SIZE (16 << 10) /*16 K reserved for BL1*/
38#define BL2_SIZE (512UL << 10UL) /* 512 KB */
39
40#define BL1_OFFSET (RES_BLOCK_SIZE + SEC_FW_SIZE)
41#define BL2_OFFSET (BL1_OFFSET + BL1_SIZE)
42
43/* U-Boot copy size from boot Media to DRAM.*/
44#define BL2_START_OFFSET (BL2_OFFSET/512)
45#define BL2_SIZE_BLOC_COUNT (BL2_SIZE/512)
46
47#define EXYNOS_COPY_SPI_FNPTR_ADDR 0x02020058
48#define SPI_FLASH_UBOOT_POS (SEC_FW_SIZE + BL1_SIZE)
49#elif defined(CONFIG_ARCH_EXYNOS4)
50#define COPY_BL2_SIZE 0x80000
51#define BL2_START_OFFSET ((CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE)/512)
52#define BL2_SIZE_BLOC_COUNT (COPY_BL2_SIZE/512)
53#endif
54
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053055DECLARE_GLOBAL_DATA_PTR;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053056
Amarc748be02013-04-27 11:42:59 +053057/* Index into irom ptr table */
58enum index {
59 MMC_INDEX,
60 EMMC44_INDEX,
61 EMMC44_END_INDEX,
62 SPI_INDEX,
63 USB_INDEX,
64};
65
66/* IROM Function Pointers Table */
67u32 irom_ptr_table[] = {
68 [MMC_INDEX] = 0x02020030, /* iROM Function Pointer-SDMMC boot */
69 [EMMC44_INDEX] = 0x02020044, /* iROM Function Pointer-EMMC4.4 boot*/
70 [EMMC44_END_INDEX] = 0x02020048,/* iROM Function Pointer
71 -EMMC4.4 end boot operation */
72 [SPI_INDEX] = 0x02020058, /* iROM Function Pointer-SPI boot */
73 [USB_INDEX] = 0x02020070, /* iROM Function Pointer-USB boot*/
74 };
75
Amarc748be02013-04-27 11:42:59 +053076void *get_irom_func(int index)
77{
78 return (void *)*(u32 *)irom_ptr_table[index];
79}
Vivek Gautam70656c72013-01-28 00:39:59 +000080
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053081#ifdef CONFIG_USB_BOOTING
Vivek Gautam70656c72013-01-28 00:39:59 +000082/*
83 * Set/clear program flow prediction and return the previous state.
84 */
85static int config_branch_prediction(int set_cr_z)
86{
87 unsigned int cr;
88
89 /* System Control Register: 11th bit Z Branch prediction enable */
90 cr = get_cr();
91 set_cr(set_cr_z ? cr | CR_Z : cr & ~CR_Z);
92
93 return cr & CR_Z;
94}
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053095#endif
Rajeshwari Shinde7a533772012-11-02 01:15:38 +000096
Minkyu Kangd8fa31a2013-12-06 19:04:03 +090097#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +053098static void spi_rx_tx(struct exynos_spi *regs, int todo,
99 void *dinp, void const *doutp, int i)
100{
101 uint *rxp = (uint *)(dinp + (i * (32 * 1024)));
102 int rx_lvl, tx_lvl;
103 uint out_bytes, in_bytes;
104
105 out_bytes = todo;
106 in_bytes = todo;
107 setbits_le32(&regs->ch_cfg, SPI_CH_RST);
108 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
109 writel(((todo * 8) / 32) | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
110
111 while (in_bytes) {
112 uint32_t spi_sts;
113 int temp;
114
115 spi_sts = readl(&regs->spi_sts);
116 rx_lvl = ((spi_sts >> 15) & 0x7f);
117 tx_lvl = ((spi_sts >> 6) & 0x7f);
118 while (tx_lvl < 32 && out_bytes) {
119 temp = 0xffffffff;
120 writel(temp, &regs->tx_data);
121 out_bytes -= 4;
122 tx_lvl += 4;
123 }
124 while (rx_lvl >= 4 && in_bytes) {
125 temp = readl(&regs->rx_data);
126 if (rxp)
127 *rxp++ = temp;
128 in_bytes -= 4;
129 rx_lvl -= 4;
130 }
131 }
132}
133
134/*
135 * Copy uboot from spi flash to RAM
136 *
137 * @parma uboot_size size of u-boot to copy
138 * @param uboot_addr address in u-boot to copy
139 */
140static void exynos_spi_copy(unsigned int uboot_size, unsigned int uboot_addr)
141{
142 int upto, todo;
143 int i, timeout = 100;
Patrick Delaunayac31d0d2019-02-27 15:20:34 +0100144 struct exynos_spi *regs = (struct exynos_spi *)CONFIG_SYS_SPI_BASE;
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530145
146 set_spi_clk(PERIPH_ID_SPI1, 50000000); /* set spi clock to 50Mhz */
147 /* set the spi1 GPIO */
148 exynos_pinmux_config(PERIPH_ID_SPI1, PINMUX_FLAG_NONE);
149
150 /* set pktcnt and enable it */
151 writel(4 | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
152 /* set FB_CLK_SEL */
153 writel(SPI_FB_DELAY_180, &regs->fb_clk);
154 /* set CH_WIDTH and BUS_WIDTH as word */
155 setbits_le32(&regs->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
156 SPI_MODE_BUS_WIDTH_WORD);
157 clrbits_le32(&regs->ch_cfg, SPI_CH_CPOL_L); /* CPOL: active high */
158
159 /* clear rx and tx channel if set priveously */
160 clrbits_le32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON);
161
162 setbits_le32(&regs->swap_cfg, SPI_RX_SWAP_EN |
163 SPI_RX_BYTE_SWAP |
164 SPI_RX_HWORD_SWAP);
165
166 /* do a soft reset */
167 setbits_le32(&regs->ch_cfg, SPI_CH_RST);
168 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
169
170 /* now set rx and tx channel ON */
171 setbits_le32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON | SPI_CH_HS_EN);
172 clrbits_le32(&regs->cs_reg, SPI_SLAVE_SIG_INACT); /* CS low */
173
174 /* Send read instruction (0x3h) followed by a 24 bit addr */
175 writel((SF_READ_DATA_CMD << 24) | SPI_FLASH_UBOOT_POS, &regs->tx_data);
176
177 /* waiting for TX done */
178 while (!(readl(&regs->spi_sts) & SPI_ST_TX_DONE)) {
179 if (!timeout) {
180 debug("SPI TIMEOUT\n");
181 break;
182 }
183 timeout--;
184 }
185
186 for (upto = 0, i = 0; upto < uboot_size; upto += todo, i++) {
Masahiro Yamadab4141192014-11-07 03:03:31 +0900187 todo = min(uboot_size - upto, (unsigned int)(1 << 15));
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530188 spi_rx_tx(regs, todo, (void *)(uboot_addr),
189 (void *)(SPI_FLASH_UBOOT_POS), i);
190 }
191
192 setbits_le32(&regs->cs_reg, SPI_SLAVE_SIG_INACT);/* make the CS high */
193
194 /*
195 * Let put controller mode to BYTE as
196 * SPI driver does not support WORD mode yet
197 */
198 clrbits_le32(&regs->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
199 SPI_MODE_BUS_WIDTH_WORD);
200 writel(0, &regs->swap_cfg);
201
202 /*
203 * Flush spi tx, rx fifos and reset the SPI controller
204 * and clear rx/tx channel
205 */
206 clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
207 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
208 clrbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
209}
Minkyu Kangd8fa31a2013-12-06 19:04:03 +0900210#endif
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530211
Chander Kashyap81e35202012-02-05 23:01:48 +0000212/*
Bin Menga1875592016-02-05 19:30:11 -0800213* Copy U-Boot from mmc to RAM:
Chander Kashyap81e35202012-02-05 23:01:48 +0000214* COPY_BL2_FNPTR_ADDR: Address in iRAM, which Contains
215* Pointer to API (Data transfer from mmc to ram)
216*/
217void copy_uboot_to_ram(void)
218{
Przemyslaw Marczak4fb4d552014-09-01 13:50:44 +0200219 unsigned int bootmode = BOOT_MODE_OM;
Amarc748be02013-04-27 11:42:59 +0530220
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530221 u32 (*copy_bl2)(u32 offset, u32 nblock, u32 dst) = NULL;
222 u32 offset = 0, size = 0;
Minkyu Kangd8fa31a2013-12-06 19:04:03 +0900223#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530224 struct spl_machine_param *param = spl_get_machine_params();
Minkyu Kangd8fa31a2013-12-06 19:04:03 +0900225#endif
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530226#ifdef CONFIG_SUPPORT_EMMC_BOOT
Amarc748be02013-04-27 11:42:59 +0530227 u32 (*copy_bl2_from_emmc)(u32 nblock, u32 dst);
228 void (*end_bootop_from_emmc)(void);
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530229#endif
230#ifdef CONFIG_USB_BOOTING
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530231 int is_cr_z_set;
232 unsigned int sec_boot_check;
Chander Kashyap81e35202012-02-05 23:01:48 +0000233
Vadim Bendebury4f298622014-11-13 22:38:21 +0530234 /*
235 * Note that older hardware (before Exynos5800) does not expect any
236 * arguments, but it does not hurt to pass them, so a common function
237 * prototype is used.
238 */
239 u32 (*usb_copy)(u32 num_of_block, u32 *dst);
240
Vivek Gautam70656c72013-01-28 00:39:59 +0000241 /* Read iRAM location to check for secondary USB boot mode */
242 sec_boot_check = readl(EXYNOS_IRAM_SECONDARY_BASE);
243 if (sec_boot_check == EXYNOS_USB_SECONDARY_BOOT)
244 bootmode = BOOT_MODE_USB;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530245#endif
Vivek Gautam70656c72013-01-28 00:39:59 +0000246
247 if (bootmode == BOOT_MODE_OM)
Przemyslaw Marczak4fb4d552014-09-01 13:50:44 +0200248 bootmode = get_boot_mode();
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000249
250 switch (bootmode) {
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530251#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000252 case BOOT_MODE_SERIAL:
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530253 /* Customised function to copy u-boot from SF */
254 exynos_spi_copy(param->uboot_size, CONFIG_SYS_TEXT_BASE);
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000255 break;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530256#endif
Przemyslaw Marczak4fb4d552014-09-01 13:50:44 +0200257 case BOOT_MODE_SD:
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530258 offset = BL2_START_OFFSET;
259 size = BL2_SIZE_BLOC_COUNT;
Amarc748be02013-04-27 11:42:59 +0530260 copy_bl2 = get_irom_func(MMC_INDEX);
Amarc748be02013-04-27 11:42:59 +0530261 break;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530262#ifdef CONFIG_SUPPORT_EMMC_BOOT
Amarc748be02013-04-27 11:42:59 +0530263 case BOOT_MODE_EMMC:
264 /* Set the FSYS1 clock divisor value for EMMC boot */
265 emmc_boot_clk_div_set();
266
267 copy_bl2_from_emmc = get_irom_func(EMMC44_INDEX);
268 end_bootop_from_emmc = get_irom_func(EMMC44_END_INDEX);
269
270 copy_bl2_from_emmc(BL2_SIZE_BLOC_COUNT, CONFIG_SYS_TEXT_BASE);
271 end_bootop_from_emmc();
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000272 break;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530273#endif
274#ifdef CONFIG_USB_BOOTING
Vivek Gautam70656c72013-01-28 00:39:59 +0000275 case BOOT_MODE_USB:
276 /*
277 * iROM needs program flow prediction to be disabled
278 * before copy from USB device to RAM
279 */
280 is_cr_z_set = config_branch_prediction(0);
Amarc748be02013-04-27 11:42:59 +0530281 usb_copy = get_irom_func(USB_INDEX);
Vadim Bendebury4f298622014-11-13 22:38:21 +0530282 usb_copy(0, (u32 *)CONFIG_SYS_TEXT_BASE);
Vivek Gautam70656c72013-01-28 00:39:59 +0000283 config_branch_prediction(is_cr_z_set);
284 break;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530285#endif
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000286 default:
287 break;
288 }
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530289
290 if (copy_bl2)
291 copy_bl2(offset, size, CONFIG_SYS_TEXT_BASE);
292}
293
294void memzero(void *s, size_t n)
295{
296 char *ptr = s;
297 size_t i;
298
299 for (i = 0; i < n; i++)
300 *ptr++ = '\0';
301}
302
303/**
304 * Set up the U-Boot global_data pointer
305 *
306 * This sets the address of the global data, and sets up basic values.
307 *
308 * @param gdp Value to give to gd
309 */
310static void setup_global_data(gd_t *gdp)
311{
Marek BehĂșn86c5e212021-05-20 13:24:10 +0200312 set_gd(gdp);
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530313 memzero((void *)gd, sizeof(gd_t));
314 gd->flags |= GD_FLG_RELOC;
315 gd->baudrate = CONFIG_BAUDRATE;
316 gd->have_console = 1;
Chander Kashyap81e35202012-02-05 23:01:48 +0000317}
318
319void board_init_f(unsigned long bootflag)
320{
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530321 __aligned(8) gd_t local_gd;
Chander Kashyap81e35202012-02-05 23:01:48 +0000322 __attribute__((noreturn)) void (*uboot)(void);
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530323
324 setup_global_data(&local_gd);
325
326 if (do_lowlevel_init())
327 power_exit_wakeup();
328
Chander Kashyap81e35202012-02-05 23:01:48 +0000329 copy_uboot_to_ram();
330
331 /* Jump to U-Boot image */
332 uboot = (void *)CONFIG_SYS_TEXT_BASE;
333 (*uboot)();
334 /* Never returns Here */
335}
336
337/* Place Holders */
338void board_init_r(gd_t *id, ulong dest_addr)
339{
340 /* Function attribute is no-return */
341 /* This Function never executes */
342 while (1)
343 ;
344}