starfive-gpio.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2022-2023 StarFive Technology Co., Ltd.
  4. * Author: yanhong <yanhong.wang@starfivetech.com>
  5. *
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <asm/arch/gpio.h>
  10. #include <asm/io.h>
  11. #include <errno.h>
  12. #include <asm/gpio.h>
  13. #include <linux/bitops.h>
  14. static int starfive_gpio_probe(struct udevice *dev)
  15. {
  16. struct starfive_gpio_platdata *plat = dev_get_plat(dev);
  17. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  18. char name[18], *str;
  19. sprintf(name, "gpio@%4lx_", (uintptr_t)plat->base);
  20. str = strdup(name);
  21. if (!str)
  22. return -ENOMEM;
  23. uc_priv->bank_name = str;
  24. /*
  25. * Use the gpio count mentioned in device tree,
  26. * if not specified in dt, set NR_GPIOS as default
  27. */
  28. uc_priv->gpio_count = dev_read_u32_default(dev, "ngpios", NR_GPIOS);
  29. return 0;
  30. }
  31. static int starfive_gpio_direction_input(struct udevice *dev, u32 offset)
  32. {
  33. struct starfive_gpio_platdata *plat = dev_get_plat(dev);
  34. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  35. if (offset > uc_priv->gpio_count)
  36. return -EINVAL;
  37. /* Configure gpio direction as input */
  38. clrsetbits_le32(plat->base + GPIO_OFFSET(offset),
  39. GPIO_DOEN_MASK << GPIO_SHIFT(offset),
  40. HIGH << GPIO_SHIFT(offset));
  41. return 0;
  42. }
  43. static int starfive_gpio_direction_output(struct udevice *dev, u32 offset,
  44. int value)
  45. {
  46. struct starfive_gpio_platdata *plat = dev_get_plat(dev);
  47. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  48. if (offset > uc_priv->gpio_count)
  49. return -EINVAL;
  50. /* Configure gpio direction as output */
  51. clrsetbits_le32(plat->base + GPIO_OFFSET(offset),
  52. GPIO_DOEN_MASK << GPIO_SHIFT(offset),
  53. LOW << GPIO_SHIFT(offset));
  54. /* Set the output value of the pin */
  55. clrsetbits_le32(plat->base + GPIO_DOUT + GPIO_OFFSET(offset),
  56. GPIO_DOUT_MASK << GPIO_SHIFT(offset),
  57. (value & GPIO_DOUT_MASK) << GPIO_SHIFT(offset));
  58. return 0;
  59. }
  60. static int starfive_gpio_get_value(struct udevice *dev, u32 offset)
  61. {
  62. struct starfive_gpio_platdata *plat = dev_get_plat(dev);
  63. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  64. int val;
  65. if (offset > uc_priv->gpio_count)
  66. return -EINVAL;
  67. /*Get dout value*/
  68. val = readl(plat->base + GPIO_DIN + GPIO_OFFSET(offset));
  69. val &= GPIO_DIN_MASK << GPIO_SHIFT(offset);
  70. return val ? HIGH : LOW;
  71. }
  72. static int starfive_gpio_set_value(struct udevice *dev, u32 offset, int value)
  73. {
  74. struct starfive_gpio_platdata *plat = dev_get_plat(dev);
  75. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  76. if (offset > uc_priv->gpio_count)
  77. return -EINVAL;
  78. clrsetbits_le32(plat->base + GPIO_DOUT + GPIO_OFFSET(offset),
  79. GPIO_DOUT_MASK << GPIO_SHIFT(offset),
  80. (value & GPIO_DOUT_MASK) << GPIO_SHIFT(offset));
  81. return 0;
  82. }
  83. static int starfive_gpio_get_function(struct udevice *dev, unsigned int offset)
  84. {
  85. struct starfive_gpio_platdata *plat = dev_get_plat(dev);
  86. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  87. u32 dir, val;
  88. if (offset > uc_priv->gpio_count)
  89. return -1;
  90. /*Get doen value*/
  91. val = readl(plat->base + GPIO_OFFSET(offset));
  92. val &= (GPIO_DOEN_MASK << GPIO_SHIFT(offset));
  93. dir = (val > 1) ? GPIOF_UNUSED : (val ? GPIOF_INPUT : GPIOF_OUTPUT);
  94. return dir;
  95. }
  96. static int starfive_gpio_ofdata_to_platdata(struct udevice *dev)
  97. {
  98. struct starfive_gpio_platdata *plat = dev_get_plat(dev);
  99. fdt_addr_t addr;
  100. addr = dev_read_addr(dev);
  101. if (addr == FDT_ADDR_T_NONE)
  102. return -EINVAL;
  103. plat->base = (void *)addr;
  104. writel(0, plat->base + GPIO_EN);
  105. writel(0, plat->base + GPIO_LOW_IE);
  106. writel(0, plat->base + GPIO_HIGH_IE);
  107. writel(1, plat->base + GPIO_EN);
  108. return 0;
  109. }
  110. static const struct udevice_id starfive_gpio_match[] = {
  111. { .compatible = "starfive,jh7110-gpio" },
  112. { }
  113. };
  114. static const struct dm_gpio_ops starfive_gpio_ops = {
  115. .direction_input = starfive_gpio_direction_input,
  116. .direction_output = starfive_gpio_direction_output,
  117. .get_value = starfive_gpio_get_value,
  118. .set_value = starfive_gpio_set_value,
  119. .get_function = starfive_gpio_get_function,
  120. };
  121. U_BOOT_DRIVER(gpio_starfive) = {
  122. .name = "gpio_starfive",
  123. .id = UCLASS_GPIO,
  124. .of_match = starfive_gpio_match,
  125. .of_to_plat = starfive_gpio_ofdata_to_platdata,
  126. .plat_auto = sizeof(struct starfive_gpio_platdata),
  127. .ops = &starfive_gpio_ops,
  128. .probe = starfive_gpio_probe,
  129. };