pic32_gpio.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2015 Microchip Technology Inc
  4. * Purna Chandra Mandal <purna.mandal@microchip.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <malloc.h>
  10. #include <asm/io.h>
  11. #include <asm/gpio.h>
  12. #include <linux/compat.h>
  13. #include <mach/pic32.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. /* Peripheral Pin Control */
  16. struct pic32_reg_port {
  17. struct pic32_reg_atomic ansel;
  18. struct pic32_reg_atomic tris;
  19. struct pic32_reg_atomic port;
  20. struct pic32_reg_atomic lat;
  21. struct pic32_reg_atomic open_drain;
  22. struct pic32_reg_atomic cnpu;
  23. struct pic32_reg_atomic cnpd;
  24. struct pic32_reg_atomic cncon;
  25. };
  26. enum {
  27. MICROCHIP_GPIO_DIR_OUT,
  28. MICROCHIP_GPIO_DIR_IN,
  29. MICROCHIP_GPIOS_PER_BANK = 16,
  30. };
  31. struct pic32_gpio_priv {
  32. struct pic32_reg_port *regs;
  33. char name[2];
  34. };
  35. static int pic32_gpio_get_value(struct udevice *dev, unsigned offset)
  36. {
  37. struct pic32_gpio_priv *priv = dev_get_priv(dev);
  38. return !!(readl(&priv->regs->port.raw) & BIT(offset));
  39. }
  40. static int pic32_gpio_set_value(struct udevice *dev, unsigned offset,
  41. int value)
  42. {
  43. struct pic32_gpio_priv *priv = dev_get_priv(dev);
  44. int mask = BIT(offset);
  45. if (value)
  46. writel(mask, &priv->regs->port.set);
  47. else
  48. writel(mask, &priv->regs->port.clr);
  49. return 0;
  50. }
  51. static int pic32_gpio_direction(struct udevice *dev, unsigned offset)
  52. {
  53. struct pic32_gpio_priv *priv = dev_get_priv(dev);
  54. /* pin in analog mode ? */
  55. if (readl(&priv->regs->ansel.raw) & BIT(offset))
  56. return -EPERM;
  57. if (readl(&priv->regs->tris.raw) & BIT(offset))
  58. return MICROCHIP_GPIO_DIR_IN;
  59. else
  60. return MICROCHIP_GPIO_DIR_OUT;
  61. }
  62. static int pic32_gpio_direction_input(struct udevice *dev, unsigned offset)
  63. {
  64. struct pic32_gpio_priv *priv = dev_get_priv(dev);
  65. int mask = BIT(offset);
  66. writel(mask, &priv->regs->ansel.clr);
  67. writel(mask, &priv->regs->tris.set);
  68. return 0;
  69. }
  70. static int pic32_gpio_direction_output(struct udevice *dev,
  71. unsigned offset, int value)
  72. {
  73. struct pic32_gpio_priv *priv = dev_get_priv(dev);
  74. int mask = BIT(offset);
  75. writel(mask, &priv->regs->ansel.clr);
  76. writel(mask, &priv->regs->tris.clr);
  77. pic32_gpio_set_value(dev, offset, value);
  78. return 0;
  79. }
  80. static int pic32_gpio_get_function(struct udevice *dev, unsigned offset)
  81. {
  82. int ret = GPIOF_UNUSED;
  83. switch (pic32_gpio_direction(dev, offset)) {
  84. case MICROCHIP_GPIO_DIR_OUT:
  85. ret = GPIOF_OUTPUT;
  86. break;
  87. case MICROCHIP_GPIO_DIR_IN:
  88. ret = GPIOF_INPUT;
  89. break;
  90. default:
  91. ret = GPIOF_UNUSED;
  92. break;
  93. }
  94. return ret;
  95. }
  96. static const struct dm_gpio_ops gpio_pic32_ops = {
  97. .direction_input = pic32_gpio_direction_input,
  98. .direction_output = pic32_gpio_direction_output,
  99. .get_value = pic32_gpio_get_value,
  100. .set_value = pic32_gpio_set_value,
  101. .get_function = pic32_gpio_get_function,
  102. };
  103. static int pic32_gpio_probe(struct udevice *dev)
  104. {
  105. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  106. struct pic32_gpio_priv *priv = dev_get_priv(dev);
  107. fdt_addr_t addr;
  108. fdt_size_t size;
  109. char *end;
  110. int bank;
  111. addr = fdtdec_get_addr_size(gd->fdt_blob, dev_of_offset(dev), "reg",
  112. &size);
  113. if (addr == FDT_ADDR_T_NONE)
  114. return -EINVAL;
  115. priv->regs = ioremap(addr, size);
  116. uc_priv->gpio_count = MICROCHIP_GPIOS_PER_BANK;
  117. /* extract bank name */
  118. end = strrchr(dev->name, '@');
  119. bank = trailing_strtoln(dev->name, end);
  120. priv->name[0] = 'A' + bank;
  121. uc_priv->bank_name = priv->name;
  122. return 0;
  123. }
  124. static const struct udevice_id pic32_gpio_ids[] = {
  125. { .compatible = "microchip,pic32mzda-gpio" },
  126. { }
  127. };
  128. U_BOOT_DRIVER(gpio_pic32) = {
  129. .name = "gpio_pic32",
  130. .id = UCLASS_GPIO,
  131. .of_match = pic32_gpio_ids,
  132. .ops = &gpio_pic32_ops,
  133. .probe = pic32_gpio_probe,
  134. .priv_auto_alloc_size = sizeof(struct pic32_gpio_priv),
  135. };