1 /* 2 * Copyright (c) 2019-2022, STMicroelectronics - All Rights Reserved 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <common/debug.h> 8 #include <drivers/clk.h> 9 #include <drivers/delay_timer.h> 10 #include <drivers/st/stpmic1.h> 11 #include <lib/mmio.h> 12 13 #include <platform_def.h> 14 #include <stm32mp_common.h> 15 #include <stm32mp_dt.h> 16 #include <stm32mp1_private.h> 17 18 /* 19 * SYSCFG REGISTER OFFSET (base relative) 20 */ 21 #define SYSCFG_BOOTR 0x00U 22 #define SYSCFG_IOCTRLSETR 0x18U 23 #define SYSCFG_ICNR 0x1CU 24 #define SYSCFG_CMPCR 0x20U 25 #define SYSCFG_CMPENSETR 0x24U 26 #define SYSCFG_CMPENCLRR 0x28U 27 28 #define CMPCR_CMPENSETR_OFFSET 0x4U 29 #define CMPCR_CMPENCLRR_OFFSET 0x8U 30 31 /* 32 * SYSCFG_BOOTR Register 33 */ 34 #define SYSCFG_BOOTR_BOOT_MASK GENMASK(2, 0) 35 #define SYSCFG_BOOTR_BOOTPD_MASK GENMASK(6, 4) 36 #define SYSCFG_BOOTR_BOOTPD_SHIFT 4 37 /* 38 * SYSCFG_IOCTRLSETR Register 39 */ 40 #define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0) 41 #define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1) 42 #define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2) 43 #define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3) 44 #define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4) 45 46 /* 47 * SYSCFG_ICNR Register 48 */ 49 #define SYSCFG_ICNR_AXI_M9 BIT(9) 50 51 /* 52 * SYSCFG_CMPCR Register 53 */ 54 #define SYSCFG_CMPCR_SW_CTRL BIT(1) 55 #define SYSCFG_CMPCR_READY BIT(8) 56 #define SYSCFG_CMPCR_RANSRC GENMASK(19, 16) 57 #define SYSCFG_CMPCR_RANSRC_SHIFT 16 58 #define SYSCFG_CMPCR_RAPSRC GENMASK(23, 20) 59 #define SYSCFG_CMPCR_ANSRC_SHIFT 24 60 61 #define SYSCFG_CMPCR_READY_TIMEOUT_US 10000U 62 63 /* 64 * SYSCFG_CMPENSETR Register 65 */ 66 #define SYSCFG_CMPENSETR_MPU_EN BIT(0) 67 68 static void enable_io_comp_cell_finish(uintptr_t cmpcr_off) 69 { 70 uint64_t start; 71 72 start = timeout_init_us(SYSCFG_CMPCR_READY_TIMEOUT_US); 73 74 while ((mmio_read_32(SYSCFG_BASE + cmpcr_off) & SYSCFG_CMPCR_READY) == 0U) { 75 if (timeout_elapsed(start)) { 76 /* Failure on IO compensation enable is not a issue: warn only. */ 77 WARN("IO compensation cell not ready\n"); 78 break; 79 } 80 } 81 82 mmio_clrbits_32(SYSCFG_BASE + cmpcr_off, SYSCFG_CMPCR_SW_CTRL); 83 } 84 85 static void disable_io_comp_cell(uintptr_t cmpcr_off) 86 { 87 uint32_t value; 88 89 if (((mmio_read_32(SYSCFG_BASE + cmpcr_off) & SYSCFG_CMPCR_READY) == 0U) || 90 ((mmio_read_32(SYSCFG_BASE + cmpcr_off + CMPCR_CMPENSETR_OFFSET) & 91 SYSCFG_CMPENSETR_MPU_EN) == 0U)) { 92 return; 93 } 94 95 value = mmio_read_32(SYSCFG_BASE + cmpcr_off) >> SYSCFG_CMPCR_ANSRC_SHIFT; 96 97 mmio_clrbits_32(SYSCFG_BASE + cmpcr_off, SYSCFG_CMPCR_RANSRC | SYSCFG_CMPCR_RAPSRC); 98 99 value <<= SYSCFG_CMPCR_RANSRC_SHIFT; 100 value |= mmio_read_32(SYSCFG_BASE + cmpcr_off); 101 102 mmio_write_32(SYSCFG_BASE + cmpcr_off, value | SYSCFG_CMPCR_SW_CTRL); 103 104 mmio_setbits_32(SYSCFG_BASE + cmpcr_off + CMPCR_CMPENCLRR_OFFSET, SYSCFG_CMPENSETR_MPU_EN); 105 } 106 107 static void enable_high_speed_mode_low_voltage(void) 108 { 109 mmio_write_32(SYSCFG_BASE + SYSCFG_IOCTRLSETR, 110 SYSCFG_IOCTRLSETR_HSLVEN_TRACE | 111 SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI | 112 SYSCFG_IOCTRLSETR_HSLVEN_ETH | 113 SYSCFG_IOCTRLSETR_HSLVEN_SDMMC | 114 SYSCFG_IOCTRLSETR_HSLVEN_SPI); 115 } 116 117 static void stm32mp1_syscfg_set_hslv(void) 118 { 119 uint32_t otp_value; 120 uint32_t vdd_voltage; 121 bool product_below_2v5; 122 123 /* 124 * High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI 125 * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection. 126 * It could be disabled for low frequencies or if AFMUX is selected 127 * but the function is not used, typically for TRACE. 128 * If high speed low voltage pad mode is node enable, platform will 129 * over consume. 130 * 131 * WARNING: 132 * Enabling High Speed mode while VDD > 2.7V 133 * with the OTP product_below_2v5 (OTP 18, BIT 13) 134 * erroneously set to 1 can damage the SoC! 135 * => TF-A enables the low power mode only if VDD < 2.7V (in DT) 136 * but this value needs to be consistent with board design. 137 */ 138 if (stm32_get_otp_value(HW2_OTP, &otp_value) != 0) { 139 panic(); 140 } 141 142 product_below_2v5 = (otp_value & HW2_OTP_PRODUCT_BELOW_2V5) != 0U; 143 144 /* Get VDD supply */ 145 vdd_voltage = dt_get_pwr_vdd_voltage(); 146 147 /* Check if VDD is Low Voltage */ 148 if (vdd_voltage == 0U) { 149 WARN("VDD unknown\n"); 150 } else if (vdd_voltage < 2700000U) { 151 enable_high_speed_mode_low_voltage(); 152 153 if (!product_below_2v5) { 154 INFO("Product_below_2v5=0: HSLVEN protected by HW\n"); 155 } 156 } else { 157 if (product_below_2v5) { 158 ERROR("Product_below_2v5=1:\n"); 159 ERROR("\tHSLVEN update is destructive,\n"); 160 ERROR("\tno update as VDD > 2.7V\n"); 161 panic(); 162 } 163 } 164 } 165 166 void stm32mp1_syscfg_init(void) 167 { 168 uint32_t bootr; 169 170 /* 171 * Interconnect update : select master using the port 1. 172 * LTDC = AXI_M9. 173 */ 174 mmio_write_32(SYSCFG_BASE + SYSCFG_ICNR, SYSCFG_ICNR_AXI_M9); 175 176 /* Disable Pull-Down for boot pin connected to VDD */ 177 bootr = mmio_read_32(SYSCFG_BASE + SYSCFG_BOOTR) & 178 SYSCFG_BOOTR_BOOT_MASK; 179 mmio_clrsetbits_32(SYSCFG_BASE + SYSCFG_BOOTR, SYSCFG_BOOTR_BOOTPD_MASK, 180 bootr << SYSCFG_BOOTR_BOOTPD_SHIFT); 181 182 stm32mp1_syscfg_set_hslv(); 183 184 stm32mp1_syscfg_enable_io_compensation_start(); 185 } 186 187 void stm32mp1_syscfg_enable_io_compensation_start(void) 188 { 189 /* 190 * Activate automatic I/O compensation. 191 * Warning: need to ensure CSI enabled and ready in clock driver. 192 * Enable non-secure clock, we assume non-secure is suspended. 193 */ 194 clk_enable(SYSCFG); 195 196 mmio_setbits_32(SYSCFG_BASE + CMPCR_CMPENSETR_OFFSET + SYSCFG_CMPCR, 197 SYSCFG_CMPENSETR_MPU_EN); 198 } 199 200 void stm32mp1_syscfg_enable_io_compensation_finish(void) 201 { 202 enable_io_comp_cell_finish(SYSCFG_CMPCR); 203 } 204 205 void stm32mp1_syscfg_disable_io_compensation(void) 206 { 207 clk_enable(SYSCFG); 208 209 /* 210 * Deactivate automatic I/O compensation. 211 * Warning: CSI is disabled automatically in STOP if not 212 * requested for other usages and always OFF in STANDBY. 213 * Disable non-secure SYSCFG clock, we assume non-secure is suspended. 214 */ 215 disable_io_comp_cell(SYSCFG_CMPCR); 216 217 clk_disable(SYSCFG); 218 } 219