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