omap_gpio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright (c) 2009 Wind River Systems, Inc.
  3. * Tom Rix <Tom.Rix@windriver.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This work is derived from the linux 2.6.27 kernel source
  8. * To fetch, use the kernel repository
  9. * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
  10. * Use the v2.6.27 tag.
  11. *
  12. * Below is the original's header including its copyright
  13. *
  14. * linux/arch/arm/plat-omap/gpio.c
  15. *
  16. * Support functions for OMAP GPIO
  17. *
  18. * Copyright (C) 2003-2005 Nokia Corporation
  19. * Written by Juha Yrjölä <juha.yrjola@nokia.com>
  20. */
  21. #include <common.h>
  22. #include <dm.h>
  23. #include <fdtdec.h>
  24. #include <asm/gpio.h>
  25. #include <asm/io.h>
  26. #include <linux/errno.h>
  27. #include <malloc.h>
  28. DECLARE_GLOBAL_DATA_PTR;
  29. #define OMAP_GPIO_DIR_OUT 0
  30. #define OMAP_GPIO_DIR_IN 1
  31. #ifdef CONFIG_DM_GPIO
  32. #define GPIO_PER_BANK 32
  33. struct gpio_bank {
  34. /* TODO(sjg@chromium.org): Can we use a struct here? */
  35. void *base; /* address of registers in physical memory */
  36. };
  37. #endif
  38. static inline int get_gpio_index(int gpio)
  39. {
  40. return gpio & 0x1f;
  41. }
  42. int gpio_is_valid(int gpio)
  43. {
  44. return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
  45. }
  46. static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
  47. int is_input)
  48. {
  49. void *reg = bank->base;
  50. u32 l;
  51. reg += OMAP_GPIO_OE;
  52. l = __raw_readl(reg);
  53. if (is_input)
  54. l |= 1 << gpio;
  55. else
  56. l &= ~(1 << gpio);
  57. __raw_writel(l, reg);
  58. }
  59. /**
  60. * Get the direction of the GPIO by reading the GPIO_OE register
  61. * corresponding to the specified bank.
  62. */
  63. static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
  64. {
  65. void *reg = bank->base;
  66. u32 v;
  67. reg += OMAP_GPIO_OE;
  68. v = __raw_readl(reg);
  69. if (v & (1 << gpio))
  70. return OMAP_GPIO_DIR_IN;
  71. else
  72. return OMAP_GPIO_DIR_OUT;
  73. }
  74. static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
  75. int enable)
  76. {
  77. void *reg = bank->base;
  78. u32 l = 0;
  79. if (enable)
  80. reg += OMAP_GPIO_SETDATAOUT;
  81. else
  82. reg += OMAP_GPIO_CLEARDATAOUT;
  83. l = 1 << gpio;
  84. __raw_writel(l, reg);
  85. }
  86. static int _get_gpio_value(const struct gpio_bank *bank, int gpio)
  87. {
  88. void *reg = bank->base;
  89. int input;
  90. input = _get_gpio_direction(bank, gpio);
  91. switch (input) {
  92. case OMAP_GPIO_DIR_IN:
  93. reg += OMAP_GPIO_DATAIN;
  94. break;
  95. case OMAP_GPIO_DIR_OUT:
  96. reg += OMAP_GPIO_DATAOUT;
  97. break;
  98. default:
  99. return -1;
  100. }
  101. return (__raw_readl(reg) & (1 << gpio)) != 0;
  102. }
  103. #ifndef CONFIG_DM_GPIO
  104. static inline const struct gpio_bank *get_gpio_bank(int gpio)
  105. {
  106. return &omap_gpio_bank[gpio >> 5];
  107. }
  108. static int check_gpio(int gpio)
  109. {
  110. if (!gpio_is_valid(gpio)) {
  111. printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. /**
  117. * Set value of the specified gpio
  118. */
  119. int gpio_set_value(unsigned gpio, int value)
  120. {
  121. const struct gpio_bank *bank;
  122. if (check_gpio(gpio) < 0)
  123. return -1;
  124. bank = get_gpio_bank(gpio);
  125. _set_gpio_dataout(bank, get_gpio_index(gpio), value);
  126. return 0;
  127. }
  128. /**
  129. * Get value of the specified gpio
  130. */
  131. int gpio_get_value(unsigned gpio)
  132. {
  133. const struct gpio_bank *bank;
  134. if (check_gpio(gpio) < 0)
  135. return -1;
  136. bank = get_gpio_bank(gpio);
  137. return _get_gpio_value(bank, get_gpio_index(gpio));
  138. }
  139. /**
  140. * Set gpio direction as input
  141. */
  142. int gpio_direction_input(unsigned gpio)
  143. {
  144. const struct gpio_bank *bank;
  145. if (check_gpio(gpio) < 0)
  146. return -1;
  147. bank = get_gpio_bank(gpio);
  148. _set_gpio_direction(bank, get_gpio_index(gpio), 1);
  149. return 0;
  150. }
  151. /**
  152. * Set gpio direction as output
  153. */
  154. int gpio_direction_output(unsigned gpio, int value)
  155. {
  156. const struct gpio_bank *bank;
  157. if (check_gpio(gpio) < 0)
  158. return -1;
  159. bank = get_gpio_bank(gpio);
  160. _set_gpio_dataout(bank, get_gpio_index(gpio), value);
  161. _set_gpio_direction(bank, get_gpio_index(gpio), 0);
  162. return 0;
  163. }
  164. /**
  165. * Request a gpio before using it.
  166. *
  167. * NOTE: Argument 'label' is unused.
  168. */
  169. int gpio_request(unsigned gpio, const char *label)
  170. {
  171. if (check_gpio(gpio) < 0)
  172. return -1;
  173. return 0;
  174. }
  175. /**
  176. * Reset and free the gpio after using it.
  177. */
  178. int gpio_free(unsigned gpio)
  179. {
  180. return 0;
  181. }
  182. #else /* new driver model interface CONFIG_DM_GPIO */
  183. /* set GPIO pin 'gpio' as an input */
  184. static int omap_gpio_direction_input(struct udevice *dev, unsigned offset)
  185. {
  186. struct gpio_bank *bank = dev_get_priv(dev);
  187. /* Configure GPIO direction as input. */
  188. _set_gpio_direction(bank, offset, 1);
  189. return 0;
  190. }
  191. /* set GPIO pin 'gpio' as an output, with polarity 'value' */
  192. static int omap_gpio_direction_output(struct udevice *dev, unsigned offset,
  193. int value)
  194. {
  195. struct gpio_bank *bank = dev_get_priv(dev);
  196. _set_gpio_dataout(bank, offset, value);
  197. _set_gpio_direction(bank, offset, 0);
  198. return 0;
  199. }
  200. /* read GPIO IN value of pin 'gpio' */
  201. static int omap_gpio_get_value(struct udevice *dev, unsigned offset)
  202. {
  203. struct gpio_bank *bank = dev_get_priv(dev);
  204. return _get_gpio_value(bank, offset);
  205. }
  206. /* write GPIO OUT value to pin 'gpio' */
  207. static int omap_gpio_set_value(struct udevice *dev, unsigned offset,
  208. int value)
  209. {
  210. struct gpio_bank *bank = dev_get_priv(dev);
  211. _set_gpio_dataout(bank, offset, value);
  212. return 0;
  213. }
  214. static int omap_gpio_get_function(struct udevice *dev, unsigned offset)
  215. {
  216. struct gpio_bank *bank = dev_get_priv(dev);
  217. /* GPIOF_FUNC is not implemented yet */
  218. if (_get_gpio_direction(bank, offset) == OMAP_GPIO_DIR_OUT)
  219. return GPIOF_OUTPUT;
  220. else
  221. return GPIOF_INPUT;
  222. }
  223. static const struct dm_gpio_ops gpio_omap_ops = {
  224. .direction_input = omap_gpio_direction_input,
  225. .direction_output = omap_gpio_direction_output,
  226. .get_value = omap_gpio_get_value,
  227. .set_value = omap_gpio_set_value,
  228. .get_function = omap_gpio_get_function,
  229. };
  230. static int omap_gpio_probe(struct udevice *dev)
  231. {
  232. struct gpio_bank *bank = dev_get_priv(dev);
  233. struct omap_gpio_platdata *plat = dev_get_platdata(dev);
  234. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  235. uc_priv->bank_name = plat->port_name;
  236. uc_priv->gpio_count = GPIO_PER_BANK;
  237. bank->base = (void *)plat->base;
  238. return 0;
  239. }
  240. static int omap_gpio_bind(struct udevice *dev)
  241. {
  242. struct omap_gpio_platdata *plat = dev->platdata;
  243. fdt_addr_t base_addr;
  244. if (plat)
  245. return 0;
  246. base_addr = dev_get_addr(dev);
  247. if (base_addr == FDT_ADDR_T_NONE)
  248. return -ENODEV;
  249. /*
  250. * TODO:
  251. * When every board is converted to driver model and DT is
  252. * supported, this can be done by auto-alloc feature, but
  253. * not using calloc to alloc memory for platdata.
  254. */
  255. plat = calloc(1, sizeof(*plat));
  256. if (!plat)
  257. return -ENOMEM;
  258. plat->base = base_addr;
  259. plat->port_name = fdt_get_name(gd->fdt_blob, dev->of_offset, NULL);
  260. dev->platdata = plat;
  261. return 0;
  262. }
  263. static const struct udevice_id omap_gpio_ids[] = {
  264. { .compatible = "ti,omap3-gpio" },
  265. { .compatible = "ti,omap4-gpio" },
  266. { .compatible = "ti,am4372-gpio" },
  267. { }
  268. };
  269. U_BOOT_DRIVER(gpio_omap) = {
  270. .name = "gpio_omap",
  271. .id = UCLASS_GPIO,
  272. .ops = &gpio_omap_ops,
  273. .of_match = omap_gpio_ids,
  274. .bind = omap_gpio_bind,
  275. .probe = omap_gpio_probe,
  276. .priv_auto_alloc_size = sizeof(struct gpio_bank),
  277. };
  278. #endif /* CONFIG_DM_GPIO */