Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | c6bc1db | 2012-01-03 16:49:01 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 |
| 4 | * Heiko Schocher, DENX Software Engineering, hs@denx.de. |
| 5 | * |
| 6 | * Copyright (C) 2012 Stefan Roese <sr@denx.de> |
Stefan Roese | c6bc1db | 2012-01-03 16:49:01 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame^] | 10 | #include <init.h> |
Stefan Roese | 7618ad0 | 2015-08-18 09:27:17 +0200 | [diff] [blame] | 11 | #include <spl.h> |
Stefan Roese | c6bc1db | 2012-01-03 16:49:01 +0100 | [diff] [blame] | 12 | #include <version.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <asm/arch/hardware.h> |
| 15 | #include <asm/arch/spr_defs.h> |
| 16 | #include <asm/arch/spr_misc.h> |
| 17 | #include <asm/arch/spr_syscntl.h> |
Stefan Roese | 7618ad0 | 2015-08-18 09:27:17 +0200 | [diff] [blame] | 18 | #include <linux/mtd/st_smi.h> |
Stefan Roese | c6bc1db | 2012-01-03 16:49:01 +0100 | [diff] [blame] | 19 | |
Miquel Raynal | f8c5c78 | 2019-05-07 14:18:52 +0200 | [diff] [blame] | 20 | /* Reserve some space to store the BootROM's stack pointer during SPL operation. |
| 21 | * The BSS cannot be used for this purpose because it will be zeroed after |
| 22 | * having stored the pointer, so force the location to the data section. |
| 23 | */ |
| 24 | u32 bootrom_stash_sp __attribute__((section(".data"))); |
| 25 | |
Stefan Roese | c6bc1db | 2012-01-03 16:49:01 +0100 | [diff] [blame] | 26 | static void ddr_clock_init(void) |
| 27 | { |
| 28 | struct misc_regs *misc_p = (struct misc_regs *)CONFIG_SPEAR_MISCBASE; |
| 29 | u32 clkenb, ddrpll; |
| 30 | |
| 31 | clkenb = readl(&misc_p->periph1_clken); |
| 32 | clkenb &= ~PERIPH_MPMCMSK; |
| 33 | clkenb |= PERIPH_MPMC_WE; |
| 34 | |
| 35 | /* Intentionally done twice */ |
| 36 | writel(clkenb, &misc_p->periph1_clken); |
| 37 | writel(clkenb, &misc_p->periph1_clken); |
| 38 | |
| 39 | ddrpll = readl(&misc_p->pll_ctr_reg); |
| 40 | ddrpll &= ~MEM_CLK_SEL_MSK; |
| 41 | #if (CONFIG_DDR_HCLK) |
| 42 | ddrpll |= MEM_CLK_HCLK; |
| 43 | #elif (CONFIG_DDR_2HCLK) |
| 44 | ddrpll |= MEM_CLK_2HCLK; |
| 45 | #elif (CONFIG_DDR_PLL2) |
| 46 | ddrpll |= MEM_CLK_PLL2; |
| 47 | #else |
| 48 | #error "please define one of CONFIG_DDR_(HCLK|2HCLK|PLL2)" |
| 49 | #endif |
| 50 | writel(ddrpll, &misc_p->pll_ctr_reg); |
| 51 | |
| 52 | writel(readl(&misc_p->periph1_clken) | PERIPH_MPMC_EN, |
| 53 | &misc_p->periph1_clken); |
| 54 | } |
| 55 | |
| 56 | static void mpmc_init_values(void) |
| 57 | { |
| 58 | u32 i; |
| 59 | u32 *mpmc_reg_p = (u32 *)CONFIG_SPEAR_MPMCBASE; |
| 60 | u32 *mpmc_val_p = &mpmc_conf_vals[0]; |
| 61 | |
| 62 | for (i = 0; i < CONFIG_SPEAR_MPMCREGS; i++, mpmc_reg_p++, mpmc_val_p++) |
| 63 | writel(*mpmc_val_p, mpmc_reg_p); |
| 64 | |
| 65 | mpmc_reg_p = (u32 *)CONFIG_SPEAR_MPMCBASE; |
| 66 | |
| 67 | /* |
| 68 | * MPMC controller start |
| 69 | * MPMC waiting for DLLLOCKREG high |
| 70 | */ |
| 71 | writel(0x01000100, &mpmc_reg_p[7]); |
| 72 | |
| 73 | while (!(readl(&mpmc_reg_p[3]) & 0x10000)) |
| 74 | ; |
| 75 | } |
| 76 | |
| 77 | static void mpmc_init(void) |
| 78 | { |
| 79 | /* Clock related settings for DDR */ |
| 80 | ddr_clock_init(); |
| 81 | |
| 82 | /* |
| 83 | * DDR pad register bits are different for different SoCs |
| 84 | * Compensation values are also handled separately |
| 85 | */ |
| 86 | plat_ddr_init(); |
| 87 | |
| 88 | /* Initialize mpmc register values */ |
| 89 | mpmc_init_values(); |
| 90 | } |
| 91 | |
| 92 | static void pll_init(void) |
| 93 | { |
| 94 | struct misc_regs *misc_p = (struct misc_regs *)CONFIG_SPEAR_MISCBASE; |
| 95 | |
| 96 | /* Initialize PLLs */ |
| 97 | writel(FREQ_332, &misc_p->pll1_frq); |
| 98 | writel(0x1C0A, &misc_p->pll1_cntl); |
| 99 | writel(0x1C0E, &misc_p->pll1_cntl); |
| 100 | writel(0x1C06, &misc_p->pll1_cntl); |
| 101 | writel(0x1C0E, &misc_p->pll1_cntl); |
| 102 | |
| 103 | writel(FREQ_332, &misc_p->pll2_frq); |
| 104 | writel(0x1C0A, &misc_p->pll2_cntl); |
| 105 | writel(0x1C0E, &misc_p->pll2_cntl); |
| 106 | writel(0x1C06, &misc_p->pll2_cntl); |
| 107 | writel(0x1C0E, &misc_p->pll2_cntl); |
| 108 | |
| 109 | /* wait for pll locks */ |
| 110 | while (!(readl(&misc_p->pll1_cntl) & 0x1)) |
| 111 | ; |
| 112 | while (!(readl(&misc_p->pll2_cntl) & 0x1)) |
| 113 | ; |
| 114 | } |
| 115 | |
| 116 | static void mac_init(void) |
| 117 | { |
| 118 | struct misc_regs *misc_p = (struct misc_regs *)CONFIG_SPEAR_MISCBASE; |
| 119 | |
| 120 | writel(readl(&misc_p->periph1_clken) & (~PERIPH_GMAC), |
| 121 | &misc_p->periph1_clken); |
| 122 | |
| 123 | writel(SYNTH23, &misc_p->gmac_synth_clk); |
| 124 | |
| 125 | switch (get_socrev()) { |
| 126 | case SOC_SPEAR600_AA: |
| 127 | case SOC_SPEAR600_AB: |
| 128 | case SOC_SPEAR600_BA: |
| 129 | case SOC_SPEAR600_BB: |
| 130 | case SOC_SPEAR600_BC: |
| 131 | case SOC_SPEAR600_BD: |
| 132 | writel(0x0, &misc_p->gmac_ctr_reg); |
| 133 | break; |
| 134 | |
| 135 | case SOC_SPEAR300: |
| 136 | case SOC_SPEAR310: |
| 137 | case SOC_SPEAR320: |
| 138 | writel(0x4, &misc_p->gmac_ctr_reg); |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | writel(readl(&misc_p->periph1_clken) | PERIPH_GMAC, |
| 143 | &misc_p->periph1_clken); |
| 144 | |
| 145 | writel(readl(&misc_p->periph1_rst) | PERIPH_GMAC, |
| 146 | &misc_p->periph1_rst); |
| 147 | writel(readl(&misc_p->periph1_rst) & (~PERIPH_GMAC), |
| 148 | &misc_p->periph1_rst); |
| 149 | } |
| 150 | |
| 151 | static void sys_init(void) |
| 152 | { |
| 153 | struct misc_regs *misc_p = (struct misc_regs *)CONFIG_SPEAR_MISCBASE; |
| 154 | struct syscntl_regs *syscntl_p = |
| 155 | (struct syscntl_regs *)CONFIG_SPEAR_SYSCNTLBASE; |
| 156 | |
| 157 | /* Set system state to SLOW */ |
| 158 | writel(SLOW, &syscntl_p->scctrl); |
| 159 | writel(PLL_TIM << 3, &syscntl_p->scpllctrl); |
| 160 | |
| 161 | /* Initialize PLLs */ |
| 162 | pll_init(); |
| 163 | |
| 164 | /* |
| 165 | * Ethernet configuration |
| 166 | * To be done only if the tftp boot is not selected already |
| 167 | * Boot code ensures the correct configuration in tftp booting |
| 168 | */ |
| 169 | if (!tftp_boot_selected()) |
| 170 | mac_init(); |
| 171 | |
| 172 | writel(RTC_DISABLE | PLLTIMEEN, &misc_p->periph_clk_cfg); |
| 173 | writel(0x555, &misc_p->amba_clk_cfg); |
| 174 | |
| 175 | writel(NORMAL, &syscntl_p->scctrl); |
| 176 | |
| 177 | /* Wait for system to switch to normal mode */ |
| 178 | while (((readl(&syscntl_p->scctrl) >> MODE_SHIFT) & MODE_MASK) |
| 179 | != NORMAL) |
| 180 | ; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * get_socrev |
| 185 | * |
| 186 | * Get SoC Revision. |
| 187 | * @return SOC_SPEARXXX |
| 188 | */ |
| 189 | int get_socrev(void) |
| 190 | { |
| 191 | #if defined(CONFIG_SPEAR600) |
| 192 | struct misc_regs *misc_p = (struct misc_regs *)CONFIG_SPEAR_MISCBASE; |
| 193 | u32 soc_id = readl(&misc_p->soc_core_id); |
| 194 | u32 pri_socid = (soc_id >> SOC_PRI_SHFT) & 0xFF; |
| 195 | u32 sec_socid = (soc_id >> SOC_SEC_SHFT) & 0xFF; |
| 196 | |
| 197 | if ((pri_socid == 'B') && (sec_socid == 'B')) |
| 198 | return SOC_SPEAR600_BB; |
| 199 | else if ((pri_socid == 'B') && (sec_socid == 'C')) |
| 200 | return SOC_SPEAR600_BC; |
| 201 | else if ((pri_socid == 'B') && (sec_socid == 'D')) |
| 202 | return SOC_SPEAR600_BD; |
| 203 | else if (soc_id == 0) |
| 204 | return SOC_SPEAR600_BA; |
| 205 | else |
| 206 | return SOC_SPEAR_NA; |
| 207 | #elif defined(CONFIG_SPEAR300) |
| 208 | return SOC_SPEAR300; |
| 209 | #elif defined(CONFIG_SPEAR310) |
| 210 | return SOC_SPEAR310; |
| 211 | #elif defined(CONFIG_SPEAR320) |
| 212 | return SOC_SPEAR320; |
| 213 | #endif |
| 214 | } |
| 215 | |
Stefan Roese | 7618ad0 | 2015-08-18 09:27:17 +0200 | [diff] [blame] | 216 | /* |
| 217 | * SNOR (Serial NOR flash) related functions |
| 218 | */ |
| 219 | static void snor_init(void) |
| 220 | { |
| 221 | struct smi_regs *const smicntl = |
| 222 | (struct smi_regs * const)CONFIG_SYS_SMI_BASE; |
| 223 | |
| 224 | /* Setting the fast mode values. SMI working at 166/4 = 41.5 MHz */ |
| 225 | writel(HOLD1 | FAST_MODE | BANK_EN | DSEL_TIME | PRESCAL4, |
| 226 | &smicntl->smi_cr1); |
| 227 | } |
| 228 | |
| 229 | u32 spl_boot_device(void) |
| 230 | { |
xypron.glpk@gmx.de | 699c223 | 2017-07-30 20:09:38 +0200 | [diff] [blame] | 231 | u32 mode = 0; |
Stefan Roese | 7618ad0 | 2015-08-18 09:27:17 +0200 | [diff] [blame] | 232 | |
Miquel Raynal | f8c5c78 | 2019-05-07 14:18:52 +0200 | [diff] [blame] | 233 | if (usb_boot_selected()) { |
| 234 | mode = BOOT_DEVICE_BOOTROM; |
| 235 | } else if (snor_boot_selected()) { |
Stefan Roese | 7618ad0 | 2015-08-18 09:27:17 +0200 | [diff] [blame] | 236 | /* SNOR-SMI initialization */ |
| 237 | snor_init(); |
| 238 | |
| 239 | mode = BOOT_DEVICE_NOR; |
| 240 | } |
| 241 | |
| 242 | return mode; |
| 243 | } |
| 244 | |
Miquel Raynal | 307bfd6 | 2019-05-07 14:18:54 +0200 | [diff] [blame] | 245 | void board_boot_order(u32 *spl_boot_list) |
| 246 | { |
| 247 | spl_boot_list[0] = spl_boot_device(); |
| 248 | |
| 249 | /* |
| 250 | * If the main boot device (eg. NOR) is empty, try to jump back into the |
| 251 | * BootROM for USB boot process. |
| 252 | */ |
| 253 | if (USB_BOOT_SUPPORTED) |
| 254 | spl_boot_list[1] = BOOT_DEVICE_BOOTROM; |
| 255 | } |
| 256 | |
Stefan Roese | 7618ad0 | 2015-08-18 09:27:17 +0200 | [diff] [blame] | 257 | void board_init_f(ulong dummy) |
Stefan Roese | c6bc1db | 2012-01-03 16:49:01 +0100 | [diff] [blame] | 258 | { |
| 259 | struct misc_regs *misc_p = (struct misc_regs *)CONFIG_SPEAR_MISCBASE; |
Stefan Roese | c6bc1db | 2012-01-03 16:49:01 +0100 | [diff] [blame] | 260 | |
| 261 | /* Initialize PLLs */ |
| 262 | sys_init(); |
| 263 | |
Stefan Roese | 7618ad0 | 2015-08-18 09:27:17 +0200 | [diff] [blame] | 264 | preloader_console_init(); |
| 265 | arch_cpu_init(); |
Stefan Roese | c6bc1db | 2012-01-03 16:49:01 +0100 | [diff] [blame] | 266 | |
| 267 | /* Enable IPs (release reset) */ |
| 268 | writel(PERIPH_RST_ALL, &misc_p->periph1_rst); |
| 269 | |
| 270 | /* Initialize MPMC */ |
Stefan Roese | 7618ad0 | 2015-08-18 09:27:17 +0200 | [diff] [blame] | 271 | puts("Configure DDR\n"); |
Stefan Roese | c6bc1db | 2012-01-03 16:49:01 +0100 | [diff] [blame] | 272 | mpmc_init(); |
Stefan Roese | 7618ad0 | 2015-08-18 09:27:17 +0200 | [diff] [blame] | 273 | spear_late_init(); |
Stefan Roese | c6bc1db | 2012-01-03 16:49:01 +0100 | [diff] [blame] | 274 | } |
Miquel Raynal | f8c5c78 | 2019-05-07 14:18:52 +0200 | [diff] [blame] | 275 | |
| 276 | /* |
| 277 | * In a few cases (Ethernet, UART or USB boot, we might want to go back into the |
| 278 | * BootROM code right after having initialized a few components like the DRAM). |
| 279 | * The following function is called from SPL common code (board_init_r). |
| 280 | */ |
Peng Fan | aa050c5 | 2019-08-07 06:40:53 +0000 | [diff] [blame] | 281 | int board_return_to_bootrom(struct spl_image_info *spl_image, |
| 282 | struct spl_boot_device *bootdev) |
Miquel Raynal | f8c5c78 | 2019-05-07 14:18:52 +0200 | [diff] [blame] | 283 | { |
| 284 | /* |
| 285 | * Retrieve the BootROM's stack pointer and jump back to the start of |
| 286 | * the SPL, where we can easily branch back into the BootROM. Don't do |
| 287 | * it right here because SPL might be compiled in Thumb mode while the |
| 288 | * BootROM expects ARM mode. |
| 289 | */ |
| 290 | asm volatile ("ldr r0, =bootrom_stash_sp;" |
| 291 | "ldr r0, [r0];" |
| 292 | "mov sp, r0;" |
| 293 | #if defined(CONFIG_SPL_SYS_THUMB_BUILD) |
| 294 | "blx back_to_bootrom;" |
| 295 | #else |
| 296 | "bl back_to_bootrom;" |
| 297 | #endif |
| 298 | ); |
Peng Fan | aa050c5 | 2019-08-07 06:40:53 +0000 | [diff] [blame] | 299 | |
| 300 | return 0; |
Miquel Raynal | f8c5c78 | 2019-05-07 14:18:52 +0200 | [diff] [blame] | 301 | } |