tegra2_gpio.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * NVIDIA Tegra2 GPIO handling.
  3. * (C) Copyright 2010,2011
  4. * NVIDIA Corporation <www.nvidia.com>
  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., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. /*
  25. * Based on (mostly copied from) kw_gpio.c based Linux 2.6 kernel driver.
  26. * Tom Warren (twarren@nvidia.com)
  27. */
  28. #include <common.h>
  29. #include <asm/io.h>
  30. #include <asm/bitops.h>
  31. #include <asm/arch/tegra2.h>
  32. #include <asm/gpio.h>
  33. enum {
  34. TEGRA2_CMD_INFO,
  35. TEGRA2_CMD_PORT,
  36. TEGRA2_CMD_OUTPUT,
  37. TEGRA2_CMD_INPUT,
  38. };
  39. static struct gpio_names {
  40. char name[GPIO_NAME_SIZE];
  41. } gpio_names[MAX_NUM_GPIOS];
  42. static char *get_name(int i)
  43. {
  44. return *gpio_names[i].name ? gpio_names[i].name : "UNKNOWN";
  45. }
  46. /* Return config of pin 'gpio' as GPIO (1) or SFPIO (0) */
  47. static int get_config(unsigned gpio)
  48. {
  49. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  50. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  51. u32 u;
  52. int type;
  53. u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
  54. type = (u >> GPIO_BIT(gpio)) & 1;
  55. debug("get_config: port = %d, bit = %d is %s\n",
  56. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
  57. return type;
  58. }
  59. /* Config pin 'gpio' as GPIO or SFPIO, based on 'type' */
  60. static void set_config(unsigned gpio, int type)
  61. {
  62. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  63. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  64. u32 u;
  65. debug("set_config: port = %d, bit = %d, %s\n",
  66. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
  67. u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
  68. if (type) /* GPIO */
  69. u |= 1 << GPIO_BIT(gpio);
  70. else
  71. u &= ~(1 << GPIO_BIT(gpio));
  72. writel(u, &bank->gpio_config[GPIO_PORT(gpio)]);
  73. }
  74. /* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */
  75. static int get_direction(unsigned gpio)
  76. {
  77. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  78. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  79. u32 u;
  80. int dir;
  81. u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
  82. dir = (u >> GPIO_BIT(gpio)) & 1;
  83. debug("get_direction: port = %d, bit = %d, %s\n",
  84. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN");
  85. return dir;
  86. }
  87. /* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */
  88. static void set_direction(unsigned gpio, int output)
  89. {
  90. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  91. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  92. u32 u;
  93. debug("set_direction: port = %d, bit = %d, %s\n",
  94. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN");
  95. u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
  96. if (output)
  97. u |= 1 << GPIO_BIT(gpio);
  98. else
  99. u &= ~(1 << GPIO_BIT(gpio));
  100. writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]);
  101. }
  102. /* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */
  103. static void set_level(unsigned gpio, int high)
  104. {
  105. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  106. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  107. u32 u;
  108. debug("set_level: port = %d, bit %d == %d\n",
  109. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high);
  110. u = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
  111. if (high)
  112. u |= 1 << GPIO_BIT(gpio);
  113. else
  114. u &= ~(1 << GPIO_BIT(gpio));
  115. writel(u, &bank->gpio_out[GPIO_PORT(gpio)]);
  116. }
  117. /*
  118. * Generic_GPIO primitives.
  119. */
  120. int gpio_request(unsigned gpio, const char *label)
  121. {
  122. if (gpio >= MAX_NUM_GPIOS)
  123. return -1;
  124. if (label != NULL) {
  125. strncpy(gpio_names[gpio].name, label, GPIO_NAME_SIZE);
  126. gpio_names[gpio].name[GPIO_NAME_SIZE - 1] = '\0';
  127. }
  128. /* Configure as a GPIO */
  129. set_config(gpio, 1);
  130. return 0;
  131. }
  132. int gpio_free(unsigned gpio)
  133. {
  134. if (gpio >= MAX_NUM_GPIOS)
  135. return -1;
  136. gpio_names[gpio].name[0] = '\0';
  137. /* Do not configure as input or change pin mux here */
  138. return 0;
  139. }
  140. /* read GPIO OUT value of pin 'gpio' */
  141. static int gpio_get_output_value(unsigned gpio)
  142. {
  143. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  144. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  145. int val;
  146. debug("gpio_get_output_value: pin = %d (port %d:bit %d)\n",
  147. gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
  148. val = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
  149. return (val >> GPIO_BIT(gpio)) & 1;
  150. }
  151. /* set GPIO pin 'gpio' as an input */
  152. int gpio_direction_input(unsigned gpio)
  153. {
  154. debug("gpio_direction_input: pin = %d (port %d:bit %d)\n",
  155. gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
  156. /* Configure GPIO direction as input. */
  157. set_direction(gpio, 0);
  158. return 0;
  159. }
  160. /* set GPIO pin 'gpio' as an output, with polarity 'value' */
  161. int gpio_direction_output(unsigned gpio, int value)
  162. {
  163. debug("gpio_direction_output: pin = %d (port %d:bit %d) = %s\n",
  164. gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio),
  165. value ? "HIGH" : "LOW");
  166. /* Configure GPIO output value. */
  167. set_level(gpio, value);
  168. /* Configure GPIO direction as output. */
  169. set_direction(gpio, 1);
  170. return 0;
  171. }
  172. /* read GPIO IN value of pin 'gpio' */
  173. int gpio_get_value(unsigned gpio)
  174. {
  175. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  176. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  177. int val;
  178. debug("gpio_get_value: pin = %d (port %d:bit %d)\n",
  179. gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
  180. val = readl(&bank->gpio_in[GPIO_PORT(gpio)]);
  181. return (val >> GPIO_BIT(gpio)) & 1;
  182. }
  183. /* write GPIO OUT value to pin 'gpio' */
  184. int gpio_set_value(unsigned gpio, int value)
  185. {
  186. debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
  187. gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value);
  188. /* Configure GPIO output value. */
  189. set_level(gpio, value);
  190. return 0;
  191. }
  192. /*
  193. * Display Tegra GPIO information
  194. */
  195. void gpio_info(void)
  196. {
  197. unsigned c;
  198. int type;
  199. for (c = 0; c < MAX_NUM_GPIOS; c++) {
  200. type = get_config(c); /* GPIO, not SFPIO */
  201. if (type) {
  202. printf("GPIO_%d:\t%s is an %s, ", c,
  203. get_name(c),
  204. get_direction(c) ? "OUTPUT" : "INPUT");
  205. if (get_direction(c))
  206. printf("value = %d", gpio_get_output_value(c));
  207. else
  208. printf("value = %d", gpio_get_value(c));
  209. printf("\n");
  210. } else
  211. continue;
  212. }
  213. }