tegra_gpio.c 9.6 KB

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