1 /* 2 * Copyright (c) 2007-2016, Synaptics Incorporated 3 * Copyright (C) 2016 Zodiac Inflight Innovations 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 as published by 7 * the Free Software Foundation. 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/rmi.h> 12 #include <linux/firmware.h> 13 #include <asm/unaligned.h> 14 #include <linux/bitops.h> 15 16 #include "rmi_driver.h" 17 #include "rmi_f34.h" 18 19 static int rmi_f34_write_bootloader_id(struct f34_data *f34) 20 { 21 struct rmi_function *fn = f34->fn; 22 struct rmi_device *rmi_dev = fn->rmi_dev; 23 u8 bootloader_id[F34_BOOTLOADER_ID_LEN]; 24 int ret; 25 26 ret = rmi_read_block(rmi_dev, fn->fd.query_base_addr, 27 bootloader_id, sizeof(bootloader_id)); 28 if (ret) { 29 dev_err(&fn->dev, "%s: Reading bootloader ID failed: %d\n", 30 __func__, ret); 31 return ret; 32 } 33 34 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: writing bootloader id '%c%c'\n", 35 __func__, bootloader_id[0], bootloader_id[1]); 36 37 ret = rmi_write_block(rmi_dev, 38 fn->fd.data_base_addr + F34_BLOCK_DATA_OFFSET, 39 bootloader_id, sizeof(bootloader_id)); 40 if (ret) { 41 dev_err(&fn->dev, "Failed to write bootloader ID: %d\n", ret); 42 return ret; 43 } 44 45 return 0; 46 } 47 48 static int rmi_f34_command(struct f34_data *f34, u8 command, 49 unsigned int timeout, bool write_bl_id) 50 { 51 struct rmi_function *fn = f34->fn; 52 struct rmi_device *rmi_dev = fn->rmi_dev; 53 int ret; 54 55 if (write_bl_id) { 56 ret = rmi_f34_write_bootloader_id(f34); 57 if (ret) 58 return ret; 59 } 60 61 init_completion(&f34->v5.cmd_done); 62 63 ret = rmi_read(rmi_dev, f34->v5.ctrl_address, &f34->v5.status); 64 if (ret) { 65 dev_err(&f34->fn->dev, 66 "%s: Failed to read cmd register: %d (command %#02x)\n", 67 __func__, ret, command); 68 return ret; 69 } 70 71 f34->v5.status |= command & 0x0f; 72 73 ret = rmi_write(rmi_dev, f34->v5.ctrl_address, f34->v5.status); 74 if (ret < 0) { 75 dev_err(&f34->fn->dev, 76 "Failed to write F34 command %#02x: %d\n", 77 command, ret); 78 return ret; 79 } 80 81 if (!wait_for_completion_timeout(&f34->v5.cmd_done, 82 msecs_to_jiffies(timeout))) { 83 84 ret = rmi_read(rmi_dev, f34->v5.ctrl_address, &f34->v5.status); 85 if (ret) { 86 dev_err(&f34->fn->dev, 87 "%s: cmd %#02x timed out: %d\n", 88 __func__, command, ret); 89 return ret; 90 } 91 92 if (f34->v5.status & 0x7f) { 93 dev_err(&f34->fn->dev, 94 "%s: cmd %#02x timed out, status: %#02x\n", 95 __func__, command, f34->v5.status); 96 return -ETIMEDOUT; 97 } 98 } 99 100 return 0; 101 } 102 103 static int rmi_f34_attention(struct rmi_function *fn, unsigned long *irq_bits) 104 { 105 struct f34_data *f34 = dev_get_drvdata(&fn->dev); 106 int ret; 107 u8 status; 108 109 if (f34->bl_version == 5) { 110 ret = rmi_read(f34->fn->rmi_dev, f34->v5.ctrl_address, 111 &status); 112 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: status: %#02x, ret: %d\n", 113 __func__, status, ret); 114 115 if (!ret && !(status & 0x7f)) 116 complete(&f34->v5.cmd_done); 117 } else { 118 ret = rmi_read_block(f34->fn->rmi_dev, 119 f34->fn->fd.data_base_addr + 120 f34->v7.off.flash_status, 121 &status, sizeof(status)); 122 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: status: %#02x, ret: %d\n", 123 __func__, status, ret); 124 125 if (!ret && !(status & 0x1f)) 126 complete(&f34->v7.cmd_done); 127 } 128 129 return 0; 130 } 131 132 static int rmi_f34_write_blocks(struct f34_data *f34, const void *data, 133 int block_count, u8 command) 134 { 135 struct rmi_function *fn = f34->fn; 136 struct rmi_device *rmi_dev = fn->rmi_dev; 137 u16 address = fn->fd.data_base_addr + F34_BLOCK_DATA_OFFSET; 138 u8 start_address[] = { 0, 0 }; 139 int i; 140 int ret; 141 142 ret = rmi_write_block(rmi_dev, fn->fd.data_base_addr, 143 start_address, sizeof(start_address)); 144 if (ret) { 145 dev_err(&fn->dev, "Failed to write initial zeros: %d\n", ret); 146 return ret; 147 } 148 149 for (i = 0; i < block_count; i++) { 150 ret = rmi_write_block(rmi_dev, address, 151 data, f34->v5.block_size); 152 if (ret) { 153 dev_err(&fn->dev, 154 "failed to write block #%d: %d\n", i, ret); 155 return ret; 156 } 157 158 ret = rmi_f34_command(f34, command, F34_IDLE_WAIT_MS, false); 159 if (ret) { 160 dev_err(&fn->dev, 161 "Failed to write command for block #%d: %d\n", 162 i, ret); 163 return ret; 164 } 165 166 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "wrote block %d of %d\n", 167 i + 1, block_count); 168 169 data += f34->v5.block_size; 170 f34->update_progress += f34->v5.block_size; 171 f34->update_status = (f34->update_progress * 100) / 172 f34->update_size; 173 } 174 175 return 0; 176 } 177 178 static int rmi_f34_write_firmware(struct f34_data *f34, const void *data) 179 { 180 return rmi_f34_write_blocks(f34, data, f34->v5.fw_blocks, 181 F34_WRITE_FW_BLOCK); 182 } 183 184 static int rmi_f34_write_config(struct f34_data *f34, const void *data) 185 { 186 return rmi_f34_write_blocks(f34, data, f34->v5.config_blocks, 187 F34_WRITE_CONFIG_BLOCK); 188 } 189 190 static int rmi_f34_enable_flash(struct f34_data *f34) 191 { 192 return rmi_f34_command(f34, F34_ENABLE_FLASH_PROG, 193 F34_ENABLE_WAIT_MS, true); 194 } 195 196 static int rmi_f34_flash_firmware(struct f34_data *f34, 197 const struct rmi_f34_firmware *syn_fw) 198 { 199 struct rmi_function *fn = f34->fn; 200 u32 image_size = le32_to_cpu(syn_fw->image_size); 201 u32 config_size = le32_to_cpu(syn_fw->config_size); 202 int ret; 203 204 f34->update_progress = 0; 205 f34->update_size = image_size + config_size; 206 207 if (image_size) { 208 dev_info(&fn->dev, "Erasing firmware...\n"); 209 ret = rmi_f34_command(f34, F34_ERASE_ALL, 210 F34_ERASE_WAIT_MS, true); 211 if (ret) 212 return ret; 213 214 dev_info(&fn->dev, "Writing firmware (%d bytes)...\n", 215 image_size); 216 ret = rmi_f34_write_firmware(f34, syn_fw->data); 217 if (ret) 218 return ret; 219 } 220 221 if (config_size) { 222 /* 223 * We only need to erase config if we haven't updated 224 * firmware. 225 */ 226 if (!image_size) { 227 dev_info(&fn->dev, "Erasing config...\n"); 228 ret = rmi_f34_command(f34, F34_ERASE_CONFIG, 229 F34_ERASE_WAIT_MS, true); 230 if (ret) 231 return ret; 232 } 233 234 dev_info(&fn->dev, "Writing config (%d bytes)...\n", 235 config_size); 236 ret = rmi_f34_write_config(f34, &syn_fw->data[image_size]); 237 if (ret) 238 return ret; 239 } 240 241 return 0; 242 } 243 244 static int rmi_f34_update_firmware(struct f34_data *f34, 245 const struct firmware *fw) 246 { 247 const struct rmi_f34_firmware *syn_fw = 248 (const struct rmi_f34_firmware *)fw->data; 249 u32 image_size = le32_to_cpu(syn_fw->image_size); 250 u32 config_size = le32_to_cpu(syn_fw->config_size); 251 int ret; 252 253 BUILD_BUG_ON(offsetof(struct rmi_f34_firmware, data) != 254 F34_FW_IMAGE_OFFSET); 255 256 rmi_dbg(RMI_DEBUG_FN, &f34->fn->dev, 257 "FW size:%zd, checksum:%08x, image_size:%d, config_size:%d\n", 258 fw->size, 259 le32_to_cpu(syn_fw->checksum), 260 image_size, config_size); 261 262 rmi_dbg(RMI_DEBUG_FN, &f34->fn->dev, 263 "FW bootloader_id:%02x, product_id:%.*s, info: %02x%02x\n", 264 syn_fw->bootloader_version, 265 (int)sizeof(syn_fw->product_id), syn_fw->product_id, 266 syn_fw->product_info[0], syn_fw->product_info[1]); 267 268 if (image_size && image_size != f34->v5.fw_blocks * f34->v5.block_size) { 269 dev_err(&f34->fn->dev, 270 "Bad firmware image: fw size %d, expected %d\n", 271 image_size, f34->v5.fw_blocks * f34->v5.block_size); 272 ret = -EILSEQ; 273 goto out; 274 } 275 276 if (config_size && 277 config_size != f34->v5.config_blocks * f34->v5.block_size) { 278 dev_err(&f34->fn->dev, 279 "Bad firmware image: config size %d, expected %d\n", 280 config_size, 281 f34->v5.config_blocks * f34->v5.block_size); 282 ret = -EILSEQ; 283 goto out; 284 } 285 286 if (image_size && !config_size) { 287 dev_err(&f34->fn->dev, "Bad firmware image: no config data\n"); 288 ret = -EILSEQ; 289 goto out; 290 } 291 292 dev_info(&f34->fn->dev, "Firmware image OK\n"); 293 mutex_lock(&f34->v5.flash_mutex); 294 295 ret = rmi_f34_flash_firmware(f34, syn_fw); 296 297 mutex_unlock(&f34->v5.flash_mutex); 298 299 out: 300 return ret; 301 } 302 303 static int rmi_f34_status(struct rmi_function *fn) 304 { 305 struct f34_data *f34 = dev_get_drvdata(&fn->dev); 306 307 /* 308 * The status is the percentage complete, or once complete, 309 * zero for success or a negative return code. 310 */ 311 return f34->update_status; 312 } 313 314 static ssize_t rmi_driver_bootloader_id_show(struct device *dev, 315 struct device_attribute *dattr, 316 char *buf) 317 { 318 struct rmi_driver_data *data = dev_get_drvdata(dev); 319 struct rmi_function *fn = data->f34_container; 320 struct f34_data *f34; 321 322 if (fn) { 323 f34 = dev_get_drvdata(&fn->dev); 324 325 if (f34->bl_version == 5) 326 return scnprintf(buf, PAGE_SIZE, "%c%c\n", 327 f34->bootloader_id[0], 328 f34->bootloader_id[1]); 329 else 330 return scnprintf(buf, PAGE_SIZE, "V%d.%d\n", 331 f34->bootloader_id[1], 332 f34->bootloader_id[0]); 333 } 334 335 return 0; 336 } 337 338 static DEVICE_ATTR(bootloader_id, 0444, rmi_driver_bootloader_id_show, NULL); 339 340 static ssize_t rmi_driver_configuration_id_show(struct device *dev, 341 struct device_attribute *dattr, 342 char *buf) 343 { 344 struct rmi_driver_data *data = dev_get_drvdata(dev); 345 struct rmi_function *fn = data->f34_container; 346 struct f34_data *f34; 347 348 if (fn) { 349 f34 = dev_get_drvdata(&fn->dev); 350 351 return scnprintf(buf, PAGE_SIZE, "%s\n", f34->configuration_id); 352 } 353 354 return 0; 355 } 356 357 static DEVICE_ATTR(configuration_id, 0444, 358 rmi_driver_configuration_id_show, NULL); 359 360 static int rmi_firmware_update(struct rmi_driver_data *data, 361 const struct firmware *fw) 362 { 363 struct rmi_device *rmi_dev = data->rmi_dev; 364 struct device *dev = &rmi_dev->dev; 365 struct f34_data *f34; 366 int ret; 367 368 if (!data->f34_container) { 369 dev_warn(dev, "%s: No F34 present!\n", __func__); 370 return -EINVAL; 371 } 372 373 f34 = dev_get_drvdata(&data->f34_container->dev); 374 375 if (f34->bl_version == 7) { 376 if (data->pdt_props & HAS_BSR) { 377 dev_err(dev, "%s: LTS not supported\n", __func__); 378 return -ENODEV; 379 } 380 } else if (f34->bl_version != 5) { 381 dev_warn(dev, "F34 V%d not supported!\n", 382 data->f34_container->fd.function_version); 383 return -ENODEV; 384 } 385 386 /* Enter flash mode */ 387 if (f34->bl_version == 7) 388 ret = rmi_f34v7_start_reflash(f34, fw); 389 else 390 ret = rmi_f34_enable_flash(f34); 391 if (ret) 392 return ret; 393 394 rmi_disable_irq(rmi_dev, false); 395 396 /* Tear down functions and re-probe */ 397 rmi_free_function_list(rmi_dev); 398 399 ret = rmi_probe_interrupts(data); 400 if (ret) 401 return ret; 402 403 ret = rmi_init_functions(data); 404 if (ret) 405 return ret; 406 407 if (!data->bootloader_mode || !data->f34_container) { 408 dev_warn(dev, "%s: No F34 present or not in bootloader!\n", 409 __func__); 410 return -EINVAL; 411 } 412 413 rmi_enable_irq(rmi_dev, false); 414 415 f34 = dev_get_drvdata(&data->f34_container->dev); 416 417 /* Perform firmware update */ 418 if (f34->bl_version == 7) 419 ret = rmi_f34v7_do_reflash(f34, fw); 420 else 421 ret = rmi_f34_update_firmware(f34, fw); 422 423 if (ret) { 424 f34->update_status = ret; 425 dev_err(&f34->fn->dev, 426 "Firmware update failed, status: %d\n", ret); 427 } else { 428 dev_info(&f34->fn->dev, "Firmware update complete\n"); 429 } 430 431 rmi_disable_irq(rmi_dev, false); 432 433 /* Re-probe */ 434 rmi_dbg(RMI_DEBUG_FN, dev, "Re-probing device\n"); 435 rmi_free_function_list(rmi_dev); 436 437 ret = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset); 438 if (ret < 0) 439 dev_warn(dev, "RMI reset failed!\n"); 440 441 ret = rmi_probe_interrupts(data); 442 if (ret) 443 return ret; 444 445 ret = rmi_init_functions(data); 446 if (ret) 447 return ret; 448 449 rmi_enable_irq(rmi_dev, false); 450 451 if (data->f01_container->dev.driver) 452 /* Driver already bound, so enable ATTN now. */ 453 return rmi_enable_sensor(rmi_dev); 454 455 rmi_dbg(RMI_DEBUG_FN, dev, "%s complete\n", __func__); 456 457 return ret; 458 } 459 460 static ssize_t rmi_driver_update_fw_store(struct device *dev, 461 struct device_attribute *dattr, 462 const char *buf, size_t count) 463 { 464 struct rmi_driver_data *data = dev_get_drvdata(dev); 465 char fw_name[NAME_MAX]; 466 const struct firmware *fw; 467 size_t copy_count = count; 468 int ret; 469 470 if (count == 0 || count >= NAME_MAX) 471 return -EINVAL; 472 473 if (buf[count - 1] == '\0' || buf[count - 1] == '\n') 474 copy_count -= 1; 475 476 strncpy(fw_name, buf, copy_count); 477 fw_name[copy_count] = '\0'; 478 479 ret = request_firmware(&fw, fw_name, dev); 480 if (ret) 481 return ret; 482 483 dev_info(dev, "Flashing %s\n", fw_name); 484 485 ret = rmi_firmware_update(data, fw); 486 487 release_firmware(fw); 488 489 return ret ?: count; 490 } 491 492 static DEVICE_ATTR(update_fw, 0200, NULL, rmi_driver_update_fw_store); 493 494 static ssize_t rmi_driver_update_fw_status_show(struct device *dev, 495 struct device_attribute *dattr, 496 char *buf) 497 { 498 struct rmi_driver_data *data = dev_get_drvdata(dev); 499 int update_status = 0; 500 501 if (data->f34_container) 502 update_status = rmi_f34_status(data->f34_container); 503 504 return scnprintf(buf, PAGE_SIZE, "%d\n", update_status); 505 } 506 507 static DEVICE_ATTR(update_fw_status, 0444, 508 rmi_driver_update_fw_status_show, NULL); 509 510 static struct attribute *rmi_firmware_attrs[] = { 511 &dev_attr_bootloader_id.attr, 512 &dev_attr_configuration_id.attr, 513 &dev_attr_update_fw.attr, 514 &dev_attr_update_fw_status.attr, 515 NULL 516 }; 517 518 static const struct attribute_group rmi_firmware_attr_group = { 519 .attrs = rmi_firmware_attrs, 520 }; 521 522 static int rmi_f34_probe(struct rmi_function *fn) 523 { 524 struct f34_data *f34; 525 unsigned char f34_queries[9]; 526 bool has_config_id; 527 u8 version = fn->fd.function_version; 528 int ret; 529 530 f34 = devm_kzalloc(&fn->dev, sizeof(struct f34_data), GFP_KERNEL); 531 if (!f34) 532 return -ENOMEM; 533 534 f34->fn = fn; 535 dev_set_drvdata(&fn->dev, f34); 536 537 /* v5 code only supported version 0, try V7 probe */ 538 if (version > 0) 539 return rmi_f34v7_probe(f34); 540 541 f34->bl_version = 5; 542 543 ret = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr, 544 f34_queries, sizeof(f34_queries)); 545 if (ret) { 546 dev_err(&fn->dev, "%s: Failed to query properties\n", 547 __func__); 548 return ret; 549 } 550 551 snprintf(f34->bootloader_id, sizeof(f34->bootloader_id), 552 "%c%c", f34_queries[0], f34_queries[1]); 553 554 mutex_init(&f34->v5.flash_mutex); 555 init_completion(&f34->v5.cmd_done); 556 557 f34->v5.block_size = get_unaligned_le16(&f34_queries[3]); 558 f34->v5.fw_blocks = get_unaligned_le16(&f34_queries[5]); 559 f34->v5.config_blocks = get_unaligned_le16(&f34_queries[7]); 560 f34->v5.ctrl_address = fn->fd.data_base_addr + F34_BLOCK_DATA_OFFSET + 561 f34->v5.block_size; 562 has_config_id = f34_queries[2] & (1 << 2); 563 564 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Bootloader ID: %s\n", 565 f34->bootloader_id); 566 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Block size: %d\n", 567 f34->v5.block_size); 568 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "FW blocks: %d\n", 569 f34->v5.fw_blocks); 570 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "CFG blocks: %d\n", 571 f34->v5.config_blocks); 572 573 if (has_config_id) { 574 ret = rmi_read_block(fn->rmi_dev, fn->fd.control_base_addr, 575 f34_queries, sizeof(f34_queries)); 576 if (ret) { 577 dev_err(&fn->dev, "Failed to read F34 config ID\n"); 578 return ret; 579 } 580 581 snprintf(f34->configuration_id, sizeof(f34->configuration_id), 582 "%02x%02x%02x%02x", 583 f34_queries[0], f34_queries[1], 584 f34_queries[2], f34_queries[3]); 585 586 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Configuration ID: %s\n", 587 f34->configuration_id); 588 } 589 590 return 0; 591 } 592 593 int rmi_f34_create_sysfs(struct rmi_device *rmi_dev) 594 { 595 return sysfs_create_group(&rmi_dev->dev.kobj, &rmi_firmware_attr_group); 596 } 597 598 void rmi_f34_remove_sysfs(struct rmi_device *rmi_dev) 599 { 600 sysfs_remove_group(&rmi_dev->dev.kobj, &rmi_firmware_attr_group); 601 } 602 603 struct rmi_function_handler rmi_f34_handler = { 604 .driver = { 605 .name = "rmi4_f34", 606 }, 607 .func = 0x34, 608 .probe = rmi_f34_probe, 609 .attention = rmi_f34_attention, 610 }; 611