Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | 23ccda0 | 2013-04-24 10:01:20 +0200 | [diff] [blame] | 2 | /* |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 3 | * Copyright (c) 2013 - 2018 Xilinx, Michal Simek |
Michal Simek | 23ccda0 | 2013-04-24 10:01:20 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Michal Simek | 23ccda0 | 2013-04-24 10:01:20 +0200 | [diff] [blame] | 9 | #include <malloc.h> |
| 10 | #include <linux/list.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <asm/gpio.h> |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 13 | #include <dm.h> |
Michal Simek | e2f91e5 | 2018-07-23 13:40:01 +0200 | [diff] [blame] | 14 | #include <dt-bindings/gpio/gpio.h> |
Michal Simek | 23ccda0 | 2013-04-24 10:01:20 +0200 | [diff] [blame] | 15 | |
Michal Simek | e2f91e5 | 2018-07-23 13:40:01 +0200 | [diff] [blame] | 16 | #define XILINX_GPIO_MAX_BANK 2 |
Michal Simek | 23ccda0 | 2013-04-24 10:01:20 +0200 | [diff] [blame] | 17 | |
| 18 | /* Gpio simple map */ |
| 19 | struct gpio_regs { |
| 20 | u32 gpiodata; |
| 21 | u32 gpiodir; |
| 22 | }; |
| 23 | |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 24 | struct xilinx_gpio_platdata { |
| 25 | struct gpio_regs *regs; |
| 26 | int bank_max[XILINX_GPIO_MAX_BANK]; |
| 27 | int bank_input[XILINX_GPIO_MAX_BANK]; |
| 28 | int bank_output[XILINX_GPIO_MAX_BANK]; |
Michal Simek | 810e4bc | 2018-07-23 12:40:36 +0200 | [diff] [blame] | 29 | u32 dout_default[XILINX_GPIO_MAX_BANK]; |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
Michal Simek | 810e4bc | 2018-07-23 12:40:36 +0200 | [diff] [blame] | 32 | struct xilinx_gpio_privdata { |
| 33 | u32 output_val[XILINX_GPIO_MAX_BANK]; |
| 34 | }; |
| 35 | |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 36 | static int xilinx_gpio_get_bank_pin(unsigned offset, u32 *bank_num, |
| 37 | u32 *bank_pin_num, struct udevice *dev) |
| 38 | { |
| 39 | struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev); |
| 40 | u32 bank, max_pins; |
| 41 | /* the first gpio is 0 not 1 */ |
| 42 | u32 pin_num = offset; |
| 43 | |
| 44 | for (bank = 0; bank < XILINX_GPIO_MAX_BANK; bank++) { |
| 45 | max_pins = platdata->bank_max[bank]; |
| 46 | if (pin_num < max_pins) { |
| 47 | debug("%s: found at bank 0x%x pin 0x%x\n", __func__, |
| 48 | bank, pin_num); |
| 49 | *bank_num = bank; |
| 50 | *bank_pin_num = pin_num; |
| 51 | return 0; |
| 52 | } |
| 53 | pin_num -= max_pins; |
| 54 | } |
| 55 | |
| 56 | return -EINVAL; |
| 57 | } |
| 58 | |
| 59 | static int xilinx_gpio_set_value(struct udevice *dev, unsigned offset, |
| 60 | int value) |
| 61 | { |
| 62 | struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev); |
Michal Simek | 810e4bc | 2018-07-23 12:40:36 +0200 | [diff] [blame] | 63 | struct xilinx_gpio_privdata *priv = dev_get_priv(dev); |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 64 | int val, ret; |
| 65 | u32 bank, pin; |
| 66 | |
| 67 | ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev); |
| 68 | if (ret) |
| 69 | return ret; |
| 70 | |
Michal Simek | 810e4bc | 2018-07-23 12:40:36 +0200 | [diff] [blame] | 71 | val = priv->output_val[bank]; |
Michal Simek | ee39afb | 2018-07-30 14:29:27 +0200 | [diff] [blame] | 72 | |
Michal Simek | 810e4bc | 2018-07-23 12:40:36 +0200 | [diff] [blame] | 73 | debug("%s: regs: %lx, value: %x, gpio: %x, bank %x, pin %x, out %x\n", |
| 74 | __func__, (ulong)platdata->regs, value, offset, bank, pin, val); |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 75 | |
Michal Simek | ee39afb | 2018-07-30 14:29:27 +0200 | [diff] [blame] | 76 | if (value) |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 77 | val = val | (1 << pin); |
Michal Simek | ee39afb | 2018-07-30 14:29:27 +0200 | [diff] [blame] | 78 | else |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 79 | val = val & ~(1 << pin); |
Michal Simek | ee39afb | 2018-07-30 14:29:27 +0200 | [diff] [blame] | 80 | |
| 81 | writel(val, &platdata->regs->gpiodata + bank * 2); |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 82 | |
Michal Simek | 810e4bc | 2018-07-23 12:40:36 +0200 | [diff] [blame] | 83 | priv->output_val[bank] = val; |
| 84 | |
Michal Simek | 7fa52c2 | 2018-08-06 07:42:40 +0200 | [diff] [blame] | 85 | return 0; |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | static int xilinx_gpio_get_value(struct udevice *dev, unsigned offset) |
| 89 | { |
| 90 | struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev); |
Michal Simek | 810e4bc | 2018-07-23 12:40:36 +0200 | [diff] [blame] | 91 | struct xilinx_gpio_privdata *priv = dev_get_priv(dev); |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 92 | int val, ret; |
| 93 | u32 bank, pin; |
| 94 | |
| 95 | ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev); |
| 96 | if (ret) |
| 97 | return ret; |
| 98 | |
| 99 | debug("%s: regs: %lx, gpio: %x, bank %x, pin %x\n", __func__, |
| 100 | (ulong)platdata->regs, offset, bank, pin); |
| 101 | |
Michal Simek | 810e4bc | 2018-07-23 12:40:36 +0200 | [diff] [blame] | 102 | if (platdata->bank_output[bank]) { |
| 103 | debug("%s: Read saved output value\n", __func__); |
| 104 | val = priv->output_val[bank]; |
| 105 | } else { |
| 106 | debug("%s: Read input value from reg\n", __func__); |
| 107 | val = readl(&platdata->regs->gpiodata + bank * 2); |
| 108 | } |
| 109 | |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 110 | val = !!(val & (1 << pin)); |
| 111 | |
| 112 | return val; |
| 113 | }; |
| 114 | |
| 115 | static int xilinx_gpio_get_function(struct udevice *dev, unsigned offset) |
| 116 | { |
| 117 | struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev); |
| 118 | int val, ret; |
| 119 | u32 bank, pin; |
| 120 | |
Michal Simek | 9260d99 | 2018-07-23 12:08:49 +0200 | [diff] [blame] | 121 | ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev); |
| 122 | if (ret) |
| 123 | return ret; |
| 124 | |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 125 | /* Check if all pins are inputs */ |
| 126 | if (platdata->bank_input[bank]) |
| 127 | return GPIOF_INPUT; |
| 128 | |
| 129 | /* Check if all pins are outputs */ |
| 130 | if (platdata->bank_output[bank]) |
| 131 | return GPIOF_OUTPUT; |
| 132 | |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 133 | /* FIXME test on dual */ |
| 134 | val = readl(&platdata->regs->gpiodir + bank * 2); |
| 135 | val = !(val & (1 << pin)); |
| 136 | |
| 137 | /* input is 1 in reg but GPIOF_INPUT is 0 */ |
| 138 | /* output is 0 in reg but GPIOF_OUTPUT is 1 */ |
| 139 | |
| 140 | return val; |
| 141 | } |
| 142 | |
| 143 | static int xilinx_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 144 | int value) |
| 145 | { |
| 146 | struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev); |
| 147 | int val, ret; |
| 148 | u32 bank, pin; |
| 149 | |
| 150 | ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev); |
| 151 | if (ret) |
| 152 | return ret; |
| 153 | |
| 154 | /* can't change it if all is input by default */ |
| 155 | if (platdata->bank_input[bank]) |
| 156 | return -EINVAL; |
| 157 | |
Michal Simek | c2116f3 | 2018-07-30 10:02:53 +0200 | [diff] [blame] | 158 | xilinx_gpio_set_value(dev, offset, value); |
| 159 | |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 160 | if (!platdata->bank_output[bank]) { |
| 161 | val = readl(&platdata->regs->gpiodir + bank * 2); |
| 162 | val = val & ~(1 << pin); |
| 163 | writel(val, &platdata->regs->gpiodir + bank * 2); |
| 164 | } |
| 165 | |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static int xilinx_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 170 | { |
| 171 | struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev); |
| 172 | int val, ret; |
| 173 | u32 bank, pin; |
| 174 | |
| 175 | ret = xilinx_gpio_get_bank_pin(offset, &bank, &pin, dev); |
| 176 | if (ret) |
| 177 | return ret; |
| 178 | |
| 179 | /* Already input */ |
| 180 | if (platdata->bank_input[bank]) |
| 181 | return 0; |
| 182 | |
| 183 | /* can't change it if all is output by default */ |
| 184 | if (platdata->bank_output[bank]) |
| 185 | return -EINVAL; |
| 186 | |
| 187 | val = readl(&platdata->regs->gpiodir + bank * 2); |
| 188 | val = val | (1 << pin); |
| 189 | writel(val, &platdata->regs->gpiodir + bank * 2); |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int xilinx_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, |
| 195 | struct ofnode_phandle_args *args) |
| 196 | { |
| 197 | struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev); |
| 198 | |
| 199 | desc->offset = args->args[0]; |
| 200 | |
| 201 | debug("%s: argc: %x, [0]: %x, [1]: %x, [2]: %x\n", __func__, |
| 202 | args->args_count, args->args[0], args->args[1], args->args[2]); |
| 203 | |
| 204 | /* |
| 205 | * The second cell is channel offset: |
| 206 | * 0 is first channel, 8 is second channel |
| 207 | * |
| 208 | * U-Boot driver just combine channels together that's why simply |
| 209 | * add amount of pins in second channel if present. |
| 210 | */ |
| 211 | if (args->args[1]) { |
| 212 | if (!platdata->bank_max[1]) { |
| 213 | printf("%s: %s has no second channel\n", |
| 214 | __func__, dev->name); |
| 215 | return -EINVAL; |
| 216 | } |
| 217 | |
| 218 | desc->offset += platdata->bank_max[0]; |
| 219 | } |
| 220 | |
| 221 | /* The third cell is optional */ |
| 222 | if (args->args_count > 2) |
| 223 | desc->flags = (args->args[2] & |
| 224 | GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0); |
| 225 | |
| 226 | debug("%s: offset %x, flags %lx\n", |
| 227 | __func__, desc->offset, desc->flags); |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static const struct dm_gpio_ops xilinx_gpio_ops = { |
| 232 | .direction_input = xilinx_gpio_direction_input, |
| 233 | .direction_output = xilinx_gpio_direction_output, |
| 234 | .get_value = xilinx_gpio_get_value, |
| 235 | .set_value = xilinx_gpio_set_value, |
| 236 | .get_function = xilinx_gpio_get_function, |
| 237 | .xlate = xilinx_gpio_xlate, |
| 238 | }; |
| 239 | |
| 240 | static int xilinx_gpio_probe(struct udevice *dev) |
| 241 | { |
| 242 | struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev); |
Michal Simek | 810e4bc | 2018-07-23 12:40:36 +0200 | [diff] [blame] | 243 | struct xilinx_gpio_privdata *priv = dev_get_priv(dev); |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 244 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Michal Simek | 305f1c5 | 2018-08-02 12:58:54 +0200 | [diff] [blame] | 245 | const void *label_ptr; |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 246 | |
Michal Simek | 305f1c5 | 2018-08-02 12:58:54 +0200 | [diff] [blame] | 247 | label_ptr = dev_read_prop(dev, "label", NULL); |
| 248 | if (label_ptr) { |
| 249 | uc_priv->bank_name = strdup(label_ptr); |
| 250 | if (!uc_priv->bank_name) |
| 251 | return -ENOMEM; |
| 252 | } else { |
| 253 | uc_priv->bank_name = dev->name; |
| 254 | } |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 255 | |
| 256 | uc_priv->gpio_count = platdata->bank_max[0] + platdata->bank_max[1]; |
| 257 | |
Michal Simek | 810e4bc | 2018-07-23 12:40:36 +0200 | [diff] [blame] | 258 | priv->output_val[0] = platdata->dout_default[0]; |
| 259 | |
| 260 | if (platdata->bank_max[1]) |
| 261 | priv->output_val[1] = platdata->dout_default[1]; |
| 262 | |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | static int xilinx_gpio_ofdata_to_platdata(struct udevice *dev) |
| 267 | { |
| 268 | struct xilinx_gpio_platdata *platdata = dev_get_platdata(dev); |
| 269 | int is_dual; |
| 270 | |
| 271 | platdata->regs = (struct gpio_regs *)dev_read_addr(dev); |
| 272 | |
| 273 | platdata->bank_max[0] = dev_read_u32_default(dev, |
| 274 | "xlnx,gpio-width", 0); |
| 275 | platdata->bank_input[0] = dev_read_u32_default(dev, |
| 276 | "xlnx,all-inputs", 0); |
| 277 | platdata->bank_output[0] = dev_read_u32_default(dev, |
| 278 | "xlnx,all-outputs", 0); |
Michal Simek | 810e4bc | 2018-07-23 12:40:36 +0200 | [diff] [blame] | 279 | platdata->dout_default[0] = dev_read_u32_default(dev, |
| 280 | "xlnx,dout-default", |
| 281 | 0); |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 282 | |
| 283 | is_dual = dev_read_u32_default(dev, "xlnx,is-dual", 0); |
| 284 | if (is_dual) { |
| 285 | platdata->bank_max[1] = dev_read_u32_default(dev, |
| 286 | "xlnx,gpio2-width", 0); |
| 287 | platdata->bank_input[1] = dev_read_u32_default(dev, |
| 288 | "xlnx,all-inputs-2", 0); |
| 289 | platdata->bank_output[1] = dev_read_u32_default(dev, |
| 290 | "xlnx,all-outputs-2", 0); |
Michal Simek | 810e4bc | 2018-07-23 12:40:36 +0200 | [diff] [blame] | 291 | platdata->dout_default[1] = dev_read_u32_default(dev, |
| 292 | "xlnx,dout-default-2", 0); |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static const struct udevice_id xilinx_gpio_ids[] = { |
| 299 | { .compatible = "xlnx,xps-gpio-1.00.a",}, |
| 300 | { } |
| 301 | }; |
| 302 | |
| 303 | U_BOOT_DRIVER(xilinx_gpio) = { |
| 304 | .name = "xlnx_gpio", |
| 305 | .id = UCLASS_GPIO, |
| 306 | .ops = &xilinx_gpio_ops, |
| 307 | .of_match = xilinx_gpio_ids, |
| 308 | .ofdata_to_platdata = xilinx_gpio_ofdata_to_platdata, |
| 309 | .probe = xilinx_gpio_probe, |
| 310 | .platdata_auto_alloc_size = sizeof(struct xilinx_gpio_platdata), |
Michal Simek | 810e4bc | 2018-07-23 12:40:36 +0200 | [diff] [blame] | 311 | .priv_auto_alloc_size = sizeof(struct xilinx_gpio_privdata), |
Michal Simek | 6887797 | 2018-07-12 16:05:46 +0200 | [diff] [blame] | 312 | }; |