qcom_wcnss.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * Qualcomm Wireless Connectivity Subsystem Peripheral Image Loader
  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/delay.h>
  19. #include <linux/firmware.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/io.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/qcom_scm.h>
  28. #include <linux/regulator/consumer.h>
  29. #include <linux/remoteproc.h>
  30. #include <linux/soc/qcom/mdt_loader.h>
  31. #include <linux/soc/qcom/smem.h>
  32. #include <linux/soc/qcom/smem_state.h>
  33. #include <linux/rpmsg/qcom_smd.h>
  34. #include "qcom_common.h"
  35. #include "remoteproc_internal.h"
  36. #include "qcom_wcnss.h"
  37. #define WCNSS_CRASH_REASON_SMEM 422
  38. #define WCNSS_FIRMWARE_NAME "wcnss.mdt"
  39. #define WCNSS_PAS_ID 6
  40. #define WCNSS_SSCTL_ID 0x13
  41. #define WCNSS_SPARE_NVBIN_DLND BIT(25)
  42. #define WCNSS_PMU_IRIS_XO_CFG BIT(3)
  43. #define WCNSS_PMU_IRIS_XO_EN BIT(4)
  44. #define WCNSS_PMU_GC_BUS_MUX_SEL_TOP BIT(5)
  45. #define WCNSS_PMU_IRIS_XO_CFG_STS BIT(6) /* 1: in progress, 0: done */
  46. #define WCNSS_PMU_IRIS_RESET BIT(7)
  47. #define WCNSS_PMU_IRIS_RESET_STS BIT(8) /* 1: in progress, 0: done */
  48. #define WCNSS_PMU_IRIS_XO_READ BIT(9)
  49. #define WCNSS_PMU_IRIS_XO_READ_STS BIT(10)
  50. #define WCNSS_PMU_XO_MODE_MASK GENMASK(2, 1)
  51. #define WCNSS_PMU_XO_MODE_19p2 0
  52. #define WCNSS_PMU_XO_MODE_48 3
  53. struct wcnss_data {
  54. size_t pmu_offset;
  55. size_t spare_offset;
  56. const struct wcnss_vreg_info *vregs;
  57. size_t num_vregs;
  58. };
  59. struct qcom_wcnss {
  60. struct device *dev;
  61. struct rproc *rproc;
  62. void __iomem *pmu_cfg;
  63. void __iomem *spare_out;
  64. bool use_48mhz_xo;
  65. int wdog_irq;
  66. int fatal_irq;
  67. int ready_irq;
  68. int handover_irq;
  69. int stop_ack_irq;
  70. struct qcom_smem_state *state;
  71. unsigned stop_bit;
  72. struct mutex iris_lock;
  73. struct qcom_iris *iris;
  74. struct regulator_bulk_data *vregs;
  75. size_t num_vregs;
  76. struct completion start_done;
  77. struct completion stop_done;
  78. phys_addr_t mem_phys;
  79. phys_addr_t mem_reloc;
  80. void *mem_region;
  81. size_t mem_size;
  82. struct qcom_rproc_subdev smd_subdev;
  83. struct qcom_sysmon *sysmon;
  84. };
  85. static const struct wcnss_data riva_data = {
  86. .pmu_offset = 0x28,
  87. .spare_offset = 0xb4,
  88. .vregs = (struct wcnss_vreg_info[]) {
  89. { "vddmx", 1050000, 1150000, 0 },
  90. { "vddcx", 1050000, 1150000, 0 },
  91. { "vddpx", 1800000, 1800000, 0 },
  92. },
  93. .num_vregs = 3,
  94. };
  95. static const struct wcnss_data pronto_v1_data = {
  96. .pmu_offset = 0x1004,
  97. .spare_offset = 0x1088,
  98. .vregs = (struct wcnss_vreg_info[]) {
  99. { "vddmx", 950000, 1150000, 0 },
  100. { "vddcx", .super_turbo = true},
  101. { "vddpx", 1800000, 1800000, 0 },
  102. },
  103. .num_vregs = 3,
  104. };
  105. static const struct wcnss_data pronto_v2_data = {
  106. .pmu_offset = 0x1004,
  107. .spare_offset = 0x1088,
  108. .vregs = (struct wcnss_vreg_info[]) {
  109. { "vddmx", 1287500, 1287500, 0 },
  110. { "vddcx", .super_turbo = true },
  111. { "vddpx", 1800000, 1800000, 0 },
  112. },
  113. .num_vregs = 3,
  114. };
  115. void qcom_wcnss_assign_iris(struct qcom_wcnss *wcnss,
  116. struct qcom_iris *iris,
  117. bool use_48mhz_xo)
  118. {
  119. mutex_lock(&wcnss->iris_lock);
  120. wcnss->iris = iris;
  121. wcnss->use_48mhz_xo = use_48mhz_xo;
  122. mutex_unlock(&wcnss->iris_lock);
  123. }
  124. static int wcnss_load(struct rproc *rproc, const struct firmware *fw)
  125. {
  126. struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
  127. return qcom_mdt_load(wcnss->dev, fw, rproc->firmware, WCNSS_PAS_ID,
  128. wcnss->mem_region, wcnss->mem_phys,
  129. wcnss->mem_size, &wcnss->mem_reloc);
  130. }
  131. static void wcnss_indicate_nv_download(struct qcom_wcnss *wcnss)
  132. {
  133. u32 val;
  134. /* Indicate NV download capability */
  135. val = readl(wcnss->spare_out);
  136. val |= WCNSS_SPARE_NVBIN_DLND;
  137. writel(val, wcnss->spare_out);
  138. }
  139. static void wcnss_configure_iris(struct qcom_wcnss *wcnss)
  140. {
  141. u32 val;
  142. /* Clear PMU cfg register */
  143. writel(0, wcnss->pmu_cfg);
  144. val = WCNSS_PMU_GC_BUS_MUX_SEL_TOP | WCNSS_PMU_IRIS_XO_EN;
  145. writel(val, wcnss->pmu_cfg);
  146. /* Clear XO_MODE */
  147. val &= ~WCNSS_PMU_XO_MODE_MASK;
  148. if (wcnss->use_48mhz_xo)
  149. val |= WCNSS_PMU_XO_MODE_48 << 1;
  150. else
  151. val |= WCNSS_PMU_XO_MODE_19p2 << 1;
  152. writel(val, wcnss->pmu_cfg);
  153. /* Reset IRIS */
  154. val |= WCNSS_PMU_IRIS_RESET;
  155. writel(val, wcnss->pmu_cfg);
  156. /* Wait for PMU.iris_reg_reset_sts */
  157. while (readl(wcnss->pmu_cfg) & WCNSS_PMU_IRIS_RESET_STS)
  158. cpu_relax();
  159. /* Clear IRIS reset */
  160. val &= ~WCNSS_PMU_IRIS_RESET;
  161. writel(val, wcnss->pmu_cfg);
  162. /* Start IRIS XO configuration */
  163. val |= WCNSS_PMU_IRIS_XO_CFG;
  164. writel(val, wcnss->pmu_cfg);
  165. /* Wait for XO configuration to finish */
  166. while (readl(wcnss->pmu_cfg) & WCNSS_PMU_IRIS_XO_CFG_STS)
  167. cpu_relax();
  168. /* Stop IRIS XO configuration */
  169. val &= ~WCNSS_PMU_GC_BUS_MUX_SEL_TOP;
  170. val &= ~WCNSS_PMU_IRIS_XO_CFG;
  171. writel(val, wcnss->pmu_cfg);
  172. /* Add some delay for XO to settle */
  173. msleep(20);
  174. }
  175. static int wcnss_start(struct rproc *rproc)
  176. {
  177. struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
  178. int ret;
  179. mutex_lock(&wcnss->iris_lock);
  180. if (!wcnss->iris) {
  181. dev_err(wcnss->dev, "no iris registered\n");
  182. ret = -EINVAL;
  183. goto release_iris_lock;
  184. }
  185. ret = regulator_bulk_enable(wcnss->num_vregs, wcnss->vregs);
  186. if (ret)
  187. goto release_iris_lock;
  188. ret = qcom_iris_enable(wcnss->iris);
  189. if (ret)
  190. goto disable_regulators;
  191. wcnss_indicate_nv_download(wcnss);
  192. wcnss_configure_iris(wcnss);
  193. ret = qcom_scm_pas_auth_and_reset(WCNSS_PAS_ID);
  194. if (ret) {
  195. dev_err(wcnss->dev,
  196. "failed to authenticate image and release reset\n");
  197. goto disable_iris;
  198. }
  199. ret = wait_for_completion_timeout(&wcnss->start_done,
  200. msecs_to_jiffies(5000));
  201. if (wcnss->ready_irq > 0 && ret == 0) {
  202. /* We have a ready_irq, but it didn't fire in time. */
  203. dev_err(wcnss->dev, "start timed out\n");
  204. qcom_scm_pas_shutdown(WCNSS_PAS_ID);
  205. ret = -ETIMEDOUT;
  206. goto disable_iris;
  207. }
  208. ret = 0;
  209. disable_iris:
  210. qcom_iris_disable(wcnss->iris);
  211. disable_regulators:
  212. regulator_bulk_disable(wcnss->num_vregs, wcnss->vregs);
  213. release_iris_lock:
  214. mutex_unlock(&wcnss->iris_lock);
  215. return ret;
  216. }
  217. static int wcnss_stop(struct rproc *rproc)
  218. {
  219. struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
  220. int ret;
  221. if (wcnss->state) {
  222. qcom_smem_state_update_bits(wcnss->state,
  223. BIT(wcnss->stop_bit),
  224. BIT(wcnss->stop_bit));
  225. ret = wait_for_completion_timeout(&wcnss->stop_done,
  226. msecs_to_jiffies(5000));
  227. if (ret == 0)
  228. dev_err(wcnss->dev, "timed out on wait\n");
  229. qcom_smem_state_update_bits(wcnss->state,
  230. BIT(wcnss->stop_bit),
  231. 0);
  232. }
  233. ret = qcom_scm_pas_shutdown(WCNSS_PAS_ID);
  234. if (ret)
  235. dev_err(wcnss->dev, "failed to shutdown: %d\n", ret);
  236. return ret;
  237. }
  238. static void *wcnss_da_to_va(struct rproc *rproc, u64 da, int len)
  239. {
  240. struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
  241. int offset;
  242. offset = da - wcnss->mem_reloc;
  243. if (offset < 0 || offset + len > wcnss->mem_size)
  244. return NULL;
  245. return wcnss->mem_region + offset;
  246. }
  247. static const struct rproc_ops wcnss_ops = {
  248. .start = wcnss_start,
  249. .stop = wcnss_stop,
  250. .da_to_va = wcnss_da_to_va,
  251. .parse_fw = qcom_register_dump_segments,
  252. .load = wcnss_load,
  253. };
  254. static irqreturn_t wcnss_wdog_interrupt(int irq, void *dev)
  255. {
  256. struct qcom_wcnss *wcnss = dev;
  257. rproc_report_crash(wcnss->rproc, RPROC_WATCHDOG);
  258. return IRQ_HANDLED;
  259. }
  260. static irqreturn_t wcnss_fatal_interrupt(int irq, void *dev)
  261. {
  262. struct qcom_wcnss *wcnss = dev;
  263. size_t len;
  264. char *msg;
  265. msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, WCNSS_CRASH_REASON_SMEM, &len);
  266. if (!IS_ERR(msg) && len > 0 && msg[0])
  267. dev_err(wcnss->dev, "fatal error received: %s\n", msg);
  268. rproc_report_crash(wcnss->rproc, RPROC_FATAL_ERROR);
  269. return IRQ_HANDLED;
  270. }
  271. static irqreturn_t wcnss_ready_interrupt(int irq, void *dev)
  272. {
  273. struct qcom_wcnss *wcnss = dev;
  274. complete(&wcnss->start_done);
  275. return IRQ_HANDLED;
  276. }
  277. static irqreturn_t wcnss_handover_interrupt(int irq, void *dev)
  278. {
  279. /*
  280. * XXX: At this point we're supposed to release the resources that we
  281. * have been holding on behalf of the WCNSS. Unfortunately this
  282. * interrupt comes way before the other side seems to be done.
  283. *
  284. * So we're currently relying on the ready interrupt firing later then
  285. * this and we just disable the resources at the end of wcnss_start().
  286. */
  287. return IRQ_HANDLED;
  288. }
  289. static irqreturn_t wcnss_stop_ack_interrupt(int irq, void *dev)
  290. {
  291. struct qcom_wcnss *wcnss = dev;
  292. complete(&wcnss->stop_done);
  293. return IRQ_HANDLED;
  294. }
  295. static int wcnss_init_regulators(struct qcom_wcnss *wcnss,
  296. const struct wcnss_vreg_info *info,
  297. int num_vregs)
  298. {
  299. struct regulator_bulk_data *bulk;
  300. int ret;
  301. int i;
  302. bulk = devm_kcalloc(wcnss->dev,
  303. num_vregs, sizeof(struct regulator_bulk_data),
  304. GFP_KERNEL);
  305. if (!bulk)
  306. return -ENOMEM;
  307. for (i = 0; i < num_vregs; i++)
  308. bulk[i].supply = info[i].name;
  309. ret = devm_regulator_bulk_get(wcnss->dev, num_vregs, bulk);
  310. if (ret)
  311. return ret;
  312. for (i = 0; i < num_vregs; i++) {
  313. if (info[i].max_voltage)
  314. regulator_set_voltage(bulk[i].consumer,
  315. info[i].min_voltage,
  316. info[i].max_voltage);
  317. if (info[i].load_uA)
  318. regulator_set_load(bulk[i].consumer, info[i].load_uA);
  319. }
  320. wcnss->vregs = bulk;
  321. wcnss->num_vregs = num_vregs;
  322. return 0;
  323. }
  324. static int wcnss_request_irq(struct qcom_wcnss *wcnss,
  325. struct platform_device *pdev,
  326. const char *name,
  327. bool optional,
  328. irq_handler_t thread_fn)
  329. {
  330. int ret;
  331. ret = platform_get_irq_byname(pdev, name);
  332. if (ret < 0 && optional) {
  333. dev_dbg(&pdev->dev, "no %s IRQ defined, ignoring\n", name);
  334. return 0;
  335. } else if (ret < 0) {
  336. dev_err(&pdev->dev, "no %s IRQ defined\n", name);
  337. return ret;
  338. }
  339. ret = devm_request_threaded_irq(&pdev->dev, ret,
  340. NULL, thread_fn,
  341. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  342. "wcnss", wcnss);
  343. if (ret)
  344. dev_err(&pdev->dev, "request %s IRQ failed\n", name);
  345. return ret;
  346. }
  347. static int wcnss_alloc_memory_region(struct qcom_wcnss *wcnss)
  348. {
  349. struct device_node *node;
  350. struct resource r;
  351. int ret;
  352. node = of_parse_phandle(wcnss->dev->of_node, "memory-region", 0);
  353. if (!node) {
  354. dev_err(wcnss->dev, "no memory-region specified\n");
  355. return -EINVAL;
  356. }
  357. ret = of_address_to_resource(node, 0, &r);
  358. if (ret)
  359. return ret;
  360. wcnss->mem_phys = wcnss->mem_reloc = r.start;
  361. wcnss->mem_size = resource_size(&r);
  362. wcnss->mem_region = devm_ioremap_wc(wcnss->dev, wcnss->mem_phys, wcnss->mem_size);
  363. if (!wcnss->mem_region) {
  364. dev_err(wcnss->dev, "unable to map memory region: %pa+%zx\n",
  365. &r.start, wcnss->mem_size);
  366. return -EBUSY;
  367. }
  368. return 0;
  369. }
  370. static int wcnss_probe(struct platform_device *pdev)
  371. {
  372. const struct wcnss_data *data;
  373. struct qcom_wcnss *wcnss;
  374. struct resource *res;
  375. struct rproc *rproc;
  376. void __iomem *mmio;
  377. int ret;
  378. data = of_device_get_match_data(&pdev->dev);
  379. if (!qcom_scm_is_available())
  380. return -EPROBE_DEFER;
  381. if (!qcom_scm_pas_supported(WCNSS_PAS_ID)) {
  382. dev_err(&pdev->dev, "PAS is not available for WCNSS\n");
  383. return -ENXIO;
  384. }
  385. rproc = rproc_alloc(&pdev->dev, pdev->name, &wcnss_ops,
  386. WCNSS_FIRMWARE_NAME, sizeof(*wcnss));
  387. if (!rproc) {
  388. dev_err(&pdev->dev, "unable to allocate remoteproc\n");
  389. return -ENOMEM;
  390. }
  391. wcnss = (struct qcom_wcnss *)rproc->priv;
  392. wcnss->dev = &pdev->dev;
  393. wcnss->rproc = rproc;
  394. platform_set_drvdata(pdev, wcnss);
  395. init_completion(&wcnss->start_done);
  396. init_completion(&wcnss->stop_done);
  397. mutex_init(&wcnss->iris_lock);
  398. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pmu");
  399. mmio = devm_ioremap_resource(&pdev->dev, res);
  400. if (IS_ERR(mmio)) {
  401. ret = PTR_ERR(mmio);
  402. goto free_rproc;
  403. };
  404. ret = wcnss_alloc_memory_region(wcnss);
  405. if (ret)
  406. goto free_rproc;
  407. wcnss->pmu_cfg = mmio + data->pmu_offset;
  408. wcnss->spare_out = mmio + data->spare_offset;
  409. ret = wcnss_init_regulators(wcnss, data->vregs, data->num_vregs);
  410. if (ret)
  411. goto free_rproc;
  412. ret = wcnss_request_irq(wcnss, pdev, "wdog", false, wcnss_wdog_interrupt);
  413. if (ret < 0)
  414. goto free_rproc;
  415. wcnss->wdog_irq = ret;
  416. ret = wcnss_request_irq(wcnss, pdev, "fatal", false, wcnss_fatal_interrupt);
  417. if (ret < 0)
  418. goto free_rproc;
  419. wcnss->fatal_irq = ret;
  420. ret = wcnss_request_irq(wcnss, pdev, "ready", true, wcnss_ready_interrupt);
  421. if (ret < 0)
  422. goto free_rproc;
  423. wcnss->ready_irq = ret;
  424. ret = wcnss_request_irq(wcnss, pdev, "handover", true, wcnss_handover_interrupt);
  425. if (ret < 0)
  426. goto free_rproc;
  427. wcnss->handover_irq = ret;
  428. ret = wcnss_request_irq(wcnss, pdev, "stop-ack", true, wcnss_stop_ack_interrupt);
  429. if (ret < 0)
  430. goto free_rproc;
  431. wcnss->stop_ack_irq = ret;
  432. if (wcnss->stop_ack_irq) {
  433. wcnss->state = qcom_smem_state_get(&pdev->dev, "stop",
  434. &wcnss->stop_bit);
  435. if (IS_ERR(wcnss->state)) {
  436. ret = PTR_ERR(wcnss->state);
  437. goto free_rproc;
  438. }
  439. }
  440. qcom_add_smd_subdev(rproc, &wcnss->smd_subdev);
  441. wcnss->sysmon = qcom_add_sysmon_subdev(rproc, "wcnss", WCNSS_SSCTL_ID);
  442. ret = rproc_add(rproc);
  443. if (ret)
  444. goto free_rproc;
  445. return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  446. free_rproc:
  447. rproc_free(rproc);
  448. return ret;
  449. }
  450. static int wcnss_remove(struct platform_device *pdev)
  451. {
  452. struct qcom_wcnss *wcnss = platform_get_drvdata(pdev);
  453. of_platform_depopulate(&pdev->dev);
  454. qcom_smem_state_put(wcnss->state);
  455. rproc_del(wcnss->rproc);
  456. qcom_remove_sysmon_subdev(wcnss->sysmon);
  457. qcom_remove_smd_subdev(wcnss->rproc, &wcnss->smd_subdev);
  458. rproc_free(wcnss->rproc);
  459. return 0;
  460. }
  461. static const struct of_device_id wcnss_of_match[] = {
  462. { .compatible = "qcom,riva-pil", &riva_data },
  463. { .compatible = "qcom,pronto-v1-pil", &pronto_v1_data },
  464. { .compatible = "qcom,pronto-v2-pil", &pronto_v2_data },
  465. { },
  466. };
  467. MODULE_DEVICE_TABLE(of, wcnss_of_match);
  468. static struct platform_driver wcnss_driver = {
  469. .probe = wcnss_probe,
  470. .remove = wcnss_remove,
  471. .driver = {
  472. .name = "qcom-wcnss-pil",
  473. .of_match_table = wcnss_of_match,
  474. },
  475. };
  476. static int __init wcnss_init(void)
  477. {
  478. int ret;
  479. ret = platform_driver_register(&wcnss_driver);
  480. if (ret)
  481. return ret;
  482. ret = platform_driver_register(&qcom_iris_driver);
  483. if (ret)
  484. platform_driver_unregister(&wcnss_driver);
  485. return ret;
  486. }
  487. module_init(wcnss_init);
  488. static void __exit wcnss_exit(void)
  489. {
  490. platform_driver_unregister(&qcom_iris_driver);
  491. platform_driver_unregister(&wcnss_driver);
  492. }
  493. module_exit(wcnss_exit);
  494. MODULE_DESCRIPTION("Qualcomm Peripherial Image Loader for Wireless Subsystem");
  495. MODULE_LICENSE("GPL v2");