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> |
| 10 | #include <errno.h> |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 11 | #include <asm/gpio.h> |
| 12 | #include <asm/io.h> |
Fabian Vogt | 4faf5f9 | 2016-09-26 14:26:43 +0200 | [diff] [blame] | 13 | #include <fdtdec.h> |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 14 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 15 | struct bcm2835_gpios { |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 16 | struct bcm2835_gpio_regs *reg; |
| 17 | }; |
| 18 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 19 | static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio) |
| 20 | { |
| 21 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 22 | unsigned val; |
| 23 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 24 | val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 25 | val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
| 26 | val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 27 | writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 28 | |
| 29 | return 0; |
| 30 | } |
| 31 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 32 | static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned gpio, |
| 33 | int value) |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 34 | { |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 35 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 36 | unsigned val; |
| 37 | |
| 38 | gpio_set_value(gpio, value); |
| 39 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 40 | val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 41 | val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
| 42 | val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 43 | writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 48 | static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio) |
| 49 | { |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 50 | unsigned val; |
| 51 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 52 | val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]); |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 53 | |
| 54 | return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1; |
| 55 | } |
| 56 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 57 | static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio) |
Stephen Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 58 | { |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 59 | const struct bcm2835_gpios *gpios = dev_get_priv(dev); |
| 60 | |
| 61 | return bcm2835_get_value(gpios, gpio); |
| 62 | } |
| 63 | |
| 64 | static 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 Warren | efad6cf | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 69 | |
| 70 | writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio), |
| 71 | &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]); |
| 72 | |
| 73 | return 0; |
| 74 | } |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 75 | |
Alexander Graf | 04a993f | 2016-08-11 13:38:31 +0200 | [diff] [blame] | 76 | int bcm2835_gpio_get_func_id(struct udevice *dev, unsigned gpio) |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 77 | { |
| 78 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
Alexander Graf | 04a993f | 2016-08-11 13:38:31 +0200 | [diff] [blame] | 79 | u32 val; |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 80 | |
Alexander Graf | 04a993f | 2016-08-11 13:38:31 +0200 | [diff] [blame] | 81 | 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 | |
| 86 | static 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 Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 92 | return GPIOF_OUTPUT; |
Alexander Graf | 04a993f | 2016-08-11 13:38:31 +0200 | [diff] [blame] | 93 | case BCM2835_GPIO_INPUT: |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 94 | return GPIOF_INPUT; |
Alexander Graf | 04a993f | 2016-08-11 13:38:31 +0200 | [diff] [blame] | 95 | default: |
| 96 | return GPIOF_FUNC; |
| 97 | } |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 98 | } |
| 99 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 100 | |
| 101 | static const struct dm_gpio_ops gpio_bcm2835_ops = { |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 102 | .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 Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | static 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 Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 113 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 114 | |
| 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 Vogt | 4faf5f9 | 2016-09-26 14:26:43 +0200 | [diff] [blame] | 122 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 123 | static const struct udevice_id bcm2835_gpio_id[] = { |
| 124 | {.compatible = "brcm,bcm2835-gpio"}, |
| 125 | {} |
| 126 | }; |
| 127 | |
| 128 | static 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 | |
| 133 | addr = dev_get_addr(dev); |
| 134 | if (addr == FDT_ADDR_T_NONE) |
| 135 | return -EINVAL; |
| 136 | |
| 137 | plat->base = addr; |
| 138 | return 0; |
| 139 | } |
| 140 | #endif |
| 141 | |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 142 | U_BOOT_DRIVER(gpio_bcm2835) = { |
| 143 | .name = "gpio_bcm2835", |
| 144 | .id = UCLASS_GPIO, |
Fabian Vogt | 4faf5f9 | 2016-09-26 14:26:43 +0200 | [diff] [blame] | 145 | .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 Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 148 | .ops = &gpio_bcm2835_ops, |
| 149 | .probe = bcm2835_gpio_probe, |
Alexander Graf | 601147b | 2016-08-15 17:48:51 +0200 | [diff] [blame] | 150 | .flags = DM_FLAG_PRE_RELOC, |
Simon Glass | 41e98e0 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 151 | .priv_auto_alloc_size = sizeof(struct bcm2835_gpios), |
| 152 | }; |