npcm_gpio.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2022 Nuvoton Technology Corp.
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <asm/gpio.h>
  8. #include <linux/io.h>
  9. #define NPCM_GPIOS_PER_BANK 32
  10. /* Register offsets */
  11. #define GPIO_DIN 0x4 /* RO - Data In */
  12. #define GPIO_DOUT 0xC /* RW - Data Out */
  13. #define GPIO_OE 0x10 /* RW - Output Enable */
  14. #define GPIO_IEM 0x58 /* RW - Input Enable Mask */
  15. #define GPIO_OES 0x70 /* WO - Output Enable Register Set */
  16. #define GPIO_OEC 0x74 /* WO - Output Enable Register Clear */
  17. struct npcm_gpio_priv {
  18. void __iomem *base;
  19. };
  20. static int npcm_gpio_direction_input(struct udevice *dev, unsigned int offset)
  21. {
  22. struct npcm_gpio_priv *priv = dev_get_priv(dev);
  23. writel(BIT(offset), priv->base + GPIO_OEC);
  24. setbits_le32(priv->base + GPIO_IEM, BIT(offset));
  25. return 0;
  26. }
  27. static int npcm_gpio_direction_output(struct udevice *dev, unsigned int offset,
  28. int value)
  29. {
  30. struct npcm_gpio_priv *priv = dev_get_priv(dev);
  31. if (value)
  32. setbits_le32(priv->base + GPIO_DOUT, BIT(offset));
  33. else
  34. clrbits_le32(priv->base + GPIO_DOUT, BIT(offset));
  35. clrbits_le32(priv->base + GPIO_IEM, BIT(offset));
  36. writel(BIT(offset), priv->base + GPIO_OES);
  37. return 0;
  38. }
  39. static int npcm_gpio_get_value(struct udevice *dev, unsigned int offset)
  40. {
  41. struct npcm_gpio_priv *priv = dev_get_priv(dev);
  42. if (readl(priv->base + GPIO_IEM) & BIT(offset))
  43. return !!(readl(priv->base + GPIO_DIN) & BIT(offset));
  44. if (readl(priv->base + GPIO_OE) & BIT(offset))
  45. return !!(readl(priv->base + GPIO_DOUT) & BIT(offset));
  46. return -EINVAL;
  47. }
  48. static int npcm_gpio_set_value(struct udevice *dev, unsigned int offset,
  49. int value)
  50. {
  51. struct npcm_gpio_priv *priv = dev_get_priv(dev);
  52. if (value)
  53. setbits_le32(priv->base + GPIO_DOUT, BIT(offset));
  54. else
  55. clrbits_le32(priv->base + GPIO_DOUT, BIT(offset));
  56. return 0;
  57. }
  58. static int npcm_gpio_get_function(struct udevice *dev, unsigned int offset)
  59. {
  60. struct npcm_gpio_priv *priv = dev_get_priv(dev);
  61. if (readl(priv->base + GPIO_IEM) & BIT(offset))
  62. return GPIOF_INPUT;
  63. if (readl(priv->base + GPIO_OE) & BIT(offset))
  64. return GPIOF_OUTPUT;
  65. return GPIOF_FUNC;
  66. }
  67. static const struct dm_gpio_ops npcm_gpio_ops = {
  68. .direction_input = npcm_gpio_direction_input,
  69. .direction_output = npcm_gpio_direction_output,
  70. .get_value = npcm_gpio_get_value,
  71. .set_value = npcm_gpio_set_value,
  72. .get_function = npcm_gpio_get_function,
  73. };
  74. static int npcm_gpio_probe(struct udevice *dev)
  75. {
  76. struct npcm_gpio_priv *priv = dev_get_priv(dev);
  77. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  78. priv->base = dev_read_addr_ptr(dev);
  79. uc_priv->gpio_count = NPCM_GPIOS_PER_BANK;
  80. uc_priv->bank_name = dev->name;
  81. return 0;
  82. }
  83. static const struct udevice_id npcm_gpio_match[] = {
  84. { .compatible = "nuvoton,npcm845-gpio" },
  85. { .compatible = "nuvoton,npcm750-gpio" },
  86. { }
  87. };
  88. U_BOOT_DRIVER(npcm_gpio) = {
  89. .name = "npcm_gpio",
  90. .id = UCLASS_GPIO,
  91. .of_match = npcm_gpio_match,
  92. .probe = npcm_gpio_probe,
  93. .priv_auto = sizeof(struct npcm_gpio_priv),
  94. .ops = &npcm_gpio_ops,
  95. };