1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2015 IBM Corp.
4 * Joel Stanley <joel@jms.id.au>
5 * Ryan Chen <ryan_chen@aspeedtech.com>
7 * Implementation extracted from the Linux kernel and adapted for u-boot.
18 #include <linux/bug.h>
19 #include <linux/sizes.h>
21 struct aspeed_gpio_priv {
25 struct aspeed_gpio_bank {
26 u16 val_regs; /* +0: Rd: read input value, Wr: set write latch
27 * +4: Rd/Wr: Direction (0=in, 1=out)
29 u16 rdata_reg; /* Rd: read write latch, Wr: <none> */
34 const char names[4][3];
37 static const struct aspeed_gpio_bank aspeed_gpio_banks[] = {
42 .debounce_regs = 0x0040,
43 .tolerance_regs = 0x001c,
44 .cmdsrc_regs = 0x0060,
45 .names = { "A", "B", "C", "D" },
51 .debounce_regs = 0x0048,
52 .tolerance_regs = 0x003c,
53 .cmdsrc_regs = 0x0068,
54 .names = { "E", "F", "G", "H" },
60 .debounce_regs = 0x00b0,
61 .tolerance_regs = 0x00ac,
62 .cmdsrc_regs = 0x0090,
63 .names = { "I", "J", "K", "L" },
69 .debounce_regs = 0x0100,
70 .tolerance_regs = 0x00fc,
71 .cmdsrc_regs = 0x00e0,
72 .names = { "M", "N", "O", "P" },
78 .debounce_regs = 0x0130,
79 .tolerance_regs = 0x012c,
80 .cmdsrc_regs = 0x0110,
81 .names = { "Q", "R", "S", "T" },
87 .debounce_regs = 0x0160,
88 .tolerance_regs = 0x015c,
89 .cmdsrc_regs = 0x0140,
90 .names = { "U", "V", "W", "X" },
96 .debounce_regs = 0x0190,
97 .tolerance_regs = 0x018c,
98 .cmdsrc_regs = 0x0170,
99 .names = { "Y", "Z", "AA", "AB" },
105 .debounce_regs = 0x01c0,
106 .tolerance_regs = 0x01bc,
107 .cmdsrc_regs = 0x01a0,
108 .names = { "AC", "", "", "" },
112 enum aspeed_gpio_reg {
128 #define GPIO_VAL_VALUE 0x00
129 #define GPIO_VAL_DIR 0x04
131 #define GPIO_IRQ_ENABLE 0x00
132 #define GPIO_IRQ_TYPE0 0x04
133 #define GPIO_IRQ_TYPE1 0x08
134 #define GPIO_IRQ_TYPE2 0x0c
135 #define GPIO_IRQ_STATUS 0x10
137 #define GPIO_DEBOUNCE_SEL1 0x00
138 #define GPIO_DEBOUNCE_SEL2 0x04
140 #define GPIO_CMDSRC_0 0x00
141 #define GPIO_CMDSRC_1 0x04
142 #define GPIO_CMDSRC_ARM 0
143 #define GPIO_CMDSRC_LPC 1
144 #define GPIO_CMDSRC_COLDFIRE 2
145 #define GPIO_CMDSRC_RESERVED 3
147 /* This will be resolved at compile time */
148 static inline void __iomem *bank_reg(struct aspeed_gpio_priv *gpio,
149 const struct aspeed_gpio_bank *bank,
150 const enum aspeed_gpio_reg reg)
154 return gpio->regs + bank->val_regs + GPIO_VAL_VALUE;
156 return gpio->regs + bank->rdata_reg;
158 return gpio->regs + bank->val_regs + GPIO_VAL_DIR;
160 return gpio->regs + bank->irq_regs + GPIO_IRQ_ENABLE;
162 return gpio->regs + bank->irq_regs + GPIO_IRQ_TYPE0;
164 return gpio->regs + bank->irq_regs + GPIO_IRQ_TYPE1;
166 return gpio->regs + bank->irq_regs + GPIO_IRQ_TYPE2;
168 return gpio->regs + bank->irq_regs + GPIO_IRQ_STATUS;
169 case reg_debounce_sel1:
170 return gpio->regs + bank->debounce_regs + GPIO_DEBOUNCE_SEL1;
171 case reg_debounce_sel2:
172 return gpio->regs + bank->debounce_regs + GPIO_DEBOUNCE_SEL2;
174 return gpio->regs + bank->tolerance_regs;
176 return gpio->regs + bank->cmdsrc_regs + GPIO_CMDSRC_0;
178 return gpio->regs + bank->cmdsrc_regs + GPIO_CMDSRC_1;
183 #define GPIO_BANK(x) ((x) >> 5)
184 #define GPIO_OFFSET(x) ((x) & 0x1f)
185 #define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
187 static const struct aspeed_gpio_bank *to_bank(unsigned int offset)
189 unsigned int bank = GPIO_BANK(offset);
191 WARN_ON(bank >= ARRAY_SIZE(aspeed_gpio_banks));
192 return &aspeed_gpio_banks[bank];
196 aspeed_gpio_direction_input(struct udevice *dev, unsigned int offset)
198 struct aspeed_gpio_priv *priv = dev_get_priv(dev);
199 const struct aspeed_gpio_bank *bank = to_bank(offset);
200 u32 dir = readl(bank_reg(priv, bank, reg_dir));
202 dir &= ~GPIO_BIT(offset);
203 writel(dir, bank_reg(priv, bank, reg_dir));
208 static int aspeed_gpio_direction_output(struct udevice *dev, unsigned int offset,
211 struct aspeed_gpio_priv *priv = dev_get_priv(dev);
212 const struct aspeed_gpio_bank *bank = to_bank(offset);
213 u32 dir = readl(bank_reg(priv, bank, reg_dir));
214 u32 output = readl(bank_reg(priv, bank, reg_val));
216 dir |= GPIO_BIT(offset);
217 writel(dir, bank_reg(priv, bank, reg_dir));
220 output |= GPIO_BIT(offset);
222 output &= ~GPIO_BIT(offset);
224 writel(output, bank_reg(priv, bank, reg_val));
229 static int aspeed_gpio_get_value(struct udevice *dev, unsigned int offset)
231 struct aspeed_gpio_priv *priv = dev_get_priv(dev);
232 const struct aspeed_gpio_bank *bank = to_bank(offset);
234 return !!(readl(bank_reg(priv, bank, reg_val)) & GPIO_BIT(offset));
238 aspeed_gpio_set_value(struct udevice *dev, unsigned int offset, int value)
240 struct aspeed_gpio_priv *priv = dev_get_priv(dev);
241 const struct aspeed_gpio_bank *bank = to_bank(offset);
242 u32 data = readl(bank_reg(priv, bank, reg_val));
245 data |= GPIO_BIT(offset);
247 data &= ~GPIO_BIT(offset);
249 writel(data, bank_reg(priv, bank, reg_val));
254 static int aspeed_gpio_get_function(struct udevice *dev, unsigned int offset)
256 struct aspeed_gpio_priv *priv = dev_get_priv(dev);
257 const struct aspeed_gpio_bank *bank = to_bank(offset);
259 if (readl(bank_reg(priv, bank, reg_dir)) & GPIO_BIT(offset))
265 static const struct dm_gpio_ops aspeed_gpio_ops = {
266 .direction_input = aspeed_gpio_direction_input,
267 .direction_output = aspeed_gpio_direction_output,
268 .get_value = aspeed_gpio_get_value,
269 .set_value = aspeed_gpio_set_value,
270 .get_function = aspeed_gpio_get_function,
273 static int aspeed_gpio_probe(struct udevice *dev)
275 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
276 struct aspeed_gpio_priv *priv = dev_get_priv(dev);
278 uc_priv->bank_name = dev->name;
279 ofnode_read_u32(dev_ofnode(dev), "ngpios", &uc_priv->gpio_count);
280 priv->regs = devfdt_get_addr_ptr(dev);
285 static const struct udevice_id aspeed_gpio_ids[] = {
286 { .compatible = "aspeed,ast2400-gpio", },
287 { .compatible = "aspeed,ast2500-gpio", },
288 { .compatible = "aspeed,ast2600-gpio", },
292 U_BOOT_DRIVER(gpio_aspeed) = {
293 .name = "gpio-aspeed",
295 .of_match = aspeed_gpio_ids,
296 .ops = &aspeed_gpio_ops,
297 .probe = aspeed_gpio_probe,
298 .priv_auto = sizeof(struct aspeed_gpio_priv),