qcom_adsp_pil.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Qualcomm ADSP/SLPI Peripheral Image Loader for MSM8974 and MSM8996
  3. *
  4. * Copyright (C) 2016 Linaro Ltd
  5. * Copyright (C) 2014 Sony Mobile Communications AB
  6. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/firmware.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/qcom_scm.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/remoteproc.h>
  28. #include <linux/soc/qcom/mdt_loader.h>
  29. #include <linux/soc/qcom/smem.h>
  30. #include <linux/soc/qcom/smem_state.h>
  31. #include "qcom_common.h"
  32. #include "qcom_q6v5.h"
  33. #include "remoteproc_internal.h"
  34. struct adsp_data {
  35. int crash_reason_smem;
  36. const char *firmware_name;
  37. int pas_id;
  38. bool has_aggre2_clk;
  39. const char *ssr_name;
  40. const char *sysmon_name;
  41. int ssctl_id;
  42. };
  43. struct qcom_adsp {
  44. struct device *dev;
  45. struct rproc *rproc;
  46. struct qcom_q6v5 q6v5;
  47. struct clk *xo;
  48. struct clk *aggre2_clk;
  49. struct regulator *cx_supply;
  50. struct regulator *px_supply;
  51. int pas_id;
  52. int crash_reason_smem;
  53. bool has_aggre2_clk;
  54. struct completion start_done;
  55. struct completion stop_done;
  56. phys_addr_t mem_phys;
  57. phys_addr_t mem_reloc;
  58. void *mem_region;
  59. size_t mem_size;
  60. struct qcom_rproc_glink glink_subdev;
  61. struct qcom_rproc_subdev smd_subdev;
  62. struct qcom_rproc_ssr ssr_subdev;
  63. struct qcom_sysmon *sysmon;
  64. };
  65. static int adsp_load(struct rproc *rproc, const struct firmware *fw)
  66. {
  67. struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
  68. return qcom_mdt_load(adsp->dev, fw, rproc->firmware, adsp->pas_id,
  69. adsp->mem_region, adsp->mem_phys, adsp->mem_size,
  70. &adsp->mem_reloc);
  71. }
  72. static int adsp_start(struct rproc *rproc)
  73. {
  74. struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
  75. int ret;
  76. qcom_q6v5_prepare(&adsp->q6v5);
  77. ret = clk_prepare_enable(adsp->xo);
  78. if (ret)
  79. return ret;
  80. ret = clk_prepare_enable(adsp->aggre2_clk);
  81. if (ret)
  82. goto disable_xo_clk;
  83. ret = regulator_enable(adsp->cx_supply);
  84. if (ret)
  85. goto disable_aggre2_clk;
  86. ret = regulator_enable(adsp->px_supply);
  87. if (ret)
  88. goto disable_cx_supply;
  89. ret = qcom_scm_pas_auth_and_reset(adsp->pas_id);
  90. if (ret) {
  91. dev_err(adsp->dev,
  92. "failed to authenticate image and release reset\n");
  93. goto disable_px_supply;
  94. }
  95. ret = qcom_q6v5_wait_for_start(&adsp->q6v5, msecs_to_jiffies(5000));
  96. if (ret == -ETIMEDOUT) {
  97. dev_err(adsp->dev, "start timed out\n");
  98. qcom_scm_pas_shutdown(adsp->pas_id);
  99. goto disable_px_supply;
  100. }
  101. return 0;
  102. disable_px_supply:
  103. regulator_disable(adsp->px_supply);
  104. disable_cx_supply:
  105. regulator_disable(adsp->cx_supply);
  106. disable_aggre2_clk:
  107. clk_disable_unprepare(adsp->aggre2_clk);
  108. disable_xo_clk:
  109. clk_disable_unprepare(adsp->xo);
  110. return ret;
  111. }
  112. static void qcom_pas_handover(struct qcom_q6v5 *q6v5)
  113. {
  114. struct qcom_adsp *adsp = container_of(q6v5, struct qcom_adsp, q6v5);
  115. regulator_disable(adsp->px_supply);
  116. regulator_disable(adsp->cx_supply);
  117. clk_disable_unprepare(adsp->aggre2_clk);
  118. clk_disable_unprepare(adsp->xo);
  119. }
  120. static int adsp_stop(struct rproc *rproc)
  121. {
  122. struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
  123. int handover;
  124. int ret;
  125. ret = qcom_q6v5_request_stop(&adsp->q6v5);
  126. if (ret == -ETIMEDOUT)
  127. dev_err(adsp->dev, "timed out on wait\n");
  128. ret = qcom_scm_pas_shutdown(adsp->pas_id);
  129. if (ret)
  130. dev_err(adsp->dev, "failed to shutdown: %d\n", ret);
  131. handover = qcom_q6v5_unprepare(&adsp->q6v5);
  132. if (handover)
  133. qcom_pas_handover(&adsp->q6v5);
  134. return ret;
  135. }
  136. static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
  137. {
  138. struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
  139. int offset;
  140. offset = da - adsp->mem_reloc;
  141. if (offset < 0 || offset + len > adsp->mem_size)
  142. return NULL;
  143. return adsp->mem_region + offset;
  144. }
  145. static const struct rproc_ops adsp_ops = {
  146. .start = adsp_start,
  147. .stop = adsp_stop,
  148. .da_to_va = adsp_da_to_va,
  149. .parse_fw = qcom_register_dump_segments,
  150. .load = adsp_load,
  151. };
  152. static int adsp_init_clock(struct qcom_adsp *adsp)
  153. {
  154. int ret;
  155. adsp->xo = devm_clk_get(adsp->dev, "xo");
  156. if (IS_ERR(adsp->xo)) {
  157. ret = PTR_ERR(adsp->xo);
  158. if (ret != -EPROBE_DEFER)
  159. dev_err(adsp->dev, "failed to get xo clock");
  160. return ret;
  161. }
  162. if (adsp->has_aggre2_clk) {
  163. adsp->aggre2_clk = devm_clk_get(adsp->dev, "aggre2");
  164. if (IS_ERR(adsp->aggre2_clk)) {
  165. ret = PTR_ERR(adsp->aggre2_clk);
  166. if (ret != -EPROBE_DEFER)
  167. dev_err(adsp->dev,
  168. "failed to get aggre2 clock");
  169. return ret;
  170. }
  171. }
  172. return 0;
  173. }
  174. static int adsp_init_regulator(struct qcom_adsp *adsp)
  175. {
  176. adsp->cx_supply = devm_regulator_get(adsp->dev, "cx");
  177. if (IS_ERR(adsp->cx_supply))
  178. return PTR_ERR(adsp->cx_supply);
  179. regulator_set_load(adsp->cx_supply, 100000);
  180. adsp->px_supply = devm_regulator_get(adsp->dev, "px");
  181. return PTR_ERR_OR_ZERO(adsp->px_supply);
  182. }
  183. static int adsp_alloc_memory_region(struct qcom_adsp *adsp)
  184. {
  185. struct device_node *node;
  186. struct resource r;
  187. int ret;
  188. node = of_parse_phandle(adsp->dev->of_node, "memory-region", 0);
  189. if (!node) {
  190. dev_err(adsp->dev, "no memory-region specified\n");
  191. return -EINVAL;
  192. }
  193. ret = of_address_to_resource(node, 0, &r);
  194. if (ret)
  195. return ret;
  196. adsp->mem_phys = adsp->mem_reloc = r.start;
  197. adsp->mem_size = resource_size(&r);
  198. adsp->mem_region = devm_ioremap_wc(adsp->dev, adsp->mem_phys, adsp->mem_size);
  199. if (!adsp->mem_region) {
  200. dev_err(adsp->dev, "unable to map memory region: %pa+%zx\n",
  201. &r.start, adsp->mem_size);
  202. return -EBUSY;
  203. }
  204. return 0;
  205. }
  206. static int adsp_probe(struct platform_device *pdev)
  207. {
  208. const struct adsp_data *desc;
  209. struct qcom_adsp *adsp;
  210. struct rproc *rproc;
  211. int ret;
  212. desc = of_device_get_match_data(&pdev->dev);
  213. if (!desc)
  214. return -EINVAL;
  215. if (!qcom_scm_is_available())
  216. return -EPROBE_DEFER;
  217. rproc = rproc_alloc(&pdev->dev, pdev->name, &adsp_ops,
  218. desc->firmware_name, sizeof(*adsp));
  219. if (!rproc) {
  220. dev_err(&pdev->dev, "unable to allocate remoteproc\n");
  221. return -ENOMEM;
  222. }
  223. adsp = (struct qcom_adsp *)rproc->priv;
  224. adsp->dev = &pdev->dev;
  225. adsp->rproc = rproc;
  226. adsp->pas_id = desc->pas_id;
  227. adsp->has_aggre2_clk = desc->has_aggre2_clk;
  228. platform_set_drvdata(pdev, adsp);
  229. ret = adsp_alloc_memory_region(adsp);
  230. if (ret)
  231. goto free_rproc;
  232. ret = adsp_init_clock(adsp);
  233. if (ret)
  234. goto free_rproc;
  235. ret = adsp_init_regulator(adsp);
  236. if (ret)
  237. goto free_rproc;
  238. ret = qcom_q6v5_init(&adsp->q6v5, pdev, rproc, desc->crash_reason_smem,
  239. qcom_pas_handover);
  240. if (ret)
  241. goto free_rproc;
  242. qcom_add_glink_subdev(rproc, &adsp->glink_subdev);
  243. qcom_add_smd_subdev(rproc, &adsp->smd_subdev);
  244. qcom_add_ssr_subdev(rproc, &adsp->ssr_subdev, desc->ssr_name);
  245. adsp->sysmon = qcom_add_sysmon_subdev(rproc,
  246. desc->sysmon_name,
  247. desc->ssctl_id);
  248. ret = rproc_add(rproc);
  249. if (ret)
  250. goto free_rproc;
  251. return 0;
  252. free_rproc:
  253. rproc_free(rproc);
  254. return ret;
  255. }
  256. static int adsp_remove(struct platform_device *pdev)
  257. {
  258. struct qcom_adsp *adsp = platform_get_drvdata(pdev);
  259. rproc_del(adsp->rproc);
  260. qcom_remove_glink_subdev(adsp->rproc, &adsp->glink_subdev);
  261. qcom_remove_sysmon_subdev(adsp->sysmon);
  262. qcom_remove_smd_subdev(adsp->rproc, &adsp->smd_subdev);
  263. qcom_remove_ssr_subdev(adsp->rproc, &adsp->ssr_subdev);
  264. rproc_free(adsp->rproc);
  265. return 0;
  266. }
  267. static const struct adsp_data adsp_resource_init = {
  268. .crash_reason_smem = 423,
  269. .firmware_name = "adsp.mdt",
  270. .pas_id = 1,
  271. .has_aggre2_clk = false,
  272. .ssr_name = "lpass",
  273. .sysmon_name = "adsp",
  274. .ssctl_id = 0x14,
  275. };
  276. static const struct adsp_data slpi_resource_init = {
  277. .crash_reason_smem = 424,
  278. .firmware_name = "slpi.mdt",
  279. .pas_id = 12,
  280. .has_aggre2_clk = true,
  281. .ssr_name = "dsps",
  282. .sysmon_name = "slpi",
  283. .ssctl_id = 0x16,
  284. };
  285. static const struct of_device_id adsp_of_match[] = {
  286. { .compatible = "qcom,msm8974-adsp-pil", .data = &adsp_resource_init},
  287. { .compatible = "qcom,msm8996-adsp-pil", .data = &adsp_resource_init},
  288. { .compatible = "qcom,msm8996-slpi-pil", .data = &slpi_resource_init},
  289. { },
  290. };
  291. MODULE_DEVICE_TABLE(of, adsp_of_match);
  292. static struct platform_driver adsp_driver = {
  293. .probe = adsp_probe,
  294. .remove = adsp_remove,
  295. .driver = {
  296. .name = "qcom_adsp_pil",
  297. .of_match_table = adsp_of_match,
  298. },
  299. };
  300. module_platform_driver(adsp_driver);
  301. MODULE_DESCRIPTION("Qualcomm MSM8974/MSM8996 ADSP Peripherial Image Loader");
  302. MODULE_LICENSE("GPL v2");