rockchip.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-3-08 GuEe-GUI the first version
  9. */
  10. #ifndef __ROCKCHIP_H__
  11. #define __ROCKCHIP_H__
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. #include <dt-bindings/size.h>
  16. #define rk_clrsetreg(addr, clr, set) HWREG32(addr) = (((clr) | (set)) << 16 | (set))
  17. #define rk_clrreg(addr, clr) HWREG32(addr) = ((clr) << 16)
  18. #define rk_setreg(addr, set) HWREG32(addr) = ((set) << 16 | (set))
  19. #define HIWORD_UPDATE(val, mask, shift) ((val) << (shift) | (mask) << ((shift) + 16))
  20. #define readx_poll_timeout(OP, ADDR, VAL, COND, DELAY_US, TIMEOUT_US) \
  21. ({ \
  22. rt_uint64_t timeout_us = (TIMEOUT_US); \
  23. rt_int64_t left_ns = timeout_us * 1000L; \
  24. rt_ubase_t delay_us = (DELAY_US); \
  25. rt_uint64_t delay_ns = delay_us * 1000L; \
  26. for (;;) \
  27. { \
  28. (VAL) = OP(ADDR); \
  29. if (COND) \
  30. { \
  31. break; \
  32. } \
  33. if (timeout_us && left_ns < 0) \
  34. { \
  35. (VAL) = OP(ADDR); \
  36. break; \
  37. } \
  38. if (delay_us) \
  39. { \
  40. rt_hw_us_delay(delay_us); \
  41. if (timeout_us) \
  42. { \
  43. left_ns -= delay_ns; \
  44. } \
  45. } \
  46. rt_hw_cpu_relax(); \
  47. if (timeout_us) \
  48. { \
  49. --left_ns; \
  50. } \
  51. } \
  52. (COND) ? RT_EOK : -RT_ETIMEOUT; \
  53. })
  54. #define readl_poll_timeout(ADDR, VAL, COND, DELAY_US, TIMEOUT_US) \
  55. readx_poll_timeout(HWREG32, ADDR, VAL, COND, DELAY_US, TIMEOUT_US)
  56. #define is_aligned(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
  57. rt_inline int fls(int x)
  58. {
  59. return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
  60. }
  61. rt_inline int fls_long(unsigned long x)
  62. {
  63. return x ? sizeof(x) * 8 - __builtin_clzl(x) : 0;
  64. }
  65. rt_inline int __roundup_pow_of_two(rt_uint32_t x)
  66. {
  67. return 1UL << fls(x - 1);
  68. }
  69. rt_inline int __rounddown_pow_of_two(rt_uint32_t x)
  70. {
  71. return (1UL << fls(x - 1)) / 2;
  72. }
  73. #endif /* __ROCKCHIP_H__ */