tegra_gpio.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 <dm.h>
  14. #include <malloc.h>
  15. #include <errno.h>
  16. #include <fdtdec.h>
  17. #include <asm/io.h>
  18. #include <asm/bitops.h>
  19. #include <asm/arch/tegra.h>
  20. #include <asm/gpio.h>
  21. #include <dm/device-internal.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. enum {
  24. TEGRA_CMD_INFO,
  25. TEGRA_CMD_PORT,
  26. TEGRA_CMD_OUTPUT,
  27. TEGRA_CMD_INPUT,
  28. };
  29. struct tegra_gpio_platdata {
  30. struct gpio_ctlr_bank *bank;
  31. const char *port_name; /* Name of port, e.g. "B" */
  32. int base_gpio; /* Port number for this port (0, 1,.., n-1) */
  33. };
  34. /* Information about each port at run-time */
  35. struct tegra_port_info {
  36. struct gpio_ctlr_bank *bank;
  37. int base_gpio; /* Port number for this port (0, 1,.., n-1) */
  38. };
  39. /* Return config of pin 'gpio' as GPIO (1) or SFPIO (0) */
  40. static int get_config(unsigned gpio)
  41. {
  42. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  43. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  44. u32 u;
  45. int type;
  46. u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
  47. type = (u >> GPIO_BIT(gpio)) & 1;
  48. debug("get_config: port = %d, bit = %d is %s\n",
  49. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
  50. return type;
  51. }
  52. /* Config pin 'gpio' as GPIO or SFPIO, based on 'type' */
  53. static void set_config(unsigned gpio, int type)
  54. {
  55. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  56. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  57. u32 u;
  58. debug("set_config: port = %d, bit = %d, %s\n",
  59. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
  60. u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
  61. if (type) /* GPIO */
  62. u |= 1 << GPIO_BIT(gpio);
  63. else
  64. u &= ~(1 << GPIO_BIT(gpio));
  65. writel(u, &bank->gpio_config[GPIO_PORT(gpio)]);
  66. }
  67. /* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */
  68. static int get_direction(unsigned gpio)
  69. {
  70. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  71. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  72. u32 u;
  73. int dir;
  74. u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
  75. dir = (u >> GPIO_BIT(gpio)) & 1;
  76. debug("get_direction: port = %d, bit = %d, %s\n",
  77. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN");
  78. return dir;
  79. }
  80. /* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */
  81. static void set_direction(unsigned gpio, int output)
  82. {
  83. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  84. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  85. u32 u;
  86. debug("set_direction: port = %d, bit = %d, %s\n",
  87. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN");
  88. u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
  89. if (output)
  90. u |= 1 << GPIO_BIT(gpio);
  91. else
  92. u &= ~(1 << GPIO_BIT(gpio));
  93. writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]);
  94. }
  95. /* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */
  96. static void set_level(unsigned gpio, int high)
  97. {
  98. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  99. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  100. u32 u;
  101. debug("set_level: port = %d, bit %d == %d\n",
  102. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high);
  103. u = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
  104. if (high)
  105. u |= 1 << GPIO_BIT(gpio);
  106. else
  107. u &= ~(1 << GPIO_BIT(gpio));
  108. writel(u, &bank->gpio_out[GPIO_PORT(gpio)]);
  109. }
  110. /* set GPIO pin 'gpio' as an output, with polarity 'value' */
  111. int tegra_spl_gpio_direction_output(int gpio, int value)
  112. {
  113. /* Configure as a GPIO */
  114. set_config(gpio, 1);
  115. /* Configure GPIO output value. */
  116. set_level(gpio, value);
  117. /* Configure GPIO direction as output. */
  118. set_direction(gpio, 1);
  119. return 0;
  120. }
  121. /*
  122. * Generic_GPIO primitives.
  123. */
  124. static int tegra_gpio_request(struct udevice *dev, unsigned offset,
  125. const char *label)
  126. {
  127. struct tegra_port_info *state = dev_get_priv(dev);
  128. /* Configure as a GPIO */
  129. set_config(state->base_gpio + offset, 1);
  130. return 0;
  131. }
  132. /* set GPIO pin 'gpio' as an input */
  133. static int tegra_gpio_direction_input(struct udevice *dev, unsigned offset)
  134. {
  135. struct tegra_port_info *state = dev_get_priv(dev);
  136. /* Configure GPIO direction as input. */
  137. set_direction(state->base_gpio + offset, 0);
  138. return 0;
  139. }
  140. /* set GPIO pin 'gpio' as an output, with polarity 'value' */
  141. static int tegra_gpio_direction_output(struct udevice *dev, unsigned offset,
  142. int value)
  143. {
  144. struct tegra_port_info *state = dev_get_priv(dev);
  145. int gpio = state->base_gpio + offset;
  146. /* Configure GPIO output value. */
  147. set_level(gpio, value);
  148. /* Configure GPIO direction as output. */
  149. set_direction(gpio, 1);
  150. return 0;
  151. }
  152. /* read GPIO IN value of pin 'gpio' */
  153. static int tegra_gpio_get_value(struct udevice *dev, unsigned offset)
  154. {
  155. struct tegra_port_info *state = dev_get_priv(dev);
  156. int gpio = state->base_gpio + offset;
  157. int val;
  158. debug("%s: pin = %d (port %d:bit %d)\n", __func__,
  159. gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
  160. val = readl(&state->bank->gpio_in[GPIO_PORT(gpio)]);
  161. return (val >> GPIO_BIT(gpio)) & 1;
  162. }
  163. /* write GPIO OUT value to pin 'gpio' */
  164. static int tegra_gpio_set_value(struct udevice *dev, unsigned offset, int value)
  165. {
  166. struct tegra_port_info *state = dev_get_priv(dev);
  167. int gpio = state->base_gpio + offset;
  168. debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
  169. gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value);
  170. /* Configure GPIO output value. */
  171. set_level(gpio, value);
  172. return 0;
  173. }
  174. void gpio_config_table(const struct tegra_gpio_config *config, int len)
  175. {
  176. int i;
  177. for (i = 0; i < len; i++) {
  178. switch (config[i].init) {
  179. case TEGRA_GPIO_INIT_IN:
  180. gpio_direction_input(config[i].gpio);
  181. break;
  182. case TEGRA_GPIO_INIT_OUT0:
  183. gpio_direction_output(config[i].gpio, 0);
  184. break;
  185. case TEGRA_GPIO_INIT_OUT1:
  186. gpio_direction_output(config[i].gpio, 1);
  187. break;
  188. }
  189. set_config(config[i].gpio, 1);
  190. }
  191. }
  192. static int tegra_gpio_get_function(struct udevice *dev, unsigned offset)
  193. {
  194. struct tegra_port_info *state = dev_get_priv(dev);
  195. int gpio = state->base_gpio + offset;
  196. if (!get_config(gpio))
  197. return GPIOF_FUNC;
  198. else if (get_direction(gpio))
  199. return GPIOF_OUTPUT;
  200. else
  201. return GPIOF_INPUT;
  202. }
  203. static const struct dm_gpio_ops gpio_tegra_ops = {
  204. .request = tegra_gpio_request,
  205. .direction_input = tegra_gpio_direction_input,
  206. .direction_output = tegra_gpio_direction_output,
  207. .get_value = tegra_gpio_get_value,
  208. .set_value = tegra_gpio_set_value,
  209. .get_function = tegra_gpio_get_function,
  210. };
  211. /**
  212. * Returns the name of a GPIO port
  213. *
  214. * GPIOs are named A, B, C, ..., Z, AA, BB, CC, ...
  215. *
  216. * @base_port: Base port number (0, 1..n-1)
  217. * @return allocated string containing the name
  218. */
  219. static char *gpio_port_name(int base_port)
  220. {
  221. char *name, *s;
  222. name = malloc(3);
  223. if (name) {
  224. s = name;
  225. *s++ = 'A' + (base_port % 26);
  226. if (base_port >= 26)
  227. *s++ = *name;
  228. *s = '\0';
  229. }
  230. return name;
  231. }
  232. static const struct udevice_id tegra_gpio_ids[] = {
  233. { .compatible = "nvidia,tegra30-gpio" },
  234. { .compatible = "nvidia,tegra20-gpio" },
  235. { }
  236. };
  237. static int gpio_tegra_probe(struct udevice *dev)
  238. {
  239. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  240. struct tegra_port_info *priv = dev->priv;
  241. struct tegra_gpio_platdata *plat = dev->platdata;
  242. /* Only child devices have ports */
  243. if (!plat)
  244. return 0;
  245. priv->bank = plat->bank;
  246. priv->base_gpio = plat->base_gpio;
  247. uc_priv->gpio_count = TEGRA_GPIOS_PER_PORT;
  248. uc_priv->bank_name = plat->port_name;
  249. return 0;
  250. }
  251. /**
  252. * We have a top-level GPIO device with no actual GPIOs. It has a child
  253. * device for each Tegra port.
  254. */
  255. static int gpio_tegra_bind(struct udevice *parent)
  256. {
  257. struct tegra_gpio_platdata *plat = parent->platdata;
  258. struct gpio_ctlr *ctlr;
  259. int bank_count;
  260. int bank;
  261. int ret;
  262. int len;
  263. /* If this is a child device, there is nothing to do here */
  264. if (plat)
  265. return 0;
  266. /*
  267. * This driver does not make use of interrupts, other than to figure
  268. * out the number of GPIO banks
  269. */
  270. if (!fdt_getprop(gd->fdt_blob, parent->of_offset, "interrupts", &len))
  271. return -EINVAL;
  272. bank_count = len / 3 / sizeof(u32);
  273. ctlr = (struct gpio_ctlr *)fdtdec_get_addr(gd->fdt_blob,
  274. parent->of_offset, "reg");
  275. for (bank = 0; bank < bank_count; bank++) {
  276. int port;
  277. for (port = 0; port < TEGRA_PORTS_PER_BANK; port++) {
  278. struct tegra_gpio_platdata *plat;
  279. struct udevice *dev;
  280. int base_port;
  281. plat = calloc(1, sizeof(*plat));
  282. if (!plat)
  283. return -ENOMEM;
  284. plat->bank = &ctlr->gpio_bank[bank];
  285. base_port = bank * TEGRA_PORTS_PER_BANK + port;
  286. plat->base_gpio = TEGRA_GPIOS_PER_PORT * base_port;
  287. plat->port_name = gpio_port_name(base_port);
  288. ret = device_bind(parent, parent->driver,
  289. plat->port_name, plat, -1, &dev);
  290. if (ret)
  291. return ret;
  292. dev->of_offset = parent->of_offset;
  293. }
  294. }
  295. return 0;
  296. }
  297. U_BOOT_DRIVER(gpio_tegra) = {
  298. .name = "gpio_tegra",
  299. .id = UCLASS_GPIO,
  300. .of_match = tegra_gpio_ids,
  301. .bind = gpio_tegra_bind,
  302. .probe = gpio_tegra_probe,
  303. .priv_auto_alloc_size = sizeof(struct tegra_port_info),
  304. .ops = &gpio_tegra_ops,
  305. };