tegra_gpio.c 9.1 KB

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