blob: 3f34af15b1e0d2c8d0fd50e2109f5b6df3352e1e [file] [log] [blame]
Yann Gautier3edc7c32019-05-20 19:17:08 +02001/*
Nicolas Le Bayon36898692019-11-18 17:18:06 +01002 * Copyright (c) 2019-2022, STMicroelectronics - All Rights Reserved
Yann Gautier3edc7c32019-05-20 19:17:08 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Yann Gautier3edc7c32019-05-20 19:17:08 +02007#include <common/debug.h>
Etienne Carriere1de46d02020-02-07 13:38:30 +01008#include <drivers/clk.h>
Nicolas Le Bayon36898692019-11-18 17:18:06 +01009#include <drivers/delay_timer.h>
Yann Gautier3edc7c32019-05-20 19:17:08 +020010#include <drivers/st/stpmic1.h>
11#include <lib/mmio.h>
12
Nicolas Le Bayon36898692019-11-18 17:18:06 +010013#include <platform_def.h>
Lionel Debievebc2d88d2019-11-04 14:31:38 +010014#include <stm32mp_common.h>
Yann Gautier3edc7c32019-05-20 19:17:08 +020015#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 Gautier0315fa02020-10-26 15:21:25 +010026#define SYSCFG_CMPENCLRR 0x28U
Yann Gautier3edc7c32019-05-20 19:17:08 +020027
Yann Gautierb76c61a2020-12-16 10:17:35 +010028#define CMPCR_CMPENSETR_OFFSET 0x4U
29#define CMPCR_CMPENCLRR_OFFSET 0x8U
30
Yann Gautier3edc7c32019-05-20 19:17:08 +020031/*
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 Bayon36898692019-11-18 17:18:06 +010061#define SYSCFG_CMPCR_READY_TIMEOUT_US 10000U
62
Yann Gautier3edc7c32019-05-20 19:17:08 +020063/*
64 * SYSCFG_CMPENSETR Register
65 */
66#define SYSCFG_CMPENSETR_MPU_EN BIT(0)
67
Yann Gautierb76c61a2020-12-16 10:17:35 +010068static 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
85static 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 Gautiere0a60cb2021-01-12 14:45:02 +0100107static void enable_high_speed_mode_low_voltage(void)
Yann Gautier3edc7c32019-05-20 19:17:08 +0200108{
Yann Gautiere0a60cb2021-01-12 14:45:02 +0100109 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
117static void stm32mp1_syscfg_set_hslv(void)
118{
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100119 uint32_t otp_value;
Yann Gautier3edc7c32019-05-20 19:17:08 +0200120 uint32_t vdd_voltage;
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100121 bool product_below_2v5;
Yann Gautier3edc7c32019-05-20 19:17:08 +0200122
123 /*
Yann Gautier3edc7c32019-05-20 19:17:08 +0200124 * 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 Debievebc2d88d2019-11-04 14:31:38 +0100138 if (stm32_get_otp_value(HW2_OTP, &otp_value) != 0) {
Yann Gautier3edc7c32019-05-20 19:17:08 +0200139 panic();
140 }
141
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100142 product_below_2v5 = (otp_value & HW2_OTP_PRODUCT_BELOW_2V5) != 0U;
Yann Gautier3edc7c32019-05-20 19:17:08 +0200143
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 Debievebc2d88d2019-11-04 14:31:38 +0100149 WARN("VDD unknown\n");
Yann Gautier3edc7c32019-05-20 19:17:08 +0200150 } else if (vdd_voltage < 2700000U) {
Yann Gautiere0a60cb2021-01-12 14:45:02 +0100151 enable_high_speed_mode_low_voltage();
Yann Gautier3edc7c32019-05-20 19:17:08 +0200152
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100153 if (!product_below_2v5) {
Yann Gautier3edc7c32019-05-20 19:17:08 +0200154 INFO("Product_below_2v5=0: HSLVEN protected by HW\n");
155 }
156 } else {
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100157 if (product_below_2v5) {
Yann Gautier3edc7c32019-05-20 19:17:08 +0200158 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 Gautiere0a60cb2021-01-12 14:45:02 +0100164}
165
166void 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 Gautier3edc7c32019-05-20 19:17:08 +0200183
Yann Gautierb76c61a2020-12-16 10:17:35 +0100184 stm32mp1_syscfg_enable_io_compensation_start();
Yann Gautier3edc7c32019-05-20 19:17:08 +0200185}
186
Yann Gautierb76c61a2020-12-16 10:17:35 +0100187void stm32mp1_syscfg_enable_io_compensation_start(void)
Yann Gautier3edc7c32019-05-20 19:17:08 +0200188{
Yann Gautier3edc7c32019-05-20 19:17:08 +0200189 /*
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 Carriere1de46d02020-02-07 13:38:30 +0100194 clk_enable(SYSCFG);
Yann Gautier3edc7c32019-05-20 19:17:08 +0200195
Yann Gautierb76c61a2020-12-16 10:17:35 +0100196 mmio_setbits_32(SYSCFG_BASE + CMPCR_CMPENSETR_OFFSET + SYSCFG_CMPCR,
Yann Gautier3edc7c32019-05-20 19:17:08 +0200197 SYSCFG_CMPENSETR_MPU_EN);
Yann Gautierb76c61a2020-12-16 10:17:35 +0100198}
Yann Gautier3edc7c32019-05-20 19:17:08 +0200199
Yann Gautierb76c61a2020-12-16 10:17:35 +0100200void stm32mp1_syscfg_enable_io_compensation_finish(void)
201{
202 enable_io_comp_cell_finish(SYSCFG_CMPCR);
Yann Gautier3edc7c32019-05-20 19:17:08 +0200203}
204
205void stm32mp1_syscfg_disable_io_compensation(void)
206{
Yann Gautierb76c61a2020-12-16 10:17:35 +0100207 clk_enable(SYSCFG);
Yann Gautier3edc7c32019-05-20 19:17:08 +0200208
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 Gautierb76c61a2020-12-16 10:17:35 +0100215 disable_io_comp_cell(SYSCFG_CMPCR);
Yann Gautier3edc7c32019-05-20 19:17:08 +0200216
Etienne Carriere1de46d02020-02-07 13:38:30 +0100217 clk_disable(SYSCFG);
Yann Gautier3edc7c32019-05-20 19:17:08 +0200218}