travis: Install pyelftools via pip
[platform/kernel/u-boot.git] / board / freescale / ls1046aqds / ls1046aqds.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2016 Freescale Semiconductor, Inc.
4  * Copyright 2019 NXP
5  */
6
7 #include <common.h>
8 #include <i2c.h>
9 #include <fdt_support.h>
10 #include <fsl_ddr_sdram.h>
11 #include <init.h>
12 #include <asm/io.h>
13 #include <asm/arch/clock.h>
14 #include <asm/arch/fsl_serdes.h>
15 #include <asm/arch/ppa.h>
16 #include <asm/arch/fdt.h>
17 #include <asm/arch/mmu.h>
18 #include <asm/arch/cpu.h>
19 #include <asm/arch/soc.h>
20 #include <asm/arch-fsl-layerscape/fsl_icid.h>
21 #include <ahci.h>
22 #include <hwconfig.h>
23 #include <mmc.h>
24 #include <scsi.h>
25 #include <fm_eth.h>
26 #include <fsl_csu.h>
27 #include <fsl_esdhc.h>
28 #include <fsl_ifc.h>
29 #include <fsl_sec.h>
30 #include <spl.h>
31
32 #include "../common/vid.h"
33 #include "../common/qixis.h"
34 #include "ls1046aqds_qixis.h"
35
36 DECLARE_GLOBAL_DATA_PTR;
37
38 #ifdef CONFIG_TFABOOT
39 struct ifc_regs ifc_cfg_nor_boot[CONFIG_SYS_FSL_IFC_BANK_COUNT] = {
40         {
41                 "nor0",
42                 CONFIG_SYS_NOR0_CSPR,
43                 CONFIG_SYS_NOR0_CSPR_EXT,
44                 CONFIG_SYS_NOR_AMASK,
45                 CONFIG_SYS_NOR_CSOR,
46                 {
47                         CONFIG_SYS_NOR_FTIM0,
48                         CONFIG_SYS_NOR_FTIM1,
49                         CONFIG_SYS_NOR_FTIM2,
50                         CONFIG_SYS_NOR_FTIM3
51                 },
52
53         },
54         {
55                 "nor1",
56                 CONFIG_SYS_NOR1_CSPR,
57                 CONFIG_SYS_NOR1_CSPR_EXT,
58                 CONFIG_SYS_NOR_AMASK,
59                 CONFIG_SYS_NOR_CSOR,
60                 {
61                         CONFIG_SYS_NOR_FTIM0,
62                         CONFIG_SYS_NOR_FTIM1,
63                         CONFIG_SYS_NOR_FTIM2,
64                         CONFIG_SYS_NOR_FTIM3
65                 },
66         },
67         {
68                 "nand",
69                 CONFIG_SYS_NAND_CSPR,
70                 CONFIG_SYS_NAND_CSPR_EXT,
71                 CONFIG_SYS_NAND_AMASK,
72                 CONFIG_SYS_NAND_CSOR,
73                 {
74                         CONFIG_SYS_NAND_FTIM0,
75                         CONFIG_SYS_NAND_FTIM1,
76                         CONFIG_SYS_NAND_FTIM2,
77                         CONFIG_SYS_NAND_FTIM3
78                 },
79         },
80         {
81                 "fpga",
82                 CONFIG_SYS_FPGA_CSPR,
83                 CONFIG_SYS_FPGA_CSPR_EXT,
84                 CONFIG_SYS_FPGA_AMASK,
85                 CONFIG_SYS_FPGA_CSOR,
86                 {
87                         CONFIG_SYS_FPGA_FTIM0,
88                         CONFIG_SYS_FPGA_FTIM1,
89                         CONFIG_SYS_FPGA_FTIM2,
90                         CONFIG_SYS_FPGA_FTIM3
91                 },
92         }
93 };
94
95 struct ifc_regs ifc_cfg_nand_boot[CONFIG_SYS_FSL_IFC_BANK_COUNT] = {
96         {
97                 "nand",
98                 CONFIG_SYS_NAND_CSPR,
99                 CONFIG_SYS_NAND_CSPR_EXT,
100                 CONFIG_SYS_NAND_AMASK,
101                 CONFIG_SYS_NAND_CSOR,
102                 {
103                         CONFIG_SYS_NAND_FTIM0,
104                         CONFIG_SYS_NAND_FTIM1,
105                         CONFIG_SYS_NAND_FTIM2,
106                         CONFIG_SYS_NAND_FTIM3
107                 },
108         },
109         {
110                 "nor0",
111                 CONFIG_SYS_NOR0_CSPR,
112                 CONFIG_SYS_NOR0_CSPR_EXT,
113                 CONFIG_SYS_NOR_AMASK,
114                 CONFIG_SYS_NOR_CSOR,
115                 {
116                         CONFIG_SYS_NOR_FTIM0,
117                         CONFIG_SYS_NOR_FTIM1,
118                         CONFIG_SYS_NOR_FTIM2,
119                         CONFIG_SYS_NOR_FTIM3
120                 },
121         },
122         {
123                 "nor1",
124                 CONFIG_SYS_NOR1_CSPR,
125                 CONFIG_SYS_NOR1_CSPR_EXT,
126                 CONFIG_SYS_NOR_AMASK,
127                 CONFIG_SYS_NOR_CSOR,
128                 {
129                         CONFIG_SYS_NOR_FTIM0,
130                         CONFIG_SYS_NOR_FTIM1,
131                         CONFIG_SYS_NOR_FTIM2,
132                         CONFIG_SYS_NOR_FTIM3
133                 },
134         },
135         {
136                 "fpga",
137                 CONFIG_SYS_FPGA_CSPR,
138                 CONFIG_SYS_FPGA_CSPR_EXT,
139                 CONFIG_SYS_FPGA_AMASK,
140                 CONFIG_SYS_FPGA_CSOR,
141                 {
142                         CONFIG_SYS_FPGA_FTIM0,
143                         CONFIG_SYS_FPGA_FTIM1,
144                         CONFIG_SYS_FPGA_FTIM2,
145                         CONFIG_SYS_FPGA_FTIM3
146                 },
147         }
148 };
149
150 void ifc_cfg_boot_info(struct ifc_regs_info *regs_info)
151 {
152         enum boot_src src = get_boot_src();
153
154         if (src == BOOT_SOURCE_IFC_NAND)
155                 regs_info->regs = ifc_cfg_nand_boot;
156         else
157                 regs_info->regs = ifc_cfg_nor_boot;
158         regs_info->cs_size = CONFIG_SYS_FSL_IFC_BANK_COUNT;
159 }
160
161 #endif
162
163 enum {
164         MUX_TYPE_GPIO,
165 };
166
167 int checkboard(void)
168 {
169 #ifdef CONFIG_TFABOOT
170         enum boot_src src = get_boot_src();
171 #endif
172         char buf[64];
173 #ifndef CONFIG_SD_BOOT
174         u8 sw;
175 #endif
176
177         puts("Board: LS1046AQDS, boot from ");
178
179 #ifdef CONFIG_TFABOOT
180         if (src == BOOT_SOURCE_SD_MMC)
181                 puts("SD\n");
182         else {
183 #endif
184
185 #ifdef CONFIG_SD_BOOT
186         puts("SD\n");
187 #else
188         sw = QIXIS_READ(brdcfg[0]);
189         sw = (sw & QIXIS_LBMAP_MASK) >> QIXIS_LBMAP_SHIFT;
190
191         if (sw < 0x8)
192                 printf("vBank: %d\n", sw);
193         else if (sw == 0x8)
194                 puts("PromJet\n");
195         else if (sw == 0x9)
196                 puts("NAND\n");
197         else if (sw == 0xF)
198                 printf("QSPI\n");
199         else
200                 printf("invalid setting of SW%u\n", QIXIS_LBMAP_SWITCH);
201 #endif
202
203 #ifdef CONFIG_TFABOOT
204         }
205 #endif
206         printf("Sys ID: 0x%02x, Sys Ver: 0x%02x\n",
207                QIXIS_READ(id), QIXIS_READ(arch));
208
209         printf("FPGA:  v%d (%s), build %d\n",
210                (int)QIXIS_READ(scver), qixis_read_tag(buf),
211                (int)qixis_read_minor());
212
213         return 0;
214 }
215
216 bool if_board_diff_clk(void)
217 {
218         u8 diff_conf = QIXIS_READ(brdcfg[11]);
219
220         return diff_conf & 0x40;
221 }
222
223 unsigned long get_board_sys_clk(void)
224 {
225         u8 sysclk_conf = QIXIS_READ(brdcfg[1]);
226
227         switch (sysclk_conf & 0x0f) {
228         case QIXIS_SYSCLK_64:
229                 return 64000000;
230         case QIXIS_SYSCLK_83:
231                 return 83333333;
232         case QIXIS_SYSCLK_100:
233                 return 100000000;
234         case QIXIS_SYSCLK_125:
235                 return 125000000;
236         case QIXIS_SYSCLK_133:
237                 return 133333333;
238         case QIXIS_SYSCLK_150:
239                 return 150000000;
240         case QIXIS_SYSCLK_160:
241                 return 160000000;
242         case QIXIS_SYSCLK_166:
243                 return 166666666;
244         }
245
246         return 66666666;
247 }
248
249 unsigned long get_board_ddr_clk(void)
250 {
251         u8 ddrclk_conf = QIXIS_READ(brdcfg[1]);
252
253         if (if_board_diff_clk())
254                 return get_board_sys_clk();
255         switch ((ddrclk_conf & 0x30) >> 4) {
256         case QIXIS_DDRCLK_100:
257                 return 100000000;
258         case QIXIS_DDRCLK_125:
259                 return 125000000;
260         case QIXIS_DDRCLK_133:
261                 return 133333333;
262         }
263
264         return 66666666;
265 }
266
267 #ifdef CONFIG_LPUART
268 u32 get_lpuart_clk(void)
269 {
270         return gd->bus_clk;
271 }
272 #endif
273
274 int select_i2c_ch_pca9547(u8 ch, int bus_num)
275 {
276         int ret;
277 #ifdef CONFIG_DM_I2C
278         struct udevice *dev;
279
280         ret = i2c_get_chip_for_busnum(bus_num, I2C_MUX_PCA_ADDR_PRI,
281                                       1, &dev);
282         if (ret) {
283                 printf("%s: Cannot find udev for a bus %d\n", __func__,
284                        bus_num);
285                 return ret;
286         }
287         ret = dm_i2c_write(dev, 0, &ch, 1);
288 #else
289         ret = i2c_write(I2C_MUX_PCA_ADDR_PRI, 0, 1, &ch, 1);
290 #endif
291         if (ret) {
292                 puts("PCA: failed to select proper channel\n");
293                 return ret;
294         }
295
296         return 0;
297 }
298
299 int dram_init(void)
300 {
301         /*
302          * When resuming from deep sleep, the I2C channel may not be
303          * in the default channel. So, switch to the default channel
304          * before accessing DDR SPD.
305          *
306          * PCA9547 mount on I2C1 bus
307          */
308         select_i2c_ch_pca9547(I2C_MUX_CH_DEFAULT, 0);
309         fsl_initdram();
310 #if (!defined(CONFIG_SPL) && !defined(CONFIG_TFABOOT)) || \
311         defined(CONFIG_SPL_BUILD)
312         /* This will break-before-make MMU for DDR */
313         update_early_mmu_table();
314 #endif
315
316         return 0;
317 }
318
319 int i2c_multiplexer_select_vid_channel(u8 channel)
320 {
321         return select_i2c_ch_pca9547(channel, 0);
322 }
323
324 int board_early_init_f(void)
325 {
326 #ifdef CONFIG_HAS_FSL_XHCI_USB
327         struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
328         u32 usb_pwrfault;
329 #endif
330 #ifdef CONFIG_LPUART
331         u8 uart;
332 #endif
333
334 #ifdef CONFIG_SYS_I2C
335 #ifdef CONFIG_SYS_I2C_EARLY_INIT
336         i2c_early_init_f();
337 #endif
338 #endif
339         fsl_lsch2_early_init_f();
340
341 #ifdef CONFIG_HAS_FSL_XHCI_USB
342         out_be32(&scfg->rcwpmuxcr0, 0x3333);
343         out_be32(&scfg->usbdrvvbus_selcr, SCFG_USBDRVVBUS_SELCR_USB1);
344         usb_pwrfault = (SCFG_USBPWRFAULT_DEDICATED <<
345                         SCFG_USBPWRFAULT_USB3_SHIFT) |
346                         (SCFG_USBPWRFAULT_DEDICATED <<
347                         SCFG_USBPWRFAULT_USB2_SHIFT) |
348                         (SCFG_USBPWRFAULT_SHARED <<
349                         SCFG_USBPWRFAULT_USB1_SHIFT);
350         out_be32(&scfg->usbpwrfault_selcr, usb_pwrfault);
351 #endif
352
353 #ifdef CONFIG_LPUART
354         /* We use lpuart0 as system console */
355         uart = QIXIS_READ(brdcfg[14]);
356         uart &= ~CFG_UART_MUX_MASK;
357         uart |= CFG_LPUART_EN << CFG_UART_MUX_SHIFT;
358         QIXIS_WRITE(brdcfg[14], uart);
359 #endif
360
361         return 0;
362 }
363
364 #ifdef CONFIG_FSL_DEEP_SLEEP
365 /* determine if it is a warm boot */
366 bool is_warm_boot(void)
367 {
368 #define DCFG_CCSR_CRSTSR_WDRFR  (1 << 3)
369         struct ccsr_gur __iomem *gur = (void *)CONFIG_SYS_FSL_GUTS_ADDR;
370
371         if (in_be32(&gur->crstsr) & DCFG_CCSR_CRSTSR_WDRFR)
372                 return 1;
373
374         return 0;
375 }
376 #endif
377
378 int config_board_mux(int ctrl_type)
379 {
380         u8 reg14;
381
382         reg14 = QIXIS_READ(brdcfg[14]);
383
384         switch (ctrl_type) {
385         case MUX_TYPE_GPIO:
386                 reg14 = (reg14 & (~0x6)) | 0x2;
387                 break;
388         default:
389                 puts("Unsupported mux interface type\n");
390                 return -1;
391         }
392
393         QIXIS_WRITE(brdcfg[14], reg14);
394
395         return 0;
396 }
397
398 int config_serdes_mux(void)
399 {
400         return 0;
401 }
402
403 #ifdef CONFIG_MISC_INIT_R
404 int misc_init_r(void)
405 {
406         if (hwconfig("gpio"))
407                 config_board_mux(MUX_TYPE_GPIO);
408
409         return 0;
410 }
411 #endif
412
413 int board_init(void)
414 {
415         select_i2c_ch_pca9547(I2C_MUX_CH_DEFAULT, 0);
416
417 #ifdef CONFIG_SYS_FSL_SERDES
418         config_serdes_mux();
419 #endif
420
421         if (adjust_vdd(0))
422                 printf("Warning: Adjusting core voltage failed.\n");
423
424 #ifdef CONFIG_FSL_LS_PPA
425         ppa_init();
426 #endif
427
428 #ifdef CONFIG_NXP_ESBC
429         /*
430          * In case of Secure Boot, the IBR configures the SMMU
431          * to allow only Secure transactions.
432          * SMMU must be reset in bypass mode.
433          * Set the ClientPD bit and Clear the USFCFG Bit
434          */
435         u32 val;
436         val = (in_le32(SMMU_SCR0) | SCR0_CLIENTPD_MASK) & ~(SCR0_USFCFG_MASK);
437         out_le32(SMMU_SCR0, val);
438         val = (in_le32(SMMU_NSCR0) | SCR0_CLIENTPD_MASK) & ~(SCR0_USFCFG_MASK);
439         out_le32(SMMU_NSCR0, val);
440 #endif
441
442 #ifdef CONFIG_FSL_CAAM
443         sec_init();
444 #endif
445
446         return 0;
447 }
448
449 #ifdef CONFIG_OF_BOARD_SETUP
450 int ft_board_setup(void *blob, struct bd_info *bd)
451 {
452         u64 base[CONFIG_NR_DRAM_BANKS];
453         u64 size[CONFIG_NR_DRAM_BANKS];
454         u8 reg;
455
456         /* fixup DT for the two DDR banks */
457         base[0] = gd->bd->bi_dram[0].start;
458         size[0] = gd->bd->bi_dram[0].size;
459         base[1] = gd->bd->bi_dram[1].start;
460         size[1] = gd->bd->bi_dram[1].size;
461
462         fdt_fixup_memory_banks(blob, base, size, 2);
463         ft_cpu_setup(blob, bd);
464
465 #ifdef CONFIG_SYS_DPAA_FMAN
466 #ifndef CONFIG_DM_ETH
467         fdt_fixup_fman_ethernet(blob);
468 #endif
469         fdt_fixup_board_enet(blob);
470 #endif
471
472         fdt_fixup_icid(blob);
473
474         reg = QIXIS_READ(brdcfg[0]);
475         reg = (reg & QIXIS_LBMAP_MASK) >> QIXIS_LBMAP_SHIFT;
476
477         /* Disable IFC if QSPI is enabled */
478         if (reg == 0xF)
479                 do_fixup_by_compat(blob, "fsl,ifc",
480                                    "status", "disabled", 8 + 1, 1);
481
482         return 0;
483 }
484 #endif
485
486 u8 flash_read8(void *addr)
487 {
488         return __raw_readb(addr + 1);
489 }
490
491 void flash_write16(u16 val, void *addr)
492 {
493         u16 shftval = (((val >> 8) & 0xff) | ((val << 8) & 0xff00));
494
495         __raw_writew(shftval, addr);
496 }
497
498 u16 flash_read16(void *addr)
499 {
500         u16 val = __raw_readw(addr);
501
502         return (((val) >> 8) & 0x00ff) | (((val) << 8) & 0xff00);
503 }
504
505 #if defined(CONFIG_TFABOOT) && defined(CONFIG_ENV_IS_IN_SPI_FLASH)
506 void *env_sf_get_env_addr(void)
507 {
508         return (void *)(CONFIG_SYS_FSL_QSPI_BASE + CONFIG_ENV_OFFSET);
509 }
510 #endif