mailbox_s10.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. ret = mbox_prepare_cmd_only(id, cmd, is_indirect, len, arg);
  131. if (ret)
  132. return ret;
  133. if (urgent) {
  134. /* Read status because it is toggled */
  135. status = MBOX_READL(MBOX_STATUS) & MBOX_STATUS_UA_MSK;
  136. /* Send command as urgent command */
  137. MBOX_WRITEL(1, MBOX_URG);
  138. }
  139. /* write doorbell */
  140. MBOX_WRITEL(1, MBOX_DOORBELL_TO_SDM);
  141. while (1) {
  142. ret = ~0;
  143. /* Wait for doorbell from SDM */
  144. while (!MBOX_READL(MBOX_DOORBELL_FROM_SDM) && ret--)
  145. ;
  146. if (!ret)
  147. return -ETIMEDOUT;
  148. /* clear interrupt */
  149. MBOX_WRITEL(0, MBOX_DOORBELL_FROM_SDM);
  150. if (urgent) {
  151. u32 new_status = MBOX_READL(MBOX_STATUS);
  152. /* urgent command doesn't have response */
  153. MBOX_WRITEL(0, MBOX_URG);
  154. /* Urgent ACK is toggled */
  155. if ((new_status & MBOX_STATUS_UA_MSK) ^ status)
  156. return 0;
  157. return -ECOMM;
  158. }
  159. /* read current response offset */
  160. rout = MBOX_READL(MBOX_ROUT);
  161. /* read response valid offset */
  162. rin = MBOX_READL(MBOX_RIN);
  163. if (rout != rin) {
  164. /* Response received */
  165. resp = MBOX_READ_RESP_BUF(rout);
  166. rout++;
  167. /* wrapping around when it reach the buffer size */
  168. rout %= MBOX_RESP_BUFFER_SIZE;
  169. /* update next ROUT */
  170. MBOX_WRITEL(rout, MBOX_ROUT);
  171. /* check client ID and ID */
  172. if ((MBOX_RESP_CLIENT_GET(resp) ==
  173. MBOX_CLIENT_ID_UBOOT) &&
  174. (MBOX_RESP_ID_GET(resp) == id)) {
  175. ret = MBOX_RESP_ERR_GET(resp);
  176. if (ret)
  177. return ret;
  178. if (resp_buf_len) {
  179. buf_len = *resp_buf_len;
  180. *resp_buf_len = 0;
  181. } else {
  182. buf_len = 0;
  183. }
  184. resp_len = MBOX_RESP_LEN_GET(resp);
  185. while (resp_len) {
  186. ret = mbox_polling_resp(rout);
  187. if (ret)
  188. return ret;
  189. /* we need to process response buffer
  190. * even caller doesn't need it
  191. */
  192. resp = MBOX_READ_RESP_BUF(rout);
  193. rout++;
  194. resp_len--;
  195. rout %= MBOX_RESP_BUFFER_SIZE;
  196. MBOX_WRITEL(rout, MBOX_ROUT);
  197. if (buf_len) {
  198. /* copy response to buffer */
  199. resp_buf[*resp_buf_len] = resp;
  200. (*resp_buf_len)++;
  201. buf_len--;
  202. }
  203. }
  204. return ret;
  205. }
  206. }
  207. };
  208. return -EIO;
  209. }
  210. int mbox_init(void)
  211. {
  212. int ret;
  213. /* enable mailbox interrupts */
  214. MBOX_WRITEL(MBOX_ALL_INTRS, MBOX_FLAGS);
  215. /* Ensure urgent request is cleared */
  216. MBOX_WRITEL(0, MBOX_URG);
  217. /* Ensure the Doorbell Interrupt is cleared */
  218. MBOX_WRITEL(0, MBOX_DOORBELL_FROM_SDM);
  219. ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_RESTART, MBOX_CMD_DIRECT, 0,
  220. NULL, 1, 0, NULL);
  221. if (ret)
  222. return ret;
  223. /* Renable mailbox interrupts after MBOX_RESTART */
  224. MBOX_WRITEL(MBOX_ALL_INTRS, MBOX_FLAGS);
  225. return 0;
  226. }
  227. #ifdef CONFIG_CADENCE_QSPI
  228. int mbox_qspi_close(void)
  229. {
  230. return mbox_send_cmd(MBOX_ID_UBOOT, MBOX_QSPI_CLOSE, MBOX_CMD_DIRECT,
  231. 0, NULL, 0, 0, NULL);
  232. }
  233. int mbox_qspi_open(void)
  234. {
  235. static const struct socfpga_system_manager *sysmgr_regs =
  236. (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
  237. int ret;
  238. u32 resp_buf[1];
  239. u32 resp_buf_len;
  240. ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_QSPI_OPEN, MBOX_CMD_DIRECT,
  241. 0, NULL, 0, 0, NULL);
  242. if (ret) {
  243. /* retry again by closing and reopen the QSPI again */
  244. ret = mbox_qspi_close();
  245. if (ret)
  246. return ret;
  247. ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_QSPI_OPEN,
  248. MBOX_CMD_DIRECT, 0, NULL, 0, 0, NULL);
  249. if (ret)
  250. return ret;
  251. }
  252. /* HPS will directly control the QSPI controller, no longer mailbox */
  253. resp_buf_len = 1;
  254. ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_QSPI_DIRECT, MBOX_CMD_DIRECT,
  255. 0, NULL, 0, (u32 *)&resp_buf_len,
  256. (u32 *)&resp_buf);
  257. if (ret)
  258. goto error;
  259. /* We are getting QSPI ref clock and set into sysmgr boot register */
  260. printf("QSPI: Reference clock at %d Hz\n", resp_buf[0]);
  261. writel(resp_buf[0], &sysmgr_regs->boot_scratch_cold0);
  262. return 0;
  263. error:
  264. mbox_qspi_close();
  265. return ret;
  266. }
  267. #endif /* CONFIG_CADENCE_QSPI */
  268. int mbox_reset_cold(void)
  269. {
  270. int ret;
  271. ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_REBOOT_HPS, MBOX_CMD_DIRECT,
  272. 0, NULL, 0, 0, NULL);
  273. if (ret) {
  274. /* mailbox sent failure, wait for watchdog to kick in */
  275. hang();
  276. }
  277. return 0;
  278. }
  279. int mbox_send_cmd(u8 id, u32 cmd, u8 is_indirect, u32 len, u32 *arg,
  280. u8 urgent, u32 *resp_buf_len, u32 *resp_buf)
  281. {
  282. return mbox_send_cmd_common(id, cmd, is_indirect, len, arg, urgent,
  283. resp_buf_len, resp_buf);
  284. }
  285. int __secure mbox_send_cmd_psci(u8 id, u32 cmd, u8 is_indirect, u32 len,
  286. u32 *arg, u8 urgent, u32 *resp_buf_len,
  287. u32 *resp_buf)
  288. {
  289. return mbox_send_cmd_common(id, cmd, is_indirect, len, arg, urgent,
  290. resp_buf_len, resp_buf);
  291. }
  292. int mbox_send_cmd_only(u8 id, u32 cmd, u8 is_indirect, u32 len, u32 *arg)
  293. {
  294. return mbox_send_cmd_only_common(id, cmd, is_indirect, len, arg);
  295. }
  296. int __secure mbox_send_cmd_only_psci(u8 id, u32 cmd, u8 is_indirect, u32 len,
  297. u32 *arg)
  298. {
  299. return mbox_send_cmd_only_common(id, cmd, is_indirect, len, arg);
  300. }
  301. int mbox_rcv_resp(u32 *resp_buf, u32 resp_buf_max_len)
  302. {
  303. return __mbox_rcv_resp(resp_buf, resp_buf_max_len);
  304. }
  305. int __secure mbox_rcv_resp_psci(u32 *resp_buf, u32 resp_buf_max_len)
  306. {
  307. return __mbox_rcv_resp(resp_buf, resp_buf_max_len);
  308. }