blob: f4b67f1cf0aff523a19bcaf6bcf64833f0c1abca [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stephen Warrenefad6cf2012-08-05 16:07:21 +00002/*
3 * Copyright (C) 2012 Vikram Narayananan
4 * <vikram186@gmail.com>
Stephen Warrenefad6cf2012-08-05 16:07:21 +00005 */
6
7#include <common.h>
Simon Glass41e98e02014-09-22 17:30:56 -06008#include <dm.h>
Alexander Grafcaf22332018-01-23 18:05:21 +01009#include <dm/pinctrl.h>
Simon Glass41e98e02014-09-22 17:30:56 -060010#include <errno.h>
Stephen Warrenefad6cf2012-08-05 16:07:21 +000011#include <asm/gpio.h>
12#include <asm/io.h>
Fabian Vogt4faf5f92016-09-26 14:26:43 +020013#include <fdtdec.h>
Stephen Warrenefad6cf2012-08-05 16:07:21 +000014
Simon Glass41e98e02014-09-22 17:30:56 -060015struct bcm2835_gpios {
Simon Glass41e98e02014-09-22 17:30:56 -060016 struct bcm2835_gpio_regs *reg;
Alexander Grafcaf22332018-01-23 18:05:21 +010017 struct udevice *pinctrl;
Simon Glass41e98e02014-09-22 17:30:56 -060018};
19
Simon Glass41e98e02014-09-22 17:30:56 -060020static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio)
21{
22 struct bcm2835_gpios *gpios = dev_get_priv(dev);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000023 unsigned val;
24
Simon Glass41e98e02014-09-22 17:30:56 -060025 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000026 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
27 val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
Simon Glass41e98e02014-09-22 17:30:56 -060028 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000029
30 return 0;
31}
32
Alexander Grafcaf22332018-01-23 18:05:21 +010033static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned int gpio,
Simon Glass41e98e02014-09-22 17:30:56 -060034 int value)
Stephen Warrenefad6cf2012-08-05 16:07:21 +000035{
Simon Glass41e98e02014-09-22 17:30:56 -060036 struct bcm2835_gpios *gpios = dev_get_priv(dev);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000037 unsigned val;
38
39 gpio_set_value(gpio, value);
40
Simon Glass41e98e02014-09-22 17:30:56 -060041 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000042 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
43 val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
Simon Glass41e98e02014-09-22 17:30:56 -060044 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000045
46 return 0;
47}
48
Simon Glass41e98e02014-09-22 17:30:56 -060049static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
50{
Stephen Warrenefad6cf2012-08-05 16:07:21 +000051 unsigned val;
52
Simon Glass41e98e02014-09-22 17:30:56 -060053 val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000054
55 return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
56}
57
Simon Glass41e98e02014-09-22 17:30:56 -060058static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio)
Stephen Warrenefad6cf2012-08-05 16:07:21 +000059{
Simon Glass41e98e02014-09-22 17:30:56 -060060 const struct bcm2835_gpios *gpios = dev_get_priv(dev);
61
62 return bcm2835_get_value(gpios, gpio);
63}
64
65static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
66 int value)
67{
68 struct bcm2835_gpios *gpios = dev_get_priv(dev);
69 u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr;
Stephen Warrenefad6cf2012-08-05 16:07:21 +000070
71 writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
72 &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
73
74 return 0;
75}
Simon Glass41e98e02014-09-22 17:30:56 -060076
Alexander Graf04a993f2016-08-11 13:38:31 +020077static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
78{
Alexander Grafcaf22332018-01-23 18:05:21 +010079 struct bcm2835_gpios *priv = dev_get_priv(dev);
80 int funcid;
81
82 funcid = pinctrl_get_gpio_mux(priv->pinctrl, 0, offset);
Alexander Graf04a993f2016-08-11 13:38:31 +020083
84 switch (funcid) {
85 case BCM2835_GPIO_OUTPUT:
Simon Glass41e98e02014-09-22 17:30:56 -060086 return GPIOF_OUTPUT;
Alexander Graf04a993f2016-08-11 13:38:31 +020087 case BCM2835_GPIO_INPUT:
Simon Glass41e98e02014-09-22 17:30:56 -060088 return GPIOF_INPUT;
Alexander Graf04a993f2016-08-11 13:38:31 +020089 default:
90 return GPIOF_FUNC;
91 }
Simon Glass41e98e02014-09-22 17:30:56 -060092}
93
Simon Glass41e98e02014-09-22 17:30:56 -060094static const struct dm_gpio_ops gpio_bcm2835_ops = {
Simon Glass41e98e02014-09-22 17:30:56 -060095 .direction_input = bcm2835_gpio_direction_input,
96 .direction_output = bcm2835_gpio_direction_output,
97 .get_value = bcm2835_gpio_get_value,
98 .set_value = bcm2835_gpio_set_value,
99 .get_function = bcm2835_gpio_get_function,
Simon Glass41e98e02014-09-22 17:30:56 -0600100};
101
102static int bcm2835_gpio_probe(struct udevice *dev)
103{
104 struct bcm2835_gpios *gpios = dev_get_priv(dev);
105 struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
Simon Glasse564f052015-03-05 12:25:20 -0700106 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass41e98e02014-09-22 17:30:56 -0600107
108 uc_priv->bank_name = "GPIO";
109 uc_priv->gpio_count = BCM2835_GPIO_COUNT;
110 gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
111
Alexander Grafcaf22332018-01-23 18:05:21 +0100112 /* We know we're spawned by the pinctrl driver */
113 gpios->pinctrl = dev->parent;
114
Simon Glass41e98e02014-09-22 17:30:56 -0600115 return 0;
116}
117
Fabian Vogt4faf5f92016-09-26 14:26:43 +0200118#if CONFIG_IS_ENABLED(OF_CONTROL)
Fabian Vogt4faf5f92016-09-26 14:26:43 +0200119static int bcm2835_gpio_ofdata_to_platdata(struct udevice *dev)
120{
121 struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
122 fdt_addr_t addr;
123
Simon Glassa821c4a2017-05-17 17:18:05 -0600124 addr = devfdt_get_addr(dev);
Fabian Vogt4faf5f92016-09-26 14:26:43 +0200125 if (addr == FDT_ADDR_T_NONE)
126 return -EINVAL;
127
128 plat->base = addr;
129 return 0;
130}
131#endif
132
Simon Glass41e98e02014-09-22 17:30:56 -0600133U_BOOT_DRIVER(gpio_bcm2835) = {
134 .name = "gpio_bcm2835",
135 .id = UCLASS_GPIO,
Fabian Vogt4faf5f92016-09-26 14:26:43 +0200136 .ofdata_to_platdata = of_match_ptr(bcm2835_gpio_ofdata_to_platdata),
137 .platdata_auto_alloc_size = sizeof(struct bcm2835_gpio_platdata),
Simon Glass41e98e02014-09-22 17:30:56 -0600138 .ops = &gpio_bcm2835_ops,
139 .probe = bcm2835_gpio_probe,
Alexander Graf601147b2016-08-15 17:48:51 +0200140 .flags = DM_FLAG_PRE_RELOC,
Simon Glass41e98e02014-09-22 17:30:56 -0600141 .priv_auto_alloc_size = sizeof(struct bcm2835_gpios),
142};