1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016, Zodiac Inflight Innovations
4 * Copyright (c) 2007-2016, Synaptics Incorporated
5 * Copyright (C) 2012 Alexandra Chin <alexandra.chin@tw.synaptics.com>
6 * Copyright (C) 2012 Scott Lin <scott.lin@tw.synaptics.com>
9 #include <linux/bitops.h>
10 #include <linux/kernel.h>
11 #include <linux/rmi.h>
12 #include <linux/firmware.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/jiffies.h>
16 #include <linux/unaligned.h>
18 #include "rmi_driver.h"
21 static int rmi_f34v7_read_flash_status(struct f34_data
*f34
)
27 ret
= rmi_read_block(f34
->fn
->rmi_dev
,
28 f34
->fn
->fd
.data_base_addr
+ V7_FLASH_STATUS_OFFSET
,
32 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
33 "%s: Error %d reading flash status\n", __func__
, ret
);
37 f34
->v7
.in_bl_mode
= status
>> 7;
38 f34
->v7
.flash_status
= status
& 0x1f;
40 if (f34
->v7
.flash_status
!= 0x00) {
41 dev_err(&f34
->fn
->dev
, "%s: status=%d, command=0x%02x\n",
42 __func__
, f34
->v7
.flash_status
, f34
->v7
.command
);
45 ret
= rmi_read_block(f34
->fn
->rmi_dev
,
46 f34
->fn
->fd
.data_base_addr
+ V7_COMMAND_OFFSET
,
50 dev_err(&f34
->fn
->dev
, "%s: Failed to read flash command\n",
55 f34
->v7
.command
= command
;
60 static int rmi_f34v7_wait_for_idle(struct f34_data
*f34
, int timeout_ms
)
62 unsigned long timeout
;
64 timeout
= msecs_to_jiffies(timeout_ms
);
66 if (!wait_for_completion_timeout(&f34
->v7
.cmd_done
, timeout
)) {
67 dev_warn(&f34
->fn
->dev
, "%s: Timed out waiting for idle status\n",
75 static int rmi_f34v7_check_command_status(struct f34_data
*f34
, int timeout_ms
)
79 ret
= rmi_f34v7_wait_for_idle(f34
, timeout_ms
);
83 ret
= rmi_f34v7_read_flash_status(f34
);
87 if (f34
->v7
.flash_status
!= 0x00)
93 static int rmi_f34v7_write_command_single_transaction(struct f34_data
*f34
,
98 struct f34v7_data_1_5 data_1_5
;
100 base
= f34
->fn
->fd
.data_base_addr
;
102 memset(&data_1_5
, 0, sizeof(data_1_5
));
105 case v7_CMD_ERASE_ALL
:
106 data_1_5
.partition_id
= CORE_CODE_PARTITION
;
107 data_1_5
.command
= CMD_V7_ERASE_AP
;
109 case v7_CMD_ERASE_UI_FIRMWARE
:
110 data_1_5
.partition_id
= CORE_CODE_PARTITION
;
111 data_1_5
.command
= CMD_V7_ERASE
;
113 case v7_CMD_ERASE_BL_CONFIG
:
114 data_1_5
.partition_id
= GLOBAL_PARAMETERS_PARTITION
;
115 data_1_5
.command
= CMD_V7_ERASE
;
117 case v7_CMD_ERASE_UI_CONFIG
:
118 data_1_5
.partition_id
= CORE_CONFIG_PARTITION
;
119 data_1_5
.command
= CMD_V7_ERASE
;
121 case v7_CMD_ERASE_DISP_CONFIG
:
122 data_1_5
.partition_id
= DISPLAY_CONFIG_PARTITION
;
123 data_1_5
.command
= CMD_V7_ERASE
;
125 case v7_CMD_ERASE_FLASH_CONFIG
:
126 data_1_5
.partition_id
= FLASH_CONFIG_PARTITION
;
127 data_1_5
.command
= CMD_V7_ERASE
;
129 case v7_CMD_ERASE_GUEST_CODE
:
130 data_1_5
.partition_id
= GUEST_CODE_PARTITION
;
131 data_1_5
.command
= CMD_V7_ERASE
;
133 case v7_CMD_ENABLE_FLASH_PROG
:
134 data_1_5
.partition_id
= BOOTLOADER_PARTITION
;
135 data_1_5
.command
= CMD_V7_ENTER_BL
;
139 data_1_5
.payload
[0] = f34
->bootloader_id
[0];
140 data_1_5
.payload
[1] = f34
->bootloader_id
[1];
142 ret
= rmi_write_block(f34
->fn
->rmi_dev
,
143 base
+ V7_PARTITION_ID_OFFSET
,
144 &data_1_5
, sizeof(data_1_5
));
146 dev_err(&f34
->fn
->dev
,
147 "%s: Failed to write single transaction command\n",
155 static int rmi_f34v7_write_command(struct f34_data
*f34
, u8 cmd
)
161 base
= f34
->fn
->fd
.data_base_addr
;
164 case v7_CMD_WRITE_FW
:
165 case v7_CMD_WRITE_CONFIG
:
166 case v7_CMD_WRITE_GUEST_CODE
:
167 command
= CMD_V7_WRITE
;
169 case v7_CMD_READ_CONFIG
:
170 command
= CMD_V7_READ
;
172 case v7_CMD_ERASE_ALL
:
173 command
= CMD_V7_ERASE_AP
;
175 case v7_CMD_ERASE_UI_FIRMWARE
:
176 case v7_CMD_ERASE_BL_CONFIG
:
177 case v7_CMD_ERASE_UI_CONFIG
:
178 case v7_CMD_ERASE_DISP_CONFIG
:
179 case v7_CMD_ERASE_FLASH_CONFIG
:
180 case v7_CMD_ERASE_GUEST_CODE
:
181 command
= CMD_V7_ERASE
;
183 case v7_CMD_ENABLE_FLASH_PROG
:
184 command
= CMD_V7_ENTER_BL
;
187 dev_err(&f34
->fn
->dev
, "%s: Invalid command 0x%02x\n",
192 f34
->v7
.command
= command
;
195 case v7_CMD_ERASE_ALL
:
196 case v7_CMD_ERASE_UI_FIRMWARE
:
197 case v7_CMD_ERASE_BL_CONFIG
:
198 case v7_CMD_ERASE_UI_CONFIG
:
199 case v7_CMD_ERASE_DISP_CONFIG
:
200 case v7_CMD_ERASE_FLASH_CONFIG
:
201 case v7_CMD_ERASE_GUEST_CODE
:
202 case v7_CMD_ENABLE_FLASH_PROG
:
203 ret
= rmi_f34v7_write_command_single_transaction(f34
, cmd
);
212 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
, "%s: writing cmd %02X\n",
215 ret
= rmi_write_block(f34
->fn
->rmi_dev
,
216 base
+ V7_COMMAND_OFFSET
,
217 &command
, sizeof(command
));
219 dev_err(&f34
->fn
->dev
, "%s: Failed to write flash command\n",
227 static int rmi_f34v7_write_partition_id(struct f34_data
*f34
, u8 cmd
)
233 base
= f34
->fn
->fd
.data_base_addr
;
236 case v7_CMD_WRITE_FW
:
237 partition
= CORE_CODE_PARTITION
;
239 case v7_CMD_WRITE_CONFIG
:
240 case v7_CMD_READ_CONFIG
:
241 if (f34
->v7
.config_area
== v7_UI_CONFIG_AREA
)
242 partition
= CORE_CONFIG_PARTITION
;
243 else if (f34
->v7
.config_area
== v7_DP_CONFIG_AREA
)
244 partition
= DISPLAY_CONFIG_PARTITION
;
245 else if (f34
->v7
.config_area
== v7_PM_CONFIG_AREA
)
246 partition
= GUEST_SERIALIZATION_PARTITION
;
247 else if (f34
->v7
.config_area
== v7_BL_CONFIG_AREA
)
248 partition
= GLOBAL_PARAMETERS_PARTITION
;
249 else if (f34
->v7
.config_area
== v7_FLASH_CONFIG_AREA
)
250 partition
= FLASH_CONFIG_PARTITION
;
252 case v7_CMD_WRITE_GUEST_CODE
:
253 partition
= GUEST_CODE_PARTITION
;
255 case v7_CMD_ERASE_ALL
:
256 partition
= CORE_CODE_PARTITION
;
258 case v7_CMD_ERASE_BL_CONFIG
:
259 partition
= GLOBAL_PARAMETERS_PARTITION
;
261 case v7_CMD_ERASE_UI_CONFIG
:
262 partition
= CORE_CONFIG_PARTITION
;
264 case v7_CMD_ERASE_DISP_CONFIG
:
265 partition
= DISPLAY_CONFIG_PARTITION
;
267 case v7_CMD_ERASE_FLASH_CONFIG
:
268 partition
= FLASH_CONFIG_PARTITION
;
270 case v7_CMD_ERASE_GUEST_CODE
:
271 partition
= GUEST_CODE_PARTITION
;
273 case v7_CMD_ENABLE_FLASH_PROG
:
274 partition
= BOOTLOADER_PARTITION
;
277 dev_err(&f34
->fn
->dev
, "%s: Invalid command 0x%02x\n",
282 ret
= rmi_write_block(f34
->fn
->rmi_dev
,
283 base
+ V7_PARTITION_ID_OFFSET
,
284 &partition
, sizeof(partition
));
286 dev_err(&f34
->fn
->dev
, "%s: Failed to write partition ID\n",
294 static int rmi_f34v7_read_partition_table(struct f34_data
*f34
)
297 unsigned long timeout
;
300 u16 block_number
= 0;
302 base
= f34
->fn
->fd
.data_base_addr
;
304 f34
->v7
.config_area
= v7_FLASH_CONFIG_AREA
;
306 ret
= rmi_f34v7_write_partition_id(f34
, v7_CMD_READ_CONFIG
);
310 ret
= rmi_write_block(f34
->fn
->rmi_dev
,
311 base
+ V7_BLOCK_NUMBER_OFFSET
,
312 &block_number
, sizeof(block_number
));
314 dev_err(&f34
->fn
->dev
, "%s: Failed to write block number\n",
319 put_unaligned_le16(f34
->v7
.flash_config_length
, &length
);
321 ret
= rmi_write_block(f34
->fn
->rmi_dev
,
322 base
+ V7_TRANSFER_LENGTH_OFFSET
,
323 &length
, sizeof(length
));
325 dev_err(&f34
->fn
->dev
, "%s: Failed to write transfer length\n",
330 init_completion(&f34
->v7
.cmd_done
);
332 ret
= rmi_f34v7_write_command(f34
, v7_CMD_READ_CONFIG
);
334 dev_err(&f34
->fn
->dev
, "%s: Failed to write command\n",
340 * rmi_f34v7_check_command_status() can't be used here, as this
341 * function is called before IRQs are available
343 timeout
= msecs_to_jiffies(F34_WRITE_WAIT_MS
);
344 while (time_before(jiffies
, timeout
)) {
345 usleep_range(5000, 6000);
346 rmi_f34v7_read_flash_status(f34
);
348 if (f34
->v7
.command
== v7_CMD_IDLE
&&
349 f34
->v7
.flash_status
== 0x00) {
354 ret
= rmi_read_block(f34
->fn
->rmi_dev
,
355 base
+ V7_PAYLOAD_OFFSET
,
356 f34
->v7
.read_config_buf
,
357 f34
->v7
.partition_table_bytes
);
359 dev_err(&f34
->fn
->dev
, "%s: Failed to read block data\n",
367 static void rmi_f34v7_parse_partition_table(struct f34_data
*f34
,
368 const void *partition_table
,
369 struct block_count
*blkcount
,
370 struct physical_address
*phyaddr
)
374 u16 partition_length
;
375 u16 physical_address
;
376 const struct partition_table
*ptable
;
378 for (i
= 0; i
< f34
->v7
.partitions
; i
++) {
380 ptable
= partition_table
+ index
;
381 partition_length
= le16_to_cpu(ptable
->partition_length
);
382 physical_address
= le16_to_cpu(ptable
->start_physical_address
);
383 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
384 "%s: Partition entry %d: %*ph\n",
385 __func__
, i
, sizeof(struct partition_table
), ptable
);
386 switch (ptable
->partition_id
& 0x1f) {
387 case CORE_CODE_PARTITION
:
388 blkcount
->ui_firmware
= partition_length
;
389 phyaddr
->ui_firmware
= physical_address
;
390 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
391 "%s: Core code block count: %d\n",
392 __func__
, blkcount
->ui_firmware
);
394 case CORE_CONFIG_PARTITION
:
395 blkcount
->ui_config
= partition_length
;
396 phyaddr
->ui_config
= physical_address
;
397 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
398 "%s: Core config block count: %d\n",
399 __func__
, blkcount
->ui_config
);
401 case DISPLAY_CONFIG_PARTITION
:
402 blkcount
->dp_config
= partition_length
;
403 phyaddr
->dp_config
= physical_address
;
404 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
405 "%s: Display config block count: %d\n",
406 __func__
, blkcount
->dp_config
);
408 case FLASH_CONFIG_PARTITION
:
409 blkcount
->fl_config
= partition_length
;
410 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
411 "%s: Flash config block count: %d\n",
412 __func__
, blkcount
->fl_config
);
414 case GUEST_CODE_PARTITION
:
415 blkcount
->guest_code
= partition_length
;
416 phyaddr
->guest_code
= physical_address
;
417 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
418 "%s: Guest code block count: %d\n",
419 __func__
, blkcount
->guest_code
);
421 case GUEST_SERIALIZATION_PARTITION
:
422 blkcount
->pm_config
= partition_length
;
423 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
424 "%s: Guest serialization block count: %d\n",
425 __func__
, blkcount
->pm_config
);
427 case GLOBAL_PARAMETERS_PARTITION
:
428 blkcount
->bl_config
= partition_length
;
429 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
430 "%s: Global parameters block count: %d\n",
431 __func__
, blkcount
->bl_config
);
433 case DEVICE_CONFIG_PARTITION
:
434 blkcount
->lockdown
= partition_length
;
435 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
436 "%s: Device config block count: %d\n",
437 __func__
, blkcount
->lockdown
);
443 static int rmi_f34v7_read_queries_bl_version(struct f34_data
*f34
)
449 struct f34v7_query_1_7 query_1_7
;
451 base
= f34
->fn
->fd
.query_base_addr
;
453 ret
= rmi_read_block(f34
->fn
->rmi_dev
,
458 dev_err(&f34
->fn
->dev
,
459 "%s: Failed to read query 0\n", __func__
);
463 offset
= (query_0
& 0x7) + 1;
465 ret
= rmi_read_block(f34
->fn
->rmi_dev
,
470 dev_err(&f34
->fn
->dev
, "%s: Failed to read queries 1 to 7\n",
475 f34
->bootloader_id
[0] = query_1_7
.bl_minor_revision
;
476 f34
->bootloader_id
[1] = query_1_7
.bl_major_revision
;
478 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
, "Bootloader V%d.%d\n",
479 f34
->bootloader_id
[1], f34
->bootloader_id
[0]);
484 static int rmi_f34v7_read_queries(struct f34_data
*f34
)
492 struct f34v7_query_1_7 query_1_7
;
494 base
= f34
->fn
->fd
.query_base_addr
;
496 ret
= rmi_read_block(f34
->fn
->rmi_dev
,
501 dev_err(&f34
->fn
->dev
,
502 "%s: Failed to read query 0\n", __func__
);
506 offset
= (query_0
& 0x07) + 1;
508 ret
= rmi_read_block(f34
->fn
->rmi_dev
,
513 dev_err(&f34
->fn
->dev
, "%s: Failed to read queries 1 to 7\n",
518 f34
->bootloader_id
[0] = query_1_7
.bl_minor_revision
;
519 f34
->bootloader_id
[1] = query_1_7
.bl_major_revision
;
521 f34
->v7
.block_size
= le16_to_cpu(query_1_7
.block_size
);
522 f34
->v7
.flash_config_length
=
523 le16_to_cpu(query_1_7
.flash_config_length
);
524 f34
->v7
.payload_length
= le16_to_cpu(query_1_7
.payload_length
);
526 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
, "%s: f34->v7.block_size = %d\n",
527 __func__
, f34
->v7
.block_size
);
529 f34
->v7
.has_display_cfg
= query_1_7
.partition_support
[1] & HAS_DISP_CFG
;
530 f34
->v7
.has_guest_code
=
531 query_1_7
.partition_support
[1] & HAS_GUEST_CODE
;
533 if (query_0
& HAS_CONFIG_ID
) {
534 u8 f34_ctrl
[CONFIG_ID_SIZE
];
536 ret
= rmi_read_block(f34
->fn
->rmi_dev
,
537 f34
->fn
->fd
.control_base_addr
,
543 /* Eat leading zeros */
544 for (i
= 0; i
< sizeof(f34_ctrl
) - 1 && !f34_ctrl
[i
]; i
++)
547 snprintf(f34
->configuration_id
, sizeof(f34
->configuration_id
),
548 "%*phN", (int)sizeof(f34_ctrl
) - i
, f34_ctrl
+ i
);
550 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
, "Configuration ID: %s\n",
551 f34
->configuration_id
);
554 f34
->v7
.partitions
= 0;
555 for (i
= 0; i
< sizeof(query_1_7
.partition_support
); i
++)
556 f34
->v7
.partitions
+= hweight8(query_1_7
.partition_support
[i
]);
558 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
, "%s: Supported partitions: %*ph\n",
559 __func__
, sizeof(query_1_7
.partition_support
),
560 query_1_7
.partition_support
);
563 f34
->v7
.partition_table_bytes
= f34
->v7
.partitions
* 8 + 2;
565 f34
->v7
.read_config_buf
= devm_kzalloc(&f34
->fn
->dev
,
566 f34
->v7
.partition_table_bytes
,
568 if (!f34
->v7
.read_config_buf
) {
569 f34
->v7
.read_config_buf_size
= 0;
573 f34
->v7
.read_config_buf_size
= f34
->v7
.partition_table_bytes
;
574 ptable
= f34
->v7
.read_config_buf
;
576 ret
= rmi_f34v7_read_partition_table(f34
);
578 dev_err(&f34
->fn
->dev
, "%s: Failed to read partition table\n",
583 rmi_f34v7_parse_partition_table(f34
, ptable
,
584 &f34
->v7
.blkcount
, &f34
->v7
.phyaddr
);
589 static int rmi_f34v7_check_bl_config_size(struct f34_data
*f34
)
593 block_count
= f34
->v7
.img
.bl_config
.size
/ f34
->v7
.block_size
;
594 f34
->update_size
+= block_count
;
596 if (block_count
!= f34
->v7
.blkcount
.bl_config
) {
597 dev_err(&f34
->fn
->dev
, "Bootloader config size mismatch\n");
604 static int rmi_f34v7_erase_all(struct f34_data
*f34
)
608 dev_info(&f34
->fn
->dev
, "Erasing firmware...\n");
610 init_completion(&f34
->v7
.cmd_done
);
612 ret
= rmi_f34v7_write_command(f34
, v7_CMD_ERASE_ALL
);
616 ret
= rmi_f34v7_check_command_status(f34
, F34_ERASE_WAIT_MS
);
623 static int rmi_f34v7_read_blocks(struct f34_data
*f34
,
624 u16 block_cnt
, u8 command
)
631 u16 remaining
= block_cnt
;
632 u16 block_number
= 0;
635 base
= f34
->fn
->fd
.data_base_addr
;
637 ret
= rmi_f34v7_write_partition_id(f34
, command
);
641 ret
= rmi_write_block(f34
->fn
->rmi_dev
,
642 base
+ V7_BLOCK_NUMBER_OFFSET
,
643 &block_number
, sizeof(block_number
));
645 dev_err(&f34
->fn
->dev
, "%s: Failed to write block number\n",
650 max_transfer
= min(f34
->v7
.payload_length
,
651 (u16
)(PAGE_SIZE
/ f34
->v7
.block_size
));
654 transfer
= min(remaining
, max_transfer
);
655 put_unaligned_le16(transfer
, &length
);
657 ret
= rmi_write_block(f34
->fn
->rmi_dev
,
658 base
+ V7_TRANSFER_LENGTH_OFFSET
,
659 &length
, sizeof(length
));
661 dev_err(&f34
->fn
->dev
,
662 "%s: Write transfer length fail (%d remaining)\n",
663 __func__
, remaining
);
667 init_completion(&f34
->v7
.cmd_done
);
669 ret
= rmi_f34v7_write_command(f34
, command
);
673 ret
= rmi_f34v7_check_command_status(f34
, F34_ENABLE_WAIT_MS
);
677 ret
= rmi_read_block(f34
->fn
->rmi_dev
,
678 base
+ V7_PAYLOAD_OFFSET
,
679 &f34
->v7
.read_config_buf
[index
],
680 transfer
* f34
->v7
.block_size
);
682 dev_err(&f34
->fn
->dev
,
683 "%s: Read block failed (%d blks remaining)\n",
684 __func__
, remaining
);
688 index
+= (transfer
* f34
->v7
.block_size
);
689 remaining
-= transfer
;
695 static int rmi_f34v7_write_f34v7_blocks(struct f34_data
*f34
,
696 const void *block_ptr
, u16 block_cnt
,
704 u16 remaining
= block_cnt
;
705 u16 block_number
= 0;
707 base
= f34
->fn
->fd
.data_base_addr
;
709 ret
= rmi_f34v7_write_partition_id(f34
, command
);
713 ret
= rmi_write_block(f34
->fn
->rmi_dev
,
714 base
+ V7_BLOCK_NUMBER_OFFSET
,
715 &block_number
, sizeof(block_number
));
717 dev_err(&f34
->fn
->dev
, "%s: Failed to write block number\n",
722 if (f34
->v7
.payload_length
> (PAGE_SIZE
/ f34
->v7
.block_size
))
723 max_transfer
= PAGE_SIZE
/ f34
->v7
.block_size
;
725 max_transfer
= f34
->v7
.payload_length
;
728 transfer
= min(remaining
, max_transfer
);
729 put_unaligned_le16(transfer
, &length
);
731 init_completion(&f34
->v7
.cmd_done
);
733 ret
= rmi_write_block(f34
->fn
->rmi_dev
,
734 base
+ V7_TRANSFER_LENGTH_OFFSET
,
735 &length
, sizeof(length
));
737 dev_err(&f34
->fn
->dev
,
738 "%s: Write transfer length fail (%d remaining)\n",
739 __func__
, remaining
);
743 ret
= rmi_f34v7_write_command(f34
, command
);
747 ret
= rmi_write_block(f34
->fn
->rmi_dev
,
748 base
+ V7_PAYLOAD_OFFSET
,
749 block_ptr
, transfer
* f34
->v7
.block_size
);
751 dev_err(&f34
->fn
->dev
,
752 "%s: Failed writing data (%d blks remaining)\n",
753 __func__
, remaining
);
757 ret
= rmi_f34v7_check_command_status(f34
, F34_ENABLE_WAIT_MS
);
761 block_ptr
+= (transfer
* f34
->v7
.block_size
);
762 remaining
-= transfer
;
763 f34
->update_progress
+= transfer
;
764 f34
->update_status
= (f34
->update_progress
* 100) /
771 static int rmi_f34v7_write_config(struct f34_data
*f34
)
773 return rmi_f34v7_write_f34v7_blocks(f34
, f34
->v7
.config_data
,
774 f34
->v7
.config_block_count
,
775 v7_CMD_WRITE_CONFIG
);
778 static int rmi_f34v7_write_ui_config(struct f34_data
*f34
)
780 f34
->v7
.config_area
= v7_UI_CONFIG_AREA
;
781 f34
->v7
.config_data
= f34
->v7
.img
.ui_config
.data
;
782 f34
->v7
.config_size
= f34
->v7
.img
.ui_config
.size
;
783 f34
->v7
.config_block_count
= f34
->v7
.config_size
/ f34
->v7
.block_size
;
785 return rmi_f34v7_write_config(f34
);
788 static int rmi_f34v7_write_dp_config(struct f34_data
*f34
)
790 f34
->v7
.config_area
= v7_DP_CONFIG_AREA
;
791 f34
->v7
.config_data
= f34
->v7
.img
.dp_config
.data
;
792 f34
->v7
.config_size
= f34
->v7
.img
.dp_config
.size
;
793 f34
->v7
.config_block_count
= f34
->v7
.config_size
/ f34
->v7
.block_size
;
795 return rmi_f34v7_write_config(f34
);
798 static int rmi_f34v7_write_guest_code(struct f34_data
*f34
)
800 return rmi_f34v7_write_f34v7_blocks(f34
, f34
->v7
.img
.guest_code
.data
,
801 f34
->v7
.img
.guest_code
.size
/
803 v7_CMD_WRITE_GUEST_CODE
);
806 static int rmi_f34v7_write_flash_config(struct f34_data
*f34
)
810 f34
->v7
.config_area
= v7_FLASH_CONFIG_AREA
;
811 f34
->v7
.config_data
= f34
->v7
.img
.fl_config
.data
;
812 f34
->v7
.config_size
= f34
->v7
.img
.fl_config
.size
;
813 f34
->v7
.config_block_count
= f34
->v7
.config_size
/ f34
->v7
.block_size
;
815 if (f34
->v7
.config_block_count
!= f34
->v7
.blkcount
.fl_config
) {
816 dev_err(&f34
->fn
->dev
, "%s: Flash config size mismatch\n",
821 init_completion(&f34
->v7
.cmd_done
);
823 ret
= rmi_f34v7_write_config(f34
);
830 static int rmi_f34v7_write_partition_table(struct f34_data
*f34
)
835 block_count
= f34
->v7
.blkcount
.bl_config
;
836 f34
->v7
.config_area
= v7_BL_CONFIG_AREA
;
837 f34
->v7
.config_size
= f34
->v7
.block_size
* block_count
;
838 devm_kfree(&f34
->fn
->dev
, f34
->v7
.read_config_buf
);
839 f34
->v7
.read_config_buf
= devm_kzalloc(&f34
->fn
->dev
,
840 f34
->v7
.config_size
, GFP_KERNEL
);
841 if (!f34
->v7
.read_config_buf
) {
842 f34
->v7
.read_config_buf_size
= 0;
846 f34
->v7
.read_config_buf_size
= f34
->v7
.config_size
;
848 ret
= rmi_f34v7_read_blocks(f34
, block_count
, v7_CMD_READ_CONFIG
);
852 ret
= rmi_f34v7_write_flash_config(f34
);
856 f34
->v7
.config_area
= v7_BL_CONFIG_AREA
;
857 f34
->v7
.config_data
= f34
->v7
.read_config_buf
;
858 f34
->v7
.config_size
= f34
->v7
.img
.bl_config
.size
;
859 f34
->v7
.config_block_count
= f34
->v7
.config_size
/ f34
->v7
.block_size
;
861 ret
= rmi_f34v7_write_config(f34
);
868 static int rmi_f34v7_write_firmware(struct f34_data
*f34
)
872 blk_count
= f34
->v7
.img
.ui_firmware
.size
/ f34
->v7
.block_size
;
874 return rmi_f34v7_write_f34v7_blocks(f34
, f34
->v7
.img
.ui_firmware
.data
,
875 blk_count
, v7_CMD_WRITE_FW
);
878 static void rmi_f34v7_parse_img_header_10_bl_container(struct f34_data
*f34
,
882 int num_of_containers
;
884 unsigned int container_id
;
887 const struct container_descriptor
*descriptor
;
889 num_of_containers
= f34
->v7
.img
.bootloader
.size
/ 4 - 1;
891 for (i
= 1; i
<= num_of_containers
; i
++) {
892 addr
= get_unaligned_le32(f34
->v7
.img
.bootloader
.data
+ i
* 4);
893 descriptor
= image
+ addr
;
894 container_id
= le16_to_cpu(descriptor
->container_id
);
895 content
= image
+ le32_to_cpu(descriptor
->content_address
);
896 length
= le32_to_cpu(descriptor
->content_length
);
897 switch (container_id
) {
898 case BL_CONFIG_CONTAINER
:
899 case GLOBAL_PARAMETERS_CONTAINER
:
900 f34
->v7
.img
.bl_config
.data
= content
;
901 f34
->v7
.img
.bl_config
.size
= length
;
903 case BL_LOCKDOWN_INFO_CONTAINER
:
904 case DEVICE_CONFIG_CONTAINER
:
905 f34
->v7
.img
.lockdown
.data
= content
;
906 f34
->v7
.img
.lockdown
.size
= length
;
914 static void rmi_f34v7_parse_image_header_10(struct f34_data
*f34
)
917 unsigned int num_of_containers
;
920 unsigned int container_id
;
922 const void *image
= f34
->v7
.image
;
924 const struct container_descriptor
*descriptor
;
925 const struct image_header_10
*header
= image
;
927 f34
->v7
.img
.checksum
= le32_to_cpu(header
->checksum
);
929 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
, "%s: f34->v7.img.checksum=%X\n",
930 __func__
, f34
->v7
.img
.checksum
);
932 /* address of top level container */
933 offset
= le32_to_cpu(header
->top_level_container_start_addr
);
934 descriptor
= image
+ offset
;
936 /* address of top level container content */
937 offset
= le32_to_cpu(descriptor
->content_address
);
938 num_of_containers
= le32_to_cpu(descriptor
->content_length
) / 4;
940 for (i
= 0; i
< num_of_containers
; i
++) {
941 addr
= get_unaligned_le32(image
+ offset
);
943 descriptor
= image
+ addr
;
944 container_id
= le16_to_cpu(descriptor
->container_id
);
945 content
= image
+ le32_to_cpu(descriptor
->content_address
);
946 length
= le32_to_cpu(descriptor
->content_length
);
948 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
949 "%s: container_id=%d, length=%d\n", __func__
,
950 container_id
, length
);
952 switch (container_id
) {
954 case CORE_CODE_CONTAINER
:
955 f34
->v7
.img
.ui_firmware
.data
= content
;
956 f34
->v7
.img
.ui_firmware
.size
= length
;
958 case UI_CONFIG_CONTAINER
:
959 case CORE_CONFIG_CONTAINER
:
960 f34
->v7
.img
.ui_config
.data
= content
;
961 f34
->v7
.img
.ui_config
.size
= length
;
964 f34
->v7
.img
.bl_version
= *content
;
965 f34
->v7
.img
.bootloader
.data
= content
;
966 f34
->v7
.img
.bootloader
.size
= length
;
967 rmi_f34v7_parse_img_header_10_bl_container(f34
, image
);
969 case GUEST_CODE_CONTAINER
:
970 f34
->v7
.img
.contains_guest_code
= true;
971 f34
->v7
.img
.guest_code
.data
= content
;
972 f34
->v7
.img
.guest_code
.size
= length
;
974 case DISPLAY_CONFIG_CONTAINER
:
975 f34
->v7
.img
.contains_display_cfg
= true;
976 f34
->v7
.img
.dp_config
.data
= content
;
977 f34
->v7
.img
.dp_config
.size
= length
;
979 case FLASH_CONFIG_CONTAINER
:
980 f34
->v7
.img
.contains_flash_config
= true;
981 f34
->v7
.img
.fl_config
.data
= content
;
982 f34
->v7
.img
.fl_config
.size
= length
;
984 case GENERAL_INFORMATION_CONTAINER
:
985 f34
->v7
.img
.contains_firmware_id
= true;
986 f34
->v7
.img
.firmware_id
=
987 get_unaligned_le32(content
+ 4);
995 static int rmi_f34v7_parse_image_info(struct f34_data
*f34
)
997 const struct image_header_10
*header
= f34
->v7
.image
;
999 memset(&f34
->v7
.img
, 0x00, sizeof(f34
->v7
.img
));
1001 rmi_dbg(RMI_DEBUG_FN
, &f34
->fn
->dev
,
1002 "%s: header->major_header_version = %d\n",
1003 __func__
, header
->major_header_version
);
1005 switch (header
->major_header_version
) {
1006 case IMAGE_HEADER_VERSION_10
:
1007 rmi_f34v7_parse_image_header_10(f34
);
1010 dev_err(&f34
->fn
->dev
, "Unsupported image file format %02X\n",
1011 header
->major_header_version
);
1015 if (!f34
->v7
.img
.contains_flash_config
) {
1016 dev_err(&f34
->fn
->dev
, "%s: No flash config in fw image\n",
1021 rmi_f34v7_parse_partition_table(f34
, f34
->v7
.img
.fl_config
.data
,
1022 &f34
->v7
.img
.blkcount
, &f34
->v7
.img
.phyaddr
);
1027 int rmi_f34v7_do_reflash(struct f34_data
*f34
, const struct firmware
*fw
)
1031 f34
->fn
->rmi_dev
->driver
->set_irq_bits(f34
->fn
->rmi_dev
,
1034 rmi_f34v7_read_queries_bl_version(f34
);
1036 f34
->v7
.image
= fw
->data
;
1037 f34
->update_progress
= 0;
1038 f34
->update_size
= 0;
1040 ret
= rmi_f34v7_parse_image_info(f34
);
1044 ret
= rmi_f34v7_check_bl_config_size(f34
);
1048 ret
= rmi_f34v7_erase_all(f34
);
1052 ret
= rmi_f34v7_write_partition_table(f34
);
1055 dev_info(&f34
->fn
->dev
, "%s: Partition table programmed\n", __func__
);
1058 * Reset to reload partition table - as the previous firmware has been
1059 * erased, we remain in bootloader mode.
1061 ret
= rmi_scan_pdt(f34
->fn
->rmi_dev
, NULL
, rmi_initial_reset
);
1063 dev_warn(&f34
->fn
->dev
, "RMI reset failed!\n");
1065 dev_info(&f34
->fn
->dev
, "Writing firmware (%d bytes)...\n",
1066 f34
->v7
.img
.ui_firmware
.size
);
1068 ret
= rmi_f34v7_write_firmware(f34
);
1072 dev_info(&f34
->fn
->dev
, "Writing config (%d bytes)...\n",
1073 f34
->v7
.img
.ui_config
.size
);
1075 f34
->v7
.config_area
= v7_UI_CONFIG_AREA
;
1076 ret
= rmi_f34v7_write_ui_config(f34
);
1080 if (f34
->v7
.has_display_cfg
&& f34
->v7
.img
.contains_display_cfg
) {
1081 dev_info(&f34
->fn
->dev
, "Writing display config...\n");
1083 ret
= rmi_f34v7_write_dp_config(f34
);
1088 if (f34
->v7
.has_guest_code
&& f34
->v7
.img
.contains_guest_code
) {
1089 dev_info(&f34
->fn
->dev
, "Writing guest code...\n");
1091 ret
= rmi_f34v7_write_guest_code(f34
);
1099 static int rmi_f34v7_enter_flash_prog(struct f34_data
*f34
)
1103 f34
->fn
->rmi_dev
->driver
->set_irq_bits(f34
->fn
->rmi_dev
, f34
->fn
->irq_mask
);
1105 ret
= rmi_f34v7_read_flash_status(f34
);
1109 if (f34
->v7
.in_bl_mode
) {
1110 dev_info(&f34
->fn
->dev
, "%s: Device in bootloader mode\n",
1115 init_completion(&f34
->v7
.cmd_done
);
1117 ret
= rmi_f34v7_write_command(f34
, v7_CMD_ENABLE_FLASH_PROG
);
1121 ret
= rmi_f34v7_check_command_status(f34
, F34_ENABLE_WAIT_MS
);
1128 int rmi_f34v7_start_reflash(struct f34_data
*f34
, const struct firmware
*fw
)
1132 f34
->v7
.config_area
= v7_UI_CONFIG_AREA
;
1133 f34
->v7
.image
= fw
->data
;
1135 ret
= rmi_f34v7_parse_image_info(f34
);
1139 dev_info(&f34
->fn
->dev
, "Firmware image OK\n");
1141 return rmi_f34v7_enter_flash_prog(f34
);
1144 int rmi_f34v7_probe(struct f34_data
*f34
)
1148 /* Read bootloader version */
1149 ret
= rmi_read_block(f34
->fn
->rmi_dev
,
1150 f34
->fn
->fd
.query_base_addr
+ V7_BOOTLOADER_ID_OFFSET
,
1152 sizeof(f34
->bootloader_id
));
1154 dev_err(&f34
->fn
->dev
, "%s: Failed to read bootloader ID\n",
1159 if (f34
->bootloader_id
[1] == '5') {
1160 f34
->bl_version
= 5;
1161 } else if (f34
->bootloader_id
[1] == '6') {
1162 f34
->bl_version
= 6;
1163 } else if (f34
->bootloader_id
[1] == 7) {
1164 f34
->bl_version
= 7;
1165 } else if (f34
->bootloader_id
[1] == 8) {
1166 f34
->bl_version
= 8;
1168 dev_err(&f34
->fn
->dev
,
1169 "%s: Unrecognized bootloader version: %d (%c) %d (%c)\n",
1171 f34
->bootloader_id
[0], f34
->bootloader_id
[0],
1172 f34
->bootloader_id
[1], f34
->bootloader_id
[1]);
1176 memset(&f34
->v7
.blkcount
, 0x00, sizeof(f34
->v7
.blkcount
));
1177 memset(&f34
->v7
.phyaddr
, 0x00, sizeof(f34
->v7
.phyaddr
));
1179 init_completion(&f34
->v7
.cmd_done
);
1181 ret
= rmi_f34v7_read_queries(f34
);