blob: b9100cdc58117b7f56c6f43000b0814d5abfa568 [file] [log] [blame]
Álvaro Fernández Rojase64bdb22017-05-07 20:09:30 +02001/*
2 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
3 *
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>
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10
11#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -060012#include <dm.h>
Álvaro Fernández Rojase64bdb22017-05-07 20:09:30 +020013#include <errno.h>
14#include <asm/gpio.h>
15#include <asm/io.h>
Álvaro Fernández Rojase64bdb22017-05-07 20:09:30 +020016
17DECLARE_GLOBAL_DATA_PTR;
18
19struct bcm6345_gpio_priv {
20 void __iomem *reg_dirout;
21 void __iomem *reg_data;
22};
23
24static int bcm6345_gpio_get_value(struct udevice *dev, unsigned offset)
25{
26 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
27
28 return !!(readl_be(priv->reg_data) & BIT(offset));
29}
30
31static int bcm6345_gpio_set_value(struct udevice *dev, unsigned offset,
32 int value)
33{
34 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
35
36 if (value)
37 setbits_be32(priv->reg_data, BIT(offset));
38 else
39 clrbits_be32(priv->reg_data, BIT(offset));
40
41 return 0;
42}
43
44static int bcm6345_gpio_set_direction(void __iomem *dirout, unsigned offset,
45 bool input)
46{
47 if (input)
48 clrbits_be32(dirout, BIT(offset));
49 else
50 setbits_be32(dirout, BIT(offset));
51
52 return 0;
53}
54
55static int bcm6345_gpio_direction_input(struct udevice *dev, unsigned offset)
56{
57 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
58
59 return bcm6345_gpio_set_direction(priv->reg_dirout, offset, 1);
60}
61
62static int bcm6345_gpio_direction_output(struct udevice *dev, unsigned offset,
63 int value)
64{
65 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
66
Axel Lind2d20d92017-06-08 17:20:12 +080067 bcm6345_gpio_set_value(dev, offset, value);
68
Álvaro Fernández Rojase64bdb22017-05-07 20:09:30 +020069 return bcm6345_gpio_set_direction(priv->reg_dirout, offset, 0);
70}
71
72static int bcm6345_gpio_get_function(struct udevice *dev, unsigned offset)
73{
74 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
75
76 if (readl_be(priv->reg_dirout) & BIT(offset))
77 return GPIOF_OUTPUT;
78 else
79 return GPIOF_INPUT;
80}
81
82static 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,
88};
89
90static int bcm6345_gpio_probe(struct udevice *dev)
91{
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;
96
Simon Glassa821c4a2017-05-17 17:18:05 -060097 dirout_addr = devfdt_get_addr_size_index(dev, 0, &dirout_size);
Álvaro Fernández Rojase64bdb22017-05-07 20:09:30 +020098 if (dirout_addr == FDT_ADDR_T_NONE)
99 return -EINVAL;
100
Simon Glassa821c4a2017-05-17 17:18:05 -0600101 data_addr = devfdt_get_addr_size_index(dev, 1, &data_size);
Álvaro Fernández Rojase64bdb22017-05-07 20:09:30 +0200102 if (data_addr == FDT_ADDR_T_NONE)
103 return -EINVAL;
104
105 priv->reg_data = ioremap(data_addr, data_size);
106 priv->reg_dirout = ioremap(dirout_addr, dirout_size);
107
108 uc_priv->gpio_count = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
109 "ngpios", 32);
110 uc_priv->bank_name = dev->name;
111
112 return 0;
113}
114
115static const struct udevice_id bcm6345_gpio_ids[] = {
116 { .compatible = "brcm,bcm6345-gpio" },
117 { /* sentinel */ }
118};
119
120U_BOOT_DRIVER(bcm6345_gpio) = {
121 .name = "bcm6345-gpio",
122 .id = UCLASS_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,
127};