gpio.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * (C) Copyright 2008 Stefan Roese <sr@denx.de>, DENX Software Engineering
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <asm/io.h>
  8. #include "vct.h"
  9. /*
  10. * Find out to which of the 2 gpio modules the pin specified in the
  11. * argument belongs:
  12. * GPIO_MODULE yields 0 for pins 0 to 31,
  13. * 1 for pins 32 to 63
  14. */
  15. #define GPIO_MODULE(pin) ((pin) >> 5)
  16. /*
  17. * Bit position within a 32-bit peripheral register (where every
  18. * bit is one bitslice)
  19. */
  20. #define MASK(pin) (1 << ((pin) & 0x1F))
  21. #define BASE_ADDR(mod) module_base[mod]
  22. /*
  23. * Lookup table for transforming gpio module number 0 to 2 to
  24. * address offsets
  25. */
  26. static u32 module_base[] = {
  27. GPIO1_BASE,
  28. GPIO2_BASE
  29. };
  30. static void clrsetbits(u32 addr, u32 and_mask, u32 or_mask)
  31. {
  32. reg_write(addr, (reg_read(addr) & ~and_mask) | or_mask);
  33. }
  34. int vct_gpio_dir(int pin, int dir)
  35. {
  36. u32 gpio_base;
  37. gpio_base = BASE_ADDR(GPIO_MODULE(pin));
  38. if (dir == 0)
  39. clrsetbits(GPIO_SWPORTA_DDR(gpio_base), MASK(pin), 0);
  40. else
  41. clrsetbits(GPIO_SWPORTA_DDR(gpio_base), 0, MASK(pin));
  42. return 0;
  43. }
  44. void vct_gpio_set(int pin, int val)
  45. {
  46. u32 gpio_base;
  47. gpio_base = BASE_ADDR(GPIO_MODULE(pin));
  48. if (val == 0)
  49. clrsetbits(GPIO_SWPORTA_DR(gpio_base), MASK(pin), 0);
  50. else
  51. clrsetbits(GPIO_SWPORTA_DR(gpio_base), 0, MASK(pin));
  52. }
  53. int vct_gpio_get(int pin)
  54. {
  55. u32 gpio_base;
  56. u32 value;
  57. gpio_base = BASE_ADDR(GPIO_MODULE(pin));
  58. value = reg_read(GPIO_EXT_PORTA(gpio_base));
  59. return ((value & MASK(pin)) ? 1 : 0);
  60. }