mxc_gpio.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include <asm/arch/imx-regs.h>
  25. #include <asm/io.h>
  26. #include <mxc_gpio.h>
  27. #include <errno.h>
  28. /* GPIO port description */
  29. static unsigned long gpio_ports[] = {
  30. [0] = GPIO1_BASE_ADDR,
  31. [1] = GPIO2_BASE_ADDR,
  32. [2] = GPIO3_BASE_ADDR,
  33. #if defined(CONFIG_MX51) || defined(CONFIG_MX53)
  34. [3] = GPIO4_BASE_ADDR,
  35. #endif
  36. #if defined(CONFIG_MX53)
  37. [4] = GPIO5_BASE_ADDR,
  38. [5] = GPIO6_BASE_ADDR,
  39. [6] = GPIO7_BASE_ADDR,
  40. #endif
  41. };
  42. int mxc_gpio_direction(unsigned int gpio, enum mxc_gpio_direction direction)
  43. {
  44. unsigned int port = gpio >> 5;
  45. struct gpio_regs *regs;
  46. u32 l;
  47. if (port >= ARRAY_SIZE(gpio_ports))
  48. return -EINVAL;
  49. gpio &= 0x1f;
  50. regs = (struct gpio_regs *)gpio_ports[port];
  51. l = readl(&regs->gpio_dir);
  52. switch (direction) {
  53. case MXC_GPIO_DIRECTION_OUT:
  54. l |= 1 << gpio;
  55. break;
  56. case MXC_GPIO_DIRECTION_IN:
  57. l &= ~(1 << gpio);
  58. }
  59. writel(l, &regs->gpio_dir);
  60. return 0;
  61. }
  62. void mxc_gpio_set(unsigned int gpio, unsigned int value)
  63. {
  64. unsigned int port = gpio >> 5;
  65. struct gpio_regs *regs;
  66. u32 l;
  67. if (port >= ARRAY_SIZE(gpio_ports))
  68. return;
  69. gpio &= 0x1f;
  70. regs = (struct gpio_regs *)gpio_ports[port];
  71. l = readl(&regs->gpio_dr);
  72. if (value)
  73. l |= 1 << gpio;
  74. else
  75. l &= ~(1 << gpio);
  76. writel(l, &regs->gpio_dr);
  77. }
  78. int mxc_gpio_get(unsigned int gpio)
  79. {
  80. unsigned int port = gpio >> 5;
  81. struct gpio_regs *regs;
  82. u32 l;
  83. if (port >= ARRAY_SIZE(gpio_ports))
  84. return -EINVAL;
  85. gpio &= 0x1f;
  86. regs = (struct gpio_regs *)gpio_ports[port];
  87. l = (readl(&regs->gpio_dr) >> gpio) & 0x01;
  88. return l;
  89. }