gpio.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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_request(unsigned gpio, const char *label)
  16. {
  17. return 0;
  18. }
  19. int gpio_direction_input(unsigned gpio)
  20. {
  21. u32 mask = 1 << gpio;
  22. writel(pio_dir_reg &= ~mask, ALTERA_PIO_DIR);
  23. return 0;
  24. }
  25. int gpio_direction_output(unsigned gpio, int value)
  26. {
  27. u32 mask = 1 << gpio;
  28. if (value)
  29. pio_data_reg |= mask;
  30. else
  31. pio_data_reg &= ~mask;
  32. writel(pio_data_reg, ALTERA_PIO_DATA);
  33. writel(pio_dir_reg |= mask, ALTERA_PIO_DIR);
  34. return 0;
  35. }
  36. int gpio_get_value(unsigned gpio)
  37. {
  38. u32 mask = 1 << gpio;
  39. if (pio_dir_reg & mask)
  40. return (pio_data_reg & mask) ? 1 : 0;
  41. else
  42. return (readl(ALTERA_PIO_DATA) & mask) ? 1 : 0;
  43. }
  44. void gpio_set_value(unsigned gpio, int value)
  45. {
  46. u32 mask = 1 << gpio;
  47. if (value)
  48. pio_data_reg |= mask;
  49. else
  50. pio_data_reg &= ~mask;
  51. writel(pio_data_reg, ALTERA_PIO_DATA);
  52. }
  53. #endif