gpio.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "amt630h.h"
  2. #define GPIO_SWPORTA_DR 0x00
  3. #define GPIO_SWPORTA_DDR 0x04
  4. #define GPIO_SWPORTA_CTL 0x08
  5. #define GPIO_SWPORTA_INTEN 0x30
  6. #define GPIO_SWPORTA_INTMASK 0x34
  7. #define GPIO_SWPORTA_INTTYPE_LEVEL 0x38
  8. #define GPIO_SWPORTA_INT_POLARITY 0x3c
  9. #define GPIO_SWPORTA_INTSTATUS 0x40
  10. #define GPIO_SWPORTA_RAW_INTSTATUS 0x44
  11. #define GPIO_SWPORTA_DEBOUNCE 0x48
  12. #define GPIO_SWPORTA_EOI 0x4c
  13. #define GPIO_SWPORTA_EXT_PORTA 0x50
  14. #define GPIO_SWPORTA_EXT_PORTB 0x54
  15. #define GPIO_SWPORTA_EXT_PORTC 0x58
  16. #define GPIO_SWPORTA_EXT_PORTD 0x5c
  17. #define GPIO_SWPORTA_LS_SYNC 0x60
  18. #define GPIO_SWPORTA_ID_CODE 0x64
  19. #define GPIO_SWPORTA_INT_BOTHEDGE 0x68
  20. #define GPIO_SWPORTA_VER_ID_CODE 0x6C
  21. #define GPIO_SWPORTA_CONFIG_REG2 0x70
  22. #define GPIO_SWPORTA_CONFIG_REG1 0x74
  23. #define GPIO_NUM 128
  24. static uint32_t gpio_get_regbase(int gpio)
  25. {
  26. int gpiox = (gpio >> 5) & 0x3;
  27. return REGS_GPIO_BASE + 0x80 * gpiox;
  28. }
  29. static int GPIO_OFFSET(unsigned gpio)
  30. {
  31. return gpio & 0x1F;
  32. }
  33. static void *GPIO_MODREG(unsigned gpio)
  34. {
  35. return (void*)(gpio_get_regbase(gpio) + GPIO_SWPORTA_DDR);
  36. }
  37. static void *GPIO_WDATAREG(unsigned gpio)
  38. {
  39. return (void*)(gpio_get_regbase(gpio) + GPIO_SWPORTA_DR);
  40. }
  41. static void *GPIO_RDATAREG(unsigned gpio)
  42. {
  43. return (void*)(gpio_get_regbase(gpio) + GPIO_SWPORTA_EXT_PORTA);
  44. }
  45. void gpio_direction_output(unsigned gpio, int value)
  46. {
  47. writel(readl(GPIO_MODREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_MODREG(gpio));
  48. if (value)
  49. writel(readl(GPIO_WDATAREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_WDATAREG(gpio));
  50. else
  51. writel(readl(GPIO_WDATAREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_WDATAREG(gpio));
  52. }
  53. void gpio_direction_input(unsigned gpio)
  54. {
  55. writel(readl(GPIO_MODREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_MODREG(gpio));
  56. }
  57. void gpio_set_value(unsigned gpio, int value)
  58. {
  59. if (value)
  60. writel(readl(GPIO_WDATAREG(gpio)) | (1 << GPIO_OFFSET(gpio)), GPIO_WDATAREG(gpio));
  61. else
  62. writel(readl(GPIO_WDATAREG(gpio)) & ~(1 << GPIO_OFFSET(gpio)), GPIO_WDATAREG(gpio));
  63. }
  64. int gpio_get_value(unsigned gpio)
  65. {
  66. return !!(readl(GPIO_RDATAREG(gpio)) & (1 << GPIO_OFFSET(gpio)));
  67. }