Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Samsung Electronics |
| 3 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Vasili Galka | 36ac1b5 | 2014-06-10 16:06:52 +0300 | [diff] [blame] | 7 | #include <common.h> |
| 8 | #include <config.h> |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 9 | |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 10 | #include <asm/arch/clock.h> |
| 11 | #include <asm/arch/clk.h> |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 12 | #include <asm/arch/dmc.h> |
Rajeshwari Shinde | 507f892 | 2013-10-08 18:42:22 +0530 | [diff] [blame] | 13 | #include <asm/arch/periph.h> |
| 14 | #include <asm/arch/pinmux.h> |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 15 | #include <asm/arch/power.h> |
Rajeshwari Shinde | bff6d0a | 2013-06-25 19:17:06 +0530 | [diff] [blame] | 16 | #include <asm/arch/spl.h> |
Rajeshwari Shinde | 507f892 | 2013-10-08 18:42:22 +0530 | [diff] [blame] | 17 | #include <asm/arch/spi.h> |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 18 | |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 19 | #include "common_setup.h" |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 20 | #include "clock_init.h" |
| 21 | |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 22 | DECLARE_GLOBAL_DATA_PTR; |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 23 | |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 24 | /* Index into irom ptr table */ |
| 25 | enum index { |
| 26 | MMC_INDEX, |
| 27 | EMMC44_INDEX, |
| 28 | EMMC44_END_INDEX, |
| 29 | SPI_INDEX, |
| 30 | USB_INDEX, |
| 31 | }; |
| 32 | |
| 33 | /* IROM Function Pointers Table */ |
| 34 | u32 irom_ptr_table[] = { |
| 35 | [MMC_INDEX] = 0x02020030, /* iROM Function Pointer-SDMMC boot */ |
| 36 | [EMMC44_INDEX] = 0x02020044, /* iROM Function Pointer-EMMC4.4 boot*/ |
| 37 | [EMMC44_END_INDEX] = 0x02020048,/* iROM Function Pointer |
| 38 | -EMMC4.4 end boot operation */ |
| 39 | [SPI_INDEX] = 0x02020058, /* iROM Function Pointer-SPI boot */ |
| 40 | [USB_INDEX] = 0x02020070, /* iROM Function Pointer-USB boot*/ |
| 41 | }; |
| 42 | |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 43 | void *get_irom_func(int index) |
| 44 | { |
| 45 | return (void *)*(u32 *)irom_ptr_table[index]; |
| 46 | } |
Rajeshwari Shinde | 9cb48e8 | 2012-11-02 01:15:38 +0000 | [diff] [blame] | 47 | |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 48 | #ifdef CONFIG_USB_BOOTING |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 49 | /* |
Vivek Gautam | 681dd83 | 2013-01-28 00:39:59 +0000 | [diff] [blame] | 50 | * Set/clear program flow prediction and return the previous state. |
| 51 | */ |
| 52 | static int config_branch_prediction(int set_cr_z) |
| 53 | { |
| 54 | unsigned int cr; |
| 55 | |
| 56 | /* System Control Register: 11th bit Z Branch prediction enable */ |
| 57 | cr = get_cr(); |
| 58 | set_cr(set_cr_z ? cr | CR_Z : cr & ~CR_Z); |
| 59 | |
| 60 | return cr & CR_Z; |
| 61 | } |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 62 | #endif |
Vivek Gautam | 681dd83 | 2013-01-28 00:39:59 +0000 | [diff] [blame] | 63 | |
Minkyu Kang | 1609f51 | 2013-12-06 19:04:03 +0900 | [diff] [blame] | 64 | #ifdef CONFIG_SPI_BOOTING |
Rajeshwari Shinde | 507f892 | 2013-10-08 18:42:22 +0530 | [diff] [blame] | 65 | static void spi_rx_tx(struct exynos_spi *regs, int todo, |
| 66 | void *dinp, void const *doutp, int i) |
| 67 | { |
| 68 | uint *rxp = (uint *)(dinp + (i * (32 * 1024))); |
| 69 | int rx_lvl, tx_lvl; |
| 70 | uint out_bytes, in_bytes; |
| 71 | |
| 72 | out_bytes = todo; |
| 73 | in_bytes = todo; |
| 74 | setbits_le32(®s->ch_cfg, SPI_CH_RST); |
| 75 | clrbits_le32(®s->ch_cfg, SPI_CH_RST); |
| 76 | writel(((todo * 8) / 32) | SPI_PACKET_CNT_EN, ®s->pkt_cnt); |
| 77 | |
| 78 | while (in_bytes) { |
| 79 | uint32_t spi_sts; |
| 80 | int temp; |
| 81 | |
| 82 | spi_sts = readl(®s->spi_sts); |
| 83 | rx_lvl = ((spi_sts >> 15) & 0x7f); |
| 84 | tx_lvl = ((spi_sts >> 6) & 0x7f); |
| 85 | while (tx_lvl < 32 && out_bytes) { |
| 86 | temp = 0xffffffff; |
| 87 | writel(temp, ®s->tx_data); |
| 88 | out_bytes -= 4; |
| 89 | tx_lvl += 4; |
| 90 | } |
| 91 | while (rx_lvl >= 4 && in_bytes) { |
| 92 | temp = readl(®s->rx_data); |
| 93 | if (rxp) |
| 94 | *rxp++ = temp; |
| 95 | in_bytes -= 4; |
| 96 | rx_lvl -= 4; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Copy uboot from spi flash to RAM |
| 103 | * |
| 104 | * @parma uboot_size size of u-boot to copy |
| 105 | * @param uboot_addr address in u-boot to copy |
| 106 | */ |
| 107 | static void exynos_spi_copy(unsigned int uboot_size, unsigned int uboot_addr) |
| 108 | { |
| 109 | int upto, todo; |
| 110 | int i, timeout = 100; |
| 111 | struct exynos_spi *regs = (struct exynos_spi *)CONFIG_ENV_SPI_BASE; |
| 112 | |
| 113 | set_spi_clk(PERIPH_ID_SPI1, 50000000); /* set spi clock to 50Mhz */ |
| 114 | /* set the spi1 GPIO */ |
| 115 | exynos_pinmux_config(PERIPH_ID_SPI1, PINMUX_FLAG_NONE); |
| 116 | |
| 117 | /* set pktcnt and enable it */ |
| 118 | writel(4 | SPI_PACKET_CNT_EN, ®s->pkt_cnt); |
| 119 | /* set FB_CLK_SEL */ |
| 120 | writel(SPI_FB_DELAY_180, ®s->fb_clk); |
| 121 | /* set CH_WIDTH and BUS_WIDTH as word */ |
| 122 | setbits_le32(®s->mode_cfg, SPI_MODE_CH_WIDTH_WORD | |
| 123 | SPI_MODE_BUS_WIDTH_WORD); |
| 124 | clrbits_le32(®s->ch_cfg, SPI_CH_CPOL_L); /* CPOL: active high */ |
| 125 | |
| 126 | /* clear rx and tx channel if set priveously */ |
| 127 | clrbits_le32(®s->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON); |
| 128 | |
| 129 | setbits_le32(®s->swap_cfg, SPI_RX_SWAP_EN | |
| 130 | SPI_RX_BYTE_SWAP | |
| 131 | SPI_RX_HWORD_SWAP); |
| 132 | |
| 133 | /* do a soft reset */ |
| 134 | setbits_le32(®s->ch_cfg, SPI_CH_RST); |
| 135 | clrbits_le32(®s->ch_cfg, SPI_CH_RST); |
| 136 | |
| 137 | /* now set rx and tx channel ON */ |
| 138 | setbits_le32(®s->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON | SPI_CH_HS_EN); |
| 139 | clrbits_le32(®s->cs_reg, SPI_SLAVE_SIG_INACT); /* CS low */ |
| 140 | |
| 141 | /* Send read instruction (0x3h) followed by a 24 bit addr */ |
| 142 | writel((SF_READ_DATA_CMD << 24) | SPI_FLASH_UBOOT_POS, ®s->tx_data); |
| 143 | |
| 144 | /* waiting for TX done */ |
| 145 | while (!(readl(®s->spi_sts) & SPI_ST_TX_DONE)) { |
| 146 | if (!timeout) { |
| 147 | debug("SPI TIMEOUT\n"); |
| 148 | break; |
| 149 | } |
| 150 | timeout--; |
| 151 | } |
| 152 | |
| 153 | for (upto = 0, i = 0; upto < uboot_size; upto += todo, i++) { |
| 154 | todo = min(uboot_size - upto, (1 << 15)); |
| 155 | spi_rx_tx(regs, todo, (void *)(uboot_addr), |
| 156 | (void *)(SPI_FLASH_UBOOT_POS), i); |
| 157 | } |
| 158 | |
| 159 | setbits_le32(®s->cs_reg, SPI_SLAVE_SIG_INACT);/* make the CS high */ |
| 160 | |
| 161 | /* |
| 162 | * Let put controller mode to BYTE as |
| 163 | * SPI driver does not support WORD mode yet |
| 164 | */ |
| 165 | clrbits_le32(®s->mode_cfg, SPI_MODE_CH_WIDTH_WORD | |
| 166 | SPI_MODE_BUS_WIDTH_WORD); |
| 167 | writel(0, ®s->swap_cfg); |
| 168 | |
| 169 | /* |
| 170 | * Flush spi tx, rx fifos and reset the SPI controller |
| 171 | * and clear rx/tx channel |
| 172 | */ |
| 173 | clrsetbits_le32(®s->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST); |
| 174 | clrbits_le32(®s->ch_cfg, SPI_CH_RST); |
| 175 | clrbits_le32(®s->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON); |
| 176 | } |
Minkyu Kang | 1609f51 | 2013-12-06 19:04:03 +0900 | [diff] [blame] | 177 | #endif |
Rajeshwari Shinde | 507f892 | 2013-10-08 18:42:22 +0530 | [diff] [blame] | 178 | |
Vivek Gautam | 681dd83 | 2013-01-28 00:39:59 +0000 | [diff] [blame] | 179 | /* |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 180 | * Copy U-boot from mmc to RAM: |
| 181 | * COPY_BL2_FNPTR_ADDR: Address in iRAM, which Contains |
| 182 | * Pointer to API (Data transfer from mmc to ram) |
| 183 | */ |
| 184 | void copy_uboot_to_ram(void) |
| 185 | { |
Przemyslaw Marczak | 93976f7 | 2014-09-01 13:50:44 +0200 | [diff] [blame] | 186 | unsigned int bootmode = BOOT_MODE_OM; |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 187 | |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 188 | u32 (*copy_bl2)(u32 offset, u32 nblock, u32 dst) = NULL; |
| 189 | u32 offset = 0, size = 0; |
Minkyu Kang | 1609f51 | 2013-12-06 19:04:03 +0900 | [diff] [blame] | 190 | #ifdef CONFIG_SPI_BOOTING |
Rajeshwari Shinde | 507f892 | 2013-10-08 18:42:22 +0530 | [diff] [blame] | 191 | struct spl_machine_param *param = spl_get_machine_params(); |
Minkyu Kang | 1609f51 | 2013-12-06 19:04:03 +0900 | [diff] [blame] | 192 | #endif |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 193 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 194 | u32 (*copy_bl2_from_emmc)(u32 nblock, u32 dst); |
| 195 | void (*end_bootop_from_emmc)(void); |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 196 | #endif |
| 197 | #ifdef CONFIG_USB_BOOTING |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 198 | u32 (*usb_copy)(void); |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 199 | int is_cr_z_set; |
| 200 | unsigned int sec_boot_check; |
Rajeshwari Shinde | 9cb48e8 | 2012-11-02 01:15:38 +0000 | [diff] [blame] | 201 | |
Vivek Gautam | 681dd83 | 2013-01-28 00:39:59 +0000 | [diff] [blame] | 202 | /* Read iRAM location to check for secondary USB boot mode */ |
| 203 | sec_boot_check = readl(EXYNOS_IRAM_SECONDARY_BASE); |
| 204 | if (sec_boot_check == EXYNOS_USB_SECONDARY_BOOT) |
| 205 | bootmode = BOOT_MODE_USB; |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 206 | #endif |
Vivek Gautam | 681dd83 | 2013-01-28 00:39:59 +0000 | [diff] [blame] | 207 | |
| 208 | if (bootmode == BOOT_MODE_OM) |
Przemyslaw Marczak | 93976f7 | 2014-09-01 13:50:44 +0200 | [diff] [blame] | 209 | bootmode = get_boot_mode(); |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 210 | |
Rajeshwari Shinde | 9cb48e8 | 2012-11-02 01:15:38 +0000 | [diff] [blame] | 211 | switch (bootmode) { |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 212 | #ifdef CONFIG_SPI_BOOTING |
Rajeshwari Shinde | 9cb48e8 | 2012-11-02 01:15:38 +0000 | [diff] [blame] | 213 | case BOOT_MODE_SERIAL: |
Rajeshwari Shinde | 507f892 | 2013-10-08 18:42:22 +0530 | [diff] [blame] | 214 | /* Customised function to copy u-boot from SF */ |
| 215 | exynos_spi_copy(param->uboot_size, CONFIG_SYS_TEXT_BASE); |
Rajeshwari Shinde | 9cb48e8 | 2012-11-02 01:15:38 +0000 | [diff] [blame] | 216 | break; |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 217 | #endif |
Przemyslaw Marczak | 93976f7 | 2014-09-01 13:50:44 +0200 | [diff] [blame] | 218 | case BOOT_MODE_SD: |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 219 | offset = BL2_START_OFFSET; |
| 220 | size = BL2_SIZE_BLOC_COUNT; |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 221 | copy_bl2 = get_irom_func(MMC_INDEX); |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 222 | break; |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 223 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 224 | case BOOT_MODE_EMMC: |
| 225 | /* Set the FSYS1 clock divisor value for EMMC boot */ |
| 226 | emmc_boot_clk_div_set(); |
| 227 | |
| 228 | copy_bl2_from_emmc = get_irom_func(EMMC44_INDEX); |
| 229 | end_bootop_from_emmc = get_irom_func(EMMC44_END_INDEX); |
| 230 | |
| 231 | copy_bl2_from_emmc(BL2_SIZE_BLOC_COUNT, CONFIG_SYS_TEXT_BASE); |
| 232 | end_bootop_from_emmc(); |
Rajeshwari Shinde | 9cb48e8 | 2012-11-02 01:15:38 +0000 | [diff] [blame] | 233 | break; |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 234 | #endif |
| 235 | #ifdef CONFIG_USB_BOOTING |
Vivek Gautam | 681dd83 | 2013-01-28 00:39:59 +0000 | [diff] [blame] | 236 | case BOOT_MODE_USB: |
| 237 | /* |
| 238 | * iROM needs program flow prediction to be disabled |
| 239 | * before copy from USB device to RAM |
| 240 | */ |
| 241 | is_cr_z_set = config_branch_prediction(0); |
Amar | e33add8 | 2013-04-27 11:42:59 +0530 | [diff] [blame] | 242 | usb_copy = get_irom_func(USB_INDEX); |
Vivek Gautam | 681dd83 | 2013-01-28 00:39:59 +0000 | [diff] [blame] | 243 | usb_copy(); |
| 244 | config_branch_prediction(is_cr_z_set); |
| 245 | break; |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 246 | #endif |
Rajeshwari Shinde | 9cb48e8 | 2012-11-02 01:15:38 +0000 | [diff] [blame] | 247 | default: |
| 248 | break; |
| 249 | } |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 250 | |
| 251 | if (copy_bl2) |
| 252 | copy_bl2(offset, size, CONFIG_SYS_TEXT_BASE); |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 255 | void memzero(void *s, size_t n) |
| 256 | { |
| 257 | char *ptr = s; |
| 258 | size_t i; |
| 259 | |
| 260 | for (i = 0; i < n; i++) |
| 261 | *ptr++ = '\0'; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Set up the U-Boot global_data pointer |
| 266 | * |
| 267 | * This sets the address of the global data, and sets up basic values. |
| 268 | * |
| 269 | * @param gdp Value to give to gd |
| 270 | */ |
| 271 | static void setup_global_data(gd_t *gdp) |
| 272 | { |
| 273 | gd = gdp; |
| 274 | memzero((void *)gd, sizeof(gd_t)); |
| 275 | gd->flags |= GD_FLG_RELOC; |
| 276 | gd->baudrate = CONFIG_BAUDRATE; |
| 277 | gd->have_console = 1; |
| 278 | } |
| 279 | |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 280 | void board_init_f(unsigned long bootflag) |
| 281 | { |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 282 | __aligned(8) gd_t local_gd; |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 283 | __attribute__((noreturn)) void (*uboot)(void); |
Rajeshwari Shinde | bed2442 | 2013-07-04 12:29:17 +0530 | [diff] [blame] | 284 | |
| 285 | setup_global_data(&local_gd); |
| 286 | |
| 287 | if (do_lowlevel_init()) |
| 288 | power_exit_wakeup(); |
| 289 | |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 290 | copy_uboot_to_ram(); |
| 291 | |
| 292 | /* Jump to U-Boot image */ |
| 293 | uboot = (void *)CONFIG_SYS_TEXT_BASE; |
| 294 | (*uboot)(); |
| 295 | /* Never returns Here */ |
| 296 | } |
| 297 | |
| 298 | /* Place Holders */ |
| 299 | void board_init_r(gd_t *id, ulong dest_addr) |
| 300 | { |
| 301 | /* Function attribute is no-return */ |
| 302 | /* This Function never executes */ |
| 303 | while (1) |
| 304 | ; |
| 305 | } |
Chander Kashyap | 1633dd1 | 2012-02-05 23:01:48 +0000 | [diff] [blame] | 306 | void save_boot_params(u32 r0, u32 r1, u32 r2, u32 r3) {} |