mailbox_s10.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017-2018 Intel Corporation <www.intel.com>
  4. *
  5. */
  6. #include <common.h>
  7. #include <wait_bit.h>
  8. #include <asm/io.h>
  9. #include <asm/arch/mailbox_s10.h>
  10. #include <asm/arch/system_manager.h>
  11. #include <asm/secure.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. #define MBOX_READL(reg) \
  14. readl(SOCFPGA_MAILBOX_ADDRESS + (reg))
  15. #define MBOX_WRITEL(data, reg) \
  16. writel(data, SOCFPGA_MAILBOX_ADDRESS + (reg))
  17. #define MBOX_READ_RESP_BUF(rout) \
  18. MBOX_READL(MBOX_RESP_BUF + ((rout) * sizeof(u32)))
  19. #define MBOX_WRITE_CMD_BUF(data, cin) \
  20. MBOX_WRITEL(data, MBOX_CMD_BUF + ((cin) * sizeof(u32)))
  21. static __always_inline int mbox_polling_resp(u32 rout)
  22. {
  23. u32 rin;
  24. unsigned long i = ~0;
  25. while (i) {
  26. rin = MBOX_READL(MBOX_RIN);
  27. if (rout != rin)
  28. return 0;
  29. i--;
  30. }
  31. return -ETIMEDOUT;
  32. }
  33. /* Check for available slot and write to circular buffer.
  34. * It also update command valid offset (cin) register.
  35. */
  36. static __always_inline int mbox_fill_cmd_circular_buff(u32 header, u32 len,
  37. u32 *arg)
  38. {
  39. u32 cin;
  40. u32 cout;
  41. u32 i;
  42. cin = MBOX_READL(MBOX_CIN) % MBOX_CMD_BUFFER_SIZE;
  43. cout = MBOX_READL(MBOX_COUT) % MBOX_CMD_BUFFER_SIZE;
  44. /* if command buffer is full or not enough free space
  45. * to fit the data
  46. */
  47. if (((cin + 1) % MBOX_CMD_BUFFER_SIZE) == cout ||
  48. ((MBOX_CMD_BUFFER_SIZE - cin + cout - 1) %
  49. MBOX_CMD_BUFFER_SIZE) < len)
  50. return -ENOMEM;
  51. /* write header to circular buffer */
  52. MBOX_WRITE_CMD_BUF(header, cin++);
  53. /* wrapping around when it reach the buffer size */
  54. cin %= MBOX_CMD_BUFFER_SIZE;
  55. /* write arguments */
  56. for (i = 0; i < len; i++) {
  57. MBOX_WRITE_CMD_BUF(arg[i], cin++);
  58. /* wrapping around when it reach the buffer size */
  59. cin %= MBOX_CMD_BUFFER_SIZE;
  60. }
  61. /* write command valid offset */
  62. MBOX_WRITEL(cin, MBOX_CIN);
  63. return 0;
  64. }
  65. /* Check the command and fill it into circular buffer */
  66. static __always_inline int mbox_prepare_cmd_only(u8 id, u32 cmd,
  67. u8 is_indirect, u32 len,
  68. u32 *arg)
  69. {
  70. u32 header;
  71. int ret;
  72. /* Total length is command + argument length */
  73. if ((len + 1) > MBOX_CMD_BUFFER_SIZE)
  74. return -EINVAL;
  75. if (cmd > MBOX_MAX_CMD_INDEX)
  76. return -EINVAL;
  77. header = MBOX_CMD_HEADER(MBOX_CLIENT_ID_UBOOT, id, len,
  78. (is_indirect) ? 1 : 0, cmd);
  79. ret = mbox_fill_cmd_circular_buff(header, len, arg);
  80. return ret;
  81. }
  82. /* Send command only without waiting for responses from SDM */
  83. static __always_inline int mbox_send_cmd_only_common(u8 id, u32 cmd,
  84. u8 is_indirect, u32 len,
  85. u32 *arg)
  86. {
  87. int ret = mbox_prepare_cmd_only(id, cmd, is_indirect, len, arg);
  88. /* write doorbell */
  89. MBOX_WRITEL(1, MBOX_DOORBELL_TO_SDM);
  90. return ret;
  91. }
  92. /* Return number of responses received in buffer */
  93. static __always_inline int __mbox_rcv_resp(u32 *resp_buf, u32 resp_buf_max_len)
  94. {
  95. u32 rin;
  96. u32 rout;
  97. u32 resp_len = 0;
  98. /* clear doorbell from SDM if it was SET */
  99. if (MBOX_READL(MBOX_DOORBELL_FROM_SDM) & 1)
  100. MBOX_WRITEL(0, MBOX_DOORBELL_FROM_SDM);
  101. /* read current response offset */
  102. rout = MBOX_READL(MBOX_ROUT);
  103. /* read response valid offset */
  104. rin = MBOX_READL(MBOX_RIN);
  105. while (rin != rout && (resp_len < resp_buf_max_len)) {
  106. /* Response received */
  107. if (resp_buf)
  108. resp_buf[resp_len++] = MBOX_READ_RESP_BUF(rout);
  109. rout++;
  110. /* wrapping around when it reach the buffer size */
  111. rout %= MBOX_RESP_BUFFER_SIZE;
  112. /* update next ROUT */
  113. MBOX_WRITEL(rout, MBOX_ROUT);
  114. }
  115. return resp_len;
  116. }
  117. /* Support one command and up to 31 words argument length only */
  118. static __always_inline int mbox_send_cmd_common(u8 id, u32 cmd, u8 is_indirect,
  119. u32 len, u32 *arg, u8 urgent,
  120. u32 *resp_buf_len,
  121. u32 *resp_buf)
  122. {
  123. u32 rin;
  124. u32 resp;
  125. u32 rout;
  126. u32 status;
  127. u32 resp_len;
  128. u32 buf_len;
  129. int ret;
  130. if (urgent) {
  131. /* Read status because it is toggled */
  132. status = MBOX_READL(MBOX_STATUS) & MBOX_STATUS_UA_MSK;
  133. /* Write urgent command to urgent register */
  134. MBOX_WRITEL(cmd, MBOX_URG);
  135. } else {
  136. ret = mbox_prepare_cmd_only(id, cmd, is_indirect, len, arg);
  137. if (ret)
  138. return ret;
  139. }
  140. /* write doorbell */
  141. MBOX_WRITEL(1, MBOX_DOORBELL_TO_SDM);
  142. while (1) {
  143. ret = ~0;
  144. /* Wait for doorbell from SDM */
  145. while (!MBOX_READL(MBOX_DOORBELL_FROM_SDM) && ret--)
  146. ;
  147. if (!ret)
  148. return -ETIMEDOUT;
  149. /* clear interrupt */
  150. MBOX_WRITEL(0, MBOX_DOORBELL_FROM_SDM);
  151. if (urgent) {
  152. u32 new_status = MBOX_READL(MBOX_STATUS);
  153. /* Urgent ACK is toggled */
  154. if ((new_status & MBOX_STATUS_UA_MSK) ^ status)
  155. return 0;
  156. return -ECOMM;
  157. }
  158. /* read current response offset */
  159. rout = MBOX_READL(MBOX_ROUT);
  160. /* read response valid offset */
  161. rin = MBOX_READL(MBOX_RIN);
  162. if (rout != rin) {
  163. /* Response received */
  164. resp = MBOX_READ_RESP_BUF(rout);
  165. rout++;
  166. /* wrapping around when it reach the buffer size */
  167. rout %= MBOX_RESP_BUFFER_SIZE;
  168. /* update next ROUT */
  169. MBOX_WRITEL(rout, MBOX_ROUT);
  170. /* check client ID and ID */
  171. if ((MBOX_RESP_CLIENT_GET(resp) ==
  172. MBOX_CLIENT_ID_UBOOT) &&
  173. (MBOX_RESP_ID_GET(resp) == id)) {
  174. ret = MBOX_RESP_ERR_GET(resp);
  175. if (ret)
  176. return ret;
  177. if (resp_buf_len) {
  178. buf_len = *resp_buf_len;
  179. *resp_buf_len = 0;
  180. } else {
  181. buf_len = 0;
  182. }
  183. resp_len = MBOX_RESP_LEN_GET(resp);
  184. while (resp_len) {
  185. ret = mbox_polling_resp(rout);
  186. if (ret)
  187. return ret;
  188. /* we need to process response buffer
  189. * even caller doesn't need it
  190. */
  191. resp = MBOX_READ_RESP_BUF(rout);
  192. rout++;
  193. resp_len--;
  194. rout %= MBOX_RESP_BUFFER_SIZE;
  195. MBOX_WRITEL(rout, MBOX_ROUT);
  196. if (buf_len) {
  197. /* copy response to buffer */
  198. resp_buf[*resp_buf_len] = resp;
  199. (*resp_buf_len)++;
  200. buf_len--;
  201. }
  202. }
  203. return ret;
  204. }
  205. }
  206. };
  207. return -EIO;
  208. }
  209. int mbox_init(void)
  210. {
  211. int ret;
  212. /* enable mailbox interrupts */
  213. MBOX_WRITEL(MBOX_ALL_INTRS, MBOX_FLAGS);
  214. /* Ensure urgent request is cleared */
  215. MBOX_WRITEL(0, MBOX_URG);
  216. /* Ensure the Doorbell Interrupt is cleared */
  217. MBOX_WRITEL(0, MBOX_DOORBELL_FROM_SDM);
  218. ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_RESTART, MBOX_CMD_DIRECT, 0,
  219. NULL, 1, 0, NULL);
  220. if (ret)
  221. return ret;
  222. /* Renable mailbox interrupts after MBOX_RESTART */
  223. MBOX_WRITEL(MBOX_ALL_INTRS, MBOX_FLAGS);
  224. return 0;
  225. }
  226. #ifdef CONFIG_CADENCE_QSPI
  227. int mbox_qspi_close(void)
  228. {
  229. return mbox_send_cmd(MBOX_ID_UBOOT, MBOX_QSPI_CLOSE, MBOX_CMD_DIRECT,
  230. 0, NULL, 0, 0, NULL);
  231. }
  232. int mbox_qspi_open(void)
  233. {
  234. static const struct socfpga_system_manager *sysmgr_regs =
  235. (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
  236. int ret;
  237. u32 resp_buf[1];
  238. u32 resp_buf_len;
  239. ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_QSPI_OPEN, MBOX_CMD_DIRECT,
  240. 0, NULL, 0, 0, NULL);
  241. if (ret) {
  242. /* retry again by closing and reopen the QSPI again */
  243. ret = mbox_qspi_close();
  244. if (ret)
  245. return ret;
  246. ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_QSPI_OPEN,
  247. MBOX_CMD_DIRECT, 0, NULL, 0, 0, NULL);
  248. if (ret)
  249. return ret;
  250. }
  251. /* HPS will directly control the QSPI controller, no longer mailbox */
  252. resp_buf_len = 1;
  253. ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_QSPI_DIRECT, MBOX_CMD_DIRECT,
  254. 0, NULL, 0, (u32 *)&resp_buf_len,
  255. (u32 *)&resp_buf);
  256. if (ret)
  257. goto error;
  258. /* We are getting QSPI ref clock and set into sysmgr boot register */
  259. printf("QSPI: Reference clock at %d Hz\n", resp_buf[0]);
  260. writel(resp_buf[0], &sysmgr_regs->boot_scratch_cold0);
  261. return 0;
  262. error:
  263. mbox_qspi_close();
  264. return ret;
  265. }
  266. #endif /* CONFIG_CADENCE_QSPI */
  267. int mbox_reset_cold(void)
  268. {
  269. int ret;
  270. ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_REBOOT_HPS, MBOX_CMD_DIRECT,
  271. 0, NULL, 0, 0, NULL);
  272. if (ret) {
  273. /* mailbox sent failure, wait for watchdog to kick in */
  274. hang();
  275. }
  276. return 0;
  277. }
  278. int mbox_send_cmd(u8 id, u32 cmd, u8 is_indirect, u32 len, u32 *arg,
  279. u8 urgent, u32 *resp_buf_len, u32 *resp_buf)
  280. {
  281. return mbox_send_cmd_common(id, cmd, is_indirect, len, arg, urgent,
  282. resp_buf_len, resp_buf);
  283. }
  284. int __secure mbox_send_cmd_psci(u8 id, u32 cmd, u8 is_indirect, u32 len,
  285. u32 *arg, u8 urgent, u32 *resp_buf_len,
  286. u32 *resp_buf)
  287. {
  288. return mbox_send_cmd_common(id, cmd, is_indirect, len, arg, urgent,
  289. resp_buf_len, resp_buf);
  290. }
  291. int mbox_send_cmd_only(u8 id, u32 cmd, u8 is_indirect, u32 len, u32 *arg)
  292. {
  293. return mbox_send_cmd_only_common(id, cmd, is_indirect, len, arg);
  294. }
  295. int __secure mbox_send_cmd_only_psci(u8 id, u32 cmd, u8 is_indirect, u32 len,
  296. u32 *arg)
  297. {
  298. return mbox_send_cmd_only_common(id, cmd, is_indirect, len, arg);
  299. }
  300. int mbox_rcv_resp(u32 *resp_buf, u32 resp_buf_max_len)
  301. {
  302. return __mbox_rcv_resp(resp_buf, resp_buf_max_len);
  303. }
  304. int __secure mbox_rcv_resp_psci(u32 *resp_buf, u32 resp_buf_max_len)
  305. {
  306. return __mbox_rcv_resp(resp_buf, resp_buf_max_len);
  307. }