mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2016 |
| 3 | * Mario Six, Guntermann & Drunck GmbH, six@gdsys.de |
| 4 | * |
| 5 | * based on arch/powerpc/include/asm/mpc85xx_gpio.h, which is |
| 6 | * |
| 7 | * Copyright 2010 eXMeritus, A Boeing Company |
| 8 | * |
| 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <dm.h> |
| 14 | #include <asm/gpio.h> |
| 15 | #include <mapmem.h> |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | struct ccsr_gpio { |
| 20 | u32 gpdir; |
| 21 | u32 gpodr; |
| 22 | u32 gpdat; |
| 23 | u32 gpier; |
| 24 | u32 gpimr; |
| 25 | u32 gpicr; |
| 26 | }; |
| 27 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 28 | struct mpc8xxx_gpio_data { |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 29 | /* The bank's register base in memory */ |
| 30 | struct ccsr_gpio __iomem *base; |
| 31 | /* The address of the registers; used to identify the bank */ |
| 32 | ulong addr; |
| 33 | /* The GPIO count of the bank */ |
| 34 | uint gpio_count; |
| 35 | /* The GPDAT register cannot be used to determine the value of output |
| 36 | * pins on MPC8572/MPC8536, so we shadow it and use the shadowed value |
Mario Six | cc94b17 | 2018-01-15 11:07:46 +0100 | [diff] [blame] | 37 | * for output pins |
| 38 | */ |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 39 | u32 dat_shadow; |
| 40 | }; |
| 41 | |
Mario Six | cc94b17 | 2018-01-15 11:07:46 +0100 | [diff] [blame] | 42 | inline u32 gpio_mask(uint gpio) |
| 43 | { |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 44 | return (1U << (31 - (gpio))); |
| 45 | } |
| 46 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 47 | static inline u32 mpc8xxx_gpio_get_val(struct ccsr_gpio *base, u32 mask) |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 48 | { |
| 49 | return in_be32(&base->gpdat) & mask; |
| 50 | } |
| 51 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 52 | static inline u32 mpc8xxx_gpio_get_dir(struct ccsr_gpio *base, u32 mask) |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 53 | { |
| 54 | return in_be32(&base->gpdir) & mask; |
| 55 | } |
| 56 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 57 | static inline void mpc8xxx_gpio_set_in(struct ccsr_gpio *base, u32 gpios) |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 58 | { |
| 59 | clrbits_be32(&base->gpdat, gpios); |
| 60 | /* GPDIR register 0 -> input */ |
| 61 | clrbits_be32(&base->gpdir, gpios); |
| 62 | } |
| 63 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 64 | static inline void mpc8xxx_gpio_set_low(struct ccsr_gpio *base, u32 gpios) |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 65 | { |
| 66 | clrbits_be32(&base->gpdat, gpios); |
| 67 | /* GPDIR register 1 -> output */ |
| 68 | setbits_be32(&base->gpdir, gpios); |
| 69 | } |
| 70 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 71 | static inline void mpc8xxx_gpio_set_high(struct ccsr_gpio *base, u32 gpios) |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 72 | { |
| 73 | setbits_be32(&base->gpdat, gpios); |
| 74 | /* GPDIR register 1 -> output */ |
| 75 | setbits_be32(&base->gpdir, gpios); |
| 76 | } |
| 77 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 78 | static inline int mpc8xxx_gpio_open_drain_val(struct ccsr_gpio *base, u32 mask) |
mario.six@gdsys.cc | 7b4cf8b | 2016-05-25 15:15:22 +0200 | [diff] [blame] | 79 | { |
| 80 | return in_be32(&base->gpodr) & mask; |
| 81 | } |
| 82 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 83 | static inline void mpc8xxx_gpio_open_drain_on(struct ccsr_gpio *base, u32 |
mario.six@gdsys.cc | 7b4cf8b | 2016-05-25 15:15:22 +0200 | [diff] [blame] | 84 | gpios) |
| 85 | { |
| 86 | /* GPODR register 1 -> open drain on */ |
| 87 | setbits_be32(&base->gpodr, gpios); |
| 88 | } |
| 89 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 90 | static inline void mpc8xxx_gpio_open_drain_off(struct ccsr_gpio *base, |
mario.six@gdsys.cc | 7b4cf8b | 2016-05-25 15:15:22 +0200 | [diff] [blame] | 91 | u32 gpios) |
| 92 | { |
| 93 | /* GPODR register 0 -> open drain off (actively driven) */ |
| 94 | clrbits_be32(&base->gpodr, gpios); |
| 95 | } |
| 96 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 97 | static int mpc8xxx_gpio_direction_input(struct udevice *dev, uint gpio) |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 98 | { |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 99 | struct mpc8xxx_gpio_data *data = dev_get_priv(dev); |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 100 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 101 | mpc8xxx_gpio_set_in(data->base, gpio_mask(gpio)); |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 105 | static int mpc8xxx_gpio_set_value(struct udevice *dev, uint gpio, int value) |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 106 | { |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 107 | struct mpc8xxx_gpio_data *data = dev_get_priv(dev); |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 108 | |
| 109 | if (value) { |
| 110 | data->dat_shadow |= gpio_mask(gpio); |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 111 | mpc8xxx_gpio_set_high(data->base, gpio_mask(gpio)); |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 112 | } else { |
| 113 | data->dat_shadow &= ~gpio_mask(gpio); |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 114 | mpc8xxx_gpio_set_low(data->base, gpio_mask(gpio)); |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 119 | static int mpc8xxx_gpio_direction_output(struct udevice *dev, uint gpio, |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 120 | int value) |
| 121 | { |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 122 | return mpc8xxx_gpio_set_value(dev, gpio, value); |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 125 | static int mpc8xxx_gpio_get_value(struct udevice *dev, uint gpio) |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 126 | { |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 127 | struct mpc8xxx_gpio_data *data = dev_get_priv(dev); |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 128 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 129 | if (!!mpc8xxx_gpio_get_dir(data->base, gpio_mask(gpio))) { |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 130 | /* Output -> use shadowed value */ |
| 131 | return !!(data->dat_shadow & gpio_mask(gpio)); |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 132 | } |
Mario Six | cc94b17 | 2018-01-15 11:07:46 +0100 | [diff] [blame] | 133 | |
| 134 | /* Input -> read value from GPDAT register */ |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 135 | return !!mpc8xxx_gpio_get_val(data->base, gpio_mask(gpio)); |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 138 | static int mpc8xxx_gpio_get_open_drain(struct udevice *dev, uint gpio) |
mario.six@gdsys.cc | 7b4cf8b | 2016-05-25 15:15:22 +0200 | [diff] [blame] | 139 | { |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 140 | struct mpc8xxx_gpio_data *data = dev_get_priv(dev); |
mario.six@gdsys.cc | 7b4cf8b | 2016-05-25 15:15:22 +0200 | [diff] [blame] | 141 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 142 | return !!mpc8xxx_gpio_open_drain_val(data->base, gpio_mask(gpio)); |
mario.six@gdsys.cc | 7b4cf8b | 2016-05-25 15:15:22 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 145 | static int mpc8xxx_gpio_set_open_drain(struct udevice *dev, uint gpio, |
mario.six@gdsys.cc | 7b4cf8b | 2016-05-25 15:15:22 +0200 | [diff] [blame] | 146 | int value) |
| 147 | { |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 148 | struct mpc8xxx_gpio_data *data = dev_get_priv(dev); |
mario.six@gdsys.cc | 7b4cf8b | 2016-05-25 15:15:22 +0200 | [diff] [blame] | 149 | |
Mario Six | cc94b17 | 2018-01-15 11:07:46 +0100 | [diff] [blame] | 150 | if (value) |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 151 | mpc8xxx_gpio_open_drain_on(data->base, gpio_mask(gpio)); |
Mario Six | cc94b17 | 2018-01-15 11:07:46 +0100 | [diff] [blame] | 152 | else |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 153 | mpc8xxx_gpio_open_drain_off(data->base, gpio_mask(gpio)); |
Mario Six | cc94b17 | 2018-01-15 11:07:46 +0100 | [diff] [blame] | 154 | |
mario.six@gdsys.cc | 7b4cf8b | 2016-05-25 15:15:22 +0200 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 158 | static int mpc8xxx_gpio_get_function(struct udevice *dev, uint gpio) |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 159 | { |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 160 | struct mpc8xxx_gpio_data *data = dev_get_priv(dev); |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 161 | int dir; |
| 162 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 163 | dir = !!mpc8xxx_gpio_get_dir(data->base, gpio_mask(gpio)); |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 164 | return dir ? GPIOF_OUTPUT : GPIOF_INPUT; |
| 165 | } |
| 166 | |
Hamish Martin | 47ca9fc | 2016-06-14 10:17:05 +1200 | [diff] [blame] | 167 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 168 | static int mpc8xxx_gpio_ofdata_to_platdata(struct udevice *dev) |
Mario Six | cc94b17 | 2018-01-15 11:07:46 +0100 | [diff] [blame] | 169 | { |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 170 | struct mpc8xxx_gpio_plat *plat = dev_get_platdata(dev); |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 171 | fdt_addr_t addr; |
| 172 | fdt_size_t size; |
| 173 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 174 | addr = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, |
Mario Six | cc94b17 | 2018-01-15 11:07:46 +0100 | [diff] [blame] | 175 | dev_of_offset(dev), |
| 176 | "reg", 0, &size, false); |
Hamish Martin | 47ca9fc | 2016-06-14 10:17:05 +1200 | [diff] [blame] | 177 | plat->addr = addr; |
| 178 | plat->size = size; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 179 | plat->ngpios = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
| 180 | "ngpios", 32); |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 181 | |
Hamish Martin | 47ca9fc | 2016-06-14 10:17:05 +1200 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | #endif |
| 185 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 186 | static int mpc8xxx_gpio_platdata_to_priv(struct udevice *dev) |
Hamish Martin | 47ca9fc | 2016-06-14 10:17:05 +1200 | [diff] [blame] | 187 | { |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 188 | struct mpc8xxx_gpio_data *priv = dev_get_priv(dev); |
| 189 | struct mpc8xxx_gpio_plat *plat = dev_get_platdata(dev); |
Hamish Martin | 47ca9fc | 2016-06-14 10:17:05 +1200 | [diff] [blame] | 190 | unsigned long size = plat->size; |
| 191 | |
| 192 | if (size == 0) |
| 193 | size = 0x100; |
| 194 | |
| 195 | priv->addr = plat->addr; |
| 196 | priv->base = map_sysmem(CONFIG_SYS_IMMR + plat->addr, size); |
| 197 | |
| 198 | if (!priv->base) |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 199 | return -ENOMEM; |
| 200 | |
Hamish Martin | 47ca9fc | 2016-06-14 10:17:05 +1200 | [diff] [blame] | 201 | priv->gpio_count = plat->ngpios; |
| 202 | priv->dat_shadow = 0; |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 203 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 204 | priv->type = driver_data; |
| 205 | |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 209 | static int mpc8xxx_gpio_probe(struct udevice *dev) |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 210 | { |
| 211 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 212 | struct mpc8xxx_gpio_data *data = dev_get_priv(dev); |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 213 | char name[32], *str; |
| 214 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 215 | mpc8xxx_gpio_platdata_to_priv(dev); |
Hamish Martin | 47ca9fc | 2016-06-14 10:17:05 +1200 | [diff] [blame] | 216 | |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 217 | snprintf(name, sizeof(name), "MPC@%lx_", data->addr); |
| 218 | str = strdup(name); |
| 219 | |
| 220 | if (!str) |
| 221 | return -ENOMEM; |
| 222 | |
| 223 | uc_priv->bank_name = str; |
| 224 | uc_priv->gpio_count = data->gpio_count; |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 229 | static const struct dm_gpio_ops gpio_mpc8xxx_ops = { |
| 230 | .direction_input = mpc8xxx_gpio_direction_input, |
| 231 | .direction_output = mpc8xxx_gpio_direction_output, |
| 232 | .get_value = mpc8xxx_gpio_get_value, |
| 233 | .set_value = mpc8xxx_gpio_set_value, |
| 234 | .get_open_drain = mpc8xxx_gpio_get_open_drain, |
| 235 | .set_open_drain = mpc8xxx_gpio_set_open_drain, |
| 236 | .get_function = mpc8xxx_gpio_get_function, |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 237 | }; |
| 238 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 239 | static const struct udevice_id mpc8xxx_gpio_ids[] = { |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 240 | { .compatible = "fsl,pq3-gpio" }, |
| 241 | { /* sentinel */ } |
| 242 | }; |
| 243 | |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 244 | U_BOOT_DRIVER(gpio_mpc8xxx) = { |
| 245 | .name = "gpio_mpc8xxx", |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 246 | .id = UCLASS_GPIO, |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 247 | .ops = &gpio_mpc8xxx_ops, |
Hamish Martin | 47ca9fc | 2016-06-14 10:17:05 +1200 | [diff] [blame] | 248 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 249 | .ofdata_to_platdata = mpc8xxx_gpio_ofdata_to_platdata, |
| 250 | .platdata_auto_alloc_size = sizeof(struct mpc8xxx_gpio_plat), |
| 251 | .of_match = mpc8xxx_gpio_ids, |
Hamish Martin | 47ca9fc | 2016-06-14 10:17:05 +1200 | [diff] [blame] | 252 | #endif |
Mario Six | d006f68 | 2018-01-15 11:07:48 +0100 | [diff] [blame^] | 253 | .probe = mpc8xxx_gpio_probe, |
| 254 | .priv_auto_alloc_size = sizeof(struct mpc8xxx_gpio_data), |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 255 | }; |