gpio.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * board gpio driver
  3. *
  4. * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
  5. * Licensed under the GPL-2 or later.
  6. */
  7. #include <common.h>
  8. #include <asm/io.h>
  9. #ifndef CONFIG_SYS_GPIO_BASE
  10. #define ALTERA_PIO_BASE LED_PIO_BASE
  11. #define ALTERA_PIO_DATA (ALTERA_PIO_BASE + 0)
  12. #define ALTERA_PIO_DIR (ALTERA_PIO_BASE + 4)
  13. static u32 pio_data_reg;
  14. static u32 pio_dir_reg;
  15. int gpio_direction_input(unsigned gpio)
  16. {
  17. u32 mask = 1 << gpio;
  18. writel(pio_dir_reg &= ~mask, ALTERA_PIO_DIR);
  19. return 0;
  20. }
  21. int gpio_direction_output(unsigned gpio, int value)
  22. {
  23. u32 mask = 1 << gpio;
  24. if (value)
  25. pio_data_reg |= mask;
  26. else
  27. pio_data_reg &= ~mask;
  28. writel(pio_data_reg, ALTERA_PIO_DATA);
  29. writel(pio_dir_reg |= mask, ALTERA_PIO_DIR);
  30. return 0;
  31. }
  32. int gpio_get_value(unsigned gpio)
  33. {
  34. u32 mask = 1 << gpio;
  35. if (pio_dir_reg & mask)
  36. return (pio_data_reg & mask) ? 1 : 0;
  37. else
  38. return (readl(ALTERA_PIO_DATA) & mask) ? 1 : 0;
  39. }
  40. void gpio_set_value(unsigned gpio, int value)
  41. {
  42. u32 mask = 1 << gpio;
  43. if (value)
  44. pio_data_reg |= mask;
  45. else
  46. pio_data_reg &= ~mask;
  47. writel(pio_data_reg, ALTERA_PIO_DATA);
  48. }
  49. #endif