scu.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2018 NXP
  4. *
  5. * Peng Fan <peng.fan@nxp.com>
  6. */
  7. #include <common.h>
  8. #include <log.h>
  9. #include <asm/global_data.h>
  10. #include <asm/io.h>
  11. #include <dm.h>
  12. #include <dm/lists.h>
  13. #include <dm/root.h>
  14. #include <dm/device-internal.h>
  15. #include <firmware/imx/sci/sci.h>
  16. #include <linux/bitops.h>
  17. #include <linux/iopoll.h>
  18. #include <misc.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. struct mu_type {
  21. u32 tr[4];
  22. u32 rr[4];
  23. u32 sr;
  24. u32 cr;
  25. };
  26. struct imx8_scu {
  27. struct mu_type *base;
  28. };
  29. #define MU_CR_GIE_MASK 0xF0000000u
  30. #define MU_CR_RIE_MASK 0xF000000u
  31. #define MU_CR_GIR_MASK 0xF0000u
  32. #define MU_CR_TIE_MASK 0xF00000u
  33. #define MU_CR_F_MASK 0x7u
  34. #define MU_SR_TE0_MASK BIT(23)
  35. #define MU_SR_RF0_MASK BIT(27)
  36. #define MU_TR_COUNT 4
  37. #define MU_RR_COUNT 4
  38. static inline void mu_hal_init(struct mu_type *base)
  39. {
  40. /* Clear GIEn, RIEn, TIEn, GIRn and ABFn. */
  41. clrbits_le32(&base->cr, MU_CR_GIE_MASK | MU_CR_RIE_MASK |
  42. MU_CR_TIE_MASK | MU_CR_GIR_MASK | MU_CR_F_MASK);
  43. }
  44. static int mu_hal_sendmsg(struct mu_type *base, u32 reg_index, u32 msg)
  45. {
  46. u32 mask = MU_SR_TE0_MASK >> reg_index;
  47. u32 val;
  48. int ret;
  49. assert(reg_index < MU_TR_COUNT);
  50. /* Wait TX register to be empty. */
  51. ret = readl_poll_timeout(&base->sr, val, val & mask, 10000);
  52. if (ret < 0) {
  53. printf("%s timeout\n", __func__);
  54. return -ETIMEDOUT;
  55. }
  56. writel(msg, &base->tr[reg_index]);
  57. return 0;
  58. }
  59. static int mu_hal_receivemsg(struct mu_type *base, u32 reg_index, u32 *msg)
  60. {
  61. u32 mask = MU_SR_RF0_MASK >> reg_index;
  62. u32 val;
  63. int ret;
  64. assert(reg_index < MU_TR_COUNT);
  65. /* Wait RX register to be full. */
  66. ret = readl_poll_timeout(&base->sr, val, val & mask, 1000000);
  67. if (ret < 0) {
  68. printf("%s timeout\n", __func__);
  69. return -ETIMEDOUT;
  70. }
  71. *msg = readl(&base->rr[reg_index]);
  72. return 0;
  73. }
  74. static int sc_ipc_read(struct mu_type *base, void *data)
  75. {
  76. struct sc_rpc_msg_s *msg = (struct sc_rpc_msg_s *)data;
  77. int ret;
  78. u8 count = 0;
  79. if (!msg)
  80. return -EINVAL;
  81. /* Read first word */
  82. ret = mu_hal_receivemsg(base, 0, (u32 *)msg);
  83. if (ret)
  84. return ret;
  85. count++;
  86. /* Check size */
  87. if (msg->size > SC_RPC_MAX_MSG) {
  88. *((u32 *)msg) = 0;
  89. return -EINVAL;
  90. }
  91. /* Read remaining words */
  92. while (count < msg->size) {
  93. ret = mu_hal_receivemsg(base, count % MU_RR_COUNT,
  94. &msg->DATA.u32[count - 1]);
  95. if (ret)
  96. return ret;
  97. count++;
  98. }
  99. return 0;
  100. }
  101. static int sc_ipc_write(struct mu_type *base, void *data)
  102. {
  103. struct sc_rpc_msg_s *msg = (struct sc_rpc_msg_s *)data;
  104. int ret;
  105. u8 count = 0;
  106. if (!msg)
  107. return -EINVAL;
  108. /* Check size */
  109. if (msg->size > SC_RPC_MAX_MSG)
  110. return -EINVAL;
  111. /* Write first word */
  112. ret = mu_hal_sendmsg(base, 0, *((u32 *)msg));
  113. if (ret)
  114. return ret;
  115. count++;
  116. /* Write remaining words */
  117. while (count < msg->size) {
  118. ret = mu_hal_sendmsg(base, count % MU_TR_COUNT,
  119. msg->DATA.u32[count - 1]);
  120. if (ret)
  121. return ret;
  122. count++;
  123. }
  124. return 0;
  125. }
  126. /*
  127. * Note the function prototype use msgid as the 2nd parameter, here
  128. * we take it as no_resp.
  129. */
  130. static int imx8_scu_call(struct udevice *dev, int no_resp, void *tx_msg,
  131. int tx_size, void *rx_msg, int rx_size)
  132. {
  133. struct imx8_scu *plat = dev_get_plat(dev);
  134. sc_err_t result;
  135. int ret;
  136. /* Expect tx_msg, rx_msg are the same value */
  137. if (rx_msg && tx_msg != rx_msg)
  138. printf("tx_msg %p, rx_msg %p\n", tx_msg, rx_msg);
  139. ret = sc_ipc_write(plat->base, tx_msg);
  140. if (ret)
  141. return ret;
  142. if (!no_resp) {
  143. ret = sc_ipc_read(plat->base, rx_msg);
  144. if (ret)
  145. return ret;
  146. }
  147. result = RPC_R8((struct sc_rpc_msg_s *)tx_msg);
  148. return sc_err_to_linux(result);
  149. }
  150. static int imx8_scu_probe(struct udevice *dev)
  151. {
  152. struct imx8_scu *plat = dev_get_plat(dev);
  153. fdt_addr_t addr;
  154. debug("%s(dev=%p) (plat=%p)\n", __func__, dev, plat);
  155. addr = dev_read_addr(dev);
  156. if (addr == FDT_ADDR_T_NONE)
  157. return -EINVAL;
  158. #ifdef CONFIG_SPL_BUILD
  159. plat->base = (struct mu_type *)CONFIG_MU_BASE_SPL;
  160. #else
  161. plat->base = (struct mu_type *)addr;
  162. #endif
  163. /* U-Boot not enable interrupts, so need to enable RX interrupts */
  164. mu_hal_init(plat->base);
  165. gd->arch.scu_dev = dev;
  166. return 0;
  167. }
  168. static int imx8_scu_remove(struct udevice *dev)
  169. {
  170. return 0;
  171. }
  172. static int imx8_scu_bind(struct udevice *dev)
  173. {
  174. int ret;
  175. struct udevice *child;
  176. ofnode node;
  177. debug("%s(dev=%p)\n", __func__, dev);
  178. ofnode_for_each_subnode(node, dev_ofnode(dev)) {
  179. ret = lists_bind_fdt(dev, node, &child, NULL, true);
  180. if (ret)
  181. return ret;
  182. debug("bind child dev %s\n", child->name);
  183. }
  184. return 0;
  185. }
  186. static struct misc_ops imx8_scu_ops = {
  187. .call = imx8_scu_call,
  188. };
  189. static const struct udevice_id imx8_scu_ids[] = {
  190. { .compatible = "fsl,imx8qxp-mu" },
  191. { .compatible = "fsl,imx8-mu" },
  192. { }
  193. };
  194. U_BOOT_DRIVER(imx8_scu) = {
  195. .name = "imx8_scu",
  196. .id = UCLASS_MISC,
  197. .of_match = imx8_scu_ids,
  198. .probe = imx8_scu_probe,
  199. .bind = imx8_scu_bind,
  200. .remove = imx8_scu_remove,
  201. .ops = &imx8_scu_ops,
  202. .plat_auto = sizeof(struct imx8_scu),
  203. .flags = DM_FLAG_PRE_RELOC,
  204. };