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 | |
| 28 | struct mpc85xx_gpio_data { |
| 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 |
| 37 | * for output pins */ |
| 38 | u32 dat_shadow; |
| 39 | }; |
| 40 | |
| 41 | inline u32 gpio_mask(unsigned gpio) { |
| 42 | return (1U << (31 - (gpio))); |
| 43 | } |
| 44 | |
| 45 | static inline u32 mpc85xx_gpio_get_val(struct ccsr_gpio *base, u32 mask) |
| 46 | { |
| 47 | return in_be32(&base->gpdat) & mask; |
| 48 | } |
| 49 | |
| 50 | static inline u32 mpc85xx_gpio_get_dir(struct ccsr_gpio *base, u32 mask) |
| 51 | { |
| 52 | return in_be32(&base->gpdir) & mask; |
| 53 | } |
| 54 | |
| 55 | static inline void mpc85xx_gpio_set_in(struct ccsr_gpio *base, u32 gpios) |
| 56 | { |
| 57 | clrbits_be32(&base->gpdat, gpios); |
| 58 | /* GPDIR register 0 -> input */ |
| 59 | clrbits_be32(&base->gpdir, gpios); |
| 60 | } |
| 61 | |
| 62 | static inline void mpc85xx_gpio_set_low(struct ccsr_gpio *base, u32 gpios) |
| 63 | { |
| 64 | clrbits_be32(&base->gpdat, gpios); |
| 65 | /* GPDIR register 1 -> output */ |
| 66 | setbits_be32(&base->gpdir, gpios); |
| 67 | } |
| 68 | |
| 69 | static inline void mpc85xx_gpio_set_high(struct ccsr_gpio *base, u32 gpios) |
| 70 | { |
| 71 | setbits_be32(&base->gpdat, gpios); |
| 72 | /* GPDIR register 1 -> output */ |
| 73 | setbits_be32(&base->gpdir, gpios); |
| 74 | } |
| 75 | |
mario.six@gdsys.cc | 7b4cf8b | 2016-05-25 15:15:22 +0200 | [diff] [blame] | 76 | static inline int mpc85xx_gpio_open_drain_val(struct ccsr_gpio *base, u32 mask) |
| 77 | { |
| 78 | return in_be32(&base->gpodr) & mask; |
| 79 | } |
| 80 | |
| 81 | static inline void mpc85xx_gpio_open_drain_on(struct ccsr_gpio *base, u32 |
| 82 | gpios) |
| 83 | { |
| 84 | /* GPODR register 1 -> open drain on */ |
| 85 | setbits_be32(&base->gpodr, gpios); |
| 86 | } |
| 87 | |
| 88 | static inline void mpc85xx_gpio_open_drain_off(struct ccsr_gpio *base, |
| 89 | u32 gpios) |
| 90 | { |
| 91 | /* GPODR register 0 -> open drain off (actively driven) */ |
| 92 | clrbits_be32(&base->gpodr, gpios); |
| 93 | } |
| 94 | |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 95 | static int mpc85xx_gpio_direction_input(struct udevice *dev, unsigned gpio) |
| 96 | { |
| 97 | struct mpc85xx_gpio_data *data = dev_get_priv(dev); |
| 98 | |
| 99 | mpc85xx_gpio_set_in(data->base, gpio_mask(gpio)); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int mpc85xx_gpio_set_value(struct udevice *dev, unsigned gpio, |
| 104 | int value) |
| 105 | { |
| 106 | struct mpc85xx_gpio_data *data = dev_get_priv(dev); |
| 107 | |
| 108 | if (value) { |
| 109 | data->dat_shadow |= gpio_mask(gpio); |
| 110 | mpc85xx_gpio_set_high(data->base, gpio_mask(gpio)); |
| 111 | } else { |
| 112 | data->dat_shadow &= ~gpio_mask(gpio); |
| 113 | mpc85xx_gpio_set_low(data->base, gpio_mask(gpio)); |
| 114 | } |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int mpc85xx_gpio_direction_output(struct udevice *dev, unsigned gpio, |
| 119 | int value) |
| 120 | { |
| 121 | return mpc85xx_gpio_set_value(dev, gpio, value); |
| 122 | } |
| 123 | |
| 124 | static int mpc85xx_gpio_get_value(struct udevice *dev, unsigned gpio) |
| 125 | { |
| 126 | struct mpc85xx_gpio_data *data = dev_get_priv(dev); |
| 127 | |
| 128 | if (!!mpc85xx_gpio_get_dir(data->base, gpio_mask(gpio))) { |
| 129 | /* Output -> use shadowed value */ |
| 130 | return !!(data->dat_shadow & gpio_mask(gpio)); |
| 131 | } else { |
| 132 | /* Input -> read value from GPDAT register */ |
| 133 | return !!mpc85xx_gpio_get_val(data->base, gpio_mask(gpio)); |
| 134 | } |
| 135 | } |
| 136 | |
mario.six@gdsys.cc | 7b4cf8b | 2016-05-25 15:15:22 +0200 | [diff] [blame] | 137 | static int mpc85xx_gpio_get_open_drain(struct udevice *dev, unsigned gpio) |
| 138 | { |
| 139 | struct mpc85xx_gpio_data *data = dev_get_priv(dev); |
| 140 | |
| 141 | return !!mpc85xx_gpio_open_drain_val(data->base, gpio_mask(gpio)); |
| 142 | } |
| 143 | |
| 144 | static int mpc85xx_gpio_set_open_drain(struct udevice *dev, unsigned gpio, |
| 145 | int value) |
| 146 | { |
| 147 | struct mpc85xx_gpio_data *data = dev_get_priv(dev); |
| 148 | |
| 149 | if (value) { |
| 150 | mpc85xx_gpio_open_drain_on(data->base, gpio_mask(gpio)); |
| 151 | } else { |
| 152 | mpc85xx_gpio_open_drain_off(data->base, gpio_mask(gpio)); |
| 153 | } |
| 154 | return 0; |
| 155 | } |
| 156 | |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 157 | static int mpc85xx_gpio_get_function(struct udevice *dev, unsigned gpio) |
| 158 | { |
| 159 | struct mpc85xx_gpio_data *data = dev_get_priv(dev); |
| 160 | int dir; |
| 161 | |
| 162 | dir = !!mpc85xx_gpio_get_dir(data->base, gpio_mask(gpio)); |
| 163 | return dir ? GPIOF_OUTPUT : GPIOF_INPUT; |
| 164 | } |
| 165 | |
| 166 | static int mpc85xx_gpio_ofdata_to_platdata(struct udevice *dev) { |
| 167 | struct mpc85xx_gpio_data *data = dev_get_priv(dev); |
| 168 | fdt_addr_t addr; |
| 169 | fdt_size_t size; |
| 170 | |
| 171 | addr = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, dev->of_offset, |
| 172 | "reg", 0, &size); |
| 173 | |
| 174 | data->addr = addr; |
| 175 | data->base = map_sysmem(CONFIG_SYS_IMMR + addr, size); |
| 176 | |
| 177 | if (!data->base) |
| 178 | return -ENOMEM; |
| 179 | |
| 180 | data->gpio_count = fdtdec_get_int(gd->fdt_blob, dev->of_offset, |
| 181 | "ngpios", 32); |
| 182 | data->dat_shadow = 0; |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static int mpc85xx_gpio_probe(struct udevice *dev) |
| 188 | { |
| 189 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 190 | struct mpc85xx_gpio_data *data = dev_get_priv(dev); |
| 191 | char name[32], *str; |
| 192 | |
| 193 | snprintf(name, sizeof(name), "MPC@%lx_", data->addr); |
| 194 | str = strdup(name); |
| 195 | |
| 196 | if (!str) |
| 197 | return -ENOMEM; |
| 198 | |
| 199 | uc_priv->bank_name = str; |
| 200 | uc_priv->gpio_count = data->gpio_count; |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static const struct dm_gpio_ops gpio_mpc85xx_ops = { |
| 206 | .direction_input = mpc85xx_gpio_direction_input, |
| 207 | .direction_output = mpc85xx_gpio_direction_output, |
| 208 | .get_value = mpc85xx_gpio_get_value, |
| 209 | .set_value = mpc85xx_gpio_set_value, |
mario.six@gdsys.cc | 7b4cf8b | 2016-05-25 15:15:22 +0200 | [diff] [blame] | 210 | .get_open_drain = mpc85xx_gpio_get_open_drain, |
| 211 | .set_open_drain = mpc85xx_gpio_set_open_drain, |
mario.six@gdsys.cc | 5b59a35 | 2016-05-25 15:15:20 +0200 | [diff] [blame] | 212 | .get_function = mpc85xx_gpio_get_function, |
| 213 | }; |
| 214 | |
| 215 | static const struct udevice_id mpc85xx_gpio_ids[] = { |
| 216 | { .compatible = "fsl,pq3-gpio" }, |
| 217 | { /* sentinel */ } |
| 218 | }; |
| 219 | |
| 220 | U_BOOT_DRIVER(gpio_mpc85xx) = { |
| 221 | .name = "gpio_mpc85xx", |
| 222 | .id = UCLASS_GPIO, |
| 223 | .ops = &gpio_mpc85xx_ops, |
| 224 | .ofdata_to_platdata = mpc85xx_gpio_ofdata_to_platdata, |
| 225 | .of_match = mpc85xx_gpio_ids, |
| 226 | .probe = mpc85xx_gpio_probe, |
| 227 | .priv_auto_alloc_size = sizeof(struct mpc85xx_gpio_data), |
| 228 | }; |