pic32_gpio.c 3.7 KB

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