Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 1 | /* |
Fabien Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 2 | * Copyright (c) 2016-2022, STMicroelectronics - All Rights Reserved |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | #include <errno.h> |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 9 | #include <stdbool.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | |
| 11 | #include <common/bl_common.h> |
| 12 | #include <common/debug.h> |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 13 | #include <drivers/clk.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | #include <drivers/st/stm32_gpio.h> |
Yann Gautier | 4d42947 | 2019-02-14 11:15:20 +0100 | [diff] [blame] | 15 | #include <drivers/st/stm32mp_clkfunc.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 16 | #include <lib/mmio.h> |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 17 | #include <lib/utils_def.h> |
Fabien Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 18 | #include <libfdt.h> |
| 19 | |
| 20 | #include <platform_def.h> |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 21 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 22 | #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 Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 28 | static void set_gpio(uint32_t bank, uint32_t pin, uint32_t mode, uint32_t type, |
| 29 | uint32_t speed, uint32_t pull, uint32_t alternate, |
| 30 | uint8_t status); |
| 31 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 32 | /******************************************************************************* |
| 33 | * This function gets GPIO bank node in DT. |
| 34 | * Returns node offset if status is okay in DT, else return 0 |
| 35 | ******************************************************************************/ |
| 36 | static int ckeck_gpio_bank(void *fdt, uint32_t bank, int pinctrl_node) |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 37 | { |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 38 | int pinctrl_subnode; |
| 39 | uint32_t bank_offset = stm32_get_gpio_bank_offset(bank); |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 40 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 41 | 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 Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 60 | return 0; |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 63 | /******************************************************************************* |
| 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 | ******************************************************************************/ |
| 68 | static int dt_set_gpio_config(void *fdt, int node, uint8_t status) |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 69 | { |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 70 | 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 Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 76 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 77 | cuint = fdt_getprop(fdt, node, "pinmux", &len); |
| 78 | if (cuint == NULL) { |
| 79 | return -FDT_ERR_NOTFOUND; |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 82 | 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 Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 96 | } else { |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 97 | VERBOSE("No bias configured in node %d\n", node); |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 100 | 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 Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 106 | uint32_t type; |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 107 | int bank_node; |
| 108 | int clk; |
| 109 | |
| 110 | pincfg = fdt32_to_cpu(*cuint); |
| 111 | cuint++; |
| 112 | |
| 113 | bank = (pincfg & DT_GPIO_BANK_MASK) >> DT_GPIO_BANK_SHIFT; |
| 114 | |
| 115 | pin = (pincfg & DT_GPIO_PIN_MASK) >> DT_GPIO_PIN_SHIFT; |
| 116 | |
| 117 | mode = pincfg & DT_GPIO_MODE_MASK; |
| 118 | |
| 119 | switch (mode) { |
| 120 | case 0: |
| 121 | mode = GPIO_MODE_INPUT; |
| 122 | break; |
| 123 | case 1 ... 16: |
| 124 | alternate = mode - 1U; |
| 125 | mode = GPIO_MODE_ALTERNATE; |
| 126 | break; |
| 127 | case 17: |
| 128 | mode = GPIO_MODE_ANALOG; |
| 129 | break; |
| 130 | default: |
| 131 | mode = GPIO_MODE_OUTPUT; |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | if (fdt_getprop(fdt, node, "drive-open-drain", NULL) != NULL) { |
Fabien Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 136 | type = GPIO_TYPE_OPEN_DRAIN; |
| 137 | } else { |
| 138 | type = GPIO_TYPE_PUSH_PULL; |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | bank_node = ckeck_gpio_bank(fdt, bank, pinctrl_node); |
| 142 | if (bank_node == 0) { |
| 143 | ERROR("PINCTRL inconsistent in DT\n"); |
| 144 | panic(); |
| 145 | } |
| 146 | |
| 147 | clk = fdt_get_clock_id(bank_node); |
| 148 | if (clk < 0) { |
| 149 | return -FDT_ERR_NOTFOUND; |
| 150 | } |
| 151 | |
| 152 | /* Platform knows the clock: assert it is okay */ |
| 153 | assert((unsigned long)clk == stm32_get_gpio_bank_clock(bank)); |
| 154 | |
Fabien Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 155 | set_gpio(bank, pin, mode, type, speed, pull, alternate, status); |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /******************************************************************************* |
| 162 | * This function gets the pin settings from DT information. |
| 163 | * When analyze and parsing is done, set the GPIO registers. |
| 164 | * Returns 0 on success and a negative FDT/ERRNO error code on failure. |
| 165 | ******************************************************************************/ |
| 166 | int dt_set_pinctrl_config(int node) |
| 167 | { |
| 168 | const fdt32_t *cuint; |
Fabien Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 169 | int lenp; |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 170 | uint32_t i; |
Yann Gautier | 2e002b0 | 2020-09-04 13:25:27 +0200 | [diff] [blame] | 171 | uint8_t status; |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 172 | void *fdt; |
| 173 | |
| 174 | if (fdt_get_address(&fdt) == 0) { |
Nicolas Le Bayon | c5c6945 | 2019-09-11 15:58:31 +0200 | [diff] [blame] | 175 | return -FDT_ERR_NOTFOUND; |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Yann Gautier | 2e002b0 | 2020-09-04 13:25:27 +0200 | [diff] [blame] | 178 | status = fdt_get_status(node); |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 179 | if (status == DT_DISABLED) { |
| 180 | return -FDT_ERR_NOTFOUND; |
| 181 | } |
| 182 | |
| 183 | cuint = fdt_getprop(fdt, node, "pinctrl-0", &lenp); |
| 184 | if (cuint == NULL) { |
| 185 | return -FDT_ERR_NOTFOUND; |
| 186 | } |
| 187 | |
| 188 | for (i = 0; i < ((uint32_t)lenp / 4U); i++) { |
| 189 | int p_node, p_subnode; |
| 190 | |
| 191 | p_node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint)); |
| 192 | if (p_node < 0) { |
| 193 | return -FDT_ERR_NOTFOUND; |
| 194 | } |
| 195 | |
| 196 | fdt_for_each_subnode(p_subnode, fdt, p_node) { |
| 197 | int ret = dt_set_gpio_config(fdt, p_subnode, status); |
| 198 | |
| 199 | if (ret < 0) { |
| 200 | return ret; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | cuint++; |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
Fabien Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 210 | static void set_gpio(uint32_t bank, uint32_t pin, uint32_t mode, uint32_t type, |
| 211 | uint32_t speed, uint32_t pull, uint32_t alternate, |
| 212 | uint8_t status) |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 213 | { |
| 214 | uintptr_t base = stm32_get_gpio_bank_base(bank); |
| 215 | unsigned long clock = stm32_get_gpio_bank_clock(bank); |
| 216 | |
| 217 | assert(pin <= GPIO_PIN_MAX); |
| 218 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 219 | clk_enable(clock); |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 220 | |
Fabien Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 221 | mmio_clrsetbits_32(base + GPIO_MODE_OFFSET, |
| 222 | (uint32_t)GPIO_MODE_MASK << (pin << 1), |
| 223 | mode << (pin << 1)); |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 224 | |
Fabien Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 225 | mmio_clrsetbits_32(base + GPIO_TYPE_OFFSET, |
| 226 | (uint32_t)GPIO_TYPE_MASK << pin, |
| 227 | type << pin); |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 228 | |
Fabien Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 229 | mmio_clrsetbits_32(base + GPIO_SPEED_OFFSET, |
| 230 | (uint32_t)GPIO_SPEED_MASK << (pin << 1), |
| 231 | speed << (pin << 1)); |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 232 | |
Fabien Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 233 | mmio_clrsetbits_32(base + GPIO_PUPD_OFFSET, |
| 234 | (uint32_t)GPIO_PULL_MASK << (pin << 1), |
| 235 | pull << (pin << 1)); |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 236 | |
| 237 | if (pin < GPIO_ALT_LOWER_LIMIT) { |
Fabien Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 238 | mmio_clrsetbits_32(base + GPIO_AFRL_OFFSET, |
| 239 | (uint32_t)GPIO_ALTERNATE_MASK << (pin << 2), |
| 240 | alternate << (pin << 2)); |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 241 | } else { |
Fabien Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 242 | size_t shift = (pin - GPIO_ALT_LOWER_LIMIT) << 2; |
| 243 | |
| 244 | mmio_clrsetbits_32(base + GPIO_AFRH_OFFSET, |
| 245 | (uint32_t)GPIO_ALTERNATE_MASK << shift, |
| 246 | alternate << shift); |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | VERBOSE("GPIO %u mode set to 0x%x\n", bank, |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 250 | mmio_read_32(base + GPIO_MODE_OFFSET)); |
Fabien Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 251 | VERBOSE("GPIO %u type set to 0x%x\n", bank, |
| 252 | mmio_read_32(base + GPIO_TYPE_OFFSET)); |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 253 | VERBOSE("GPIO %u speed set to 0x%x\n", bank, |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 254 | mmio_read_32(base + GPIO_SPEED_OFFSET)); |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 255 | VERBOSE("GPIO %u mode pull to 0x%x\n", bank, |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 256 | mmio_read_32(base + GPIO_PUPD_OFFSET)); |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 257 | VERBOSE("GPIO %u mode alternate low to 0x%x\n", bank, |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 258 | mmio_read_32(base + GPIO_AFRL_OFFSET)); |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 259 | VERBOSE("GPIO %u mode alternate high to 0x%x\n", bank, |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 260 | mmio_read_32(base + GPIO_AFRH_OFFSET)); |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 261 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 262 | clk_disable(clock); |
Etienne Carriere | 3018921 | 2019-12-02 10:11:32 +0100 | [diff] [blame] | 263 | |
| 264 | if (status == DT_SECURE) { |
| 265 | stm32mp_register_secure_gpio(bank, pin); |
| 266 | set_gpio_secure_cfg(bank, pin, true); |
| 267 | |
| 268 | } else { |
| 269 | stm32mp_register_non_secure_gpio(bank, pin); |
| 270 | set_gpio_secure_cfg(bank, pin, false); |
| 271 | } |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void set_gpio_secure_cfg(uint32_t bank, uint32_t pin, bool secure) |
| 275 | { |
| 276 | uintptr_t base = stm32_get_gpio_bank_base(bank); |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 277 | unsigned long clock = stm32_get_gpio_bank_clock(bank); |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 278 | |
| 279 | assert(pin <= GPIO_PIN_MAX); |
| 280 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 281 | clk_enable(clock); |
Yann Gautier | 038bff2 | 2019-01-17 19:17:47 +0100 | [diff] [blame] | 282 | |
| 283 | if (secure) { |
| 284 | mmio_setbits_32(base + GPIO_SECR_OFFSET, BIT(pin)); |
| 285 | } else { |
| 286 | mmio_clrbits_32(base + GPIO_SECR_OFFSET, BIT(pin)); |
| 287 | } |
| 288 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 289 | clk_disable(clock); |
Yann Gautier | d0ca7f4 | 2018-07-13 21:33:09 +0200 | [diff] [blame] | 290 | } |
Yann Gautier | 2b79c37 | 2021-06-11 10:54:56 +0200 | [diff] [blame] | 291 | |
| 292 | void set_gpio_reset_cfg(uint32_t bank, uint32_t pin) |
| 293 | { |
Fabien Dessenne | 83969cf | 2021-09-21 11:05:06 +0200 | [diff] [blame^] | 294 | set_gpio(bank, pin, GPIO_MODE_ANALOG, GPIO_TYPE_PUSH_PULL, |
| 295 | GPIO_SPEED_LOW, GPIO_NO_PULL, GPIO_ALTERNATE_(0), DT_DISABLED); |
Yann Gautier | 2b79c37 | 2021-06-11 10:54:56 +0200 | [diff] [blame] | 296 | set_gpio_secure_cfg(bank, pin, stm32_gpio_is_secure_at_reset(bank)); |
| 297 | } |