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; 23 #endif 24 25 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_BOARD_EARLY_INIT_F) 26 int board_early_init_f(void) 27 { 28 # if defined(CONFIG_WDT) 29 /* bss is not cleared at time when watchdog_reset() is called */ 30 watchdog_dev = NULL; 31 # endif 32 33 return 0; 34 } 35 #endif 36 37 int board_init(void) 38 { 39 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_WDT) 40 if (uclass_get_device_by_seq(UCLASS_WDT, 0, &watchdog_dev)) { 41 debug("Watchdog: Not found by seq!\n"); 42 if (uclass_get_device(UCLASS_WDT, 0, &watchdog_dev)) { 43 puts("Watchdog: Not found!\n"); 44 return 0; 45 } 46 } 47 48 wdt_start(watchdog_dev, 0, 0); 49 puts("Watchdog: Started\n"); 50 # endif 51 52 return 0; 53 } 54 55 int board_late_init(void) 56 { 57 int env_targets_len = 0; 58 const char *mode; 59 char *new_targets; 60 char *env_targets; 61 62 switch ((zynq_slcr_get_boot_mode()) & ZYNQ_BM_MASK) { 63 case ZYNQ_BM_QSPI: 64 mode = "qspi"; 65 env_set("modeboot", "qspiboot"); 66 break; 67 case ZYNQ_BM_NAND: 68 mode = "nand"; 69 env_set("modeboot", "nandboot"); 70 break; 71 case ZYNQ_BM_NOR: 72 mode = "nor"; 73 env_set("modeboot", "norboot"); 74 break; 75 case ZYNQ_BM_SD: 76 mode = "mmc"; 77 env_set("modeboot", "sdboot"); 78 break; 79 case ZYNQ_BM_JTAG: 80 mode = "pxe dhcp"; 81 env_set("modeboot", "jtagboot"); 82 break; 83 default: 84 mode = ""; 85 env_set("modeboot", ""); 86 break; 87 } 88 89 /* 90 * One terminating char + one byte for space between mode 91 * and default boot_targets 92 */ 93 env_targets = env_get("boot_targets"); 94 if (env_targets) 95 env_targets_len = strlen(env_targets); 96 97 new_targets = calloc(1, strlen(mode) + env_targets_len + 2); 98 if (!new_targets) 99 return -ENOMEM; 100 101 sprintf(new_targets, "%s %s", mode, 102 env_targets ? env_targets : ""); 103 104 env_set("boot_targets", new_targets); 105 106 return 0; 107 } 108 109 #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE) 110 int dram_init_banksize(void) 111 { 112 return fdtdec_setup_memory_banksize(); 113 } 114 115 int dram_init(void) 116 { 117 if (fdtdec_setup_mem_size_base() != 0) 118 return -EINVAL; 119 120 zynq_ddrc_init(); 121 122 return 0; 123 } 124 #else 125 int dram_init(void) 126 { 127 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE, 128 CONFIG_SYS_SDRAM_SIZE); 129 130 zynq_ddrc_init(); 131 132 return 0; 133 } 134 #endif 135 136 #if defined(CONFIG_WATCHDOG) 137 /* Called by macro WATCHDOG_RESET */ 138 void watchdog_reset(void) 139 { 140 # if !defined(CONFIG_SPL_BUILD) 141 static ulong next_reset; 142 ulong now; 143 144 if (!watchdog_dev) 145 return; 146 147 now = timer_get_us(); 148 149 /* Do not reset the watchdog too often */ 150 if (now > next_reset) { 151 wdt_reset(watchdog_dev); 152 next_reset = now + 1000; 153 } 154 # endif 155 } 156 #endif 157