sandbox.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * See file CREDITS for list of people who contributed to this
  4. * project.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. #include <common.h>
  22. #include <asm/gpio.h>
  23. /* Flags for each GPIO */
  24. #define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
  25. #define GPIOF_HIGH (1 << 1) /* Currently set high */
  26. #define GPIOF_RESERVED (1 << 2) /* Is in use / requested */
  27. struct gpio_state {
  28. const char *label; /* label given by requester */
  29. u8 flags; /* flags (GPIOF_...) */
  30. };
  31. /*
  32. * State of GPIOs
  33. * TODO: Put this into sandbox state
  34. */
  35. static struct gpio_state state[CONFIG_SANDBOX_GPIO_COUNT];
  36. /* Access routines for GPIO state */
  37. static u8 *get_gpio_flags(unsigned gp)
  38. {
  39. /* assert()'s could be disabled, so make sure we handle that */
  40. assert(gp < ARRAY_SIZE(state));
  41. if (gp >= ARRAY_SIZE(state)) {
  42. static u8 invalid_flags;
  43. printf("sandbox_gpio: error: invalid gpio %u\n", gp);
  44. return &invalid_flags;
  45. }
  46. return &state[gp].flags;
  47. }
  48. static int get_gpio_flag(unsigned gp, int flag)
  49. {
  50. return (*get_gpio_flags(gp) & flag) != 0;
  51. }
  52. static int set_gpio_flag(unsigned gp, int flag, int value)
  53. {
  54. u8 *gpio = get_gpio_flags(gp);
  55. if (value)
  56. *gpio |= flag;
  57. else
  58. *gpio &= ~flag;
  59. return 0;
  60. }
  61. static int check_reserved(unsigned gpio, const char *func)
  62. {
  63. if (!get_gpio_flag(gpio, GPIOF_RESERVED)) {
  64. printf("sandbox_gpio: %s: error: gpio %u not reserved\n",
  65. func, gpio);
  66. return -1;
  67. }
  68. return 0;
  69. }
  70. /*
  71. * Back-channel sandbox-internal-only access to GPIO state
  72. */
  73. int sandbox_gpio_get_value(unsigned gp)
  74. {
  75. if (get_gpio_flag(gp, GPIOF_OUTPUT))
  76. debug("sandbox_gpio: get_value on output gpio %u\n", gp);
  77. return get_gpio_flag(gp, GPIOF_HIGH);
  78. }
  79. int sandbox_gpio_set_value(unsigned gp, int value)
  80. {
  81. return set_gpio_flag(gp, GPIOF_HIGH, value);
  82. }
  83. int sandbox_gpio_get_direction(unsigned gp)
  84. {
  85. return get_gpio_flag(gp, GPIOF_OUTPUT);
  86. }
  87. int sandbox_gpio_set_direction(unsigned gp, int output)
  88. {
  89. return set_gpio_flag(gp, GPIOF_OUTPUT, output);
  90. }
  91. /*
  92. * These functions implement the public interface within U-Boot
  93. */
  94. /* set GPIO port 'gp' as an input */
  95. int gpio_direction_input(unsigned gp)
  96. {
  97. debug("%s: gp:%u\n", __func__, gp);
  98. if (check_reserved(gp, __func__))
  99. return -1;
  100. return sandbox_gpio_set_direction(gp, 0);
  101. }
  102. /* set GPIO port 'gp' as an output, with polarity 'value' */
  103. int gpio_direction_output(unsigned gp, int value)
  104. {
  105. debug("%s: gp:%u, value = %d\n", __func__, gp, value);
  106. if (check_reserved(gp, __func__))
  107. return -1;
  108. return sandbox_gpio_set_direction(gp, 1) |
  109. sandbox_gpio_set_value(gp, value);
  110. }
  111. /* read GPIO IN value of port 'gp' */
  112. int gpio_get_value(unsigned gp)
  113. {
  114. debug("%s: gp:%u\n", __func__, gp);
  115. if (check_reserved(gp, __func__))
  116. return -1;
  117. return sandbox_gpio_get_value(gp);
  118. }
  119. /* write GPIO OUT value to port 'gp' */
  120. int gpio_set_value(unsigned gp, int value)
  121. {
  122. debug("%s: gp:%u, value = %d\n", __func__, gp, value);
  123. if (check_reserved(gp, __func__))
  124. return -1;
  125. if (!sandbox_gpio_get_direction(gp)) {
  126. printf("sandbox_gpio: error: set_value on input gpio %u\n", gp);
  127. return -1;
  128. }
  129. return sandbox_gpio_set_value(gp, value);
  130. }
  131. int gpio_request(unsigned gp, const char *label)
  132. {
  133. debug("%s: gp:%u, label:%s\n", __func__, gp, label);
  134. if (gp >= ARRAY_SIZE(state)) {
  135. printf("sandbox_gpio: error: invalid gpio %u\n", gp);
  136. return -1;
  137. }
  138. if (get_gpio_flag(gp, GPIOF_RESERVED)) {
  139. printf("sandbox_gpio: error: gpio %u already reserved\n", gp);
  140. return -1;
  141. }
  142. state[gp].label = label;
  143. return set_gpio_flag(gp, GPIOF_RESERVED, 1);
  144. }
  145. int gpio_free(unsigned gp)
  146. {
  147. debug("%s: gp:%u\n", __func__, gp);
  148. if (check_reserved(gp, __func__))
  149. return -1;
  150. state[gp].label = NULL;
  151. return set_gpio_flag(gp, GPIOF_RESERVED, 0);
  152. }
  153. /* Display GPIO information */
  154. void gpio_info(void)
  155. {
  156. unsigned gpio;
  157. puts("Sandbox GPIOs\n");
  158. for (gpio = 0; gpio < ARRAY_SIZE(state); ++gpio) {
  159. const char *label = state[gpio].label;
  160. printf("%4d: %s: %d [%c] %s\n",
  161. gpio,
  162. sandbox_gpio_get_direction(gpio) ? "out" : " in",
  163. sandbox_gpio_get_value(gpio),
  164. get_gpio_flag(gpio, GPIOF_RESERVED) ? 'x' : ' ',
  165. label ? label : "");
  166. }
  167. }