tegra_gpio.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * NVIDIA Tegra20 GPIO handling.
  4. * (C) Copyright 2010-2012,2015
  5. * NVIDIA Corporation <www.nvidia.com>
  6. */
  7. /*
  8. * Based on (mostly copied from) kw_gpio.c based Linux 2.6 kernel driver.
  9. * Tom Warren (twarren@nvidia.com)
  10. */
  11. #include <common.h>
  12. #include <dm.h>
  13. #include <malloc.h>
  14. #include <errno.h>
  15. #include <fdtdec.h>
  16. #include <asm/io.h>
  17. #include <asm/bitops.h>
  18. #include <asm/arch/tegra.h>
  19. #include <asm/gpio.h>
  20. #include <dm/device-internal.h>
  21. #include <dt-bindings/gpio/gpio.h>
  22. static const int CONFIG_SFIO = 0;
  23. static const int CONFIG_GPIO = 1;
  24. static const int DIRECTION_INPUT = 0;
  25. static const int DIRECTION_OUTPUT = 1;
  26. struct tegra_gpio_platdata {
  27. struct gpio_ctlr_bank *bank;
  28. const char *port_name; /* Name of port, e.g. "B" */
  29. int base_gpio; /* Port number for this port (0, 1,.., n-1) */
  30. };
  31. /* Information about each port at run-time */
  32. struct tegra_port_info {
  33. struct gpio_ctlr_bank *bank;
  34. int base_gpio; /* Port number for this port (0, 1,.., n-1) */
  35. };
  36. /* Return config of pin 'gpio' as GPIO (1) or SFIO (0) */
  37. static int get_config(unsigned gpio)
  38. {
  39. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  40. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  41. u32 u;
  42. int type;
  43. u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
  44. type = (u >> GPIO_BIT(gpio)) & 1;
  45. debug("get_config: port = %d, bit = %d is %s\n",
  46. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
  47. return type ? CONFIG_GPIO : CONFIG_SFIO;
  48. }
  49. /* Config pin 'gpio' as GPIO or SFIO, based on 'type' */
  50. static void set_config(unsigned gpio, int type)
  51. {
  52. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  53. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  54. u32 u;
  55. debug("set_config: port = %d, bit = %d, %s\n",
  56. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), type ? "GPIO" : "SFPIO");
  57. u = readl(&bank->gpio_config[GPIO_PORT(gpio)]);
  58. if (type != CONFIG_SFIO)
  59. u |= 1 << GPIO_BIT(gpio);
  60. else
  61. u &= ~(1 << GPIO_BIT(gpio));
  62. writel(u, &bank->gpio_config[GPIO_PORT(gpio)]);
  63. }
  64. /* Return GPIO pin 'gpio' direction - 0 = input or 1 = output */
  65. static int get_direction(unsigned gpio)
  66. {
  67. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  68. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  69. u32 u;
  70. int dir;
  71. u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
  72. dir = (u >> GPIO_BIT(gpio)) & 1;
  73. debug("get_direction: port = %d, bit = %d, %s\n",
  74. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), dir ? "OUT" : "IN");
  75. return dir ? DIRECTION_OUTPUT : DIRECTION_INPUT;
  76. }
  77. /* Config GPIO pin 'gpio' as input or output (OE) as per 'output' */
  78. static void set_direction(unsigned gpio, int output)
  79. {
  80. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  81. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  82. u32 u;
  83. debug("set_direction: port = %d, bit = %d, %s\n",
  84. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), output ? "OUT" : "IN");
  85. u = readl(&bank->gpio_dir_out[GPIO_PORT(gpio)]);
  86. if (output != DIRECTION_INPUT)
  87. u |= 1 << GPIO_BIT(gpio);
  88. else
  89. u &= ~(1 << GPIO_BIT(gpio));
  90. writel(u, &bank->gpio_dir_out[GPIO_PORT(gpio)]);
  91. }
  92. /* set GPIO pin 'gpio' output bit as 0 or 1 as per 'high' */
  93. static void set_level(unsigned gpio, int high)
  94. {
  95. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  96. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  97. u32 u;
  98. debug("set_level: port = %d, bit %d == %d\n",
  99. GPIO_FULLPORT(gpio), GPIO_BIT(gpio), high);
  100. u = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
  101. if (high)
  102. u |= 1 << GPIO_BIT(gpio);
  103. else
  104. u &= ~(1 << GPIO_BIT(gpio));
  105. writel(u, &bank->gpio_out[GPIO_PORT(gpio)]);
  106. }
  107. /*
  108. * Generic_GPIO primitives.
  109. */
  110. /* set GPIO pin 'gpio' as an input */
  111. static int tegra_gpio_direction_input(struct udevice *dev, unsigned offset)
  112. {
  113. struct tegra_port_info *state = dev_get_priv(dev);
  114. /* Configure GPIO direction as input. */
  115. set_direction(state->base_gpio + offset, DIRECTION_INPUT);
  116. /* Enable the pin as a GPIO */
  117. set_config(state->base_gpio + offset, 1);
  118. return 0;
  119. }
  120. /* set GPIO pin 'gpio' as an output, with polarity 'value' */
  121. static int tegra_gpio_direction_output(struct udevice *dev, unsigned offset,
  122. int value)
  123. {
  124. struct tegra_port_info *state = dev_get_priv(dev);
  125. int gpio = state->base_gpio + offset;
  126. /* Configure GPIO output value. */
  127. set_level(gpio, value);
  128. /* Configure GPIO direction as output. */
  129. set_direction(gpio, DIRECTION_OUTPUT);
  130. /* Enable the pin as a GPIO */
  131. set_config(state->base_gpio + offset, 1);
  132. return 0;
  133. }
  134. /* read GPIO IN value of pin 'gpio' */
  135. static int tegra_gpio_get_value(struct udevice *dev, unsigned offset)
  136. {
  137. struct tegra_port_info *state = dev_get_priv(dev);
  138. int gpio = state->base_gpio + offset;
  139. int val;
  140. debug("%s: pin = %d (port %d:bit %d)\n", __func__,
  141. gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
  142. if (get_direction(gpio) == DIRECTION_INPUT)
  143. val = readl(&state->bank->gpio_in[GPIO_PORT(gpio)]);
  144. else
  145. val = readl(&state->bank->gpio_out[GPIO_PORT(gpio)]);
  146. return (val >> GPIO_BIT(gpio)) & 1;
  147. }
  148. /* write GPIO OUT value to pin 'gpio' */
  149. static int tegra_gpio_set_value(struct udevice *dev, unsigned offset, int value)
  150. {
  151. struct tegra_port_info *state = dev_get_priv(dev);
  152. int gpio = state->base_gpio + offset;
  153. debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
  154. gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value);
  155. /* Configure GPIO output value. */
  156. set_level(gpio, value);
  157. return 0;
  158. }
  159. void gpio_config_table(const struct tegra_gpio_config *config, int len)
  160. {
  161. int i;
  162. for (i = 0; i < len; i++) {
  163. switch (config[i].init) {
  164. case TEGRA_GPIO_INIT_IN:
  165. set_direction(config[i].gpio, DIRECTION_INPUT);
  166. break;
  167. case TEGRA_GPIO_INIT_OUT0:
  168. set_level(config[i].gpio, 0);
  169. set_direction(config[i].gpio, DIRECTION_OUTPUT);
  170. break;
  171. case TEGRA_GPIO_INIT_OUT1:
  172. set_level(config[i].gpio, 1);
  173. set_direction(config[i].gpio, DIRECTION_OUTPUT);
  174. break;
  175. }
  176. set_config(config[i].gpio, CONFIG_GPIO);
  177. }
  178. }
  179. static int tegra_gpio_get_function(struct udevice *dev, unsigned offset)
  180. {
  181. struct tegra_port_info *state = dev_get_priv(dev);
  182. int gpio = state->base_gpio + offset;
  183. if (!get_config(gpio))
  184. return GPIOF_FUNC;
  185. else if (get_direction(gpio))
  186. return GPIOF_OUTPUT;
  187. else
  188. return GPIOF_INPUT;
  189. }
  190. static int tegra_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
  191. struct ofnode_phandle_args *args)
  192. {
  193. int gpio, port, ret;
  194. gpio = args->args[0];
  195. port = gpio / TEGRA_GPIOS_PER_PORT;
  196. ret = device_get_child(dev, port, &desc->dev);
  197. if (ret)
  198. return ret;
  199. desc->offset = gpio % TEGRA_GPIOS_PER_PORT;
  200. desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
  201. return 0;
  202. }
  203. static const struct dm_gpio_ops gpio_tegra_ops = {
  204. .direction_input = tegra_gpio_direction_input,
  205. .direction_output = tegra_gpio_direction_output,
  206. .get_value = tegra_gpio_get_value,
  207. .set_value = tegra_gpio_set_value,
  208. .get_function = tegra_gpio_get_function,
  209. .xlate = tegra_gpio_xlate,
  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_get_uclass_priv(dev);
  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. /* If this is a child device, there is nothing to do here */
  263. if (plat)
  264. return 0;
  265. /* TODO(sjg@chromium.org): Remove once SPL supports device tree */
  266. #ifdef CONFIG_SPL_BUILD
  267. ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  268. bank_count = TEGRA_GPIO_BANKS;
  269. #else
  270. {
  271. int len;
  272. /*
  273. * This driver does not make use of interrupts, other than to figure
  274. * out the number of GPIO banks
  275. */
  276. len = dev_read_size(parent, "interrupts");
  277. if (len < 0)
  278. return len;
  279. bank_count = len / 3 / sizeof(u32);
  280. ctlr = (struct gpio_ctlr *)dev_read_addr(parent);
  281. if ((ulong)ctlr == FDT_ADDR_T_NONE)
  282. return -EINVAL;
  283. }
  284. #endif
  285. for (bank = 0; bank < bank_count; bank++) {
  286. int port;
  287. for (port = 0; port < TEGRA_PORTS_PER_BANK; port++) {
  288. struct tegra_gpio_platdata *plat;
  289. struct udevice *dev;
  290. int base_port;
  291. plat = calloc(1, sizeof(*plat));
  292. if (!plat)
  293. return -ENOMEM;
  294. plat->bank = &ctlr->gpio_bank[bank];
  295. base_port = bank * TEGRA_PORTS_PER_BANK + port;
  296. plat->base_gpio = TEGRA_GPIOS_PER_PORT * base_port;
  297. plat->port_name = gpio_port_name(base_port);
  298. ret = device_bind(parent, parent->driver,
  299. plat->port_name, plat, -1, &dev);
  300. if (ret)
  301. return ret;
  302. dev_set_of_offset(dev, dev_of_offset(parent));
  303. }
  304. }
  305. return 0;
  306. }
  307. U_BOOT_DRIVER(gpio_tegra) = {
  308. .name = "gpio_tegra",
  309. .id = UCLASS_GPIO,
  310. .of_match = tegra_gpio_ids,
  311. .bind = gpio_tegra_bind,
  312. .probe = gpio_tegra_probe,
  313. .priv_auto_alloc_size = sizeof(struct tegra_port_info),
  314. .ops = &gpio_tegra_ops,
  315. };