xilinx: Show silicon version in SPL
[platform/kernel/u-boot.git] / board / xilinx / zynqmp / zynqmp.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2014 - 2015 Xilinx, Inc.
4  * Michal Simek <michal.simek@xilinx.com>
5  */
6
7 #include <common.h>
8 #include <command.h>
9 #include <cpu_func.h>
10 #include <debug_uart.h>
11 #include <env.h>
12 #include <env_internal.h>
13 #include <init.h>
14 #include <log.h>
15 #include <net.h>
16 #include <sata.h>
17 #include <ahci.h>
18 #include <scsi.h>
19 #include <malloc.h>
20 #include <wdt.h>
21 #include <asm/arch/clk.h>
22 #include <asm/arch/hardware.h>
23 #include <asm/arch/sys_proto.h>
24 #include <asm/arch/psu_init_gpl.h>
25 #include <asm/cache.h>
26 #include <asm/io.h>
27 #include <asm/ptrace.h>
28 #include <dm/device.h>
29 #include <dm/uclass.h>
30 #include <usb.h>
31 #include <dwc3-uboot.h>
32 #include <zynqmppl.h>
33 #include <zynqmp_firmware.h>
34 #include <g_dnl.h>
35 #include <linux/bitops.h>
36 #include <linux/delay.h>
37 #include <linux/sizes.h>
38 #include "../common/board.h"
39
40 #include "pm_cfg_obj.h"
41
42 #define ZYNQMP_VERSION_SIZE     7
43 #define EFUSE_VCU_DIS_MASK      0x100
44 #define EFUSE_VCU_DIS_SHIFT     8
45 #define EFUSE_GPU_DIS_MASK      0x20
46 #define EFUSE_GPU_DIS_SHIFT     5
47 #define IDCODE2_PL_INIT_MASK    0x200
48 #define IDCODE2_PL_INIT_SHIFT   9
49
50 DECLARE_GLOBAL_DATA_PTR;
51
52 #if CONFIG_IS_ENABLED(FPGA) && defined(CONFIG_FPGA_ZYNQMPPL)
53 static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
54
55 enum {
56         ZYNQMP_VARIANT_EG = BIT(0U),
57         ZYNQMP_VARIANT_EV = BIT(1U),
58         ZYNQMP_VARIANT_CG = BIT(2U),
59         ZYNQMP_VARIANT_DR = BIT(3U),
60 };
61
62 static const struct {
63         u32 id;
64         u8 device;
65         u8 variants;
66 } zynqmp_devices[] = {
67         {
68                 .id = 0x04711093,
69                 .device = 2,
70                 .variants = ZYNQMP_VARIANT_EG | ZYNQMP_VARIANT_CG,
71         },
72         {
73                 .id = 0x04710093,
74                 .device = 3,
75                 .variants = ZYNQMP_VARIANT_EG | ZYNQMP_VARIANT_CG,
76         },
77         {
78                 .id = 0x04721093,
79                 .device = 4,
80                 .variants = ZYNQMP_VARIANT_EG | ZYNQMP_VARIANT_CG |
81                         ZYNQMP_VARIANT_EV,
82         },
83         {
84                 .id = 0x04720093,
85                 .device = 5,
86                 .variants = ZYNQMP_VARIANT_EG | ZYNQMP_VARIANT_CG |
87                         ZYNQMP_VARIANT_EV,
88         },
89         {
90                 .id = 0x04739093,
91                 .device = 6,
92                 .variants = ZYNQMP_VARIANT_EG | ZYNQMP_VARIANT_CG,
93         },
94         {
95                 .id = 0x04730093,
96                 .device = 7,
97                 .variants = ZYNQMP_VARIANT_EG | ZYNQMP_VARIANT_CG |
98                         ZYNQMP_VARIANT_EV,
99         },
100         {
101                 .id = 0x04738093,
102                 .device = 9,
103                 .variants = ZYNQMP_VARIANT_EG | ZYNQMP_VARIANT_CG,
104         },
105         {
106                 .id = 0x04740093,
107                 .device = 11,
108                 .variants = ZYNQMP_VARIANT_EG,
109         },
110         {
111                 .id = 0x04750093,
112                 .device = 15,
113                 .variants = ZYNQMP_VARIANT_EG,
114         },
115         {
116                 .id = 0x04759093,
117                 .device = 17,
118                 .variants = ZYNQMP_VARIANT_EG,
119         },
120         {
121                 .id = 0x04758093,
122                 .device = 19,
123                 .variants = ZYNQMP_VARIANT_EG,
124         },
125         {
126                 .id = 0x047E1093,
127                 .device = 21,
128                 .variants = ZYNQMP_VARIANT_DR,
129         },
130         {
131                 .id = 0x047E3093,
132                 .device = 23,
133                 .variants = ZYNQMP_VARIANT_DR,
134         },
135         {
136                 .id = 0x047E5093,
137                 .device = 25,
138                 .variants = ZYNQMP_VARIANT_DR,
139         },
140         {
141                 .id = 0x047E4093,
142                 .device = 27,
143                 .variants = ZYNQMP_VARIANT_DR,
144         },
145         {
146                 .id = 0x047E0093,
147                 .device = 28,
148                 .variants = ZYNQMP_VARIANT_DR,
149         },
150         {
151                 .id = 0x047E2093,
152                 .device = 29,
153                 .variants = ZYNQMP_VARIANT_DR,
154         },
155         {
156                 .id = 0x047E6093,
157                 .device = 39,
158                 .variants = ZYNQMP_VARIANT_DR,
159         },
160         {
161                 .id = 0x047FD093,
162                 .device = 43,
163                 .variants = ZYNQMP_VARIANT_DR,
164         },
165         {
166                 .id = 0x047F8093,
167                 .device = 46,
168                 .variants = ZYNQMP_VARIANT_DR,
169         },
170         {
171                 .id = 0x047FF093,
172                 .device = 47,
173                 .variants = ZYNQMP_VARIANT_DR,
174         },
175         {
176                 .id = 0x047FB093,
177                 .device = 48,
178                 .variants = ZYNQMP_VARIANT_DR,
179         },
180         {
181                 .id = 0x047FE093,
182                 .device = 49,
183                 .variants = ZYNQMP_VARIANT_DR,
184         },
185 };
186
187 static char *zynqmp_get_silicon_idcode_name(void)
188 {
189         u32 i;
190         u32 idcode, idcode2;
191         char name[ZYNQMP_VERSION_SIZE];
192         u32 ret_payload[PAYLOAD_ARG_CNT];
193         int ret;
194
195         ret = xilinx_pm_request(PM_GET_CHIPID, 0, 0, 0, 0, ret_payload);
196         if (ret) {
197                 debug("%s: Getting chipid failed\n", __func__);
198                 return "unknown";
199         }
200
201         /*
202          * Firmware returns:
203          * payload[0][31:0]  = status of the operation
204          * payload[1]] = IDCODE
205          * payload[2][19:0]  = Version
206          * payload[2][28:20] = EXTENDED_IDCODE
207          * payload[2][29] = PL_INIT
208          */
209
210         idcode  = ret_payload[1];
211         idcode2 = ret_payload[2] >> ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
212         debug("%s, IDCODE: 0x%0x, IDCODE2: 0x%0x\r\n", __func__, idcode,
213               idcode2);
214
215         for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
216                 if (zynqmp_devices[i].id == (idcode & 0x0FFFFFFF))
217                         break;
218         }
219
220         if (i >= ARRAY_SIZE(zynqmp_devices))
221                 return "unknown";
222
223         /* Add device prefix to the name */
224         ret = snprintf(name, ZYNQMP_VERSION_SIZE, "zu%d",
225                        zynqmp_devices[i].device);
226         if (ret < 0)
227                 return "unknown";
228
229         if (zynqmp_devices[i].variants & ZYNQMP_VARIANT_EV) {
230                 /* Devices with EV variant might be EG/CG/EV family */
231                 if (idcode2 & IDCODE2_PL_INIT_MASK) {
232                         u32 family = ((idcode2 & EFUSE_VCU_DIS_MASK) >>
233                                       EFUSE_VCU_DIS_SHIFT) << 1 |
234                                      ((idcode2 & EFUSE_GPU_DIS_MASK) >>
235                                       EFUSE_GPU_DIS_SHIFT);
236
237                         /*
238                          * Get family name based on extended idcode values as
239                          * determined on UG1087, EXTENDED_IDCODE register
240                          * description
241                          */
242                         switch (family) {
243                         case 0x00:
244                                 strncat(name, "ev", 2);
245                                 break;
246                         case 0x10:
247                                 strncat(name, "eg", 2);
248                                 break;
249                         case 0x11:
250                                 strncat(name, "cg", 2);
251                                 break;
252                         default:
253                                 /* Do not append family name*/
254                                 break;
255                         }
256                 } else {
257                         /*
258                          * When PL powered down the VCU Disable efuse cannot be
259                          * read. So, ignore the bit and just findout if it is CG
260                          * or EG/EV variant.
261                          */
262                         strncat(name, (idcode2 & EFUSE_GPU_DIS_MASK) ? "cg" :
263                                 "e", 2);
264                 }
265         } else if (zynqmp_devices[i].variants & ZYNQMP_VARIANT_CG) {
266                 /* Devices with CG variant might be EG or CG family */
267                 strncat(name, (idcode2 & EFUSE_GPU_DIS_MASK) ? "cg" : "eg", 2);
268         } else if (zynqmp_devices[i].variants & ZYNQMP_VARIANT_EG) {
269                 strncat(name, "eg", 2);
270         } else if (zynqmp_devices[i].variants & ZYNQMP_VARIANT_DR) {
271                 strncat(name, "dr", 2);
272         } else {
273                 debug("Variant not identified\n");
274         }
275
276         return strdup(name);
277 }
278 #endif
279
280 int board_early_init_f(void)
281 {
282 #if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
283         int ret;
284
285         ret = psu_init();
286         if (ret)
287                 return ret;
288
289         /* Delay is required for clocks to be propagated */
290         udelay(1000000);
291 #endif
292
293 #ifdef CONFIG_DEBUG_UART
294         /* Uart debug for sure */
295         debug_uart_init();
296         puts("Debug uart enabled\n"); /* or printch() */
297 #endif
298
299         return 0;
300 }
301
302 static int multi_boot(void)
303 {
304         u32 multiboot;
305
306         multiboot = readl(&csu_base->multi_boot);
307
308         printf("Multiboot:\t%d\n", multiboot);
309
310         return 0;
311 }
312
313 #define PS_SYSMON_ANALOG_BUS_VAL        0x3210
314 #define PS_SYSMON_ANALOG_BUS_REG        0xFFA50914
315
316 int board_init(void)
317 {
318 #if defined(CONFIG_ZYNQMP_FIRMWARE)
319         struct udevice *dev;
320
321         uclass_get_device_by_name(UCLASS_FIRMWARE, "zynqmp-power", &dev);
322         if (!dev)
323                 panic("PMU Firmware device not found - Enable it");
324 #endif
325
326 #if defined(CONFIG_SPL_BUILD)
327         /* Check *at build time* if the filename is an non-empty string */
328         if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
329                 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
330                                                 zynqmp_pm_cfg_obj_size);
331         printf("Silicon version:\t%d\n", zynqmp_get_silicon_version());
332 #else
333         if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM))
334                 xilinx_read_eeprom();
335 #endif
336
337         printf("EL Level:\tEL%d\n", current_el());
338
339         /* Bug in ROM sets wrong value in this register */
340         writel(PS_SYSMON_ANALOG_BUS_VAL, PS_SYSMON_ANALOG_BUS_REG);
341
342 #if CONFIG_IS_ENABLED(FPGA) && defined(CONFIG_FPGA_ZYNQMPPL)
343         zynqmppl.name = zynqmp_get_silicon_idcode_name();
344         printf("Chip ID:\t%s\n", zynqmppl.name);
345         fpga_init();
346         fpga_add(fpga_xilinx, &zynqmppl);
347 #endif
348
349         if (current_el() == 3)
350                 multi_boot();
351
352         return 0;
353 }
354
355 int board_early_init_r(void)
356 {
357         u32 val;
358
359         if (current_el() != 3)
360                 return 0;
361
362         val = readl(&crlapb_base->timestamp_ref_ctrl);
363         val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
364
365         if (!val) {
366                 val = readl(&crlapb_base->timestamp_ref_ctrl);
367                 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
368                 writel(val, &crlapb_base->timestamp_ref_ctrl);
369
370                 /* Program freq register in System counter */
371                 writel(zynqmp_get_system_timer_freq(),
372                        &iou_scntr_secure->base_frequency_id_register);
373                 /* And enable system counter */
374                 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
375                        &iou_scntr_secure->counter_control_register);
376         }
377         return 0;
378 }
379
380 unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
381                          char *const argv[])
382 {
383         int ret = 0;
384
385         if (current_el() > 1) {
386                 smp_kick_all_cpus();
387                 dcache_disable();
388                 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
389                                     ES_TO_AARCH64);
390         } else {
391                 printf("FAIL: current EL is not above EL1\n");
392                 ret = EINVAL;
393         }
394         return ret;
395 }
396
397 #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
398 int dram_init_banksize(void)
399 {
400         int ret;
401
402         ret = fdtdec_setup_memory_banksize();
403         if (ret)
404                 return ret;
405
406         mem_map_fill();
407
408         return 0;
409 }
410
411 int dram_init(void)
412 {
413         if (fdtdec_setup_mem_size_base() != 0)
414                 return -EINVAL;
415
416         return 0;
417 }
418 #else
419 int dram_init_banksize(void)
420 {
421         gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
422         gd->bd->bi_dram[0].size = get_effective_memsize();
423
424         mem_map_fill();
425
426         return 0;
427 }
428
429 int dram_init(void)
430 {
431         gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
432                                     CONFIG_SYS_SDRAM_SIZE);
433
434         return 0;
435 }
436 #endif
437
438 void reset_cpu(ulong addr)
439 {
440 }
441
442 static u8 __maybe_unused zynqmp_get_bootmode(void)
443 {
444         u8 bootmode;
445         u32 reg = 0;
446         int ret;
447
448         ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
449         if (ret)
450                 return -EINVAL;
451
452         if (reg >> BOOT_MODE_ALT_SHIFT)
453                 reg >>= BOOT_MODE_ALT_SHIFT;
454
455         bootmode = reg & BOOT_MODES_MASK;
456
457         return bootmode;
458 }
459
460 #if defined(CONFIG_BOARD_LATE_INIT)
461 static const struct {
462         u32 bit;
463         const char *name;
464 } reset_reasons[] = {
465         { RESET_REASON_DEBUG_SYS, "DEBUG" },
466         { RESET_REASON_SOFT, "SOFT" },
467         { RESET_REASON_SRST, "SRST" },
468         { RESET_REASON_PSONLY, "PS-ONLY" },
469         { RESET_REASON_PMU, "PMU" },
470         { RESET_REASON_INTERNAL, "INTERNAL" },
471         { RESET_REASON_EXTERNAL, "EXTERNAL" },
472         {}
473 };
474
475 static int reset_reason(void)
476 {
477         u32 reg;
478         int i, ret;
479         const char *reason = NULL;
480
481         ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, &reg);
482         if (ret)
483                 return -EINVAL;
484
485         puts("Reset reason:\t");
486
487         for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
488                 if (reg & reset_reasons[i].bit) {
489                         reason = reset_reasons[i].name;
490                         printf("%s ", reset_reasons[i].name);
491                         break;
492                 }
493         }
494
495         puts("\n");
496
497         env_set("reset_reason", reason);
498
499         ret = zynqmp_mmio_write((ulong)&crlapb_base->reset_reason, ~0, ~0);
500         if (ret)
501                 return -EINVAL;
502
503         return ret;
504 }
505
506 static int set_fdtfile(void)
507 {
508         char *compatible, *fdtfile;
509         const char *suffix = ".dtb";
510         const char *vendor = "xilinx/";
511         int fdt_compat_len;
512
513         if (env_get("fdtfile"))
514                 return 0;
515
516         compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible",
517                                          &fdt_compat_len);
518         if (compatible && fdt_compat_len) {
519                 char *name;
520
521                 debug("Compatible: %s\n", compatible);
522
523                 name = strchr(compatible, ',');
524                 if (!name)
525                         return -EINVAL;
526
527                 name++;
528
529                 fdtfile = calloc(1, strlen(vendor) + strlen(name) +
530                                  strlen(suffix) + 1);
531                 if (!fdtfile)
532                         return -ENOMEM;
533
534                 sprintf(fdtfile, "%s%s%s", vendor, name, suffix);
535
536                 env_set("fdtfile", fdtfile);
537                 free(fdtfile);
538         }
539
540         return 0;
541 }
542
543 int board_late_init(void)
544 {
545         u8 bootmode;
546         struct udevice *dev;
547         int bootseq = -1;
548         int bootseq_len = 0;
549         int env_targets_len = 0;
550         const char *mode;
551         char *new_targets;
552         char *env_targets;
553         int ret;
554
555 #if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
556         usb_ether_init();
557 #endif
558
559         if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
560                 debug("Saved variables - Skipping\n");
561                 return 0;
562         }
563
564         if (!CONFIG_IS_ENABLED(ENV_VARS_UBOOT_RUNTIME_CONFIG))
565                 return 0;
566
567         ret = set_fdtfile();
568         if (ret)
569                 return ret;
570
571         bootmode = zynqmp_get_bootmode();
572
573         puts("Bootmode: ");
574         switch (bootmode) {
575         case USB_MODE:
576                 puts("USB_MODE\n");
577                 mode = "usb";
578                 env_set("modeboot", "usb_dfu_spl");
579                 break;
580         case JTAG_MODE:
581                 puts("JTAG_MODE\n");
582                 mode = "jtag pxe dhcp";
583                 env_set("modeboot", "jtagboot");
584                 break;
585         case QSPI_MODE_24BIT:
586         case QSPI_MODE_32BIT:
587                 mode = "qspi0";
588                 puts("QSPI_MODE\n");
589                 env_set("modeboot", "qspiboot");
590                 break;
591         case EMMC_MODE:
592                 puts("EMMC_MODE\n");
593                 if (uclass_get_device_by_name(UCLASS_MMC,
594                                               "mmc@ff160000", &dev) &&
595                     uclass_get_device_by_name(UCLASS_MMC,
596                                               "sdhci@ff160000", &dev)) {
597                         puts("Boot from EMMC but without SD0 enabled!\n");
598                         return -1;
599                 }
600                 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
601
602                 mode = "mmc";
603                 bootseq = dev_seq(dev);
604                 break;
605         case SD_MODE:
606                 puts("SD_MODE\n");
607                 if (uclass_get_device_by_name(UCLASS_MMC,
608                                               "mmc@ff160000", &dev) &&
609                     uclass_get_device_by_name(UCLASS_MMC,
610                                               "sdhci@ff160000", &dev)) {
611                         puts("Boot from SD0 but without SD0 enabled!\n");
612                         return -1;
613                 }
614                 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
615
616                 mode = "mmc";
617                 bootseq = dev_seq(dev);
618                 env_set("modeboot", "sdboot");
619                 break;
620         case SD1_LSHFT_MODE:
621                 puts("LVL_SHFT_");
622                 /* fall through */
623         case SD_MODE1:
624                 puts("SD_MODE1\n");
625                 if (uclass_get_device_by_name(UCLASS_MMC,
626                                               "mmc@ff170000", &dev) &&
627                     uclass_get_device_by_name(UCLASS_MMC,
628                                               "sdhci@ff170000", &dev)) {
629                         puts("Boot from SD1 but without SD1 enabled!\n");
630                         return -1;
631                 }
632                 debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev));
633
634                 mode = "mmc";
635                 bootseq = dev_seq(dev);
636                 env_set("modeboot", "sdboot");
637                 break;
638         case NAND_MODE:
639                 puts("NAND_MODE\n");
640                 mode = "nand0";
641                 env_set("modeboot", "nandboot");
642                 break;
643         default:
644                 mode = "";
645                 printf("Invalid Boot Mode:0x%x\n", bootmode);
646                 break;
647         }
648
649         if (bootseq >= 0) {
650                 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
651                 debug("Bootseq len: %x\n", bootseq_len);
652                 env_set_hex("bootseq", bootseq);
653         }
654
655         /*
656          * One terminating char + one byte for space between mode
657          * and default boot_targets
658          */
659         env_targets = env_get("boot_targets");
660         if (env_targets)
661                 env_targets_len = strlen(env_targets);
662
663         new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
664                              bootseq_len);
665         if (!new_targets)
666                 return -ENOMEM;
667
668         if (bootseq >= 0)
669                 sprintf(new_targets, "%s%x %s", mode, bootseq,
670                         env_targets ? env_targets : "");
671         else
672                 sprintf(new_targets, "%s %s", mode,
673                         env_targets ? env_targets : "");
674
675         env_set("boot_targets", new_targets);
676
677         reset_reason();
678
679         return board_late_init_xilinx();
680 }
681 #endif
682
683 int checkboard(void)
684 {
685         puts("Board: Xilinx ZynqMP\n");
686         return 0;
687 }
688
689 enum env_location env_get_location(enum env_operation op, int prio)
690 {
691         u32 bootmode = zynqmp_get_bootmode();
692
693         if (prio)
694                 return ENVL_UNKNOWN;
695
696         switch (bootmode) {
697         case EMMC_MODE:
698         case SD_MODE:
699         case SD1_LSHFT_MODE:
700         case SD_MODE1:
701                 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
702                         return ENVL_FAT;
703                 if (IS_ENABLED(CONFIG_ENV_IS_IN_EXT4))
704                         return ENVL_EXT4;
705                 return ENVL_UNKNOWN;
706         case NAND_MODE:
707                 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
708                         return ENVL_NAND;
709                 if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
710                         return ENVL_UBI;
711                 return ENVL_UNKNOWN;
712         case QSPI_MODE_24BIT:
713         case QSPI_MODE_32BIT:
714                 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
715                         return ENVL_SPI_FLASH;
716                 return ENVL_UNKNOWN;
717         case JTAG_MODE:
718         default:
719                 return ENVL_NOWHERE;
720         }
721 }