lpc32xx_gpio.c 7.1 KB

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