kw_gpio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * arch/arm/plat-orion/gpio.c
  3. *
  4. * Marvell Orion SoC GPIO handling.
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. * MA 02110-1301 USA
  23. */
  24. /*
  25. * Based on (mostly copied from) plat-orion based Linux 2.6 kernel driver.
  26. * Removed orion_gpiochip struct and kernel level irq handling.
  27. *
  28. * Dieter Kiermaier dk-arm-linux@gmx.de
  29. */
  30. #include <common.h>
  31. #include <asm/bitops.h>
  32. #include <asm/arch/kirkwood.h>
  33. #include <asm/arch/gpio.h>
  34. static unsigned long gpio_valid_input[BITS_TO_LONGS(GPIO_MAX)];
  35. static unsigned long gpio_valid_output[BITS_TO_LONGS(GPIO_MAX)];
  36. void __set_direction(unsigned pin, int input)
  37. {
  38. u32 u;
  39. u = readl(GPIO_IO_CONF(pin));
  40. if (input)
  41. u |= 1 << (pin & 31);
  42. else
  43. u &= ~(1 << (pin & 31));
  44. writel(u, GPIO_IO_CONF(pin));
  45. u = readl(GPIO_IO_CONF(pin));
  46. }
  47. void __set_level(unsigned pin, int high)
  48. {
  49. u32 u;
  50. u = readl(GPIO_OUT(pin));
  51. if (high)
  52. u |= 1 << (pin & 31);
  53. else
  54. u &= ~(1 << (pin & 31));
  55. writel(u, GPIO_OUT(pin));
  56. }
  57. void __set_blinking(unsigned pin, int blink)
  58. {
  59. u32 u;
  60. u = readl(GPIO_BLINK_EN(pin));
  61. if (blink)
  62. u |= 1 << (pin & 31);
  63. else
  64. u &= ~(1 << (pin & 31));
  65. writel(u, GPIO_BLINK_EN(pin));
  66. }
  67. int kw_gpio_is_valid(unsigned pin, int mode)
  68. {
  69. if (pin < GPIO_MAX) {
  70. if ((mode & GPIO_INPUT_OK) && !test_bit(pin, gpio_valid_input))
  71. goto err_out;
  72. if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, gpio_valid_output))
  73. goto err_out;
  74. return 0;
  75. }
  76. err_out:
  77. printf("%s: invalid GPIO %d\n", __func__, pin);
  78. return 1;
  79. }
  80. void kw_gpio_set_valid(unsigned pin, int mode)
  81. {
  82. if (mode == 1)
  83. mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
  84. if (mode & GPIO_INPUT_OK)
  85. __set_bit(pin, gpio_valid_input);
  86. else
  87. __clear_bit(pin, gpio_valid_input);
  88. if (mode & GPIO_OUTPUT_OK)
  89. __set_bit(pin, gpio_valid_output);
  90. else
  91. __clear_bit(pin, gpio_valid_output);
  92. }
  93. /*
  94. * GENERIC_GPIO primitives.
  95. */
  96. int kw_gpio_direction_input(unsigned pin)
  97. {
  98. if (!kw_gpio_is_valid(pin, GPIO_INPUT_OK))
  99. return 1;
  100. /* Configure GPIO direction. */
  101. __set_direction(pin, 1);
  102. return 0;
  103. }
  104. int kw_gpio_direction_output(unsigned pin, int value)
  105. {
  106. if (kw_gpio_is_valid(pin, GPIO_OUTPUT_OK) != 0)
  107. {
  108. printf("%s: invalid GPIO %d\n", __func__, pin);
  109. return 1;
  110. }
  111. __set_blinking(pin, 0);
  112. /* Configure GPIO output value. */
  113. __set_level(pin, value);
  114. /* Configure GPIO direction. */
  115. __set_direction(pin, 0);
  116. return 0;
  117. }
  118. int kw_gpio_get_value(unsigned pin)
  119. {
  120. int val;
  121. if (readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31)))
  122. val = readl(GPIO_DATA_IN(pin)) ^ readl(GPIO_IN_POL(pin));
  123. else
  124. val = readl(GPIO_OUT(pin));
  125. return (val >> (pin & 31)) & 1;
  126. }
  127. void kw_gpio_set_value(unsigned pin, int value)
  128. {
  129. /* Configure GPIO output value. */
  130. __set_level(pin, value);
  131. }
  132. void kw_gpio_set_blink(unsigned pin, int blink)
  133. {
  134. /* Set output value to zero. */
  135. __set_level(pin, 0);
  136. /* Set blinking. */
  137. __set_blinking(pin, blink);
  138. }