dwapb_gpio.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015 Marek Vasut <marex@denx.de>
  4. *
  5. * DesignWare APB GPIO driver
  6. */
  7. #include <common.h>
  8. #include <malloc.h>
  9. #include <asm/arch/gpio.h>
  10. #include <asm/gpio.h>
  11. #include <asm/io.h>
  12. #include <dm.h>
  13. #include <dm/device-internal.h>
  14. #include <dm/lists.h>
  15. #include <dm/root.h>
  16. #include <errno.h>
  17. #include <reset.h>
  18. #define GPIO_SWPORT_DR(p) (0x00 + (p) * 0xc)
  19. #define GPIO_SWPORT_DDR(p) (0x04 + (p) * 0xc)
  20. #define GPIO_INTEN 0x30
  21. #define GPIO_INTMASK 0x34
  22. #define GPIO_INTTYPE_LEVEL 0x38
  23. #define GPIO_INT_POLARITY 0x3c
  24. #define GPIO_INTSTATUS 0x40
  25. #define GPIO_PORTA_DEBOUNCE 0x48
  26. #define GPIO_PORTA_EOI 0x4c
  27. #define GPIO_EXT_PORT(p) (0x50 + (p) * 4)
  28. struct gpio_dwapb_priv {
  29. struct reset_ctl_bulk resets;
  30. };
  31. struct gpio_dwapb_platdata {
  32. const char *name;
  33. int bank;
  34. int pins;
  35. fdt_addr_t base;
  36. };
  37. static int dwapb_gpio_direction_input(struct udevice *dev, unsigned pin)
  38. {
  39. struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
  40. clrbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin);
  41. return 0;
  42. }
  43. static int dwapb_gpio_direction_output(struct udevice *dev, unsigned pin,
  44. int val)
  45. {
  46. struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
  47. setbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin);
  48. if (val)
  49. setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
  50. else
  51. clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
  52. return 0;
  53. }
  54. static int dwapb_gpio_get_value(struct udevice *dev, unsigned pin)
  55. {
  56. struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
  57. return !!(readl(plat->base + GPIO_EXT_PORT(plat->bank)) & (1 << pin));
  58. }
  59. static int dwapb_gpio_set_value(struct udevice *dev, unsigned pin, int val)
  60. {
  61. struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
  62. if (val)
  63. setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
  64. else
  65. clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
  66. return 0;
  67. }
  68. static int dwapb_gpio_get_function(struct udevice *dev, unsigned offset)
  69. {
  70. struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
  71. u32 gpio;
  72. gpio = readl(plat->base + GPIO_SWPORT_DDR(plat->bank));
  73. if (gpio & BIT(offset))
  74. return GPIOF_OUTPUT;
  75. else
  76. return GPIOF_INPUT;
  77. }
  78. static const struct dm_gpio_ops gpio_dwapb_ops = {
  79. .direction_input = dwapb_gpio_direction_input,
  80. .direction_output = dwapb_gpio_direction_output,
  81. .get_value = dwapb_gpio_get_value,
  82. .set_value = dwapb_gpio_set_value,
  83. .get_function = dwapb_gpio_get_function,
  84. };
  85. static int gpio_dwapb_reset(struct udevice *dev)
  86. {
  87. int ret;
  88. struct gpio_dwapb_priv *priv = dev_get_priv(dev);
  89. ret = reset_get_bulk(dev, &priv->resets);
  90. if (ret) {
  91. /* Return 0 if error due to !CONFIG_DM_RESET and reset
  92. * DT property is not present.
  93. */
  94. if (ret == -ENOENT || ret == -ENOTSUPP)
  95. return 0;
  96. dev_warn(dev, "Can't get reset: %d\n", ret);
  97. return ret;
  98. }
  99. ret = reset_deassert_bulk(&priv->resets);
  100. if (ret) {
  101. reset_release_bulk(&priv->resets);
  102. dev_err(dev, "Failed to reset: %d\n", ret);
  103. return ret;
  104. }
  105. return 0;
  106. }
  107. static int gpio_dwapb_probe(struct udevice *dev)
  108. {
  109. struct gpio_dev_priv *priv = dev_get_uclass_priv(dev);
  110. struct gpio_dwapb_platdata *plat = dev->platdata;
  111. if (!plat) {
  112. /* Reset on parent device only */
  113. return gpio_dwapb_reset(dev);
  114. }
  115. priv->gpio_count = plat->pins;
  116. priv->bank_name = plat->name;
  117. return 0;
  118. }
  119. static int gpio_dwapb_bind(struct udevice *dev)
  120. {
  121. struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
  122. struct udevice *subdev;
  123. fdt_addr_t base;
  124. int ret, bank = 0;
  125. ofnode node;
  126. /* If this is a child device, there is nothing to do here */
  127. if (plat)
  128. return 0;
  129. base = dev_read_addr(dev);
  130. if (base == FDT_ADDR_T_NONE) {
  131. debug("Can't get the GPIO register base address\n");
  132. return -ENXIO;
  133. }
  134. for (node = dev_read_first_subnode(dev); ofnode_valid(node);
  135. node = dev_read_next_subnode(node)) {
  136. if (!ofnode_read_bool(node, "gpio-controller"))
  137. continue;
  138. plat = devm_kcalloc(dev, 1, sizeof(*plat), GFP_KERNEL);
  139. if (!plat)
  140. return -ENOMEM;
  141. plat->base = base;
  142. plat->bank = bank;
  143. plat->pins = ofnode_read_u32_default(node, "snps,nr-gpios", 0);
  144. if (ofnode_read_string_index(node, "bank-name", 0,
  145. &plat->name)) {
  146. /*
  147. * Fall back to node name. This means accessing pins
  148. * via bank name won't work.
  149. */
  150. plat->name = ofnode_get_name(node);
  151. }
  152. ret = device_bind(dev, dev->driver, plat->name,
  153. plat, node.of_offset, &subdev);
  154. if (ret)
  155. return ret;
  156. bank++;
  157. }
  158. return 0;
  159. }
  160. static int gpio_dwapb_remove(struct udevice *dev)
  161. {
  162. struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
  163. struct gpio_dwapb_priv *priv = dev_get_priv(dev);
  164. if (!plat && priv)
  165. return reset_release_bulk(&priv->resets);
  166. return 0;
  167. }
  168. static const struct udevice_id gpio_dwapb_ids[] = {
  169. { .compatible = "snps,dw-apb-gpio" },
  170. { }
  171. };
  172. U_BOOT_DRIVER(gpio_dwapb) = {
  173. .name = "gpio-dwapb",
  174. .id = UCLASS_GPIO,
  175. .of_match = gpio_dwapb_ids,
  176. .ops = &gpio_dwapb_ops,
  177. .bind = gpio_dwapb_bind,
  178. .probe = gpio_dwapb_probe,
  179. .remove = gpio_dwapb_remove,
  180. .priv_auto_alloc_size = sizeof(struct gpio_dwapb_priv),
  181. };