Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 1 | /* |
Nicolas Le Bayon | 3689869 | 2019-11-18 17:18:06 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2022, STMicroelectronics - All Rights Reserved |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 7 | #include <common/debug.h> |
Etienne Carriere | 1de46d0 | 2020-02-07 13:38:30 +0100 | [diff] [blame] | 8 | #include <drivers/clk.h> |
Nicolas Le Bayon | 3689869 | 2019-11-18 17:18:06 +0100 | [diff] [blame] | 9 | #include <drivers/delay_timer.h> |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 10 | #include <drivers/st/stpmic1.h> |
| 11 | #include <lib/mmio.h> |
| 12 | |
Nicolas Le Bayon | 3689869 | 2019-11-18 17:18:06 +0100 | [diff] [blame] | 13 | #include <platform_def.h> |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 14 | #include <stm32mp_common.h> |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 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 |
Yann Gautier | 0315fa0 | 2020-10-26 15:21:25 +0100 | [diff] [blame] | 26 | #define SYSCFG_CMPENCLRR 0x28U |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 27 | |
Yann Gautier | b76c61a | 2020-12-16 10:17:35 +0100 | [diff] [blame] | 28 | #define CMPCR_CMPENSETR_OFFSET 0x4U |
| 29 | #define CMPCR_CMPENCLRR_OFFSET 0x8U |
| 30 | |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 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 | |
Nicolas Le Bayon | 3689869 | 2019-11-18 17:18:06 +0100 | [diff] [blame] | 61 | #define SYSCFG_CMPCR_READY_TIMEOUT_US 10000U |
| 62 | |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 63 | /* |
| 64 | * SYSCFG_CMPENSETR Register |
| 65 | */ |
| 66 | #define SYSCFG_CMPENSETR_MPU_EN BIT(0) |
| 67 | |
Yann Gautier | b76c61a | 2020-12-16 10:17:35 +0100 | [diff] [blame] | 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 | |
Yann Gautier | e0a60cb | 2021-01-12 14:45:02 +0100 | [diff] [blame] | 107 | static void enable_high_speed_mode_low_voltage(void) |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 108 | { |
Yann Gautier | e0a60cb | 2021-01-12 14:45:02 +0100 | [diff] [blame] | 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 | { |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 119 | uint32_t otp_value; |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 120 | uint32_t vdd_voltage; |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 121 | bool product_below_2v5; |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 122 | |
| 123 | /* |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 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 | */ |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 138 | if (stm32_get_otp_value(HW2_OTP, &otp_value) != 0) { |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 139 | panic(); |
| 140 | } |
| 141 | |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 142 | product_below_2v5 = (otp_value & HW2_OTP_PRODUCT_BELOW_2V5) != 0U; |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 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) { |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 149 | WARN("VDD unknown\n"); |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 150 | } else if (vdd_voltage < 2700000U) { |
Yann Gautier | e0a60cb | 2021-01-12 14:45:02 +0100 | [diff] [blame] | 151 | enable_high_speed_mode_low_voltage(); |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 152 | |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 153 | if (!product_below_2v5) { |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 154 | INFO("Product_below_2v5=0: HSLVEN protected by HW\n"); |
| 155 | } |
| 156 | } else { |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 157 | if (product_below_2v5) { |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 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 | } |
Yann Gautier | e0a60cb | 2021-01-12 14:45:02 +0100 | [diff] [blame] | 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(); |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 183 | |
Yann Gautier | b76c61a | 2020-12-16 10:17:35 +0100 | [diff] [blame] | 184 | stm32mp1_syscfg_enable_io_compensation_start(); |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Yann Gautier | b76c61a | 2020-12-16 10:17:35 +0100 | [diff] [blame] | 187 | void stm32mp1_syscfg_enable_io_compensation_start(void) |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 188 | { |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 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 | */ |
Etienne Carriere | 1de46d0 | 2020-02-07 13:38:30 +0100 | [diff] [blame] | 194 | clk_enable(SYSCFG); |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 195 | |
Yann Gautier | b76c61a | 2020-12-16 10:17:35 +0100 | [diff] [blame] | 196 | mmio_setbits_32(SYSCFG_BASE + CMPCR_CMPENSETR_OFFSET + SYSCFG_CMPCR, |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 197 | SYSCFG_CMPENSETR_MPU_EN); |
Yann Gautier | b76c61a | 2020-12-16 10:17:35 +0100 | [diff] [blame] | 198 | } |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 199 | |
Yann Gautier | b76c61a | 2020-12-16 10:17:35 +0100 | [diff] [blame] | 200 | void stm32mp1_syscfg_enable_io_compensation_finish(void) |
| 201 | { |
| 202 | enable_io_comp_cell_finish(SYSCFG_CMPCR); |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void stm32mp1_syscfg_disable_io_compensation(void) |
| 206 | { |
Yann Gautier | b76c61a | 2020-12-16 10:17:35 +0100 | [diff] [blame] | 207 | clk_enable(SYSCFG); |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 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 | */ |
Yann Gautier | b76c61a | 2020-12-16 10:17:35 +0100 | [diff] [blame] | 215 | disable_io_comp_cell(SYSCFG_CMPCR); |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 216 | |
Etienne Carriere | 1de46d0 | 2020-02-07 13:38:30 +0100 | [diff] [blame] | 217 | clk_disable(SYSCFG); |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 218 | } |