blob: d68f8df32d55fdff1b44f28eebb62b9cd1026dbe [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>
Alexander Grafcaf22332018-01-23 18:05:21 +010010#include <dm/pinctrl.h>
Simon Glass41e98e02014-09-22 17:30:56 -060011#include <errno.h>
Stephen Warrenefad6cf2012-08-05 16:07:21 +000012#include <asm/gpio.h>
13#include <asm/io.h>
Fabian Vogt4faf5f92016-09-26 14:26:43 +020014#include <fdtdec.h>
Stephen Warrenefad6cf2012-08-05 16:07:21 +000015
Simon Glass41e98e02014-09-22 17:30:56 -060016struct bcm2835_gpios {
Simon Glass41e98e02014-09-22 17:30:56 -060017 struct bcm2835_gpio_regs *reg;
Alexander Grafcaf22332018-01-23 18:05:21 +010018 struct udevice *pinctrl;
Simon Glass41e98e02014-09-22 17:30:56 -060019};
20
Simon Glass41e98e02014-09-22 17:30:56 -060021static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio)
22{
23 struct bcm2835_gpios *gpios = dev_get_priv(dev);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000024 unsigned val;
25
Simon Glass41e98e02014-09-22 17:30:56 -060026 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000027 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
28 val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
Simon Glass41e98e02014-09-22 17:30:56 -060029 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000030
31 return 0;
32}
33
Alexander Grafcaf22332018-01-23 18:05:21 +010034static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned int gpio,
Simon Glass41e98e02014-09-22 17:30:56 -060035 int value)
Stephen Warrenefad6cf2012-08-05 16:07:21 +000036{
Simon Glass41e98e02014-09-22 17:30:56 -060037 struct bcm2835_gpios *gpios = dev_get_priv(dev);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000038 unsigned val;
39
40 gpio_set_value(gpio, value);
41
Simon Glass41e98e02014-09-22 17:30:56 -060042 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000043 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
44 val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
Simon Glass41e98e02014-09-22 17:30:56 -060045 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000046
47 return 0;
48}
49
Simon Glass41e98e02014-09-22 17:30:56 -060050static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
51{
Stephen Warrenefad6cf2012-08-05 16:07:21 +000052 unsigned val;
53
Simon Glass41e98e02014-09-22 17:30:56 -060054 val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000055
56 return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
57}
58
Simon Glass41e98e02014-09-22 17:30:56 -060059static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio)
Stephen Warrenefad6cf2012-08-05 16:07:21 +000060{
Simon Glass41e98e02014-09-22 17:30:56 -060061 const struct bcm2835_gpios *gpios = dev_get_priv(dev);
62
63 return bcm2835_get_value(gpios, gpio);
64}
65
66static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
67 int value)
68{
69 struct bcm2835_gpios *gpios = dev_get_priv(dev);
70 u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr;
Stephen Warrenefad6cf2012-08-05 16:07:21 +000071
72 writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
73 &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
74
75 return 0;
76}
Simon Glass41e98e02014-09-22 17:30:56 -060077
Alexander Graf04a993f2016-08-11 13:38:31 +020078static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
79{
Alexander Grafcaf22332018-01-23 18:05:21 +010080 struct bcm2835_gpios *priv = dev_get_priv(dev);
81 int funcid;
82
83 funcid = pinctrl_get_gpio_mux(priv->pinctrl, 0, offset);
Alexander Graf04a993f2016-08-11 13:38:31 +020084
85 switch (funcid) {
86 case BCM2835_GPIO_OUTPUT:
Simon Glass41e98e02014-09-22 17:30:56 -060087 return GPIOF_OUTPUT;
Alexander Graf04a993f2016-08-11 13:38:31 +020088 case BCM2835_GPIO_INPUT:
Simon Glass41e98e02014-09-22 17:30:56 -060089 return GPIOF_INPUT;
Alexander Graf04a993f2016-08-11 13:38:31 +020090 default:
91 return GPIOF_FUNC;
92 }
Simon Glass41e98e02014-09-22 17:30:56 -060093}
94
Simon Glass41e98e02014-09-22 17:30:56 -060095static const struct dm_gpio_ops gpio_bcm2835_ops = {
Simon Glass41e98e02014-09-22 17:30:56 -060096 .direction_input = bcm2835_gpio_direction_input,
97 .direction_output = bcm2835_gpio_direction_output,
98 .get_value = bcm2835_gpio_get_value,
99 .set_value = bcm2835_gpio_set_value,
100 .get_function = bcm2835_gpio_get_function,
Simon Glass41e98e02014-09-22 17:30:56 -0600101};
102
103static int bcm2835_gpio_probe(struct udevice *dev)
104{
105 struct bcm2835_gpios *gpios = dev_get_priv(dev);
106 struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
Simon Glasse564f052015-03-05 12:25:20 -0700107 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass41e98e02014-09-22 17:30:56 -0600108
109 uc_priv->bank_name = "GPIO";
110 uc_priv->gpio_count = BCM2835_GPIO_COUNT;
111 gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
112
Alexander Grafcaf22332018-01-23 18:05:21 +0100113 /* We know we're spawned by the pinctrl driver */
114 gpios->pinctrl = dev->parent;
115
Simon Glass41e98e02014-09-22 17:30:56 -0600116 return 0;
117}
118
Fabian Vogt4faf5f92016-09-26 14:26:43 +0200119#if CONFIG_IS_ENABLED(OF_CONTROL)
Fabian Vogt4faf5f92016-09-26 14:26:43 +0200120static int bcm2835_gpio_ofdata_to_platdata(struct udevice *dev)
121{
122 struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
123 fdt_addr_t addr;
124
Simon Glassa821c4a2017-05-17 17:18:05 -0600125 addr = devfdt_get_addr(dev);
Fabian Vogt4faf5f92016-09-26 14:26:43 +0200126 if (addr == FDT_ADDR_T_NONE)
127 return -EINVAL;
128
129 plat->base = addr;
130 return 0;
131}
132#endif
133
Simon Glass41e98e02014-09-22 17:30:56 -0600134U_BOOT_DRIVER(gpio_bcm2835) = {
135 .name = "gpio_bcm2835",
136 .id = UCLASS_GPIO,
Fabian Vogt4faf5f92016-09-26 14:26:43 +0200137 .ofdata_to_platdata = of_match_ptr(bcm2835_gpio_ofdata_to_platdata),
138 .platdata_auto_alloc_size = sizeof(struct bcm2835_gpio_platdata),
Simon Glass41e98e02014-09-22 17:30:56 -0600139 .ops = &gpio_bcm2835_ops,
140 .probe = bcm2835_gpio_probe,
Alexander Graf601147b2016-08-15 17:48:51 +0200141 .flags = DM_FLAG_PRE_RELOC,
Simon Glass41e98e02014-09-22 17:30:56 -0600142 .priv_auto_alloc_size = sizeof(struct bcm2835_gpios),
143};