1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2022 Nuvoton Technology Corp.
10 #define NPCM_GPIOS_PER_BANK 32
12 /* Register offsets */
13 #define GPIO_DIN 0x4 /* RO - Data In */
14 #define GPIO_DOUT 0xC /* RW - Data Out */
15 #define GPIO_OE 0x10 /* RW - Output Enable */
16 #define GPIO_IEM 0x58 /* RW - Input Enable Mask */
17 #define GPIO_OES 0x70 /* WO - Output Enable Register Set */
18 #define GPIO_OEC 0x74 /* WO - Output Enable Register Clear */
20 struct npcm_gpio_priv {
24 static int npcm_gpio_direction_input(struct udevice *dev, unsigned int offset)
26 struct npcm_gpio_priv *priv = dev_get_priv(dev);
28 writel(BIT(offset), priv->base + GPIO_OEC);
29 setbits_le32(priv->base + GPIO_IEM, BIT(offset));
34 static int npcm_gpio_direction_output(struct udevice *dev, unsigned int offset,
37 struct npcm_gpio_priv *priv = dev_get_priv(dev);
40 setbits_le32(priv->base + GPIO_DOUT, BIT(offset));
42 clrbits_le32(priv->base + GPIO_DOUT, BIT(offset));
44 clrbits_le32(priv->base + GPIO_IEM, BIT(offset));
45 writel(BIT(offset), priv->base + GPIO_OES);
50 static int npcm_gpio_get_value(struct udevice *dev, unsigned int offset)
52 struct npcm_gpio_priv *priv = dev_get_priv(dev);
54 if (readl(priv->base + GPIO_IEM) & BIT(offset))
55 return !!(readl(priv->base + GPIO_DIN) & BIT(offset));
57 if (readl(priv->base + GPIO_OE) & BIT(offset))
58 return !!(readl(priv->base + GPIO_DOUT) & BIT(offset));
63 static int npcm_gpio_set_value(struct udevice *dev, unsigned int offset,
66 struct npcm_gpio_priv *priv = dev_get_priv(dev);
69 setbits_le32(priv->base + GPIO_DOUT, BIT(offset));
71 clrbits_le32(priv->base + GPIO_DOUT, BIT(offset));
76 static int npcm_gpio_get_function(struct udevice *dev, unsigned int offset)
78 struct npcm_gpio_priv *priv = dev_get_priv(dev);
80 if (readl(priv->base + GPIO_IEM) & BIT(offset))
83 if (readl(priv->base + GPIO_OE) & BIT(offset))
89 static const struct dm_gpio_ops npcm_gpio_ops = {
90 .direction_input = npcm_gpio_direction_input,
91 .direction_output = npcm_gpio_direction_output,
92 .get_value = npcm_gpio_get_value,
93 .set_value = npcm_gpio_set_value,
94 .get_function = npcm_gpio_get_function,
97 static int npcm_gpio_probe(struct udevice *dev)
99 struct npcm_gpio_priv *priv = dev_get_priv(dev);
100 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
102 priv->base = dev_read_addr_ptr(dev);
103 uc_priv->gpio_count = NPCM_GPIOS_PER_BANK;
104 uc_priv->bank_name = dev->name;
109 static const struct udevice_id npcm_gpio_match[] = {
110 { .compatible = "nuvoton,npcm845-gpio" },
111 { .compatible = "nuvoton,npcm750-gpio" },
115 U_BOOT_DRIVER(npcm_gpio) = {
118 .of_match = npcm_gpio_match,
119 .probe = npcm_gpio_probe,
120 .priv_auto = sizeof(struct npcm_gpio_priv),
121 .ops = &npcm_gpio_ops,