blob: 722449881af27ab3803cbe655aa59c3a70f865cc [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
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053025DECLARE_GLOBAL_DATA_PTR;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053026
Amarc748be02013-04-27 11:42:59 +053027/* Index into irom ptr table */
28enum index {
29 MMC_INDEX,
30 EMMC44_INDEX,
31 EMMC44_END_INDEX,
32 SPI_INDEX,
33 USB_INDEX,
34};
35
36/* IROM Function Pointers Table */
37u32 irom_ptr_table[] = {
38 [MMC_INDEX] = 0x02020030, /* iROM Function Pointer-SDMMC boot */
39 [EMMC44_INDEX] = 0x02020044, /* iROM Function Pointer-EMMC4.4 boot*/
40 [EMMC44_END_INDEX] = 0x02020048,/* iROM Function Pointer
41 -EMMC4.4 end boot operation */
42 [SPI_INDEX] = 0x02020058, /* iROM Function Pointer-SPI boot */
43 [USB_INDEX] = 0x02020070, /* iROM Function Pointer-USB boot*/
44 };
45
Amarc748be02013-04-27 11:42:59 +053046void *get_irom_func(int index)
47{
48 return (void *)*(u32 *)irom_ptr_table[index];
49}
Vivek Gautam70656c72013-01-28 00:39:59 +000050
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053051#ifdef CONFIG_USB_BOOTING
Vivek Gautam70656c72013-01-28 00:39:59 +000052/*
53 * Set/clear program flow prediction and return the previous state.
54 */
55static int config_branch_prediction(int set_cr_z)
56{
57 unsigned int cr;
58
59 /* System Control Register: 11th bit Z Branch prediction enable */
60 cr = get_cr();
61 set_cr(set_cr_z ? cr | CR_Z : cr & ~CR_Z);
62
63 return cr & CR_Z;
64}
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053065#endif
Rajeshwari Shinde7a533772012-11-02 01:15:38 +000066
Minkyu Kangd8fa31a2013-12-06 19:04:03 +090067#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +053068static void spi_rx_tx(struct exynos_spi *regs, int todo,
69 void *dinp, void const *doutp, int i)
70{
71 uint *rxp = (uint *)(dinp + (i * (32 * 1024)));
72 int rx_lvl, tx_lvl;
73 uint out_bytes, in_bytes;
74
75 out_bytes = todo;
76 in_bytes = todo;
77 setbits_le32(&regs->ch_cfg, SPI_CH_RST);
78 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
79 writel(((todo * 8) / 32) | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
80
81 while (in_bytes) {
82 uint32_t spi_sts;
83 int temp;
84
85 spi_sts = readl(&regs->spi_sts);
86 rx_lvl = ((spi_sts >> 15) & 0x7f);
87 tx_lvl = ((spi_sts >> 6) & 0x7f);
88 while (tx_lvl < 32 && out_bytes) {
89 temp = 0xffffffff;
90 writel(temp, &regs->tx_data);
91 out_bytes -= 4;
92 tx_lvl += 4;
93 }
94 while (rx_lvl >= 4 && in_bytes) {
95 temp = readl(&regs->rx_data);
96 if (rxp)
97 *rxp++ = temp;
98 in_bytes -= 4;
99 rx_lvl -= 4;
100 }
101 }
102}
103
104/*
105 * Copy uboot from spi flash to RAM
106 *
107 * @parma uboot_size size of u-boot to copy
108 * @param uboot_addr address in u-boot to copy
109 */
110static void exynos_spi_copy(unsigned int uboot_size, unsigned int uboot_addr)
111{
112 int upto, todo;
113 int i, timeout = 100;
Patrick Delaunayac31d0d2019-02-27 15:20:34 +0100114 struct exynos_spi *regs = (struct exynos_spi *)CONFIG_SYS_SPI_BASE;
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530115
116 set_spi_clk(PERIPH_ID_SPI1, 50000000); /* set spi clock to 50Mhz */
117 /* set the spi1 GPIO */
118 exynos_pinmux_config(PERIPH_ID_SPI1, PINMUX_FLAG_NONE);
119
120 /* set pktcnt and enable it */
121 writel(4 | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
122 /* set FB_CLK_SEL */
123 writel(SPI_FB_DELAY_180, &regs->fb_clk);
124 /* set CH_WIDTH and BUS_WIDTH as word */
125 setbits_le32(&regs->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
126 SPI_MODE_BUS_WIDTH_WORD);
127 clrbits_le32(&regs->ch_cfg, SPI_CH_CPOL_L); /* CPOL: active high */
128
129 /* clear rx and tx channel if set priveously */
130 clrbits_le32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON);
131
132 setbits_le32(&regs->swap_cfg, SPI_RX_SWAP_EN |
133 SPI_RX_BYTE_SWAP |
134 SPI_RX_HWORD_SWAP);
135
136 /* do a soft reset */
137 setbits_le32(&regs->ch_cfg, SPI_CH_RST);
138 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
139
140 /* now set rx and tx channel ON */
141 setbits_le32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON | SPI_CH_HS_EN);
142 clrbits_le32(&regs->cs_reg, SPI_SLAVE_SIG_INACT); /* CS low */
143
144 /* Send read instruction (0x3h) followed by a 24 bit addr */
145 writel((SF_READ_DATA_CMD << 24) | SPI_FLASH_UBOOT_POS, &regs->tx_data);
146
147 /* waiting for TX done */
148 while (!(readl(&regs->spi_sts) & SPI_ST_TX_DONE)) {
149 if (!timeout) {
150 debug("SPI TIMEOUT\n");
151 break;
152 }
153 timeout--;
154 }
155
156 for (upto = 0, i = 0; upto < uboot_size; upto += todo, i++) {
Masahiro Yamadab4141192014-11-07 03:03:31 +0900157 todo = min(uboot_size - upto, (unsigned int)(1 << 15));
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530158 spi_rx_tx(regs, todo, (void *)(uboot_addr),
159 (void *)(SPI_FLASH_UBOOT_POS), i);
160 }
161
162 setbits_le32(&regs->cs_reg, SPI_SLAVE_SIG_INACT);/* make the CS high */
163
164 /*
165 * Let put controller mode to BYTE as
166 * SPI driver does not support WORD mode yet
167 */
168 clrbits_le32(&regs->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
169 SPI_MODE_BUS_WIDTH_WORD);
170 writel(0, &regs->swap_cfg);
171
172 /*
173 * Flush spi tx, rx fifos and reset the SPI controller
174 * and clear rx/tx channel
175 */
176 clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
177 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
178 clrbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
179}
Minkyu Kangd8fa31a2013-12-06 19:04:03 +0900180#endif
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530181
Chander Kashyap81e35202012-02-05 23:01:48 +0000182/*
Bin Menga1875592016-02-05 19:30:11 -0800183* Copy U-Boot from mmc to RAM:
Chander Kashyap81e35202012-02-05 23:01:48 +0000184* COPY_BL2_FNPTR_ADDR: Address in iRAM, which Contains
185* Pointer to API (Data transfer from mmc to ram)
186*/
187void copy_uboot_to_ram(void)
188{
Przemyslaw Marczak4fb4d552014-09-01 13:50:44 +0200189 unsigned int bootmode = BOOT_MODE_OM;
Amarc748be02013-04-27 11:42:59 +0530190
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530191 u32 (*copy_bl2)(u32 offset, u32 nblock, u32 dst) = NULL;
192 u32 offset = 0, size = 0;
Minkyu Kangd8fa31a2013-12-06 19:04:03 +0900193#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530194 struct spl_machine_param *param = spl_get_machine_params();
Minkyu Kangd8fa31a2013-12-06 19:04:03 +0900195#endif
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530196#ifdef CONFIG_SUPPORT_EMMC_BOOT
Amarc748be02013-04-27 11:42:59 +0530197 u32 (*copy_bl2_from_emmc)(u32 nblock, u32 dst);
198 void (*end_bootop_from_emmc)(void);
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530199#endif
200#ifdef CONFIG_USB_BOOTING
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530201 int is_cr_z_set;
202 unsigned int sec_boot_check;
Chander Kashyap81e35202012-02-05 23:01:48 +0000203
Vadim Bendebury4f298622014-11-13 22:38:21 +0530204 /*
205 * Note that older hardware (before Exynos5800) does not expect any
206 * arguments, but it does not hurt to pass them, so a common function
207 * prototype is used.
208 */
209 u32 (*usb_copy)(u32 num_of_block, u32 *dst);
210
Vivek Gautam70656c72013-01-28 00:39:59 +0000211 /* Read iRAM location to check for secondary USB boot mode */
212 sec_boot_check = readl(EXYNOS_IRAM_SECONDARY_BASE);
213 if (sec_boot_check == EXYNOS_USB_SECONDARY_BOOT)
214 bootmode = BOOT_MODE_USB;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530215#endif
Vivek Gautam70656c72013-01-28 00:39:59 +0000216
217 if (bootmode == BOOT_MODE_OM)
Przemyslaw Marczak4fb4d552014-09-01 13:50:44 +0200218 bootmode = get_boot_mode();
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000219
220 switch (bootmode) {
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530221#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000222 case BOOT_MODE_SERIAL:
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530223 /* Customised function to copy u-boot from SF */
224 exynos_spi_copy(param->uboot_size, CONFIG_SYS_TEXT_BASE);
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000225 break;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530226#endif
Przemyslaw Marczak4fb4d552014-09-01 13:50:44 +0200227 case BOOT_MODE_SD:
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530228 offset = BL2_START_OFFSET;
229 size = BL2_SIZE_BLOC_COUNT;
Amarc748be02013-04-27 11:42:59 +0530230 copy_bl2 = get_irom_func(MMC_INDEX);
Amarc748be02013-04-27 11:42:59 +0530231 break;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530232#ifdef CONFIG_SUPPORT_EMMC_BOOT
Amarc748be02013-04-27 11:42:59 +0530233 case BOOT_MODE_EMMC:
234 /* Set the FSYS1 clock divisor value for EMMC boot */
235 emmc_boot_clk_div_set();
236
237 copy_bl2_from_emmc = get_irom_func(EMMC44_INDEX);
238 end_bootop_from_emmc = get_irom_func(EMMC44_END_INDEX);
239
240 copy_bl2_from_emmc(BL2_SIZE_BLOC_COUNT, CONFIG_SYS_TEXT_BASE);
241 end_bootop_from_emmc();
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000242 break;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530243#endif
244#ifdef CONFIG_USB_BOOTING
Vivek Gautam70656c72013-01-28 00:39:59 +0000245 case BOOT_MODE_USB:
246 /*
247 * iROM needs program flow prediction to be disabled
248 * before copy from USB device to RAM
249 */
250 is_cr_z_set = config_branch_prediction(0);
Amarc748be02013-04-27 11:42:59 +0530251 usb_copy = get_irom_func(USB_INDEX);
Vadim Bendebury4f298622014-11-13 22:38:21 +0530252 usb_copy(0, (u32 *)CONFIG_SYS_TEXT_BASE);
Vivek Gautam70656c72013-01-28 00:39:59 +0000253 config_branch_prediction(is_cr_z_set);
254 break;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530255#endif
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000256 default:
257 break;
258 }
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530259
260 if (copy_bl2)
261 copy_bl2(offset, size, CONFIG_SYS_TEXT_BASE);
262}
263
264void memzero(void *s, size_t n)
265{
266 char *ptr = s;
267 size_t i;
268
269 for (i = 0; i < n; i++)
270 *ptr++ = '\0';
271}
272
273/**
274 * Set up the U-Boot global_data pointer
275 *
276 * This sets the address of the global data, and sets up basic values.
277 *
278 * @param gdp Value to give to gd
279 */
280static void setup_global_data(gd_t *gdp)
281{
Marek BehĂșn86c5e212021-05-20 13:24:10 +0200282 set_gd(gdp);
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530283 memzero((void *)gd, sizeof(gd_t));
284 gd->flags |= GD_FLG_RELOC;
285 gd->baudrate = CONFIG_BAUDRATE;
286 gd->have_console = 1;
Chander Kashyap81e35202012-02-05 23:01:48 +0000287}
288
289void board_init_f(unsigned long bootflag)
290{
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530291 __aligned(8) gd_t local_gd;
Chander Kashyap81e35202012-02-05 23:01:48 +0000292 __attribute__((noreturn)) void (*uboot)(void);
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530293
294 setup_global_data(&local_gd);
295
296 if (do_lowlevel_init())
297 power_exit_wakeup();
298
Chander Kashyap81e35202012-02-05 23:01:48 +0000299 copy_uboot_to_ram();
300
301 /* Jump to U-Boot image */
302 uboot = (void *)CONFIG_SYS_TEXT_BASE;
303 (*uboot)();
304 /* Never returns Here */
305}
306
307/* Place Holders */
308void board_init_r(gd_t *id, ulong dest_addr)
309{
310 /* Function attribute is no-return */
311 /* This Function never executes */
312 while (1)
313 ;
314}