gpio.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // gpio.c
  3. //
  4. // GPIO
  5. //
  6. // Description:
  7. //
  8. // Revision History:
  9. // DATE VERSION AUTHOR NOTE
  10. // ---------- ------- ----------- ------------------------------
  11. // 2019/12/27 0.1 Zavier Initial
  12. //
  13. #include "gpio.h"
  14. #include <sys.h>
  15. //#include <ezGPIO_fullMux_ctrl_macro.h>
  16. #include <clkgen_ctrl_macro.h>
  17. #include <rstgen_ctrl_macro.h>
  18. #define GPIO_EN (0x0)
  19. #define GPIO_IS_LOW (0x10) //select level or edge trigge
  20. #define GPIO_IS_HIGH (0x14)
  21. #define GPIO_IBE_LOW (0x18) //interrupt both edge trigger 1:both, 0:disable both
  22. #define GPIO_IBE_HIGH (0x1c)
  23. #define GPIO_IEV_LOW (0x20) //trigger condition 1:rising, 0 falling
  24. #define GPIO_IEV_HIGH (0x24)
  25. #define GPIO_IE_LOW (0x28) //interrupt enable
  26. #define GPIO_IE_HIGH (0x2c)
  27. #define GPIO_IC_LOW (0x30) //interrupt clear
  28. #define GPIO_IC_HIGH (0x34)
  29. //read only
  30. #define GPIO_RIS_LOW (0x38) //raw interrupt
  31. #define GPIO_RIS_HIGH (0x3c)
  32. #define GPIO_MIS_LOW (0x40) //masked interrupt
  33. #define GPIO_MIS_HIGH (0x44)
  34. #define GPIO_DIN_LOW (0x48) //data in
  35. #define GPIO_DIN_HIGH (0x4c)
  36. static inline void gpio_write_reg(unsigned long base, unsigned long addr, unsigned int val)
  37. {
  38. writel(val, (volatile void *)(base + addr));
  39. }
  40. static inline unsigned int gpio_read_reg(unsigned long base, unsigned long addr)
  41. {
  42. return readl((volatile void *)(base + addr));
  43. }
  44. void gpio_enable(int enable)
  45. {
  46. if(enable == 0)
  47. gpio_write_reg(EZGPIO_FULLMUX_BASE_ADDR, GPIO_EN, 0);
  48. else
  49. gpio_write_reg(EZGPIO_FULLMUX_BASE_ADDR, GPIO_EN, 1);
  50. }
  51. int gpio_get_val(int gpio_num)
  52. {
  53. int value = 0;
  54. if(gpio_num < 32)
  55. {
  56. value = gpio_read_reg(EZGPIO_FULLMUX_BASE_ADDR, GPIO_DIN_LOW);
  57. return (value >> gpio_num) & 0x1;
  58. }
  59. else
  60. {
  61. value = gpio_read_reg(EZGPIO_FULLMUX_BASE_ADDR, GPIO_DIN_HIGH);
  62. return (value >> (gpio_num - 32)) & 0x1;
  63. }
  64. }
  65. int get_boot_mode()
  66. {
  67. int boot_mode = 0;
  68. boot_mode = gpio_get_val(BOOT_GPIO_60);
  69. boot_mode |= (gpio_get_val(BOOT_GPIO_61) << 1);
  70. boot_mode |= (gpio_get_val(BOOT_GPIO_62) << 2);
  71. return boot_mode;
  72. }
  73. void gpio_init(void)
  74. {
  75. _ENABLE_CLOCK_clk_gpio_apb_;
  76. _ASSERT_RESET_rstgen_rstn_gpio_apb_;
  77. _CLEAR_RESET_rstgen_rstn_gpio_apb_;
  78. gpio_enable(1);
  79. }