dwapb_gpio.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. DECLARE_GLOBAL_DATA_PTR;
  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_platdata {
  29. const char *name;
  30. int bank;
  31. int pins;
  32. fdt_addr_t base;
  33. };
  34. static int dwapb_gpio_direction_input(struct udevice *dev, unsigned pin)
  35. {
  36. struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
  37. clrbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin);
  38. return 0;
  39. }
  40. static int dwapb_gpio_direction_output(struct udevice *dev, unsigned pin,
  41. int val)
  42. {
  43. struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
  44. setbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin);
  45. if (val)
  46. setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
  47. else
  48. clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
  49. return 0;
  50. }
  51. static int dwapb_gpio_get_value(struct udevice *dev, unsigned pin)
  52. {
  53. struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
  54. return !!(readl(plat->base + GPIO_EXT_PORT(plat->bank)) & (1 << pin));
  55. }
  56. static int dwapb_gpio_set_value(struct udevice *dev, unsigned pin, int val)
  57. {
  58. struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
  59. if (val)
  60. setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
  61. else
  62. clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
  63. return 0;
  64. }
  65. static const struct dm_gpio_ops gpio_dwapb_ops = {
  66. .direction_input = dwapb_gpio_direction_input,
  67. .direction_output = dwapb_gpio_direction_output,
  68. .get_value = dwapb_gpio_get_value,
  69. .set_value = dwapb_gpio_set_value,
  70. };
  71. static int gpio_dwapb_probe(struct udevice *dev)
  72. {
  73. struct gpio_dev_priv *priv = dev_get_uclass_priv(dev);
  74. struct gpio_dwapb_platdata *plat = dev->platdata;
  75. if (!plat)
  76. return 0;
  77. priv->gpio_count = plat->pins;
  78. priv->bank_name = plat->name;
  79. return 0;
  80. }
  81. static int gpio_dwapb_bind(struct udevice *dev)
  82. {
  83. struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
  84. const void *blob = gd->fdt_blob;
  85. struct udevice *subdev;
  86. fdt_addr_t base;
  87. int ret, node, bank = 0;
  88. /* If this is a child device, there is nothing to do here */
  89. if (plat)
  90. return 0;
  91. base = fdtdec_get_addr(blob, dev_of_offset(dev), "reg");
  92. if (base == FDT_ADDR_T_NONE) {
  93. debug("Can't get the GPIO register base address\n");
  94. return -ENXIO;
  95. }
  96. for (node = fdt_first_subnode(blob, dev_of_offset(dev));
  97. node > 0;
  98. node = fdt_next_subnode(blob, node)) {
  99. if (!fdtdec_get_bool(blob, node, "gpio-controller"))
  100. continue;
  101. plat = NULL;
  102. plat = calloc(1, sizeof(*plat));
  103. if (!plat)
  104. return -ENOMEM;
  105. plat->base = base;
  106. plat->bank = bank;
  107. plat->pins = fdtdec_get_int(blob, node, "snps,nr-gpios", 0);
  108. plat->name = fdt_stringlist_get(blob, node, "bank-name", 0,
  109. NULL);
  110. if (ret)
  111. goto err;
  112. ret = device_bind(dev, dev->driver, plat->name,
  113. plat, -1, &subdev);
  114. if (ret)
  115. goto err;
  116. dev_set_of_offset(subdev, node);
  117. bank++;
  118. }
  119. return 0;
  120. err:
  121. free(plat);
  122. return ret;
  123. }
  124. static const struct udevice_id gpio_dwapb_ids[] = {
  125. { .compatible = "snps,dw-apb-gpio" },
  126. { }
  127. };
  128. U_BOOT_DRIVER(gpio_dwapb) = {
  129. .name = "gpio-dwapb",
  130. .id = UCLASS_GPIO,
  131. .of_match = gpio_dwapb_ids,
  132. .ops = &gpio_dwapb_ops,
  133. .bind = gpio_dwapb_bind,
  134. .probe = gpio_dwapb_probe,
  135. };