177b55e8cSThomas Abraham /*
277b55e8cSThomas Abraham * Copyright (C) 2012 Samsung Electronics
377b55e8cSThomas Abraham *
477b55e8cSThomas Abraham * SPDX-License-Identifier: GPL-2.0+
577b55e8cSThomas Abraham */
677b55e8cSThomas Abraham
777b55e8cSThomas Abraham #include <common.h>
877b55e8cSThomas Abraham #include <config.h>
977b55e8cSThomas Abraham
1077b55e8cSThomas Abraham #include <asm/arch/clock.h>
1177b55e8cSThomas Abraham #include <asm/arch/clk.h>
1277b55e8cSThomas Abraham #include <asm/arch/dmc.h>
1377b55e8cSThomas Abraham #include <asm/arch/periph.h>
1477b55e8cSThomas Abraham #include <asm/arch/pinmux.h>
1577b55e8cSThomas Abraham #include <asm/arch/power.h>
1677b55e8cSThomas Abraham #include <asm/arch/spl.h>
1777b55e8cSThomas Abraham #include <asm/arch/spi.h>
1877b55e8cSThomas Abraham
1977b55e8cSThomas Abraham #include "common_setup.h"
2077b55e8cSThomas Abraham #include "clock_init.h"
2177b55e8cSThomas Abraham
2277b55e8cSThomas Abraham DECLARE_GLOBAL_DATA_PTR;
2377b55e8cSThomas Abraham
2477b55e8cSThomas Abraham /* Index into irom ptr table */
2577b55e8cSThomas Abraham enum index {
2677b55e8cSThomas Abraham MMC_INDEX,
2777b55e8cSThomas Abraham EMMC44_INDEX,
2877b55e8cSThomas Abraham EMMC44_END_INDEX,
2977b55e8cSThomas Abraham SPI_INDEX,
3077b55e8cSThomas Abraham USB_INDEX,
3177b55e8cSThomas Abraham };
3277b55e8cSThomas Abraham
3377b55e8cSThomas Abraham /* IROM Function Pointers Table */
3477b55e8cSThomas Abraham u32 irom_ptr_table[] = {
3577b55e8cSThomas Abraham [MMC_INDEX] = 0x02020030, /* iROM Function Pointer-SDMMC boot */
3677b55e8cSThomas Abraham [EMMC44_INDEX] = 0x02020044, /* iROM Function Pointer-EMMC4.4 boot*/
3777b55e8cSThomas Abraham [EMMC44_END_INDEX] = 0x02020048,/* iROM Function Pointer
3877b55e8cSThomas Abraham -EMMC4.4 end boot operation */
3977b55e8cSThomas Abraham [SPI_INDEX] = 0x02020058, /* iROM Function Pointer-SPI boot */
4077b55e8cSThomas Abraham [USB_INDEX] = 0x02020070, /* iROM Function Pointer-USB boot*/
4177b55e8cSThomas Abraham };
4277b55e8cSThomas Abraham
get_irom_func(int index)4377b55e8cSThomas Abraham void *get_irom_func(int index)
4477b55e8cSThomas Abraham {
4577b55e8cSThomas Abraham return (void *)*(u32 *)irom_ptr_table[index];
4677b55e8cSThomas Abraham }
4777b55e8cSThomas Abraham
4877b55e8cSThomas Abraham #ifdef CONFIG_USB_BOOTING
4977b55e8cSThomas Abraham /*
5077b55e8cSThomas Abraham * Set/clear program flow prediction and return the previous state.
5177b55e8cSThomas Abraham */
config_branch_prediction(int set_cr_z)5277b55e8cSThomas Abraham static int config_branch_prediction(int set_cr_z)
5377b55e8cSThomas Abraham {
5477b55e8cSThomas Abraham unsigned int cr;
5577b55e8cSThomas Abraham
5677b55e8cSThomas Abraham /* System Control Register: 11th bit Z Branch prediction enable */
5777b55e8cSThomas Abraham cr = get_cr();
5877b55e8cSThomas Abraham set_cr(set_cr_z ? cr | CR_Z : cr & ~CR_Z);
5977b55e8cSThomas Abraham
6077b55e8cSThomas Abraham return cr & CR_Z;
6177b55e8cSThomas Abraham }
6277b55e8cSThomas Abraham #endif
6377b55e8cSThomas Abraham
6477b55e8cSThomas Abraham #ifdef CONFIG_SPI_BOOTING
spi_rx_tx(struct exynos_spi * regs,int todo,void * dinp,void const * doutp,int i)6577b55e8cSThomas Abraham static void spi_rx_tx(struct exynos_spi *regs, int todo,
6677b55e8cSThomas Abraham void *dinp, void const *doutp, int i)
6777b55e8cSThomas Abraham {
6877b55e8cSThomas Abraham uint *rxp = (uint *)(dinp + (i * (32 * 1024)));
6977b55e8cSThomas Abraham int rx_lvl, tx_lvl;
7077b55e8cSThomas Abraham uint out_bytes, in_bytes;
7177b55e8cSThomas Abraham
7277b55e8cSThomas Abraham out_bytes = todo;
7377b55e8cSThomas Abraham in_bytes = todo;
7477b55e8cSThomas Abraham setbits_le32(®s->ch_cfg, SPI_CH_RST);
7577b55e8cSThomas Abraham clrbits_le32(®s->ch_cfg, SPI_CH_RST);
7677b55e8cSThomas Abraham writel(((todo * 8) / 32) | SPI_PACKET_CNT_EN, ®s->pkt_cnt);
7777b55e8cSThomas Abraham
7877b55e8cSThomas Abraham while (in_bytes) {
7977b55e8cSThomas Abraham uint32_t spi_sts;
8077b55e8cSThomas Abraham int temp;
8177b55e8cSThomas Abraham
8277b55e8cSThomas Abraham spi_sts = readl(®s->spi_sts);
8377b55e8cSThomas Abraham rx_lvl = ((spi_sts >> 15) & 0x7f);
8477b55e8cSThomas Abraham tx_lvl = ((spi_sts >> 6) & 0x7f);
8577b55e8cSThomas Abraham while (tx_lvl < 32 && out_bytes) {
8677b55e8cSThomas Abraham temp = 0xffffffff;
8777b55e8cSThomas Abraham writel(temp, ®s->tx_data);
8877b55e8cSThomas Abraham out_bytes -= 4;
8977b55e8cSThomas Abraham tx_lvl += 4;
9077b55e8cSThomas Abraham }
9177b55e8cSThomas Abraham while (rx_lvl >= 4 && in_bytes) {
9277b55e8cSThomas Abraham temp = readl(®s->rx_data);
9377b55e8cSThomas Abraham if (rxp)
9477b55e8cSThomas Abraham *rxp++ = temp;
9577b55e8cSThomas Abraham in_bytes -= 4;
9677b55e8cSThomas Abraham rx_lvl -= 4;
9777b55e8cSThomas Abraham }
9877b55e8cSThomas Abraham }
9977b55e8cSThomas Abraham }
10077b55e8cSThomas Abraham
10177b55e8cSThomas Abraham /*
10277b55e8cSThomas Abraham * Copy uboot from spi flash to RAM
10377b55e8cSThomas Abraham *
10477b55e8cSThomas Abraham * @parma uboot_size size of u-boot to copy
10577b55e8cSThomas Abraham * @param uboot_addr address in u-boot to copy
10677b55e8cSThomas Abraham */
exynos_spi_copy(unsigned int uboot_size,unsigned int uboot_addr)10777b55e8cSThomas Abraham static void exynos_spi_copy(unsigned int uboot_size, unsigned int uboot_addr)
10877b55e8cSThomas Abraham {
10977b55e8cSThomas Abraham int upto, todo;
11077b55e8cSThomas Abraham int i, timeout = 100;
11177b55e8cSThomas Abraham struct exynos_spi *regs = (struct exynos_spi *)CONFIG_ENV_SPI_BASE;
11277b55e8cSThomas Abraham
11377b55e8cSThomas Abraham set_spi_clk(PERIPH_ID_SPI1, 50000000); /* set spi clock to 50Mhz */
11477b55e8cSThomas Abraham /* set the spi1 GPIO */
11577b55e8cSThomas Abraham exynos_pinmux_config(PERIPH_ID_SPI1, PINMUX_FLAG_NONE);
11677b55e8cSThomas Abraham
11777b55e8cSThomas Abraham /* set pktcnt and enable it */
11877b55e8cSThomas Abraham writel(4 | SPI_PACKET_CNT_EN, ®s->pkt_cnt);
11977b55e8cSThomas Abraham /* set FB_CLK_SEL */
12077b55e8cSThomas Abraham writel(SPI_FB_DELAY_180, ®s->fb_clk);
12177b55e8cSThomas Abraham /* set CH_WIDTH and BUS_WIDTH as word */
12277b55e8cSThomas Abraham setbits_le32(®s->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
12377b55e8cSThomas Abraham SPI_MODE_BUS_WIDTH_WORD);
12477b55e8cSThomas Abraham clrbits_le32(®s->ch_cfg, SPI_CH_CPOL_L); /* CPOL: active high */
12577b55e8cSThomas Abraham
12677b55e8cSThomas Abraham /* clear rx and tx channel if set priveously */
12777b55e8cSThomas Abraham clrbits_le32(®s->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON);
12877b55e8cSThomas Abraham
12977b55e8cSThomas Abraham setbits_le32(®s->swap_cfg, SPI_RX_SWAP_EN |
13077b55e8cSThomas Abraham SPI_RX_BYTE_SWAP |
13177b55e8cSThomas Abraham SPI_RX_HWORD_SWAP);
13277b55e8cSThomas Abraham
13377b55e8cSThomas Abraham /* do a soft reset */
13477b55e8cSThomas Abraham setbits_le32(®s->ch_cfg, SPI_CH_RST);
13577b55e8cSThomas Abraham clrbits_le32(®s->ch_cfg, SPI_CH_RST);
13677b55e8cSThomas Abraham
13777b55e8cSThomas Abraham /* now set rx and tx channel ON */
13877b55e8cSThomas Abraham setbits_le32(®s->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON | SPI_CH_HS_EN);
13977b55e8cSThomas Abraham clrbits_le32(®s->cs_reg, SPI_SLAVE_SIG_INACT); /* CS low */
14077b55e8cSThomas Abraham
14177b55e8cSThomas Abraham /* Send read instruction (0x3h) followed by a 24 bit addr */
14277b55e8cSThomas Abraham writel((SF_READ_DATA_CMD << 24) | SPI_FLASH_UBOOT_POS, ®s->tx_data);
14377b55e8cSThomas Abraham
14477b55e8cSThomas Abraham /* waiting for TX done */
14577b55e8cSThomas Abraham while (!(readl(®s->spi_sts) & SPI_ST_TX_DONE)) {
14677b55e8cSThomas Abraham if (!timeout) {
14777b55e8cSThomas Abraham debug("SPI TIMEOUT\n");
14877b55e8cSThomas Abraham break;
14977b55e8cSThomas Abraham }
15077b55e8cSThomas Abraham timeout--;
15177b55e8cSThomas Abraham }
15277b55e8cSThomas Abraham
15377b55e8cSThomas Abraham for (upto = 0, i = 0; upto < uboot_size; upto += todo, i++) {
15477b55e8cSThomas Abraham todo = min(uboot_size - upto, (unsigned int)(1 << 15));
15577b55e8cSThomas Abraham spi_rx_tx(regs, todo, (void *)(uboot_addr),
15677b55e8cSThomas Abraham (void *)(SPI_FLASH_UBOOT_POS), i);
15777b55e8cSThomas Abraham }
15877b55e8cSThomas Abraham
15977b55e8cSThomas Abraham setbits_le32(®s->cs_reg, SPI_SLAVE_SIG_INACT);/* make the CS high */
16077b55e8cSThomas Abraham
16177b55e8cSThomas Abraham /*
16277b55e8cSThomas Abraham * Let put controller mode to BYTE as
16377b55e8cSThomas Abraham * SPI driver does not support WORD mode yet
16477b55e8cSThomas Abraham */
16577b55e8cSThomas Abraham clrbits_le32(®s->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
16677b55e8cSThomas Abraham SPI_MODE_BUS_WIDTH_WORD);
16777b55e8cSThomas Abraham writel(0, ®s->swap_cfg);
16877b55e8cSThomas Abraham
16977b55e8cSThomas Abraham /*
17077b55e8cSThomas Abraham * Flush spi tx, rx fifos and reset the SPI controller
17177b55e8cSThomas Abraham * and clear rx/tx channel
17277b55e8cSThomas Abraham */
17377b55e8cSThomas Abraham clrsetbits_le32(®s->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
17477b55e8cSThomas Abraham clrbits_le32(®s->ch_cfg, SPI_CH_RST);
17577b55e8cSThomas Abraham clrbits_le32(®s->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
17677b55e8cSThomas Abraham }
17777b55e8cSThomas Abraham #endif
17877b55e8cSThomas Abraham
17977b55e8cSThomas Abraham /*
180*a187559eSBin Meng * Copy U-Boot from mmc to RAM:
18177b55e8cSThomas Abraham * COPY_BL2_FNPTR_ADDR: Address in iRAM, which Contains
18277b55e8cSThomas Abraham * Pointer to API (Data transfer from mmc to ram)
18377b55e8cSThomas Abraham */
copy_uboot_to_ram(void)18477b55e8cSThomas Abraham void copy_uboot_to_ram(void)
18577b55e8cSThomas Abraham {
18677b55e8cSThomas Abraham unsigned int bootmode = BOOT_MODE_OM;
18777b55e8cSThomas Abraham
18877b55e8cSThomas Abraham u32 (*copy_bl2)(u32 offset, u32 nblock, u32 dst) = NULL;
18977b55e8cSThomas Abraham u32 offset = 0, size = 0;
19077b55e8cSThomas Abraham #ifdef CONFIG_SPI_BOOTING
19177b55e8cSThomas Abraham struct spl_machine_param *param = spl_get_machine_params();
19277b55e8cSThomas Abraham #endif
19377b55e8cSThomas Abraham #ifdef CONFIG_SUPPORT_EMMC_BOOT
19477b55e8cSThomas Abraham u32 (*copy_bl2_from_emmc)(u32 nblock, u32 dst);
19577b55e8cSThomas Abraham void (*end_bootop_from_emmc)(void);
19677b55e8cSThomas Abraham #endif
19777b55e8cSThomas Abraham #ifdef CONFIG_USB_BOOTING
19877b55e8cSThomas Abraham int is_cr_z_set;
19977b55e8cSThomas Abraham unsigned int sec_boot_check;
20077b55e8cSThomas Abraham
20177b55e8cSThomas Abraham /*
20277b55e8cSThomas Abraham * Note that older hardware (before Exynos5800) does not expect any
20377b55e8cSThomas Abraham * arguments, but it does not hurt to pass them, so a common function
20477b55e8cSThomas Abraham * prototype is used.
20577b55e8cSThomas Abraham */
20677b55e8cSThomas Abraham u32 (*usb_copy)(u32 num_of_block, u32 *dst);
20777b55e8cSThomas Abraham
20877b55e8cSThomas Abraham /* Read iRAM location to check for secondary USB boot mode */
20977b55e8cSThomas Abraham sec_boot_check = readl(EXYNOS_IRAM_SECONDARY_BASE);
21077b55e8cSThomas Abraham if (sec_boot_check == EXYNOS_USB_SECONDARY_BOOT)
21177b55e8cSThomas Abraham bootmode = BOOT_MODE_USB;
21277b55e8cSThomas Abraham #endif
21377b55e8cSThomas Abraham
21477b55e8cSThomas Abraham if (bootmode == BOOT_MODE_OM)
21577b55e8cSThomas Abraham bootmode = get_boot_mode();
21677b55e8cSThomas Abraham
21777b55e8cSThomas Abraham switch (bootmode) {
21877b55e8cSThomas Abraham #ifdef CONFIG_SPI_BOOTING
21977b55e8cSThomas Abraham case BOOT_MODE_SERIAL:
22077b55e8cSThomas Abraham /* Customised function to copy u-boot from SF */
22177b55e8cSThomas Abraham exynos_spi_copy(param->uboot_size, CONFIG_SYS_TEXT_BASE);
22277b55e8cSThomas Abraham break;
22377b55e8cSThomas Abraham #endif
22477b55e8cSThomas Abraham case BOOT_MODE_SD:
22577b55e8cSThomas Abraham offset = BL2_START_OFFSET;
22677b55e8cSThomas Abraham size = BL2_SIZE_BLOC_COUNT;
22777b55e8cSThomas Abraham copy_bl2 = get_irom_func(MMC_INDEX);
22877b55e8cSThomas Abraham break;
22977b55e8cSThomas Abraham #ifdef CONFIG_SUPPORT_EMMC_BOOT
23077b55e8cSThomas Abraham case BOOT_MODE_EMMC:
23177b55e8cSThomas Abraham /* Set the FSYS1 clock divisor value for EMMC boot */
23277b55e8cSThomas Abraham emmc_boot_clk_div_set();
23377b55e8cSThomas Abraham
23477b55e8cSThomas Abraham copy_bl2_from_emmc = get_irom_func(EMMC44_INDEX);
23577b55e8cSThomas Abraham end_bootop_from_emmc = get_irom_func(EMMC44_END_INDEX);
23677b55e8cSThomas Abraham
23777b55e8cSThomas Abraham copy_bl2_from_emmc(BL2_SIZE_BLOC_COUNT, CONFIG_SYS_TEXT_BASE);
23877b55e8cSThomas Abraham end_bootop_from_emmc();
23977b55e8cSThomas Abraham break;
24077b55e8cSThomas Abraham #endif
24177b55e8cSThomas Abraham #ifdef CONFIG_USB_BOOTING
24277b55e8cSThomas Abraham case BOOT_MODE_USB:
24377b55e8cSThomas Abraham /*
24477b55e8cSThomas Abraham * iROM needs program flow prediction to be disabled
24577b55e8cSThomas Abraham * before copy from USB device to RAM
24677b55e8cSThomas Abraham */
24777b55e8cSThomas Abraham is_cr_z_set = config_branch_prediction(0);
24877b55e8cSThomas Abraham usb_copy = get_irom_func(USB_INDEX);
24977b55e8cSThomas Abraham usb_copy(0, (u32 *)CONFIG_SYS_TEXT_BASE);
25077b55e8cSThomas Abraham config_branch_prediction(is_cr_z_set);
25177b55e8cSThomas Abraham break;
25277b55e8cSThomas Abraham #endif
25377b55e8cSThomas Abraham default:
25477b55e8cSThomas Abraham break;
25577b55e8cSThomas Abraham }
25677b55e8cSThomas Abraham
25777b55e8cSThomas Abraham if (copy_bl2)
25877b55e8cSThomas Abraham copy_bl2(offset, size, CONFIG_SYS_TEXT_BASE);
25977b55e8cSThomas Abraham }
26077b55e8cSThomas Abraham
memzero(void * s,size_t n)26177b55e8cSThomas Abraham void memzero(void *s, size_t n)
26277b55e8cSThomas Abraham {
26377b55e8cSThomas Abraham char *ptr = s;
26477b55e8cSThomas Abraham size_t i;
26577b55e8cSThomas Abraham
26677b55e8cSThomas Abraham for (i = 0; i < n; i++)
26777b55e8cSThomas Abraham *ptr++ = '\0';
26877b55e8cSThomas Abraham }
26977b55e8cSThomas Abraham
27077b55e8cSThomas Abraham /**
27177b55e8cSThomas Abraham * Set up the U-Boot global_data pointer
27277b55e8cSThomas Abraham *
27377b55e8cSThomas Abraham * This sets the address of the global data, and sets up basic values.
27477b55e8cSThomas Abraham *
27577b55e8cSThomas Abraham * @param gdp Value to give to gd
27677b55e8cSThomas Abraham */
setup_global_data(gd_t * gdp)27777b55e8cSThomas Abraham static void setup_global_data(gd_t *gdp)
27877b55e8cSThomas Abraham {
27977b55e8cSThomas Abraham gd = gdp;
28077b55e8cSThomas Abraham memzero((void *)gd, sizeof(gd_t));
28177b55e8cSThomas Abraham gd->flags |= GD_FLG_RELOC;
28277b55e8cSThomas Abraham gd->baudrate = CONFIG_BAUDRATE;
28377b55e8cSThomas Abraham gd->have_console = 1;
28477b55e8cSThomas Abraham }
28577b55e8cSThomas Abraham
board_init_f(unsigned long bootflag)28677b55e8cSThomas Abraham void board_init_f(unsigned long bootflag)
28777b55e8cSThomas Abraham {
28877b55e8cSThomas Abraham __aligned(8) gd_t local_gd;
28977b55e8cSThomas Abraham __attribute__((noreturn)) void (*uboot)(void);
29077b55e8cSThomas Abraham
29177b55e8cSThomas Abraham setup_global_data(&local_gd);
29277b55e8cSThomas Abraham
29377b55e8cSThomas Abraham if (do_lowlevel_init())
29477b55e8cSThomas Abraham power_exit_wakeup();
29577b55e8cSThomas Abraham
29677b55e8cSThomas Abraham copy_uboot_to_ram();
29777b55e8cSThomas Abraham
29877b55e8cSThomas Abraham /* Jump to U-Boot image */
29977b55e8cSThomas Abraham uboot = (void *)CONFIG_SYS_TEXT_BASE;
30077b55e8cSThomas Abraham (*uboot)();
30177b55e8cSThomas Abraham /* Never returns Here */
30277b55e8cSThomas Abraham }
30377b55e8cSThomas Abraham
30477b55e8cSThomas Abraham /* Place Holders */
board_init_r(gd_t * id,ulong dest_addr)30577b55e8cSThomas Abraham void board_init_r(gd_t *id, ulong dest_addr)
30677b55e8cSThomas Abraham {
30777b55e8cSThomas Abraham /* Function attribute is no-return */
30877b55e8cSThomas Abraham /* This Function never executes */
30977b55e8cSThomas Abraham while (1)
31077b55e8cSThomas Abraham ;
31177b55e8cSThomas Abraham }
312