fio.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fio.h
  15. * Date: 2021-04-07 09:53:07
  16. * LastEditTime: 2022-02-18 08:24:01
  17. * Description:  This file is for general reigster io functions
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. * 1.0 zhugengyu 2021/04/07 init
  23. * 1.1 zhugengyu 2022/02/18 add Phytium Public License 1.0
  24. */
  25. #ifndef FIO_H
  26. #define FIO_H
  27. #ifdef __cplusplus
  28. extern "C"
  29. {
  30. #endif
  31. #include "ftypes.h"
  32. static _INLINE u8 FtIn8(uintptr addr)
  33. {
  34. return *(volatile u8 *)addr;
  35. }
  36. static _INLINE u16 FtIn16(uintptr addr)
  37. {
  38. return *(volatile u16 *)addr;
  39. }
  40. static _INLINE u32 FtIn32(uintptr addr)
  41. {
  42. return *(volatile u32 *)addr;
  43. }
  44. static _INLINE u64 FtIn64(uintptr addr)
  45. {
  46. return *(volatile u64 *)addr;
  47. }
  48. static _INLINE void FtOut8(uintptr addr, u8 value)
  49. {
  50. volatile u8 *local_addr = (volatile u8 *)addr;
  51. *local_addr = value;
  52. }
  53. static _INLINE void FtOut16(uintptr addr, u16 value)
  54. {
  55. volatile u16 *local_addr = (volatile u16 *)addr;
  56. *local_addr = value;
  57. }
  58. static _INLINE void FtOut32(uintptr addr, u32 value)
  59. {
  60. volatile u32 *local_addr = (volatile u32 *)addr;
  61. *local_addr = value;
  62. }
  63. static _INLINE void FtOut64(uintptr addr, u64 value)
  64. {
  65. volatile u64 *local_addr = (volatile u64 *)addr;
  66. *local_addr = value;
  67. }
  68. static _INLINE void FtSetBit32(uintptr addr, u32 value)
  69. {
  70. volatile u32 last_value;
  71. last_value = FtIn32(addr);
  72. last_value |= value;
  73. FtOut32(addr, last_value);
  74. }
  75. static _INLINE void FtClearBit32(uintptr addr, u32 value)
  76. {
  77. volatile u32 last_value;
  78. last_value = FtIn32(addr);
  79. last_value &= ~value;
  80. FtOut32(addr, last_value);
  81. }
  82. static _INLINE void FtToggleBit32(uintptr addr, u32 toggle_pos)
  83. {
  84. volatile u32 value;
  85. value = FtIn32(addr);
  86. value ^= (1 << toggle_pos);
  87. FtOut32(addr, value);
  88. }
  89. static _INLINE u16 FtEndianSwap16(u16 data)
  90. {
  91. return (u16)(((data & 0xFF00U) >> 8U) | ((data & 0x00FFU) << 8U));
  92. }
  93. #define FT_WRITE32(_reg, _val) (*(volatile uint32_t *)&_reg = _val)
  94. #define FT_READ32(_reg) (*(volatile uint32_t *)&_reg)
  95. #ifdef __cplusplus
  96. }
  97. #endif
  98. #endif // !