bcm2835_gpio.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2012 Vikram Narayananan
  4. * <vikram186@gmail.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <dm/pinctrl.h>
  9. #include <errno.h>
  10. #include <asm/gpio.h>
  11. #include <asm/io.h>
  12. #include <fdtdec.h>
  13. struct bcm2835_gpios {
  14. struct bcm2835_gpio_regs *reg;
  15. struct udevice *pinctrl;
  16. };
  17. static int bcm2835_gpio_direction_input(struct udevice *dev, unsigned gpio)
  18. {
  19. struct bcm2835_gpios *gpios = dev_get_priv(dev);
  20. unsigned val;
  21. val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
  22. val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
  23. val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
  24. writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
  25. return 0;
  26. }
  27. static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned int gpio,
  28. int value)
  29. {
  30. struct bcm2835_gpios *gpios = dev_get_priv(dev);
  31. unsigned val;
  32. gpio_set_value(gpio, value);
  33. val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
  34. val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
  35. val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
  36. writel(val, &gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
  37. return 0;
  38. }
  39. static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
  40. {
  41. unsigned val;
  42. val = readl(&gpios->reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
  43. return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
  44. }
  45. static int bcm2835_gpio_get_value(struct udevice *dev, unsigned gpio)
  46. {
  47. const struct bcm2835_gpios *gpios = dev_get_priv(dev);
  48. return bcm2835_get_value(gpios, gpio);
  49. }
  50. static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
  51. int value)
  52. {
  53. struct bcm2835_gpios *gpios = dev_get_priv(dev);
  54. u32 *output_reg = value ? gpios->reg->gpset : gpios->reg->gpclr;
  55. writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
  56. &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
  57. return 0;
  58. }
  59. static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
  60. {
  61. struct bcm2835_gpios *priv = dev_get_priv(dev);
  62. int funcid;
  63. funcid = pinctrl_get_gpio_mux(priv->pinctrl, 0, offset);
  64. switch (funcid) {
  65. case BCM2835_GPIO_OUTPUT:
  66. return GPIOF_OUTPUT;
  67. case BCM2835_GPIO_INPUT:
  68. return GPIOF_INPUT;
  69. default:
  70. return GPIOF_FUNC;
  71. }
  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_get_uclass_priv(dev);
  85. uc_priv->bank_name = "GPIO";
  86. uc_priv->gpio_count = BCM2835_GPIO_COUNT;
  87. gpios->reg = (struct bcm2835_gpio_regs *)plat->base;
  88. /* We know we're spawned by the pinctrl driver */
  89. gpios->pinctrl = dev->parent;
  90. return 0;
  91. }
  92. #if CONFIG_IS_ENABLED(OF_CONTROL)
  93. static int bcm2835_gpio_ofdata_to_platdata(struct udevice *dev)
  94. {
  95. struct bcm2835_gpio_platdata *plat = dev_get_platdata(dev);
  96. fdt_addr_t addr;
  97. addr = dev_read_addr(dev);
  98. if (addr == FDT_ADDR_T_NONE)
  99. return -EINVAL;
  100. plat->base = addr;
  101. return 0;
  102. }
  103. #endif
  104. U_BOOT_DRIVER(gpio_bcm2835) = {
  105. .name = "gpio_bcm2835",
  106. .id = UCLASS_GPIO,
  107. .ofdata_to_platdata = of_match_ptr(bcm2835_gpio_ofdata_to_platdata),
  108. .platdata_auto_alloc_size = sizeof(struct bcm2835_gpio_platdata),
  109. .ops = &gpio_bcm2835_ops,
  110. .probe = bcm2835_gpio_probe,
  111. .flags = DM_FLAG_PRE_RELOC,
  112. .priv_auto_alloc_size = sizeof(struct bcm2835_gpios),
  113. };