1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/firmware.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/io.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>
22
23#define WCNSS_REQUEST_TIMEOUT (5 * HZ)
24#define WCNSS_CBC_TIMEOUT (10 * HZ)
25
26#define WCNSS_ACK_DONE_BOOTING 1
27#define WCNSS_ACK_COLD_BOOTING 2
28
29#define NV_FRAGMENT_SIZE 3072
30#define NVBIN_FILE "wlan/prima/WCNSS_qcom_wlan_nv.bin"
31
32
33
34
35
36
37
38
39
40
41struct wcnss_ctrl {
42 struct device *dev;
43 struct rpmsg_endpoint *channel;
44
45 struct completion ack;
46 struct completion cbc;
47 int ack_status;
48
49 struct work_struct probe_work;
50};
51
52
53enum {
54 WCNSS_VERSION_REQ = 0x01000000,
55 WCNSS_VERSION_RESP,
56 WCNSS_DOWNLOAD_NV_REQ,
57 WCNSS_DOWNLOAD_NV_RESP,
58 WCNSS_UPLOAD_CAL_REQ,
59 WCNSS_UPLOAD_CAL_RESP,
60 WCNSS_DOWNLOAD_CAL_REQ,
61 WCNSS_DOWNLOAD_CAL_RESP,
62 WCNSS_VBAT_LEVEL_IND,
63 WCNSS_BUILD_VERSION_REQ,
64 WCNSS_BUILD_VERSION_RESP,
65 WCNSS_PM_CONFIG_REQ,
66 WCNSS_CBC_COMPLETE_IND,
67};
68
69
70
71
72
73
74struct wcnss_msg_hdr {
75 u32 type;
76 u32 len;
77} __packed;
78
79
80
81
82
83struct wcnss_version_resp {
84 struct wcnss_msg_hdr hdr;
85 u8 major;
86 u8 minor;
87 u8 version;
88 u8 revision;
89} __packed;
90
91
92
93
94
95
96
97
98
99struct wcnss_download_nv_req {
100 struct wcnss_msg_hdr hdr;
101 u16 seq;
102 u16 last;
103 u32 frag_size;
104 u8 fragment[];
105} __packed;
106
107
108
109
110
111
112struct wcnss_download_nv_resp {
113 struct wcnss_msg_hdr hdr;
114 u8 status;
115} __packed;
116
117
118
119
120
121
122
123
124
125static int wcnss_ctrl_smd_callback(struct rpmsg_device *rpdev,
126 void *data,
127 int count,
128 void *priv,
129 u32 addr)
130{
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;
135
136 switch (hdr->type) {
137 case WCNSS_VERSION_RESP:
138 if (count != sizeof(*version)) {
139 dev_err(wcnss->dev,
140 "invalid size of version response\n");
141 break;
142 }
143
144 version = data;
145 dev_info(wcnss->dev, "WCNSS Version %d.%d %d.%d\n",
146 version->major, version->minor,
147 version->version, version->revision);
148
149 complete(&wcnss->ack);
150 break;
151 case WCNSS_DOWNLOAD_NV_RESP:
152 if (count != sizeof(*nvresp)) {
153 dev_err(wcnss->dev,
154 "invalid size of download response\n");
155 break;
156 }
157
158 nvresp = data;
159 wcnss->ack_status = nvresp->status;
160 complete(&wcnss->ack);
161 break;
162 case WCNSS_CBC_COMPLETE_IND:
163 dev_dbg(wcnss->dev, "cold boot complete\n");
164 complete(&wcnss->cbc);
165 break;
166 default:
167 dev_info(wcnss->dev, "unknown message type %d\n", hdr->type);
168 break;
169 }
170
171 return 0;
172}
173
174
175
176
177
178static int wcnss_request_version(struct wcnss_ctrl *wcnss)
179{
180 struct wcnss_msg_hdr msg;
181 int ret;
182
183 msg.type = WCNSS_VERSION_REQ;
184 msg.len = sizeof(msg);
185 ret = rpmsg_send(wcnss->channel, &msg, sizeof(msg));
186 if (ret < 0)
187 return ret;
188
189 ret = wait_for_completion_timeout(&wcnss->ack, WCNSS_CBC_TIMEOUT);
190 if (!ret) {
191 dev_err(wcnss->dev, "timeout waiting for version response\n");
192 return -ETIMEDOUT;
193 }
194
195 return 0;
196}
197
198
199
200
201
202
203
204
205static int wcnss_download_nv(struct wcnss_ctrl *wcnss, bool *expect_cbc)
206{
207 struct wcnss_download_nv_req *req;
208 const struct firmware *fw;
209 const void *data;
210 ssize_t left;
211 int ret;
212
213 req = kzalloc(sizeof(*req) + NV_FRAGMENT_SIZE, GFP_KERNEL);
214 if (!req)
215 return -ENOMEM;
216
217 ret = request_firmware(&fw, NVBIN_FILE, wcnss->dev);
218 if (ret < 0) {
219 dev_err(wcnss->dev, "Failed to load nv file %s: %d\n",
220 NVBIN_FILE, ret);
221 goto free_req;
222 }
223
224 data = fw->data;
225 left = fw->size;
226
227 req->hdr.type = WCNSS_DOWNLOAD_NV_REQ;
228 req->hdr.len = sizeof(*req) + NV_FRAGMENT_SIZE;
229
230 req->last = 0;
231 req->frag_size = NV_FRAGMENT_SIZE;
232
233 req->seq = 0;
234 do {
235 if (left <= NV_FRAGMENT_SIZE) {
236 req->last = 1;
237 req->frag_size = left;
238 req->hdr.len = sizeof(*req) + left;
239 }
240
241 memcpy(req->fragment, data, req->frag_size);
242
243 ret = rpmsg_send(wcnss->channel, req, req->hdr.len);
244 if (ret < 0) {
245 dev_err(wcnss->dev, "failed to send smd packet\n");
246 goto release_fw;
247 }
248
249
250 req->seq++;
251
252 data += NV_FRAGMENT_SIZE;
253 left -= NV_FRAGMENT_SIZE;
254 } while (left > 0);
255
256 ret = wait_for_completion_timeout(&wcnss->ack, WCNSS_REQUEST_TIMEOUT);
257 if (!ret) {
258 dev_err(wcnss->dev, "timeout waiting for nv upload ack\n");
259 ret = -ETIMEDOUT;
260 } else {
261 *expect_cbc = wcnss->ack_status == WCNSS_ACK_COLD_BOOTING;
262 ret = 0;
263 }
264
265release_fw:
266 release_firmware(fw);
267free_req:
268 kfree(req);
269
270 return ret;
271}
272
273
274
275
276
277
278
279struct rpmsg_endpoint *qcom_wcnss_open_channel(void *wcnss, const char *name, rpmsg_rx_cb_t cb, void *priv)
280{
281 struct rpmsg_channel_info chinfo;
282 struct wcnss_ctrl *_wcnss = wcnss;
283
284 strncpy(chinfo.name, name, sizeof(chinfo.name));
285 chinfo.src = RPMSG_ADDR_ANY;
286 chinfo.dst = RPMSG_ADDR_ANY;
287
288 return rpmsg_create_ept(_wcnss->channel->rpdev, cb, priv, chinfo);
289}
290EXPORT_SYMBOL(qcom_wcnss_open_channel);
291
292static void wcnss_async_probe(struct work_struct *work)
293{
294 struct wcnss_ctrl *wcnss = container_of(work, struct wcnss_ctrl, probe_work);
295 bool expect_cbc;
296 int ret;
297
298 ret = wcnss_request_version(wcnss);
299 if (ret < 0)
300 return;
301
302 ret = wcnss_download_nv(wcnss, &expect_cbc);
303 if (ret < 0)
304 return;
305
306
307 if (expect_cbc) {
308 ret = wait_for_completion_timeout(&wcnss->cbc, WCNSS_REQUEST_TIMEOUT);
309 if (!ret)
310 dev_err(wcnss->dev, "expected cold boot completion\n");
311 }
312
313 of_platform_populate(wcnss->dev->of_node, NULL, NULL, wcnss->dev);
314}
315
316static int wcnss_ctrl_probe(struct rpmsg_device *rpdev)
317{
318 struct wcnss_ctrl *wcnss;
319
320 wcnss = devm_kzalloc(&rpdev->dev, sizeof(*wcnss), GFP_KERNEL);
321 if (!wcnss)
322 return -ENOMEM;
323
324 wcnss->dev = &rpdev->dev;
325 wcnss->channel = rpdev->ept;
326
327 init_completion(&wcnss->ack);
328 init_completion(&wcnss->cbc);
329 INIT_WORK(&wcnss->probe_work, wcnss_async_probe);
330
331 dev_set_drvdata(&rpdev->dev, wcnss);
332
333 schedule_work(&wcnss->probe_work);
334
335 return 0;
336}
337
338static void wcnss_ctrl_remove(struct rpmsg_device *rpdev)
339{
340 struct wcnss_ctrl *wcnss = dev_get_drvdata(&rpdev->dev);
341
342 cancel_work_sync(&wcnss->probe_work);
343 of_platform_depopulate(&rpdev->dev);
344}
345
346static const struct of_device_id wcnss_ctrl_of_match[] = {
347 { .compatible = "qcom,wcnss", },
348 {}
349};
350MODULE_DEVICE_TABLE(of, wcnss_ctrl_of_match);
351
352static struct rpmsg_driver wcnss_ctrl_driver = {
353 .probe = wcnss_ctrl_probe,
354 .remove = wcnss_ctrl_remove,
355 .callback = wcnss_ctrl_smd_callback,
356 .drv = {
357 .name = "qcom_wcnss_ctrl",
358 .owner = THIS_MODULE,
359 .of_match_table = wcnss_ctrl_of_match,
360 },
361};
362
363module_rpmsg_driver(wcnss_ctrl_driver);
364
365MODULE_DESCRIPTION("Qualcomm WCNSS control driver");
366MODULE_LICENSE("GPL v2");
367