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