]> git.sur5r.net Git - u-boot/blob - arch/arm/cpu/armv7/exynos/spl_boot.c
exynos: spl: Add a custom spi copy function
[u-boot] / arch / arm / cpu / armv7 / exynos / spl_boot.c
1 /*
2  * Copyright (C) 2012 Samsung Electronics
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #include<common.h>
8 #include<config.h>
9
10 #include <asm/arch/clock.h>
11 #include <asm/arch/clk.h>
12 #include <asm/arch/dmc.h>
13 #include <asm/arch/periph.h>
14 #include <asm/arch/pinmux.h>
15 #include <asm/arch/power.h>
16 #include <asm/arch/spl.h>
17 #include <asm/arch/spi.h>
18
19 #include "common_setup.h"
20 #include "clock_init.h"
21
22 DECLARE_GLOBAL_DATA_PTR;
23 #define OM_STAT         (0x1f << 1)
24
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
44 void *get_irom_func(int index)
45 {
46         return (void *)*(u32 *)irom_ptr_table[index];
47 }
48
49 #ifdef CONFIG_USB_BOOTING
50 /*
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 }
63 #endif
64
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(&regs->ch_cfg, SPI_CH_RST);
75         clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
76         writel(((todo * 8) / 32) | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
77
78         while (in_bytes) {
79                 uint32_t spi_sts;
80                 int temp;
81
82                 spi_sts = readl(&regs->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, &regs->tx_data);
88                         out_bytes -= 4;
89                         tx_lvl += 4;
90                 }
91                 while (rx_lvl >= 4 && in_bytes) {
92                         temp = readl(&regs->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, &regs->pkt_cnt);
119         /* set FB_CLK_SEL */
120         writel(SPI_FB_DELAY_180, &regs->fb_clk);
121         /* set CH_WIDTH and BUS_WIDTH as word */
122         setbits_le32(&regs->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
123                                         SPI_MODE_BUS_WIDTH_WORD);
124         clrbits_le32(&regs->ch_cfg, SPI_CH_CPOL_L); /* CPOL: active high */
125
126         /* clear rx and tx channel if set priveously */
127         clrbits_le32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON);
128
129         setbits_le32(&regs->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(&regs->ch_cfg, SPI_CH_RST);
135         clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
136
137         /* now set rx and tx channel ON */
138         setbits_le32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON | SPI_CH_HS_EN);
139         clrbits_le32(&regs->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, &regs->tx_data);
143
144         /* waiting for TX done */
145         while (!(readl(&regs->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(&regs->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(&regs->mode_cfg, SPI_MODE_CH_WIDTH_WORD |
166                                         SPI_MODE_BUS_WIDTH_WORD);
167         writel(0, &regs->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(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
174         clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
175         clrbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
176 }
177
178 /*
179 * Copy U-boot from mmc to RAM:
180 * COPY_BL2_FNPTR_ADDR: Address in iRAM, which Contains
181 * Pointer to API (Data transfer from mmc to ram)
182 */
183 void copy_uboot_to_ram(void)
184 {
185         enum boot_mode bootmode = BOOT_MODE_OM;
186
187         u32 (*copy_bl2)(u32 offset, u32 nblock, u32 dst) = NULL;
188         u32 offset = 0, size = 0;
189         struct spl_machine_param *param = spl_get_machine_params();
190 #ifdef CONFIG_SUPPORT_EMMC_BOOT
191         u32 (*copy_bl2_from_emmc)(u32 nblock, u32 dst);
192         void (*end_bootop_from_emmc)(void);
193 #endif
194 #ifdef CONFIG_USB_BOOTING
195         u32 (*usb_copy)(void);
196         int is_cr_z_set;
197         unsigned int sec_boot_check;
198
199         /* Read iRAM location to check for secondary USB boot mode */
200         sec_boot_check = readl(EXYNOS_IRAM_SECONDARY_BASE);
201         if (sec_boot_check == EXYNOS_USB_SECONDARY_BOOT)
202                 bootmode = BOOT_MODE_USB;
203 #endif
204
205         if (bootmode == BOOT_MODE_OM)
206                 bootmode = readl(samsung_get_base_power()) & OM_STAT;
207
208         switch (bootmode) {
209 #ifdef CONFIG_SPI_BOOTING
210         case BOOT_MODE_SERIAL:
211                 /* Customised function to copy u-boot from SF */
212                 exynos_spi_copy(param->uboot_size, CONFIG_SYS_TEXT_BASE);
213                 break;
214 #endif
215         case BOOT_MODE_MMC:
216                 offset = BL2_START_OFFSET;
217                 size = BL2_SIZE_BLOC_COUNT;
218                 copy_bl2 = get_irom_func(MMC_INDEX);
219                 break;
220 #ifdef CONFIG_SUPPORT_EMMC_BOOT
221         case BOOT_MODE_EMMC:
222                 /* Set the FSYS1 clock divisor value for EMMC boot */
223                 emmc_boot_clk_div_set();
224
225                 copy_bl2_from_emmc = get_irom_func(EMMC44_INDEX);
226                 end_bootop_from_emmc = get_irom_func(EMMC44_END_INDEX);
227
228                 copy_bl2_from_emmc(BL2_SIZE_BLOC_COUNT, CONFIG_SYS_TEXT_BASE);
229                 end_bootop_from_emmc();
230                 break;
231 #endif
232 #ifdef CONFIG_USB_BOOTING
233         case BOOT_MODE_USB:
234                 /*
235                  * iROM needs program flow prediction to be disabled
236                  * before copy from USB device to RAM
237                  */
238                 is_cr_z_set = config_branch_prediction(0);
239                 usb_copy = get_irom_func(USB_INDEX);
240                 usb_copy();
241                 config_branch_prediction(is_cr_z_set);
242                 break;
243 #endif
244         default:
245                 break;
246         }
247
248         if (copy_bl2)
249                 copy_bl2(offset, size, CONFIG_SYS_TEXT_BASE);
250 }
251
252 void memzero(void *s, size_t n)
253 {
254         char *ptr = s;
255         size_t i;
256
257         for (i = 0; i < n; i++)
258                 *ptr++ = '\0';
259 }
260
261 /**
262  * Set up the U-Boot global_data pointer
263  *
264  * This sets the address of the global data, and sets up basic values.
265  *
266  * @param gdp   Value to give to gd
267  */
268 static void setup_global_data(gd_t *gdp)
269 {
270         gd = gdp;
271         memzero((void *)gd, sizeof(gd_t));
272         gd->flags |= GD_FLG_RELOC;
273         gd->baudrate = CONFIG_BAUDRATE;
274         gd->have_console = 1;
275 }
276
277 void board_init_f(unsigned long bootflag)
278 {
279         __aligned(8) gd_t local_gd;
280         __attribute__((noreturn)) void (*uboot)(void);
281
282         setup_global_data(&local_gd);
283
284         if (do_lowlevel_init())
285                 power_exit_wakeup();
286
287         copy_uboot_to_ram();
288
289         /* Jump to U-Boot image */
290         uboot = (void *)CONFIG_SYS_TEXT_BASE;
291         (*uboot)();
292         /* Never returns Here */
293 }
294
295 /* Place Holders */
296 void board_init_r(gd_t *id, ulong dest_addr)
297 {
298         /* Function attribute is no-return */
299         /* This Function never executes */
300         while (1)
301                 ;
302 }
303 void save_boot_params(u32 r0, u32 r1, u32 r2, u32 r3) {}