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