gpio.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_WIDTH LED_PIO_WIDTH
  12. #define ALTERA_PIO_DATA (ALTERA_PIO_BASE + 0)
  13. #define ALTERA_PIO_DIR (ALTERA_PIO_BASE + 4)
  14. static u32 pio_data_reg;
  15. static u32 pio_dir_reg;
  16. int gpio_request(unsigned gpio, const char *label)
  17. {
  18. return 0;
  19. }
  20. int gpio_free(unsigned gpio)
  21. {
  22. return 0;
  23. }
  24. int gpio_direction_input(unsigned gpio)
  25. {
  26. u32 mask = 1 << gpio;
  27. writel(pio_dir_reg &= ~mask, ALTERA_PIO_DIR);
  28. return 0;
  29. }
  30. int gpio_direction_output(unsigned gpio, int value)
  31. {
  32. u32 mask = 1 << gpio;
  33. if (value)
  34. pio_data_reg |= mask;
  35. else
  36. pio_data_reg &= ~mask;
  37. writel(pio_data_reg, ALTERA_PIO_DATA);
  38. writel(pio_dir_reg |= mask, ALTERA_PIO_DIR);
  39. return 0;
  40. }
  41. int gpio_get_value(unsigned gpio)
  42. {
  43. u32 mask = 1 << gpio;
  44. if (pio_dir_reg & mask)
  45. return (pio_data_reg & mask) ? 1 : 0;
  46. else
  47. return (readl(ALTERA_PIO_DATA) & mask) ? 1 : 0;
  48. }
  49. void gpio_set_value(unsigned gpio, int value)
  50. {
  51. u32 mask = 1 << gpio;
  52. if (value)
  53. pio_data_reg |= mask;
  54. else
  55. pio_data_reg &= ~mask;
  56. writel(pio_data_reg, ALTERA_PIO_DATA);
  57. }
  58. int gpio_is_valid(int number)
  59. {
  60. return ((unsigned)number) < ALTERA_PIO_WIDTH;
  61. }
  62. #endif