mxc_gpio.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_MX51) || defined(CONFIG_MX53) || \
  42. defined(CONFIG_MX6Q)
  43. [3] = GPIO4_BASE_ADDR,
  44. #endif
  45. #if defined(CONFIG_MX53) || defined(CONFIG_MX6Q)
  46. [4] = GPIO5_BASE_ADDR,
  47. [5] = GPIO6_BASE_ADDR,
  48. [6] = GPIO7_BASE_ADDR,
  49. #endif
  50. };
  51. static int mxc_gpio_direction(unsigned int gpio,
  52. enum mxc_gpio_direction direction)
  53. {
  54. unsigned int port = GPIO_TO_PORT(gpio);
  55. struct gpio_regs *regs;
  56. u32 l;
  57. if (port >= ARRAY_SIZE(gpio_ports))
  58. return -1;
  59. gpio &= 0x1f;
  60. regs = (struct gpio_regs *)gpio_ports[port];
  61. l = readl(&regs->gpio_dir);
  62. switch (direction) {
  63. case MXC_GPIO_DIRECTION_OUT:
  64. l |= 1 << gpio;
  65. break;
  66. case MXC_GPIO_DIRECTION_IN:
  67. l &= ~(1 << gpio);
  68. }
  69. writel(l, &regs->gpio_dir);
  70. return 0;
  71. }
  72. int gpio_set_value(unsigned gpio, int value)
  73. {
  74. unsigned int port = GPIO_TO_PORT(gpio);
  75. struct gpio_regs *regs;
  76. u32 l;
  77. if (port >= ARRAY_SIZE(gpio_ports))
  78. return -1;
  79. gpio &= 0x1f;
  80. regs = (struct gpio_regs *)gpio_ports[port];
  81. l = readl(&regs->gpio_dr);
  82. if (value)
  83. l |= 1 << gpio;
  84. else
  85. l &= ~(1 << gpio);
  86. writel(l, &regs->gpio_dr);
  87. return 0;
  88. }
  89. int gpio_get_value(unsigned gpio)
  90. {
  91. unsigned int port = GPIO_TO_PORT(gpio);
  92. struct gpio_regs *regs;
  93. u32 val;
  94. if (port >= ARRAY_SIZE(gpio_ports))
  95. return -1;
  96. gpio &= 0x1f;
  97. regs = (struct gpio_regs *)gpio_ports[port];
  98. val = (readl(&regs->gpio_dr) >> gpio) & 0x01;
  99. return val;
  100. }
  101. int gpio_request(unsigned gpio, const char *label)
  102. {
  103. unsigned int port = GPIO_TO_PORT(gpio);
  104. if (port >= ARRAY_SIZE(gpio_ports))
  105. return -1;
  106. return 0;
  107. }
  108. int gpio_free(unsigned gpio)
  109. {
  110. return 0;
  111. }
  112. int gpio_direction_input(unsigned gpio)
  113. {
  114. return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
  115. }
  116. int gpio_direction_output(unsigned gpio, int value)
  117. {
  118. int ret = mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
  119. if (ret < 0)
  120. return ret;
  121. return gpio_set_value(gpio, value);
  122. }