xref: /linux/drivers/soc/qcom/wcnss_ctrl.c (revision 63ba5b0fb4f54db256ec43b3062b2606b383055d)
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/cleanup.h>
7 #include <linux/firmware.h>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/io.h>
11 #include <linux/of.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/rpmsg.h>
15 #include <linux/soc/qcom/wcnss_ctrl.h>
16 
17 #define WCNSS_REQUEST_TIMEOUT	(5 * HZ)
18 #define WCNSS_CBC_TIMEOUT	(10 * HZ)
19 
20 #define WCNSS_ACK_DONE_BOOTING	1
21 #define WCNSS_ACK_COLD_BOOTING	2
22 
23 #define NV_FRAGMENT_SIZE	3072
24 #define NVBIN_FILE		"wlan/prima/WCNSS_qcom_wlan_nv.bin"
25 
26 /**
27  * struct wcnss_ctrl - driver context
28  * @dev:	device handle
29  * @channel:	SMD channel handle
30  * @ack:	completion for outstanding requests
31  * @cbc:	completion for cbc complete indication
32  * @ack_status:	status of the outstanding request
33  * @probe_work: worker for uploading nv binary
34  */
35 struct wcnss_ctrl {
36 	struct device *dev;
37 	struct rpmsg_endpoint *channel;
38 
39 	struct completion ack;
40 	struct completion cbc;
41 	int ack_status;
42 
43 	struct work_struct probe_work;
44 };
45 
46 /* message types */
47 enum {
48 	WCNSS_VERSION_REQ = 0x01000000,
49 	WCNSS_VERSION_RESP,
50 	WCNSS_DOWNLOAD_NV_REQ,
51 	WCNSS_DOWNLOAD_NV_RESP,
52 	WCNSS_UPLOAD_CAL_REQ,
53 	WCNSS_UPLOAD_CAL_RESP,
54 	WCNSS_DOWNLOAD_CAL_REQ,
55 	WCNSS_DOWNLOAD_CAL_RESP,
56 	WCNSS_VBAT_LEVEL_IND,
57 	WCNSS_BUILD_VERSION_REQ,
58 	WCNSS_BUILD_VERSION_RESP,
59 	WCNSS_PM_CONFIG_REQ,
60 	WCNSS_CBC_COMPLETE_IND,
61 };
62 
63 /**
64  * struct wcnss_msg_hdr - common packet header for requests and responses
65  * @type:	packet message type
66  * @len:	total length of the packet, including this header
67  */
68 struct wcnss_msg_hdr {
69 	u32 type;
70 	u32 len;
71 } __packed;
72 
73 /*
74  * struct wcnss_version_resp - version request response
75  */
76 struct wcnss_version_resp {
77 	struct wcnss_msg_hdr hdr;
78 	u8 major;
79 	u8 minor;
80 	u8 version;
81 	u8 revision;
82 } __packed;
83 
84 /**
85  * struct wcnss_download_nv_req - firmware fragment request
86  * @hdr:	common packet wcnss_msg_hdr header
87  * @seq:	sequence number of this fragment
88  * @last:	boolean indicator of this being the last fragment of the binary
89  * @frag_size:	length of this fragment
90  * @fragment:	fragment data
91  */
92 struct wcnss_download_nv_req {
93 	struct wcnss_msg_hdr hdr;
94 	u16 seq;
95 	u16 last;
96 	u32 frag_size;
97 	u8 fragment[];
98 } __packed;
99 
100 /**
101  * struct wcnss_download_nv_resp - firmware download response
102  * @hdr:	common packet wcnss_msg_hdr header
103  * @status:	boolean to indicate success of the download
104  */
105 struct wcnss_download_nv_resp {
106 	struct wcnss_msg_hdr hdr;
107 	u8 status;
108 } __packed;
109 
110 /**
111  * wcnss_ctrl_smd_callback() - handler from SMD responses
112  * @rpdev:	remote processor message device pointer
113  * @data:	pointer to the incoming data packet
114  * @count:	size of the incoming data packet
115  * @priv:	unused
116  * @addr:	unused
117  *
118  * Handles any incoming packets from the remote WCNSS_CTRL service.
119  */
120 static int wcnss_ctrl_smd_callback(struct rpmsg_device *rpdev,
121 				   void *data,
122 				   int count,
123 				   void *priv,
124 				   u32 addr)
125 {
126 	struct wcnss_ctrl *wcnss = dev_get_drvdata(&rpdev->dev);
127 	const struct wcnss_download_nv_resp *nvresp;
128 	const struct wcnss_version_resp *version;
129 	const struct wcnss_msg_hdr *hdr = data;
130 
131 	switch (hdr->type) {
132 	case WCNSS_VERSION_RESP:
133 		if (count != sizeof(*version)) {
134 			dev_err(wcnss->dev,
135 				"invalid size of version response\n");
136 			break;
137 		}
138 
139 		version = data;
140 		dev_info(wcnss->dev, "WCNSS Version %d.%d %d.%d\n",
141 			 version->major, version->minor,
142 			 version->version, version->revision);
143 
144 		complete(&wcnss->ack);
145 		break;
146 	case WCNSS_DOWNLOAD_NV_RESP:
147 		if (count != sizeof(*nvresp)) {
148 			dev_err(wcnss->dev,
149 				"invalid size of download response\n");
150 			break;
151 		}
152 
153 		nvresp = data;
154 		wcnss->ack_status = nvresp->status;
155 		complete(&wcnss->ack);
156 		break;
157 	case WCNSS_CBC_COMPLETE_IND:
158 		dev_dbg(wcnss->dev, "cold boot complete\n");
159 		complete(&wcnss->cbc);
160 		break;
161 	default:
162 		dev_info(wcnss->dev, "unknown message type %d\n", hdr->type);
163 		break;
164 	}
165 
166 	return 0;
167 }
168 
169 /**
170  * wcnss_request_version() - send a version request to WCNSS
171  * @wcnss:	wcnss ctrl driver context
172  */
173 static int wcnss_request_version(struct wcnss_ctrl *wcnss)
174 {
175 	struct wcnss_msg_hdr msg;
176 	int ret;
177 
178 	msg.type = WCNSS_VERSION_REQ;
179 	msg.len = sizeof(msg);
180 	ret = rpmsg_send(wcnss->channel, &msg, sizeof(msg));
181 	if (ret < 0)
182 		return ret;
183 
184 	ret = wait_for_completion_timeout(&wcnss->ack, WCNSS_CBC_TIMEOUT);
185 	if (!ret) {
186 		dev_err(wcnss->dev, "timeout waiting for version response\n");
187 		return -ETIMEDOUT;
188 	}
189 
190 	return 0;
191 }
192 
193 /**
194  * wcnss_download_nv() - send nv binary to WCNSS
195  * @wcnss:	wcnss_ctrl state handle
196  * @expect_cbc:	indicator to caller that an cbc event is expected
197  *
198  * Returns 0 on success. Negative errno on failure.
199  */
200 static int wcnss_download_nv(struct wcnss_ctrl *wcnss, bool *expect_cbc)
201 {
202 	const struct firmware *fw;
203 	struct device *dev = wcnss->dev;
204 	const char *nvbin = NVBIN_FILE;
205 	const void *data;
206 	ssize_t left;
207 	int ret;
208 
209 	struct wcnss_download_nv_req *req __free(kfree) = kzalloc(sizeof(*req) + NV_FRAGMENT_SIZE,
210 								  GFP_KERNEL);
211 	if (!req)
212 		return -ENOMEM;
213 
214 	ret = of_property_read_string(dev->of_node, "firmware-name", &nvbin);
215 	if (ret < 0 && ret != -EINVAL)
216 		return ret;
217 
218 	ret = request_firmware(&fw, nvbin, dev);
219 	if (ret < 0) {
220 		dev_err(dev, "Failed to load nv file %s: %d\n", nvbin, ret);
221 		return ret;
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(dev, "failed to send smd packet\n");
246 			goto release_fw;
247 		}
248 
249 		/* Increment for next fragment */
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(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 
265 release_fw:
266 	release_firmware(fw);
267 
268 	return ret;
269 }
270 
271 /**
272  * qcom_wcnss_open_channel() - open additional SMD channel to WCNSS
273  * @wcnss:	wcnss handle, retrieved from drvdata
274  * @name:	SMD channel name
275  * @cb:		callback to handle incoming data on the channel
276  * @priv:	private data for use in the call-back
277  */
278 struct rpmsg_endpoint *qcom_wcnss_open_channel(void *wcnss, const char *name, rpmsg_rx_cb_t cb, void *priv)
279 {
280 	struct rpmsg_channel_info chinfo;
281 	struct wcnss_ctrl *_wcnss = wcnss;
282 
283 	strscpy(chinfo.name, name, sizeof(chinfo.name));
284 	chinfo.src = RPMSG_ADDR_ANY;
285 	chinfo.dst = RPMSG_ADDR_ANY;
286 
287 	return rpmsg_create_ept(_wcnss->channel->rpdev, cb, priv, chinfo);
288 }
289 EXPORT_SYMBOL_GPL(qcom_wcnss_open_channel);
290 
291 static void wcnss_async_probe(struct work_struct *work)
292 {
293 	struct wcnss_ctrl *wcnss = container_of(work, struct wcnss_ctrl, probe_work);
294 	bool expect_cbc;
295 	int ret;
296 
297 	ret = wcnss_request_version(wcnss);
298 	if (ret < 0)
299 		return;
300 
301 	ret = wcnss_download_nv(wcnss, &expect_cbc);
302 	if (ret < 0)
303 		return;
304 
305 	/* Wait for pending cold boot completion if indicated by the nv downloader */
306 	if (expect_cbc) {
307 		ret = wait_for_completion_timeout(&wcnss->cbc, WCNSS_REQUEST_TIMEOUT);
308 		if (!ret)
309 			dev_err(wcnss->dev, "expected cold boot completion\n");
310 	}
311 
312 	of_platform_populate(wcnss->dev->of_node, NULL, NULL, wcnss->dev);
313 }
314 
315 static int wcnss_ctrl_probe(struct rpmsg_device *rpdev)
316 {
317 	struct wcnss_ctrl *wcnss;
318 
319 	wcnss = devm_kzalloc(&rpdev->dev, sizeof(*wcnss), GFP_KERNEL);
320 	if (!wcnss)
321 		return -ENOMEM;
322 
323 	wcnss->dev = &rpdev->dev;
324 	wcnss->channel = rpdev->ept;
325 
326 	init_completion(&wcnss->ack);
327 	init_completion(&wcnss->cbc);
328 	INIT_WORK(&wcnss->probe_work, wcnss_async_probe);
329 
330 	dev_set_drvdata(&rpdev->dev, wcnss);
331 
332 	schedule_work(&wcnss->probe_work);
333 
334 	return 0;
335 }
336 
337 static void wcnss_ctrl_remove(struct rpmsg_device *rpdev)
338 {
339 	struct wcnss_ctrl *wcnss = dev_get_drvdata(&rpdev->dev);
340 
341 	cancel_work_sync(&wcnss->probe_work);
342 	of_platform_depopulate(&rpdev->dev);
343 }
344 
345 static const struct of_device_id wcnss_ctrl_of_match[] = {
346 	{ .compatible = "qcom,wcnss", },
347 	{}
348 };
349 MODULE_DEVICE_TABLE(of, wcnss_ctrl_of_match);
350 
351 static struct rpmsg_driver wcnss_ctrl_driver = {
352 	.probe = wcnss_ctrl_probe,
353 	.remove = wcnss_ctrl_remove,
354 	.callback = wcnss_ctrl_smd_callback,
355 	.drv  = {
356 		.name  = "qcom_wcnss_ctrl",
357 		.of_match_table = wcnss_ctrl_of_match,
358 	},
359 };
360 
361 module_rpmsg_driver(wcnss_ctrl_driver);
362 
363 MODULE_DESCRIPTION("Qualcomm WCNSS control driver");
364 MODULE_LICENSE("GPL v2");
365