Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 1 | /* |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Linaro Ltd. |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 3 | * Copyright (c) 2015, Sony Mobile Communications Inc. |
| 4 | * |
| 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. |
| 8 | * |
| 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. |
| 13 | */ |
| 14 | #include <linux/firmware.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/soc/qcom/smd.h> |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 18 | #include <linux/io.h> |
| 19 | #include <linux/of_platform.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/soc/qcom/wcnss_ctrl.h> |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 22 | |
| 23 | #define WCNSS_REQUEST_TIMEOUT (5 * HZ) |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 24 | #define WCNSS_CBC_TIMEOUT (10 * HZ) |
| 25 | |
| 26 | #define WCNSS_ACK_DONE_BOOTING 1 |
| 27 | #define WCNSS_ACK_COLD_BOOTING 2 |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 28 | |
| 29 | #define NV_FRAGMENT_SIZE 3072 |
| 30 | #define NVBIN_FILE "wlan/prima/WCNSS_qcom_wlan_nv.bin" |
| 31 | |
| 32 | /** |
| 33 | * struct wcnss_ctrl - driver context |
| 34 | * @dev: device handle |
| 35 | * @channel: SMD channel handle |
| 36 | * @ack: completion for outstanding requests |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 37 | * @cbc: completion for cbc complete indication |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 38 | * @ack_status: status of the outstanding request |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 39 | * @probe_work: worker for uploading nv binary |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 40 | */ |
| 41 | struct wcnss_ctrl { |
| 42 | struct device *dev; |
| 43 | struct qcom_smd_channel *channel; |
| 44 | |
| 45 | struct completion ack; |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 46 | struct completion cbc; |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 47 | int ack_status; |
| 48 | |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 49 | struct work_struct probe_work; |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | /* message types */ |
| 53 | enum { |
| 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, |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 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, |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /** |
| 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 |
| 73 | */ |
| 74 | struct wcnss_msg_hdr { |
| 75 | u32 type; |
| 76 | u32 len; |
| 77 | } __packed; |
| 78 | |
| 79 | /** |
| 80 | * struct wcnss_version_resp - version request response |
| 81 | * @hdr: common packet wcnss_msg_hdr header |
| 82 | */ |
| 83 | struct 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 | * 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 |
| 98 | */ |
| 99 | struct 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 | * 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 |
| 111 | */ |
| 112 | struct wcnss_download_nv_resp { |
| 113 | struct wcnss_msg_hdr hdr; |
| 114 | u8 status; |
| 115 | } __packed; |
| 116 | |
| 117 | /** |
| 118 | * wcnss_ctrl_smd_callback() - handler from SMD responses |
Bjorn Andersson | b853cb9 | 2016-03-28 21:35:22 -0700 | [diff] [blame] | 119 | * @channel: smd channel handle |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 120 | * @data: pointer to the incoming data packet |
| 121 | * @count: size of the incoming data packet |
| 122 | * |
| 123 | * Handles any incoming packets from the remote WCNSS_CTRL service. |
| 124 | */ |
Bjorn Andersson | b853cb9 | 2016-03-28 21:35:22 -0700 | [diff] [blame] | 125 | static int wcnss_ctrl_smd_callback(struct qcom_smd_channel *channel, |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 126 | const void *data, |
| 127 | size_t count) |
| 128 | { |
Bjorn Andersson | b853cb9 | 2016-03-28 21:35:22 -0700 | [diff] [blame] | 129 | struct wcnss_ctrl *wcnss = qcom_smd_get_drvdata(channel); |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 130 | const struct wcnss_download_nv_resp *nvresp; |
| 131 | const struct wcnss_version_resp *version; |
| 132 | const struct wcnss_msg_hdr *hdr = data; |
| 133 | |
| 134 | switch (hdr->type) { |
| 135 | case WCNSS_VERSION_RESP: |
| 136 | if (count != sizeof(*version)) { |
| 137 | dev_err(wcnss->dev, |
| 138 | "invalid size of version response\n"); |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | version = data; |
| 143 | dev_info(wcnss->dev, "WCNSS Version %d.%d %d.%d\n", |
| 144 | version->major, version->minor, |
| 145 | version->version, version->revision); |
| 146 | |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 147 | complete(&wcnss->ack); |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 148 | break; |
| 149 | case WCNSS_DOWNLOAD_NV_RESP: |
| 150 | if (count != sizeof(*nvresp)) { |
| 151 | dev_err(wcnss->dev, |
| 152 | "invalid size of download response\n"); |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | nvresp = data; |
| 157 | wcnss->ack_status = nvresp->status; |
| 158 | complete(&wcnss->ack); |
| 159 | break; |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 160 | case WCNSS_CBC_COMPLETE_IND: |
| 161 | dev_dbg(wcnss->dev, "cold boot complete\n"); |
| 162 | complete(&wcnss->cbc); |
| 163 | break; |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 164 | default: |
| 165 | dev_info(wcnss->dev, "unknown message type %d\n", hdr->type); |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * wcnss_request_version() - send a version request to WCNSS |
| 174 | * @wcnss: wcnss ctrl driver context |
| 175 | */ |
| 176 | static int wcnss_request_version(struct wcnss_ctrl *wcnss) |
| 177 | { |
| 178 | struct wcnss_msg_hdr msg; |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 179 | int ret; |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 180 | |
| 181 | msg.type = WCNSS_VERSION_REQ; |
| 182 | msg.len = sizeof(msg); |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 183 | ret = qcom_smd_send(wcnss->channel, &msg, sizeof(msg)); |
| 184 | if (ret < 0) |
| 185 | return ret; |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 186 | |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 187 | ret = wait_for_completion_timeout(&wcnss->ack, WCNSS_CBC_TIMEOUT); |
| 188 | if (!ret) { |
| 189 | dev_err(wcnss->dev, "timeout waiting for version response\n"); |
| 190 | return -ETIMEDOUT; |
| 191 | } |
| 192 | |
| 193 | return 0; |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
| 197 | * wcnss_download_nv() - send nv binary to WCNSS |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 198 | * @wcnss: wcnss_ctrl state handle |
| 199 | * @expect_cbc: indicator to caller that an cbc event is expected |
| 200 | * |
| 201 | * Returns 0 on success. Negative errno on failure. |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 202 | */ |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 203 | static int wcnss_download_nv(struct wcnss_ctrl *wcnss, bool *expect_cbc) |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 204 | { |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 205 | struct wcnss_download_nv_req *req; |
| 206 | const struct firmware *fw; |
| 207 | const void *data; |
| 208 | ssize_t left; |
| 209 | int ret; |
| 210 | |
| 211 | req = kzalloc(sizeof(*req) + NV_FRAGMENT_SIZE, GFP_KERNEL); |
| 212 | if (!req) |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 213 | return -ENOMEM; |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 214 | |
| 215 | ret = request_firmware(&fw, NVBIN_FILE, wcnss->dev); |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 216 | if (ret < 0) { |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 217 | dev_err(wcnss->dev, "Failed to load nv file %s: %d\n", |
| 218 | NVBIN_FILE, ret); |
| 219 | goto free_req; |
| 220 | } |
| 221 | |
| 222 | data = fw->data; |
| 223 | left = fw->size; |
| 224 | |
| 225 | req->hdr.type = WCNSS_DOWNLOAD_NV_REQ; |
| 226 | req->hdr.len = sizeof(*req) + NV_FRAGMENT_SIZE; |
| 227 | |
| 228 | req->last = 0; |
| 229 | req->frag_size = NV_FRAGMENT_SIZE; |
| 230 | |
| 231 | req->seq = 0; |
| 232 | do { |
| 233 | if (left <= NV_FRAGMENT_SIZE) { |
| 234 | req->last = 1; |
| 235 | req->frag_size = left; |
| 236 | req->hdr.len = sizeof(*req) + left; |
| 237 | } |
| 238 | |
| 239 | memcpy(req->fragment, data, req->frag_size); |
| 240 | |
| 241 | ret = qcom_smd_send(wcnss->channel, req, req->hdr.len); |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 242 | if (ret < 0) { |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 243 | dev_err(wcnss->dev, "failed to send smd packet\n"); |
| 244 | goto release_fw; |
| 245 | } |
| 246 | |
| 247 | /* Increment for next fragment */ |
| 248 | req->seq++; |
| 249 | |
| 250 | data += req->hdr.len; |
| 251 | left -= NV_FRAGMENT_SIZE; |
| 252 | } while (left > 0); |
| 253 | |
| 254 | ret = wait_for_completion_timeout(&wcnss->ack, WCNSS_REQUEST_TIMEOUT); |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 255 | if (!ret) { |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 256 | dev_err(wcnss->dev, "timeout waiting for nv upload ack\n"); |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 257 | ret = -ETIMEDOUT; |
| 258 | } else { |
| 259 | *expect_cbc = wcnss->ack_status == WCNSS_ACK_COLD_BOOTING; |
| 260 | ret = 0; |
| 261 | } |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 262 | |
| 263 | release_fw: |
| 264 | release_firmware(fw); |
| 265 | free_req: |
| 266 | kfree(req); |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 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 | */ |
| 277 | struct qcom_smd_channel *qcom_wcnss_open_channel(void *wcnss, const char *name, qcom_smd_cb_t cb) |
| 278 | { |
| 279 | struct wcnss_ctrl *_wcnss = wcnss; |
| 280 | |
| 281 | return qcom_smd_open_channel(_wcnss->channel, name, cb); |
| 282 | } |
| 283 | EXPORT_SYMBOL(qcom_wcnss_open_channel); |
| 284 | |
| 285 | static void wcnss_async_probe(struct work_struct *work) |
| 286 | { |
| 287 | struct wcnss_ctrl *wcnss = container_of(work, struct wcnss_ctrl, probe_work); |
| 288 | bool expect_cbc; |
| 289 | int ret; |
| 290 | |
| 291 | ret = wcnss_request_version(wcnss); |
| 292 | if (ret < 0) |
| 293 | return; |
| 294 | |
| 295 | ret = wcnss_download_nv(wcnss, &expect_cbc); |
| 296 | if (ret < 0) |
| 297 | return; |
| 298 | |
| 299 | /* Wait for pending cold boot completion if indicated by the nv downloader */ |
| 300 | if (expect_cbc) { |
| 301 | ret = wait_for_completion_timeout(&wcnss->cbc, WCNSS_REQUEST_TIMEOUT); |
| 302 | if (!ret) |
| 303 | dev_err(wcnss->dev, "expected cold boot completion\n"); |
| 304 | } |
| 305 | |
| 306 | of_platform_populate(wcnss->dev->of_node, NULL, NULL, wcnss->dev); |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static int wcnss_ctrl_probe(struct qcom_smd_device *sdev) |
| 310 | { |
| 311 | struct wcnss_ctrl *wcnss; |
| 312 | |
| 313 | wcnss = devm_kzalloc(&sdev->dev, sizeof(*wcnss), GFP_KERNEL); |
| 314 | if (!wcnss) |
| 315 | return -ENOMEM; |
| 316 | |
| 317 | wcnss->dev = &sdev->dev; |
| 318 | wcnss->channel = sdev->channel; |
| 319 | |
| 320 | init_completion(&wcnss->ack); |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 321 | init_completion(&wcnss->cbc); |
| 322 | INIT_WORK(&wcnss->probe_work, wcnss_async_probe); |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 323 | |
Bjorn Andersson | b853cb9 | 2016-03-28 21:35:22 -0700 | [diff] [blame] | 324 | qcom_smd_set_drvdata(sdev->channel, wcnss); |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 325 | dev_set_drvdata(&sdev->dev, wcnss); |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 326 | |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 327 | schedule_work(&wcnss->probe_work); |
| 328 | |
| 329 | return 0; |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 332 | static void wcnss_ctrl_remove(struct qcom_smd_device *sdev) |
| 333 | { |
| 334 | struct wcnss_ctrl *wcnss = qcom_smd_get_drvdata(sdev->channel); |
| 335 | |
| 336 | cancel_work_sync(&wcnss->probe_work); |
| 337 | of_platform_depopulate(&sdev->dev); |
| 338 | } |
| 339 | |
| 340 | static const struct of_device_id wcnss_ctrl_of_match[] = { |
| 341 | { .compatible = "qcom,wcnss", }, |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 342 | {} |
| 343 | }; |
| 344 | |
| 345 | static struct qcom_smd_driver wcnss_ctrl_driver = { |
| 346 | .probe = wcnss_ctrl_probe, |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 347 | .remove = wcnss_ctrl_remove, |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 348 | .callback = wcnss_ctrl_smd_callback, |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 349 | .driver = { |
| 350 | .name = "qcom_wcnss_ctrl", |
| 351 | .owner = THIS_MODULE, |
Bjorn Andersson | 6be2b3d | 2016-06-06 16:58:20 -0700 | [diff] [blame] | 352 | .of_match_table = wcnss_ctrl_of_match, |
Bjorn Andersson | ea7a1f2 | 2015-09-21 10:52:55 -0700 | [diff] [blame] | 353 | }, |
| 354 | }; |
| 355 | |
| 356 | module_qcom_smd_driver(wcnss_ctrl_driver); |
| 357 | |
| 358 | MODULE_DESCRIPTION("Qualcomm WCNSS control driver"); |
| 359 | MODULE_LICENSE("GPL v2"); |