gpio.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * (C) Copyright 2008 Stefan Roese <sr@denx.de>, DENX Software Engineering
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  17. * MA 02111-1307 USA
  18. */
  19. #include <common.h>
  20. #include <asm/io.h>
  21. #include "vct.h"
  22. /*
  23. * Find out to which of the 2 gpio modules the pin specified in the
  24. * argument belongs:
  25. * GPIO_MODULE yields 0 for pins 0 to 31,
  26. * 1 for pins 32 to 63
  27. */
  28. #define GPIO_MODULE(pin) ((pin) >> 5)
  29. /*
  30. * Bit position within a 32-bit peripheral register (where every
  31. * bit is one bitslice)
  32. */
  33. #define MASK(pin) (1 << ((pin) & 0x1F))
  34. #define BASE_ADDR(mod) module_base[mod]
  35. /*
  36. * Lookup table for transforming gpio module number 0 to 2 to
  37. * address offsets
  38. */
  39. static u32 module_base[] = {
  40. GPIO1_BASE,
  41. GPIO2_BASE
  42. };
  43. static void clrsetbits(u32 addr, u32 and_mask, u32 or_mask)
  44. {
  45. reg_write(addr, (reg_read(addr) & ~and_mask) | or_mask);
  46. }
  47. int vct_gpio_dir(int pin, int dir)
  48. {
  49. u32 gpio_base;
  50. gpio_base = BASE_ADDR(GPIO_MODULE(pin));
  51. if (dir == 0)
  52. clrsetbits(GPIO_SWPORTA_DDR(gpio_base), MASK(pin), 0);
  53. else
  54. clrsetbits(GPIO_SWPORTA_DDR(gpio_base), 0, MASK(pin));
  55. return 0;
  56. }
  57. void vct_gpio_set(int pin, int val)
  58. {
  59. u32 gpio_base;
  60. gpio_base = BASE_ADDR(GPIO_MODULE(pin));
  61. if (val == 0)
  62. clrsetbits(GPIO_SWPORTA_DR(gpio_base), MASK(pin), 0);
  63. else
  64. clrsetbits(GPIO_SWPORTA_DR(gpio_base), 0, MASK(pin));
  65. }
  66. int vct_gpio_get(int pin)
  67. {
  68. u32 gpio_base;
  69. u32 value;
  70. gpio_base = BASE_ADDR(GPIO_MODULE(pin));
  71. value = reg_read(GPIO_EXT_PORTA(gpio_base));
  72. return ((value & MASK(pin)) ? 1 : 0);
  73. }