mxc_gpio.c 8.5 KB

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