mxc_gpio.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (C) 2009
  3. * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
  4. *
  5. * Copyright (C) 2011
  6. * Stefano Babic, DENX Software Engineering, <sbabic@denx.de>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <errno.h>
  12. #include <dm.h>
  13. #include <malloc.h>
  14. #include <asm/arch/imx-regs.h>
  15. #include <asm/gpio.h>
  16. #include <asm/io.h>
  17. enum mxc_gpio_direction {
  18. MXC_GPIO_DIRECTION_IN,
  19. MXC_GPIO_DIRECTION_OUT,
  20. };
  21. #define GPIO_PER_BANK 32
  22. struct mxc_gpio_plat {
  23. struct gpio_regs *regs;
  24. };
  25. struct mxc_bank_info {
  26. struct gpio_regs *regs;
  27. };
  28. #ifndef CONFIG_DM_GPIO
  29. #define GPIO_TO_PORT(n) (n / 32)
  30. /* GPIO port description */
  31. static unsigned long gpio_ports[] = {
  32. [0] = GPIO1_BASE_ADDR,
  33. [1] = GPIO2_BASE_ADDR,
  34. [2] = GPIO3_BASE_ADDR,
  35. #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
  36. defined(CONFIG_MX53) || defined(CONFIG_MX6)
  37. [3] = GPIO4_BASE_ADDR,
  38. #endif
  39. #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
  40. [4] = GPIO5_BASE_ADDR,
  41. [5] = GPIO6_BASE_ADDR,
  42. #endif
  43. #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
  44. [6] = GPIO7_BASE_ADDR,
  45. #endif
  46. };
  47. static int mxc_gpio_direction(unsigned int gpio,
  48. enum mxc_gpio_direction direction)
  49. {
  50. unsigned int port = GPIO_TO_PORT(gpio);
  51. struct gpio_regs *regs;
  52. u32 l;
  53. if (port >= ARRAY_SIZE(gpio_ports))
  54. return -1;
  55. gpio &= 0x1f;
  56. regs = (struct gpio_regs *)gpio_ports[port];
  57. l = readl(&regs->gpio_dir);
  58. switch (direction) {
  59. case MXC_GPIO_DIRECTION_OUT:
  60. l |= 1 << gpio;
  61. break;
  62. case MXC_GPIO_DIRECTION_IN:
  63. l &= ~(1 << gpio);
  64. }
  65. writel(l, &regs->gpio_dir);
  66. return 0;
  67. }
  68. int gpio_set_value(unsigned gpio, int value)
  69. {
  70. unsigned int port = GPIO_TO_PORT(gpio);
  71. struct gpio_regs *regs;
  72. u32 l;
  73. if (port >= ARRAY_SIZE(gpio_ports))
  74. return -1;
  75. gpio &= 0x1f;
  76. regs = (struct gpio_regs *)gpio_ports[port];
  77. l = readl(&regs->gpio_dr);
  78. if (value)
  79. l |= 1 << gpio;
  80. else
  81. l &= ~(1 << gpio);
  82. writel(l, &regs->gpio_dr);
  83. return 0;
  84. }
  85. int gpio_get_value(unsigned gpio)
  86. {
  87. unsigned int port = GPIO_TO_PORT(gpio);
  88. struct gpio_regs *regs;
  89. u32 val;
  90. if (port >= ARRAY_SIZE(gpio_ports))
  91. return -1;
  92. gpio &= 0x1f;
  93. regs = (struct gpio_regs *)gpio_ports[port];
  94. val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
  95. return val;
  96. }
  97. int gpio_request(unsigned gpio, const char *label)
  98. {
  99. unsigned int port = GPIO_TO_PORT(gpio);
  100. if (port >= ARRAY_SIZE(gpio_ports))
  101. return -1;
  102. return 0;
  103. }
  104. int gpio_free(unsigned gpio)
  105. {
  106. return 0;
  107. }
  108. int gpio_direction_input(unsigned gpio)
  109. {
  110. return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
  111. }
  112. int gpio_direction_output(unsigned gpio, int value)
  113. {
  114. int ret = gpio_set_value(gpio, value);
  115. if (ret < 0)
  116. return ret;
  117. return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
  118. }
  119. #endif
  120. #ifdef CONFIG_DM_GPIO
  121. static int mxc_gpio_is_output(struct gpio_regs *regs, int offset)
  122. {
  123. u32 val;
  124. val = readl(&regs->gpio_dir);
  125. return val & (1 << offset) ? 1 : 0;
  126. }
  127. static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset,
  128. enum mxc_gpio_direction direction)
  129. {
  130. u32 l;
  131. l = readl(&regs->gpio_dir);
  132. switch (direction) {
  133. case MXC_GPIO_DIRECTION_OUT:
  134. l |= 1 << offset;
  135. break;
  136. case MXC_GPIO_DIRECTION_IN:
  137. l &= ~(1 << offset);
  138. }
  139. writel(l, &regs->gpio_dir);
  140. }
  141. static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset,
  142. int value)
  143. {
  144. u32 l;
  145. l = readl(&regs->gpio_dr);
  146. if (value)
  147. l |= 1 << offset;
  148. else
  149. l &= ~(1 << offset);
  150. writel(l, &regs->gpio_dr);
  151. }
  152. static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset)
  153. {
  154. return (readl(&regs->gpio_psr) >> offset) & 0x01;
  155. }
  156. /* set GPIO pin 'gpio' as an input */
  157. static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset)
  158. {
  159. struct mxc_bank_info *bank = dev_get_priv(dev);
  160. /* Configure GPIO direction as input. */
  161. mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN);
  162. return 0;
  163. }
  164. /* set GPIO pin 'gpio' as an output, with polarity 'value' */
  165. static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset,
  166. int value)
  167. {
  168. struct mxc_bank_info *bank = dev_get_priv(dev);
  169. /* Configure GPIO output value. */
  170. mxc_gpio_bank_set_value(bank->regs, offset, value);
  171. /* Configure GPIO direction as output. */
  172. mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT);
  173. return 0;
  174. }
  175. /* read GPIO IN value of pin 'gpio' */
  176. static int mxc_gpio_get_value(struct udevice *dev, unsigned offset)
  177. {
  178. struct mxc_bank_info *bank = dev_get_priv(dev);
  179. return mxc_gpio_bank_get_value(bank->regs, offset);
  180. }
  181. /* write GPIO OUT value to pin 'gpio' */
  182. static int mxc_gpio_set_value(struct udevice *dev, unsigned offset,
  183. int value)
  184. {
  185. struct mxc_bank_info *bank = dev_get_priv(dev);
  186. mxc_gpio_bank_set_value(bank->regs, offset, value);
  187. return 0;
  188. }
  189. static int mxc_gpio_get_function(struct udevice *dev, unsigned offset)
  190. {
  191. struct mxc_bank_info *bank = dev_get_priv(dev);
  192. /* GPIOF_FUNC is not implemented yet */
  193. if (mxc_gpio_is_output(bank->regs, offset))
  194. return GPIOF_OUTPUT;
  195. else
  196. return GPIOF_INPUT;
  197. }
  198. static const struct dm_gpio_ops gpio_mxc_ops = {
  199. .direction_input = mxc_gpio_direction_input,
  200. .direction_output = mxc_gpio_direction_output,
  201. .get_value = mxc_gpio_get_value,
  202. .set_value = mxc_gpio_set_value,
  203. .get_function = mxc_gpio_get_function,
  204. };
  205. static const struct mxc_gpio_plat mxc_plat[] = {
  206. { (struct gpio_regs *)GPIO1_BASE_ADDR },
  207. { (struct gpio_regs *)GPIO2_BASE_ADDR },
  208. { (struct gpio_regs *)GPIO3_BASE_ADDR },
  209. #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
  210. defined(CONFIG_MX53) || defined(CONFIG_MX6)
  211. { (struct gpio_regs *)GPIO4_BASE_ADDR },
  212. #endif
  213. #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
  214. { (struct gpio_regs *)GPIO5_BASE_ADDR },
  215. { (struct gpio_regs *)GPIO6_BASE_ADDR },
  216. #endif
  217. #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
  218. { (struct gpio_regs *)GPIO7_BASE_ADDR },
  219. #endif
  220. };
  221. static int mxc_gpio_probe(struct udevice *dev)
  222. {
  223. struct mxc_bank_info *bank = dev_get_priv(dev);
  224. struct mxc_gpio_plat *plat = dev_get_platdata(dev);
  225. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  226. int banknum;
  227. char name[18], *str;
  228. banknum = plat - mxc_plat;
  229. sprintf(name, "GPIO%d_", banknum + 1);
  230. str = strdup(name);
  231. if (!str)
  232. return -ENOMEM;
  233. uc_priv->bank_name = str;
  234. uc_priv->gpio_count = GPIO_PER_BANK;
  235. bank->regs = plat->regs;
  236. return 0;
  237. }
  238. U_BOOT_DRIVER(gpio_mxc) = {
  239. .name = "gpio_mxc",
  240. .id = UCLASS_GPIO,
  241. .ops = &gpio_mxc_ops,
  242. .probe = mxc_gpio_probe,
  243. .priv_auto_alloc_size = sizeof(struct mxc_bank_info),
  244. };
  245. U_BOOT_DEVICES(mxc_gpios) = {
  246. { "gpio_mxc", &mxc_plat[0] },
  247. { "gpio_mxc", &mxc_plat[1] },
  248. { "gpio_mxc", &mxc_plat[2] },
  249. #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
  250. defined(CONFIG_MX53) || defined(CONFIG_MX6)
  251. { "gpio_mxc", &mxc_plat[3] },
  252. #endif
  253. #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
  254. { "gpio_mxc", &mxc_plat[4] },
  255. { "gpio_mxc", &mxc_plat[5] },
  256. #endif
  257. #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
  258. { "gpio_mxc", &mxc_plat[6] },
  259. #endif
  260. };
  261. #endif