tegra_gpio.c 6.2 KB

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