wcnss_ctrl.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016, Linaro Ltd.
  4. * Copyright (c) 2015, Sony Mobile Communications Inc.
  5. */
  6. #include <linux/firmware.h>
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/io.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/rpmsg.h>
  13. #include <linux/soc/qcom/wcnss_ctrl.h>
  14. #define WCNSS_REQUEST_TIMEOUT (5 * HZ)
  15. #define WCNSS_CBC_TIMEOUT (10 * HZ)
  16. #define WCNSS_ACK_DONE_BOOTING 1
  17. #define WCNSS_ACK_COLD_BOOTING 2
  18. #define NV_FRAGMENT_SIZE 3072
  19. #define NVBIN_FILE "wlan/prima/WCNSS_qcom_wlan_nv.bin"
  20. /**
  21. * struct wcnss_ctrl - driver context
  22. * @dev: device handle
  23. * @channel: SMD channel handle
  24. * @ack: completion for outstanding requests
  25. * @cbc: completion for cbc complete indication
  26. * @ack_status: status of the outstanding request
  27. * @probe_work: worker for uploading nv binary
  28. */
  29. struct wcnss_ctrl {
  30. struct device *dev;
  31. struct rpmsg_endpoint *channel;
  32. struct completion ack;
  33. struct completion cbc;
  34. int ack_status;
  35. struct work_struct probe_work;
  36. };
  37. /* message types */
  38. enum {
  39. WCNSS_VERSION_REQ = 0x01000000,
  40. WCNSS_VERSION_RESP,
  41. WCNSS_DOWNLOAD_NV_REQ,
  42. WCNSS_DOWNLOAD_NV_RESP,
  43. WCNSS_UPLOAD_CAL_REQ,
  44. WCNSS_UPLOAD_CAL_RESP,
  45. WCNSS_DOWNLOAD_CAL_REQ,
  46. WCNSS_DOWNLOAD_CAL_RESP,
  47. WCNSS_VBAT_LEVEL_IND,
  48. WCNSS_BUILD_VERSION_REQ,
  49. WCNSS_BUILD_VERSION_RESP,
  50. WCNSS_PM_CONFIG_REQ,
  51. WCNSS_CBC_COMPLETE_IND,
  52. };
  53. /**
  54. * struct wcnss_msg_hdr - common packet header for requests and responses
  55. * @type: packet message type
  56. * @len: total length of the packet, including this header
  57. */
  58. struct wcnss_msg_hdr {
  59. u32 type;
  60. u32 len;
  61. } __packed;
  62. /**
  63. * struct wcnss_version_resp - version request response
  64. * @hdr: common packet wcnss_msg_hdr header
  65. */
  66. struct wcnss_version_resp {
  67. struct wcnss_msg_hdr hdr;
  68. u8 major;
  69. u8 minor;
  70. u8 version;
  71. u8 revision;
  72. } __packed;
  73. /**
  74. * struct wcnss_download_nv_req - firmware fragment request
  75. * @hdr: common packet wcnss_msg_hdr header
  76. * @seq: sequence number of this fragment
  77. * @last: boolean indicator of this being the last fragment of the binary
  78. * @frag_size: length of this fragment
  79. * @fragment: fragment data
  80. */
  81. struct wcnss_download_nv_req {
  82. struct wcnss_msg_hdr hdr;
  83. u16 seq;
  84. u16 last;
  85. u32 frag_size;
  86. u8 fragment[];
  87. } __packed;
  88. /**
  89. * struct wcnss_download_nv_resp - firmware download response
  90. * @hdr: common packet wcnss_msg_hdr header
  91. * @status: boolean to indicate success of the download
  92. */
  93. struct wcnss_download_nv_resp {
  94. struct wcnss_msg_hdr hdr;
  95. u8 status;
  96. } __packed;
  97. /**
  98. * wcnss_ctrl_smd_callback() - handler from SMD responses
  99. * @channel: smd channel handle
  100. * @data: pointer to the incoming data packet
  101. * @count: size of the incoming data packet
  102. *
  103. * Handles any incoming packets from the remote WCNSS_CTRL service.
  104. */
  105. static int wcnss_ctrl_smd_callback(struct rpmsg_device *rpdev,
  106. void *data,
  107. int count,
  108. void *priv,
  109. u32 addr)
  110. {
  111. struct wcnss_ctrl *wcnss = dev_get_drvdata(&rpdev->dev);
  112. const struct wcnss_download_nv_resp *nvresp;
  113. const struct wcnss_version_resp *version;
  114. const struct wcnss_msg_hdr *hdr = data;
  115. switch (hdr->type) {
  116. case WCNSS_VERSION_RESP:
  117. if (count != sizeof(*version)) {
  118. dev_err(wcnss->dev,
  119. "invalid size of version response\n");
  120. break;
  121. }
  122. version = data;
  123. dev_info(wcnss->dev, "WCNSS Version %d.%d %d.%d\n",
  124. version->major, version->minor,
  125. version->version, version->revision);
  126. complete(&wcnss->ack);
  127. break;
  128. case WCNSS_DOWNLOAD_NV_RESP:
  129. if (count != sizeof(*nvresp)) {
  130. dev_err(wcnss->dev,
  131. "invalid size of download response\n");
  132. break;
  133. }
  134. nvresp = data;
  135. wcnss->ack_status = nvresp->status;
  136. complete(&wcnss->ack);
  137. break;
  138. case WCNSS_CBC_COMPLETE_IND:
  139. dev_dbg(wcnss->dev, "cold boot complete\n");
  140. complete(&wcnss->cbc);
  141. break;
  142. default:
  143. dev_info(wcnss->dev, "unknown message type %d\n", hdr->type);
  144. break;
  145. }
  146. return 0;
  147. }
  148. /**
  149. * wcnss_request_version() - send a version request to WCNSS
  150. * @wcnss: wcnss ctrl driver context
  151. */
  152. static int wcnss_request_version(struct wcnss_ctrl *wcnss)
  153. {
  154. struct wcnss_msg_hdr msg;
  155. int ret;
  156. msg.type = WCNSS_VERSION_REQ;
  157. msg.len = sizeof(msg);
  158. ret = rpmsg_send(wcnss->channel, &msg, sizeof(msg));
  159. if (ret < 0)
  160. return ret;
  161. ret = wait_for_completion_timeout(&wcnss->ack, WCNSS_CBC_TIMEOUT);
  162. if (!ret) {
  163. dev_err(wcnss->dev, "timeout waiting for version response\n");
  164. return -ETIMEDOUT;
  165. }
  166. return 0;
  167. }
  168. /**
  169. * wcnss_download_nv() - send nv binary to WCNSS
  170. * @wcnss: wcnss_ctrl state handle
  171. * @expect_cbc: indicator to caller that an cbc event is expected
  172. *
  173. * Returns 0 on success. Negative errno on failure.
  174. */
  175. static int wcnss_download_nv(struct wcnss_ctrl *wcnss, bool *expect_cbc)
  176. {
  177. struct wcnss_download_nv_req *req;
  178. const struct firmware *fw;
  179. const void *data;
  180. ssize_t left;
  181. int ret;
  182. req = kzalloc(sizeof(*req) + NV_FRAGMENT_SIZE, GFP_KERNEL);
  183. if (!req)
  184. return -ENOMEM;
  185. ret = request_firmware(&fw, NVBIN_FILE, wcnss->dev);
  186. if (ret < 0) {
  187. dev_err(wcnss->dev, "Failed to load nv file %s: %d\n",
  188. NVBIN_FILE, ret);
  189. goto free_req;
  190. }
  191. data = fw->data;
  192. left = fw->size;
  193. req->hdr.type = WCNSS_DOWNLOAD_NV_REQ;
  194. req->hdr.len = sizeof(*req) + NV_FRAGMENT_SIZE;
  195. req->last = 0;
  196. req->frag_size = NV_FRAGMENT_SIZE;
  197. req->seq = 0;
  198. do {
  199. if (left <= NV_FRAGMENT_SIZE) {
  200. req->last = 1;
  201. req->frag_size = left;
  202. req->hdr.len = sizeof(*req) + left;
  203. }
  204. memcpy(req->fragment, data, req->frag_size);
  205. ret = rpmsg_send(wcnss->channel, req, req->hdr.len);
  206. if (ret < 0) {
  207. dev_err(wcnss->dev, "failed to send smd packet\n");
  208. goto release_fw;
  209. }
  210. /* Increment for next fragment */
  211. req->seq++;
  212. data += NV_FRAGMENT_SIZE;
  213. left -= NV_FRAGMENT_SIZE;
  214. } while (left > 0);
  215. ret = wait_for_completion_timeout(&wcnss->ack, WCNSS_REQUEST_TIMEOUT);
  216. if (!ret) {
  217. dev_err(wcnss->dev, "timeout waiting for nv upload ack\n");
  218. ret = -ETIMEDOUT;
  219. } else {
  220. *expect_cbc = wcnss->ack_status == WCNSS_ACK_COLD_BOOTING;
  221. ret = 0;
  222. }
  223. release_fw:
  224. release_firmware(fw);
  225. free_req:
  226. kfree(req);
  227. return ret;
  228. }
  229. /**
  230. * qcom_wcnss_open_channel() - open additional SMD channel to WCNSS
  231. * @wcnss: wcnss handle, retrieved from drvdata
  232. * @name: SMD channel name
  233. * @cb: callback to handle incoming data on the channel
  234. */
  235. struct rpmsg_endpoint *qcom_wcnss_open_channel(void *wcnss, const char *name, rpmsg_rx_cb_t cb, void *priv)
  236. {
  237. struct rpmsg_channel_info chinfo;
  238. struct wcnss_ctrl *_wcnss = wcnss;
  239. strscpy(chinfo.name, name, sizeof(chinfo.name));
  240. chinfo.src = RPMSG_ADDR_ANY;
  241. chinfo.dst = RPMSG_ADDR_ANY;
  242. return rpmsg_create_ept(_wcnss->channel->rpdev, cb, priv, chinfo);
  243. }
  244. EXPORT_SYMBOL(qcom_wcnss_open_channel);
  245. static void wcnss_async_probe(struct work_struct *work)
  246. {
  247. struct wcnss_ctrl *wcnss = container_of(work, struct wcnss_ctrl, probe_work);
  248. bool expect_cbc;
  249. int ret;
  250. ret = wcnss_request_version(wcnss);
  251. if (ret < 0)
  252. return;
  253. ret = wcnss_download_nv(wcnss, &expect_cbc);
  254. if (ret < 0)
  255. return;
  256. /* Wait for pending cold boot completion if indicated by the nv downloader */
  257. if (expect_cbc) {
  258. ret = wait_for_completion_timeout(&wcnss->cbc, WCNSS_REQUEST_TIMEOUT);
  259. if (!ret)
  260. dev_err(wcnss->dev, "expected cold boot completion\n");
  261. }
  262. of_platform_populate(wcnss->dev->of_node, NULL, NULL, wcnss->dev);
  263. }
  264. static int wcnss_ctrl_probe(struct rpmsg_device *rpdev)
  265. {
  266. struct wcnss_ctrl *wcnss;
  267. wcnss = devm_kzalloc(&rpdev->dev, sizeof(*wcnss), GFP_KERNEL);
  268. if (!wcnss)
  269. return -ENOMEM;
  270. wcnss->dev = &rpdev->dev;
  271. wcnss->channel = rpdev->ept;
  272. init_completion(&wcnss->ack);
  273. init_completion(&wcnss->cbc);
  274. INIT_WORK(&wcnss->probe_work, wcnss_async_probe);
  275. dev_set_drvdata(&rpdev->dev, wcnss);
  276. schedule_work(&wcnss->probe_work);
  277. return 0;
  278. }
  279. static void wcnss_ctrl_remove(struct rpmsg_device *rpdev)
  280. {
  281. struct wcnss_ctrl *wcnss = dev_get_drvdata(&rpdev->dev);
  282. cancel_work_sync(&wcnss->probe_work);
  283. of_platform_depopulate(&rpdev->dev);
  284. }
  285. static const struct of_device_id wcnss_ctrl_of_match[] = {
  286. { .compatible = "qcom,wcnss", },
  287. {}
  288. };
  289. MODULE_DEVICE_TABLE(of, wcnss_ctrl_of_match);
  290. static struct rpmsg_driver wcnss_ctrl_driver = {
  291. .probe = wcnss_ctrl_probe,
  292. .remove = wcnss_ctrl_remove,
  293. .callback = wcnss_ctrl_smd_callback,
  294. .drv = {
  295. .name = "qcom_wcnss_ctrl",
  296. .owner = THIS_MODULE,
  297. .of_match_table = wcnss_ctrl_of_match,
  298. },
  299. };
  300. module_rpmsg_driver(wcnss_ctrl_driver);
  301. MODULE_DESCRIPTION("Qualcomm WCNSS control driver");
  302. MODULE_LICENSE("GPL v2");