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