stm32_copro.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
  2. /*
  3. * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
  4. */
  5. #define LOG_CATEGORY UCLASS_REMOTEPROC
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <fdtdec.h>
  10. #include <log.h>
  11. #include <remoteproc.h>
  12. #include <reset.h>
  13. #include <asm/io.h>
  14. #include <dm/device_compat.h>
  15. #include <linux/err.h>
  16. /**
  17. * struct stm32_copro_privdata - power processor private data
  18. * @reset_ctl: reset controller handle
  19. * @hold_boot: hold boot controller handle
  20. * @rsc_table_addr: resource table address
  21. */
  22. struct stm32_copro_privdata {
  23. struct reset_ctl reset_ctl;
  24. struct reset_ctl hold_boot;
  25. ulong rsc_table_addr;
  26. };
  27. /**
  28. * stm32_copro_probe() - Basic probe
  29. * @dev: corresponding STM32 remote processor device
  30. * @return 0 if all went ok, else corresponding -ve error
  31. */
  32. static int stm32_copro_probe(struct udevice *dev)
  33. {
  34. struct stm32_copro_privdata *priv;
  35. int ret;
  36. priv = dev_get_priv(dev);
  37. ret = reset_get_by_name(dev, "mcu_rst", &priv->reset_ctl);
  38. if (ret) {
  39. dev_err(dev, "failed to get reset (%d)\n", ret);
  40. return ret;
  41. }
  42. ret = reset_get_by_name(dev, "hold_boot", &priv->hold_boot);
  43. if (ret) {
  44. dev_err(dev, "failed to get hold boot (%d)\n", ret);
  45. return ret;
  46. }
  47. dev_dbg(dev, "probed\n");
  48. return 0;
  49. }
  50. /**
  51. * stm32_copro_device_to_virt() - Convert device address to virtual address
  52. * @dev: corresponding STM32 remote processor device
  53. * @da: device address
  54. * @size: Size of the memory region @da is pointing to
  55. * @return converted virtual address
  56. */
  57. static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
  58. ulong size)
  59. {
  60. fdt32_t in_addr = cpu_to_be32(da), end_addr;
  61. u64 paddr;
  62. paddr = dev_translate_dma_address(dev, &in_addr);
  63. if (paddr == OF_BAD_ADDR) {
  64. dev_err(dev, "Unable to convert address %ld\n", da);
  65. return NULL;
  66. }
  67. end_addr = cpu_to_be32(da + size - 1);
  68. if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) {
  69. dev_err(dev, "Unable to convert address %ld\n", da + size - 1);
  70. return NULL;
  71. }
  72. return phys_to_virt(paddr);
  73. }
  74. /**
  75. * stm32_copro_load() - Loadup the STM32 remote processor
  76. * @dev: corresponding STM32 remote processor device
  77. * @addr: Address in memory where image is stored
  78. * @size: Size in bytes of the image
  79. * @return 0 if all went ok, else corresponding -ve error
  80. */
  81. static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
  82. {
  83. struct stm32_copro_privdata *priv;
  84. ulong rsc_table_size;
  85. int ret;
  86. priv = dev_get_priv(dev);
  87. ret = reset_assert(&priv->hold_boot);
  88. if (ret) {
  89. dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
  90. return ret;
  91. }
  92. ret = reset_assert(&priv->reset_ctl);
  93. if (ret) {
  94. dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
  95. return ret;
  96. }
  97. if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr,
  98. &rsc_table_size)) {
  99. priv->rsc_table_addr = 0;
  100. dev_warn(dev, "No valid resource table for this firmware\n");
  101. }
  102. return rproc_elf32_load_image(dev, addr, size);
  103. }
  104. /**
  105. * stm32_copro_start() - Start the STM32 remote processor
  106. * @dev: corresponding STM32 remote processor device
  107. * @return 0 if all went ok, else corresponding -ve error
  108. */
  109. static int stm32_copro_start(struct udevice *dev)
  110. {
  111. struct stm32_copro_privdata *priv;
  112. int ret;
  113. priv = dev_get_priv(dev);
  114. ret = reset_deassert(&priv->hold_boot);
  115. if (ret) {
  116. dev_err(dev, "Unable to deassert hold boot (ret=%d)\n", ret);
  117. return ret;
  118. }
  119. /*
  120. * Once copro running, reset hold boot flag to avoid copro
  121. * rebooting autonomously (error should never occur)
  122. */
  123. ret = reset_assert(&priv->hold_boot);
  124. if (ret)
  125. dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
  126. /* indicates that copro is running */
  127. writel(TAMP_COPRO_STATE_CRUN, TAMP_COPRO_STATE);
  128. /* Store rsc_address in bkp register */
  129. writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
  130. return 0;
  131. }
  132. /**
  133. * stm32_copro_reset() - Reset the STM32 remote processor
  134. * @dev: corresponding STM32 remote processor device
  135. * @return 0 if all went ok, else corresponding -ve error
  136. */
  137. static int stm32_copro_reset(struct udevice *dev)
  138. {
  139. struct stm32_copro_privdata *priv;
  140. int ret;
  141. priv = dev_get_priv(dev);
  142. ret = reset_assert(&priv->hold_boot);
  143. if (ret) {
  144. dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
  145. return ret;
  146. }
  147. ret = reset_assert(&priv->reset_ctl);
  148. if (ret) {
  149. dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
  150. return ret;
  151. }
  152. writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
  153. return 0;
  154. }
  155. /**
  156. * stm32_copro_stop() - Stop the STM32 remote processor
  157. * @dev: corresponding STM32 remote processor device
  158. * @return 0 if all went ok, else corresponding -ve error
  159. */
  160. static int stm32_copro_stop(struct udevice *dev)
  161. {
  162. return stm32_copro_reset(dev);
  163. }
  164. /**
  165. * stm32_copro_is_running() - Is the STM32 remote processor running
  166. * @dev: corresponding STM32 remote processor device
  167. * @return 0 if the remote processor is running, 1 otherwise
  168. */
  169. static int stm32_copro_is_running(struct udevice *dev)
  170. {
  171. return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
  172. }
  173. static const struct dm_rproc_ops stm32_copro_ops = {
  174. .load = stm32_copro_load,
  175. .start = stm32_copro_start,
  176. .stop = stm32_copro_stop,
  177. .reset = stm32_copro_reset,
  178. .is_running = stm32_copro_is_running,
  179. .device_to_virt = stm32_copro_device_to_virt,
  180. };
  181. static const struct udevice_id stm32_copro_ids[] = {
  182. {.compatible = "st,stm32mp1-m4"},
  183. {}
  184. };
  185. U_BOOT_DRIVER(stm32_copro) = {
  186. .name = "stm32_m4_proc",
  187. .of_match = stm32_copro_ids,
  188. .id = UCLASS_REMOTEPROC,
  189. .ops = &stm32_copro_ops,
  190. .probe = stm32_copro_probe,
  191. .priv_auto = sizeof(struct stm32_copro_privdata),
  192. };