blob: 0244c01882863903596f76f153a11cbbaf98dc3e [file] [log] [blame]
Stephen Warrenefad6cf2012-08-05 16:07:21 +00001/*
2 * Copyright (C) 2012 Vikram Narayananan
3 * <vikram186@gmail.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Stephen Warrenefad6cf2012-08-05 16:07:21 +00006 */
7
8#include <common.h>
Simon Glass41e98e02014-09-22 17:30:56 -06009#include <dm.h>
10#include <errno.h>
Stephen Warrenefad6cf2012-08-05 16:07:21 +000011#include <asm/gpio.h>
12#include <asm/io.h>
13
Simon Glass41e98e02014-09-22 17:30:56 -060014struct bcm2835_gpios {
Simon Glass41e98e02014-09-22 17:30:56 -060015 struct bcm2835_gpio_regs *reg;
16};
17
Simon Glass41e98e02014-09-22 17:30:56 -060018static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio)
19{
20 struct bcm2835_gpios *gpios = dev_get_priv(dev);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000021 unsigned val;
22
Simon Glass41e98e02014-09-22 17:30:56 -060023 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000024 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
25 val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
Simon Glass41e98e02014-09-22 17:30:56 -060026 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000027
28 return 0;
29}
30
Simon Glass41e98e02014-09-22 17:30:56 -060031static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned gpio,
32 int value)
Stephen Warrenefad6cf2012-08-05 16:07:21 +000033{
Simon Glass41e98e02014-09-22 17:30:56 -060034 struct bcm2835_gpios *gpios = dev_get_priv(dev);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000035 unsigned val;
36
37 gpio_set_value(gpio, value);
38
Simon Glass41e98e02014-09-22 17:30:56 -060039 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000040 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
41 val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
Simon Glass41e98e02014-09-22 17:30:56 -060042 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000043
44 return 0;
45}
46
Simon Glass41e98e02014-09-22 17:30:56 -060047static bool bcm2835_gpio_is_output(const struct bcm2835_gpios *gpios, int gpio)
Stephen Warrenefad6cf2012-08-05 16:07:21 +000048{
Simon Glass41e98e02014-09-22 17:30:56 -060049 u32 val;
50
51 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
52 val &= BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio);
53 return val ? true : false;
54}
55
56static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
57{
Stephen Warrenefad6cf2012-08-05 16:07:21 +000058 unsigned val;
59
Simon Glass41e98e02014-09-22 17:30:56 -060060 val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000061
62 return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
63}
64
Simon Glass41e98e02014-09-22 17:30:56 -060065static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio)
Stephen Warrenefad6cf2012-08-05 16:07:21 +000066{
Simon Glass41e98e02014-09-22 17:30:56 -060067 const struct bcm2835_gpios *gpios = dev_get_priv(dev);
68
69 return bcm2835_get_value(gpios, gpio);
70}
71
72static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
73 int value)
74{
75 struct bcm2835_gpios *gpios = dev_get_priv(dev);
76 u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr;
Stephen Warrenefad6cf2012-08-05 16:07:21 +000077
78 writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
79 &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
80
81 return 0;
82}
Simon Glass41e98e02014-09-22 17:30:56 -060083
84static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
85{
86 struct bcm2835_gpios *gpios = dev_get_priv(dev);
87
Simon Glass41e98e02014-09-22 17:30:56 -060088 /* GPIOF_FUNC is not implemented yet */
89 if (bcm2835_gpio_is_output(gpios, offset))
90 return GPIOF_OUTPUT;
91 else
92 return GPIOF_INPUT;
93}
94
Simon Glass41e98e02014-09-22 17:30:56 -060095
96static const struct dm_gpio_ops gpio_bcm2835_ops = {
Simon Glass41e98e02014-09-22 17:30:56 -060097 .direction_input = bcm2835_gpio_direction_input,
98 .direction_output = bcm2835_gpio_direction_output,
99 .get_value = bcm2835_gpio_get_value,
100 .set_value = bcm2835_gpio_set_value,
101 .get_function = bcm2835_gpio_get_function,
Simon Glass41e98e02014-09-22 17:30:56 -0600102};
103
104static int bcm2835_gpio_probe(struct udevice *dev)
105{
106 struct bcm2835_gpios *gpios = dev_get_priv(dev);
107 struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
108 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
109
110 uc_priv->bank_name = "GPIO";
111 uc_priv->gpio_count = BCM2835_GPIO_COUNT;
112 gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
113
114 return 0;
115}
116
117U_BOOT_DRIVER(gpio_bcm2835) = {
118 .name = "gpio_bcm2835",
119 .id = UCLASS_GPIO,
120 .ops = &gpio_bcm2835_ops,
121 .probe = bcm2835_gpio_probe,
122 .priv_auto_alloc_size = sizeof(struct bcm2835_gpios),
123};