meson_mx_ao_arc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2020 Martin Blumenstingl <[email protected]>
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/bitops.h>
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/genalloc.h>
  10. #include <linux/io.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/property.h>
  15. #include <linux/regmap.h>
  16. #include <linux/remoteproc.h>
  17. #include <linux/reset.h>
  18. #include <linux/sizes.h>
  19. #include "remoteproc_internal.h"
  20. #define AO_REMAP_REG0 0x0
  21. #define AO_REMAP_REG0_REMAP_AHB_SRAM_BITS_17_14_FOR_ARM_CPU GENMASK(3, 0)
  22. #define AO_REMAP_REG1 0x4
  23. #define AO_REMAP_REG1_MOVE_AHB_SRAM_TO_0X0_INSTEAD_OF_DDR BIT(4)
  24. #define AO_REMAP_REG1_REMAP_AHB_SRAM_BITS_17_14_FOR_MEDIA_CPU GENMASK(3, 0)
  25. #define AO_CPU_CNTL 0x0
  26. #define AO_CPU_CNTL_AHB_SRAM_BITS_31_20 GENMASK(28, 16)
  27. #define AO_CPU_CNTL_HALT BIT(9)
  28. #define AO_CPU_CNTL_UNKNONWN BIT(8)
  29. #define AO_CPU_CNTL_RUN BIT(0)
  30. #define AO_CPU_STAT 0x4
  31. #define AO_SECURE_REG0 0x0
  32. #define AO_SECURE_REG0_AHB_SRAM_BITS_19_12 GENMASK(15, 8)
  33. /* Only bits [31:20] and [17:14] are usable, all other bits must be zero */
  34. #define MESON_AO_RPROC_SRAM_USABLE_BITS 0xfff3c000ULL
  35. #define MESON_AO_RPROC_MEMORY_OFFSET 0x10000000
  36. struct meson_mx_ao_arc_rproc_priv {
  37. void __iomem *remap_base;
  38. void __iomem *cpu_base;
  39. unsigned long sram_va;
  40. phys_addr_t sram_pa;
  41. size_t sram_size;
  42. struct gen_pool *sram_pool;
  43. struct reset_control *arc_reset;
  44. struct clk *arc_pclk;
  45. struct regmap *secbus2_regmap;
  46. };
  47. static int meson_mx_ao_arc_rproc_start(struct rproc *rproc)
  48. {
  49. struct meson_mx_ao_arc_rproc_priv *priv = rproc->priv;
  50. phys_addr_t translated_sram_addr;
  51. u32 tmp;
  52. int ret;
  53. ret = clk_prepare_enable(priv->arc_pclk);
  54. if (ret)
  55. return ret;
  56. tmp = FIELD_PREP(AO_REMAP_REG0_REMAP_AHB_SRAM_BITS_17_14_FOR_ARM_CPU,
  57. priv->sram_pa >> 14);
  58. writel(tmp, priv->remap_base + AO_REMAP_REG0);
  59. /*
  60. * The SRAM content as seen by the ARC core always starts at 0x0
  61. * regardless of the value given here (this was discovered by trial and
  62. * error). For SoCs older than Meson6 we probably have to set
  63. * AO_REMAP_REG1_MOVE_AHB_SRAM_TO_0X0_INSTEAD_OF_DDR to achieve the
  64. * same. (At least) For Meson8 and newer that bit must not be set.
  65. */
  66. writel(0x0, priv->remap_base + AO_REMAP_REG1);
  67. regmap_update_bits(priv->secbus2_regmap, AO_SECURE_REG0,
  68. AO_SECURE_REG0_AHB_SRAM_BITS_19_12,
  69. FIELD_PREP(AO_SECURE_REG0_AHB_SRAM_BITS_19_12,
  70. priv->sram_pa >> 12));
  71. ret = reset_control_reset(priv->arc_reset);
  72. if (ret) {
  73. clk_disable_unprepare(priv->arc_pclk);
  74. return ret;
  75. }
  76. usleep_range(10, 100);
  77. /*
  78. * Convert from 0xd9000000 to 0xc9000000 as the vendor driver does.
  79. * This only seems to be relevant for the AO_CPU_CNTL register. It is
  80. * unknown why this is needed.
  81. */
  82. translated_sram_addr = priv->sram_pa - MESON_AO_RPROC_MEMORY_OFFSET;
  83. tmp = FIELD_PREP(AO_CPU_CNTL_AHB_SRAM_BITS_31_20,
  84. translated_sram_addr >> 20);
  85. tmp |= AO_CPU_CNTL_UNKNONWN | AO_CPU_CNTL_RUN;
  86. writel(tmp, priv->cpu_base + AO_CPU_CNTL);
  87. usleep_range(20, 200);
  88. return 0;
  89. }
  90. static int meson_mx_ao_arc_rproc_stop(struct rproc *rproc)
  91. {
  92. struct meson_mx_ao_arc_rproc_priv *priv = rproc->priv;
  93. writel(AO_CPU_CNTL_HALT, priv->cpu_base + AO_CPU_CNTL);
  94. clk_disable_unprepare(priv->arc_pclk);
  95. return 0;
  96. }
  97. static void *meson_mx_ao_arc_rproc_da_to_va(struct rproc *rproc, u64 da,
  98. size_t len, bool *is_iomem)
  99. {
  100. struct meson_mx_ao_arc_rproc_priv *priv = rproc->priv;
  101. /* The memory from the ARC core's perspective always starts at 0x0. */
  102. if ((da + len) > priv->sram_size)
  103. return NULL;
  104. return (void *)priv->sram_va + da;
  105. }
  106. static struct rproc_ops meson_mx_ao_arc_rproc_ops = {
  107. .start = meson_mx_ao_arc_rproc_start,
  108. .stop = meson_mx_ao_arc_rproc_stop,
  109. .da_to_va = meson_mx_ao_arc_rproc_da_to_va,
  110. .get_boot_addr = rproc_elf_get_boot_addr,
  111. .load = rproc_elf_load_segments,
  112. .sanity_check = rproc_elf_sanity_check,
  113. };
  114. static int meson_mx_ao_arc_rproc_probe(struct platform_device *pdev)
  115. {
  116. struct meson_mx_ao_arc_rproc_priv *priv;
  117. struct device *dev = &pdev->dev;
  118. const char *fw_name = NULL;
  119. struct rproc *rproc;
  120. int ret;
  121. device_property_read_string(dev, "firmware-name", &fw_name);
  122. rproc = devm_rproc_alloc(dev, "meson-mx-ao-arc",
  123. &meson_mx_ao_arc_rproc_ops, fw_name,
  124. sizeof(*priv));
  125. if (!rproc)
  126. return -ENOMEM;
  127. rproc->has_iommu = false;
  128. priv = rproc->priv;
  129. priv->sram_pool = of_gen_pool_get(dev->of_node, "sram", 0);
  130. if (!priv->sram_pool) {
  131. dev_err(dev, "Could not get SRAM pool\n");
  132. return -ENODEV;
  133. }
  134. priv->sram_size = gen_pool_avail(priv->sram_pool);
  135. priv->sram_va = gen_pool_alloc(priv->sram_pool, priv->sram_size);
  136. if (!priv->sram_va) {
  137. dev_err(dev, "Could not alloc memory in SRAM pool\n");
  138. return -ENOMEM;
  139. }
  140. priv->sram_pa = gen_pool_virt_to_phys(priv->sram_pool, priv->sram_va);
  141. if (priv->sram_pa & ~MESON_AO_RPROC_SRAM_USABLE_BITS) {
  142. dev_err(dev, "SRAM address contains unusable bits\n");
  143. ret = -EINVAL;
  144. goto err_free_genpool;
  145. }
  146. priv->secbus2_regmap = syscon_regmap_lookup_by_phandle(dev->of_node,
  147. "amlogic,secbus2");
  148. if (IS_ERR(priv->secbus2_regmap)) {
  149. dev_err(dev, "Failed to find SECBUS2 regmap\n");
  150. ret = PTR_ERR(priv->secbus2_regmap);
  151. goto err_free_genpool;
  152. }
  153. priv->remap_base = devm_platform_ioremap_resource_byname(pdev, "remap");
  154. if (IS_ERR(priv->remap_base)) {
  155. ret = PTR_ERR(priv->remap_base);
  156. goto err_free_genpool;
  157. }
  158. priv->cpu_base = devm_platform_ioremap_resource_byname(pdev, "cpu");
  159. if (IS_ERR(priv->cpu_base)) {
  160. ret = PTR_ERR(priv->cpu_base);
  161. goto err_free_genpool;
  162. }
  163. priv->arc_reset = devm_reset_control_get_exclusive(dev, NULL);
  164. if (IS_ERR(priv->arc_reset)) {
  165. dev_err(dev, "Failed to get ARC reset\n");
  166. ret = PTR_ERR(priv->arc_reset);
  167. goto err_free_genpool;
  168. }
  169. priv->arc_pclk = devm_clk_get(dev, NULL);
  170. if (IS_ERR(priv->arc_pclk)) {
  171. dev_err(dev, "Failed to get the ARC PCLK\n");
  172. ret = PTR_ERR(priv->arc_pclk);
  173. goto err_free_genpool;
  174. }
  175. platform_set_drvdata(pdev, rproc);
  176. ret = rproc_add(rproc);
  177. if (ret)
  178. goto err_free_genpool;
  179. return 0;
  180. err_free_genpool:
  181. gen_pool_free(priv->sram_pool, priv->sram_va, priv->sram_size);
  182. return ret;
  183. }
  184. static int meson_mx_ao_arc_rproc_remove(struct platform_device *pdev)
  185. {
  186. struct rproc *rproc = platform_get_drvdata(pdev);
  187. struct meson_mx_ao_arc_rproc_priv *priv = rproc->priv;
  188. rproc_del(rproc);
  189. gen_pool_free(priv->sram_pool, priv->sram_va, priv->sram_size);
  190. return 0;
  191. }
  192. static const struct of_device_id meson_mx_ao_arc_rproc_match[] = {
  193. { .compatible = "amlogic,meson8-ao-arc" },
  194. { .compatible = "amlogic,meson8b-ao-arc" },
  195. { /* sentinel */ }
  196. };
  197. MODULE_DEVICE_TABLE(of, meson_mx_ao_arc_rproc_match);
  198. static struct platform_driver meson_mx_ao_arc_rproc_driver = {
  199. .probe = meson_mx_ao_arc_rproc_probe,
  200. .remove = meson_mx_ao_arc_rproc_remove,
  201. .driver = {
  202. .name = "meson-mx-ao-arc-rproc",
  203. .of_match_table = meson_mx_ao_arc_rproc_match,
  204. },
  205. };
  206. module_platform_driver(meson_mx_ao_arc_rproc_driver);
  207. MODULE_DESCRIPTION("Amlogic Meson6/8/8b/8m2 AO ARC remote processor driver");
  208. MODULE_AUTHOR("Martin Blumenstingl <[email protected]>");
  209. MODULE_LICENSE("GPL v2");