qcom_q6v5_adsp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Qualcomm Technology Inc. ADSP Peripheral Image Loader for SDM845.
  4. * Copyright (c) 2018, The Linux Foundation. All rights reserved.
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #include <linux/firmware.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_domain.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/regmap.h>
  21. #include <linux/remoteproc.h>
  22. #include <linux/reset.h>
  23. #include <linux/soc/qcom/mdt_loader.h>
  24. #include <linux/soc/qcom/smem.h>
  25. #include <linux/soc/qcom/smem_state.h>
  26. #include "qcom_common.h"
  27. #include "qcom_q6v5.h"
  28. #include "remoteproc_internal.h"
  29. /* time out value */
  30. #define ACK_TIMEOUT 1000
  31. #define BOOT_FSM_TIMEOUT 10000
  32. /* mask values */
  33. #define EVB_MASK GENMASK(27, 4)
  34. /*QDSP6SS register offsets*/
  35. #define RST_EVB_REG 0x10
  36. #define CORE_START_REG 0x400
  37. #define BOOT_CMD_REG 0x404
  38. #define BOOT_STATUS_REG 0x408
  39. #define RET_CFG_REG 0x1C
  40. /*TCSR register offsets*/
  41. #define LPASS_MASTER_IDLE_REG 0x8
  42. #define LPASS_HALTACK_REG 0x4
  43. #define LPASS_PWR_ON_REG 0x10
  44. #define LPASS_HALTREQ_REG 0x0
  45. #define QDSP6SS_XO_CBCR 0x38
  46. #define QDSP6SS_CORE_CBCR 0x20
  47. #define QDSP6SS_SLEEP_CBCR 0x3c
  48. struct adsp_pil_data {
  49. int crash_reason_smem;
  50. const char *firmware_name;
  51. const char *ssr_name;
  52. const char *sysmon_name;
  53. int ssctl_id;
  54. const char **clk_ids;
  55. int num_clks;
  56. };
  57. struct qcom_adsp {
  58. struct device *dev;
  59. struct rproc *rproc;
  60. struct qcom_q6v5 q6v5;
  61. struct clk *xo;
  62. int num_clks;
  63. struct clk_bulk_data *clks;
  64. void __iomem *qdsp6ss_base;
  65. struct reset_control *pdc_sync_reset;
  66. struct reset_control *restart;
  67. struct regmap *halt_map;
  68. unsigned int halt_lpass;
  69. int crash_reason_smem;
  70. struct completion start_done;
  71. struct completion stop_done;
  72. phys_addr_t mem_phys;
  73. phys_addr_t mem_reloc;
  74. void *mem_region;
  75. size_t mem_size;
  76. struct qcom_rproc_glink glink_subdev;
  77. struct qcom_rproc_ssr ssr_subdev;
  78. struct qcom_sysmon *sysmon;
  79. };
  80. static int qcom_adsp_shutdown(struct qcom_adsp *adsp)
  81. {
  82. unsigned long timeout;
  83. unsigned int val;
  84. int ret;
  85. /* Reset the retention logic */
  86. val = readl(adsp->qdsp6ss_base + RET_CFG_REG);
  87. val |= 0x1;
  88. writel(val, adsp->qdsp6ss_base + RET_CFG_REG);
  89. clk_bulk_disable_unprepare(adsp->num_clks, adsp->clks);
  90. /* QDSP6 master port needs to be explicitly halted */
  91. ret = regmap_read(adsp->halt_map,
  92. adsp->halt_lpass + LPASS_PWR_ON_REG, &val);
  93. if (ret || !val)
  94. goto reset;
  95. ret = regmap_read(adsp->halt_map,
  96. adsp->halt_lpass + LPASS_MASTER_IDLE_REG,
  97. &val);
  98. if (ret || val)
  99. goto reset;
  100. regmap_write(adsp->halt_map,
  101. adsp->halt_lpass + LPASS_HALTREQ_REG, 1);
  102. /* Wait for halt ACK from QDSP6 */
  103. timeout = jiffies + msecs_to_jiffies(ACK_TIMEOUT);
  104. for (;;) {
  105. ret = regmap_read(adsp->halt_map,
  106. adsp->halt_lpass + LPASS_HALTACK_REG, &val);
  107. if (ret || val || time_after(jiffies, timeout))
  108. break;
  109. usleep_range(1000, 1100);
  110. }
  111. ret = regmap_read(adsp->halt_map,
  112. adsp->halt_lpass + LPASS_MASTER_IDLE_REG, &val);
  113. if (ret || !val)
  114. dev_err(adsp->dev, "port failed halt\n");
  115. reset:
  116. /* Assert the LPASS PDC Reset */
  117. reset_control_assert(adsp->pdc_sync_reset);
  118. /* Place the LPASS processor into reset */
  119. reset_control_assert(adsp->restart);
  120. /* wait after asserting subsystem restart from AOSS */
  121. usleep_range(200, 300);
  122. /* Clear the halt request for the AXIM and AHBM for Q6 */
  123. regmap_write(adsp->halt_map, adsp->halt_lpass + LPASS_HALTREQ_REG, 0);
  124. /* De-assert the LPASS PDC Reset */
  125. reset_control_deassert(adsp->pdc_sync_reset);
  126. /* Remove the LPASS reset */
  127. reset_control_deassert(adsp->restart);
  128. /* wait after de-asserting subsystem restart from AOSS */
  129. usleep_range(200, 300);
  130. return 0;
  131. }
  132. static int adsp_load(struct rproc *rproc, const struct firmware *fw)
  133. {
  134. struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
  135. return qcom_mdt_load_no_init(adsp->dev, fw, rproc->firmware, 0,
  136. adsp->mem_region, adsp->mem_phys, adsp->mem_size,
  137. &adsp->mem_reloc);
  138. }
  139. static int adsp_start(struct rproc *rproc)
  140. {
  141. struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
  142. int ret;
  143. unsigned int val;
  144. qcom_q6v5_prepare(&adsp->q6v5);
  145. ret = clk_prepare_enable(adsp->xo);
  146. if (ret)
  147. goto disable_irqs;
  148. dev_pm_genpd_set_performance_state(adsp->dev, INT_MAX);
  149. ret = pm_runtime_get_sync(adsp->dev);
  150. if (ret) {
  151. pm_runtime_put_noidle(adsp->dev);
  152. goto disable_xo_clk;
  153. }
  154. ret = clk_bulk_prepare_enable(adsp->num_clks, adsp->clks);
  155. if (ret) {
  156. dev_err(adsp->dev, "adsp clk_enable failed\n");
  157. goto disable_power_domain;
  158. }
  159. /* Enable the XO clock */
  160. writel(1, adsp->qdsp6ss_base + QDSP6SS_XO_CBCR);
  161. /* Enable the QDSP6SS sleep clock */
  162. writel(1, adsp->qdsp6ss_base + QDSP6SS_SLEEP_CBCR);
  163. /* Enable the QDSP6 core clock */
  164. writel(1, adsp->qdsp6ss_base + QDSP6SS_CORE_CBCR);
  165. /* Program boot address */
  166. writel(adsp->mem_phys >> 4, adsp->qdsp6ss_base + RST_EVB_REG);
  167. /* De-assert QDSP6 stop core. QDSP6 will execute after out of reset */
  168. writel(0x1, adsp->qdsp6ss_base + CORE_START_REG);
  169. /* Trigger boot FSM to start QDSP6 */
  170. writel(0x1, adsp->qdsp6ss_base + BOOT_CMD_REG);
  171. /* Wait for core to come out of reset */
  172. ret = readl_poll_timeout(adsp->qdsp6ss_base + BOOT_STATUS_REG,
  173. val, (val & BIT(0)) != 0, 10, BOOT_FSM_TIMEOUT);
  174. if (ret) {
  175. dev_err(adsp->dev, "failed to bootup adsp\n");
  176. goto disable_adsp_clks;
  177. }
  178. ret = qcom_q6v5_wait_for_start(&adsp->q6v5, msecs_to_jiffies(5 * HZ));
  179. if (ret == -ETIMEDOUT) {
  180. dev_err(adsp->dev, "start timed out\n");
  181. goto disable_adsp_clks;
  182. }
  183. return 0;
  184. disable_adsp_clks:
  185. clk_bulk_disable_unprepare(adsp->num_clks, adsp->clks);
  186. disable_power_domain:
  187. dev_pm_genpd_set_performance_state(adsp->dev, 0);
  188. pm_runtime_put(adsp->dev);
  189. disable_xo_clk:
  190. clk_disable_unprepare(adsp->xo);
  191. disable_irqs:
  192. qcom_q6v5_unprepare(&adsp->q6v5);
  193. return ret;
  194. }
  195. static void qcom_adsp_pil_handover(struct qcom_q6v5 *q6v5)
  196. {
  197. struct qcom_adsp *adsp = container_of(q6v5, struct qcom_adsp, q6v5);
  198. clk_disable_unprepare(adsp->xo);
  199. dev_pm_genpd_set_performance_state(adsp->dev, 0);
  200. pm_runtime_put(adsp->dev);
  201. }
  202. static int adsp_stop(struct rproc *rproc)
  203. {
  204. struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
  205. int handover;
  206. int ret;
  207. ret = qcom_q6v5_request_stop(&adsp->q6v5);
  208. if (ret == -ETIMEDOUT)
  209. dev_err(adsp->dev, "timed out on wait\n");
  210. ret = qcom_adsp_shutdown(adsp);
  211. if (ret)
  212. dev_err(adsp->dev, "failed to shutdown: %d\n", ret);
  213. handover = qcom_q6v5_unprepare(&adsp->q6v5);
  214. if (handover)
  215. qcom_adsp_pil_handover(&adsp->q6v5);
  216. return ret;
  217. }
  218. static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
  219. {
  220. struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
  221. int offset;
  222. offset = da - adsp->mem_reloc;
  223. if (offset < 0 || offset + len > adsp->mem_size)
  224. return NULL;
  225. return adsp->mem_region + offset;
  226. }
  227. static const struct rproc_ops adsp_ops = {
  228. .start = adsp_start,
  229. .stop = adsp_stop,
  230. .da_to_va = adsp_da_to_va,
  231. .parse_fw = qcom_register_dump_segments,
  232. .load = adsp_load,
  233. };
  234. static int adsp_init_clock(struct qcom_adsp *adsp, const char **clk_ids)
  235. {
  236. int num_clks = 0;
  237. int i, ret;
  238. adsp->xo = devm_clk_get(adsp->dev, "xo");
  239. if (IS_ERR(adsp->xo)) {
  240. ret = PTR_ERR(adsp->xo);
  241. if (ret != -EPROBE_DEFER)
  242. dev_err(adsp->dev, "failed to get xo clock");
  243. return ret;
  244. }
  245. for (i = 0; clk_ids[i]; i++)
  246. num_clks++;
  247. adsp->num_clks = num_clks;
  248. adsp->clks = devm_kcalloc(adsp->dev, adsp->num_clks,
  249. sizeof(*adsp->clks), GFP_KERNEL);
  250. if (!adsp->clks)
  251. return -ENOMEM;
  252. for (i = 0; i < adsp->num_clks; i++)
  253. adsp->clks[i].id = clk_ids[i];
  254. return devm_clk_bulk_get(adsp->dev, adsp->num_clks, adsp->clks);
  255. }
  256. static int adsp_init_reset(struct qcom_adsp *adsp)
  257. {
  258. adsp->pdc_sync_reset = devm_reset_control_get_optional_exclusive(adsp->dev,
  259. "pdc_sync");
  260. if (IS_ERR(adsp->pdc_sync_reset)) {
  261. dev_err(adsp->dev, "failed to acquire pdc_sync reset\n");
  262. return PTR_ERR(adsp->pdc_sync_reset);
  263. }
  264. adsp->restart = devm_reset_control_get_optional_exclusive(adsp->dev, "restart");
  265. /* Fall back to the old "cc_lpass" if "restart" is absent */
  266. if (!adsp->restart)
  267. adsp->restart = devm_reset_control_get_exclusive(adsp->dev, "cc_lpass");
  268. if (IS_ERR(adsp->restart)) {
  269. dev_err(adsp->dev, "failed to acquire restart\n");
  270. return PTR_ERR(adsp->restart);
  271. }
  272. return 0;
  273. }
  274. static int adsp_init_mmio(struct qcom_adsp *adsp,
  275. struct platform_device *pdev)
  276. {
  277. struct device_node *syscon;
  278. int ret;
  279. adsp->qdsp6ss_base = devm_platform_ioremap_resource(pdev, 0);
  280. if (IS_ERR(adsp->qdsp6ss_base)) {
  281. dev_err(adsp->dev, "failed to map QDSP6SS registers\n");
  282. return PTR_ERR(adsp->qdsp6ss_base);
  283. }
  284. syscon = of_parse_phandle(pdev->dev.of_node, "qcom,halt-regs", 0);
  285. if (!syscon) {
  286. dev_err(&pdev->dev, "failed to parse qcom,halt-regs\n");
  287. return -EINVAL;
  288. }
  289. adsp->halt_map = syscon_node_to_regmap(syscon);
  290. of_node_put(syscon);
  291. if (IS_ERR(adsp->halt_map))
  292. return PTR_ERR(adsp->halt_map);
  293. ret = of_property_read_u32_index(pdev->dev.of_node, "qcom,halt-regs",
  294. 1, &adsp->halt_lpass);
  295. if (ret < 0) {
  296. dev_err(&pdev->dev, "no offset in syscon\n");
  297. return ret;
  298. }
  299. return 0;
  300. }
  301. static int adsp_alloc_memory_region(struct qcom_adsp *adsp)
  302. {
  303. struct device_node *node;
  304. struct resource r;
  305. int ret;
  306. node = of_parse_phandle(adsp->dev->of_node, "memory-region", 0);
  307. if (!node) {
  308. dev_err(adsp->dev, "no memory-region specified\n");
  309. return -EINVAL;
  310. }
  311. ret = of_address_to_resource(node, 0, &r);
  312. if (ret)
  313. return ret;
  314. adsp->mem_phys = adsp->mem_reloc = r.start;
  315. adsp->mem_size = resource_size(&r);
  316. adsp->mem_region = devm_ioremap_wc(adsp->dev,
  317. adsp->mem_phys, adsp->mem_size);
  318. if (!adsp->mem_region) {
  319. dev_err(adsp->dev, "unable to map memory region: %pa+%zx\n",
  320. &r.start, adsp->mem_size);
  321. return -EBUSY;
  322. }
  323. return 0;
  324. }
  325. static int adsp_probe(struct platform_device *pdev)
  326. {
  327. const struct adsp_pil_data *desc;
  328. struct qcom_adsp *adsp;
  329. struct rproc *rproc;
  330. int ret;
  331. desc = of_device_get_match_data(&pdev->dev);
  332. if (!desc)
  333. return -EINVAL;
  334. rproc = rproc_alloc(&pdev->dev, pdev->name, &adsp_ops,
  335. desc->firmware_name, sizeof(*adsp));
  336. if (!rproc) {
  337. dev_err(&pdev->dev, "unable to allocate remoteproc\n");
  338. return -ENOMEM;
  339. }
  340. adsp = (struct qcom_adsp *)rproc->priv;
  341. adsp->dev = &pdev->dev;
  342. adsp->rproc = rproc;
  343. platform_set_drvdata(pdev, adsp);
  344. ret = adsp_alloc_memory_region(adsp);
  345. if (ret)
  346. goto free_rproc;
  347. ret = adsp_init_clock(adsp, desc->clk_ids);
  348. if (ret)
  349. goto free_rproc;
  350. pm_runtime_enable(adsp->dev);
  351. ret = adsp_init_reset(adsp);
  352. if (ret)
  353. goto disable_pm;
  354. ret = adsp_init_mmio(adsp, pdev);
  355. if (ret)
  356. goto disable_pm;
  357. ret = qcom_q6v5_init(&adsp->q6v5, pdev, rproc, desc->crash_reason_smem,
  358. qcom_adsp_pil_handover);
  359. if (ret)
  360. goto disable_pm;
  361. qcom_add_glink_subdev(rproc, &adsp->glink_subdev);
  362. qcom_add_ssr_subdev(rproc, &adsp->ssr_subdev, desc->ssr_name);
  363. adsp->sysmon = qcom_add_sysmon_subdev(rproc,
  364. desc->sysmon_name,
  365. desc->ssctl_id);
  366. if (IS_ERR(adsp->sysmon)) {
  367. ret = PTR_ERR(adsp->sysmon);
  368. goto disable_pm;
  369. }
  370. ret = rproc_add(rproc);
  371. if (ret)
  372. goto disable_pm;
  373. return 0;
  374. disable_pm:
  375. pm_runtime_disable(adsp->dev);
  376. free_rproc:
  377. rproc_free(rproc);
  378. return ret;
  379. }
  380. static int adsp_remove(struct platform_device *pdev)
  381. {
  382. struct qcom_adsp *adsp = platform_get_drvdata(pdev);
  383. rproc_del(adsp->rproc);
  384. qcom_remove_glink_subdev(adsp->rproc, &adsp->glink_subdev);
  385. qcom_remove_sysmon_subdev(adsp->sysmon);
  386. qcom_remove_ssr_subdev(adsp->rproc, &adsp->ssr_subdev);
  387. pm_runtime_disable(adsp->dev);
  388. rproc_free(adsp->rproc);
  389. return 0;
  390. }
  391. static const struct adsp_pil_data adsp_resource_init = {
  392. .crash_reason_smem = 423,
  393. .firmware_name = "adsp.mdt",
  394. .ssr_name = "lpass",
  395. .sysmon_name = "adsp",
  396. .ssctl_id = 0x14,
  397. .clk_ids = (const char*[]) {
  398. "sway_cbcr", "lpass_ahbs_aon_cbcr", "lpass_ahbm_aon_cbcr",
  399. "qdsp6ss_xo", "qdsp6ss_sleep", "qdsp6ss_core", NULL
  400. },
  401. .num_clks = 7,
  402. };
  403. static const struct adsp_pil_data cdsp_resource_init = {
  404. .crash_reason_smem = 601,
  405. .firmware_name = "cdsp.mdt",
  406. .ssr_name = "cdsp",
  407. .sysmon_name = "cdsp",
  408. .ssctl_id = 0x17,
  409. .clk_ids = (const char*[]) {
  410. "sway", "tbu", "bimc", "ahb_aon", "q6ss_slave", "q6ss_master",
  411. "q6_axim", NULL
  412. },
  413. .num_clks = 7,
  414. };
  415. static const struct of_device_id adsp_of_match[] = {
  416. { .compatible = "qcom,qcs404-cdsp-pil", .data = &cdsp_resource_init },
  417. { .compatible = "qcom,sdm845-adsp-pil", .data = &adsp_resource_init },
  418. { },
  419. };
  420. MODULE_DEVICE_TABLE(of, adsp_of_match);
  421. static struct platform_driver adsp_pil_driver = {
  422. .probe = adsp_probe,
  423. .remove = adsp_remove,
  424. .driver = {
  425. .name = "qcom_q6v5_adsp",
  426. .of_match_table = adsp_of_match,
  427. },
  428. };
  429. module_platform_driver(adsp_pil_driver);
  430. MODULE_DESCRIPTION("QTI SDM845 ADSP Peripheral Image Loader");
  431. MODULE_LICENSE("GPL v2");