Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Vikram Narayananan |
| 3 | * <vikram186@gmail.com> |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame^] | 10 | #include <dm/pinctrl.h> |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 11 | #include <errno.h> |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 12 | #include <asm/gpio.h> |
| 13 | #include <asm/io.h> |
Fabian Vogt | 4faf5f9 | 2016-09-26 14:26:43 +0200 | [diff] [blame] | 14 | #include <fdtdec.h> |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 15 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 16 | struct bcm2835_gpios { |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 17 | struct bcm2835_gpio_regs *reg; |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame^] | 18 | struct udevice *pinctrl; |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 19 | }; |
| 20 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 21 | static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio) |
| 22 | { |
| 23 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 24 | unsigned val; |
| 25 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 26 | val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 27 | val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
| 28 | val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 29 | writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame^] | 34 | static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned int gpio, |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 35 | int value) |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 36 | { |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 37 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 38 | unsigned val; |
| 39 | |
| 40 | gpio_set_value(gpio, value); |
| 41 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 42 | val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 43 | val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
| 44 | val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 45 | writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 50 | static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio) |
| 51 | { |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 52 | unsigned val; |
| 53 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 54 | val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 55 | |
| 56 | return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1; |
| 57 | } |
| 58 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 59 | static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio) |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 60 | { |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 61 | const struct bcm2835_gpios *gpios = dev_get_priv(dev); |
| 62 | |
| 63 | return bcm2835_get_value(gpios, gpio); |
| 64 | } |
| 65 | |
| 66 | static 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 Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 71 | |
| 72 | writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio), |
| 73 | &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]); |
| 74 | |
| 75 | return 0; |
| 76 | } |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 77 | |
Alexander Graf | 04a993f | 2016-08-11 13:38:31 +0200 | [diff] [blame] | 78 | static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset) |
| 79 | { |
Alexander Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame^] | 80 | struct bcm2835_gpios *priv = dev_get_priv(dev); |
| 81 | int funcid; |
| 82 | |
| 83 | funcid = pinctrl_get_gpio_mux(priv->pinctrl, 0, offset); |
Alexander Graf | 04a993f | 2016-08-11 13:38:31 +0200 | [diff] [blame] | 84 | |
| 85 | switch (funcid) { |
| 86 | case BCM2835_GPIO_OUTPUT: |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 87 | return GPIOF_OUTPUT; |
Alexander Graf | 04a993f | 2016-08-11 13:38:31 +0200 | [diff] [blame] | 88 | case BCM2835_GPIO_INPUT: |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 89 | return GPIOF_INPUT; |
Alexander Graf | 04a993f | 2016-08-11 13:38:31 +0200 | [diff] [blame] | 90 | default: |
| 91 | return GPIOF_FUNC; |
| 92 | } |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 93 | } |
| 94 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 95 | static const struct dm_gpio_ops gpio_bcm2835_ops = { |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 96 | .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 Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | static 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 Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 107 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 108 | |
| 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 Graf | caf2233 | 2018-01-23 18:05:21 +0100 | [diff] [blame^] | 113 | /* We know we're spawned by the pinctrl driver */ |
| 114 | gpios->pinctrl = dev->parent; |
| 115 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
Fabian Vogt | 4faf5f9 | 2016-09-26 14:26:43 +0200 | [diff] [blame] | 119 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Fabian Vogt | 4faf5f9 | 2016-09-26 14:26:43 +0200 | [diff] [blame] | 120 | static 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 Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 125 | addr = devfdt_get_addr(dev); |
Fabian Vogt | 4faf5f9 | 2016-09-26 14:26:43 +0200 | [diff] [blame] | 126 | if (addr == FDT_ADDR_T_NONE) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | plat->base = addr; |
| 130 | return 0; |
| 131 | } |
| 132 | #endif |
| 133 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 134 | U_BOOT_DRIVER(gpio_bcm2835) = { |
| 135 | .name = "gpio_bcm2835", |
| 136 | .id = UCLASS_GPIO, |
Fabian Vogt | 4faf5f9 | 2016-09-26 14:26:43 +0200 | [diff] [blame] | 137 | .ofdata_to_platdata = of_match_ptr(bcm2835_gpio_ofdata_to_platdata), |
| 138 | .platdata_auto_alloc_size = sizeof(struct bcm2835_gpio_platdata), |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 139 | .ops = &gpio_bcm2835_ops, |
| 140 | .probe = bcm2835_gpio_probe, |
Alexander Graf | 601147b | 2016-08-15 17:48:51 +0200 | [diff] [blame] | 141 | .flags = DM_FLAG_PRE_RELOC, |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 142 | .priv_auto_alloc_size = sizeof(struct bcm2835_gpios), |
| 143 | }; |