blob: 103bb38d4501ce95e96621e06ffb328f07b78132 [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>
Chander Kashyap81e35202012-02-05 23:01:48 +00008
Amarc748be02013-04-27 11:42:59 +05309#include <asm/arch/clock.h>
10#include <asm/arch/clk.h>
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053011#include <asm/arch/dmc.h>
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +053012#include <asm/arch/periph.h>
13#include <asm/arch/pinmux.h>
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053014#include <asm/arch/power.h>
Rajeshwari Shinde493c0732013-06-25 19:17:06 +053015#include <asm/arch/spl.h>
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +053016#include <asm/arch/spi.h>
Amarc748be02013-04-27 11:42:59 +053017
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053018#include "common_setup.h"
Amarc748be02013-04-27 11:42:59 +053019#include "clock_init.h"
20
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053021DECLARE_GLOBAL_DATA_PTR;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053022
Amarc748be02013-04-27 11:42:59 +053023/* Index into irom ptr table */
24enum index {
25 MMC_INDEX,
26 EMMC44_INDEX,
27 EMMC44_END_INDEX,
28 SPI_INDEX,
29 USB_INDEX,
30};
31
32/* IROM Function Pointers Table */
33u32 irom_ptr_table[] = {
34 [MMC_INDEX] = 0x02020030, /* iROM Function Pointer-SDMMC boot */
35 [EMMC44_INDEX] = 0x02020044, /* iROM Function Pointer-EMMC4.4 boot*/
36 [EMMC44_END_INDEX] = 0x02020048,/* iROM Function Pointer
37 -EMMC4.4 end boot operation */
38 [SPI_INDEX] = 0x02020058, /* iROM Function Pointer-SPI boot */
39 [USB_INDEX] = 0x02020070, /* iROM Function Pointer-USB boot*/
40 };
41
Amarc748be02013-04-27 11:42:59 +053042void *get_irom_func(int index)
43{
44 return (void *)*(u32 *)irom_ptr_table[index];
45}
Vivek Gautam70656c72013-01-28 00:39:59 +000046
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053047#ifdef CONFIG_USB_BOOTING
Vivek Gautam70656c72013-01-28 00:39:59 +000048/*
49 * Set/clear program flow prediction and return the previous state.
50 */
51static int config_branch_prediction(int set_cr_z)
52{
53 unsigned int cr;
54
55 /* System Control Register: 11th bit Z Branch prediction enable */
56 cr = get_cr();
57 set_cr(set_cr_z ? cr | CR_Z : cr & ~CR_Z);
58
59 return cr & CR_Z;
60}
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +053061#endif
Rajeshwari Shinde7a533772012-11-02 01:15:38 +000062
Minkyu Kangd8fa31a2013-12-06 19:04:03 +090063#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +053064static void spi_rx_tx(struct exynos_spi *regs, int todo,
65 void *dinp, void const *doutp, int i)
66{
67 uint *rxp = (uint *)(dinp + (i * (32 * 1024)));
68 int rx_lvl, tx_lvl;
69 uint out_bytes, in_bytes;
70
71 out_bytes = todo;
72 in_bytes = todo;
73 setbits_le32(&regs->ch_cfg, SPI_CH_RST);
74 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
75 writel(((todo * 8) / 32) | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
76
77 while (in_bytes) {
78 uint32_t spi_sts;
79 int temp;
80
81 spi_sts = readl(&regs->spi_sts);
82 rx_lvl = ((spi_sts >> 15) & 0x7f);
83 tx_lvl = ((spi_sts >> 6) & 0x7f);
84 while (tx_lvl < 32 && out_bytes) {
85 temp = 0xffffffff;
86 writel(temp, &regs->tx_data);
87 out_bytes -= 4;
88 tx_lvl += 4;
89 }
90 while (rx_lvl >= 4 && in_bytes) {
91 temp = readl(&regs->rx_data);
92 if (rxp)
93 *rxp++ = temp;
94 in_bytes -= 4;
95 rx_lvl -= 4;
96 }
97 }
98}
99
100/*
101 * Copy uboot from spi flash to RAM
102 *
103 * @parma uboot_size size of u-boot to copy
104 * @param uboot_addr address in u-boot to copy
105 */
106static void exynos_spi_copy(unsigned int uboot_size, unsigned int uboot_addr)
107{
108 int upto, todo;
109 int i, timeout = 100;
Patrick Delaunayac31d0d2019-02-27 15:20:34 +0100110 struct exynos_spi *regs = (struct exynos_spi *)CONFIG_SYS_SPI_BASE;
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530111
112 set_spi_clk(PERIPH_ID_SPI1, 50000000); /* set spi clock to 50Mhz */
113 /* set the spi1 GPIO */
114 exynos_pinmux_config(PERIPH_ID_SPI1, PINMUX_FLAG_NONE);
115
116 /* set pktcnt and enable it */
117 writel(4 | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
118 /* set FB_CLK_SEL */
119 writel(SPI_FB_DELAY_180, &regs->fb_clk);
120 /* set CH_WIDTH and BUS_WIDTH as word */
121 setbits_le32(&regs->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
122 SPI_MODE_BUS_WIDTH_WORD);
123 clrbits_le32(&regs->ch_cfg, SPI_CH_CPOL_L); /* CPOL: active high */
124
125 /* clear rx and tx channel if set priveously */
126 clrbits_le32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON);
127
128 setbits_le32(&regs->swap_cfg, SPI_RX_SWAP_EN |
129 SPI_RX_BYTE_SWAP |
130 SPI_RX_HWORD_SWAP);
131
132 /* do a soft reset */
133 setbits_le32(&regs->ch_cfg, SPI_CH_RST);
134 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
135
136 /* now set rx and tx channel ON */
137 setbits_le32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON | SPI_CH_HS_EN);
138 clrbits_le32(&regs->cs_reg, SPI_SLAVE_SIG_INACT); /* CS low */
139
140 /* Send read instruction (0x3h) followed by a 24 bit addr */
141 writel((SF_READ_DATA_CMD << 24) | SPI_FLASH_UBOOT_POS, &regs->tx_data);
142
143 /* waiting for TX done */
144 while (!(readl(&regs->spi_sts) & SPI_ST_TX_DONE)) {
145 if (!timeout) {
146 debug("SPI TIMEOUT\n");
147 break;
148 }
149 timeout--;
150 }
151
152 for (upto = 0, i = 0; upto < uboot_size; upto += todo, i++) {
Masahiro Yamadab4141192014-11-07 03:03:31 +0900153 todo = min(uboot_size - upto, (unsigned int)(1 << 15));
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530154 spi_rx_tx(regs, todo, (void *)(uboot_addr),
155 (void *)(SPI_FLASH_UBOOT_POS), i);
156 }
157
158 setbits_le32(&regs->cs_reg, SPI_SLAVE_SIG_INACT);/* make the CS high */
159
160 /*
161 * Let put controller mode to BYTE as
162 * SPI driver does not support WORD mode yet
163 */
164 clrbits_le32(&regs->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
165 SPI_MODE_BUS_WIDTH_WORD);
166 writel(0, &regs->swap_cfg);
167
168 /*
169 * Flush spi tx, rx fifos and reset the SPI controller
170 * and clear rx/tx channel
171 */
172 clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
173 clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
174 clrbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
175}
Minkyu Kangd8fa31a2013-12-06 19:04:03 +0900176#endif
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530177
Chander Kashyap81e35202012-02-05 23:01:48 +0000178/*
Bin Menga1875592016-02-05 19:30:11 -0800179* Copy U-Boot from mmc to RAM:
Chander Kashyap81e35202012-02-05 23:01:48 +0000180* COPY_BL2_FNPTR_ADDR: Address in iRAM, which Contains
181* Pointer to API (Data transfer from mmc to ram)
182*/
183void copy_uboot_to_ram(void)
184{
Przemyslaw Marczak4fb4d552014-09-01 13:50:44 +0200185 unsigned int bootmode = BOOT_MODE_OM;
Amarc748be02013-04-27 11:42:59 +0530186
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530187 u32 (*copy_bl2)(u32 offset, u32 nblock, u32 dst) = NULL;
188 u32 offset = 0, size = 0;
Minkyu Kangd8fa31a2013-12-06 19:04:03 +0900189#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530190 struct spl_machine_param *param = spl_get_machine_params();
Minkyu Kangd8fa31a2013-12-06 19:04:03 +0900191#endif
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530192#ifdef CONFIG_SUPPORT_EMMC_BOOT
Amarc748be02013-04-27 11:42:59 +0530193 u32 (*copy_bl2_from_emmc)(u32 nblock, u32 dst);
194 void (*end_bootop_from_emmc)(void);
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530195#endif
196#ifdef CONFIG_USB_BOOTING
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530197 int is_cr_z_set;
198 unsigned int sec_boot_check;
Chander Kashyap81e35202012-02-05 23:01:48 +0000199
Vadim Bendebury4f298622014-11-13 22:38:21 +0530200 /*
201 * Note that older hardware (before Exynos5800) does not expect any
202 * arguments, but it does not hurt to pass them, so a common function
203 * prototype is used.
204 */
205 u32 (*usb_copy)(u32 num_of_block, u32 *dst);
206
Vivek Gautam70656c72013-01-28 00:39:59 +0000207 /* Read iRAM location to check for secondary USB boot mode */
208 sec_boot_check = readl(EXYNOS_IRAM_SECONDARY_BASE);
209 if (sec_boot_check == EXYNOS_USB_SECONDARY_BOOT)
210 bootmode = BOOT_MODE_USB;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530211#endif
Vivek Gautam70656c72013-01-28 00:39:59 +0000212
213 if (bootmode == BOOT_MODE_OM)
Przemyslaw Marczak4fb4d552014-09-01 13:50:44 +0200214 bootmode = get_boot_mode();
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000215
216 switch (bootmode) {
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530217#ifdef CONFIG_SPI_BOOTING
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000218 case BOOT_MODE_SERIAL:
Rajeshwari Shinde347e45d2013-10-08 18:42:22 +0530219 /* Customised function to copy u-boot from SF */
220 exynos_spi_copy(param->uboot_size, CONFIG_SYS_TEXT_BASE);
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000221 break;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530222#endif
Przemyslaw Marczak4fb4d552014-09-01 13:50:44 +0200223 case BOOT_MODE_SD:
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530224 offset = BL2_START_OFFSET;
225 size = BL2_SIZE_BLOC_COUNT;
Amarc748be02013-04-27 11:42:59 +0530226 copy_bl2 = get_irom_func(MMC_INDEX);
Amarc748be02013-04-27 11:42:59 +0530227 break;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530228#ifdef CONFIG_SUPPORT_EMMC_BOOT
Amarc748be02013-04-27 11:42:59 +0530229 case BOOT_MODE_EMMC:
230 /* Set the FSYS1 clock divisor value for EMMC boot */
231 emmc_boot_clk_div_set();
232
233 copy_bl2_from_emmc = get_irom_func(EMMC44_INDEX);
234 end_bootop_from_emmc = get_irom_func(EMMC44_END_INDEX);
235
236 copy_bl2_from_emmc(BL2_SIZE_BLOC_COUNT, CONFIG_SYS_TEXT_BASE);
237 end_bootop_from_emmc();
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000238 break;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530239#endif
240#ifdef CONFIG_USB_BOOTING
Vivek Gautam70656c72013-01-28 00:39:59 +0000241 case BOOT_MODE_USB:
242 /*
243 * iROM needs program flow prediction to be disabled
244 * before copy from USB device to RAM
245 */
246 is_cr_z_set = config_branch_prediction(0);
Amarc748be02013-04-27 11:42:59 +0530247 usb_copy = get_irom_func(USB_INDEX);
Vadim Bendebury4f298622014-11-13 22:38:21 +0530248 usb_copy(0, (u32 *)CONFIG_SYS_TEXT_BASE);
Vivek Gautam70656c72013-01-28 00:39:59 +0000249 config_branch_prediction(is_cr_z_set);
250 break;
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530251#endif
Rajeshwari Shinde7a533772012-11-02 01:15:38 +0000252 default:
253 break;
254 }
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530255
256 if (copy_bl2)
257 copy_bl2(offset, size, CONFIG_SYS_TEXT_BASE);
258}
259
260void memzero(void *s, size_t n)
261{
262 char *ptr = s;
263 size_t i;
264
265 for (i = 0; i < n; i++)
266 *ptr++ = '\0';
267}
268
269/**
270 * Set up the U-Boot global_data pointer
271 *
272 * This sets the address of the global data, and sets up basic values.
273 *
274 * @param gdp Value to give to gd
275 */
276static void setup_global_data(gd_t *gdp)
277{
278 gd = gdp;
279 memzero((void *)gd, sizeof(gd_t));
280 gd->flags |= GD_FLG_RELOC;
281 gd->baudrate = CONFIG_BAUDRATE;
282 gd->have_console = 1;
Chander Kashyap81e35202012-02-05 23:01:48 +0000283}
284
285void board_init_f(unsigned long bootflag)
286{
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530287 __aligned(8) gd_t local_gd;
Chander Kashyap81e35202012-02-05 23:01:48 +0000288 __attribute__((noreturn)) void (*uboot)(void);
Rajeshwari Shinde643be9c2013-07-04 12:29:17 +0530289
290 setup_global_data(&local_gd);
291
292 if (do_lowlevel_init())
293 power_exit_wakeup();
294
Chander Kashyap81e35202012-02-05 23:01:48 +0000295 copy_uboot_to_ram();
296
297 /* Jump to U-Boot image */
298 uboot = (void *)CONFIG_SYS_TEXT_BASE;
299 (*uboot)();
300 /* Never returns Here */
301}
302
303/* Place Holders */
304void board_init_r(gd_t *id, ulong dest_addr)
305{
306 /* Function attribute is no-return */
307 /* This Function never executes */
308 while (1)
309 ;
310}