tegra_gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. char label[TEGRA_GPIOS_PER_PORT][GPIO_NAME_SIZE];
  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. static int check_reserved(struct udevice *dev, unsigned offset,
  112. const char *func)
  113. {
  114. struct tegra_port_info *state = dev_get_priv(dev);
  115. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  116. if (!*state->label[offset]) {
  117. printf("tegra_gpio: %s: error: gpio %s%d not reserved\n",
  118. func, uc_priv->bank_name, offset);
  119. return -EBUSY;
  120. }
  121. return 0;
  122. }
  123. /* set GPIO pin 'gpio' as an output, with polarity 'value' */
  124. int tegra_spl_gpio_direction_output(int gpio, int value)
  125. {
  126. /* Configure as a GPIO */
  127. set_config(gpio, 1);
  128. /* Configure GPIO output value. */
  129. set_level(gpio, value);
  130. /* Configure GPIO direction as output. */
  131. set_direction(gpio, 1);
  132. return 0;
  133. }
  134. /*
  135. * Generic_GPIO primitives.
  136. */
  137. static int tegra_gpio_request(struct udevice *dev, unsigned offset,
  138. const char *label)
  139. {
  140. struct tegra_port_info *state = dev_get_priv(dev);
  141. if (!label)
  142. return -EINVAL;
  143. if (*state->label[offset])
  144. return -EBUSY;
  145. strncpy(state->label[offset], label, GPIO_NAME_SIZE);
  146. state->label[offset][GPIO_NAME_SIZE - 1] = '\0';
  147. /* Configure as a GPIO */
  148. set_config(state->base_gpio + offset, 1);
  149. return 0;
  150. }
  151. static int tegra_gpio_free(struct udevice *dev, unsigned offset)
  152. {
  153. struct tegra_port_info *state = dev_get_priv(dev);
  154. int ret;
  155. ret = check_reserved(dev, offset, __func__);
  156. if (ret)
  157. return ret;
  158. state->label[offset][0] = '\0';
  159. return 0;
  160. }
  161. /* read GPIO OUT value of pin 'gpio' */
  162. static int tegra_gpio_get_output_value(unsigned gpio)
  163. {
  164. struct gpio_ctlr *ctlr = (struct gpio_ctlr *)NV_PA_GPIO_BASE;
  165. struct gpio_ctlr_bank *bank = &ctlr->gpio_bank[GPIO_BANK(gpio)];
  166. int val;
  167. debug("gpio_get_output_value: pin = %d (port %d:bit %d)\n",
  168. gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
  169. val = readl(&bank->gpio_out[GPIO_PORT(gpio)]);
  170. return (val >> GPIO_BIT(gpio)) & 1;
  171. }
  172. /* set GPIO pin 'gpio' as an input */
  173. static int tegra_gpio_direction_input(struct udevice *dev, unsigned offset)
  174. {
  175. struct tegra_port_info *state = dev_get_priv(dev);
  176. int ret;
  177. ret = check_reserved(dev, offset, __func__);
  178. if (ret)
  179. return ret;
  180. /* Configure GPIO direction as input. */
  181. set_direction(state->base_gpio + offset, 0);
  182. return 0;
  183. }
  184. /* set GPIO pin 'gpio' as an output, with polarity 'value' */
  185. static int tegra_gpio_direction_output(struct udevice *dev, unsigned offset,
  186. int value)
  187. {
  188. struct tegra_port_info *state = dev_get_priv(dev);
  189. int gpio = state->base_gpio + offset;
  190. int ret;
  191. ret = check_reserved(dev, offset, __func__);
  192. if (ret)
  193. return ret;
  194. /* Configure GPIO output value. */
  195. set_level(gpio, value);
  196. /* Configure GPIO direction as output. */
  197. set_direction(gpio, 1);
  198. return 0;
  199. }
  200. /* read GPIO IN value of pin 'gpio' */
  201. static int tegra_gpio_get_value(struct udevice *dev, unsigned offset)
  202. {
  203. struct tegra_port_info *state = dev_get_priv(dev);
  204. int gpio = state->base_gpio + offset;
  205. int ret;
  206. int val;
  207. ret = check_reserved(dev, offset, __func__);
  208. if (ret)
  209. return ret;
  210. debug("%s: pin = %d (port %d:bit %d)\n", __func__,
  211. gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio));
  212. val = readl(&state->bank->gpio_in[GPIO_PORT(gpio)]);
  213. return (val >> GPIO_BIT(gpio)) & 1;
  214. }
  215. /* write GPIO OUT value to pin 'gpio' */
  216. static int tegra_gpio_set_value(struct udevice *dev, unsigned offset, int value)
  217. {
  218. struct tegra_port_info *state = dev_get_priv(dev);
  219. int gpio = state->base_gpio + offset;
  220. int ret;
  221. ret = check_reserved(dev, offset, __func__);
  222. if (ret)
  223. return ret;
  224. debug("gpio_set_value: pin = %d (port %d:bit %d), value = %d\n",
  225. gpio, GPIO_FULLPORT(gpio), GPIO_BIT(gpio), value);
  226. /* Configure GPIO output value. */
  227. set_level(gpio, value);
  228. return 0;
  229. }
  230. void gpio_config_table(const struct tegra_gpio_config *config, int len)
  231. {
  232. int i;
  233. for (i = 0; i < len; i++) {
  234. switch (config[i].init) {
  235. case TEGRA_GPIO_INIT_IN:
  236. gpio_direction_input(config[i].gpio);
  237. break;
  238. case TEGRA_GPIO_INIT_OUT0:
  239. gpio_direction_output(config[i].gpio, 0);
  240. break;
  241. case TEGRA_GPIO_INIT_OUT1:
  242. gpio_direction_output(config[i].gpio, 1);
  243. break;
  244. }
  245. set_config(config[i].gpio, 1);
  246. }
  247. }
  248. static int tegra_gpio_get_function(struct udevice *dev, unsigned offset)
  249. {
  250. struct tegra_port_info *state = dev_get_priv(dev);
  251. int gpio = state->base_gpio + offset;
  252. if (!*state->label[offset])
  253. return GPIOF_UNUSED;
  254. if (!get_config(gpio))
  255. return GPIOF_FUNC;
  256. else if (get_direction(gpio))
  257. return GPIOF_OUTPUT;
  258. else
  259. return GPIOF_INPUT;
  260. }
  261. static int tegra_gpio_get_state(struct udevice *dev, unsigned int offset,
  262. char *buf, int bufsize)
  263. {
  264. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  265. struct tegra_port_info *state = dev_get_priv(dev);
  266. int gpio = state->base_gpio + offset;
  267. const char *label;
  268. int is_output;
  269. int is_gpio;
  270. int size;
  271. label = state->label[offset];
  272. is_gpio = get_config(gpio); /* GPIO, not SFPIO */
  273. size = snprintf(buf, bufsize, "%s%d: ",
  274. uc_priv->bank_name ? uc_priv->bank_name : "", offset);
  275. buf += size;
  276. bufsize -= size;
  277. if (is_gpio) {
  278. is_output = get_direction(gpio);
  279. snprintf(buf, bufsize, "%s: %d [%c]%s%s",
  280. is_output ? "out" : " in",
  281. is_output ?
  282. tegra_gpio_get_output_value(gpio) :
  283. tegra_gpio_get_value(dev, offset),
  284. *label ? 'x' : ' ',
  285. *label ? " " : "",
  286. label);
  287. } else {
  288. snprintf(buf, bufsize, "sfpio");
  289. }
  290. return 0;
  291. }
  292. static const struct dm_gpio_ops gpio_tegra_ops = {
  293. .request = tegra_gpio_request,
  294. .free = tegra_gpio_free,
  295. .direction_input = tegra_gpio_direction_input,
  296. .direction_output = tegra_gpio_direction_output,
  297. .get_value = tegra_gpio_get_value,
  298. .set_value = tegra_gpio_set_value,
  299. .get_function = tegra_gpio_get_function,
  300. .get_state = tegra_gpio_get_state,
  301. };
  302. /**
  303. * Returns the name of a GPIO port
  304. *
  305. * GPIOs are named A, B, C, ..., Z, AA, BB, CC, ...
  306. *
  307. * @base_port: Base port number (0, 1..n-1)
  308. * @return allocated string containing the name
  309. */
  310. static char *gpio_port_name(int base_port)
  311. {
  312. char *name, *s;
  313. name = malloc(3);
  314. if (name) {
  315. s = name;
  316. *s++ = 'A' + (base_port % 26);
  317. if (base_port >= 26)
  318. *s++ = *name;
  319. *s = '\0';
  320. }
  321. return name;
  322. }
  323. static const struct udevice_id tegra_gpio_ids[] = {
  324. { .compatible = "nvidia,tegra30-gpio" },
  325. { .compatible = "nvidia,tegra20-gpio" },
  326. { }
  327. };
  328. static int gpio_tegra_probe(struct udevice *dev)
  329. {
  330. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  331. struct tegra_port_info *priv = dev->priv;
  332. struct tegra_gpio_platdata *plat = dev->platdata;
  333. /* Only child devices have ports */
  334. if (!plat)
  335. return 0;
  336. priv->bank = plat->bank;
  337. priv->base_gpio = plat->base_gpio;
  338. uc_priv->gpio_count = TEGRA_GPIOS_PER_PORT;
  339. uc_priv->bank_name = plat->port_name;
  340. return 0;
  341. }
  342. /**
  343. * We have a top-level GPIO device with no actual GPIOs. It has a child
  344. * device for each Tegra port.
  345. */
  346. static int gpio_tegra_bind(struct udevice *parent)
  347. {
  348. struct tegra_gpio_platdata *plat = parent->platdata;
  349. struct gpio_ctlr *ctlr;
  350. int bank_count;
  351. int bank;
  352. int ret;
  353. int len;
  354. /* If this is a child device, there is nothing to do here */
  355. if (plat)
  356. return 0;
  357. /*
  358. * This driver does not make use of interrupts, other than to figure
  359. * out the number of GPIO banks
  360. */
  361. if (!fdt_getprop(gd->fdt_blob, parent->of_offset, "interrupts", &len))
  362. return -EINVAL;
  363. bank_count = len / 3 / sizeof(u32);
  364. ctlr = (struct gpio_ctlr *)fdtdec_get_addr(gd->fdt_blob,
  365. parent->of_offset, "reg");
  366. for (bank = 0; bank < bank_count; bank++) {
  367. int port;
  368. for (port = 0; port < TEGRA_PORTS_PER_BANK; port++) {
  369. struct tegra_gpio_platdata *plat;
  370. struct udevice *dev;
  371. int base_port;
  372. plat = calloc(1, sizeof(*plat));
  373. if (!plat)
  374. return -ENOMEM;
  375. plat->bank = &ctlr->gpio_bank[bank];
  376. base_port = bank * TEGRA_PORTS_PER_BANK + port;
  377. plat->base_gpio = TEGRA_GPIOS_PER_PORT * base_port;
  378. plat->port_name = gpio_port_name(base_port);
  379. ret = device_bind(parent, parent->driver,
  380. plat->port_name, plat, -1, &dev);
  381. if (ret)
  382. return ret;
  383. dev->of_offset = parent->of_offset;
  384. }
  385. }
  386. return 0;
  387. }
  388. U_BOOT_DRIVER(gpio_tegra) = {
  389. .name = "gpio_tegra",
  390. .id = UCLASS_GPIO,
  391. .of_match = tegra_gpio_ids,
  392. .bind = gpio_tegra_bind,
  393. .probe = gpio_tegra_probe,
  394. .priv_auto_alloc_size = sizeof(struct tegra_port_info),
  395. .ops = &gpio_tegra_ops,
  396. };