qcom_wcnss.c 15 KB

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