mhu_v2_x.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (c) 2020-2022, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdbool.h>
  8. #include <stdint.h>
  9. #include "mhu_v2_x.h"
  10. #define MHU_V2_X_MAX_CHANNELS 124
  11. #define MHU_V2_1_MAX_CHCOMB_INT 4
  12. #define ENABLE 0x1
  13. #define DISABLE 0x0
  14. #define CLEAR_INTR 0x1
  15. #define CH_PER_CH_COMB 0x20
  16. #define SEND_FRAME(p_mhu) ((struct mhu_v2_x_send_frame_t *)p_mhu)
  17. #define RECV_FRAME(p_mhu) ((struct mhu_v2_x_recv_frame_t *)p_mhu)
  18. #define MHU_MAJOR_REV_V2 0x1u
  19. #define MHU_MINOR_REV_2_0 0x0u
  20. #define MHU_MINOR_REV_2_1 0x1u
  21. struct mhu_v2_x_send_ch_window_t {
  22. /* Offset: 0x00 (R/ ) Channel Status */
  23. volatile uint32_t ch_st;
  24. /* Offset: 0x04 (R/ ) Reserved */
  25. volatile uint32_t reserved_0;
  26. /* Offset: 0x08 (R/ ) Reserved */
  27. volatile uint32_t reserved_1;
  28. /* Offset: 0x0C ( /W) Channel Set */
  29. volatile uint32_t ch_set;
  30. /* Offset: 0x10 (R/ ) Channel Interrupt Status (Reserved in 2.0) */
  31. volatile uint32_t ch_int_st;
  32. /* Offset: 0x14 ( /W) Channel Interrupt Clear (Reserved in 2.0) */
  33. volatile uint32_t ch_int_clr;
  34. /* Offset: 0x18 (R/W) Channel Interrupt Enable (Reserved in 2.0) */
  35. volatile uint32_t ch_int_en;
  36. /* Offset: 0x1C (R/ ) Reserved */
  37. volatile uint32_t reserved_2;
  38. };
  39. struct mhu_v2_x_send_frame_t {
  40. /* Offset: 0x000 ( / ) Sender Channel Window 0 -123 */
  41. struct mhu_v2_x_send_ch_window_t send_ch_window[MHU_V2_X_MAX_CHANNELS];
  42. /* Offset: 0xF80 (R/ ) Message Handling Unit Configuration */
  43. volatile uint32_t mhu_cfg;
  44. /* Offset: 0xF84 (R/W) Response Configuration */
  45. volatile uint32_t resp_cfg;
  46. /* Offset: 0xF88 (R/W) Access Request */
  47. volatile uint32_t access_request;
  48. /* Offset: 0xF8C (R/ ) Access Ready */
  49. volatile uint32_t access_ready;
  50. /* Offset: 0xF90 (R/ ) Interrupt Status */
  51. volatile uint32_t int_st;
  52. /* Offset: 0xF94 ( /W) Interrupt Clear */
  53. volatile uint32_t int_clr;
  54. /* Offset: 0xF98 (R/W) Interrupt Enable */
  55. volatile uint32_t int_en;
  56. /* Offset: 0xF9C (R/ ) Reserved */
  57. volatile uint32_t reserved_0;
  58. /* Offset: 0xFA0 (R/W) Channel Combined IRQ Stat (Reserved in 2.0) */
  59. volatile uint32_t ch_comb_int_st[MHU_V2_1_MAX_CHCOMB_INT];
  60. /* Offset: 0xFC4 (R/ ) Reserved */
  61. volatile uint32_t reserved_1[6];
  62. /* Offset: 0xFC8 (R/ ) Implementer Identification Register */
  63. volatile uint32_t iidr;
  64. /* Offset: 0xFCC (R/ ) Architecture Identification Register */
  65. volatile uint32_t aidr;
  66. /* Offset: 0xFD0 (R/ ) */
  67. volatile uint32_t pid_1[4];
  68. /* Offset: 0xFE0 (R/ ) */
  69. volatile uint32_t pid_0[4];
  70. /* Offset: 0xFF0 (R/ ) */
  71. volatile uint32_t cid[4];
  72. };
  73. struct mhu_v2_x_rec_ch_window_t {
  74. /* Offset: 0x00 (R/ ) Channel Status */
  75. volatile uint32_t ch_st;
  76. /* Offset: 0x04 (R/ ) Channel Status Masked */
  77. volatile uint32_t ch_st_msk;
  78. /* Offset: 0x08 ( /W) Channel Clear */
  79. volatile uint32_t ch_clr;
  80. /* Offset: 0x0C (R/ ) Reserved */
  81. volatile uint32_t reserved_0;
  82. /* Offset: 0x10 (R/ ) Channel Mask Status */
  83. volatile uint32_t ch_msk_st;
  84. /* Offset: 0x14 ( /W) Channel Mask Set */
  85. volatile uint32_t ch_msk_set;
  86. /* Offset: 0x18 ( /W) Channel Mask Clear */
  87. volatile uint32_t ch_msk_clr;
  88. /* Offset: 0x1C (R/ ) Reserved */
  89. volatile uint32_t reserved_1;
  90. };
  91. struct mhu_v2_x_recv_frame_t {
  92. /* Offset: 0x000 ( / ) Receiver Channel Window 0 -123 */
  93. struct mhu_v2_x_rec_ch_window_t rec_ch_window[MHU_V2_X_MAX_CHANNELS];
  94. /* Offset: 0xF80 (R/ ) Message Handling Unit Configuration */
  95. volatile uint32_t mhu_cfg;
  96. /* Offset: 0xF84 (R/ ) Reserved */
  97. volatile uint32_t reserved_0[3];
  98. /* Offset: 0xF90 (R/ ) Interrupt Status (Reserved in 2.0) */
  99. volatile uint32_t int_st;
  100. /* Offset: 0xF94 (R/ ) Interrupt Clear (Reserved in 2.0) */
  101. volatile uint32_t int_clr;
  102. /* Offset: 0xF98 (R/W) Interrupt Enable (Reserved in 2.0) */
  103. volatile uint32_t int_en;
  104. /* Offset: 0xF9C (R/ ) Reserved */
  105. volatile uint32_t reserved_1;
  106. /* Offset: 0xFA0 (R/ ) Channel Combined IRQ Stat (Reserved in 2.0) */
  107. volatile uint32_t ch_comb_int_st[MHU_V2_1_MAX_CHCOMB_INT];
  108. /* Offset: 0xFB0 (R/ ) Reserved */
  109. volatile uint32_t reserved_2[6];
  110. /* Offset: 0xFC8 (R/ ) Implementer Identification Register */
  111. volatile uint32_t iidr;
  112. /* Offset: 0xFCC (R/ ) Architecture Identification Register */
  113. volatile uint32_t aidr;
  114. /* Offset: 0xFD0 (R/ ) */
  115. volatile uint32_t pid_1[4];
  116. /* Offset: 0xFE0 (R/ ) */
  117. volatile uint32_t pid_0[4];
  118. /* Offset: 0xFF0 (R/ ) */
  119. volatile uint32_t cid[4];
  120. };
  121. union mhu_v2_x_frame {
  122. struct mhu_v2_x_send_frame_t send_frame;
  123. struct mhu_v2_x_recv_frame_t recv_frame;
  124. };
  125. enum mhu_v2_x_error_t mhu_v2_x_driver_init(struct mhu_v2_x_dev_t *dev,
  126. enum mhu_v2_x_supported_revisions rev)
  127. {
  128. uint32_t AIDR = 0;
  129. union mhu_v2_x_frame *p_mhu;
  130. assert(dev != NULL);
  131. p_mhu = (union mhu_v2_x_frame *)dev->base;
  132. if (dev->is_initialized) {
  133. return MHU_V_2_X_ERR_ALREADY_INIT;
  134. }
  135. if (rev == MHU_REV_READ_FROM_HW) {
  136. /* Read revision from HW */
  137. if (dev->frame == MHU_V2_X_RECEIVER_FRAME) {
  138. AIDR = p_mhu->recv_frame.aidr;
  139. } else {
  140. AIDR = p_mhu->send_frame.aidr;
  141. }
  142. /* Get bits 7:4 to read major revision */
  143. if (((AIDR >> 4) & 0b1111) != MHU_MAJOR_REV_V2) {
  144. /* Unsupported MHU version */
  145. return MHU_V_2_X_ERR_UNSUPPORTED_VERSION;
  146. } /* No need to save major version, driver only supports MHUv2 */
  147. /* Get bits 3:0 to read minor revision */
  148. dev->subversion = AIDR & 0b1111;
  149. if (dev->subversion != MHU_MINOR_REV_2_0 &&
  150. dev->subversion != MHU_MINOR_REV_2_1) {
  151. /* Unsupported subversion */
  152. return MHU_V_2_X_ERR_UNSUPPORTED_VERSION;
  153. }
  154. } else {
  155. /* Revisions were provided by caller */
  156. if (rev == MHU_REV_2_0) {
  157. dev->subversion = MHU_MINOR_REV_2_0;
  158. } else if (rev == MHU_REV_2_1) {
  159. dev->subversion = MHU_MINOR_REV_2_1;
  160. } else {
  161. /* Unsupported subversion */
  162. return MHU_V_2_X_ERR_UNSUPPORTED_VERSION;
  163. } /* No need to save major version, driver only supports MHUv2 */
  164. }
  165. dev->is_initialized = true;
  166. return MHU_V_2_X_ERR_NONE;
  167. }
  168. uint32_t mhu_v2_x_get_num_channel_implemented(const struct mhu_v2_x_dev_t *dev)
  169. {
  170. union mhu_v2_x_frame *p_mhu;
  171. assert(dev != NULL);
  172. p_mhu = (union mhu_v2_x_frame *)dev->base;
  173. if (!(dev->is_initialized)) {
  174. return MHU_V_2_X_ERR_NOT_INIT;
  175. }
  176. if (dev->frame == MHU_V2_X_SENDER_FRAME) {
  177. return (SEND_FRAME(p_mhu))->mhu_cfg;
  178. } else {
  179. assert(dev->frame == MHU_V2_X_RECEIVER_FRAME);
  180. return (RECV_FRAME(p_mhu))->mhu_cfg;
  181. }
  182. }
  183. enum mhu_v2_x_error_t mhu_v2_x_channel_send(const struct mhu_v2_x_dev_t *dev,
  184. uint32_t channel, uint32_t val)
  185. {
  186. union mhu_v2_x_frame *p_mhu;
  187. assert(dev != NULL);
  188. p_mhu = (union mhu_v2_x_frame *)dev->base;
  189. if (!(dev->is_initialized)) {
  190. return MHU_V_2_X_ERR_NOT_INIT;
  191. }
  192. if (dev->frame == MHU_V2_X_SENDER_FRAME) {
  193. (SEND_FRAME(p_mhu))->send_ch_window[channel].ch_set = val;
  194. return MHU_V_2_X_ERR_NONE;
  195. } else {
  196. return MHU_V_2_X_ERR_INVALID_ARG;
  197. }
  198. }
  199. enum mhu_v2_x_error_t mhu_v2_x_channel_poll(const struct mhu_v2_x_dev_t *dev,
  200. uint32_t channel, uint32_t *value)
  201. {
  202. union mhu_v2_x_frame *p_mhu;
  203. assert(dev != NULL);
  204. p_mhu = (union mhu_v2_x_frame *)dev->base;
  205. if (!(dev->is_initialized)) {
  206. return MHU_V_2_X_ERR_NOT_INIT;
  207. }
  208. if (dev->frame == MHU_V2_X_SENDER_FRAME) {
  209. *value = (SEND_FRAME(p_mhu))->send_ch_window[channel].ch_st;
  210. return MHU_V_2_X_ERR_NONE;
  211. } else {
  212. return MHU_V_2_X_ERR_INVALID_ARG;
  213. }
  214. }
  215. enum mhu_v2_x_error_t mhu_v2_x_channel_clear(const struct mhu_v2_x_dev_t *dev,
  216. uint32_t channel)
  217. {
  218. union mhu_v2_x_frame *p_mhu;
  219. assert(dev != NULL);
  220. p_mhu = (union mhu_v2_x_frame *)dev->base;
  221. if (!(dev->is_initialized)) {
  222. return MHU_V_2_X_ERR_NOT_INIT;
  223. }
  224. if (dev->frame == MHU_V2_X_RECEIVER_FRAME) {
  225. (RECV_FRAME(p_mhu))->rec_ch_window[channel].ch_clr = UINT32_MAX;
  226. return MHU_V_2_X_ERR_NONE;
  227. } else {
  228. return MHU_V_2_X_ERR_INVALID_ARG;
  229. }
  230. }
  231. enum mhu_v2_x_error_t mhu_v2_x_channel_receive(
  232. const struct mhu_v2_x_dev_t *dev, uint32_t channel, uint32_t *value)
  233. {
  234. union mhu_v2_x_frame *p_mhu;
  235. assert(dev != NULL);
  236. p_mhu = (union mhu_v2_x_frame *)dev->base;
  237. if (!(dev->is_initialized)) {
  238. return MHU_V_2_X_ERR_NOT_INIT;
  239. }
  240. if (dev->frame == MHU_V2_X_RECEIVER_FRAME) {
  241. *value = (RECV_FRAME(p_mhu))->rec_ch_window[channel].ch_st;
  242. return MHU_V_2_X_ERR_NONE;
  243. } else {
  244. return MHU_V_2_X_ERR_INVALID_ARG;
  245. }
  246. }
  247. enum mhu_v2_x_error_t mhu_v2_x_channel_mask_set(
  248. const struct mhu_v2_x_dev_t *dev, uint32_t channel, uint32_t mask)
  249. {
  250. union mhu_v2_x_frame *p_mhu;
  251. assert(dev != NULL);
  252. p_mhu = (union mhu_v2_x_frame *)dev->base;
  253. if (!(dev->is_initialized)) {
  254. return MHU_V_2_X_ERR_NOT_INIT;
  255. }
  256. if (dev->frame == MHU_V2_X_RECEIVER_FRAME) {
  257. (RECV_FRAME(p_mhu))->rec_ch_window[channel].ch_msk_set = mask;
  258. return MHU_V_2_X_ERR_NONE;
  259. } else {
  260. return MHU_V_2_X_ERR_INVALID_ARG;
  261. }
  262. }
  263. enum mhu_v2_x_error_t mhu_v2_x_channel_mask_clear(
  264. const struct mhu_v2_x_dev_t *dev, uint32_t channel, uint32_t mask)
  265. {
  266. union mhu_v2_x_frame *p_mhu;
  267. assert(dev != NULL);
  268. p_mhu = (union mhu_v2_x_frame *)dev->base;
  269. if (!(dev->is_initialized)) {
  270. return MHU_V_2_X_ERR_NOT_INIT;
  271. }
  272. if (dev->frame == MHU_V2_X_RECEIVER_FRAME) {
  273. (RECV_FRAME(p_mhu))->rec_ch_window[channel].ch_msk_clr = mask;
  274. return MHU_V_2_X_ERR_NONE;
  275. } else {
  276. return MHU_V_2_X_ERR_INVALID_ARG;
  277. }
  278. }
  279. enum mhu_v2_x_error_t mhu_v2_x_initiate_transfer(
  280. const struct mhu_v2_x_dev_t *dev)
  281. {
  282. union mhu_v2_x_frame *p_mhu;
  283. assert(dev != NULL);
  284. p_mhu = (union mhu_v2_x_frame *)dev->base;
  285. if (!(dev->is_initialized)) {
  286. return MHU_V_2_X_ERR_NOT_INIT;
  287. }
  288. if (dev->frame != MHU_V2_X_SENDER_FRAME) {
  289. return MHU_V_2_X_ERR_INVALID_ARG;
  290. }
  291. (SEND_FRAME(p_mhu))->access_request = ENABLE;
  292. while (!((SEND_FRAME(p_mhu))->access_ready)) {
  293. /* Wait in a loop for access ready signal to be high */
  294. ;
  295. }
  296. return MHU_V_2_X_ERR_NONE;
  297. }
  298. enum mhu_v2_x_error_t mhu_v2_x_close_transfer(const struct mhu_v2_x_dev_t *dev)
  299. {
  300. union mhu_v2_x_frame *p_mhu;
  301. assert(dev != NULL);
  302. p_mhu = (union mhu_v2_x_frame *)dev->base;
  303. if (!(dev->is_initialized)) {
  304. return MHU_V_2_X_ERR_NOT_INIT;
  305. }
  306. if (dev->frame != MHU_V2_X_SENDER_FRAME) {
  307. return MHU_V_2_X_ERR_INVALID_ARG;
  308. }
  309. (SEND_FRAME(p_mhu))->access_request = DISABLE;
  310. return MHU_V_2_X_ERR_NONE;
  311. }