gpio.c 1.5 KB

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