Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Robert Dolca | a06347c | 2015-10-22 12:11:42 +0300 | [diff] [blame] | 2 | /* ------------------------------------------------------------------------- |
| 3 | * Copyright (C) 2014-2016, Intel Corporation |
| 4 | * |
Robert Dolca | a06347c | 2015-10-22 12:11:42 +0300 | [diff] [blame] | 5 | * ------------------------------------------------------------------------- |
| 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/nfc.h> |
| 10 | #include <linux/i2c.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/firmware.h> |
| 13 | #include <net/nfc/nci_core.h> |
| 14 | |
| 15 | #include "fdp.h" |
| 16 | |
| 17 | #define FDP_OTP_PATCH_NAME "otp.bin" |
| 18 | #define FDP_RAM_PATCH_NAME "ram.bin" |
| 19 | #define FDP_FW_HEADER_SIZE 576 |
| 20 | #define FDP_FW_UPDATE_SLEEP 1000 |
| 21 | |
| 22 | #define NCI_GET_VERSION_TIMEOUT 8000 |
| 23 | #define NCI_PATCH_REQUEST_TIMEOUT 8000 |
| 24 | #define FDP_PATCH_CONN_DEST 0xC2 |
| 25 | #define FDP_PATCH_CONN_PARAM_TYPE 0xA0 |
| 26 | |
| 27 | #define NCI_PATCH_TYPE_RAM 0x00 |
| 28 | #define NCI_PATCH_TYPE_OTP 0x01 |
| 29 | #define NCI_PATCH_TYPE_EOT 0xFF |
| 30 | |
| 31 | #define NCI_PARAM_ID_FW_RAM_VERSION 0xA0 |
| 32 | #define NCI_PARAM_ID_FW_OTP_VERSION 0xA1 |
| 33 | #define NCI_PARAM_ID_OTP_LIMITED_VERSION 0xC5 |
| 34 | #define NCI_PARAM_ID_KEY_INDEX_ID 0xC6 |
| 35 | |
| 36 | #define NCI_GID_PROP 0x0F |
| 37 | #define NCI_OP_PROP_PATCH_OID 0x08 |
| 38 | #define NCI_OP_PROP_SET_PDATA_OID 0x23 |
| 39 | |
| 40 | struct fdp_nci_info { |
| 41 | struct nfc_phy_ops *phy_ops; |
| 42 | struct fdp_i2c_phy *phy; |
| 43 | struct nci_dev *ndev; |
| 44 | |
| 45 | const struct firmware *otp_patch; |
| 46 | const struct firmware *ram_patch; |
| 47 | u32 otp_patch_version; |
| 48 | u32 ram_patch_version; |
| 49 | |
| 50 | u32 otp_version; |
| 51 | u32 ram_version; |
| 52 | u32 limited_otp_version; |
| 53 | u8 key_index; |
| 54 | |
| 55 | u8 *fw_vsc_cfg; |
| 56 | u8 clock_type; |
| 57 | u32 clock_freq; |
| 58 | |
| 59 | atomic_t data_pkt_counter; |
| 60 | void (*data_pkt_counter_cb)(struct nci_dev *ndev); |
| 61 | u8 setup_patch_sent; |
| 62 | u8 setup_patch_ntf; |
| 63 | u8 setup_patch_status; |
| 64 | u8 setup_reset_ntf; |
| 65 | wait_queue_head_t setup_wq; |
| 66 | }; |
| 67 | |
| 68 | static u8 nci_core_get_config_otp_ram_version[5] = { |
| 69 | 0x04, |
| 70 | NCI_PARAM_ID_FW_RAM_VERSION, |
| 71 | NCI_PARAM_ID_FW_OTP_VERSION, |
| 72 | NCI_PARAM_ID_OTP_LIMITED_VERSION, |
| 73 | NCI_PARAM_ID_KEY_INDEX_ID |
| 74 | }; |
| 75 | |
| 76 | struct nci_core_get_config_rsp { |
| 77 | u8 status; |
| 78 | u8 count; |
| 79 | u8 data[0]; |
| 80 | }; |
| 81 | |
| 82 | static int fdp_nci_create_conn(struct nci_dev *ndev) |
| 83 | { |
| 84 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 85 | struct core_conn_create_dest_spec_params param; |
| 86 | int r; |
| 87 | |
| 88 | /* proprietary destination specific paramerer without value */ |
| 89 | param.type = FDP_PATCH_CONN_PARAM_TYPE; |
| 90 | param.length = 0x00; |
| 91 | |
| 92 | r = nci_core_conn_create(info->ndev, FDP_PATCH_CONN_DEST, 1, |
| 93 | sizeof(param), ¶m); |
| 94 | if (r) |
| 95 | return r; |
| 96 | |
Christophe Ricard | 9b8d1a4 | 2016-04-30 09:12:51 +0200 | [diff] [blame] | 97 | return nci_get_conn_info_by_dest_type_params(ndev, |
| 98 | FDP_PATCH_CONN_DEST, NULL); |
Robert Dolca | a06347c | 2015-10-22 12:11:42 +0300 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static inline int fdp_nci_get_versions(struct nci_dev *ndev) |
| 102 | { |
| 103 | return nci_core_cmd(ndev, NCI_OP_CORE_GET_CONFIG_CMD, |
| 104 | sizeof(nci_core_get_config_otp_ram_version), |
| 105 | (__u8 *) &nci_core_get_config_otp_ram_version); |
| 106 | } |
| 107 | |
| 108 | static inline int fdp_nci_patch_cmd(struct nci_dev *ndev, u8 type) |
| 109 | { |
| 110 | return nci_prop_cmd(ndev, NCI_OP_PROP_PATCH_OID, sizeof(type), &type); |
| 111 | } |
| 112 | |
| 113 | static inline int fdp_nci_set_production_data(struct nci_dev *ndev, u8 len, |
| 114 | char *data) |
| 115 | { |
| 116 | return nci_prop_cmd(ndev, NCI_OP_PROP_SET_PDATA_OID, len, data); |
| 117 | } |
| 118 | |
| 119 | static int fdp_nci_set_clock(struct nci_dev *ndev, u8 clock_type, |
| 120 | u32 clock_freq) |
| 121 | { |
| 122 | u32 fc = 13560; |
| 123 | u32 nd, num, delta; |
| 124 | char data[9]; |
| 125 | |
| 126 | nd = (24 * fc) / clock_freq; |
| 127 | delta = 24 * fc - nd * clock_freq; |
| 128 | num = (32768 * delta) / clock_freq; |
| 129 | |
| 130 | data[0] = 0x00; |
| 131 | data[1] = 0x00; |
| 132 | data[2] = 0x00; |
| 133 | |
| 134 | data[3] = 0x10; |
| 135 | data[4] = 0x04; |
| 136 | data[5] = num & 0xFF; |
| 137 | data[6] = (num >> 8) & 0xff; |
| 138 | data[7] = nd; |
| 139 | data[8] = clock_type; |
| 140 | |
| 141 | return fdp_nci_set_production_data(ndev, 9, data); |
| 142 | } |
| 143 | |
| 144 | static void fdp_nci_send_patch_cb(struct nci_dev *ndev) |
| 145 | { |
| 146 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 147 | |
| 148 | info->setup_patch_sent = 1; |
| 149 | wake_up(&info->setup_wq); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Register a packet sent counter and a callback |
| 154 | * |
| 155 | * We have no other way of knowing when all firmware packets were sent out |
| 156 | * on the i2c bus. We need to know that in order to close the connection and |
| 157 | * send the patch end message. |
| 158 | */ |
| 159 | static void fdp_nci_set_data_pkt_counter(struct nci_dev *ndev, |
| 160 | void (*cb)(struct nci_dev *ndev), int count) |
| 161 | { |
| 162 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 163 | struct device *dev = &info->phy->i2c_dev->dev; |
| 164 | |
| 165 | dev_dbg(dev, "NCI data pkt counter %d\n", count); |
| 166 | atomic_set(&info->data_pkt_counter, count); |
| 167 | info->data_pkt_counter_cb = cb; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * The device is expecting a stream of packets. All packets need to |
| 172 | * have the PBF flag set to 0x0 (last packet) even if the firmware |
| 173 | * file is segmented and there are multiple packets. If we give the |
| 174 | * whole firmware to nci_send_data it will segment it and it will set |
| 175 | * the PBF flag to 0x01 so we need to do the segmentation here. |
| 176 | * |
| 177 | * The firmware will be analyzed and applied when we send NCI_OP_PROP_PATCH_CMD |
| 178 | * command with NCI_PATCH_TYPE_EOT parameter. The device will send a |
| 179 | * NFCC_PATCH_NTF packaet and a NCI_OP_CORE_RESET_NTF packet. |
| 180 | */ |
| 181 | static int fdp_nci_send_patch(struct nci_dev *ndev, u8 conn_id, u8 type) |
| 182 | { |
| 183 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 184 | const struct firmware *fw; |
| 185 | struct sk_buff *skb; |
| 186 | unsigned long len; |
| 187 | u8 max_size, payload_size; |
| 188 | int rc = 0; |
| 189 | |
| 190 | if ((type == NCI_PATCH_TYPE_OTP && !info->otp_patch) || |
| 191 | (type == NCI_PATCH_TYPE_RAM && !info->ram_patch)) |
| 192 | return -EINVAL; |
| 193 | |
| 194 | if (type == NCI_PATCH_TYPE_OTP) |
| 195 | fw = info->otp_patch; |
| 196 | else |
| 197 | fw = info->ram_patch; |
| 198 | |
| 199 | max_size = nci_conn_max_data_pkt_payload_size(ndev, conn_id); |
| 200 | if (max_size <= 0) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | len = fw->size; |
| 204 | |
| 205 | fdp_nci_set_data_pkt_counter(ndev, fdp_nci_send_patch_cb, |
| 206 | DIV_ROUND_UP(fw->size, max_size)); |
| 207 | |
| 208 | while (len) { |
| 209 | |
| 210 | payload_size = min_t(unsigned long, (unsigned long) max_size, |
| 211 | len); |
| 212 | |
| 213 | skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + payload_size), |
| 214 | GFP_KERNEL); |
| 215 | if (!skb) { |
| 216 | fdp_nci_set_data_pkt_counter(ndev, NULL, 0); |
| 217 | return -ENOMEM; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | skb_reserve(skb, NCI_CTRL_HDR_SIZE); |
| 222 | |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 223 | skb_put_data(skb, fw->data + (fw->size - len), payload_size); |
Robert Dolca | a06347c | 2015-10-22 12:11:42 +0300 | [diff] [blame] | 224 | |
| 225 | rc = nci_send_data(ndev, conn_id, skb); |
| 226 | |
| 227 | if (rc) { |
| 228 | fdp_nci_set_data_pkt_counter(ndev, NULL, 0); |
| 229 | return rc; |
| 230 | } |
| 231 | |
| 232 | len -= payload_size; |
| 233 | } |
| 234 | |
| 235 | return rc; |
| 236 | } |
| 237 | |
| 238 | static int fdp_nci_open(struct nci_dev *ndev) |
| 239 | { |
| 240 | int r; |
| 241 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 242 | struct device *dev = &info->phy->i2c_dev->dev; |
| 243 | |
| 244 | dev_dbg(dev, "%s\n", __func__); |
| 245 | |
| 246 | r = info->phy_ops->enable(info->phy); |
| 247 | |
| 248 | return r; |
| 249 | } |
| 250 | |
| 251 | static int fdp_nci_close(struct nci_dev *ndev) |
| 252 | { |
| 253 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 254 | struct device *dev = &info->phy->i2c_dev->dev; |
| 255 | |
| 256 | dev_dbg(dev, "%s\n", __func__); |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int fdp_nci_send(struct nci_dev *ndev, struct sk_buff *skb) |
| 261 | { |
| 262 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 263 | struct device *dev = &info->phy->i2c_dev->dev; |
| 264 | |
| 265 | dev_dbg(dev, "%s\n", __func__); |
| 266 | |
| 267 | if (atomic_dec_and_test(&info->data_pkt_counter)) |
| 268 | info->data_pkt_counter_cb(ndev); |
| 269 | |
| 270 | return info->phy_ops->write(info->phy, skb); |
| 271 | } |
| 272 | |
| 273 | int fdp_nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb) |
| 274 | { |
| 275 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 276 | struct device *dev = &info->phy->i2c_dev->dev; |
| 277 | |
| 278 | dev_dbg(dev, "%s\n", __func__); |
| 279 | return nci_recv_frame(ndev, skb); |
| 280 | } |
| 281 | EXPORT_SYMBOL(fdp_nci_recv_frame); |
| 282 | |
| 283 | static int fdp_nci_request_firmware(struct nci_dev *ndev) |
| 284 | { |
| 285 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 286 | struct device *dev = &info->phy->i2c_dev->dev; |
| 287 | u8 *data; |
| 288 | int r; |
| 289 | |
| 290 | r = request_firmware(&info->ram_patch, FDP_RAM_PATCH_NAME, dev); |
| 291 | if (r < 0) { |
| 292 | nfc_err(dev, "RAM patch request error\n"); |
| 293 | goto error; |
| 294 | } |
| 295 | |
| 296 | data = (u8 *) info->ram_patch->data; |
| 297 | info->ram_patch_version = |
| 298 | data[FDP_FW_HEADER_SIZE] | |
| 299 | (data[FDP_FW_HEADER_SIZE + 1] << 8) | |
| 300 | (data[FDP_FW_HEADER_SIZE + 2] << 16) | |
| 301 | (data[FDP_FW_HEADER_SIZE + 3] << 24); |
| 302 | |
| 303 | dev_dbg(dev, "RAM patch version: %d, size: %d\n", |
| 304 | info->ram_patch_version, (int) info->ram_patch->size); |
| 305 | |
| 306 | |
| 307 | r = request_firmware(&info->otp_patch, FDP_OTP_PATCH_NAME, dev); |
| 308 | if (r < 0) { |
| 309 | nfc_err(dev, "OTP patch request error\n"); |
| 310 | goto out; |
| 311 | } |
| 312 | |
| 313 | data = (u8 *) info->otp_patch->data; |
| 314 | info->otp_patch_version = |
| 315 | data[FDP_FW_HEADER_SIZE] | |
| 316 | (data[FDP_FW_HEADER_SIZE + 1] << 8) | |
| 317 | (data[FDP_FW_HEADER_SIZE+2] << 16) | |
| 318 | (data[FDP_FW_HEADER_SIZE+3] << 24); |
| 319 | |
| 320 | dev_dbg(dev, "OTP patch version: %d, size: %d\n", |
| 321 | info->otp_patch_version, (int) info->otp_patch->size); |
| 322 | out: |
| 323 | return 0; |
| 324 | error: |
| 325 | return r; |
| 326 | } |
| 327 | |
| 328 | static void fdp_nci_release_firmware(struct nci_dev *ndev) |
| 329 | { |
| 330 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 331 | |
| 332 | if (info->otp_patch) { |
| 333 | release_firmware(info->otp_patch); |
| 334 | info->otp_patch = NULL; |
| 335 | } |
| 336 | |
| 337 | if (info->ram_patch) { |
| 338 | release_firmware(info->ram_patch); |
Colin Ian King | f36acc3 | 2016-03-22 23:17:00 +0000 | [diff] [blame] | 339 | info->ram_patch = NULL; |
Robert Dolca | a06347c | 2015-10-22 12:11:42 +0300 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
| 343 | static int fdp_nci_patch_otp(struct nci_dev *ndev) |
| 344 | { |
| 345 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 346 | struct device *dev = &info->phy->i2c_dev->dev; |
Geert Uytterhoeven | fa1ce54 | 2016-06-05 11:17:10 +0200 | [diff] [blame] | 347 | int conn_id; |
Robert Dolca | a06347c | 2015-10-22 12:11:42 +0300 | [diff] [blame] | 348 | int r = 0; |
| 349 | |
| 350 | if (info->otp_version >= info->otp_patch_version) |
| 351 | goto out; |
| 352 | |
| 353 | info->setup_patch_sent = 0; |
| 354 | info->setup_reset_ntf = 0; |
| 355 | info->setup_patch_ntf = 0; |
| 356 | |
| 357 | /* Patch init request */ |
| 358 | r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_OTP); |
| 359 | if (r) |
| 360 | goto out; |
| 361 | |
| 362 | /* Patch data connection creation */ |
| 363 | conn_id = fdp_nci_create_conn(ndev); |
| 364 | if (conn_id < 0) { |
| 365 | r = conn_id; |
| 366 | goto out; |
| 367 | } |
| 368 | |
| 369 | /* Send the patch over the data connection */ |
| 370 | r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_OTP); |
| 371 | if (r) |
| 372 | goto out; |
| 373 | |
| 374 | /* Wait for all the packets to be send over i2c */ |
| 375 | wait_event_interruptible(info->setup_wq, |
| 376 | info->setup_patch_sent == 1); |
| 377 | |
| 378 | /* make sure that the NFCC processed the last data packet */ |
| 379 | msleep(FDP_FW_UPDATE_SLEEP); |
| 380 | |
| 381 | /* Close the data connection */ |
| 382 | r = nci_core_conn_close(info->ndev, conn_id); |
| 383 | if (r) |
| 384 | goto out; |
| 385 | |
| 386 | /* Patch finish message */ |
| 387 | if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) { |
| 388 | nfc_err(dev, "OTP patch error 0x%x\n", r); |
| 389 | r = -EINVAL; |
| 390 | goto out; |
| 391 | } |
| 392 | |
| 393 | /* If the patch notification didn't arrive yet, wait for it */ |
| 394 | wait_event_interruptible(info->setup_wq, info->setup_patch_ntf); |
| 395 | |
| 396 | /* Check if the patching was successful */ |
| 397 | r = info->setup_patch_status; |
| 398 | if (r) { |
| 399 | nfc_err(dev, "OTP patch error 0x%x\n", r); |
| 400 | r = -EINVAL; |
| 401 | goto out; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * We need to wait for the reset notification before we |
| 406 | * can continue |
| 407 | */ |
| 408 | wait_event_interruptible(info->setup_wq, info->setup_reset_ntf); |
| 409 | |
| 410 | out: |
| 411 | return r; |
| 412 | } |
| 413 | |
| 414 | static int fdp_nci_patch_ram(struct nci_dev *ndev) |
| 415 | { |
| 416 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 417 | struct device *dev = &info->phy->i2c_dev->dev; |
Geert Uytterhoeven | fa1ce54 | 2016-06-05 11:17:10 +0200 | [diff] [blame] | 418 | int conn_id; |
Robert Dolca | a06347c | 2015-10-22 12:11:42 +0300 | [diff] [blame] | 419 | int r = 0; |
| 420 | |
| 421 | if (info->ram_version >= info->ram_patch_version) |
| 422 | goto out; |
| 423 | |
| 424 | info->setup_patch_sent = 0; |
| 425 | info->setup_reset_ntf = 0; |
| 426 | info->setup_patch_ntf = 0; |
| 427 | |
| 428 | /* Patch init request */ |
| 429 | r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_RAM); |
| 430 | if (r) |
| 431 | goto out; |
| 432 | |
| 433 | /* Patch data connection creation */ |
| 434 | conn_id = fdp_nci_create_conn(ndev); |
| 435 | if (conn_id < 0) { |
| 436 | r = conn_id; |
| 437 | goto out; |
| 438 | } |
| 439 | |
| 440 | /* Send the patch over the data connection */ |
| 441 | r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_RAM); |
| 442 | if (r) |
| 443 | goto out; |
| 444 | |
| 445 | /* Wait for all the packets to be send over i2c */ |
| 446 | wait_event_interruptible(info->setup_wq, |
| 447 | info->setup_patch_sent == 1); |
| 448 | |
| 449 | /* make sure that the NFCC processed the last data packet */ |
| 450 | msleep(FDP_FW_UPDATE_SLEEP); |
| 451 | |
| 452 | /* Close the data connection */ |
| 453 | r = nci_core_conn_close(info->ndev, conn_id); |
| 454 | if (r) |
| 455 | goto out; |
| 456 | |
| 457 | /* Patch finish message */ |
| 458 | if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) { |
| 459 | nfc_err(dev, "RAM patch error 0x%x\n", r); |
| 460 | r = -EINVAL; |
| 461 | goto out; |
| 462 | } |
| 463 | |
| 464 | /* If the patch notification didn't arrive yet, wait for it */ |
| 465 | wait_event_interruptible(info->setup_wq, info->setup_patch_ntf); |
| 466 | |
| 467 | /* Check if the patching was successful */ |
| 468 | r = info->setup_patch_status; |
| 469 | if (r) { |
| 470 | nfc_err(dev, "RAM patch error 0x%x\n", r); |
| 471 | r = -EINVAL; |
| 472 | goto out; |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | * We need to wait for the reset notification before we |
| 477 | * can continue |
| 478 | */ |
| 479 | wait_event_interruptible(info->setup_wq, info->setup_reset_ntf); |
| 480 | |
| 481 | out: |
| 482 | return r; |
| 483 | } |
| 484 | |
| 485 | static int fdp_nci_setup(struct nci_dev *ndev) |
| 486 | { |
| 487 | /* Format: total length followed by an NCI packet */ |
| 488 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 489 | struct device *dev = &info->phy->i2c_dev->dev; |
| 490 | int r; |
| 491 | u8 patched = 0; |
| 492 | |
| 493 | dev_dbg(dev, "%s\n", __func__); |
| 494 | |
| 495 | r = nci_core_init(ndev); |
| 496 | if (r) |
| 497 | goto error; |
| 498 | |
| 499 | /* Get RAM and OTP version */ |
| 500 | r = fdp_nci_get_versions(ndev); |
| 501 | if (r) |
| 502 | goto error; |
| 503 | |
| 504 | /* Load firmware from disk */ |
| 505 | r = fdp_nci_request_firmware(ndev); |
| 506 | if (r) |
| 507 | goto error; |
| 508 | |
| 509 | /* Update OTP */ |
| 510 | if (info->otp_version < info->otp_patch_version) { |
| 511 | r = fdp_nci_patch_otp(ndev); |
| 512 | if (r) |
| 513 | goto error; |
| 514 | patched = 1; |
| 515 | } |
| 516 | |
| 517 | /* Update RAM */ |
| 518 | if (info->ram_version < info->ram_patch_version) { |
| 519 | r = fdp_nci_patch_ram(ndev); |
| 520 | if (r) |
| 521 | goto error; |
| 522 | patched = 1; |
| 523 | } |
| 524 | |
| 525 | /* Release the firmware buffers */ |
| 526 | fdp_nci_release_firmware(ndev); |
| 527 | |
| 528 | /* If a patch was applied the new version is checked */ |
| 529 | if (patched) { |
| 530 | r = nci_core_init(ndev); |
| 531 | if (r) |
| 532 | goto error; |
| 533 | |
| 534 | r = fdp_nci_get_versions(ndev); |
| 535 | if (r) |
| 536 | goto error; |
| 537 | |
| 538 | if (info->otp_version != info->otp_patch_version || |
| 539 | info->ram_version != info->ram_patch_version) { |
| 540 | nfc_err(dev, "Firmware update failed"); |
| 541 | r = -EINVAL; |
| 542 | goto error; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | * We initialized the devices but the NFC subsystem expects |
| 548 | * it to not be initialized. |
| 549 | */ |
| 550 | return nci_core_reset(ndev); |
| 551 | |
| 552 | error: |
| 553 | fdp_nci_release_firmware(ndev); |
| 554 | nfc_err(dev, "Setup error %d\n", r); |
| 555 | return r; |
| 556 | } |
| 557 | |
| 558 | static int fdp_nci_post_setup(struct nci_dev *ndev) |
| 559 | { |
| 560 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 561 | struct device *dev = &info->phy->i2c_dev->dev; |
| 562 | int r; |
| 563 | |
| 564 | /* Check if the device has VSC */ |
| 565 | if (info->fw_vsc_cfg && info->fw_vsc_cfg[0]) { |
| 566 | |
| 567 | /* Set the vendor specific configuration */ |
| 568 | r = fdp_nci_set_production_data(ndev, info->fw_vsc_cfg[3], |
| 569 | &info->fw_vsc_cfg[4]); |
| 570 | if (r) { |
| 571 | nfc_err(dev, "Vendor specific config set error %d\n", |
| 572 | r); |
| 573 | return r; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /* Set clock type and frequency */ |
| 578 | r = fdp_nci_set_clock(ndev, info->clock_type, info->clock_freq); |
| 579 | if (r) { |
| 580 | nfc_err(dev, "Clock set error %d\n", r); |
| 581 | return r; |
| 582 | } |
| 583 | |
| 584 | /* |
| 585 | * In order to apply the VSC FDP needs a reset |
| 586 | */ |
| 587 | r = nci_core_reset(ndev); |
| 588 | if (r) |
| 589 | return r; |
| 590 | |
| 591 | /** |
| 592 | * The nci core was initialized when post setup was called |
| 593 | * so we leave it like that |
| 594 | */ |
| 595 | return nci_core_init(ndev); |
| 596 | } |
| 597 | |
| 598 | static int fdp_nci_core_reset_ntf_packet(struct nci_dev *ndev, |
| 599 | struct sk_buff *skb) |
| 600 | { |
| 601 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 602 | struct device *dev = &info->phy->i2c_dev->dev; |
| 603 | |
| 604 | dev_dbg(dev, "%s\n", __func__); |
| 605 | info->setup_reset_ntf = 1; |
| 606 | wake_up(&info->setup_wq); |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | static int fdp_nci_prop_patch_ntf_packet(struct nci_dev *ndev, |
| 612 | struct sk_buff *skb) |
| 613 | { |
| 614 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 615 | struct device *dev = &info->phy->i2c_dev->dev; |
| 616 | |
| 617 | dev_dbg(dev, "%s\n", __func__); |
| 618 | info->setup_patch_ntf = 1; |
| 619 | info->setup_patch_status = skb->data[0]; |
| 620 | wake_up(&info->setup_wq); |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | static int fdp_nci_prop_patch_rsp_packet(struct nci_dev *ndev, |
| 626 | struct sk_buff *skb) |
| 627 | { |
| 628 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 629 | struct device *dev = &info->phy->i2c_dev->dev; |
| 630 | u8 status = skb->data[0]; |
| 631 | |
| 632 | dev_dbg(dev, "%s: status 0x%x\n", __func__, status); |
| 633 | nci_req_complete(ndev, status); |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | static int fdp_nci_prop_set_production_data_rsp_packet(struct nci_dev *ndev, |
| 639 | struct sk_buff *skb) |
| 640 | { |
| 641 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 642 | struct device *dev = &info->phy->i2c_dev->dev; |
| 643 | u8 status = skb->data[0]; |
| 644 | |
| 645 | dev_dbg(dev, "%s: status 0x%x\n", __func__, status); |
| 646 | nci_req_complete(ndev, status); |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | static int fdp_nci_core_get_config_rsp_packet(struct nci_dev *ndev, |
| 652 | struct sk_buff *skb) |
| 653 | { |
| 654 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 655 | struct device *dev = &info->phy->i2c_dev->dev; |
| 656 | struct nci_core_get_config_rsp *rsp = (void *) skb->data; |
| 657 | u8 i, *p; |
| 658 | |
| 659 | if (rsp->status == NCI_STATUS_OK) { |
| 660 | |
| 661 | p = rsp->data; |
| 662 | for (i = 0; i < 4; i++) { |
| 663 | |
| 664 | switch (*p++) { |
| 665 | case NCI_PARAM_ID_FW_RAM_VERSION: |
| 666 | p++; |
| 667 | info->ram_version = le32_to_cpup((__le32 *) p); |
| 668 | p += 4; |
| 669 | break; |
| 670 | case NCI_PARAM_ID_FW_OTP_VERSION: |
| 671 | p++; |
| 672 | info->otp_version = le32_to_cpup((__le32 *) p); |
| 673 | p += 4; |
| 674 | break; |
| 675 | case NCI_PARAM_ID_OTP_LIMITED_VERSION: |
| 676 | p++; |
| 677 | info->otp_version = le32_to_cpup((__le32 *) p); |
| 678 | p += 4; |
| 679 | break; |
| 680 | case NCI_PARAM_ID_KEY_INDEX_ID: |
| 681 | p++; |
| 682 | info->key_index = *p++; |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | dev_dbg(dev, "OTP version %d\n", info->otp_version); |
| 688 | dev_dbg(dev, "RAM version %d\n", info->ram_version); |
| 689 | dev_dbg(dev, "key index %d\n", info->key_index); |
| 690 | dev_dbg(dev, "%s: status 0x%x\n", __func__, rsp->status); |
| 691 | |
| 692 | nci_req_complete(ndev, rsp->status); |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | static struct nci_driver_ops fdp_core_ops[] = { |
| 698 | { |
| 699 | .opcode = NCI_OP_CORE_GET_CONFIG_RSP, |
| 700 | .rsp = fdp_nci_core_get_config_rsp_packet, |
| 701 | }, |
| 702 | { |
| 703 | .opcode = NCI_OP_CORE_RESET_NTF, |
| 704 | .ntf = fdp_nci_core_reset_ntf_packet, |
| 705 | }, |
| 706 | }; |
| 707 | |
| 708 | static struct nci_driver_ops fdp_prop_ops[] = { |
| 709 | { |
| 710 | .opcode = nci_opcode_pack(NCI_GID_PROP, NCI_OP_PROP_PATCH_OID), |
| 711 | .rsp = fdp_nci_prop_patch_rsp_packet, |
| 712 | .ntf = fdp_nci_prop_patch_ntf_packet, |
| 713 | }, |
| 714 | { |
| 715 | .opcode = nci_opcode_pack(NCI_GID_PROP, |
| 716 | NCI_OP_PROP_SET_PDATA_OID), |
| 717 | .rsp = fdp_nci_prop_set_production_data_rsp_packet, |
| 718 | }, |
| 719 | }; |
| 720 | |
Colin Ian King | 81ade1c | 2017-10-05 10:47:12 +0100 | [diff] [blame] | 721 | static struct nci_ops nci_ops = { |
Robert Dolca | a06347c | 2015-10-22 12:11:42 +0300 | [diff] [blame] | 722 | .open = fdp_nci_open, |
| 723 | .close = fdp_nci_close, |
| 724 | .send = fdp_nci_send, |
| 725 | .setup = fdp_nci_setup, |
| 726 | .post_setup = fdp_nci_post_setup, |
| 727 | .prop_ops = fdp_prop_ops, |
| 728 | .n_prop_ops = ARRAY_SIZE(fdp_prop_ops), |
| 729 | .core_ops = fdp_core_ops, |
| 730 | .n_core_ops = ARRAY_SIZE(fdp_core_ops), |
| 731 | }; |
| 732 | |
| 733 | int fdp_nci_probe(struct fdp_i2c_phy *phy, struct nfc_phy_ops *phy_ops, |
| 734 | struct nci_dev **ndevp, int tx_headroom, |
| 735 | int tx_tailroom, u8 clock_type, u32 clock_freq, |
| 736 | u8 *fw_vsc_cfg) |
| 737 | { |
| 738 | struct device *dev = &phy->i2c_dev->dev; |
| 739 | struct fdp_nci_info *info; |
| 740 | struct nci_dev *ndev; |
| 741 | u32 protocols; |
| 742 | int r; |
| 743 | |
Andy Shevchenko | 7b9fcda | 2017-06-19 13:08:50 +0300 | [diff] [blame] | 744 | info = devm_kzalloc(dev, sizeof(struct fdp_nci_info), GFP_KERNEL); |
| 745 | if (!info) |
| 746 | return -ENOMEM; |
Robert Dolca | a06347c | 2015-10-22 12:11:42 +0300 | [diff] [blame] | 747 | |
| 748 | info->phy = phy; |
| 749 | info->phy_ops = phy_ops; |
| 750 | info->clock_type = clock_type; |
| 751 | info->clock_freq = clock_freq; |
| 752 | info->fw_vsc_cfg = fw_vsc_cfg; |
| 753 | |
| 754 | init_waitqueue_head(&info->setup_wq); |
| 755 | |
| 756 | protocols = NFC_PROTO_JEWEL_MASK | |
| 757 | NFC_PROTO_MIFARE_MASK | |
| 758 | NFC_PROTO_FELICA_MASK | |
| 759 | NFC_PROTO_ISO14443_MASK | |
| 760 | NFC_PROTO_ISO14443_B_MASK | |
| 761 | NFC_PROTO_NFC_DEP_MASK | |
| 762 | NFC_PROTO_ISO15693_MASK; |
| 763 | |
| 764 | ndev = nci_allocate_device(&nci_ops, protocols, tx_headroom, |
| 765 | tx_tailroom); |
| 766 | if (!ndev) { |
| 767 | nfc_err(dev, "Cannot allocate nfc ndev\n"); |
Andy Shevchenko | 7b9fcda | 2017-06-19 13:08:50 +0300 | [diff] [blame] | 768 | return -ENOMEM; |
Robert Dolca | a06347c | 2015-10-22 12:11:42 +0300 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | r = nci_register_device(ndev); |
| 772 | if (r) |
| 773 | goto err_regdev; |
| 774 | |
| 775 | *ndevp = ndev; |
| 776 | info->ndev = ndev; |
| 777 | |
| 778 | nci_set_drvdata(ndev, info); |
| 779 | |
| 780 | return 0; |
| 781 | |
| 782 | err_regdev: |
| 783 | nci_free_device(ndev); |
Robert Dolca | a06347c | 2015-10-22 12:11:42 +0300 | [diff] [blame] | 784 | return r; |
| 785 | } |
| 786 | EXPORT_SYMBOL(fdp_nci_probe); |
| 787 | |
| 788 | void fdp_nci_remove(struct nci_dev *ndev) |
| 789 | { |
| 790 | struct fdp_nci_info *info = nci_get_drvdata(ndev); |
| 791 | struct device *dev = &info->phy->i2c_dev->dev; |
| 792 | |
| 793 | dev_dbg(dev, "%s\n", __func__); |
| 794 | |
| 795 | nci_unregister_device(ndev); |
| 796 | nci_free_device(ndev); |
Robert Dolca | a06347c | 2015-10-22 12:11:42 +0300 | [diff] [blame] | 797 | } |
| 798 | EXPORT_SYMBOL(fdp_nci_remove); |
| 799 | |
| 800 | MODULE_LICENSE("GPL"); |
| 801 | MODULE_DESCRIPTION("NFC NCI driver for Intel Fields Peak NFC controller"); |
| 802 | MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>"); |