mxc_gpio.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. int bank_index;
  24. struct gpio_regs *regs;
  25. };
  26. struct mxc_bank_info {
  27. struct gpio_regs *regs;
  28. };
  29. #ifndef CONFIG_DM_GPIO
  30. #define GPIO_TO_PORT(n) (n / 32)
  31. /* GPIO port description */
  32. static unsigned long gpio_ports[] = {
  33. [0] = GPIO1_BASE_ADDR,
  34. [1] = GPIO2_BASE_ADDR,
  35. [2] = GPIO3_BASE_ADDR,
  36. #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
  37. defined(CONFIG_MX53) || defined(CONFIG_MX6)
  38. [3] = GPIO4_BASE_ADDR,
  39. #endif
  40. #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
  41. [4] = GPIO5_BASE_ADDR,
  42. [5] = GPIO6_BASE_ADDR,
  43. #endif
  44. #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
  45. [6] = GPIO7_BASE_ADDR,
  46. #endif
  47. };
  48. static int mxc_gpio_direction(unsigned int gpio,
  49. enum mxc_gpio_direction direction)
  50. {
  51. unsigned int port = GPIO_TO_PORT(gpio);
  52. struct gpio_regs *regs;
  53. u32 l;
  54. if (port >= ARRAY_SIZE(gpio_ports))
  55. return -1;
  56. gpio &= 0x1f;
  57. regs = (struct gpio_regs *)gpio_ports[port];
  58. l = readl(&regs->gpio_dir);
  59. switch (direction) {
  60. case MXC_GPIO_DIRECTION_OUT:
  61. l |= 1 << gpio;
  62. break;
  63. case MXC_GPIO_DIRECTION_IN:
  64. l &= ~(1 << gpio);
  65. }
  66. writel(l, &regs->gpio_dir);
  67. return 0;
  68. }
  69. int gpio_set_value(unsigned gpio, int value)
  70. {
  71. unsigned int port = GPIO_TO_PORT(gpio);
  72. struct gpio_regs *regs;
  73. u32 l;
  74. if (port >= ARRAY_SIZE(gpio_ports))
  75. return -1;
  76. gpio &= 0x1f;
  77. regs = (struct gpio_regs *)gpio_ports[port];
  78. l = readl(&regs->gpio_dr);
  79. if (value)
  80. l |= 1 << gpio;
  81. else
  82. l &= ~(1 << gpio);
  83. writel(l, &regs->gpio_dr);
  84. return 0;
  85. }
  86. int gpio_get_value(unsigned gpio)
  87. {
  88. unsigned int port = GPIO_TO_PORT(gpio);
  89. struct gpio_regs *regs;
  90. u32 val;
  91. if (port >= ARRAY_SIZE(gpio_ports))
  92. return -1;
  93. gpio &= 0x1f;
  94. regs = (struct gpio_regs *)gpio_ports[port];
  95. val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
  96. return val;
  97. }
  98. int gpio_request(unsigned gpio, const char *label)
  99. {
  100. unsigned int port = GPIO_TO_PORT(gpio);
  101. if (port >= ARRAY_SIZE(gpio_ports))
  102. return -1;
  103. return 0;
  104. }
  105. int gpio_free(unsigned gpio)
  106. {
  107. return 0;
  108. }
  109. int gpio_direction_input(unsigned gpio)
  110. {
  111. return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
  112. }
  113. int gpio_direction_output(unsigned gpio, int value)
  114. {
  115. int ret = gpio_set_value(gpio, value);
  116. if (ret < 0)
  117. return ret;
  118. return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
  119. }
  120. #endif
  121. #ifdef CONFIG_DM_GPIO
  122. #include <fdtdec.h>
  123. DECLARE_GLOBAL_DATA_PTR;
  124. static int mxc_gpio_is_output(struct gpio_regs *regs, int offset)
  125. {
  126. u32 val;
  127. val = readl(&regs->gpio_dir);
  128. return val & (1 << offset) ? 1 : 0;
  129. }
  130. static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset,
  131. enum mxc_gpio_direction direction)
  132. {
  133. u32 l;
  134. l = readl(&regs->gpio_dir);
  135. switch (direction) {
  136. case MXC_GPIO_DIRECTION_OUT:
  137. l |= 1 << offset;
  138. break;
  139. case MXC_GPIO_DIRECTION_IN:
  140. l &= ~(1 << offset);
  141. }
  142. writel(l, &regs->gpio_dir);
  143. }
  144. static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset,
  145. int value)
  146. {
  147. u32 l;
  148. l = readl(&regs->gpio_dr);
  149. if (value)
  150. l |= 1 << offset;
  151. else
  152. l &= ~(1 << offset);
  153. writel(l, &regs->gpio_dr);
  154. }
  155. static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset)
  156. {
  157. return (readl(&regs->gpio_psr) >> offset) & 0x01;
  158. }
  159. /* set GPIO pin 'gpio' as an input */
  160. static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset)
  161. {
  162. struct mxc_bank_info *bank = dev_get_priv(dev);
  163. /* Configure GPIO direction as input. */
  164. mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN);
  165. return 0;
  166. }
  167. /* set GPIO pin 'gpio' as an output, with polarity 'value' */
  168. static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset,
  169. int value)
  170. {
  171. struct mxc_bank_info *bank = dev_get_priv(dev);
  172. /* Configure GPIO output value. */
  173. mxc_gpio_bank_set_value(bank->regs, offset, value);
  174. /* Configure GPIO direction as output. */
  175. mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT);
  176. return 0;
  177. }
  178. /* read GPIO IN value of pin 'gpio' */
  179. static int mxc_gpio_get_value(struct udevice *dev, unsigned offset)
  180. {
  181. struct mxc_bank_info *bank = dev_get_priv(dev);
  182. return mxc_gpio_bank_get_value(bank->regs, offset);
  183. }
  184. /* write GPIO OUT value to pin 'gpio' */
  185. static int mxc_gpio_set_value(struct udevice *dev, unsigned offset,
  186. int value)
  187. {
  188. struct mxc_bank_info *bank = dev_get_priv(dev);
  189. mxc_gpio_bank_set_value(bank->regs, offset, value);
  190. return 0;
  191. }
  192. static int mxc_gpio_get_function(struct udevice *dev, unsigned offset)
  193. {
  194. struct mxc_bank_info *bank = dev_get_priv(dev);
  195. /* GPIOF_FUNC is not implemented yet */
  196. if (mxc_gpio_is_output(bank->regs, offset))
  197. return GPIOF_OUTPUT;
  198. else
  199. return GPIOF_INPUT;
  200. }
  201. static const struct dm_gpio_ops gpio_mxc_ops = {
  202. .direction_input = mxc_gpio_direction_input,
  203. .direction_output = mxc_gpio_direction_output,
  204. .get_value = mxc_gpio_get_value,
  205. .set_value = mxc_gpio_set_value,
  206. .get_function = mxc_gpio_get_function,
  207. };
  208. static int mxc_gpio_probe(struct udevice *dev)
  209. {
  210. struct mxc_bank_info *bank = dev_get_priv(dev);
  211. struct mxc_gpio_plat *plat = dev_get_platdata(dev);
  212. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  213. int banknum;
  214. char name[18], *str;
  215. banknum = plat->bank_index;
  216. sprintf(name, "GPIO%d_", banknum + 1);
  217. str = strdup(name);
  218. if (!str)
  219. return -ENOMEM;
  220. uc_priv->bank_name = str;
  221. uc_priv->gpio_count = GPIO_PER_BANK;
  222. bank->regs = plat->regs;
  223. return 0;
  224. }
  225. static int mxc_gpio_bind(struct udevice *dev)
  226. {
  227. struct mxc_gpio_plat *plat = dev->platdata;
  228. fdt_addr_t addr;
  229. /*
  230. * If platdata already exsits, directly return.
  231. * Actually only when DT is not supported, platdata
  232. * is statically initialized in U_BOOT_DEVICES.Here
  233. * will return.
  234. */
  235. if (plat)
  236. return 0;
  237. addr = dev_get_addr(dev);
  238. if (addr == FDT_ADDR_T_NONE)
  239. return -ENODEV;
  240. /*
  241. * TODO:
  242. * When every board is converted to driver model and DT is supported,
  243. * this can be done by auto-alloc feature, but not using calloc
  244. * to alloc memory for platdata.
  245. */
  246. plat = calloc(1, sizeof(*plat));
  247. if (!plat)
  248. return -ENOMEM;
  249. plat->regs = (struct gpio_regs *)addr;
  250. plat->bank_index = dev->req_seq;
  251. dev->platdata = plat;
  252. return 0;
  253. }
  254. static const struct udevice_id mxc_gpio_ids[] = {
  255. { .compatible = "fsl,imx35-gpio" },
  256. { }
  257. };
  258. U_BOOT_DRIVER(gpio_mxc) = {
  259. .name = "gpio_mxc",
  260. .id = UCLASS_GPIO,
  261. .ops = &gpio_mxc_ops,
  262. .probe = mxc_gpio_probe,
  263. .priv_auto_alloc_size = sizeof(struct mxc_bank_info),
  264. .of_match = mxc_gpio_ids,
  265. .bind = mxc_gpio_bind,
  266. };
  267. #ifndef CONFIG_OF_CONTROL
  268. static const struct mxc_gpio_plat mxc_plat[] = {
  269. { 0, (struct gpio_regs *)GPIO1_BASE_ADDR },
  270. { 1, (struct gpio_regs *)GPIO2_BASE_ADDR },
  271. { 2, (struct gpio_regs *)GPIO3_BASE_ADDR },
  272. #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
  273. defined(CONFIG_MX53) || defined(CONFIG_MX6)
  274. { 3, (struct gpio_regs *)GPIO4_BASE_ADDR },
  275. #endif
  276. #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
  277. { 4, (struct gpio_regs *)GPIO5_BASE_ADDR },
  278. { 5, (struct gpio_regs *)GPIO6_BASE_ADDR },
  279. #endif
  280. #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
  281. { 6, (struct gpio_regs *)GPIO7_BASE_ADDR },
  282. #endif
  283. };
  284. U_BOOT_DEVICES(mxc_gpios) = {
  285. { "gpio_mxc", &mxc_plat[0] },
  286. { "gpio_mxc", &mxc_plat[1] },
  287. { "gpio_mxc", &mxc_plat[2] },
  288. #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
  289. defined(CONFIG_MX53) || defined(CONFIG_MX6)
  290. { "gpio_mxc", &mxc_plat[3] },
  291. #endif
  292. #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
  293. { "gpio_mxc", &mxc_plat[4] },
  294. { "gpio_mxc", &mxc_plat[5] },
  295. #endif
  296. #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
  297. { "gpio_mxc", &mxc_plat[6] },
  298. #endif
  299. };
  300. #endif
  301. #endif