1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016, Linaro Ltd.
4 * Copyright (c) 2015, Sony Mobile Communications Inc.
6 #include <linux/firmware.h>
7 #include <linux/module.h>
8 #include <linux/slab.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>
15 #define WCNSS_REQUEST_TIMEOUT (5 * HZ)
16 #define WCNSS_CBC_TIMEOUT (10 * HZ)
18 #define WCNSS_ACK_DONE_BOOTING 1
19 #define WCNSS_ACK_COLD_BOOTING 2
21 #define NV_FRAGMENT_SIZE 3072
22 #define NVBIN_FILE "wlan/prima/WCNSS_qcom_wlan_nv.bin"
25 * struct wcnss_ctrl - driver context
27 * @channel: SMD channel handle
28 * @ack: completion for outstanding requests
29 * @cbc: completion for cbc complete indication
30 * @ack_status: status of the outstanding request
31 * @probe_work: worker for uploading nv binary
35 struct rpmsg_endpoint
*channel
;
37 struct completion ack
;
38 struct completion cbc
;
41 struct work_struct probe_work
;
46 WCNSS_VERSION_REQ
= 0x01000000,
48 WCNSS_DOWNLOAD_NV_REQ
,
49 WCNSS_DOWNLOAD_NV_RESP
,
51 WCNSS_UPLOAD_CAL_RESP
,
52 WCNSS_DOWNLOAD_CAL_REQ
,
53 WCNSS_DOWNLOAD_CAL_RESP
,
55 WCNSS_BUILD_VERSION_REQ
,
56 WCNSS_BUILD_VERSION_RESP
,
58 WCNSS_CBC_COMPLETE_IND
,
62 * struct wcnss_msg_hdr - common packet header for requests and responses
63 * @type: packet message type
64 * @len: total length of the packet, including this header
66 struct wcnss_msg_hdr
{
72 * struct wcnss_version_resp - version request response
74 struct wcnss_version_resp
{
75 struct wcnss_msg_hdr hdr
;
83 * struct wcnss_download_nv_req - firmware fragment request
84 * @hdr: common packet wcnss_msg_hdr header
85 * @seq: sequence number of this fragment
86 * @last: boolean indicator of this being the last fragment of the binary
87 * @frag_size: length of this fragment
88 * @fragment: fragment data
90 struct wcnss_download_nv_req
{
91 struct wcnss_msg_hdr hdr
;
99 * struct wcnss_download_nv_resp - firmware download response
100 * @hdr: common packet wcnss_msg_hdr header
101 * @status: boolean to indicate success of the download
103 struct wcnss_download_nv_resp
{
104 struct wcnss_msg_hdr hdr
;
109 * wcnss_ctrl_smd_callback() - handler from SMD responses
110 * @rpdev: remote processor message device pointer
111 * @data: pointer to the incoming data packet
112 * @count: size of the incoming data packet
116 * Handles any incoming packets from the remote WCNSS_CTRL service.
118 static int wcnss_ctrl_smd_callback(struct rpmsg_device
*rpdev
,
124 struct wcnss_ctrl
*wcnss
= dev_get_drvdata(&rpdev
->dev
);
125 const struct wcnss_download_nv_resp
*nvresp
;
126 const struct wcnss_version_resp
*version
;
127 const struct wcnss_msg_hdr
*hdr
= data
;
130 case WCNSS_VERSION_RESP
:
131 if (count
!= sizeof(*version
)) {
133 "invalid size of version response\n");
138 dev_info(wcnss
->dev
, "WCNSS Version %d.%d %d.%d\n",
139 version
->major
, version
->minor
,
140 version
->version
, version
->revision
);
142 complete(&wcnss
->ack
);
144 case WCNSS_DOWNLOAD_NV_RESP
:
145 if (count
!= sizeof(*nvresp
)) {
147 "invalid size of download response\n");
152 wcnss
->ack_status
= nvresp
->status
;
153 complete(&wcnss
->ack
);
155 case WCNSS_CBC_COMPLETE_IND
:
156 dev_dbg(wcnss
->dev
, "cold boot complete\n");
157 complete(&wcnss
->cbc
);
160 dev_info(wcnss
->dev
, "unknown message type %d\n", hdr
->type
);
168 * wcnss_request_version() - send a version request to WCNSS
169 * @wcnss: wcnss ctrl driver context
171 static int wcnss_request_version(struct wcnss_ctrl
*wcnss
)
173 struct wcnss_msg_hdr msg
;
176 msg
.type
= WCNSS_VERSION_REQ
;
177 msg
.len
= sizeof(msg
);
178 ret
= rpmsg_send(wcnss
->channel
, &msg
, sizeof(msg
));
182 ret
= wait_for_completion_timeout(&wcnss
->ack
, WCNSS_CBC_TIMEOUT
);
184 dev_err(wcnss
->dev
, "timeout waiting for version response\n");
192 * wcnss_download_nv() - send nv binary to WCNSS
193 * @wcnss: wcnss_ctrl state handle
194 * @expect_cbc: indicator to caller that an cbc event is expected
196 * Returns 0 on success. Negative errno on failure.
198 static int wcnss_download_nv(struct wcnss_ctrl
*wcnss
, bool *expect_cbc
)
200 struct wcnss_download_nv_req
*req
;
201 const struct firmware
*fw
;
206 req
= kzalloc(sizeof(*req
) + NV_FRAGMENT_SIZE
, GFP_KERNEL
);
210 ret
= request_firmware(&fw
, NVBIN_FILE
, wcnss
->dev
);
212 dev_err(wcnss
->dev
, "Failed to load nv file %s: %d\n",
220 req
->hdr
.type
= WCNSS_DOWNLOAD_NV_REQ
;
221 req
->hdr
.len
= sizeof(*req
) + NV_FRAGMENT_SIZE
;
224 req
->frag_size
= NV_FRAGMENT_SIZE
;
228 if (left
<= NV_FRAGMENT_SIZE
) {
230 req
->frag_size
= left
;
231 req
->hdr
.len
= sizeof(*req
) + left
;
234 memcpy(req
->fragment
, data
, req
->frag_size
);
236 ret
= rpmsg_send(wcnss
->channel
, req
, req
->hdr
.len
);
238 dev_err(wcnss
->dev
, "failed to send smd packet\n");
242 /* Increment for next fragment */
245 data
+= NV_FRAGMENT_SIZE
;
246 left
-= NV_FRAGMENT_SIZE
;
249 ret
= wait_for_completion_timeout(&wcnss
->ack
, WCNSS_REQUEST_TIMEOUT
);
251 dev_err(wcnss
->dev
, "timeout waiting for nv upload ack\n");
254 *expect_cbc
= wcnss
->ack_status
== WCNSS_ACK_COLD_BOOTING
;
259 release_firmware(fw
);
267 * qcom_wcnss_open_channel() - open additional SMD channel to WCNSS
268 * @wcnss: wcnss handle, retrieved from drvdata
269 * @name: SMD channel name
270 * @cb: callback to handle incoming data on the channel
271 * @priv: private data for use in the call-back
273 struct rpmsg_endpoint
*qcom_wcnss_open_channel(void *wcnss
, const char *name
, rpmsg_rx_cb_t cb
, void *priv
)
275 struct rpmsg_channel_info chinfo
;
276 struct wcnss_ctrl
*_wcnss
= wcnss
;
278 strscpy(chinfo
.name
, name
, sizeof(chinfo
.name
));
279 chinfo
.src
= RPMSG_ADDR_ANY
;
280 chinfo
.dst
= RPMSG_ADDR_ANY
;
282 return rpmsg_create_ept(_wcnss
->channel
->rpdev
, cb
, priv
, chinfo
);
284 EXPORT_SYMBOL(qcom_wcnss_open_channel
);
286 static void wcnss_async_probe(struct work_struct
*work
)
288 struct wcnss_ctrl
*wcnss
= container_of(work
, struct wcnss_ctrl
, probe_work
);
292 ret
= wcnss_request_version(wcnss
);
296 ret
= wcnss_download_nv(wcnss
, &expect_cbc
);
300 /* Wait for pending cold boot completion if indicated by the nv downloader */
302 ret
= wait_for_completion_timeout(&wcnss
->cbc
, WCNSS_REQUEST_TIMEOUT
);
304 dev_err(wcnss
->dev
, "expected cold boot completion\n");
307 of_platform_populate(wcnss
->dev
->of_node
, NULL
, NULL
, wcnss
->dev
);
310 static int wcnss_ctrl_probe(struct rpmsg_device
*rpdev
)
312 struct wcnss_ctrl
*wcnss
;
314 wcnss
= devm_kzalloc(&rpdev
->dev
, sizeof(*wcnss
), GFP_KERNEL
);
318 wcnss
->dev
= &rpdev
->dev
;
319 wcnss
->channel
= rpdev
->ept
;
321 init_completion(&wcnss
->ack
);
322 init_completion(&wcnss
->cbc
);
323 INIT_WORK(&wcnss
->probe_work
, wcnss_async_probe
);
325 dev_set_drvdata(&rpdev
->dev
, wcnss
);
327 schedule_work(&wcnss
->probe_work
);
332 static void wcnss_ctrl_remove(struct rpmsg_device
*rpdev
)
334 struct wcnss_ctrl
*wcnss
= dev_get_drvdata(&rpdev
->dev
);
336 cancel_work_sync(&wcnss
->probe_work
);
337 of_platform_depopulate(&rpdev
->dev
);
340 static const struct of_device_id wcnss_ctrl_of_match
[] = {
341 { .compatible
= "qcom,wcnss", },
344 MODULE_DEVICE_TABLE(of
, wcnss_ctrl_of_match
);
346 static struct rpmsg_driver wcnss_ctrl_driver
= {
347 .probe
= wcnss_ctrl_probe
,
348 .remove
= wcnss_ctrl_remove
,
349 .callback
= wcnss_ctrl_smd_callback
,
351 .name
= "qcom_wcnss_ctrl",
352 .owner
= THIS_MODULE
,
353 .of_match_table
= wcnss_ctrl_of_match
,
357 module_rpmsg_driver(wcnss_ctrl_driver
);
359 MODULE_DESCRIPTION("Qualcomm WCNSS control driver");
360 MODULE_LICENSE("GPL v2");