omap_gpio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 <asm/gpio.h>
  24. #include <asm/io.h>
  25. #include <asm/errno.h>
  26. #define OMAP_GPIO_DIR_OUT 0
  27. #define OMAP_GPIO_DIR_IN 1
  28. #ifdef CONFIG_DM_GPIO
  29. #define GPIO_PER_BANK 32
  30. struct gpio_bank {
  31. /* TODO(sjg@chromium.org): Can we use a struct here? */
  32. void *base; /* address of registers in physical memory */
  33. enum gpio_method method;
  34. };
  35. #endif
  36. static inline int get_gpio_index(int gpio)
  37. {
  38. return gpio & 0x1f;
  39. }
  40. int gpio_is_valid(int gpio)
  41. {
  42. return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
  43. }
  44. static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
  45. int is_input)
  46. {
  47. void *reg = bank->base;
  48. u32 l;
  49. switch (bank->method) {
  50. case METHOD_GPIO_24XX:
  51. reg += OMAP_GPIO_OE;
  52. break;
  53. default:
  54. return;
  55. }
  56. l = __raw_readl(reg);
  57. if (is_input)
  58. l |= 1 << gpio;
  59. else
  60. l &= ~(1 << gpio);
  61. __raw_writel(l, reg);
  62. }
  63. /**
  64. * Get the direction of the GPIO by reading the GPIO_OE register
  65. * corresponding to the specified bank.
  66. */
  67. static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
  68. {
  69. void *reg = bank->base;
  70. u32 v;
  71. switch (bank->method) {
  72. case METHOD_GPIO_24XX:
  73. reg += OMAP_GPIO_OE;
  74. break;
  75. default:
  76. return -1;
  77. }
  78. v = __raw_readl(reg);
  79. if (v & (1 << gpio))
  80. return OMAP_GPIO_DIR_IN;
  81. else
  82. return OMAP_GPIO_DIR_OUT;
  83. }
  84. static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
  85. int enable)
  86. {
  87. void *reg = bank->base;
  88. u32 l = 0;
  89. switch (bank->method) {
  90. case METHOD_GPIO_24XX:
  91. if (enable)
  92. reg += OMAP_GPIO_SETDATAOUT;
  93. else
  94. reg += OMAP_GPIO_CLEARDATAOUT;
  95. l = 1 << gpio;
  96. break;
  97. default:
  98. printf("omap3-gpio unknown bank method %s %d\n",
  99. __FILE__, __LINE__);
  100. return;
  101. }
  102. __raw_writel(l, reg);
  103. }
  104. static int _get_gpio_value(const struct gpio_bank *bank, int gpio)
  105. {
  106. void *reg = bank->base;
  107. int input;
  108. switch (bank->method) {
  109. case METHOD_GPIO_24XX:
  110. input = _get_gpio_direction(bank, gpio);
  111. switch (input) {
  112. case OMAP_GPIO_DIR_IN:
  113. reg += OMAP_GPIO_DATAIN;
  114. break;
  115. case OMAP_GPIO_DIR_OUT:
  116. reg += OMAP_GPIO_DATAOUT;
  117. break;
  118. default:
  119. return -1;
  120. }
  121. break;
  122. default:
  123. return -1;
  124. }
  125. return (__raw_readl(reg) & (1 << gpio)) != 0;
  126. }
  127. #ifndef CONFIG_DM_GPIO
  128. static inline const struct gpio_bank *get_gpio_bank(int gpio)
  129. {
  130. return &omap_gpio_bank[gpio >> 5];
  131. }
  132. static int check_gpio(int gpio)
  133. {
  134. if (!gpio_is_valid(gpio)) {
  135. printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
  136. return -1;
  137. }
  138. return 0;
  139. }
  140. /**
  141. * Set value of the specified gpio
  142. */
  143. int gpio_set_value(unsigned gpio, int value)
  144. {
  145. const struct gpio_bank *bank;
  146. if (check_gpio(gpio) < 0)
  147. return -1;
  148. bank = get_gpio_bank(gpio);
  149. _set_gpio_dataout(bank, get_gpio_index(gpio), value);
  150. return 0;
  151. }
  152. /**
  153. * Get value of the specified gpio
  154. */
  155. int gpio_get_value(unsigned gpio)
  156. {
  157. const struct gpio_bank *bank;
  158. if (check_gpio(gpio) < 0)
  159. return -1;
  160. bank = get_gpio_bank(gpio);
  161. return _get_gpio_value(bank, get_gpio_index(gpio));
  162. }
  163. /**
  164. * Set gpio direction as input
  165. */
  166. int gpio_direction_input(unsigned gpio)
  167. {
  168. const struct gpio_bank *bank;
  169. if (check_gpio(gpio) < 0)
  170. return -1;
  171. bank = get_gpio_bank(gpio);
  172. _set_gpio_direction(bank, get_gpio_index(gpio), 1);
  173. return 0;
  174. }
  175. /**
  176. * Set gpio direction as output
  177. */
  178. int gpio_direction_output(unsigned gpio, int value)
  179. {
  180. const struct gpio_bank *bank;
  181. if (check_gpio(gpio) < 0)
  182. return -1;
  183. bank = get_gpio_bank(gpio);
  184. _set_gpio_dataout(bank, get_gpio_index(gpio), value);
  185. _set_gpio_direction(bank, get_gpio_index(gpio), 0);
  186. return 0;
  187. }
  188. /**
  189. * Request a gpio before using it.
  190. *
  191. * NOTE: Argument 'label' is unused.
  192. */
  193. int gpio_request(unsigned gpio, const char *label)
  194. {
  195. if (check_gpio(gpio) < 0)
  196. return -1;
  197. return 0;
  198. }
  199. /**
  200. * Reset and free the gpio after using it.
  201. */
  202. int gpio_free(unsigned gpio)
  203. {
  204. return 0;
  205. }
  206. #else /* new driver model interface CONFIG_DM_GPIO */
  207. /* set GPIO pin 'gpio' as an input */
  208. static int omap_gpio_direction_input(struct udevice *dev, unsigned offset)
  209. {
  210. struct gpio_bank *bank = dev_get_priv(dev);
  211. /* Configure GPIO direction as input. */
  212. _set_gpio_direction(bank, offset, 1);
  213. return 0;
  214. }
  215. /* set GPIO pin 'gpio' as an output, with polarity 'value' */
  216. static int omap_gpio_direction_output(struct udevice *dev, unsigned offset,
  217. int value)
  218. {
  219. struct gpio_bank *bank = dev_get_priv(dev);
  220. _set_gpio_dataout(bank, offset, value);
  221. _set_gpio_direction(bank, offset, 0);
  222. return 0;
  223. }
  224. /* read GPIO IN value of pin 'gpio' */
  225. static int omap_gpio_get_value(struct udevice *dev, unsigned offset)
  226. {
  227. struct gpio_bank *bank = dev_get_priv(dev);
  228. return _get_gpio_value(bank, offset);
  229. }
  230. /* write GPIO OUT value to pin 'gpio' */
  231. static int omap_gpio_set_value(struct udevice *dev, unsigned offset,
  232. int value)
  233. {
  234. struct gpio_bank *bank = dev_get_priv(dev);
  235. _set_gpio_dataout(bank, offset, value);
  236. return 0;
  237. }
  238. static int omap_gpio_get_function(struct udevice *dev, unsigned offset)
  239. {
  240. struct gpio_bank *bank = dev_get_priv(dev);
  241. /* GPIOF_FUNC is not implemented yet */
  242. if (_get_gpio_direction(bank, offset) == OMAP_GPIO_DIR_OUT)
  243. return GPIOF_OUTPUT;
  244. else
  245. return GPIOF_INPUT;
  246. }
  247. static const struct dm_gpio_ops gpio_omap_ops = {
  248. .direction_input = omap_gpio_direction_input,
  249. .direction_output = omap_gpio_direction_output,
  250. .get_value = omap_gpio_get_value,
  251. .set_value = omap_gpio_set_value,
  252. .get_function = omap_gpio_get_function,
  253. };
  254. static int omap_gpio_probe(struct udevice *dev)
  255. {
  256. struct gpio_bank *bank = dev_get_priv(dev);
  257. struct omap_gpio_platdata *plat = dev_get_platdata(dev);
  258. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  259. char name[18], *str;
  260. sprintf(name, "GPIO%d_", plat->bank_index);
  261. str = strdup(name);
  262. if (!str)
  263. return -ENOMEM;
  264. uc_priv->bank_name = str;
  265. uc_priv->gpio_count = GPIO_PER_BANK;
  266. bank->base = (void *)plat->base;
  267. bank->method = plat->method;
  268. return 0;
  269. }
  270. U_BOOT_DRIVER(gpio_omap) = {
  271. .name = "gpio_omap",
  272. .id = UCLASS_GPIO,
  273. .ops = &gpio_omap_ops,
  274. .probe = omap_gpio_probe,
  275. .priv_auto_alloc_size = sizeof(struct gpio_bank),
  276. };
  277. #endif /* CONFIG_DM_GPIO */