sunxi_hal_common.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * ===========================================================================================
  3. *
  4. * Filename: sunxi_hal_common.h
  5. *
  6. * Description: Add sunxi hal common header file, common defitions used by all hal.
  7. *
  8. * Version: Melis3.0
  9. * Create: 2019-11-14 11:38:34
  10. * Revision: none
  11. * Compiler: GCC:version 7.2.1 20170904 (release),ARM/embedded-7-branch revision 255204
  12. *
  13. * Author: caozilong@allwinnertech.com
  14. * Organization: BU1-PSW
  15. * Last Modified: 2020-10-20 17:48:41
  16. *
  17. * ===========================================================================================
  18. */
  19. #ifndef SUNXI_HAL_COMMON_H
  20. #define SUNXI_HAL_COMMON_H
  21. #include <barrier.h>
  22. #ifdef __cplusplus
  23. extern "C"
  24. {
  25. #endif
  26. #include <stddef.h>
  27. #include <stdint.h>
  28. #include <stdbool.h>
  29. #include <stdio.h>
  30. #include <kconfig.h>
  31. #undef min
  32. #undef max
  33. #define min(a, b) ((a) < (b) ? (a) : (b))
  34. #define max(a,b) ((a) < (b) ? (b) : (a))
  35. /* return value defines */
  36. #define OK (0)
  37. #define FAIL (-1)
  38. #define CACHELINE_LEN (64)
  39. typedef uint64_t u64;
  40. typedef uint32_t u32;
  41. typedef uint16_t u16;
  42. typedef uint8_t u8;
  43. typedef int64_t s64;
  44. typedef int32_t s32;
  45. typedef int16_t s16;
  46. typedef int8_t s8;
  47. #define HAL_ARG_UNUSED(NAME) (void)(NAME)
  48. /* general function pointer defines */
  49. typedef s32(*__pCBK_t) (void *p_arg); /* call-back */
  50. typedef s32(*__pISR_hdle_t) (int dummy, void *p_arg); /* ISR */
  51. typedef s32(*__pNotifier_t) (u32 message, u32 aux); /* notifer call-back */
  52. typedef s32(*__pCPUExceptionHandler) (void); /* cpu exception handler pointer */
  53. typedef s32(*long_jump_fn) (void *arg);
  54. typedef s32(*mem_long_jump_fn) (u32 arg);
  55. // Exception Dealt With.
  56. #define BUG() do { \
  57. printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
  58. while(1); \
  59. } while (0)
  60. #ifndef BUG_ON
  61. #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
  62. #define WARN_ON(condition) ({ \
  63. int __ret_warn_on = !!(condition); \
  64. unlikely(__ret_warn_on); \
  65. })
  66. #endif
  67. #ifndef WARN
  68. #define WARN(condition, format...) ({ \
  69. int __ret_warn_on = !!(condition); \
  70. if(__ret_warn_on) \
  71. printf(format); \
  72. unlikely(__ret_warn_on); \
  73. })
  74. #endif
  75. #ifdef CONFIG_KERNEL_FREERTOS
  76. #define in_interrupt(...) uGetInterruptNest()
  77. #else
  78. #define in_interrupt(...) rt_interrupt_get_nest()
  79. #endif
  80. #define in_nmi(...) (0)
  81. #define hal_assert(ex) \
  82. if (!(ex)) \
  83. { \
  84. printf("%s line %d, fatal error.\n", __func__, __LINE__); \
  85. while(1); \
  86. }
  87. // common register access operation.
  88. #define hal_readb(reg) (*(volatile uint8_t *)(long)(reg))
  89. #define hal_readw(reg) (*(volatile uint16_t *)(reg))
  90. #define hal_readl(reg) (*(volatile uint32_t *)(reg))
  91. #define hal_writeb(value,reg) (*(volatile uint8_t *)(long)(reg) = (value))
  92. #define hal_writew(value,reg) (*(volatile uint16_t *)(reg) = (value))
  93. #define hal_writel(value,reg) (*(volatile uint32_t *)(reg) = (value))
  94. // version combine.
  95. #define SUNXI_HAL_VERSION_MAJOR_MINOR(major, minor) (((major) << 8) | (minor))
  96. typedef struct sunxi_hal_version
  97. {
  98. // API version NO.
  99. uint16_t api;
  100. // Driver version NO.
  101. uint16_t drv;
  102. } sunxi_hal_version_t;
  103. // General return code of hal driver.
  104. #define SUNXI_HAL_OK 0UL
  105. // Unspecified error.
  106. #define SUNXI_HAL_ERROR -1UL
  107. // Hal is busy.
  108. #define SUNXI_HAL_ERROR_BUSY -2UL
  109. // Timout occured.
  110. #define SUNXI_HAL_ERROR_TIMEOUT -3UL
  111. // Operaion not supported.
  112. #define SUNXI_HAL_ERROR_UNSUPOT -4UL
  113. // Parameter error.
  114. #define SUNXI_HAL_ERROR_PARAERR -5UL
  115. // Start of driver specific errors.
  116. #define SUNXI_HAL_ERROR_DRVSPECIFIC -6UL
  117. // brief General power states
  118. typedef enum sunxi_hal_power_state
  119. {
  120. ///< Power off: no operation possible
  121. SUSNXI_HAL_POWER_OFF,
  122. ///< Low Power mode: retain state, detect and signal wake-up events
  123. SUSNXI_HAL_POWER_LOW,
  124. ///< Power on: full operation at maximum performance
  125. SUSNXI_HAL_POWER_FULL
  126. } sunxi_hal_power_state_e;
  127. typedef int32_t (*poll_wakeup_func)(int32_t dev_id, short key);
  128. typedef struct _sunxi_hal_poll_ops
  129. {
  130. int32_t (* check_poll_state) (int32_t dev, short key);
  131. int32_t (* hal_poll_wakeup) (int32_t dev, short key);
  132. int32_t (* register_poll_wakeup) (poll_wakeup_func poll_wakeup);
  133. } sunxi_hal_poll_ops;
  134. /* bitops */
  135. extern int fls(int x);
  136. void dma_free_coherent(void *addr);
  137. void *dma_alloc_coherent(size_t size);
  138. #ifdef __cplusplus
  139. }
  140. #endif
  141. #endif /*SUNXI_HAL_COMMON_H*/