gpio.c 1.2 KB

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