blob: ccf84fdae11f292e494dc4562b74b51d5dcc1dbd [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stephen Warren45b8ae62012-08-05 16:07:21 +00002/*
3 * Copyright (C) 2012 Vikram Narayananan
4 * <vikram186@gmail.com>
Stephen Warren45b8ae62012-08-05 16:07:21 +00005 */
6
Simon Glass74807622014-09-22 17:30:56 -06007#include <dm.h>
Alexander Graffce8e5c2018-01-23 18:05:21 +01008#include <dm/pinctrl.h>
Simon Glass74807622014-09-22 17:30:56 -06009#include <errno.h>
Stephen Warren45b8ae62012-08-05 16:07:21 +000010#include <asm/gpio.h>
11#include <asm/io.h>
Fabian Vogt58eb5142016-09-26 14:26:43 +020012#include <fdtdec.h>
Stephen Warren45b8ae62012-08-05 16:07:21 +000013
Simon Glass74807622014-09-22 17:30:56 -060014struct bcm2835_gpios {
Simon Glass74807622014-09-22 17:30:56 -060015 struct bcm2835_gpio_regs *reg;
Alexander Graffce8e5c2018-01-23 18:05:21 +010016 struct udevice *pinctrl;
Simon Glass74807622014-09-22 17:30:56 -060017};
18
Simon Glass74807622014-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 Warren45b8ae62012-08-05 16:07:21 +000022 unsigned val;
23
Simon Glass74807622014-09-22 17:30:56 -060024 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warren45b8ae62012-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 Glass74807622014-09-22 17:30:56 -060027 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warren45b8ae62012-08-05 16:07:21 +000028
29 return 0;
30}
31
Alexander Graffce8e5c2018-01-23 18:05:21 +010032static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned int gpio,
Simon Glass74807622014-09-22 17:30:56 -060033 int value)
Stephen Warren45b8ae62012-08-05 16:07:21 +000034{
Simon Glass74807622014-09-22 17:30:56 -060035 struct bcm2835_gpios *gpios = dev_get_priv(dev);
Stephen Warren45b8ae62012-08-05 16:07:21 +000036 unsigned val;
37
38 gpio_set_value(gpio, value);
39
Simon Glass74807622014-09-22 17:30:56 -060040 val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warren45b8ae62012-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 Glass74807622014-09-22 17:30:56 -060043 writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
Stephen Warren45b8ae62012-08-05 16:07:21 +000044
45 return 0;
46}
47
Simon Glass74807622014-09-22 17:30:56 -060048static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
Stephen Warren45b8ae62012-08-05 16:07:21 +000049{
Stephen Warren45b8ae62012-08-05 16:07:21 +000050 unsigned val;
51
Simon Glass74807622014-09-22 17:30:56 -060052 val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
Stephen Warren45b8ae62012-08-05 16:07:21 +000053
54 return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
55}
56
Simon Glass74807622014-09-22 17:30:56 -060057static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio)
Stephen Warren45b8ae62012-08-05 16:07:21 +000058{
Simon Glass74807622014-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 Warren45b8ae62012-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 Glass74807622014-09-22 17:30:56 -060075
Alexander Graf6c68a222016-08-11 13:38:31 +020076static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
77{
Alexander Graffce8e5c2018-01-23 18:05:21 +010078 struct bcm2835_gpios *priv = dev_get_priv(dev);
79 int funcid;
80
81 funcid = pinctrl_get_gpio_mux(priv->pinctrl, 0, offset);
Alexander Graf6c68a222016-08-11 13:38:31 +020082
83 switch (funcid) {
84 case BCM2835_GPIO_OUTPUT:
Simon Glass74807622014-09-22 17:30:56 -060085 return GPIOF_OUTPUT;
Alexander Graf6c68a222016-08-11 13:38:31 +020086 case BCM2835_GPIO_INPUT:
Simon Glass74807622014-09-22 17:30:56 -060087 return GPIOF_INPUT;
Alexander Graf6c68a222016-08-11 13:38:31 +020088 default:
89 return GPIOF_FUNC;
90 }
Simon Glass74807622014-09-22 17:30:56 -060091}
92
Simon Glass74807622014-09-22 17:30:56 -060093static const struct dm_gpio_ops gpio_bcm2835_ops = {
Simon Glass74807622014-09-22 17:30:56 -060094 .direction_input = bcm2835_gpio_direction_input,
95 .direction_output = bcm2835_gpio_direction_output,
96 .get_value = bcm2835_gpio_get_value,
97 .set_value = bcm2835_gpio_set_value,
98 .get_function = bcm2835_gpio_get_function,
Simon Glass74807622014-09-22 17:30:56 -060099};
100
101static int bcm2835_gpio_probe(struct udevice *dev)
102{
103 struct bcm2835_gpios *gpios = dev_get_priv(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -0700104 struct bcm2835_gpio_plat *plat = dev_get_plat(dev);
Simon Glassde0977b2015-03-05 12:25:20 -0700105 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass74807622014-09-22 17:30:56 -0600106
107 uc_priv->bank_name = "GPIO";
108 uc_priv->gpio_count = BCM2835_GPIO_COUNT;
109 gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
110
Alexander Graffce8e5c2018-01-23 18:05:21 +0100111 /* We know we're spawned by the pinctrl driver */
112 gpios->pinctrl = dev->parent;
113
Simon Glass74807622014-09-22 17:30:56 -0600114 return 0;
115}
116
Fabian Vogt58eb5142016-09-26 14:26:43 +0200117#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassaad29ae2020-12-03 16:55:21 -0700118static int bcm2835_gpio_of_to_plat(struct udevice *dev)
Fabian Vogt58eb5142016-09-26 14:26:43 +0200119{
Simon Glassb75b15b2020-12-03 16:55:23 -0700120 struct bcm2835_gpio_plat *plat = dev_get_plat(dev);
Fabian Vogt58eb5142016-09-26 14:26:43 +0200121 fdt_addr_t addr;
122
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900123 addr = dev_read_addr(dev);
Fabian Vogt58eb5142016-09-26 14:26:43 +0200124 if (addr == FDT_ADDR_T_NONE)
125 return -EINVAL;
126
127 plat->base = addr;
128 return 0;
129}
130#endif
131
Simon Glass74807622014-09-22 17:30:56 -0600132U_BOOT_DRIVER(gpio_bcm2835) = {
133 .name = "gpio_bcm2835",
134 .id = UCLASS_GPIO,
Simon Glassaad29ae2020-12-03 16:55:21 -0700135 .of_to_plat = of_match_ptr(bcm2835_gpio_of_to_plat),
Simon Glassb75b15b2020-12-03 16:55:23 -0700136 .plat_auto = sizeof(struct bcm2835_gpio_plat),
Simon Glass74807622014-09-22 17:30:56 -0600137 .ops = &gpio_bcm2835_ops,
138 .probe = bcm2835_gpio_probe,
Alexander Grafafb99072016-08-15 17:48:51 +0200139 .flags = DM_FLAG_PRE_RELOC,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700140 .priv_auto = sizeof(struct bcm2835_gpios),
Simon Glass74807622014-09-22 17:30:56 -0600141};