Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Samsung Electronics |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Vasili Galka | 36ac1b5 | 2014-06-10 16:06:52 +0300 | [diff] [blame] | 6 | #include <common.h> |
| 7 | #include <config.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 8 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 10 | |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 11 | #include <asm/cache.h> |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 12 | #include <asm/arch/clock.h> |
| 13 | #include <asm/arch/clk.h> |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 14 | #include <asm/arch/dmc.h> |
Rajeshwari Shinde | 507f892 | 2013-10-08 18:42:22 +0530 | [diff] [blame] | 15 | #include <asm/arch/periph.h> |
| 16 | #include <asm/arch/pinmux.h> |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 17 | #include <asm/arch/power.h> |
Rajeshwari Shinde | bff6d0a | 2013-06-25 19:17:06 +0530 | [diff] [blame] | 18 | #include <asm/arch/spl.h> |
Rajeshwari Shinde | 507f892 | 2013-10-08 18:42:22 +0530 | [diff] [blame] | 19 | #include <asm/arch/spi.h> |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 20 | |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 21 | #include "common_setup.h" |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 22 | #include "clock_init.h" |
| 23 | |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 24 | DECLARE_GLOBAL_DATA_PTR; |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 25 | |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 26 | /* Index into irom ptr table */ |
| 27 | enum index { |
| 28 | MMC_INDEX, |
| 29 | EMMC44_INDEX, |
| 30 | EMMC44_END_INDEX, |
| 31 | SPI_INDEX, |
| 32 | USB_INDEX, |
| 33 | }; |
| 34 | |
| 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*/ |
| 43 | }; |
| 44 | |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 45 | void *get_irom_func(int index) |
| 46 | { |
| 47 | return (void *)*(u32 *)irom_ptr_table[index]; |
| 48 | } |
Rajeshwari Shinde | 9cb48e8 | 2012-11-02 01:15:38 +0000 | [diff] [blame] | 49 | |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 50 | #ifdef CONFIG_USB_BOOTING |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 51 | /* |
Vivek Gautam | 681dd83 | 2013-01-28 00:39:59 +0000 | [diff] [blame] | 52 | * Set/clear program flow prediction and return the previous state. |
| 53 | */ |
| 54 | static 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 Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 64 | #endif |
Vivek Gautam | 681dd83 | 2013-01-28 00:39:59 +0000 | [diff] [blame] | 65 | |
Minkyu Kang | 1609f51 | 2013-12-06 19:04:03 +0900 | [diff] [blame] | 66 | #ifdef CONFIG_SPI_BOOTING |
Rajeshwari Shinde | 507f892 | 2013-10-08 18:42:22 +0530 | [diff] [blame] | 67 | static 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(®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); |
| 79 | |
| 80 | while (in_bytes) { |
| 81 | uint32_t spi_sts; |
| 82 | int temp; |
| 83 | |
| 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) { |
| 88 | temp = 0xffffffff; |
| 89 | writel(temp, ®s->tx_data); |
| 90 | out_bytes -= 4; |
| 91 | tx_lvl += 4; |
| 92 | } |
| 93 | while (rx_lvl >= 4 && in_bytes) { |
| 94 | temp = readl(®s->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 | */ |
| 109 | static void exynos_spi_copy(unsigned int uboot_size, unsigned int uboot_addr) |
| 110 | { |
| 111 | int upto, todo; |
| 112 | int i, timeout = 100; |
Patrick Delaunay | 5c95efb | 2019-02-27 15:20:34 +0100 | [diff] [blame] | 113 | struct exynos_spi *regs = (struct exynos_spi *)CONFIG_SYS_SPI_BASE; |
Rajeshwari Shinde | 507f892 | 2013-10-08 18:42:22 +0530 | [diff] [blame] | 114 | |
| 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, ®s->pkt_cnt); |
| 121 | /* set FB_CLK_SEL */ |
| 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 */ |
| 127 | |
| 128 | /* clear rx and tx channel if set priveously */ |
| 129 | clrbits_le32(®s->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON); |
| 130 | |
| 131 | setbits_le32(®s->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(®s->ch_cfg, SPI_CH_RST); |
| 137 | clrbits_le32(®s->ch_cfg, SPI_CH_RST); |
| 138 | |
| 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 */ |
| 142 | |
| 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); |
| 145 | |
| 146 | /* waiting for TX done */ |
| 147 | while (!(readl(®s->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 Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 156 | todo = min(uboot_size - upto, (unsigned int)(1 << 15)); |
Rajeshwari Shinde | 507f892 | 2013-10-08 18:42:22 +0530 | [diff] [blame] | 157 | spi_rx_tx(regs, todo, (void *)(uboot_addr), |
| 158 | (void *)(SPI_FLASH_UBOOT_POS), i); |
| 159 | } |
| 160 | |
| 161 | setbits_le32(®s->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(®s->mode_cfg, SPI_MODE_CH_WIDTH_WORD | |
| 168 | SPI_MODE_BUS_WIDTH_WORD); |
| 169 | writel(0, ®s->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(®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); |
| 178 | } |
Minkyu Kang | 1609f51 | 2013-12-06 19:04:03 +0900 | [diff] [blame] | 179 | #endif |
Rajeshwari Shinde | 507f892 | 2013-10-08 18:42:22 +0530 | [diff] [blame] | 180 | |
Vivek Gautam | 681dd83 | 2013-01-28 00:39:59 +0000 | [diff] [blame] | 181 | /* |
Bin Meng | 7557405 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 182 | * Copy U-Boot from mmc to RAM: |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 183 | * COPY_BL2_FNPTR_ADDR: Address in iRAM, which Contains |
| 184 | * Pointer to API (Data transfer from mmc to ram) |
| 185 | */ |
| 186 | void copy_uboot_to_ram(void) |
| 187 | { |
Przemyslaw Marczak | 93976f7 | 2014-09-01 13:50:44 +0200 | [diff] [blame] | 188 | unsigned int bootmode = BOOT_MODE_OM; |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 189 | |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 190 | u32 (*copy_bl2)(u32 offset, u32 nblock, u32 dst) = NULL; |
| 191 | u32 offset = 0, size = 0; |
Minkyu Kang | 1609f51 | 2013-12-06 19:04:03 +0900 | [diff] [blame] | 192 | #ifdef CONFIG_SPI_BOOTING |
Rajeshwari Shinde | 507f892 | 2013-10-08 18:42:22 +0530 | [diff] [blame] | 193 | struct spl_machine_param *param = spl_get_machine_params(); |
Minkyu Kang | 1609f51 | 2013-12-06 19:04:03 +0900 | [diff] [blame] | 194 | #endif |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 195 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 196 | u32 (*copy_bl2_from_emmc)(u32 nblock, u32 dst); |
| 197 | void (*end_bootop_from_emmc)(void); |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 198 | #endif |
| 199 | #ifdef CONFIG_USB_BOOTING |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 200 | int is_cr_z_set; |
| 201 | unsigned int sec_boot_check; |
Rajeshwari Shinde | 9cb48e8 | 2012-11-02 01:15:38 +0000 | [diff] [blame] | 202 | |
Vadim Bendebury | c12efec | 2014-11-13 22:38:21 +0530 | [diff] [blame] | 203 | /* |
| 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 Gautam | 681dd83 | 2013-01-28 00:39:59 +0000 | [diff] [blame] | 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; |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 214 | #endif |
Vivek Gautam | 681dd83 | 2013-01-28 00:39:59 +0000 | [diff] [blame] | 215 | |
| 216 | if (bootmode == BOOT_MODE_OM) |
Przemyslaw Marczak | 93976f7 | 2014-09-01 13:50:44 +0200 | [diff] [blame] | 217 | bootmode = get_boot_mode(); |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 218 | |
Rajeshwari Shinde | 9cb48e8 | 2012-11-02 01:15:38 +0000 | [diff] [blame] | 219 | switch (bootmode) { |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 220 | #ifdef CONFIG_SPI_BOOTING |
Rajeshwari Shinde | 9cb48e8 | 2012-11-02 01:15:38 +0000 | [diff] [blame] | 221 | case BOOT_MODE_SERIAL: |
Rajeshwari Shinde | 507f892 | 2013-10-08 18:42:22 +0530 | [diff] [blame] | 222 | /* Customised function to copy u-boot from SF */ |
| 223 | exynos_spi_copy(param->uboot_size, CONFIG_SYS_TEXT_BASE); |
Rajeshwari Shinde | 9cb48e8 | 2012-11-02 01:15:38 +0000 | [diff] [blame] | 224 | break; |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 225 | #endif |
Przemyslaw Marczak | 93976f7 | 2014-09-01 13:50:44 +0200 | [diff] [blame] | 226 | case BOOT_MODE_SD: |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 227 | offset = BL2_START_OFFSET; |
| 228 | size = BL2_SIZE_BLOC_COUNT; |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 229 | copy_bl2 = get_irom_func(MMC_INDEX); |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 230 | break; |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 231 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 232 | 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 Shinde | 9cb48e8 | 2012-11-02 01:15:38 +0000 | [diff] [blame] | 241 | break; |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 242 | #endif |
| 243 | #ifdef CONFIG_USB_BOOTING |
Vivek Gautam | 681dd83 | 2013-01-28 00:39:59 +0000 | [diff] [blame] | 244 | 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); |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 250 | usb_copy = get_irom_func(USB_INDEX); |
Vadim Bendebury | c12efec | 2014-11-13 22:38:21 +0530 | [diff] [blame] | 251 | usb_copy(0, (u32 *)CONFIG_SYS_TEXT_BASE); |
Vivek Gautam | 681dd83 | 2013-01-28 00:39:59 +0000 | [diff] [blame] | 252 | config_branch_prediction(is_cr_z_set); |
| 253 | break; |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 254 | #endif |
Rajeshwari Shinde | 9cb48e8 | 2012-11-02 01:15:38 +0000 | [diff] [blame] | 255 | default: |
| 256 | break; |
| 257 | } |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 258 | |
| 259 | if (copy_bl2) |
| 260 | copy_bl2(offset, size, CONFIG_SYS_TEXT_BASE); |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 263 | void 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 | */ |
| 279 | static 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 Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 288 | void board_init_f(unsigned long bootflag) |
| 289 | { |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 290 | __aligned(8) gd_t local_gd; |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 291 | __attribute__((noreturn)) void (*uboot)(void); |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 292 | |
| 293 | setup_global_data(&local_gd); |
| 294 | |
| 295 | if (do_lowlevel_init()) |
| 296 | power_exit_wakeup(); |
| 297 | |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 298 | 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 */ |
| 307 | void 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 | } |