mxc_gpio.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2009
  3. * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
  4. *
  5. * Copyright (C) 2011
  6. * Stefano Babic, DENX Software Engineering, <sbabic@denx.de>
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <asm/arch/imx-regs.h>
  28. #include <asm/gpio.h>
  29. #include <asm/io.h>
  30. #include <errno.h>
  31. enum mxc_gpio_direction {
  32. MXC_GPIO_DIRECTION_IN,
  33. MXC_GPIO_DIRECTION_OUT,
  34. };
  35. #define GPIO_TO_PORT(n) (n / 32)
  36. /* GPIO port description */
  37. static unsigned long gpio_ports[] = {
  38. [0] = GPIO1_BASE_ADDR,
  39. [1] = GPIO2_BASE_ADDR,
  40. [2] = GPIO3_BASE_ADDR,
  41. #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
  42. defined(CONFIG_MX53) || defined(CONFIG_MX6Q)
  43. [3] = GPIO4_BASE_ADDR,
  44. #endif
  45. #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6Q)
  46. [4] = GPIO5_BASE_ADDR,
  47. [5] = GPIO6_BASE_ADDR,
  48. #endif
  49. #if defined(CONFIG_MX53) || defined(CONFIG_MX6Q)
  50. [6] = GPIO7_BASE_ADDR,
  51. #endif
  52. };
  53. static int mxc_gpio_direction(unsigned int gpio,
  54. enum mxc_gpio_direction direction)
  55. {
  56. unsigned int port = GPIO_TO_PORT(gpio);
  57. struct gpio_regs *regs;
  58. u32 l;
  59. if (port >= ARRAY_SIZE(gpio_ports))
  60. return -1;
  61. gpio &= 0x1f;
  62. regs = (struct gpio_regs *)gpio_ports[port];
  63. l = readl(&regs->gpio_dir);
  64. switch (direction) {
  65. case MXC_GPIO_DIRECTION_OUT:
  66. l |= 1 << gpio;
  67. break;
  68. case MXC_GPIO_DIRECTION_IN:
  69. l &= ~(1 << gpio);
  70. }
  71. writel(l, &regs->gpio_dir);
  72. return 0;
  73. }
  74. int gpio_set_value(unsigned gpio, int value)
  75. {
  76. unsigned int port = GPIO_TO_PORT(gpio);
  77. struct gpio_regs *regs;
  78. u32 l;
  79. if (port >= ARRAY_SIZE(gpio_ports))
  80. return -1;
  81. gpio &= 0x1f;
  82. regs = (struct gpio_regs *)gpio_ports[port];
  83. l = readl(&regs->gpio_dr);
  84. if (value)
  85. l |= 1 << gpio;
  86. else
  87. l &= ~(1 << gpio);
  88. writel(l, &regs->gpio_dr);
  89. return 0;
  90. }
  91. int gpio_get_value(unsigned gpio)
  92. {
  93. unsigned int port = GPIO_TO_PORT(gpio);
  94. struct gpio_regs *regs;
  95. u32 val;
  96. if (port >= ARRAY_SIZE(gpio_ports))
  97. return -1;
  98. gpio &= 0x1f;
  99. regs = (struct gpio_regs *)gpio_ports[port];
  100. val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
  101. return val;
  102. }
  103. int gpio_request(unsigned gpio, const char *label)
  104. {
  105. unsigned int port = GPIO_TO_PORT(gpio);
  106. if (port >= ARRAY_SIZE(gpio_ports))
  107. return -1;
  108. return 0;
  109. }
  110. int gpio_free(unsigned gpio)
  111. {
  112. return 0;
  113. }
  114. int gpio_direction_input(unsigned gpio)
  115. {
  116. return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
  117. }
  118. int gpio_direction_output(unsigned gpio, int value)
  119. {
  120. int ret = mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
  121. if (ret < 0)
  122. return ret;
  123. return gpio_set_value(gpio, value);
  124. }