lpc32xx_gpio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * LPC32xxGPIO driver
  3. *
  4. * (C) Copyright 2014 DENX Software Engineering GmbH
  5. * Written-by: Albert ARIBAUD <albert.aribaud@3adev.fr>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <asm/io.h>
  10. #include <asm/arch-lpc32xx/cpu.h>
  11. #include <asm/arch-lpc32xx/gpio.h>
  12. #include <asm-generic/gpio.h>
  13. #include <dm.h>
  14. /**
  15. * LPC32xx GPIOs work in banks but are non-homogeneous:
  16. * - each bank holds a different number of GPIOs
  17. * - some GPIOs are input/ouput, some input only, some output only;
  18. * - some GPIOs have different meanings as an input and as an output;
  19. * - some GPIOs are controlled on a given port and bit index, but
  20. * read on another one.
  21. *
  22. * In order to keep this code simple, GPIOS are considered here as
  23. * homogeneous and linear, from 0 to 127.
  24. *
  25. * ** WARNING #1 **
  26. *
  27. * Client code is responsible for properly using valid GPIO numbers,
  28. * including cases where a single physical GPIO has differing numbers
  29. * for setting its direction, reading it and/or writing to it.
  30. *
  31. * ** WARNING #2 **
  32. *
  33. * Please read NOTE in description of lpc32xx_gpio_get_function().
  34. */
  35. #define LPC32XX_GPIOS 128
  36. struct lpc32xx_gpio_platdata {
  37. struct gpio_regs *regs;
  38. /* GPIO FUNCTION: SEE WARNING #2 */
  39. signed char function[LPC32XX_GPIOS];
  40. };
  41. /**
  42. * We have 4 GPIO ports of 32 bits each
  43. */
  44. #define MAX_GPIO 128
  45. #define GPIO_TO_PORT(gpio) ((gpio / 32) & 3)
  46. #define GPIO_TO_RANK(gpio) (gpio % 32)
  47. #define GPIO_TO_MASK(gpio) (1 << (gpio % 32))
  48. /**
  49. * Configure a GPIO number 'offset' as input
  50. */
  51. static int lpc32xx_gpio_direction_input(struct udevice *dev, unsigned offset)
  52. {
  53. int port, mask;
  54. struct lpc32xx_gpio_platdata *gpio_platdata = dev_get_platdata(dev);
  55. struct gpio_regs *regs = gpio_platdata->regs;
  56. port = GPIO_TO_PORT(offset);
  57. mask = GPIO_TO_MASK(offset);
  58. switch (port) {
  59. case 0:
  60. writel(mask, &regs->p0_dir_clr);
  61. break;
  62. case 1:
  63. writel(mask, &regs->p1_dir_clr);
  64. break;
  65. case 2:
  66. /* ports 2 and 3 share a common direction */
  67. case 3:
  68. writel(mask, &regs->p2_p3_dir_clr);
  69. break;
  70. default:
  71. return -1;
  72. }
  73. /* GPIO FUNCTION: SEE WARNING #2 */
  74. gpio_platdata->function[offset] = GPIOF_INPUT;
  75. return 0;
  76. }
  77. /**
  78. * Get the value of a GPIO
  79. */
  80. static int lpc32xx_gpio_get_value(struct udevice *dev, unsigned offset)
  81. {
  82. int port, rank, mask, value;
  83. struct lpc32xx_gpio_platdata *gpio_platdata = dev_get_platdata(dev);
  84. struct gpio_regs *regs = gpio_platdata->regs;
  85. port = GPIO_TO_PORT(offset);
  86. switch (port) {
  87. case 0:
  88. value = readl(&regs->p0_inp_state);
  89. break;
  90. case 1:
  91. value = readl(&regs->p1_inp_state);
  92. break;
  93. case 2:
  94. value = readl(&regs->p2_inp_state);
  95. break;
  96. case 3:
  97. value = readl(&regs->p3_inp_state);
  98. break;
  99. default:
  100. return -1;
  101. }
  102. rank = GPIO_TO_RANK(offset);
  103. mask = GPIO_TO_MASK(offset);
  104. return (value & mask) >> rank;
  105. }
  106. /**
  107. * Set a GPIO
  108. */
  109. static int gpio_set(struct udevice *dev, unsigned gpio)
  110. {
  111. int port, mask;
  112. struct lpc32xx_gpio_platdata *gpio_platdata = dev_get_platdata(dev);
  113. struct gpio_regs *regs = gpio_platdata->regs;
  114. port = GPIO_TO_PORT(gpio);
  115. mask = GPIO_TO_MASK(gpio);
  116. switch (port) {
  117. case 0:
  118. writel(mask, &regs->p0_outp_set);
  119. break;
  120. case 1:
  121. writel(mask, &regs->p1_outp_set);
  122. break;
  123. case 2:
  124. writel(mask, &regs->p2_outp_set);
  125. break;
  126. case 3:
  127. writel(mask, &regs->p3_outp_set);
  128. break;
  129. default:
  130. return -1;
  131. }
  132. return 0;
  133. }
  134. /**
  135. * Clear a GPIO
  136. */
  137. static int gpio_clr(struct udevice *dev, unsigned gpio)
  138. {
  139. int port, mask;
  140. struct lpc32xx_gpio_platdata *gpio_platdata = dev_get_platdata(dev);
  141. struct gpio_regs *regs = gpio_platdata->regs;
  142. port = GPIO_TO_PORT(gpio);
  143. mask = GPIO_TO_MASK(gpio);
  144. switch (port) {
  145. case 0:
  146. writel(mask, &regs->p0_outp_clr);
  147. break;
  148. case 1:
  149. writel(mask, &regs->p1_outp_clr);
  150. break;
  151. case 2:
  152. writel(mask, &regs->p2_outp_clr);
  153. break;
  154. case 3:
  155. writel(mask, &regs->p3_outp_clr);
  156. break;
  157. default:
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. /**
  163. * Set the value of a GPIO
  164. */
  165. static int lpc32xx_gpio_set_value(struct udevice *dev, unsigned offset,
  166. int value)
  167. {
  168. if (value)
  169. return gpio_set(dev, offset);
  170. else
  171. return gpio_clr(dev, offset);
  172. }
  173. /**
  174. * Configure a GPIO number 'offset' as output with given initial value.
  175. */
  176. static int lpc32xx_gpio_direction_output(struct udevice *dev, unsigned offset,
  177. int value)
  178. {
  179. int port, mask;
  180. struct lpc32xx_gpio_platdata *gpio_platdata = dev_get_platdata(dev);
  181. struct gpio_regs *regs = gpio_platdata->regs;
  182. port = GPIO_TO_PORT(offset);
  183. mask = GPIO_TO_MASK(offset);
  184. switch (port) {
  185. case 0:
  186. writel(mask, &regs->p0_dir_set);
  187. break;
  188. case 1:
  189. writel(mask, &regs->p1_dir_set);
  190. break;
  191. case 2:
  192. /* ports 2 and 3 share a common direction */
  193. case 3:
  194. writel(mask, &regs->p2_p3_dir_set);
  195. break;
  196. default:
  197. return -1;
  198. }
  199. /* GPIO FUNCTION: SEE WARNING #2 */
  200. gpio_platdata->function[offset] = GPIOF_OUTPUT;
  201. return lpc32xx_gpio_set_value(dev, offset, value);
  202. }
  203. /**
  204. * GPIO functions are supposed to be computed from their current
  205. * configuration, but that's way too complicated in LPC32XX. A simpler
  206. * approach is used, where the GPIO functions are cached in an array.
  207. * When the GPIO is in use, its function is either "input" or "output"
  208. * depending on its direction, otherwise its function is "unknown".
  209. *
  210. * ** NOTE **
  211. *
  212. * THIS APPROACH WAS CHOSEN DU TO THE COMPLEX NATURE OF THE LPC32XX
  213. * GPIOS; DO NOT TAKE THIS AS AN EXAMPLE FOR NEW CODE.
  214. */
  215. static int lpc32xx_gpio_get_function(struct udevice *dev, unsigned offset)
  216. {
  217. struct lpc32xx_gpio_platdata *gpio_platdata = dev_get_platdata(dev);
  218. return gpio_platdata->function[offset];
  219. }
  220. static const struct dm_gpio_ops gpio_lpc32xx_ops = {
  221. .direction_input = lpc32xx_gpio_direction_input,
  222. .direction_output = lpc32xx_gpio_direction_output,
  223. .get_value = lpc32xx_gpio_get_value,
  224. .set_value = lpc32xx_gpio_set_value,
  225. .get_function = lpc32xx_gpio_get_function,
  226. };
  227. static int lpc32xx_gpio_probe(struct udevice *dev)
  228. {
  229. struct lpc32xx_gpio_platdata *gpio_platdata = dev_get_platdata(dev);
  230. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  231. if (dev->of_offset == -1) {
  232. /* Tell the uclass how many GPIOs we have */
  233. uc_priv->gpio_count = LPC32XX_GPIOS;
  234. }
  235. /* set base address for GPIO registers */
  236. gpio_platdata->regs = (struct gpio_regs *)GPIO_BASE;
  237. /* all GPIO functions are unknown until requested */
  238. /* GPIO FUNCTION: SEE WARNING #2 */
  239. memset(gpio_platdata->function, GPIOF_UNKNOWN,
  240. sizeof(gpio_platdata->function));
  241. return 0;
  242. }
  243. U_BOOT_DRIVER(gpio_lpc32xx) = {
  244. .name = "gpio_lpc32xx",
  245. .id = UCLASS_GPIO,
  246. .ops = &gpio_lpc32xx_ops,
  247. .probe = lpc32xx_gpio_probe,
  248. .priv_auto_alloc_size = sizeof(struct lpc32xx_gpio_platdata),
  249. };