2 * Copyright (c) 2016, Linaro Ltd.
3 * Copyright (c) 2015, Sony Mobile Communications Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 #include <linux/firmware.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
18 #include <linux/of_platform.h>
19 #include <linux/platform_device.h>
20 #include <linux/rpmsg.h>
21 #include <linux/soc/qcom/wcnss_ctrl.h>
23 #define WCNSS_REQUEST_TIMEOUT (5 * HZ)
24 #define WCNSS_CBC_TIMEOUT (10 * HZ)
26 #define WCNSS_ACK_DONE_BOOTING 1
27 #define WCNSS_ACK_COLD_BOOTING 2
29 #define NV_FRAGMENT_SIZE 3072
30 #define NVBIN_FILE "wlan/prima/WCNSS_qcom_wlan_nv.bin"
33 * struct wcnss_ctrl - driver context
35 * @channel: SMD channel handle
36 * @ack: completion for outstanding requests
37 * @cbc: completion for cbc complete indication
38 * @ack_status: status of the outstanding request
39 * @probe_work: worker for uploading nv binary
43 struct rpmsg_endpoint
*channel
;
45 struct completion ack
;
46 struct completion cbc
;
49 struct work_struct probe_work
;
54 WCNSS_VERSION_REQ
= 0x01000000,
56 WCNSS_DOWNLOAD_NV_REQ
,
57 WCNSS_DOWNLOAD_NV_RESP
,
59 WCNSS_UPLOAD_CAL_RESP
,
60 WCNSS_DOWNLOAD_CAL_REQ
,
61 WCNSS_DOWNLOAD_CAL_RESP
,
63 WCNSS_BUILD_VERSION_REQ
,
64 WCNSS_BUILD_VERSION_RESP
,
66 WCNSS_CBC_COMPLETE_IND
,
70 * struct wcnss_msg_hdr - common packet header for requests and responses
71 * @type: packet message type
72 * @len: total length of the packet, including this header
74 struct wcnss_msg_hdr
{
80 * struct wcnss_version_resp - version request response
81 * @hdr: common packet wcnss_msg_hdr header
83 struct wcnss_version_resp
{
84 struct wcnss_msg_hdr hdr
;
92 * struct wcnss_download_nv_req - firmware fragment request
93 * @hdr: common packet wcnss_msg_hdr header
94 * @seq: sequence number of this fragment
95 * @last: boolean indicator of this being the last fragment of the binary
96 * @frag_size: length of this fragment
97 * @fragment: fragment data
99 struct wcnss_download_nv_req
{
100 struct wcnss_msg_hdr hdr
;
108 * struct wcnss_download_nv_resp - firmware download response
109 * @hdr: common packet wcnss_msg_hdr header
110 * @status: boolean to indicate success of the download
112 struct wcnss_download_nv_resp
{
113 struct wcnss_msg_hdr hdr
;
118 * wcnss_ctrl_smd_callback() - handler from SMD responses
119 * @channel: smd channel handle
120 * @data: pointer to the incoming data packet
121 * @count: size of the incoming data packet
123 * Handles any incoming packets from the remote WCNSS_CTRL service.
125 static int wcnss_ctrl_smd_callback(struct rpmsg_device
*rpdev
,
131 struct wcnss_ctrl
*wcnss
= dev_get_drvdata(&rpdev
->dev
);
132 const struct wcnss_download_nv_resp
*nvresp
;
133 const struct wcnss_version_resp
*version
;
134 const struct wcnss_msg_hdr
*hdr
= data
;
137 case WCNSS_VERSION_RESP
:
138 if (count
!= sizeof(*version
)) {
140 "invalid size of version response\n");
145 dev_info(wcnss
->dev
, "WCNSS Version %d.%d %d.%d\n",
146 version
->major
, version
->minor
,
147 version
->version
, version
->revision
);
149 complete(&wcnss
->ack
);
151 case WCNSS_DOWNLOAD_NV_RESP
:
152 if (count
!= sizeof(*nvresp
)) {
154 "invalid size of download response\n");
159 wcnss
->ack_status
= nvresp
->status
;
160 complete(&wcnss
->ack
);
162 case WCNSS_CBC_COMPLETE_IND
:
163 dev_dbg(wcnss
->dev
, "cold boot complete\n");
164 complete(&wcnss
->cbc
);
167 dev_info(wcnss
->dev
, "unknown message type %d\n", hdr
->type
);
175 * wcnss_request_version() - send a version request to WCNSS
176 * @wcnss: wcnss ctrl driver context
178 static int wcnss_request_version(struct wcnss_ctrl
*wcnss
)
180 struct wcnss_msg_hdr msg
;
183 msg
.type
= WCNSS_VERSION_REQ
;
184 msg
.len
= sizeof(msg
);
185 ret
= rpmsg_send(wcnss
->channel
, &msg
, sizeof(msg
));
189 ret
= wait_for_completion_timeout(&wcnss
->ack
, WCNSS_CBC_TIMEOUT
);
191 dev_err(wcnss
->dev
, "timeout waiting for version response\n");
199 * wcnss_download_nv() - send nv binary to WCNSS
200 * @wcnss: wcnss_ctrl state handle
201 * @expect_cbc: indicator to caller that an cbc event is expected
203 * Returns 0 on success. Negative errno on failure.
205 static int wcnss_download_nv(struct wcnss_ctrl
*wcnss
, bool *expect_cbc
)
207 struct wcnss_download_nv_req
*req
;
208 const struct firmware
*fw
;
213 req
= kzalloc(sizeof(*req
) + NV_FRAGMENT_SIZE
, GFP_KERNEL
);
217 ret
= request_firmware(&fw
, NVBIN_FILE
, wcnss
->dev
);
219 dev_err(wcnss
->dev
, "Failed to load nv file %s: %d\n",
227 req
->hdr
.type
= WCNSS_DOWNLOAD_NV_REQ
;
228 req
->hdr
.len
= sizeof(*req
) + NV_FRAGMENT_SIZE
;
231 req
->frag_size
= NV_FRAGMENT_SIZE
;
235 if (left
<= NV_FRAGMENT_SIZE
) {
237 req
->frag_size
= left
;
238 req
->hdr
.len
= sizeof(*req
) + left
;
241 memcpy(req
->fragment
, data
, req
->frag_size
);
243 ret
= rpmsg_send(wcnss
->channel
, req
, req
->hdr
.len
);
245 dev_err(wcnss
->dev
, "failed to send smd packet\n");
249 /* Increment for next fragment */
252 data
+= req
->hdr
.len
;
253 left
-= NV_FRAGMENT_SIZE
;
256 ret
= wait_for_completion_timeout(&wcnss
->ack
, WCNSS_REQUEST_TIMEOUT
);
258 dev_err(wcnss
->dev
, "timeout waiting for nv upload ack\n");
261 *expect_cbc
= wcnss
->ack_status
== WCNSS_ACK_COLD_BOOTING
;
266 release_firmware(fw
);
274 * qcom_wcnss_open_channel() - open additional SMD channel to WCNSS
275 * @wcnss: wcnss handle, retrieved from drvdata
276 * @name: SMD channel name
277 * @cb: callback to handle incoming data on the channel
279 struct rpmsg_endpoint
*qcom_wcnss_open_channel(void *wcnss
, const char *name
, rpmsg_rx_cb_t cb
, void *priv
)
281 struct rpmsg_channel_info chinfo
;
282 struct wcnss_ctrl
*_wcnss
= wcnss
;
284 strncpy(chinfo
.name
, name
, sizeof(chinfo
.name
));
285 chinfo
.src
= RPMSG_ADDR_ANY
;
286 chinfo
.dst
= RPMSG_ADDR_ANY
;
288 return rpmsg_create_ept(_wcnss
->channel
->rpdev
, cb
, priv
, chinfo
);
290 EXPORT_SYMBOL(qcom_wcnss_open_channel
);
292 static void wcnss_async_probe(struct work_struct
*work
)
294 struct wcnss_ctrl
*wcnss
= container_of(work
, struct wcnss_ctrl
, probe_work
);
298 ret
= wcnss_request_version(wcnss
);
302 ret
= wcnss_download_nv(wcnss
, &expect_cbc
);
306 /* Wait for pending cold boot completion if indicated by the nv downloader */
308 ret
= wait_for_completion_timeout(&wcnss
->cbc
, WCNSS_REQUEST_TIMEOUT
);
310 dev_err(wcnss
->dev
, "expected cold boot completion\n");
313 of_platform_populate(wcnss
->dev
->of_node
, NULL
, NULL
, wcnss
->dev
);
316 static int wcnss_ctrl_probe(struct rpmsg_device
*rpdev
)
318 struct wcnss_ctrl
*wcnss
;
320 wcnss
= devm_kzalloc(&rpdev
->dev
, sizeof(*wcnss
), GFP_KERNEL
);
324 wcnss
->dev
= &rpdev
->dev
;
325 wcnss
->channel
= rpdev
->ept
;
327 init_completion(&wcnss
->ack
);
328 init_completion(&wcnss
->cbc
);
329 INIT_WORK(&wcnss
->probe_work
, wcnss_async_probe
);
331 dev_set_drvdata(&rpdev
->dev
, wcnss
);
333 schedule_work(&wcnss
->probe_work
);
338 static void wcnss_ctrl_remove(struct rpmsg_device
*rpdev
)
340 struct wcnss_ctrl
*wcnss
= dev_get_drvdata(&rpdev
->dev
);
342 cancel_work_sync(&wcnss
->probe_work
);
343 of_platform_depopulate(&rpdev
->dev
);
346 static const struct of_device_id wcnss_ctrl_of_match
[] = {
347 { .compatible
= "qcom,wcnss", },
351 static struct rpmsg_driver wcnss_ctrl_driver
= {
352 .probe
= wcnss_ctrl_probe
,
353 .remove
= wcnss_ctrl_remove
,
354 .callback
= wcnss_ctrl_smd_callback
,
356 .name
= "qcom_wcnss_ctrl",
357 .owner
= THIS_MODULE
,
358 .of_match_table
= wcnss_ctrl_of_match
,
362 module_rpmsg_driver(wcnss_ctrl_driver
);
364 MODULE_DESCRIPTION("Qualcomm WCNSS control driver");
365 MODULE_LICENSE("GPL v2");