kw_gpio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/io.h>
  33. #include <asm/arch/kirkwood.h>
  34. #include <asm/arch/gpio.h>
  35. static unsigned long gpio_valid_input[BITS_TO_LONGS(GPIO_MAX)];
  36. static unsigned long gpio_valid_output[BITS_TO_LONGS(GPIO_MAX)];
  37. void __set_direction(unsigned pin, int input)
  38. {
  39. u32 u;
  40. u = readl(GPIO_IO_CONF(pin));
  41. if (input)
  42. u |= 1 << (pin & 31);
  43. else
  44. u &= ~(1 << (pin & 31));
  45. writel(u, GPIO_IO_CONF(pin));
  46. u = readl(GPIO_IO_CONF(pin));
  47. }
  48. void __set_level(unsigned pin, int high)
  49. {
  50. u32 u;
  51. u = readl(GPIO_OUT(pin));
  52. if (high)
  53. u |= 1 << (pin & 31);
  54. else
  55. u &= ~(1 << (pin & 31));
  56. writel(u, GPIO_OUT(pin));
  57. }
  58. void __set_blinking(unsigned pin, int blink)
  59. {
  60. u32 u;
  61. u = readl(GPIO_BLINK_EN(pin));
  62. if (blink)
  63. u |= 1 << (pin & 31);
  64. else
  65. u &= ~(1 << (pin & 31));
  66. writel(u, GPIO_BLINK_EN(pin));
  67. }
  68. int kw_gpio_is_valid(unsigned pin, int mode)
  69. {
  70. if (pin < GPIO_MAX) {
  71. if ((mode & GPIO_INPUT_OK) && !test_bit(pin, gpio_valid_input))
  72. goto err_out;
  73. if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, gpio_valid_output))
  74. goto err_out;
  75. return 0;
  76. }
  77. err_out:
  78. printf("%s: invalid GPIO %d\n", __func__, pin);
  79. return 1;
  80. }
  81. void kw_gpio_set_valid(unsigned pin, int mode)
  82. {
  83. if (mode == 1)
  84. mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
  85. if (mode & GPIO_INPUT_OK)
  86. __set_bit(pin, gpio_valid_input);
  87. else
  88. __clear_bit(pin, gpio_valid_input);
  89. if (mode & GPIO_OUTPUT_OK)
  90. __set_bit(pin, gpio_valid_output);
  91. else
  92. __clear_bit(pin, gpio_valid_output);
  93. }
  94. /*
  95. * GENERIC_GPIO primitives.
  96. */
  97. int kw_gpio_direction_input(unsigned pin)
  98. {
  99. if (kw_gpio_is_valid(pin, GPIO_INPUT_OK) != 0)
  100. return 1;
  101. /* Configure GPIO direction. */
  102. __set_direction(pin, 1);
  103. return 0;
  104. }
  105. int kw_gpio_direction_output(unsigned pin, int value)
  106. {
  107. if (kw_gpio_is_valid(pin, GPIO_OUTPUT_OK) != 0)
  108. {
  109. printf("%s: invalid GPIO %d\n", __func__, pin);
  110. return 1;
  111. }
  112. __set_blinking(pin, 0);
  113. /* Configure GPIO output value. */
  114. __set_level(pin, value);
  115. /* Configure GPIO direction. */
  116. __set_direction(pin, 0);
  117. return 0;
  118. }
  119. int kw_gpio_get_value(unsigned pin)
  120. {
  121. int val;
  122. if (readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31)))
  123. val = readl(GPIO_DATA_IN(pin)) ^ readl(GPIO_IN_POL(pin));
  124. else
  125. val = readl(GPIO_OUT(pin));
  126. return (val >> (pin & 31)) & 1;
  127. }
  128. void kw_gpio_set_value(unsigned pin, int value)
  129. {
  130. /* Configure GPIO output value. */
  131. __set_level(pin, value);
  132. }
  133. void kw_gpio_set_blink(unsigned pin, int blink)
  134. {
  135. /* Set output value to zero. */
  136. __set_level(pin, 0);
  137. /* Set blinking. */
  138. __set_blinking(pin, blink);
  139. }