Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 2 | /* |
Patrice Chotard | 789ee0e | 2017-10-23 09:53:58 +0200 | [diff] [blame] | 3 | * Copyright (C) 2017, STMicroelectronics - All Rights Reserved |
| 4 | * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics. |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <clk.h> |
| 9 | #include <dm.h> |
| 10 | #include <fdtdec.h> |
| 11 | #include <asm/arch/gpio.h> |
| 12 | #include <asm/arch/stm32.h> |
| 13 | #include <asm/gpio.h> |
| 14 | #include <asm/io.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame^] | 15 | #include <dm/device_compat.h> |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 16 | #include <linux/errno.h> |
| 17 | #include <linux/io.h> |
| 18 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 19 | #define MODE_BITS(gpio_pin) (gpio_pin * 2) |
| 20 | #define MODE_BITS_MASK 3 |
Patrice Chotard | 4e91500 | 2018-08-09 11:57:57 +0200 | [diff] [blame] | 21 | #define BSRR_BIT(gpio_pin, value) BIT(gpio_pin + (value ? 0 : 16)) |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 22 | |
Patrice Chotard | 9f62b08 | 2019-01-04 10:55:06 +0100 | [diff] [blame] | 23 | #ifndef CONFIG_SPL_BUILD |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 24 | /* |
| 25 | * convert gpio offset to gpio index taking into account gpio holes |
| 26 | * into gpio bank |
| 27 | */ |
| 28 | int stm32_offset_to_index(struct udevice *dev, unsigned int offset) |
| 29 | { |
| 30 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
Patrick Delaunay | 2a6c7ad | 2019-06-21 15:26:46 +0200 | [diff] [blame] | 31 | unsigned int idx = 0; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 32 | int i; |
| 33 | |
| 34 | for (i = 0; i < STM32_GPIOS_PER_BANK; i++) { |
| 35 | if (priv->gpio_range & BIT(i)) { |
| 36 | if (idx == offset) |
| 37 | return idx; |
| 38 | idx++; |
| 39 | } |
| 40 | } |
| 41 | /* shouldn't happen */ |
| 42 | return -EINVAL; |
| 43 | } |
| 44 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 45 | static int stm32_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 46 | { |
| 47 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 48 | struct stm32_gpio_regs *regs = priv->regs; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 49 | int bits_index; |
| 50 | int mask; |
| 51 | int idx; |
| 52 | |
| 53 | idx = stm32_offset_to_index(dev, offset); |
| 54 | if (idx < 0) |
| 55 | return idx; |
| 56 | |
| 57 | bits_index = MODE_BITS(idx); |
| 58 | mask = MODE_BITS_MASK << bits_index; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 59 | |
| 60 | clrsetbits_le32(®s->moder, mask, STM32_GPIO_MODE_IN << bits_index); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int stm32_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 66 | int value) |
| 67 | { |
| 68 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 69 | struct stm32_gpio_regs *regs = priv->regs; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 70 | int bits_index; |
| 71 | int mask; |
| 72 | int idx; |
| 73 | |
| 74 | idx = stm32_offset_to_index(dev, offset); |
| 75 | if (idx < 0) |
| 76 | return idx; |
| 77 | |
| 78 | bits_index = MODE_BITS(idx); |
| 79 | mask = MODE_BITS_MASK << bits_index; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 80 | |
| 81 | clrsetbits_le32(®s->moder, mask, STM32_GPIO_MODE_OUT << bits_index); |
Patrice Chotard | 4e91500 | 2018-08-09 11:57:57 +0200 | [diff] [blame] | 82 | |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 83 | writel(BSRR_BIT(idx, value), ®s->bsrr); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int stm32_gpio_get_value(struct udevice *dev, unsigned offset) |
| 89 | { |
| 90 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 91 | struct stm32_gpio_regs *regs = priv->regs; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 92 | int idx; |
| 93 | |
| 94 | idx = stm32_offset_to_index(dev, offset); |
| 95 | if (idx < 0) |
| 96 | return idx; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 97 | |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 98 | return readl(®s->idr) & BIT(idx) ? 1 : 0; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static int stm32_gpio_set_value(struct udevice *dev, unsigned offset, int value) |
| 102 | { |
| 103 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 104 | struct stm32_gpio_regs *regs = priv->regs; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 105 | int idx; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 106 | |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 107 | idx = stm32_offset_to_index(dev, offset); |
| 108 | if (idx < 0) |
| 109 | return idx; |
| 110 | |
| 111 | writel(BSRR_BIT(idx, value), ®s->bsrr); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Patrice Chotard | 1056123 | 2018-10-24 14:10:21 +0200 | [diff] [blame] | 116 | static int stm32_gpio_get_function(struct udevice *dev, unsigned int offset) |
| 117 | { |
| 118 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
| 119 | struct stm32_gpio_regs *regs = priv->regs; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 120 | int bits_index; |
| 121 | int mask; |
| 122 | int idx; |
Patrice Chotard | 1056123 | 2018-10-24 14:10:21 +0200 | [diff] [blame] | 123 | u32 mode; |
| 124 | |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 125 | idx = stm32_offset_to_index(dev, offset); |
| 126 | if (idx < 0) |
| 127 | return idx; |
| 128 | |
| 129 | bits_index = MODE_BITS(idx); |
| 130 | mask = MODE_BITS_MASK << bits_index; |
| 131 | |
Patrice Chotard | 1056123 | 2018-10-24 14:10:21 +0200 | [diff] [blame] | 132 | mode = (readl(®s->moder) & mask) >> bits_index; |
| 133 | if (mode == STM32_GPIO_MODE_OUT) |
| 134 | return GPIOF_OUTPUT; |
| 135 | if (mode == STM32_GPIO_MODE_IN) |
| 136 | return GPIOF_INPUT; |
| 137 | if (mode == STM32_GPIO_MODE_AN) |
| 138 | return GPIOF_UNUSED; |
| 139 | |
| 140 | return GPIOF_FUNC; |
| 141 | } |
| 142 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 143 | static const struct dm_gpio_ops gpio_stm32_ops = { |
| 144 | .direction_input = stm32_gpio_direction_input, |
| 145 | .direction_output = stm32_gpio_direction_output, |
| 146 | .get_value = stm32_gpio_get_value, |
| 147 | .set_value = stm32_gpio_set_value, |
Patrice Chotard | 1056123 | 2018-10-24 14:10:21 +0200 | [diff] [blame] | 148 | .get_function = stm32_gpio_get_function, |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 149 | }; |
Patrice Chotard | 9f62b08 | 2019-01-04 10:55:06 +0100 | [diff] [blame] | 150 | #endif |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 151 | |
| 152 | static int gpio_stm32_probe(struct udevice *dev) |
| 153 | { |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 154 | struct stm32_gpio_priv *priv = dev_get_priv(dev); |
Patrice Chotard | 159d157 | 2018-12-03 10:52:53 +0100 | [diff] [blame] | 155 | struct clk clk; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 156 | fdt_addr_t addr; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 157 | int ret; |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 158 | |
Patrick Delaunay | d78f968 | 2018-03-12 10:46:07 +0100 | [diff] [blame] | 159 | addr = dev_read_addr(dev); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 160 | if (addr == FDT_ADDR_T_NONE) |
| 161 | return -EINVAL; |
| 162 | |
| 163 | priv->regs = (struct stm32_gpio_regs *)addr; |
Patrice Chotard | 9f62b08 | 2019-01-04 10:55:06 +0100 | [diff] [blame] | 164 | |
| 165 | #ifndef CONFIG_SPL_BUILD |
| 166 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 167 | struct ofnode_phandle_args args; |
| 168 | const char *name; |
| 169 | int i; |
| 170 | |
Patrick Delaunay | d78f968 | 2018-03-12 10:46:07 +0100 | [diff] [blame] | 171 | name = dev_read_string(dev, "st,bank-name"); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 172 | if (!name) |
| 173 | return -EINVAL; |
| 174 | uc_priv->bank_name = name; |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 175 | |
| 176 | i = 0; |
| 177 | ret = dev_read_phandle_with_args(dev, "gpio-ranges", |
| 178 | NULL, 3, i, &args); |
| 179 | |
Patrice Chotard | 6225305 | 2019-01-04 10:55:05 +0100 | [diff] [blame] | 180 | if (ret == -ENOENT) { |
| 181 | uc_priv->gpio_count = STM32_GPIOS_PER_BANK; |
| 182 | priv->gpio_range = GENMASK(STM32_GPIOS_PER_BANK - 1, 0); |
| 183 | } |
| 184 | |
Patrice Chotard | 0099c1e | 2018-12-03 10:52:51 +0100 | [diff] [blame] | 185 | while (ret != -ENOENT) { |
| 186 | priv->gpio_range |= GENMASK(args.args[2] + args.args[0] - 1, |
| 187 | args.args[0]); |
| 188 | |
| 189 | uc_priv->gpio_count += args.args[2]; |
| 190 | |
| 191 | ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3, |
| 192 | ++i, &args); |
| 193 | } |
| 194 | |
| 195 | dev_dbg(dev, "addr = 0x%p bank_name = %s gpio_count = %d gpio_range = 0x%x\n", |
| 196 | (u32 *)priv->regs, uc_priv->bank_name, uc_priv->gpio_count, |
| 197 | priv->gpio_range); |
Patrice Chotard | 9f62b08 | 2019-01-04 10:55:06 +0100 | [diff] [blame] | 198 | #endif |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 199 | ret = clk_get_by_index(dev, 0, &clk); |
| 200 | if (ret < 0) |
| 201 | return ret; |
| 202 | |
| 203 | ret = clk_enable(&clk); |
| 204 | |
| 205 | if (ret) { |
| 206 | dev_err(dev, "failed to enable clock\n"); |
| 207 | return ret; |
| 208 | } |
| 209 | debug("clock enabled for device %s\n", dev->name); |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 214 | U_BOOT_DRIVER(gpio_stm32) = { |
| 215 | .name = "gpio_stm32", |
| 216 | .id = UCLASS_GPIO, |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 217 | .probe = gpio_stm32_probe, |
Patrice Chotard | 9f62b08 | 2019-01-04 10:55:06 +0100 | [diff] [blame] | 218 | #ifndef CONFIG_SPL_BUILD |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 219 | .ops = &gpio_stm32_ops, |
Patrice Chotard | 9f62b08 | 2019-01-04 10:55:06 +0100 | [diff] [blame] | 220 | #endif |
Bin Meng | b508ee5 | 2018-10-24 06:36:30 -0700 | [diff] [blame] | 221 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Vikas Manocha | ec8630a | 2017-04-10 15:02:57 -0700 | [diff] [blame] | 222 | .priv_auto_alloc_size = sizeof(struct stm32_gpio_priv), |
| 223 | }; |