Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Vikram Narayananan |
| 3 | * <vikram186@gmail.com> |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 11 | #include <asm/gpio.h> |
| 12 | #include <asm/io.h> |
| 13 | |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 14 | struct bcm2835_gpios { |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 15 | struct bcm2835_gpio_regs *reg; |
| 16 | }; |
| 17 | |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 18 | static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio) |
| 19 | { |
| 20 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 21 | unsigned val; |
| 22 | |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 23 | val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 24 | val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
| 25 | val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 26 | writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 31 | static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned gpio, |
| 32 | int value) |
Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 33 | { |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 34 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 35 | unsigned val; |
| 36 | |
| 37 | gpio_set_value(gpio, value); |
| 38 | |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 39 | val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 40 | val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
| 41 | val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio)); |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 42 | writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 47 | static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio) |
Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 48 | { |
Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 49 | unsigned val; |
| 50 | |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 51 | val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]); |
Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 52 | |
| 53 | return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1; |
| 54 | } |
| 55 | |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 56 | static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio) |
Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 57 | { |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 58 | const struct bcm2835_gpios *gpios = dev_get_priv(dev); |
| 59 | |
| 60 | return bcm2835_get_value(gpios, gpio); |
| 61 | } |
| 62 | |
| 63 | static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio, |
| 64 | int value) |
| 65 | { |
| 66 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
| 67 | u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr; |
Stephen Warren | 45b8ae6 | 2012-08-05 16:07:21 +0000 | [diff] [blame] | 68 | |
| 69 | writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio), |
| 70 | &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]); |
| 71 | |
| 72 | return 0; |
| 73 | } |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 74 | |
Alexander Graf | 6c68a22 | 2016-08-11 13:38:31 +0200 | [diff] [blame] | 75 | int bcm2835_gpio_get_func_id(struct udevice *dev, unsigned gpio) |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 76 | { |
| 77 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
Alexander Graf | 6c68a22 | 2016-08-11 13:38:31 +0200 | [diff] [blame] | 78 | u32 val; |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 79 | |
Alexander Graf | 6c68a22 | 2016-08-11 13:38:31 +0200 | [diff] [blame] | 80 | val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]); |
| 81 | |
| 82 | return (val >> BCM2835_GPIO_FSEL_SHIFT(gpio) & BCM2835_GPIO_FSEL_MASK); |
| 83 | } |
| 84 | |
| 85 | static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset) |
| 86 | { |
| 87 | int funcid = bcm2835_gpio_get_func_id(dev, offset); |
| 88 | |
| 89 | switch (funcid) { |
| 90 | case BCM2835_GPIO_OUTPUT: |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 91 | return GPIOF_OUTPUT; |
Alexander Graf | 6c68a22 | 2016-08-11 13:38:31 +0200 | [diff] [blame] | 92 | case BCM2835_GPIO_INPUT: |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 93 | return GPIOF_INPUT; |
Alexander Graf | 6c68a22 | 2016-08-11 13:38:31 +0200 | [diff] [blame] | 94 | default: |
| 95 | return GPIOF_FUNC; |
| 96 | } |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 97 | } |
| 98 | |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 99 | |
| 100 | static const struct dm_gpio_ops gpio_bcm2835_ops = { |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 101 | .direction_input = bcm2835_gpio_direction_input, |
| 102 | .direction_output = bcm2835_gpio_direction_output, |
| 103 | .get_value = bcm2835_gpio_get_value, |
| 104 | .set_value = bcm2835_gpio_set_value, |
| 105 | .get_function = bcm2835_gpio_get_function, |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | static int bcm2835_gpio_probe(struct udevice *dev) |
| 109 | { |
| 110 | struct bcm2835_gpios *gpios = dev_get_priv(dev); |
| 111 | struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev); |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 112 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 113 | |
| 114 | uc_priv->bank_name = "GPIO"; |
| 115 | uc_priv->gpio_count = BCM2835_GPIO_COUNT; |
| 116 | gpios->reg = (struct bcm2835_gpio_regs *)plat->base; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | U_BOOT_DRIVER(gpio_bcm2835) = { |
| 122 | .name = "gpio_bcm2835", |
| 123 | .id = UCLASS_GPIO, |
| 124 | .ops = &gpio_bcm2835_ops, |
| 125 | .probe = bcm2835_gpio_probe, |
Alexander Graf | afb9907 | 2016-08-15 17:48:51 +0200 | [diff] [blame^] | 126 | .flags = DM_FLAG_PRE_RELOC, |
Simon Glass | 7480762 | 2014-09-22 17:30:56 -0600 | [diff] [blame] | 127 | .priv_auto_alloc_size = sizeof(struct bcm2835_gpios), |
| 128 | }; |