gpio-uniphier.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2016 Socionext Inc.
  3. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <dm/device.h>
  9. #include <linux/bitops.h>
  10. #include <linux/io.h>
  11. #include <linux/sizes.h>
  12. #include <linux/errno.h>
  13. #include <asm/gpio.h>
  14. #define UNIPHIER_GPIO_PORTS_PER_BANK 8
  15. #define UNIPHIER_GPIO_REG_DATA 0 /* data */
  16. #define UNIPHIER_GPIO_REG_DIR 4 /* direction (1:in, 0:out) */
  17. struct uniphier_gpio_priv {
  18. void __iomem *base;
  19. char bank_name[16];
  20. };
  21. static void uniphier_gpio_offset_write(struct udevice *dev, unsigned offset,
  22. unsigned reg, int value)
  23. {
  24. struct uniphier_gpio_priv *priv = dev_get_priv(dev);
  25. u32 tmp;
  26. tmp = readl(priv->base + reg);
  27. if (value)
  28. tmp |= BIT(offset);
  29. else
  30. tmp &= ~BIT(offset);
  31. writel(tmp, priv->base + reg);
  32. }
  33. static int uniphier_gpio_offset_read(struct udevice *dev, unsigned offset,
  34. unsigned reg)
  35. {
  36. struct uniphier_gpio_priv *priv = dev_get_priv(dev);
  37. return !!(readl(priv->base + reg) & BIT(offset));
  38. }
  39. static int uniphier_gpio_direction_input(struct udevice *dev, unsigned offset)
  40. {
  41. uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_REG_DIR, 1);
  42. return 0;
  43. }
  44. static int uniphier_gpio_direction_output(struct udevice *dev, unsigned offset,
  45. int value)
  46. {
  47. uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_REG_DATA, value);
  48. uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_REG_DIR, 0);
  49. return 0;
  50. }
  51. static int uniphier_gpio_get_value(struct udevice *dev, unsigned offset)
  52. {
  53. return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_REG_DATA);
  54. }
  55. static int uniphier_gpio_set_value(struct udevice *dev, unsigned offset,
  56. int value)
  57. {
  58. uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_REG_DATA, value);
  59. return 0;
  60. }
  61. static int uniphier_gpio_get_function(struct udevice *dev, unsigned offset)
  62. {
  63. return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_REG_DIR) ?
  64. GPIOF_INPUT : GPIOF_OUTPUT;
  65. }
  66. static const struct dm_gpio_ops uniphier_gpio_ops = {
  67. .direction_input = uniphier_gpio_direction_input,
  68. .direction_output = uniphier_gpio_direction_output,
  69. .get_value = uniphier_gpio_get_value,
  70. .set_value = uniphier_gpio_set_value,
  71. .get_function = uniphier_gpio_get_function,
  72. };
  73. static int uniphier_gpio_probe(struct udevice *dev)
  74. {
  75. struct uniphier_gpio_priv *priv = dev_get_priv(dev);
  76. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  77. fdt_addr_t addr;
  78. unsigned int tmp;
  79. addr = dev_get_addr(dev);
  80. if (addr == FDT_ADDR_T_NONE)
  81. return -EINVAL;
  82. priv->base = devm_ioremap(dev, addr, SZ_8);
  83. if (!priv->base)
  84. return -ENOMEM;
  85. uc_priv->gpio_count = UNIPHIER_GPIO_PORTS_PER_BANK;
  86. tmp = (addr & 0xfff);
  87. /* Unfortunately, there is a register hole at offset 0x90-0x9f. */
  88. if (tmp > 0x90)
  89. tmp -= 0x10;
  90. snprintf(priv->bank_name, sizeof(priv->bank_name) - 1,
  91. "port%d-", (tmp - 8) / 8);
  92. uc_priv->bank_name = priv->bank_name;
  93. return 0;
  94. }
  95. /* .data = the number of GPIO banks */
  96. static const struct udevice_id uniphier_gpio_match[] = {
  97. { .compatible = "socionext,uniphier-gpio" },
  98. { /* sentinel */ }
  99. };
  100. U_BOOT_DRIVER(uniphier_gpio) = {
  101. .name = "uniphier_gpio",
  102. .id = UCLASS_GPIO,
  103. .of_match = uniphier_gpio_match,
  104. .probe = uniphier_gpio_probe,
  105. .priv_auto_alloc_size = sizeof(struct uniphier_gpio_priv),
  106. .ops = &uniphier_gpio_ops,
  107. };