gpio.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <common.h>
  2. #include <asm/io.h>
  3. #define GPIO_MOD 0x00
  4. #define GPIO_RDATA 0x04
  5. #define GPIO_INTEN 0x08
  6. #define GPIO_INTLEVEL 0x0C
  7. #define GPIO_INTGEN 0x10
  8. #define GPIO_DEBOUNCE_EN 0xA0
  9. #define GPIO_DEBOUNCE_CNT0 0x80
  10. #define GPIO_BANK_NUM 32
  11. static unsigned long gpio_bases[] = {
  12. CONFIG_GPIO_BASEADDR,
  13. CONFIG_GPIO_BASEADDR + 0x20,
  14. CONFIG_GPIO_BASEADDR + 0x40,
  15. CONFIG_GPIO_BASEADDR + 0x60,
  16. };
  17. static inline int GPIO_BANK(unsigned gpio)
  18. {
  19. return gpio >> 5;
  20. }
  21. static inline int GPIO_OFFSET(unsigned gpio)
  22. {
  23. return gpio & 0x1F;
  24. }
  25. static inline void *GPIO_MODREG(unsigned gpio)
  26. {
  27. return (void*)(gpio_bases[GPIO_BANK(gpio)] + GPIO_MOD);
  28. }
  29. static inline void *GPIO_RDATAREG(unsigned gpio)
  30. {
  31. return (void*)(gpio_bases[GPIO_BANK(gpio)] + GPIO_RDATA);
  32. }
  33. int gpio_request(unsigned gpio, const char *label)
  34. {
  35. return 0;
  36. }
  37. int gpio_free(unsigned gpio)
  38. {
  39. return 0;
  40. }
  41. int gpio_direction_input(unsigned gpio)
  42. {
  43. writel(readl(GPIO_MODREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_MODREG(gpio));
  44. return 0;
  45. }
  46. int gpio_direction_output(unsigned gpio, int value)
  47. {
  48. writel(readl(GPIO_MODREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_MODREG(gpio));
  49. if (value)
  50. writel(readl(GPIO_RDATAREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_RDATAREG(gpio));
  51. else
  52. writel(readl(GPIO_RDATAREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_RDATAREG(gpio));
  53. return 0;
  54. }
  55. int gpio_get_value(unsigned gpio)
  56. {
  57. return !!(readl(GPIO_RDATAREG(gpio)) & (1 << GPIO_OFFSET(gpio)));
  58. }
  59. int gpio_set_value(unsigned gpio, int value)
  60. {
  61. if (value)
  62. writel(readl(GPIO_RDATAREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_RDATAREG(gpio));
  63. else
  64. writel(readl(GPIO_RDATAREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_RDATAREG(gpio));
  65. return 0;
  66. }