1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * (C) Copyright 2012 Michal Simek <monstr@monstr.eu> 4 * (C) Copyright 2013 - 2018 Xilinx, Inc. 5 */ 6 7 #include <common.h> 8 #include <dm/uclass.h> 9 #include <fdtdec.h> 10 #include <fpga.h> 11 #include <malloc.h> 12 #include <mmc.h> 13 #include <watchdog.h> 14 #include <wdt.h> 15 #include <zynqpl.h> 16 #include <asm/arch/hardware.h> 17 #include <asm/arch/sys_proto.h> 18 19 DECLARE_GLOBAL_DATA_PTR; 20 21 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_WDT) 22 static struct udevice *watchdog_dev __attribute__((section(".data"))) = NULL; 23 #endif 24 25 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_BOARD_EARLY_INIT_F) 26 int board_early_init_f(void) 27 { 28 return 0; 29 } 30 #endif 31 32 int board_init(void) 33 { 34 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_WDT) 35 if (uclass_get_device_by_seq(UCLASS_WDT, 0, &watchdog_dev)) { 36 debug("Watchdog: Not found by seq!\n"); 37 if (uclass_get_device(UCLASS_WDT, 0, &watchdog_dev)) { 38 puts("Watchdog: Not found!\n"); 39 return 0; 40 } 41 } 42 43 wdt_start(watchdog_dev, 0, 0); 44 puts("Watchdog: Started\n"); 45 # endif 46 47 return 0; 48 } 49 50 int board_late_init(void) 51 { 52 int env_targets_len = 0; 53 const char *mode; 54 char *new_targets; 55 char *env_targets; 56 57 switch ((zynq_slcr_get_boot_mode()) & ZYNQ_BM_MASK) { 58 case ZYNQ_BM_QSPI: 59 mode = "qspi"; 60 env_set("modeboot", "qspiboot"); 61 break; 62 case ZYNQ_BM_NAND: 63 mode = "nand"; 64 env_set("modeboot", "nandboot"); 65 break; 66 case ZYNQ_BM_NOR: 67 mode = "nor"; 68 env_set("modeboot", "norboot"); 69 break; 70 case ZYNQ_BM_SD: 71 mode = "mmc"; 72 env_set("modeboot", "sdboot"); 73 break; 74 case ZYNQ_BM_JTAG: 75 mode = "pxe dhcp"; 76 env_set("modeboot", "jtagboot"); 77 break; 78 default: 79 mode = ""; 80 env_set("modeboot", ""); 81 break; 82 } 83 84 /* 85 * One terminating char + one byte for space between mode 86 * and default boot_targets 87 */ 88 env_targets = env_get("boot_targets"); 89 if (env_targets) 90 env_targets_len = strlen(env_targets); 91 92 new_targets = calloc(1, strlen(mode) + env_targets_len + 2); 93 if (!new_targets) 94 return -ENOMEM; 95 96 sprintf(new_targets, "%s %s", mode, 97 env_targets ? env_targets : ""); 98 99 env_set("boot_targets", new_targets); 100 101 return 0; 102 } 103 104 #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE) 105 int dram_init_banksize(void) 106 { 107 return fdtdec_setup_memory_banksize(); 108 } 109 110 int dram_init(void) 111 { 112 if (fdtdec_setup_mem_size_base() != 0) 113 return -EINVAL; 114 115 zynq_ddrc_init(); 116 117 return 0; 118 } 119 #else 120 int dram_init(void) 121 { 122 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE, 123 CONFIG_SYS_SDRAM_SIZE); 124 125 zynq_ddrc_init(); 126 127 return 0; 128 } 129 #endif 130 131 #if defined(CONFIG_WATCHDOG) 132 /* Called by macro WATCHDOG_RESET */ 133 void watchdog_reset(void) 134 { 135 # if !defined(CONFIG_SPL_BUILD) 136 static ulong next_reset; 137 ulong now; 138 139 if (!watchdog_dev) 140 return; 141 142 now = timer_get_us(); 143 144 /* Do not reset the watchdog too often */ 145 if (now > next_reset) { 146 wdt_reset(watchdog_dev); 147 next_reset = now + 1000; 148 } 149 # endif 150 } 151 #endif 152