0001
0002
0003
0004
0005
0006
0007 #include <linux/kernel.h>
0008 #include <linux/rmi.h>
0009 #include <linux/firmware.h>
0010 #include <asm/unaligned.h>
0011 #include <linux/bitops.h>
0012
0013 #include "rmi_driver.h"
0014 #include "rmi_f34.h"
0015
0016 static int rmi_f34_write_bootloader_id(struct f34_data *f34)
0017 {
0018 struct rmi_function *fn = f34->fn;
0019 struct rmi_device *rmi_dev = fn->rmi_dev;
0020 u8 bootloader_id[F34_BOOTLOADER_ID_LEN];
0021 int ret;
0022
0023 ret = rmi_read_block(rmi_dev, fn->fd.query_base_addr,
0024 bootloader_id, sizeof(bootloader_id));
0025 if (ret) {
0026 dev_err(&fn->dev, "%s: Reading bootloader ID failed: %d\n",
0027 __func__, ret);
0028 return ret;
0029 }
0030
0031 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: writing bootloader id '%c%c'\n",
0032 __func__, bootloader_id[0], bootloader_id[1]);
0033
0034 ret = rmi_write_block(rmi_dev,
0035 fn->fd.data_base_addr + F34_BLOCK_DATA_OFFSET,
0036 bootloader_id, sizeof(bootloader_id));
0037 if (ret) {
0038 dev_err(&fn->dev, "Failed to write bootloader ID: %d\n", ret);
0039 return ret;
0040 }
0041
0042 return 0;
0043 }
0044
0045 static int rmi_f34_command(struct f34_data *f34, u8 command,
0046 unsigned int timeout, bool write_bl_id)
0047 {
0048 struct rmi_function *fn = f34->fn;
0049 struct rmi_device *rmi_dev = fn->rmi_dev;
0050 int ret;
0051
0052 if (write_bl_id) {
0053 ret = rmi_f34_write_bootloader_id(f34);
0054 if (ret)
0055 return ret;
0056 }
0057
0058 init_completion(&f34->v5.cmd_done);
0059
0060 ret = rmi_read(rmi_dev, f34->v5.ctrl_address, &f34->v5.status);
0061 if (ret) {
0062 dev_err(&f34->fn->dev,
0063 "%s: Failed to read cmd register: %d (command %#02x)\n",
0064 __func__, ret, command);
0065 return ret;
0066 }
0067
0068 f34->v5.status |= command & 0x0f;
0069
0070 ret = rmi_write(rmi_dev, f34->v5.ctrl_address, f34->v5.status);
0071 if (ret < 0) {
0072 dev_err(&f34->fn->dev,
0073 "Failed to write F34 command %#02x: %d\n",
0074 command, ret);
0075 return ret;
0076 }
0077
0078 if (!wait_for_completion_timeout(&f34->v5.cmd_done,
0079 msecs_to_jiffies(timeout))) {
0080
0081 ret = rmi_read(rmi_dev, f34->v5.ctrl_address, &f34->v5.status);
0082 if (ret) {
0083 dev_err(&f34->fn->dev,
0084 "%s: cmd %#02x timed out: %d\n",
0085 __func__, command, ret);
0086 return ret;
0087 }
0088
0089 if (f34->v5.status & 0x7f) {
0090 dev_err(&f34->fn->dev,
0091 "%s: cmd %#02x timed out, status: %#02x\n",
0092 __func__, command, f34->v5.status);
0093 return -ETIMEDOUT;
0094 }
0095 }
0096
0097 return 0;
0098 }
0099
0100 static irqreturn_t rmi_f34_attention(int irq, void *ctx)
0101 {
0102 struct rmi_function *fn = ctx;
0103 struct f34_data *f34 = dev_get_drvdata(&fn->dev);
0104 int ret;
0105 u8 status;
0106
0107 if (f34->bl_version == 5) {
0108 ret = rmi_read(f34->fn->rmi_dev, f34->v5.ctrl_address,
0109 &status);
0110 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: status: %#02x, ret: %d\n",
0111 __func__, status, ret);
0112
0113 if (!ret && !(status & 0x7f))
0114 complete(&f34->v5.cmd_done);
0115 } else {
0116 ret = rmi_read_block(f34->fn->rmi_dev,
0117 f34->fn->fd.data_base_addr +
0118 f34->v7.off.flash_status,
0119 &status, sizeof(status));
0120 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: status: %#02x, ret: %d\n",
0121 __func__, status, ret);
0122
0123 if (!ret && !(status & 0x1f))
0124 complete(&f34->v7.cmd_done);
0125 }
0126
0127 return IRQ_HANDLED;
0128 }
0129
0130 static int rmi_f34_write_blocks(struct f34_data *f34, const void *data,
0131 int block_count, u8 command)
0132 {
0133 struct rmi_function *fn = f34->fn;
0134 struct rmi_device *rmi_dev = fn->rmi_dev;
0135 u16 address = fn->fd.data_base_addr + F34_BLOCK_DATA_OFFSET;
0136 u8 start_address[] = { 0, 0 };
0137 int i;
0138 int ret;
0139
0140 ret = rmi_write_block(rmi_dev, fn->fd.data_base_addr,
0141 start_address, sizeof(start_address));
0142 if (ret) {
0143 dev_err(&fn->dev, "Failed to write initial zeros: %d\n", ret);
0144 return ret;
0145 }
0146
0147 for (i = 0; i < block_count; i++) {
0148 ret = rmi_write_block(rmi_dev, address,
0149 data, f34->v5.block_size);
0150 if (ret) {
0151 dev_err(&fn->dev,
0152 "failed to write block #%d: %d\n", i, ret);
0153 return ret;
0154 }
0155
0156 ret = rmi_f34_command(f34, command, F34_IDLE_WAIT_MS, false);
0157 if (ret) {
0158 dev_err(&fn->dev,
0159 "Failed to write command for block #%d: %d\n",
0160 i, ret);
0161 return ret;
0162 }
0163
0164 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "wrote block %d of %d\n",
0165 i + 1, block_count);
0166
0167 data += f34->v5.block_size;
0168 f34->update_progress += f34->v5.block_size;
0169 f34->update_status = (f34->update_progress * 100) /
0170 f34->update_size;
0171 }
0172
0173 return 0;
0174 }
0175
0176 static int rmi_f34_write_firmware(struct f34_data *f34, const void *data)
0177 {
0178 return rmi_f34_write_blocks(f34, data, f34->v5.fw_blocks,
0179 F34_WRITE_FW_BLOCK);
0180 }
0181
0182 static int rmi_f34_write_config(struct f34_data *f34, const void *data)
0183 {
0184 return rmi_f34_write_blocks(f34, data, f34->v5.config_blocks,
0185 F34_WRITE_CONFIG_BLOCK);
0186 }
0187
0188 static int rmi_f34_enable_flash(struct f34_data *f34)
0189 {
0190 return rmi_f34_command(f34, F34_ENABLE_FLASH_PROG,
0191 F34_ENABLE_WAIT_MS, true);
0192 }
0193
0194 static int rmi_f34_flash_firmware(struct f34_data *f34,
0195 const struct rmi_f34_firmware *syn_fw)
0196 {
0197 struct rmi_function *fn = f34->fn;
0198 u32 image_size = le32_to_cpu(syn_fw->image_size);
0199 u32 config_size = le32_to_cpu(syn_fw->config_size);
0200 int ret;
0201
0202 f34->update_progress = 0;
0203 f34->update_size = image_size + config_size;
0204
0205 if (image_size) {
0206 dev_info(&fn->dev, "Erasing firmware...\n");
0207 ret = rmi_f34_command(f34, F34_ERASE_ALL,
0208 F34_ERASE_WAIT_MS, true);
0209 if (ret)
0210 return ret;
0211
0212 dev_info(&fn->dev, "Writing firmware (%d bytes)...\n",
0213 image_size);
0214 ret = rmi_f34_write_firmware(f34, syn_fw->data);
0215 if (ret)
0216 return ret;
0217 }
0218
0219 if (config_size) {
0220
0221
0222
0223
0224 if (!image_size) {
0225 dev_info(&fn->dev, "Erasing config...\n");
0226 ret = rmi_f34_command(f34, F34_ERASE_CONFIG,
0227 F34_ERASE_WAIT_MS, true);
0228 if (ret)
0229 return ret;
0230 }
0231
0232 dev_info(&fn->dev, "Writing config (%d bytes)...\n",
0233 config_size);
0234 ret = rmi_f34_write_config(f34, &syn_fw->data[image_size]);
0235 if (ret)
0236 return ret;
0237 }
0238
0239 return 0;
0240 }
0241
0242 static int rmi_f34_update_firmware(struct f34_data *f34,
0243 const struct firmware *fw)
0244 {
0245 const struct rmi_f34_firmware *syn_fw =
0246 (const struct rmi_f34_firmware *)fw->data;
0247 u32 image_size = le32_to_cpu(syn_fw->image_size);
0248 u32 config_size = le32_to_cpu(syn_fw->config_size);
0249 int ret;
0250
0251 BUILD_BUG_ON(offsetof(struct rmi_f34_firmware, data) !=
0252 F34_FW_IMAGE_OFFSET);
0253
0254 rmi_dbg(RMI_DEBUG_FN, &f34->fn->dev,
0255 "FW size:%zd, checksum:%08x, image_size:%d, config_size:%d\n",
0256 fw->size,
0257 le32_to_cpu(syn_fw->checksum),
0258 image_size, config_size);
0259
0260 rmi_dbg(RMI_DEBUG_FN, &f34->fn->dev,
0261 "FW bootloader_id:%02x, product_id:%.*s, info: %02x%02x\n",
0262 syn_fw->bootloader_version,
0263 (int)sizeof(syn_fw->product_id), syn_fw->product_id,
0264 syn_fw->product_info[0], syn_fw->product_info[1]);
0265
0266 if (image_size && image_size != f34->v5.fw_blocks * f34->v5.block_size) {
0267 dev_err(&f34->fn->dev,
0268 "Bad firmware image: fw size %d, expected %d\n",
0269 image_size, f34->v5.fw_blocks * f34->v5.block_size);
0270 ret = -EILSEQ;
0271 goto out;
0272 }
0273
0274 if (config_size &&
0275 config_size != f34->v5.config_blocks * f34->v5.block_size) {
0276 dev_err(&f34->fn->dev,
0277 "Bad firmware image: config size %d, expected %d\n",
0278 config_size,
0279 f34->v5.config_blocks * f34->v5.block_size);
0280 ret = -EILSEQ;
0281 goto out;
0282 }
0283
0284 if (image_size && !config_size) {
0285 dev_err(&f34->fn->dev, "Bad firmware image: no config data\n");
0286 ret = -EILSEQ;
0287 goto out;
0288 }
0289
0290 dev_info(&f34->fn->dev, "Firmware image OK\n");
0291 mutex_lock(&f34->v5.flash_mutex);
0292
0293 ret = rmi_f34_flash_firmware(f34, syn_fw);
0294
0295 mutex_unlock(&f34->v5.flash_mutex);
0296
0297 out:
0298 return ret;
0299 }
0300
0301 static int rmi_f34_status(struct rmi_function *fn)
0302 {
0303 struct f34_data *f34 = dev_get_drvdata(&fn->dev);
0304
0305
0306
0307
0308
0309 return f34->update_status;
0310 }
0311
0312 static ssize_t rmi_driver_bootloader_id_show(struct device *dev,
0313 struct device_attribute *dattr,
0314 char *buf)
0315 {
0316 struct rmi_driver_data *data = dev_get_drvdata(dev);
0317 struct rmi_function *fn = data->f34_container;
0318 struct f34_data *f34;
0319
0320 if (fn) {
0321 f34 = dev_get_drvdata(&fn->dev);
0322
0323 if (f34->bl_version == 5)
0324 return scnprintf(buf, PAGE_SIZE, "%c%c\n",
0325 f34->bootloader_id[0],
0326 f34->bootloader_id[1]);
0327 else
0328 return scnprintf(buf, PAGE_SIZE, "V%d.%d\n",
0329 f34->bootloader_id[1],
0330 f34->bootloader_id[0]);
0331 }
0332
0333 return 0;
0334 }
0335
0336 static DEVICE_ATTR(bootloader_id, 0444, rmi_driver_bootloader_id_show, NULL);
0337
0338 static ssize_t rmi_driver_configuration_id_show(struct device *dev,
0339 struct device_attribute *dattr,
0340 char *buf)
0341 {
0342 struct rmi_driver_data *data = dev_get_drvdata(dev);
0343 struct rmi_function *fn = data->f34_container;
0344 struct f34_data *f34;
0345
0346 if (fn) {
0347 f34 = dev_get_drvdata(&fn->dev);
0348
0349 return scnprintf(buf, PAGE_SIZE, "%s\n", f34->configuration_id);
0350 }
0351
0352 return 0;
0353 }
0354
0355 static DEVICE_ATTR(configuration_id, 0444,
0356 rmi_driver_configuration_id_show, NULL);
0357
0358 static int rmi_firmware_update(struct rmi_driver_data *data,
0359 const struct firmware *fw)
0360 {
0361 struct rmi_device *rmi_dev = data->rmi_dev;
0362 struct device *dev = &rmi_dev->dev;
0363 struct f34_data *f34;
0364 int ret;
0365
0366 if (!data->f34_container) {
0367 dev_warn(dev, "%s: No F34 present!\n", __func__);
0368 return -EINVAL;
0369 }
0370
0371 f34 = dev_get_drvdata(&data->f34_container->dev);
0372
0373 if (f34->bl_version == 7) {
0374 if (data->pdt_props & HAS_BSR) {
0375 dev_err(dev, "%s: LTS not supported\n", __func__);
0376 return -ENODEV;
0377 }
0378 } else if (f34->bl_version != 5) {
0379 dev_warn(dev, "F34 V%d not supported!\n",
0380 data->f34_container->fd.function_version);
0381 return -ENODEV;
0382 }
0383
0384
0385 if (f34->bl_version == 7)
0386 ret = rmi_f34v7_start_reflash(f34, fw);
0387 else
0388 ret = rmi_f34_enable_flash(f34);
0389 if (ret)
0390 return ret;
0391
0392 rmi_disable_irq(rmi_dev, false);
0393
0394
0395 rmi_free_function_list(rmi_dev);
0396
0397 ret = rmi_probe_interrupts(data);
0398 if (ret)
0399 return ret;
0400
0401 ret = rmi_init_functions(data);
0402 if (ret)
0403 return ret;
0404
0405 if (!data->bootloader_mode || !data->f34_container) {
0406 dev_warn(dev, "%s: No F34 present or not in bootloader!\n",
0407 __func__);
0408 return -EINVAL;
0409 }
0410
0411 rmi_enable_irq(rmi_dev, false);
0412
0413 f34 = dev_get_drvdata(&data->f34_container->dev);
0414
0415
0416 if (f34->bl_version == 7)
0417 ret = rmi_f34v7_do_reflash(f34, fw);
0418 else
0419 ret = rmi_f34_update_firmware(f34, fw);
0420
0421 if (ret) {
0422 f34->update_status = ret;
0423 dev_err(&f34->fn->dev,
0424 "Firmware update failed, status: %d\n", ret);
0425 } else {
0426 dev_info(&f34->fn->dev, "Firmware update complete\n");
0427 }
0428
0429 rmi_disable_irq(rmi_dev, false);
0430
0431
0432 rmi_dbg(RMI_DEBUG_FN, dev, "Re-probing device\n");
0433 rmi_free_function_list(rmi_dev);
0434
0435 ret = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
0436 if (ret < 0)
0437 dev_warn(dev, "RMI reset failed!\n");
0438
0439 ret = rmi_probe_interrupts(data);
0440 if (ret)
0441 return ret;
0442
0443 ret = rmi_init_functions(data);
0444 if (ret)
0445 return ret;
0446
0447 rmi_enable_irq(rmi_dev, false);
0448
0449 if (data->f01_container->dev.driver)
0450
0451 return rmi_enable_sensor(rmi_dev);
0452
0453 rmi_dbg(RMI_DEBUG_FN, dev, "%s complete\n", __func__);
0454
0455 return ret;
0456 }
0457
0458 static ssize_t rmi_driver_update_fw_store(struct device *dev,
0459 struct device_attribute *dattr,
0460 const char *buf, size_t count)
0461 {
0462 struct rmi_driver_data *data = dev_get_drvdata(dev);
0463 char fw_name[NAME_MAX];
0464 const struct firmware *fw;
0465 size_t copy_count = count;
0466 int ret;
0467
0468 if (count == 0 || count >= NAME_MAX)
0469 return -EINVAL;
0470
0471 if (buf[count - 1] == '\0' || buf[count - 1] == '\n')
0472 copy_count -= 1;
0473
0474 strncpy(fw_name, buf, copy_count);
0475 fw_name[copy_count] = '\0';
0476
0477 ret = request_firmware(&fw, fw_name, dev);
0478 if (ret)
0479 return ret;
0480
0481 dev_info(dev, "Flashing %s\n", fw_name);
0482
0483 ret = rmi_firmware_update(data, fw);
0484
0485 release_firmware(fw);
0486
0487 return ret ?: count;
0488 }
0489
0490 static DEVICE_ATTR(update_fw, 0200, NULL, rmi_driver_update_fw_store);
0491
0492 static ssize_t rmi_driver_update_fw_status_show(struct device *dev,
0493 struct device_attribute *dattr,
0494 char *buf)
0495 {
0496 struct rmi_driver_data *data = dev_get_drvdata(dev);
0497 int update_status = 0;
0498
0499 if (data->f34_container)
0500 update_status = rmi_f34_status(data->f34_container);
0501
0502 return scnprintf(buf, PAGE_SIZE, "%d\n", update_status);
0503 }
0504
0505 static DEVICE_ATTR(update_fw_status, 0444,
0506 rmi_driver_update_fw_status_show, NULL);
0507
0508 static struct attribute *rmi_firmware_attrs[] = {
0509 &dev_attr_bootloader_id.attr,
0510 &dev_attr_configuration_id.attr,
0511 &dev_attr_update_fw.attr,
0512 &dev_attr_update_fw_status.attr,
0513 NULL
0514 };
0515
0516 static const struct attribute_group rmi_firmware_attr_group = {
0517 .attrs = rmi_firmware_attrs,
0518 };
0519
0520 static int rmi_f34_probe(struct rmi_function *fn)
0521 {
0522 struct f34_data *f34;
0523 unsigned char f34_queries[9];
0524 bool has_config_id;
0525 u8 version = fn->fd.function_version;
0526 int ret;
0527
0528 f34 = devm_kzalloc(&fn->dev, sizeof(struct f34_data), GFP_KERNEL);
0529 if (!f34)
0530 return -ENOMEM;
0531
0532 f34->fn = fn;
0533 dev_set_drvdata(&fn->dev, f34);
0534
0535
0536 if (version > 0)
0537 return rmi_f34v7_probe(f34);
0538
0539 f34->bl_version = 5;
0540
0541 ret = rmi_read_block(fn->rmi_dev, fn->fd.query_base_addr,
0542 f34_queries, sizeof(f34_queries));
0543 if (ret) {
0544 dev_err(&fn->dev, "%s: Failed to query properties\n",
0545 __func__);
0546 return ret;
0547 }
0548
0549 snprintf(f34->bootloader_id, sizeof(f34->bootloader_id),
0550 "%c%c", f34_queries[0], f34_queries[1]);
0551
0552 mutex_init(&f34->v5.flash_mutex);
0553 init_completion(&f34->v5.cmd_done);
0554
0555 f34->v5.block_size = get_unaligned_le16(&f34_queries[3]);
0556 f34->v5.fw_blocks = get_unaligned_le16(&f34_queries[5]);
0557 f34->v5.config_blocks = get_unaligned_le16(&f34_queries[7]);
0558 f34->v5.ctrl_address = fn->fd.data_base_addr + F34_BLOCK_DATA_OFFSET +
0559 f34->v5.block_size;
0560 has_config_id = f34_queries[2] & (1 << 2);
0561
0562 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Bootloader ID: %s\n",
0563 f34->bootloader_id);
0564 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Block size: %d\n",
0565 f34->v5.block_size);
0566 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "FW blocks: %d\n",
0567 f34->v5.fw_blocks);
0568 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "CFG blocks: %d\n",
0569 f34->v5.config_blocks);
0570
0571 if (has_config_id) {
0572 ret = rmi_read_block(fn->rmi_dev, fn->fd.control_base_addr,
0573 f34_queries, sizeof(f34_queries));
0574 if (ret) {
0575 dev_err(&fn->dev, "Failed to read F34 config ID\n");
0576 return ret;
0577 }
0578
0579 snprintf(f34->configuration_id, sizeof(f34->configuration_id),
0580 "%02x%02x%02x%02x",
0581 f34_queries[0], f34_queries[1],
0582 f34_queries[2], f34_queries[3]);
0583
0584 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Configuration ID: %s\n",
0585 f34->configuration_id);
0586 }
0587
0588 return 0;
0589 }
0590
0591 int rmi_f34_create_sysfs(struct rmi_device *rmi_dev)
0592 {
0593 return sysfs_create_group(&rmi_dev->dev.kobj, &rmi_firmware_attr_group);
0594 }
0595
0596 void rmi_f34_remove_sysfs(struct rmi_device *rmi_dev)
0597 {
0598 sysfs_remove_group(&rmi_dev->dev.kobj, &rmi_firmware_attr_group);
0599 }
0600
0601 struct rmi_function_handler rmi_f34_handler = {
0602 .driver = {
0603 .name = "rmi4_f34",
0604 },
0605 .func = 0x34,
0606 .probe = rmi_f34_probe,
0607 .attention = rmi_f34_attention,
0608 };