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