1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2012 Samsung Electronics
11 #include <asm/cache.h>
12 #include <asm/arch/clock.h>
13 #include <asm/arch/clk.h>
14 #include <asm/arch/dmc.h>
15 #include <asm/arch/periph.h>
16 #include <asm/arch/pinmux.h>
17 #include <asm/arch/power.h>
18 #include <asm/arch/spl.h>
19 #include <asm/arch/spi.h>
21 #include "common_setup.h"
22 #include "clock_init.h"
24 DECLARE_GLOBAL_DATA_PTR;
26 /* Index into irom ptr table */
35 /* IROM Function Pointers Table */
36 u32 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*/
45 void *get_irom_func(int index)
47 return (void *)*(u32 *)irom_ptr_table[index];
50 #ifdef CONFIG_USB_BOOTING
52 * Set/clear program flow prediction and return the previous state.
54 static int config_branch_prediction(int set_cr_z)
58 /* System Control Register: 11th bit Z Branch prediction enable */
60 set_cr(set_cr_z ? cr | CR_Z : cr & ~CR_Z);
66 #ifdef CONFIG_SPI_BOOTING
67 static void spi_rx_tx(struct exynos_spi *regs, int todo,
68 void *dinp, void const *doutp, int i)
70 uint *rxp = (uint *)(dinp + (i * (32 * 1024)));
72 uint out_bytes, in_bytes;
76 setbits_le32(®s->ch_cfg, SPI_CH_RST);
77 clrbits_le32(®s->ch_cfg, SPI_CH_RST);
78 writel(((todo * 8) / 32) | SPI_PACKET_CNT_EN, ®s->pkt_cnt);
84 spi_sts = readl(®s->spi_sts);
85 rx_lvl = ((spi_sts >> 15) & 0x7f);
86 tx_lvl = ((spi_sts >> 6) & 0x7f);
87 while (tx_lvl < 32 && out_bytes) {
89 writel(temp, ®s->tx_data);
93 while (rx_lvl >= 4 && in_bytes) {
94 temp = readl(®s->rx_data);
104 * Copy uboot from spi flash to RAM
106 * @parma uboot_size size of u-boot to copy
107 * @param uboot_addr address in u-boot to copy
109 static void exynos_spi_copy(unsigned int uboot_size, unsigned int uboot_addr)
112 int i, timeout = 100;
113 struct exynos_spi *regs = (struct exynos_spi *)CONFIG_SYS_SPI_BASE;
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);
119 /* set pktcnt and enable it */
120 writel(4 | SPI_PACKET_CNT_EN, ®s->pkt_cnt);
122 writel(SPI_FB_DELAY_180, ®s->fb_clk);
123 /* set CH_WIDTH and BUS_WIDTH as word */
124 setbits_le32(®s->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
125 SPI_MODE_BUS_WIDTH_WORD);
126 clrbits_le32(®s->ch_cfg, SPI_CH_CPOL_L); /* CPOL: active high */
128 /* clear rx and tx channel if set priveously */
129 clrbits_le32(®s->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON);
131 setbits_le32(®s->swap_cfg, SPI_RX_SWAP_EN |
135 /* do a soft reset */
136 setbits_le32(®s->ch_cfg, SPI_CH_RST);
137 clrbits_le32(®s->ch_cfg, SPI_CH_RST);
139 /* now set rx and tx channel ON */
140 setbits_le32(®s->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON | SPI_CH_HS_EN);
141 clrbits_le32(®s->cs_reg, SPI_SLAVE_SIG_INACT); /* CS low */
143 /* Send read instruction (0x3h) followed by a 24 bit addr */
144 writel((SF_READ_DATA_CMD << 24) | SPI_FLASH_UBOOT_POS, ®s->tx_data);
146 /* waiting for TX done */
147 while (!(readl(®s->spi_sts) & SPI_ST_TX_DONE)) {
149 debug("SPI TIMEOUT\n");
155 for (upto = 0, i = 0; upto < uboot_size; upto += todo, i++) {
156 todo = min(uboot_size - upto, (unsigned int)(1 << 15));
157 spi_rx_tx(regs, todo, (void *)(uboot_addr),
158 (void *)(SPI_FLASH_UBOOT_POS), i);
161 setbits_le32(®s->cs_reg, SPI_SLAVE_SIG_INACT);/* make the CS high */
164 * Let put controller mode to BYTE as
165 * SPI driver does not support WORD mode yet
167 clrbits_le32(®s->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
168 SPI_MODE_BUS_WIDTH_WORD);
169 writel(0, ®s->swap_cfg);
172 * Flush spi tx, rx fifos and reset the SPI controller
173 * and clear rx/tx channel
175 clrsetbits_le32(®s->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
176 clrbits_le32(®s->ch_cfg, SPI_CH_RST);
177 clrbits_le32(®s->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
182 * Copy U-Boot from mmc to RAM:
183 * COPY_BL2_FNPTR_ADDR: Address in iRAM, which Contains
184 * Pointer to API (Data transfer from mmc to ram)
186 void copy_uboot_to_ram(void)
188 unsigned int bootmode = BOOT_MODE_OM;
190 u32 (*copy_bl2)(u32 offset, u32 nblock, u32 dst) = NULL;
191 u32 offset = 0, size = 0;
192 #ifdef CONFIG_SPI_BOOTING
193 struct spl_machine_param *param = spl_get_machine_params();
195 #ifdef CONFIG_SUPPORT_EMMC_BOOT
196 u32 (*copy_bl2_from_emmc)(u32 nblock, u32 dst);
197 void (*end_bootop_from_emmc)(void);
199 #ifdef CONFIG_USB_BOOTING
201 unsigned int sec_boot_check;
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
208 u32 (*usb_copy)(u32 num_of_block, u32 *dst);
210 /* 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;
216 if (bootmode == BOOT_MODE_OM)
217 bootmode = get_boot_mode();
220 #ifdef CONFIG_SPI_BOOTING
221 case BOOT_MODE_SERIAL:
222 /* Customised function to copy u-boot from SF */
223 exynos_spi_copy(param->uboot_size, CONFIG_SYS_TEXT_BASE);
227 offset = BL2_START_OFFSET;
228 size = BL2_SIZE_BLOC_COUNT;
229 copy_bl2 = get_irom_func(MMC_INDEX);
231 #ifdef CONFIG_SUPPORT_EMMC_BOOT
233 /* Set the FSYS1 clock divisor value for EMMC boot */
234 emmc_boot_clk_div_set();
236 copy_bl2_from_emmc = get_irom_func(EMMC44_INDEX);
237 end_bootop_from_emmc = get_irom_func(EMMC44_END_INDEX);
239 copy_bl2_from_emmc(BL2_SIZE_BLOC_COUNT, CONFIG_SYS_TEXT_BASE);
240 end_bootop_from_emmc();
243 #ifdef CONFIG_USB_BOOTING
246 * iROM needs program flow prediction to be disabled
247 * before copy from USB device to RAM
249 is_cr_z_set = config_branch_prediction(0);
250 usb_copy = get_irom_func(USB_INDEX);
251 usb_copy(0, (u32 *)CONFIG_SYS_TEXT_BASE);
252 config_branch_prediction(is_cr_z_set);
260 copy_bl2(offset, size, CONFIG_SYS_TEXT_BASE);
263 void memzero(void *s, size_t n)
268 for (i = 0; i < n; i++)
273 * Set up the U-Boot global_data pointer
275 * This sets the address of the global data, and sets up basic values.
277 * @param gdp Value to give to gd
279 static void setup_global_data(gd_t *gdp)
282 memzero((void *)gd, sizeof(gd_t));
283 gd->flags |= GD_FLG_RELOC;
284 gd->baudrate = CONFIG_BAUDRATE;
285 gd->have_console = 1;
288 void board_init_f(unsigned long bootflag)
290 __aligned(8) gd_t local_gd;
291 __attribute__((noreturn)) void (*uboot)(void);
293 setup_global_data(&local_gd);
295 if (do_lowlevel_init())
300 /* Jump to U-Boot image */
301 uboot = (void *)CONFIG_SYS_TEXT_BASE;
303 /* Never returns Here */
307 void board_init_r(gd_t *id, ulong dest_addr)
309 /* Function attribute is no-return */
310 /* This Function never executes */