tegra_gpio.c 9.3 KB

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