2 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
4 * Derived from linux/arch/mips/bcm63xx/gpio.c:
5 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
6 * Copyright (C) 2008-2011 Florian Fainelli <florian@openwrt.org>
8 * SPDX-License-Identifier: GPL-2.0+
17 DECLARE_GLOBAL_DATA_PTR;
19 struct bcm6345_gpio_priv {
20 void __iomem *reg_dirout;
21 void __iomem *reg_data;
24 static int bcm6345_gpio_get_value(struct udevice *dev, unsigned offset)
26 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
28 return !!(readl_be(priv->reg_data) & BIT(offset));
31 static int bcm6345_gpio_set_value(struct udevice *dev, unsigned offset,
34 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
37 setbits_be32(priv->reg_data, BIT(offset));
39 clrbits_be32(priv->reg_data, BIT(offset));
44 static int bcm6345_gpio_set_direction(void __iomem *dirout, unsigned offset,
48 clrbits_be32(dirout, BIT(offset));
50 setbits_be32(dirout, BIT(offset));
55 static int bcm6345_gpio_direction_input(struct udevice *dev, unsigned offset)
57 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
59 return bcm6345_gpio_set_direction(priv->reg_dirout, offset, 1);
62 static int bcm6345_gpio_direction_output(struct udevice *dev, unsigned offset,
65 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
67 bcm6345_gpio_set_value(dev, offset, value);
69 return bcm6345_gpio_set_direction(priv->reg_dirout, offset, 0);
72 static int bcm6345_gpio_get_function(struct udevice *dev, unsigned offset)
74 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
76 if (readl_be(priv->reg_dirout) & BIT(offset))
82 static const struct dm_gpio_ops bcm6345_gpio_ops = {
83 .direction_input = bcm6345_gpio_direction_input,
84 .direction_output = bcm6345_gpio_direction_output,
85 .get_value = bcm6345_gpio_get_value,
86 .set_value = bcm6345_gpio_set_value,
87 .get_function = bcm6345_gpio_get_function,
90 static int bcm6345_gpio_probe(struct udevice *dev)
92 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
93 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
94 fdt_addr_t data_addr, dirout_addr;
95 fdt_size_t data_size, dirout_size;
97 dirout_addr = devfdt_get_addr_size_index(dev, 0, &dirout_size);
98 if (dirout_addr == FDT_ADDR_T_NONE)
101 data_addr = devfdt_get_addr_size_index(dev, 1, &data_size);
102 if (data_addr == FDT_ADDR_T_NONE)
105 priv->reg_data = ioremap(data_addr, data_size);
106 priv->reg_dirout = ioremap(dirout_addr, dirout_size);
108 uc_priv->gpio_count = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
110 uc_priv->bank_name = dev->name;
115 static const struct udevice_id bcm6345_gpio_ids[] = {
116 { .compatible = "brcm,bcm6345-gpio" },
120 U_BOOT_DRIVER(bcm6345_gpio) = {
121 .name = "bcm6345-gpio",
123 .of_match = bcm6345_gpio_ids,
124 .ops = &bcm6345_gpio_ops,
125 .priv_auto_alloc_size = sizeof(struct bcm6345_gpio_priv),
126 .probe = bcm6345_gpio_probe,