blob: beaa21853a473dfaa4e59a58f995cce871afc045 [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>
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;
17};
18
Simon Glass41e98e02014-09-22 17:30:56 -060019static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio)
20{
21 struct bcm2835_gpios *gpios = dev_get_priv(dev);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000022 unsigned val;
23
Simon Glass41e98e02014-09-22 17:30:56 -060024 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000025 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
26 val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
Simon Glass41e98e02014-09-22 17:30:56 -060027 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000028
29 return 0;
30}
31
Simon Glass41e98e02014-09-22 17:30:56 -060032static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned gpio,
33 int value)
Stephen Warrenefad6cf2012-08-05 16:07:21 +000034{
Simon Glass41e98e02014-09-22 17:30:56 -060035 struct bcm2835_gpios *gpios = dev_get_priv(dev);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000036 unsigned val;
37
38 gpio_set_value(gpio, value);
39
Simon Glass41e98e02014-09-22 17:30:56 -060040 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000041 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
42 val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
Simon Glass41e98e02014-09-22 17:30:56 -060043 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000044
45 return 0;
46}
47
Simon Glass41e98e02014-09-22 17:30:56 -060048static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
49{
Stephen Warrenefad6cf2012-08-05 16:07:21 +000050 unsigned val;
51
Simon Glass41e98e02014-09-22 17:30:56 -060052 val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
Stephen Warrenefad6cf2012-08-05 16:07:21 +000053
54 return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
55}
56
Simon Glass41e98e02014-09-22 17:30:56 -060057static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio)
Stephen Warrenefad6cf2012-08-05 16:07:21 +000058{
Simon Glass41e98e02014-09-22 17:30:56 -060059 const struct bcm2835_gpios *gpios = dev_get_priv(dev);
60
61 return bcm2835_get_value(gpios, gpio);
62}
63
64static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
65 int value)
66{
67 struct bcm2835_gpios *gpios = dev_get_priv(dev);
68 u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr;
Stephen Warrenefad6cf2012-08-05 16:07:21 +000069
70 writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
71 &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
72
73 return 0;
74}
Simon Glass41e98e02014-09-22 17:30:56 -060075
Alexander Graf04a993f2016-08-11 13:38:31 +020076int bcm2835_gpio_get_func_id(struct udevice *dev, unsigned gpio)
Simon Glass41e98e02014-09-22 17:30:56 -060077{
78 struct bcm2835_gpios *gpios = dev_get_priv(dev);
Alexander Graf04a993f2016-08-11 13:38:31 +020079 u32 val;
Simon Glass41e98e02014-09-22 17:30:56 -060080
Alexander Graf04a993f2016-08-11 13:38:31 +020081 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
82
83 return (val >> BCM2835_GPIO_FSEL_SHIFT(gpio) & BCM2835_GPIO_FSEL_MASK);
84}
85
86static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
87{
88 int funcid = bcm2835_gpio_get_func_id(dev, offset);
89
90 switch (funcid) {
91 case BCM2835_GPIO_OUTPUT:
Simon Glass41e98e02014-09-22 17:30:56 -060092 return GPIOF_OUTPUT;
Alexander Graf04a993f2016-08-11 13:38:31 +020093 case BCM2835_GPIO_INPUT:
Simon Glass41e98e02014-09-22 17:30:56 -060094 return GPIOF_INPUT;
Alexander Graf04a993f2016-08-11 13:38:31 +020095 default:
96 return GPIOF_FUNC;
97 }
Simon Glass41e98e02014-09-22 17:30:56 -060098}
99
Simon Glass41e98e02014-09-22 17:30:56 -0600100
101static const struct dm_gpio_ops gpio_bcm2835_ops = {
Simon Glass41e98e02014-09-22 17:30:56 -0600102 .direction_input = bcm2835_gpio_direction_input,
103 .direction_output = bcm2835_gpio_direction_output,
104 .get_value = bcm2835_gpio_get_value,
105 .set_value = bcm2835_gpio_set_value,
106 .get_function = bcm2835_gpio_get_function,
Simon Glass41e98e02014-09-22 17:30:56 -0600107};
108
109static int bcm2835_gpio_probe(struct udevice *dev)
110{
111 struct bcm2835_gpios *gpios = dev_get_priv(dev);
112 struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
Simon Glasse564f052015-03-05 12:25:20 -0700113 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass41e98e02014-09-22 17:30:56 -0600114
115 uc_priv->bank_name = "GPIO";
116 uc_priv->gpio_count = BCM2835_GPIO_COUNT;
117 gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
118
119 return 0;
120}
121
Fabian Vogt4faf5f92016-09-26 14:26:43 +0200122#if CONFIG_IS_ENABLED(OF_CONTROL)
123static const struct udevice_id bcm2835_gpio_id[] = {
124 {.compatible = "brcm,bcm2835-gpio"},
125 {}
126};
127
128static int bcm2835_gpio_ofdata_to_platdata(struct udevice *dev)
129{
130 struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
131 fdt_addr_t addr;
132
Simon Glassa821c4a2017-05-17 17:18:05 -0600133 addr = devfdt_get_addr(dev);
Fabian Vogt4faf5f92016-09-26 14:26:43 +0200134 if (addr == FDT_ADDR_T_NONE)
135 return -EINVAL;
136
137 plat->base = addr;
138 return 0;
139}
140#endif
141
Simon Glass41e98e02014-09-22 17:30:56 -0600142U_BOOT_DRIVER(gpio_bcm2835) = {
143 .name = "gpio_bcm2835",
144 .id = UCLASS_GPIO,
Fabian Vogt4faf5f92016-09-26 14:26:43 +0200145 .of_match = of_match_ptr(bcm2835_gpio_id),
146 .ofdata_to_platdata = of_match_ptr(bcm2835_gpio_ofdata_to_platdata),
147 .platdata_auto_alloc_size = sizeof(struct bcm2835_gpio_platdata),
Simon Glass41e98e02014-09-22 17:30:56 -0600148 .ops = &gpio_bcm2835_ops,
149 .probe = bcm2835_gpio_probe,
Alexander Graf601147b2016-08-15 17:48:51 +0200150 .flags = DM_FLAG_PRE_RELOC,
Simon Glass41e98e02014-09-22 17:30:56 -0600151 .priv_auto_alloc_size = sizeof(struct bcm2835_gpios),
152};