blob: 9c7ca49519b94f8e8745bc9614906dfa8079af17 [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 <common.h>
7#include <config.h>
Simon Glass97589732020-05-10 11:40:02 -06008#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.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
Rajeshwari Shindebed24422013-07-04 12:29:17 +053024DECLARE_GLOBAL_DATA_PTR;
Rajeshwari Shindebed24422013-07-04 12:29:17 +053025
Amare33add82013-04-27 11:42:59 +053026/* Index into irom ptr table */
27enum index {
28 MMC_INDEX,
29 EMMC44_INDEX,
30 EMMC44_END_INDEX,
31 SPI_INDEX,
32 USB_INDEX,
33};
34
35/* IROM Function Pointers Table */
36u32 irom_ptr_table[] = {
37 [MMC_INDEX] = 0x02020030, /* iROM Function Pointer-SDMMC boot */
38 [EMMC44_INDEX] = 0x02020044, /* iROM Function Pointer-EMMC4.4 boot*/
39 [EMMC44_END_INDEX] = 0x02020048,/* iROM Function Pointer
40 -EMMC4.4 end boot operation */
41 [SPI_INDEX] = 0x02020058, /* iROM Function Pointer-SPI boot */
42 [USB_INDEX] = 0x02020070, /* iROM Function Pointer-USB boot*/
43 };
44
Amare33add82013-04-27 11:42:59 +053045void *get_irom_func(int index)
46{
47 return (void *)*(u32 *)irom_ptr_table[index];
48}
Rajeshwari Shinde9cb48e82012-11-02 01:15:38 +000049
Rajeshwari Shindebed24422013-07-04 12:29:17 +053050#ifdef CONFIG_USB_BOOTING
Chander Kashyap1633dd12012-02-05 23:01:48 +000051/*
Vivek Gautam681dd832013-01-28 00:39:59 +000052 * Set/clear program flow prediction and return the previous state.
53 */
54static int config_branch_prediction(int set_cr_z)
55{
56 unsigned int cr;
57
58 /* System Control Register: 11th bit Z Branch prediction enable */
59 cr = get_cr();
60 set_cr(set_cr_z ? cr | CR_Z : cr & ~CR_Z);
61
62 return cr & CR_Z;
63}
Rajeshwari Shindebed24422013-07-04 12:29:17 +053064#endif
Vivek Gautam681dd832013-01-28 00:39:59 +000065
Minkyu Kang1609f512013-12-06 19:04:03 +090066#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde507f8922013-10-08 18:42:22 +053067static void spi_rx_tx(struct exynos_spi *regs, int todo,
68 void *dinp, void const *doutp, int i)
69{
70 uint *rxp = (uint *)(dinp + (i * (32 * 1024)));
71 int rx_lvl, tx_lvl;
72 uint out_bytes, in_bytes;
73
74 out_bytes = todo;
75 in_bytes = todo;
76 setbits_le32(&regs->ch_cfg, SPI_CH_RST);
77 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
78 writel(((todo * 8) / 32) | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
79
80 while (in_bytes) {
81 uint32_t spi_sts;
82 int temp;
83
84 spi_sts = readl(&regs->spi_sts);
85 rx_lvl = ((spi_sts >> 15) & 0x7f);
86 tx_lvl = ((spi_sts >> 6) & 0x7f);
87 while (tx_lvl < 32 && out_bytes) {
88 temp = 0xffffffff;
89 writel(temp, &regs->tx_data);
90 out_bytes -= 4;
91 tx_lvl += 4;
92 }
93 while (rx_lvl >= 4 && in_bytes) {
94 temp = readl(&regs->rx_data);
95 if (rxp)
96 *rxp++ = temp;
97 in_bytes -= 4;
98 rx_lvl -= 4;
99 }
100 }
101}
102
103/*
104 * Copy uboot from spi flash to RAM
105 *
106 * @parma uboot_size size of u-boot to copy
107 * @param uboot_addr address in u-boot to copy
108 */
109static void exynos_spi_copy(unsigned int uboot_size, unsigned int uboot_addr)
110{
111 int upto, todo;
112 int i, timeout = 100;
Patrick Delaunay5c95efb2019-02-27 15:20:34 +0100113 struct exynos_spi *regs = (struct exynos_spi *)CONFIG_SYS_SPI_BASE;
Rajeshwari Shinde507f8922013-10-08 18:42:22 +0530114
115 set_spi_clk(PERIPH_ID_SPI1, 50000000); /* set spi clock to 50Mhz */
116 /* set the spi1 GPIO */
117 exynos_pinmux_config(PERIPH_ID_SPI1, PINMUX_FLAG_NONE);
118
119 /* set pktcnt and enable it */
120 writel(4 | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
121 /* set FB_CLK_SEL */
122 writel(SPI_FB_DELAY_180, &regs->fb_clk);
123 /* set CH_WIDTH and BUS_WIDTH as word */
124 setbits_le32(&regs->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
125 SPI_MODE_BUS_WIDTH_WORD);
126 clrbits_le32(&regs->ch_cfg, SPI_CH_CPOL_L); /* CPOL: active high */
127
128 /* clear rx and tx channel if set priveously */
129 clrbits_le32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON);
130
131 setbits_le32(&regs->swap_cfg, SPI_RX_SWAP_EN |
132 SPI_RX_BYTE_SWAP |
133 SPI_RX_HWORD_SWAP);
134
135 /* do a soft reset */
136 setbits_le32(&regs->ch_cfg, SPI_CH_RST);
137 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
138
139 /* now set rx and tx channel ON */
140 setbits_le32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON | SPI_CH_HS_EN);
141 clrbits_le32(&regs->cs_reg, SPI_SLAVE_SIG_INACT); /* CS low */
142
143 /* Send read instruction (0x3h) followed by a 24 bit addr */
144 writel((SF_READ_DATA_CMD << 24) | SPI_FLASH_UBOOT_POS, &regs->tx_data);
145
146 /* waiting for TX done */
147 while (!(readl(&regs->spi_sts) & SPI_ST_TX_DONE)) {
148 if (!timeout) {
149 debug("SPI TIMEOUT\n");
150 break;
151 }
152 timeout--;
153 }
154
155 for (upto = 0, i = 0; upto < uboot_size; upto += todo, i++) {
Masahiro Yamadadb204642014-11-07 03:03:31 +0900156 todo = min(uboot_size - upto, (unsigned int)(1 << 15));
Rajeshwari Shinde507f8922013-10-08 18:42:22 +0530157 spi_rx_tx(regs, todo, (void *)(uboot_addr),
158 (void *)(SPI_FLASH_UBOOT_POS), i);
159 }
160
161 setbits_le32(&regs->cs_reg, SPI_SLAVE_SIG_INACT);/* make the CS high */
162
163 /*
164 * Let put controller mode to BYTE as
165 * SPI driver does not support WORD mode yet
166 */
167 clrbits_le32(&regs->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
168 SPI_MODE_BUS_WIDTH_WORD);
169 writel(0, &regs->swap_cfg);
170
171 /*
172 * Flush spi tx, rx fifos and reset the SPI controller
173 * and clear rx/tx channel
174 */
175 clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
176 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
177 clrbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
178}
Minkyu Kang1609f512013-12-06 19:04:03 +0900179#endif
Rajeshwari Shinde507f8922013-10-08 18:42:22 +0530180
Vivek Gautam681dd832013-01-28 00:39:59 +0000181/*
Bin Meng75574052016-02-05 19:30:11 -0800182* Copy U-Boot from mmc to RAM:
Chander Kashyap1633dd12012-02-05 23:01:48 +0000183* COPY_BL2_FNPTR_ADDR: Address in iRAM, which Contains
184* Pointer to API (Data transfer from mmc to ram)
185*/
186void copy_uboot_to_ram(void)
187{
Przemyslaw Marczak93976f72014-09-01 13:50:44 +0200188 unsigned int bootmode = BOOT_MODE_OM;
Amare33add82013-04-27 11:42:59 +0530189
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530190 u32 (*copy_bl2)(u32 offset, u32 nblock, u32 dst) = NULL;
191 u32 offset = 0, size = 0;
Minkyu Kang1609f512013-12-06 19:04:03 +0900192#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde507f8922013-10-08 18:42:22 +0530193 struct spl_machine_param *param = spl_get_machine_params();
Minkyu Kang1609f512013-12-06 19:04:03 +0900194#endif
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530195#ifdef CONFIG_SUPPORT_EMMC_BOOT
Amare33add82013-04-27 11:42:59 +0530196 u32 (*copy_bl2_from_emmc)(u32 nblock, u32 dst);
197 void (*end_bootop_from_emmc)(void);
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530198#endif
199#ifdef CONFIG_USB_BOOTING
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530200 int is_cr_z_set;
201 unsigned int sec_boot_check;
Rajeshwari Shinde9cb48e82012-11-02 01:15:38 +0000202
Vadim Bendeburyc12efec2014-11-13 22:38:21 +0530203 /*
204 * Note that older hardware (before Exynos5800) does not expect any
205 * arguments, but it does not hurt to pass them, so a common function
206 * prototype is used.
207 */
208 u32 (*usb_copy)(u32 num_of_block, u32 *dst);
209
Vivek Gautam681dd832013-01-28 00:39:59 +0000210 /* Read iRAM location to check for secondary USB boot mode */
211 sec_boot_check = readl(EXYNOS_IRAM_SECONDARY_BASE);
212 if (sec_boot_check == EXYNOS_USB_SECONDARY_BOOT)
213 bootmode = BOOT_MODE_USB;
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530214#endif
Vivek Gautam681dd832013-01-28 00:39:59 +0000215
216 if (bootmode == BOOT_MODE_OM)
Przemyslaw Marczak93976f72014-09-01 13:50:44 +0200217 bootmode = get_boot_mode();
Chander Kashyap1633dd12012-02-05 23:01:48 +0000218
Rajeshwari Shinde9cb48e82012-11-02 01:15:38 +0000219 switch (bootmode) {
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530220#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde9cb48e82012-11-02 01:15:38 +0000221 case BOOT_MODE_SERIAL:
Rajeshwari Shinde507f8922013-10-08 18:42:22 +0530222 /* Customised function to copy u-boot from SF */
223 exynos_spi_copy(param->uboot_size, CONFIG_SYS_TEXT_BASE);
Rajeshwari Shinde9cb48e82012-11-02 01:15:38 +0000224 break;
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530225#endif
Przemyslaw Marczak93976f72014-09-01 13:50:44 +0200226 case BOOT_MODE_SD:
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530227 offset = BL2_START_OFFSET;
228 size = BL2_SIZE_BLOC_COUNT;
Amare33add82013-04-27 11:42:59 +0530229 copy_bl2 = get_irom_func(MMC_INDEX);
Amare33add82013-04-27 11:42:59 +0530230 break;
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530231#ifdef CONFIG_SUPPORT_EMMC_BOOT
Amare33add82013-04-27 11:42:59 +0530232 case BOOT_MODE_EMMC:
233 /* Set the FSYS1 clock divisor value for EMMC boot */
234 emmc_boot_clk_div_set();
235
236 copy_bl2_from_emmc = get_irom_func(EMMC44_INDEX);
237 end_bootop_from_emmc = get_irom_func(EMMC44_END_INDEX);
238
239 copy_bl2_from_emmc(BL2_SIZE_BLOC_COUNT, CONFIG_SYS_TEXT_BASE);
240 end_bootop_from_emmc();
Rajeshwari Shinde9cb48e82012-11-02 01:15:38 +0000241 break;
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530242#endif
243#ifdef CONFIG_USB_BOOTING
Vivek Gautam681dd832013-01-28 00:39:59 +0000244 case BOOT_MODE_USB:
245 /*
246 * iROM needs program flow prediction to be disabled
247 * before copy from USB device to RAM
248 */
249 is_cr_z_set = config_branch_prediction(0);
Amare33add82013-04-27 11:42:59 +0530250 usb_copy = get_irom_func(USB_INDEX);
Vadim Bendeburyc12efec2014-11-13 22:38:21 +0530251 usb_copy(0, (u32 *)CONFIG_SYS_TEXT_BASE);
Vivek Gautam681dd832013-01-28 00:39:59 +0000252 config_branch_prediction(is_cr_z_set);
253 break;
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530254#endif
Rajeshwari Shinde9cb48e82012-11-02 01:15:38 +0000255 default:
256 break;
257 }
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530258
259 if (copy_bl2)
260 copy_bl2(offset, size, CONFIG_SYS_TEXT_BASE);
Chander Kashyap1633dd12012-02-05 23:01:48 +0000261}
262
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530263void memzero(void *s, size_t n)
264{
265 char *ptr = s;
266 size_t i;
267
268 for (i = 0; i < n; i++)
269 *ptr++ = '\0';
270}
271
272/**
273 * Set up the U-Boot global_data pointer
274 *
275 * This sets the address of the global data, and sets up basic values.
276 *
277 * @param gdp Value to give to gd
278 */
279static void setup_global_data(gd_t *gdp)
280{
281 gd = gdp;
282 memzero((void *)gd, sizeof(gd_t));
283 gd->flags |= GD_FLG_RELOC;
284 gd->baudrate = CONFIG_BAUDRATE;
285 gd->have_console = 1;
286}
287
Chander Kashyap1633dd12012-02-05 23:01:48 +0000288void board_init_f(unsigned long bootflag)
289{
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530290 __aligned(8) gd_t local_gd;
Chander Kashyap1633dd12012-02-05 23:01:48 +0000291 __attribute__((noreturn)) void (*uboot)(void);
Rajeshwari Shindebed24422013-07-04 12:29:17 +0530292
293 setup_global_data(&local_gd);
294
295 if (do_lowlevel_init())
296 power_exit_wakeup();
297
Chander Kashyap1633dd12012-02-05 23:01:48 +0000298 copy_uboot_to_ram();
299
300 /* Jump to U-Boot image */
301 uboot = (void *)CONFIG_SYS_TEXT_BASE;
302 (*uboot)();
303 /* Never returns Here */
304}
305
306/* Place Holders */
307void board_init_r(gd_t *id, ulong dest_addr)
308{
309 /* Function attribute is no-return */
310 /* This Function never executes */
311 while (1)
312 ;
313}