mxc_gpio.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (C) 2009
  3. * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #ifdef CONFIG_MX31
  25. #include <asm/arch/mx31-regs.h>
  26. #endif
  27. #if defined(CONFIG_MX51) || defined(CONFIG_MX53)
  28. #include <asm/arch/imx-regs.h>
  29. #endif
  30. #include <asm/io.h>
  31. #include <mxc_gpio.h>
  32. /* GPIO port description */
  33. static unsigned long gpio_ports[] = {
  34. [0] = GPIO1_BASE_ADDR,
  35. [1] = GPIO2_BASE_ADDR,
  36. [2] = GPIO3_BASE_ADDR,
  37. #if defined(CONFIG_MX51) || defined(CONFIG_MX53)
  38. [3] = GPIO4_BASE_ADDR,
  39. #endif
  40. #if defined(CONFIG_MX53)
  41. [4] = GPIO5_BASE_ADDR,
  42. [5] = GPIO6_BASE_ADDR,
  43. [6] = GPIO7_BASE_ADDR,
  44. #endif
  45. };
  46. int mxc_gpio_direction(unsigned int gpio, enum mxc_gpio_direction direction)
  47. {
  48. unsigned int port = gpio >> 5;
  49. struct gpio_regs *regs;
  50. u32 l;
  51. if (port >= ARRAY_SIZE(gpio_ports))
  52. return 1;
  53. gpio &= 0x1f;
  54. regs = (struct gpio_regs *)gpio_ports[port];
  55. l = readl(&regs->gpio_dir);
  56. switch (direction) {
  57. case MXC_GPIO_DIRECTION_OUT:
  58. l |= 1 << gpio;
  59. break;
  60. case MXC_GPIO_DIRECTION_IN:
  61. l &= ~(1 << gpio);
  62. }
  63. writel(l, &regs->gpio_dir);
  64. return 0;
  65. }
  66. void mxc_gpio_set(unsigned int gpio, unsigned int value)
  67. {
  68. unsigned int port = gpio >> 5;
  69. struct gpio_regs *regs;
  70. u32 l;
  71. if (port >= ARRAY_SIZE(gpio_ports))
  72. return;
  73. gpio &= 0x1f;
  74. regs = (struct gpio_regs *)gpio_ports[port];
  75. l = readl(&regs->gpio_dr);
  76. if (value)
  77. l |= 1 << gpio;
  78. else
  79. l &= ~(1 << gpio);
  80. writel(l, &regs->gpio_dr);
  81. }
  82. int mxc_gpio_get(unsigned int gpio)
  83. {
  84. unsigned int port = gpio >> 5;
  85. struct gpio_regs *regs;
  86. u32 l;
  87. if (port >= ARRAY_SIZE(gpio_ports))
  88. return -1;
  89. gpio &= 0x1f;
  90. regs = (struct gpio_regs *)gpio_ports[port];
  91. l = (readl(&regs->gpio_dr) >> gpio) & 0x01;
  92. return l;
  93. }