blob: a4a64ca7234b804c6369b503962a93c23e561b65 [file] [log] [blame]
Yann Gautierd0ca7f42018-07-13 21:33:09 +02001/*
Fabien Dessenne83969cf2021-09-21 11:05:06 +02002 * Copyright (c) 2016-2022, STMicroelectronics - All Rights Reserved
Yann Gautierd0ca7f42018-07-13 21:33:09 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Yann Gautier038bff22019-01-17 19:17:47 +01007#include <assert.h>
8#include <errno.h>
Yann Gautierd0ca7f42018-07-13 21:33:09 +02009#include <stdbool.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
11#include <common/bl_common.h>
12#include <common/debug.h>
Yann Gautiera205a5c2021-08-30 15:06:54 +020013#include <drivers/clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <drivers/st/stm32_gpio.h>
Yann Gautier4d429472019-02-14 11:15:20 +010015#include <drivers/st/stm32mp_clkfunc.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <lib/mmio.h>
Yann Gautier038bff22019-01-17 19:17:47 +010017#include <lib/utils_def.h>
Fabien Dessenne83969cf2021-09-21 11:05:06 +020018#include <libfdt.h>
19
20#include <platform_def.h>
Yann Gautierd0ca7f42018-07-13 21:33:09 +020021
Yann Gautier038bff22019-01-17 19:17:47 +010022#define DT_GPIO_BANK_SHIFT 12
23#define DT_GPIO_BANK_MASK GENMASK(16, 12)
24#define DT_GPIO_PIN_SHIFT 8
25#define DT_GPIO_PIN_MASK GENMASK(11, 8)
26#define DT_GPIO_MODE_MASK GENMASK(7, 0)
27
Fabien Dessenne83969cf2021-09-21 11:05:06 +020028static void set_gpio(uint32_t bank, uint32_t pin, uint32_t mode, uint32_t type,
Fabien Dessennefc51b232021-09-21 14:18:34 +020029 uint32_t speed, uint32_t pull, uint32_t od,
30 uint32_t alternate, uint8_t status);
Fabien Dessenne83969cf2021-09-21 11:05:06 +020031
Yann Gautier038bff22019-01-17 19:17:47 +010032/*******************************************************************************
33 * This function gets GPIO bank node in DT.
34 * Returns node offset if status is okay in DT, else return 0
35 ******************************************************************************/
36static int ckeck_gpio_bank(void *fdt, uint32_t bank, int pinctrl_node)
Yann Gautierd0ca7f42018-07-13 21:33:09 +020037{
Yann Gautier038bff22019-01-17 19:17:47 +010038 int pinctrl_subnode;
39 uint32_t bank_offset = stm32_get_gpio_bank_offset(bank);
Yann Gautierd0ca7f42018-07-13 21:33:09 +020040
Yann Gautier038bff22019-01-17 19:17:47 +010041 fdt_for_each_subnode(pinctrl_subnode, fdt, pinctrl_node) {
42 const fdt32_t *cuint;
43
44 if (fdt_getprop(fdt, pinctrl_subnode,
45 "gpio-controller", NULL) == NULL) {
46 continue;
47 }
48
49 cuint = fdt_getprop(fdt, pinctrl_subnode, "reg", NULL);
50 if (cuint == NULL) {
51 continue;
52 }
53
54 if ((fdt32_to_cpu(*cuint) == bank_offset) &&
55 (fdt_get_status(pinctrl_subnode) != DT_DISABLED)) {
56 return pinctrl_subnode;
57 }
Yann Gautierd0ca7f42018-07-13 21:33:09 +020058 }
59
Yann Gautier038bff22019-01-17 19:17:47 +010060 return 0;
Yann Gautierd0ca7f42018-07-13 21:33:09 +020061}
62
Yann Gautier038bff22019-01-17 19:17:47 +010063/*******************************************************************************
64 * This function gets the pin settings from DT information.
65 * When analyze and parsing is done, set the GPIO registers.
66 * Returns 0 on success and a negative FDT error code on failure.
67 ******************************************************************************/
68static int dt_set_gpio_config(void *fdt, int node, uint8_t status)
Yann Gautierd0ca7f42018-07-13 21:33:09 +020069{
Yann Gautier038bff22019-01-17 19:17:47 +010070 const fdt32_t *cuint, *slewrate;
71 int len;
72 int pinctrl_node;
73 uint32_t i;
74 uint32_t speed = GPIO_SPEED_LOW;
75 uint32_t pull = GPIO_NO_PULL;
Yann Gautierd0ca7f42018-07-13 21:33:09 +020076
Yann Gautier038bff22019-01-17 19:17:47 +010077 cuint = fdt_getprop(fdt, node, "pinmux", &len);
78 if (cuint == NULL) {
79 return -FDT_ERR_NOTFOUND;
Yann Gautierd0ca7f42018-07-13 21:33:09 +020080 }
81
Yann Gautier038bff22019-01-17 19:17:47 +010082 pinctrl_node = fdt_parent_offset(fdt, fdt_parent_offset(fdt, node));
83 if (pinctrl_node < 0) {
84 return -FDT_ERR_NOTFOUND;
85 }
86
87 slewrate = fdt_getprop(fdt, node, "slew-rate", NULL);
88 if (slewrate != NULL) {
89 speed = fdt32_to_cpu(*slewrate);
90 }
91
92 if (fdt_getprop(fdt, node, "bias-pull-up", NULL) != NULL) {
93 pull = GPIO_PULL_UP;
94 } else if (fdt_getprop(fdt, node, "bias-pull-down", NULL) != NULL) {
95 pull = GPIO_PULL_DOWN;
Yann Gautierd0ca7f42018-07-13 21:33:09 +020096 } else {
Yann Gautier038bff22019-01-17 19:17:47 +010097 VERBOSE("No bias configured in node %d\n", node);
Yann Gautierd0ca7f42018-07-13 21:33:09 +020098 }
99
Yann Gautier038bff22019-01-17 19:17:47 +0100100 for (i = 0U; i < ((uint32_t)len / sizeof(uint32_t)); i++) {
101 uint32_t pincfg;
102 uint32_t bank;
103 uint32_t pin;
104 uint32_t mode;
105 uint32_t alternate = GPIO_ALTERNATE_(0);
Fabien Dessenne83969cf2021-09-21 11:05:06 +0200106 uint32_t type;
Fabien Dessennefc51b232021-09-21 14:18:34 +0200107 uint32_t od = GPIO_OD_OUTPUT_LOW;
Yann Gautier038bff22019-01-17 19:17:47 +0100108 int bank_node;
109 int clk;
110
111 pincfg = fdt32_to_cpu(*cuint);
112 cuint++;
113
114 bank = (pincfg & DT_GPIO_BANK_MASK) >> DT_GPIO_BANK_SHIFT;
115
116 pin = (pincfg & DT_GPIO_PIN_MASK) >> DT_GPIO_PIN_SHIFT;
117
118 mode = pincfg & DT_GPIO_MODE_MASK;
119
120 switch (mode) {
121 case 0:
122 mode = GPIO_MODE_INPUT;
123 break;
124 case 1 ... 16:
125 alternate = mode - 1U;
126 mode = GPIO_MODE_ALTERNATE;
127 break;
128 case 17:
129 mode = GPIO_MODE_ANALOG;
130 break;
131 default:
132 mode = GPIO_MODE_OUTPUT;
133 break;
134 }
135
136 if (fdt_getprop(fdt, node, "drive-open-drain", NULL) != NULL) {
Fabien Dessenne83969cf2021-09-21 11:05:06 +0200137 type = GPIO_TYPE_OPEN_DRAIN;
138 } else {
139 type = GPIO_TYPE_PUSH_PULL;
Yann Gautier038bff22019-01-17 19:17:47 +0100140 }
141
Fabien Dessennefc51b232021-09-21 14:18:34 +0200142 if (fdt_getprop(fdt, node, "output-high", NULL) != NULL) {
143 if (mode == GPIO_MODE_INPUT) {
144 mode = GPIO_MODE_OUTPUT;
145 od = GPIO_OD_OUTPUT_HIGH;
146 }
147 }
148
149 if (fdt_getprop(fdt, node, "output-low", NULL) != NULL) {
150 if (mode == GPIO_MODE_INPUT) {
151 mode = GPIO_MODE_OUTPUT;
152 od = GPIO_OD_OUTPUT_LOW;
153 }
154 }
155
Yann Gautier038bff22019-01-17 19:17:47 +0100156 bank_node = ckeck_gpio_bank(fdt, bank, pinctrl_node);
157 if (bank_node == 0) {
158 ERROR("PINCTRL inconsistent in DT\n");
159 panic();
160 }
161
162 clk = fdt_get_clock_id(bank_node);
163 if (clk < 0) {
164 return -FDT_ERR_NOTFOUND;
165 }
166
167 /* Platform knows the clock: assert it is okay */
168 assert((unsigned long)clk == stm32_get_gpio_bank_clock(bank));
169
Fabien Dessennefc51b232021-09-21 14:18:34 +0200170 set_gpio(bank, pin, mode, type, speed, pull, od, alternate, status);
Yann Gautier038bff22019-01-17 19:17:47 +0100171 }
172
173 return 0;
174}
175
176/*******************************************************************************
177 * This function gets the pin settings from DT information.
178 * When analyze and parsing is done, set the GPIO registers.
179 * Returns 0 on success and a negative FDT/ERRNO error code on failure.
180 ******************************************************************************/
181int dt_set_pinctrl_config(int node)
182{
183 const fdt32_t *cuint;
Fabien Dessenne83969cf2021-09-21 11:05:06 +0200184 int lenp;
Yann Gautier038bff22019-01-17 19:17:47 +0100185 uint32_t i;
Yann Gautier2e002b02020-09-04 13:25:27 +0200186 uint8_t status;
Yann Gautier038bff22019-01-17 19:17:47 +0100187 void *fdt;
188
189 if (fdt_get_address(&fdt) == 0) {
Nicolas Le Bayonc5c69452019-09-11 15:58:31 +0200190 return -FDT_ERR_NOTFOUND;
Yann Gautier038bff22019-01-17 19:17:47 +0100191 }
192
Yann Gautier2e002b02020-09-04 13:25:27 +0200193 status = fdt_get_status(node);
Yann Gautier038bff22019-01-17 19:17:47 +0100194 if (status == DT_DISABLED) {
195 return -FDT_ERR_NOTFOUND;
196 }
197
198 cuint = fdt_getprop(fdt, node, "pinctrl-0", &lenp);
199 if (cuint == NULL) {
200 return -FDT_ERR_NOTFOUND;
201 }
202
203 for (i = 0; i < ((uint32_t)lenp / 4U); i++) {
204 int p_node, p_subnode;
205
206 p_node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
207 if (p_node < 0) {
208 return -FDT_ERR_NOTFOUND;
209 }
210
211 fdt_for_each_subnode(p_subnode, fdt, p_node) {
212 int ret = dt_set_gpio_config(fdt, p_subnode, status);
213
214 if (ret < 0) {
215 return ret;
216 }
217 }
218
219 cuint++;
220 }
221
222 return 0;
223}
224
Fabien Dessenne83969cf2021-09-21 11:05:06 +0200225static void set_gpio(uint32_t bank, uint32_t pin, uint32_t mode, uint32_t type,
Fabien Dessennefc51b232021-09-21 14:18:34 +0200226 uint32_t speed, uint32_t pull, uint32_t od,
227 uint32_t alternate, uint8_t status)
Yann Gautier038bff22019-01-17 19:17:47 +0100228{
229 uintptr_t base = stm32_get_gpio_bank_base(bank);
230 unsigned long clock = stm32_get_gpio_bank_clock(bank);
231
232 assert(pin <= GPIO_PIN_MAX);
233
Yann Gautiera205a5c2021-08-30 15:06:54 +0200234 clk_enable(clock);
Yann Gautier038bff22019-01-17 19:17:47 +0100235
Fabien Dessenne83969cf2021-09-21 11:05:06 +0200236 mmio_clrsetbits_32(base + GPIO_MODE_OFFSET,
Yann Gautierfca59e02022-11-25 10:56:25 +0100237 (uint32_t)GPIO_MODE_MASK << (pin << 1U),
238 mode << (pin << 1U));
Yann Gautierd0ca7f42018-07-13 21:33:09 +0200239
Fabien Dessenne83969cf2021-09-21 11:05:06 +0200240 mmio_clrsetbits_32(base + GPIO_TYPE_OFFSET,
241 (uint32_t)GPIO_TYPE_MASK << pin,
242 type << pin);
Yann Gautierd0ca7f42018-07-13 21:33:09 +0200243
Fabien Dessenne83969cf2021-09-21 11:05:06 +0200244 mmio_clrsetbits_32(base + GPIO_SPEED_OFFSET,
Yann Gautierfca59e02022-11-25 10:56:25 +0100245 (uint32_t)GPIO_SPEED_MASK << (pin << 1U),
246 speed << (pin << 1U));
Yann Gautierd0ca7f42018-07-13 21:33:09 +0200247
Fabien Dessenne83969cf2021-09-21 11:05:06 +0200248 mmio_clrsetbits_32(base + GPIO_PUPD_OFFSET,
Yann Gautierfca59e02022-11-25 10:56:25 +0100249 (uint32_t)GPIO_PULL_MASK << (pin << 1U),
250 pull << (pin << 1U));
Yann Gautierd0ca7f42018-07-13 21:33:09 +0200251
252 if (pin < GPIO_ALT_LOWER_LIMIT) {
Fabien Dessenne83969cf2021-09-21 11:05:06 +0200253 mmio_clrsetbits_32(base + GPIO_AFRL_OFFSET,
Yann Gautierfca59e02022-11-25 10:56:25 +0100254 (uint32_t)GPIO_ALTERNATE_MASK << (pin << 2U),
255 alternate << (pin << 2U));
Yann Gautierd0ca7f42018-07-13 21:33:09 +0200256 } else {
Yann Gautierfca59e02022-11-25 10:56:25 +0100257 uint32_t shift = (pin - GPIO_ALT_LOWER_LIMIT) << 2U;
Fabien Dessenne83969cf2021-09-21 11:05:06 +0200258
259 mmio_clrsetbits_32(base + GPIO_AFRH_OFFSET,
260 (uint32_t)GPIO_ALTERNATE_MASK << shift,
261 alternate << shift);
Yann Gautierd0ca7f42018-07-13 21:33:09 +0200262 }
263
Fabien Dessennefc51b232021-09-21 14:18:34 +0200264 mmio_clrsetbits_32(base + GPIO_OD_OFFSET,
265 (uint32_t)GPIO_OD_MASK << pin,
266 od << pin);
267
Yann Gautierd0ca7f42018-07-13 21:33:09 +0200268 VERBOSE("GPIO %u mode set to 0x%x\n", bank,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100269 mmio_read_32(base + GPIO_MODE_OFFSET));
Fabien Dessenne83969cf2021-09-21 11:05:06 +0200270 VERBOSE("GPIO %u type set to 0x%x\n", bank,
271 mmio_read_32(base + GPIO_TYPE_OFFSET));
Yann Gautierd0ca7f42018-07-13 21:33:09 +0200272 VERBOSE("GPIO %u speed set to 0x%x\n", bank,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100273 mmio_read_32(base + GPIO_SPEED_OFFSET));
Yann Gautierd0ca7f42018-07-13 21:33:09 +0200274 VERBOSE("GPIO %u mode pull to 0x%x\n", bank,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100275 mmio_read_32(base + GPIO_PUPD_OFFSET));
Yann Gautierd0ca7f42018-07-13 21:33:09 +0200276 VERBOSE("GPIO %u mode alternate low to 0x%x\n", bank,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100277 mmio_read_32(base + GPIO_AFRL_OFFSET));
Yann Gautierd0ca7f42018-07-13 21:33:09 +0200278 VERBOSE("GPIO %u mode alternate high to 0x%x\n", bank,
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100279 mmio_read_32(base + GPIO_AFRH_OFFSET));
Fabien Dessennefc51b232021-09-21 14:18:34 +0200280 VERBOSE("GPIO %u output data set to 0x%x\n", bank,
281 mmio_read_32(base + GPIO_OD_OFFSET));
Yann Gautier038bff22019-01-17 19:17:47 +0100282
Yann Gautiera205a5c2021-08-30 15:06:54 +0200283 clk_disable(clock);
Etienne Carriere30189212019-12-02 10:11:32 +0100284
285 if (status == DT_SECURE) {
286 stm32mp_register_secure_gpio(bank, pin);
Yann Gautier8b6ed592020-08-11 14:21:41 +0200287#if !IMAGE_BL2
Etienne Carriere30189212019-12-02 10:11:32 +0100288 set_gpio_secure_cfg(bank, pin, true);
Yann Gautier8b6ed592020-08-11 14:21:41 +0200289#endif
Etienne Carriere30189212019-12-02 10:11:32 +0100290
291 } else {
292 stm32mp_register_non_secure_gpio(bank, pin);
Yann Gautier8b6ed592020-08-11 14:21:41 +0200293#if !IMAGE_BL2
Etienne Carriere30189212019-12-02 10:11:32 +0100294 set_gpio_secure_cfg(bank, pin, false);
Yann Gautier8b6ed592020-08-11 14:21:41 +0200295#endif
Etienne Carriere30189212019-12-02 10:11:32 +0100296 }
Yann Gautier038bff22019-01-17 19:17:47 +0100297}
298
299void set_gpio_secure_cfg(uint32_t bank, uint32_t pin, bool secure)
300{
301 uintptr_t base = stm32_get_gpio_bank_base(bank);
Yann Gautieree8f5422019-02-14 11:13:25 +0100302 unsigned long clock = stm32_get_gpio_bank_clock(bank);
Yann Gautier038bff22019-01-17 19:17:47 +0100303
304 assert(pin <= GPIO_PIN_MAX);
305
Yann Gautiera205a5c2021-08-30 15:06:54 +0200306 clk_enable(clock);
Yann Gautier038bff22019-01-17 19:17:47 +0100307
308 if (secure) {
309 mmio_setbits_32(base + GPIO_SECR_OFFSET, BIT(pin));
310 } else {
311 mmio_clrbits_32(base + GPIO_SECR_OFFSET, BIT(pin));
312 }
313
Yann Gautiera205a5c2021-08-30 15:06:54 +0200314 clk_disable(clock);
Yann Gautierd0ca7f42018-07-13 21:33:09 +0200315}
Yann Gautier2b79c372021-06-11 10:54:56 +0200316
317void set_gpio_reset_cfg(uint32_t bank, uint32_t pin)
318{
Fabien Dessenne83969cf2021-09-21 11:05:06 +0200319 set_gpio(bank, pin, GPIO_MODE_ANALOG, GPIO_TYPE_PUSH_PULL,
Fabien Dessennefc51b232021-09-21 14:18:34 +0200320 GPIO_SPEED_LOW, GPIO_NO_PULL, GPIO_OD_OUTPUT_LOW,
321 GPIO_ALTERNATE_(0), DT_DISABLED);
Yann Gautier2b79c372021-06-11 10:54:56 +0200322 set_gpio_secure_cfg(bank, pin, stm32_gpio_is_secure_at_reset(bank));
323}