bcm2835_gpio.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2012 Vikram Narayananan
  3. * <vikram186@gmail.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <asm/gpio.h>
  11. #include <asm/io.h>
  12. struct bcm2835_gpios {
  13. struct bcm2835_gpio_regs *reg;
  14. };
  15. static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio)
  16. {
  17. struct bcm2835_gpios *gpios = dev_get_priv(dev);
  18. unsigned val;
  19. val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
  20. val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
  21. val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
  22. writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
  23. return 0;
  24. }
  25. static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned gpio,
  26. int value)
  27. {
  28. struct bcm2835_gpios *gpios = dev_get_priv(dev);
  29. unsigned val;
  30. gpio_set_value(gpio, value);
  31. val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
  32. val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
  33. val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
  34. writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
  35. return 0;
  36. }
  37. static bool bcm2835_gpio_is_output(const struct bcm2835_gpios *gpios, int gpio)
  38. {
  39. u32 val;
  40. val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
  41. val &= BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio);
  42. return val ? true : false;
  43. }
  44. static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
  45. {
  46. unsigned val;
  47. val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
  48. return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
  49. }
  50. static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio)
  51. {
  52. const struct bcm2835_gpios *gpios = dev_get_priv(dev);
  53. return bcm2835_get_value(gpios, gpio);
  54. }
  55. static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
  56. int value)
  57. {
  58. struct bcm2835_gpios *gpios = dev_get_priv(dev);
  59. u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr;
  60. writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
  61. &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
  62. return 0;
  63. }
  64. static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
  65. {
  66. struct bcm2835_gpios *gpios = dev_get_priv(dev);
  67. /* GPIOF_FUNC is not implemented yet */
  68. if (bcm2835_gpio_is_output(gpios, offset))
  69. return GPIOF_OUTPUT;
  70. else
  71. return GPIOF_INPUT;
  72. }
  73. static const struct dm_gpio_ops gpio_bcm2835_ops = {
  74. .direction_input = bcm2835_gpio_direction_input,
  75. .direction_output = bcm2835_gpio_direction_output,
  76. .get_value = bcm2835_gpio_get_value,
  77. .set_value = bcm2835_gpio_set_value,
  78. .get_function = bcm2835_gpio_get_function,
  79. };
  80. static int bcm2835_gpio_probe(struct udevice *dev)
  81. {
  82. struct bcm2835_gpios *gpios = dev_get_priv(dev);
  83. struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
  84. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  85. uc_priv->bank_name = "GPIO";
  86. uc_priv->gpio_count = BCM2835_GPIO_COUNT;
  87. gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
  88. return 0;
  89. }
  90. U_BOOT_DRIVER(gpio_bcm2835) = {
  91. .name = "gpio_bcm2835",
  92. .id = UCLASS_GPIO,
  93. .ops = &gpio_bcm2835_ops,
  94. .probe = bcm2835_gpio_probe,
  95. .priv_auto_alloc_size = sizeof(struct bcm2835_gpios),
  96. };