ele_mu.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020-2022 NXP
  4. */
  5. #include <common.h>
  6. #include <asm/io.h>
  7. #include <dm.h>
  8. #include <dm/lists.h>
  9. #include <dm/root.h>
  10. #include <dm/device-internal.h>
  11. #include <asm/mach-imx/ele_api.h>
  12. #include <asm/arch/imx-regs.h>
  13. #include <linux/iopoll.h>
  14. #include <misc.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. struct imx8ulp_mu {
  17. struct mu_type *base;
  18. };
  19. #define MU_SR_TE0_MASK BIT(0)
  20. #define MU_SR_RF0_MASK BIT(0)
  21. #define MU_TR_COUNT 8
  22. #define MU_RR_COUNT 4
  23. void mu_hal_init(ulong base)
  24. {
  25. struct mu_type *mu_base = (struct mu_type *)base;
  26. writel(0, &mu_base->tcr);
  27. writel(0, &mu_base->rcr);
  28. }
  29. int mu_hal_sendmsg(ulong base, u32 reg_index, u32 msg)
  30. {
  31. struct mu_type *mu_base = (struct mu_type *)base;
  32. u32 mask = MU_SR_TE0_MASK << reg_index;
  33. u32 val;
  34. int ret;
  35. assert(reg_index < MU_TR_COUNT);
  36. debug("sendmsg tsr 0x%x\n", readl(&mu_base->tsr));
  37. /* Wait TX register to be empty. */
  38. ret = readl_poll_timeout(&mu_base->tsr, val, val & mask, 10000);
  39. if (ret < 0) {
  40. debug("%s timeout\n", __func__);
  41. return -ETIMEDOUT;
  42. }
  43. debug("tr[%d] 0x%x\n", reg_index, msg);
  44. writel(msg, &mu_base->tr[reg_index]);
  45. return 0;
  46. }
  47. int mu_hal_receivemsg(ulong base, u32 reg_index, u32 *msg)
  48. {
  49. struct mu_type *mu_base = (struct mu_type *)base;
  50. u32 mask = MU_SR_RF0_MASK << reg_index;
  51. u32 val;
  52. int ret;
  53. u32 count = 10;
  54. assert(reg_index < MU_RR_COUNT);
  55. debug("receivemsg rsr 0x%x\n", readl(&mu_base->rsr));
  56. do {
  57. /* Wait RX register to be full. */
  58. ret = readl_poll_timeout(&mu_base->rsr, val, val & mask, 1000000);
  59. if (ret < 0) {
  60. count--;
  61. printf("mu receive msg wait %us\n", 10 - count);
  62. } else {
  63. break;
  64. }
  65. } while (count > 0);
  66. if (count == 0) {
  67. debug("%s timeout\n", __func__);
  68. return -ETIMEDOUT;
  69. }
  70. *msg = readl(&mu_base->rr[reg_index]);
  71. debug("rr[%d] 0x%x\n", reg_index, *msg);
  72. return 0;
  73. }
  74. static int imx8ulp_mu_read(struct mu_type *base, void *data)
  75. {
  76. struct ele_msg *msg = (struct ele_msg *)data;
  77. int ret;
  78. u8 count = 0;
  79. if (!msg)
  80. return -EINVAL;
  81. /* Read first word */
  82. ret = mu_hal_receivemsg((ulong)base, 0, (u32 *)msg);
  83. if (ret)
  84. return ret;
  85. count++;
  86. /* Check size */
  87. if (msg->size > ELE_MAX_MSG) {
  88. *((u32 *)msg) = 0;
  89. return -EINVAL;
  90. }
  91. /* Read remaining words */
  92. while (count < msg->size) {
  93. ret = mu_hal_receivemsg((ulong)base, count % MU_RR_COUNT,
  94. &msg->data[count - 1]);
  95. if (ret)
  96. return ret;
  97. count++;
  98. }
  99. return 0;
  100. }
  101. static int imx8ulp_mu_write(struct mu_type *base, void *data)
  102. {
  103. struct ele_msg *msg = (struct ele_msg *)data;
  104. int ret;
  105. u8 count = 0;
  106. if (!msg)
  107. return -EINVAL;
  108. /* Check size */
  109. if (msg->size > ELE_MAX_MSG)
  110. return -EINVAL;
  111. /* Write first word */
  112. ret = mu_hal_sendmsg((ulong)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((ulong)base, count % MU_TR_COUNT,
  119. msg->data[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 imx8ulp_mu_call(struct udevice *dev, int no_resp, void *tx_msg,
  131. int tx_size, void *rx_msg, int rx_size)
  132. {
  133. struct imx8ulp_mu *priv = dev_get_priv(dev);
  134. u32 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 = imx8ulp_mu_write(priv->base, tx_msg);
  140. if (ret)
  141. return ret;
  142. if (!no_resp) {
  143. ret = imx8ulp_mu_read(priv->base, rx_msg);
  144. if (ret)
  145. return ret;
  146. }
  147. result = ((struct ele_msg *)rx_msg)->data[0];
  148. if ((result & 0xff) == 0xd6)
  149. return 0;
  150. return -EIO;
  151. }
  152. static int imx8ulp_mu_probe(struct udevice *dev)
  153. {
  154. struct imx8ulp_mu *priv = dev_get_priv(dev);
  155. fdt_addr_t addr;
  156. debug("%s(dev=%p) (priv=%p)\n", __func__, dev, priv);
  157. addr = devfdt_get_addr(dev);
  158. if (addr == FDT_ADDR_T_NONE)
  159. return -EINVAL;
  160. priv->base = (struct mu_type *)addr;
  161. debug("mu base 0x%lx\n", (ulong)priv->base);
  162. /* U-Boot not enable interrupts, so need to enable RX interrupts */
  163. mu_hal_init((ulong)priv->base);
  164. gd->arch.ele_dev = dev;
  165. return 0;
  166. }
  167. static int imx8ulp_mu_remove(struct udevice *dev)
  168. {
  169. return 0;
  170. }
  171. static int imx8ulp_mu_bind(struct udevice *dev)
  172. {
  173. debug("%s(dev=%p)\n", __func__, dev);
  174. return 0;
  175. }
  176. static struct misc_ops imx8ulp_mu_ops = {
  177. .call = imx8ulp_mu_call,
  178. };
  179. static const struct udevice_id imx8ulp_mu_ids[] = {
  180. { .compatible = "fsl,imx8ulp-mu" },
  181. { .compatible = "fsl,imx93-mu-s4" },
  182. { }
  183. };
  184. U_BOOT_DRIVER(imx8ulp_mu) = {
  185. .name = "imx8ulp_mu",
  186. .id = UCLASS_MISC,
  187. .of_match = imx8ulp_mu_ids,
  188. .probe = imx8ulp_mu_probe,
  189. .bind = imx8ulp_mu_bind,
  190. .remove = imx8ulp_mu_remove,
  191. .ops = &imx8ulp_mu_ops,
  192. .priv_auto = sizeof(struct imx8ulp_mu),
  193. };