summaryrefslogtreecommitdiff
path: root/tcm/synaptics_touchcom_func_reflash.c
blob: 5115b737d2583019310863ac37bb1f64c50be202 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
// SPDX-License-Identifier: GPL-2.0
/*
 * Synaptics TCM touchscreen driver
 *
 * Copyright (C) 2017-2020 Synaptics Incorporated. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * INFORMATION CONTAINED IN THIS DOCUMENT IS PROVIDED "AS-IS," AND SYNAPTICS
 * EXPRESSLY DISCLAIMS ALL EXPRESS AND IMPLIED WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
 * AND ANY WARRANTIES OF NON-INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHTS.
 * IN NO EVENT SHALL SYNAPTICS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, PUNITIVE, OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OF THE INFORMATION CONTAINED IN THIS DOCUMENT, HOWEVER CAUSED
 * AND BASED ON ANY THEORY OF LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, AND EVEN IF SYNAPTICS WAS ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE. IF A TRIBUNAL OF COMPETENT JURISDICTION DOES
 * NOT PERMIT THE DISCLAIMER OF DIRECT DAMAGES OR ANY OTHER DAMAGES, SYNAPTICS'
 * TOTAL CUMULATIVE LIABILITY TO ANY PARTY SHALL NOT EXCEED ONE HUNDRED U.S.
 * DOLLARS.
 */

/**
 * @file synaptics_touchcom_func_reflash.c
 *
 * This file implements the fw reflash related functions of TouchBoot.
 * The declarations are available in synaptics_touchcom_func_reflash.h.
 */

#include "synaptics_touchcom_func_base.h"
#include "synaptics_touchcom_func_reflash.h"

/**
 * @section: Reflash relevant definitions
 *
 */
#define FLASH_READ_DELAY_MS (10)
#define FLASH_WRITE_DELAY_MS (20)
#define FLASH_ERASE_DELAY_MS (500)

#define BOOT_CONFIG_SIZE 8
#define BOOT_CONFIG_SLOTS 16

#define DO_NONE (0)
#define DO_UPDATE (1)

/**
 * syna_tcm_set_up_flash_access()
 *
 * Enter the bootloader fw if not in the mode.
 * Besides, get the necessary parameters in boot info.
 *
 * @param
 *    [ in] tcm_dev:      the device handle
 *    [out] reflash_data: data blob for reflash
 *
 * @return
 *    Result of image file comparison
 */
static int syna_tcm_set_up_flash_access(struct tcm_dev *tcm_dev,
		struct tcm_reflash_data_blob *reflash_data)
{
	int retval;
	unsigned int temp;
	struct tcm_identification_info id_info;
	struct tcm_boot_info *boot_info;
	unsigned int wr_chunk;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!reflash_data) {
		LOGE("Invalid reflash data blob\n");
		return _EINVAL;
	}

	LOGI("Set up flash access\n");

	retval = syna_tcm_identify(tcm_dev, &id_info);
	if (retval < 0) {
		LOGE("Fail to do identification\n");
		return retval;
	}

	/* switch to bootloader mode */
	if (IS_APP_FW_MODE(id_info.mode)) {
		LOGI("Prepare to enter bootloader mode\n");

		retval = syna_tcm_switch_fw_mode(tcm_dev,
				MODE_BOOTLOADER,
				FW_MODE_SWITCH_DELAY_MS);
		if (retval < 0) {
			LOGE("Fail to enter bootloader mode\n");
			return retval;
		}
	}

	if (!IS_BOOTLOADER_MODE(tcm_dev->dev_mode)) {
		LOGE("Fail to enter bootloader mode (current: 0x%x)\n",
			tcm_dev->dev_mode);
		return retval;
	}

	boot_info = &tcm_dev->boot_info;

	/* get boot info to set up the flash access */
	retval = syna_tcm_get_boot_info(tcm_dev, boot_info);
	if (retval < 0) {
		LOGE("Fail to get boot info at mode 0x%x\n",
			id_info.mode);
		return retval;
	}

	wr_chunk = tcm_dev->max_wr_size;

	temp = boot_info->write_block_size_words;
	reflash_data->write_block_size = temp * 2;

	LOGI("Write block size: %d (words size: %d)\n",
		reflash_data->write_block_size, temp);

	temp = syna_pal_le2_to_uint(boot_info->erase_page_size_words);
	reflash_data->page_size = temp * 2;

	LOGI("Erase page size: %d (words size: %d)\n",
		reflash_data->page_size, temp);

	temp = syna_pal_le2_to_uint(boot_info->max_write_payload_size);
	reflash_data->max_write_payload_size = temp;

	LOGI("Max write flash data size: %d\n",
		reflash_data->max_write_payload_size);

	if (reflash_data->write_block_size > (wr_chunk - 9)) {
		LOGE("Write block size, %d, greater than chunk space, %d\n",
			reflash_data->write_block_size, (wr_chunk - 9));
		return _EINVAL;
	}

	if (reflash_data->write_block_size == 0) {
		LOGE("Invalid write block size %d\n",
			reflash_data->write_block_size);
		return _EINVAL;
	}

	if (reflash_data->page_size == 0) {
		LOGE("Invalid erase page size %d\n",
			reflash_data->page_size);
		return _EINVAL;
	}

	return 0;
}

/**
 * syna_tcm_compare_image_id_info()
 *
 * Compare the ID information between device and the image file,
 * and then determine the area to be updated.
 * The function should be called after parsing the image file.
 *
 * @param
 *    [ in] tcm_dev:       the device handle
 *    [ in] reflash_data:  data blob for reflash
 *
 * @return
 *    Blocks to be updated
 */
int syna_tcm_compare_image_id_info(struct tcm_dev *tcm_dev,
		struct tcm_reflash_data_blob *reflash_data)
{
	enum update_area result;
	unsigned int idx;
	unsigned int image_fw_id;
	unsigned int device_fw_id;
	unsigned char *image_config_id;
	unsigned char *device_config_id;
	struct app_config_header *header;
	const unsigned char *app_config_data;
	struct block_data *app_config;

	result = UPDATE_NONE;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!reflash_data) {
		LOGE("Invalid reflash_data\n");
		return _EINVAL;
	}

	app_config = &reflash_data->image_info.data[AREA_APP_CONFIG];

	if (app_config->size < sizeof(struct app_config_header)) {
		LOGE("Invalid application config in image file\n");
		return _EINVAL;
	}

	app_config_data = app_config->data;
	header = (struct app_config_header *)app_config_data;

	image_fw_id = syna_pal_le4_to_uint(header->build_id);
	device_fw_id = tcm_dev->packrat_number;

	LOGN("Device firmware ID: %d, image build id: %d\n",
		device_fw_id, image_fw_id);

	if (image_fw_id != device_fw_id) {
		LOGN("Image build ID and device fw ID mismatched\n");
		result = UPDATE_FIRMWARE_CONFIG;
		goto exit;
	}

	image_config_id = header->customer_config_id;
	device_config_id = tcm_dev->app_info.customer_config_id;

	for (idx = 0; idx < MAX_SIZE_CONFIG_ID; idx++) {
		if (image_config_id[idx] != device_config_id[idx]) {
			LOGN("Different Config ID\n");
			result = UPDATE_CONFIG_ONLY;
			goto exit;
		}
	}

	result = UPDATE_NONE;

exit:
	switch (result) {
	case UPDATE_FIRMWARE_CONFIG:
		LOGN("Update firmware and config\n");
		break;
	case UPDATE_CONFIG_ONLY:
		LOGN("Update config only\n");
		break;
	case UPDATE_NONE:
	default:
		LOGN("No need to do reflash\n");
		break;
	}

	return (int)result;
}

/**
 * syna_tcm_check_flash_boot_config()
 *
 * Check whether the same flash address of boot config in between the device
 * and the image file.
 *
 * @param
 *    [ in] boot_config:     block data of boot_config from image file
 *    [ in] boot_info:       data of boot info
 *    [ in] block_size:      max size of write block
 *
 * @return
 *    '0' represent no need to do reflash;
 *    '1' represent a positive result of checking;
 *    otherwise, negative value on error.
 */
static int syna_tcm_check_flash_boot_config(struct block_data *boot_config,
		struct tcm_boot_info *boot_info, unsigned int block_size)
{
	unsigned int start_block;
	unsigned int image_addr;
	unsigned int device_addr;

	if (!boot_config) {
		LOGE("Invalid boot_config block data\n");
		return _EINVAL;
	}

	if (!boot_info) {
		LOGE("Invalid boot_info\n");
		return _EINVAL;
	}

	if (boot_config->size < BOOT_CONFIG_SIZE) {
		LOGE("No valid BOOT_CONFIG size, %d, in image file\n",
			boot_config->size);
		return _EINVAL;
	}

	image_addr = boot_config->flash_addr;

	LOGD("Boot Config address in image file: 0x%x\n", image_addr);

	start_block = VALUE(boot_info->boot_config_start_block);
	device_addr = start_block * block_size;

	LOGD("Boot Config address in device: 0x%x\n", device_addr);

	return DO_NONE;
}

/**
 * syna_tcm_check_flash_app_config()
 *
 * Check whether the same flash address of app config in between the
 * device and the image file.
 *
 * @param
 *    [ in] app_config:      block data of app_config from image file
 *    [ in] app_info:        data of application info
 *    [ in] block_size:      max size of write block
 *
 * @return
 *    '0' represent no need to do reflash;
 *    '1' represent a positive result of checking;
 *    otherwise, negative value on error.
 */
static int syna_tcm_check_flash_app_config(struct block_data *app_config,
		struct tcm_application_info *app_info, unsigned int block_size)
{
	unsigned int temp;
	unsigned int image_addr;
	unsigned int image_size;
	unsigned int device_addr;
	unsigned int device_size;

	if (!app_config) {
		LOGE("Invalid app_config block data\n");
		return _EINVAL;
	}

	if (!app_info) {
		LOGE("Invalid app_info\n");
		return _EINVAL;
	}

	if (app_config->size == 0) {
		LOGD("No APP_CONFIG in image file\n");
		return DO_NONE;
	}

	image_addr = app_config->flash_addr;
	image_size = app_config->size;

	LOGD("App Config address in image file: 0x%x, size: %d\n",
		image_addr, image_size);

	temp = VALUE(app_info->app_config_start_write_block);
	device_addr = temp * block_size;
	device_size = VALUE(app_info->app_config_size);

	LOGD("App Config address in device: 0x%x, size: %d\n",
		device_addr, device_size);

	if (device_addr == 0 && device_size == 0)
		return DO_UPDATE;

	if (image_addr != device_addr)
		LOGW("App Config address mismatch, image:0x%x, dev:0x%x\n",
			image_addr, device_addr);

	if (image_size != device_size)
		LOGW("App Config address size mismatch, image:%d, dev:%d\n",
			image_size, device_size);

	return DO_UPDATE;
}

/**
 * syna_tcm_check_flash_disp_config()
 *
 * Check whether the same flash address of display config in between the
 * device and the image file.
 *
 * @param
 *    [ in] disp_config:     block data of disp_config from image file
 *    [ in] boot_info:       data of boot info
 *    [ in] block_size:      max size of write block
 *
 * @return
 *    '0' represent no need to do reflash;
 *    '1' represent a positive result of checking;
 *    otherwise, negative value on error.
 */
static int syna_tcm_check_flash_disp_config(struct block_data *disp_config,
		struct tcm_boot_info *boot_info, unsigned int block_size)
{
	unsigned int temp;
	unsigned int image_addr;
	unsigned int image_size;
	unsigned int device_addr;
	unsigned int device_size;

	if (!disp_config) {
		LOGE("Invalid disp_config block data\n");
		return _EINVAL;
	}

	if (!boot_info) {
		LOGE("Invalid boot_info\n");
		return _EINVAL;
	}

	/* disp_config area may not be included in all product */
	if (disp_config->size == 0) {
		LOGD("No DISP_CONFIG in image file\n");
		return DO_NONE;
	}

	image_addr = disp_config->flash_addr;
	image_size = disp_config->size;

	LOGD("Disp Config address in image file: 0x%x, size: %d\n",
		image_addr, image_size);

	temp = VALUE(boot_info->display_config_start_block);
	device_addr = temp * block_size;

	temp = VALUE(boot_info->display_config_length_blocks);
	device_size = temp * block_size;

	LOGD("Disp Config address in device: 0x%x, size: %d\n",
		device_addr, device_size);

	if (image_addr != device_addr)
		LOGW("Disp Config address mismatch, image:0x%x, dev:0x%x\n",
			image_addr, device_addr);

	if (image_size != device_size)
		LOGW("Disp Config address size mismatch, image:%d, dev:%d\n",
			image_size, device_size);

	return DO_UPDATE;
}

/**
 * syna_tcm_check_flash_app_code()
 *
 * Check whether the valid size of app firmware in the image file
 *
 * @param
 *    [ in] app_code:      block data of app_code from image file
 *
 * @return
 *    '0' represent no need to do reflash;
 *    '1' represent a positive result of checking;
 *    otherwise, negative value on error.
 */
static int syna_tcm_check_flash_app_code(struct block_data *app_code)
{
	if (!app_code) {
		LOGE("Invalid app_code block data\n");
		return _EINVAL;
	}

	if (app_code->size == 0) {
		LOGD("No %s in image file\n", AREA_ID_STR(app_code->id));
		return _EINVAL;
	}

	return DO_UPDATE;
}

/**
 * syna_tcm_check_flash_openshort()
 *
 * Check whether the valid size of openshort area in the image file
 *
 * @param
 *    [ in] open_short:      block data of open_short from image file
 *
 * @return
 *    '0' represent no need to do reflash;
 *    '1' represent a positive result of checking;
 *    otherwise, negative value on error.
 */
static int syna_tcm_check_flash_openshort(struct block_data *open_short)
{
	if (!open_short) {
		LOGE("Invalid open_short block data\n");
		return _EINVAL;
	}

	/* open_short area may not be included in all product */
	if (open_short->size == 0) {
		LOGD("No %s in image file\n", AREA_ID_STR(open_short->id));
		return DO_NONE;
	}

	return DO_UPDATE;
}

/**
 * syna_tcm_check_flash_app_prod_test()
 *
 * Check whether the valid size of app prod_test area in the image file
 *
 * @param
 *    [ in] prod_test:  block data of app_prod_test from image file
 *
 * @return
 *    '0' represent no need to do reflash;
 *    '1' represent a positive result of checking;
 *    otherwise, negative value on error.
 */
static int syna_tcm_check_flash_app_prod_test(struct block_data *prod_test)
{
	if (!prod_test) {
		LOGE("Invalid app_prod_test block data\n");
		return _EINVAL;
	}

	/* app_prod_test area may not be included in all product */
	if (prod_test->size == 0) {
		LOGD("No %s in image file\n", AREA_ID_STR(prod_test->id));
		return DO_NONE;
	}

	return DO_UPDATE;
}

/**
 * syna_tcm_check_flash_ppdt()
 *
 * Check whether the valid size of ppdt area in the image file
 *
 * @param
 *    [ in] ppdt:      block data of PPDT from image file
 *
 * @return
 *    '0' represent no need to do reflash;
 *    '1' represent a positive result of checking;
 *    otherwise, negative value on error.
 */
static int syna_tcm_check_flash_ppdt(struct block_data *ppdt)
{
	if (!ppdt) {
		LOGE("Invalid ppdt block data\n");
		return _EINVAL;
	}

	/* open_short area may not be included in all product */
	if (ppdt->size == 0) {
		LOGD("No %s in image file\n", AREA_ID_STR(ppdt->id));
		return DO_NONE;
	}

	return DO_UPDATE;
}

/**
 * syna_tcm_check_flash_block()
 *
 * Dispatch to the proper helper to ensure the data of associated block area
 * is correct in between the device and the image file.
 *
 * @param
 *    [ in] tcm_dev:      the device handle
 *    [ in] reflash_data: data blob for reflash
 *    [ in] block:        target block area to check
 *
 * @return
 *    '0' represent no need to do reflash;
 *    '1' represent a positive result of checking;
 *    otherwise, negative value on error.
 */
static int syna_tcm_check_flash_block(struct tcm_dev *tcm_dev,
		struct tcm_reflash_data_blob *reflash_data,
		struct block_data *block)
{
	int retval = 0;
	struct tcm_application_info *app_info;
	struct tcm_boot_info *boot_info;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return DO_NONE;
	}

	if (!reflash_data) {
		LOGE("Invalid reflash data blob\n");
		return DO_NONE;
	}

	if (!block) {
		LOGE("Invalid block data\n");
		return DO_NONE;
	}

	app_info = &tcm_dev->app_info;
	boot_info = &tcm_dev->boot_info;

	switch (block->id) {
	case AREA_APP_CODE:
		retval = syna_tcm_check_flash_app_code(block);
		break;
	case AREA_APP_CONFIG:
		retval = syna_tcm_check_flash_app_config(block, app_info,
				reflash_data->write_block_size);
		break;
	case AREA_BOOT_CONFIG:
		retval = syna_tcm_check_flash_boot_config(block, boot_info,
				reflash_data->write_block_size);
		break;
	case AREA_DISP_CONFIG:
		retval = syna_tcm_check_flash_disp_config(block, boot_info,
				reflash_data->write_block_size);
		break;
	case AREA_OPEN_SHORT_TUNING:
		retval = syna_tcm_check_flash_openshort(block);
		break;
	case AREA_PROD_TEST:
		retval = syna_tcm_check_flash_app_prod_test(block);
		break;
	case AREA_PPDT:
		retval = syna_tcm_check_flash_ppdt(block);
		break;
	default:
		retval = DO_NONE;
		break;
	}

	return retval;
}

/**
 * syna_tcm_get_flash_data_location()
 *
 * Return the address and length of the specified data area
 * in the flash memory.
 *
 * @param
 *    [ in] tcm_dev: the device handle
 *    [ in] area:    specified area in flash memory
 *    [out] addr:    the flash address of the specified area returned
 *    [out] len:     the size of the specified area returned
 *
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_get_flash_data_location(struct tcm_dev *tcm_dev,
		enum flash_area area, unsigned int *addr, unsigned int *len)
{
	int retval = 0;
	unsigned char resp_code;
	unsigned char payload;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	switch (area) {
	case AREA_CUSTOM_LCM:
		payload = FLASH_LCM_DATA;
		break;
	case AREA_CUSTOM_OEM:
		payload = FLASH_OEM_DATA;
		break;
	case AREA_PPDT:
		payload = FLASH_PPDT_DATA;
		break;
	case AREA_FORCE_TUNING:
		payload = FLASH_FORCE_CALIB_DATA;
		break;
	case AREA_OPEN_SHORT_TUNING:
		payload = FLASH_OPEN_SHORT_TUNING_DATA;
		break;
	default:
		LOGE("Invalid flash area %d\n", area);
		return _EINVAL;
	}

	retval = tcm_dev->write_message(tcm_dev,
			CMD_GET_DATA_LOCATION,
			&payload,
			sizeof(payload),
			&resp_code,
			tcm_dev->msg_data.default_resp_reading);
	if (retval < 0) {
		LOGE("Fail to send command 0x%02x\n",
			CMD_GET_DATA_LOCATION);
		goto exit;
	}

	if (tcm_dev->resp_buf.data_length != 4) {
		LOGE("Invalid data length %d\n",
			tcm_dev->resp_buf.data_length);
		retval = _EINVAL;
		goto exit;
	}

	*addr = syna_pal_le2_to_uint(&tcm_dev->resp_buf.buf[0]);
	*len = syna_pal_le2_to_uint(&tcm_dev->resp_buf.buf[2]);

	retval = 0;

exit:
	return retval;
}

/**
 * syna_tcm_reflash_send_command()
 *
 * Helper to wrap up the write_message() function.
 *
 * @param
 *    [ in] tcm_dev:        the device handle
 *    [ in] command:        given command code
 *    [ in] payload:        payload data, if any
 *    [ in] payload_len:    length of payload data
 *    [ in] delay_ms_resp:  delay time to get the response of command
 *
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_reflash_send_command(struct tcm_dev *tcm_dev,
		unsigned char command, unsigned char *payload,
		unsigned int payload_len, unsigned int delay_ms_resp)
{
	int retval = 0;
	unsigned char resp_code;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!IS_BOOTLOADER_MODE(tcm_dev->dev_mode)) {
		LOGE("Device is not in BL mode, 0x%x\n", tcm_dev->dev_mode);
		retval = _EINVAL;
	}

	retval = tcm_dev->write_message(tcm_dev,
			command,
			payload,
			payload_len,
			&resp_code,
			delay_ms_resp);
	if (retval < 0) {
		LOGE("Fail to send command 0x%02x\n", command);
		goto exit;
	}

exit:
	return retval;
}

/**
 * syna_tcm_read_flash()
 *
 * Implement the bootloader command to read specified data from flash memory.
 *
 * Reads to the protected bootloader code or application code areas will read
 * as 0. If the number of words requested is too large, it may be truncated to
 * an defined maximum read size.
 *
 * @param
 *    [ in] tcm_dev:      the device handle
 *    [ in] address:      the address in flash memory to read
 *    [out] rd_data:      data retrieved
 *    [ in] rd_len:       length of data to be read
 *    [ in] rd_delay_ms:  a short delay after the command executed
 *                        set 'DEFAULT_FLASH_READ_DELAY' to use default,
 *                        which is 10 us;
 *                        set '0' or 'RESP_IN_ATTN' to select ATTN-driven.
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_read_flash(struct tcm_dev *tcm_dev,
		unsigned int address, unsigned char *rd_data,
		unsigned int rd_len, unsigned int rd_delay_ms)
{
	int retval = 0;
	unsigned int length_words;
	unsigned int flash_addr_words;
	unsigned char out[6] = { 0 };
	unsigned int delay_ms = 0;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!rd_data) {
		LOGE("Invalid rd_data buffer\n");
		return _EINVAL;
	}

	if (address == 0 || rd_len == 0) {
		LOGE("Invalid flash address and length\n");
		retval = _EINVAL;
		goto exit;
	}

	length_words = rd_len / 2;
	flash_addr_words = address / 2;

	LOGD("Flash address: 0x%x (words: 0x%x), size: %d (words: %d)\n",
		address, flash_addr_words, rd_len, length_words);

	out[0] = (unsigned char)flash_addr_words;
	out[1] = (unsigned char)(flash_addr_words >> 8);
	out[2] = (unsigned char)(flash_addr_words >> 16);
	out[3] = (unsigned char)(flash_addr_words >> 24);
	out[4] = (unsigned char)length_words;
	out[5] = (unsigned char)(length_words >> 8);

	if (rd_delay_ms == DEFAULT_FLASH_READ_DELAY)
		delay_ms = FLASH_READ_DELAY_MS;
	else
		delay_ms = rd_delay_ms;

	if (delay_ms == RESP_IN_ATTN) {
		LOGD("xfer: %d, delay: ATTN-driven\n", length_words);
	} else {
		delay_ms = (delay_ms * length_words) / 1000;
		LOGD("xfer: %d, delay: %d\n", length_words, delay_ms);
	}

	retval = syna_tcm_reflash_send_command(tcm_dev,
			CMD_READ_FLASH,
			out,
			sizeof(out),
			delay_ms);
	if (retval < 0) {
		LOGE("Fail to read flash data from addr 0x%x, size %d\n",
			address, rd_len);
		goto exit;
	}

	if (tcm_dev->resp_buf.data_length != rd_len) {
		LOGE("Fail to read requested length %d, rd_len %d\n",
			tcm_dev->resp_buf.data_length, rd_len);
		retval = _EIO;
		goto exit;
	}

	retval = syna_pal_mem_cpy(rd_data,
			rd_len,
			tcm_dev->resp_buf.buf,
			tcm_dev->resp_buf.buf_size,
			rd_len);
	if (retval < 0) {
		LOGE("Fail to copy read data, size %d\n", rd_len);
		goto exit;
	}

exit:
	return retval;
}

/**
 * syna_tcm_read_flash_boot_config()
 *
 * Read the data of boot config area in the flash memory.
 *
 * @param
 *    [ in] tcm_dev:      the device handle
 *    [ in] reflash_data: data blob for reflash
 *    [out] rd_data:      buffer used for storing the retrieved data
 *    [ in] rd_delay_us:  delay time to access data in flash memory
 *
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_read_flash_boot_config(struct tcm_dev *tcm_dev,
		struct tcm_reflash_data_blob *reflash_data,
		struct tcm_buffer *rd_data, unsigned int rd_delay_us)
{
	int retval;
	unsigned int temp;
	unsigned int addr = 0;
	unsigned int length = 0;
	struct tcm_boot_info *boot_info;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!reflash_data) {
		LOGE("Invalid reflash data blob\n");
		return _EINVAL;
	}

	if (!rd_data) {
		LOGE("Invalid read data buffer\n");
		return _EINVAL;
	}

	boot_info = &tcm_dev->boot_info;

	if (IS_APP_FW_MODE(tcm_dev->dev_mode)) {
		LOGE("BOOT_CONFIG not available in app fw mode %d\n",
			tcm_dev->dev_mode);
		return _EINVAL;
	}

	temp = VALUE(boot_info->boot_config_start_block);
	addr = temp * reflash_data->write_block_size;
	length = BOOT_CONFIG_SIZE * BOOT_CONFIG_SLOTS;

	if (addr == 0 || length == 0) {
		LOGE("BOOT_CONFIG data area unavailable\n");
		retval = _EINVAL;
		goto exit;
	}

	LOGD("BOOT_CONFIG address: 0x%x, length: %d\n", addr, length);

	retval = syna_tcm_buf_alloc(rd_data, length);
	if (retval < 0) {
		LOGE("Fail to allocate memory for rd_data buffer\n");
		goto exit;
	}

	retval = syna_tcm_read_flash(tcm_dev, addr, rd_data->buf,
			length, rd_delay_us);
	if (retval < 0) {
		LOGE("Fail to read BOOT_CONFIG area (addr: 0x%x, length: %d)\n",
			addr, length);
		goto exit;
	}

	rd_data->data_length = length;

exit:
	return retval;
}

/**
 * syna_tcm_read_flash_app_config()
 *
 * Read the data of app config area in the flash memory.
 *
 * @param
 *    [ in] tcm_dev:      the device handle
 *    [ in] reflash_data: data blob for reflash
 *    [out] rd_data:      buffer used for storing the retrieved data
 *    [ in] rd_delay_us:  delay time to access data in flash memory
 *
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_read_flash_app_config(struct tcm_dev *tcm_dev,
		struct tcm_reflash_data_blob *reflash_data,
		struct tcm_buffer *rd_data, unsigned int rd_delay_us)
{
	int retval;
	unsigned int temp;
	unsigned int addr = 0;
	unsigned int length = 0;
	struct tcm_application_info *app_info;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!reflash_data) {
		LOGE("Invalid reflash data blob\n");
		return _EINVAL;
	}

	if (!rd_data) {
		LOGE("Invalid read data buffer\n");
		return _EINVAL;
	}

	app_info = &tcm_dev->app_info;

	if (IS_APP_FW_MODE(tcm_dev->dev_mode)) {
		LOGE("APP_CONFIG not available in app fw mode %d\n",
			tcm_dev->dev_mode);
		return _EINVAL;
	}

	temp = VALUE(app_info->app_config_start_write_block);
	addr = temp * reflash_data->write_block_size;
	length = VALUE(app_info->app_config_size);

	if (addr == 0 || length == 0) {
		LOGE("APP_CONFIG data area unavailable\n");
		retval = _EINVAL;
		goto exit;
	}

	LOGD("APP_CONFIG address: 0x%x, length: %d\n", addr, length);

	retval = syna_tcm_buf_alloc(rd_data, length);
	if (retval < 0) {
		LOGE("Fail to allocate memory for rd_data buffer\n");
		goto exit;
	}

	retval = syna_tcm_read_flash(tcm_dev, addr, rd_data->buf,
			length, rd_delay_us);
	if (retval < 0) {
		LOGE("Fail to read APP_CONFIG area (addr: 0x%x, length: %d)\n",
			addr, length);
		goto exit;
	}

	rd_data->data_length = length;

exit:
	return retval;
}

/**
 * syna_tcm_read_flash_disp_config()
 *
 * Read the data of display config area in the flash memory.
 *
 * @param
 *    [ in] tcm_dev:       the device handle
 *    [ in] reflash_data:  data blob for reflash
 *    [out] rd_data:       buffer used for storing the retrieved data
 *    [ in] rd_delay_us:   delay time to access data in flash memory
 *
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_read_flash_disp_config(struct tcm_dev *tcm_dev,
		struct tcm_reflash_data_blob *reflash_data,
		struct tcm_buffer *rd_data, unsigned int rd_delay_us)
{
	int retval;
	unsigned int temp;
	unsigned int addr = 0;
	unsigned int length = 0;
	struct tcm_boot_info *boot_info;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!reflash_data) {
		LOGE("Invalid reflash data blob\n");
		return _EINVAL;
	}

	if (!rd_data) {
		LOGE("Invalid read data buffer\n");
		return _EINVAL;
	}

	boot_info = &tcm_dev->boot_info;

	if (IS_APP_FW_MODE(tcm_dev->dev_mode)) {
		LOGE("DISP_CONFIG not available in app fw mode %d\n",
			tcm_dev->dev_mode);
		return _EINVAL;
	}

	temp = VALUE(boot_info->display_config_start_block);
	addr = temp * reflash_data->write_block_size;
	temp = VALUE(boot_info->display_config_length_blocks);
	length = temp * reflash_data->write_block_size;

	if (addr == 0 || length == 0) {
		LOGE("DISP_CONFIG data area unavailable\n");
		retval = _EINVAL;
		goto exit;
	}

	LOGD("DISP_CONFIG address: 0x%x, length: %d\n", addr, length);

	retval = syna_tcm_buf_alloc(rd_data, length);
	if (retval < 0) {
		LOGE("Fail to allocate memory for rd_data buffer\n");
		goto exit;
	}

	retval = syna_tcm_read_flash(tcm_dev, addr, rd_data->buf,
			length, rd_delay_us);
	if (retval < 0) {
		LOGE("Fail to read DISP_CONFIG area (addr: 0x%x, length: %d)\n",
			addr, length);
		goto exit;
	}

	rd_data->data_length = length;

exit:
	return retval;
}

/**
 * syna_tcm_read_flash_custom_otp()
 *
 * Read the data of custom OTP area in the flash memory.
 *
 * @param
 *    [ in] tcm_dev:      the device handle
 *    [ in] reflash_data: data blob for reflash
 *    [out] rd_data:      buffer used for storing the retrieved data
 *    [ in] rd_delay_us:  delay time to access data in flash memory
 *
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_read_flash_custom_otp(struct tcm_dev *tcm_dev,
		struct tcm_reflash_data_blob *reflash_data,
		struct tcm_buffer *rd_data, unsigned int rd_delay_us)
{
	int retval;
	unsigned int temp;
	unsigned int addr = 0;
	unsigned int length = 0;
	struct tcm_boot_info *boot_info;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!reflash_data) {
		LOGE("Invalid reflash data blob\n");
		return _EINVAL;
	}

	if (!rd_data) {
		LOGE("Invalid read data buffer\n");
		return _EINVAL;
	}

	boot_info = &tcm_dev->boot_info;

	if (IS_APP_FW_MODE(tcm_dev->dev_mode)) {
		LOGE("CUSTOM_OTP not available in app fw mode %d\n",
			tcm_dev->dev_mode);
		return _EINVAL;
	}

	temp = VALUE(boot_info->custom_otp_start_block);
	addr = temp * reflash_data->write_block_size;
	temp = VALUE(boot_info->custom_otp_length_blocks);
	length = temp * reflash_data->write_block_size;

	if (addr == 0 || length == 0) {
		LOGE("CUSTOM_OTP data area unavailable\n");
		retval = _EINVAL;
		goto exit;
	}

	LOGD("CUSTOM_OTP address: 0x%x, length: %d\n", addr, length);

	retval = syna_tcm_buf_alloc(rd_data, length);
	if (retval < 0) {
		LOGE("Fail to allocate memory for rd_data buffer\n");
		goto exit;
	}

	retval = syna_tcm_read_flash(tcm_dev, addr, rd_data->buf,
			length, rd_delay_us);
	if (retval < 0) {
		LOGE("Fail to read CUSTOM_OTP area (addr: 0x%x, length: %d)\n",
			addr, length);
		goto exit;
	}

	rd_data->data_length = length;

exit:
	return retval;
}

/**
 * syna_tcm_read_flash_custom_data()
 *
 * Read the data of custom data in the flash memory.
 *
 * @param
 *    [ in] tcm_dev:       the device handle
 *    [ in] reflash_data:  data blob for reflash
 *    [ in] address:       address generated by get_flash_data_location()
 *    [ in] size:          size generated by get_flash_data_location()
 *    [out] rd_data:       buffer used for storing the retrieved data
 *    [ in] rd_delay_us:   delay time to access data in flash memory
 *
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_read_flash_custom_data(struct tcm_dev *tcm_dev,
		struct tcm_reflash_data_blob *reflash_data,
		unsigned int address, unsigned int size,
		struct tcm_buffer *rd_data, unsigned int rd_delay_us)
{
	int retval;
	unsigned int addr = 0;
	unsigned int length = 0;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!reflash_data) {
		LOGE("Invalid reflash data blob\n");
		return _EINVAL;
	}

	if (!rd_data) {
		LOGE("Invalid read data buffer\n");
		return _EINVAL;
	}

	if (IS_APP_FW_MODE(tcm_dev->dev_mode)) {
		LOGE("Custom data not available in app fw mode %d\n",
			tcm_dev->dev_mode);
		return _EINVAL;
	}

	addr = address * reflash_data->write_block_size;
	length = size * reflash_data->write_block_size;

	if (addr == 0 || length == 0) {
		LOGE("Custom data area unavailable\n");
		retval = _EINVAL;
		goto exit;
	}

	LOGD("Custom data address: 0x%x, length: %d\n", addr, length);

	retval = syna_tcm_buf_alloc(rd_data, length);
	if (retval < 0) {
		LOGE("Fail to allocate memory for rd_data buffer\n");
		goto exit;
	}

	retval = syna_tcm_read_flash(tcm_dev, addr, rd_data->buf,
			length, rd_delay_us);
	if (retval < 0) {
		LOGE("Fail to read custom data (addr: 0x%x, length: %d)\n",
			addr, length);
		goto exit;
	}

	rd_data->data_length = length;

exit:
	return retval;
}
/**
 * syna_tcm_read_flash_area()
 *
 * Entry function to read in the data of specific area in the flash memory.
 *
 * @param
 *    [ in] tcm_dev:      the device handle
 *    [ in] area:         flash area to read
 *    [out] data:         buffer storing the retrieved data
 *    [ in] rd_delay_us:  delay time in micro-sec to read words data from flash.
 *                        set 'DEFAULT_FLASH_READ_DELAY' to use default,
 *                        which is 10 us;
 *                        set '0' or 'RESP_IN_ATTN' to select ATTN-driven.
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
int syna_tcm_read_flash_area(struct tcm_dev *tcm_dev,
		enum flash_area area, struct tcm_buffer *rd_data,
		unsigned int rd_delay_us)
{
	int retval;
	unsigned int addr = 0;
	unsigned int length = 0;
	struct tcm_reflash_data_blob reflash_data;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!rd_data) {
		LOGE("Invalid data buffer\n");
		return _EINVAL;
	}

	switch (area) {
	case AREA_CUSTOM_LCM:
	case AREA_CUSTOM_OEM:
	case AREA_PPDT:
	case AREA_FORCE_TUNING:
	case AREA_OPEN_SHORT_TUNING:
		retval = syna_tcm_get_flash_data_location(tcm_dev,
				area, &addr, &length);
		if (retval < 0) {
			LOGE("Fail to get data location of 0x%x\n", area);
			return retval;
		}
		break;
	default:
		break;
	}

	retval = syna_tcm_set_up_flash_access(tcm_dev,
			&reflash_data);
	if (retval < 0) {
		LOGE("Fail to set up flash access\n");
		return retval;
	}

	syna_tcm_buf_init(&reflash_data.out);

	switch (area) {
	case AREA_BOOT_CONFIG:
		retval = syna_tcm_read_flash_boot_config(tcm_dev,
				&reflash_data, rd_data, rd_delay_us);
		if (retval < 0) {
			LOGE("Fail to get boot config data\n");
			goto exit;
		}
		break;
	case AREA_APP_CONFIG:
		retval = syna_tcm_read_flash_app_config(tcm_dev,
				&reflash_data, rd_data, rd_delay_us);
		if (retval < 0) {
			LOGE("Fail to get app config data\n");
			goto exit;
		}
		break;
	case AREA_DISP_CONFIG:
		retval = syna_tcm_read_flash_disp_config(tcm_dev,
				&reflash_data, rd_data, rd_delay_us);
		if (retval < 0) {
			LOGE("Fail to get disp config data\n");
			goto exit;
		}
		break;
	case AREA_CUSTOM_OTP:
		retval = syna_tcm_read_flash_custom_otp(tcm_dev,
				&reflash_data, rd_data, rd_delay_us);
		if (retval < 0) {
			LOGE("Fail to get custom otp data\n");
			goto exit;
		}
		break;
	case AREA_CUSTOM_LCM:
	case AREA_CUSTOM_OEM:
	case AREA_PPDT:
	case AREA_FORCE_TUNING:
	case AREA_OPEN_SHORT_TUNING:
		retval = syna_tcm_read_flash_custom_data(tcm_dev,
				&reflash_data, addr, length, rd_data,
				rd_delay_us);
		break;
	default:
		LOGE("Invalid data area\n");
		retval = _EINVAL;
		goto exit;
	}

	LOGI("%s read\n", AREA_ID_STR(area));

	retval = 0;

exit:
	retval = syna_tcm_switch_fw_mode(tcm_dev,
			MODE_APPLICATION_FIRMWARE,
			FW_MODE_SWITCH_DELAY_MS);
	if (retval < 0)
		LOGE("Fail to go back to application firmware\n");

	syna_tcm_buf_release(&reflash_data.out);

	return retval;
}

/**
 * syna_tcm_write_flash()
 *
 * Implement the bootloader command to write specified data to flash memory.
 *
 * If the length of the data to write is not an integer multiple of words,
 * the trailing byte will be discarded.  If the length of the data to write
 * is not an integer number of write blocks, it will be zero-padded to the
 * next write block.
 *
 * @param
 *    [ in] tcm_dev:      the device handle
 *    [ in] reflash_data: data blob for reflash
 *    [ in] address:      the address in flash memory to write
 *    [ in] wr_data:      data to write
 *    [ in] wr_len:       length of data to write
 *    [ in] wr_delay_ms:  a short delay after the command executed
 *                         set 'DEFAULT_FLASH_WRITE_DELAY' to use default,
 *                         which is 20 us;
 *                         set '0' or 'RESP_IN_ATTN' to select ATTN-driven.
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_write_flash(struct tcm_dev *tcm_dev,
		struct tcm_reflash_data_blob *reflash_data,
		unsigned int address, const unsigned char *wr_data,
		unsigned int wr_len, unsigned int wr_delay_ms)
{
	int retval;
	unsigned int offset;
	unsigned int w_length;
	unsigned int xfer_length;
	unsigned int remaining_length;
	unsigned int flash_address;
	unsigned int block_address;
	unsigned int delay_ms;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	w_length = tcm_dev->max_wr_size - 8;

	w_length = w_length - (w_length % reflash_data->write_block_size);

	w_length = MIN(w_length, reflash_data->max_write_payload_size);

	offset = 0;

	remaining_length = wr_len;

	syna_tcm_buf_lock(&reflash_data->out);

	while (remaining_length) {
		if (remaining_length > w_length)
			xfer_length = w_length;
		else
			xfer_length = remaining_length;

		retval = syna_tcm_buf_alloc(&reflash_data->out,
				xfer_length + 2);
		if (retval < 0) {
			LOGE("Fail to allocate memory for buf.out\n");
			syna_tcm_buf_unlock(&reflash_data->out);
			return retval;
		}

		flash_address = address + offset;
		block_address = flash_address / reflash_data->write_block_size;
		reflash_data->out.buf[0] = (unsigned char)block_address;
		reflash_data->out.buf[1] = (unsigned char)(block_address >> 8);

		retval = syna_pal_mem_cpy(&reflash_data->out.buf[2],
				reflash_data->out.buf_size - 2,
				&wr_data[offset],
				wr_len - offset,
				xfer_length);
		if (retval < 0) {
			LOGE("Fail to copy write data ,size: %d\n",
				xfer_length);
			syna_tcm_buf_unlock(&reflash_data->out);
			return retval;
		}

		if (wr_delay_ms == DEFAULT_FLASH_WRITE_DELAY)
			delay_ms = FLASH_WRITE_DELAY_MS;
		else
			delay_ms = wr_delay_ms;

		if (delay_ms == RESP_IN_ATTN) {
			LOGD("xfer: %d, delay: ATTN-driven\n", xfer_length);
		} else {
			delay_ms = (delay_ms * xfer_length) / 1000;
			LOGD("xfer: %d, delay: %d\n", xfer_length, delay_ms);
		}

		retval = syna_tcm_reflash_send_command(tcm_dev,
				CMD_WRITE_FLASH,
				reflash_data->out.buf,
				xfer_length + 2,
				delay_ms);
		if (retval < 0) {
			LOGE("Fail to write data to flash addr 0x%x, size %d\n",
				flash_address, xfer_length + 2);
			syna_tcm_buf_unlock(&reflash_data->out);
			return retval;
		}

		offset += xfer_length;
		remaining_length -= xfer_length;
	}

	syna_tcm_buf_unlock(&reflash_data->out);

	return 0;
}

/**
 * syna_tcm_write_flash_block()
 *
 * Write data to the target block data area in the flash memory.
 *
 * @param
 *    [ in] tcm_dev:       the device handle
 *    [ in] reflash_data:  data blob for reflash
 *    [ in] area:          target block area to write
 *    [ in] wr_delay_us:   delay time in micro-sec to write block data to flash.
 *                         set 'DEFAULT_FLASH_WRITE_DELAY' to use default,
 *                         which is 20 ms;
 *                         set '0' or 'RESP_IN_ATTN' to select ATTN-driven.
 *
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_write_flash_block(struct tcm_dev *tcm_dev,
		struct tcm_reflash_data_blob *reflash_data,
		struct block_data *block, unsigned int wr_delay_us)
{
	int retval;
	unsigned int size;
	unsigned int flash_addr;
	const unsigned char *data;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!reflash_data) {
		LOGE("Invalid reflash data blob\n");
		return _EINVAL;
	}

	if (!block) {
		LOGE("Invalid block data\n");
		return _EINVAL;
	}

	data = block->data;
	size = block->size;
	flash_addr = block->flash_addr;

	LOGD("Write data to %s - address: 0x%x, size: %d\n",
		AREA_ID_STR(block->id), flash_addr, size);

	if (size == 0) {
		LOGI("No need to update, size = %d\n", size);
		goto exit;
	}

	retval = syna_tcm_write_flash(tcm_dev, reflash_data,
			flash_addr, data, size, wr_delay_us);
	if (retval < 0) {
		LOGE("Fail to write %s to flash (addr: 0x%x, size: %d)\n",
			AREA_ID_STR(block->id), flash_addr, size);
		return retval;
	}

exit:
	LOGN("%s area written\n", AREA_ID_STR(block->id));

	return 0;
}

/**
 * syna_tcm_erase_flash()
 *
 * Implement the bootloader command, which is used to erase the specified
 * blocks of flash memory.
 *
 * Until this command completes, the device may be unresponsive.
 * Therefore, this helper is implemented as a blocked function, and the delay
 * time is set to 200 ms in default.
 *
 * @param
 *    [ in] tcm_dev:        the device handle
 *    [ in] reflash_data:   data blob for reflash
 *    [ in] address:        the address in flash memory to read
 *    [ in] size:           size of data to write
 *    [ in] erase_delay_ms: the delay time to get the resp from mass erase
 *
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_erase_flash(struct tcm_dev *tcm_dev,
		struct tcm_reflash_data_blob *reflash_data,
		unsigned int address, unsigned int size,
		unsigned int erase_delay_ms)
{
	int retval;
	unsigned int page_start = 0;
	unsigned int page_count = 0;
	unsigned char out_buf[4] = {0};
	int size_erase_cmd;

	page_start = address / reflash_data->page_size;

	page_count = syna_pal_ceil_div(size, reflash_data->page_size);

	LOGD("Page start = %d (0x%04x), Page count = %d (0x%04x)\n",
		page_start, page_start, page_count, page_count);

	if ((page_start > 0xff) || (page_count > 0xff)) {
		size_erase_cmd = 4;

		out_buf[0] = (unsigned char)(page_start & 0xff);
		out_buf[1] = (unsigned char)((page_start >> 8) & 0xff);
		out_buf[2] = (unsigned char)(page_count & 0xff);
		out_buf[3] = (unsigned char)((page_count >> 8) & 0xff);
	} else {
		size_erase_cmd = 2;

		out_buf[0] = (unsigned char)(page_start & 0xff);
		out_buf[1] = (unsigned char)(page_count & 0xff);
	}

	retval = syna_tcm_reflash_send_command(tcm_dev,
			CMD_ERASE_FLASH,
			out_buf,
			size_erase_cmd,
			erase_delay_ms);
	if (retval < 0) {
		LOGE("Fail to erase data at flash page 0x%x, count %d\n",
			page_start, page_count);
		return retval;
	}

	return 0;
}

/**
 * syna_tcm_erase_flash_block()
 *
 * Mass erase the target block data area in the flash memory.
 *
 * @param
 *    [ in] tcm_dev:      the device handle
 *    [ in] reflash_data: data blob for reflash
 *    [ in] block:        target block area to erase
 *    [ in] delay_ms:     a short delay after the erase command executed
 *                        set 'DEFAULT_FLASH_ERASE_DELAY' to use default,
 *                        which is 500 ms;
 *                        set '0' or 'RESP_IN_ATTN' to select ATTN-driven.
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_erase_flash_block(struct tcm_dev *tcm_dev,
		struct tcm_reflash_data_blob *reflash_data,
		struct block_data *block, unsigned int delay_ms)
{
	int retval;
	unsigned int size;
	unsigned int flash_addr;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!reflash_data) {
		LOGE("Invalid reflash data blob\n");
		return _EINVAL;
	}

	if (!block) {
		LOGE("Invalid block data\n");
		return _EINVAL;
	}

	flash_addr = block->flash_addr;

	size = block->size;

	if (delay_ms == DEFAULT_FLASH_ERASE_DELAY)
		delay_ms = FLASH_ERASE_DELAY_MS;

	LOGD("Erase %s block - address: 0x%x, size: %d\n",
		AREA_ID_STR(block->id), flash_addr, size);

	if (size == 0) {
		LOGI("No need to erase, size = %d\n", size);
		goto exit;
	}

	retval = syna_tcm_erase_flash(tcm_dev, reflash_data,
			flash_addr, size, delay_ms);
	if (retval < 0) {
		LOGE("Fail to erase %s data (addr: 0x%x, size: %d)\n",
			AREA_ID_STR(block->id), flash_addr, size);
		return retval;
	}

exit:
	LOGN("%s area erased\n", AREA_ID_STR(block->id));

	return 0;
}

/**
 * syna_tcm_update_flash_block()
 *
 * Perform the reflash sequence to the target area
 *
 * @param
 *    [ in] tcm_dev:       the device handle
 *    [ in] reflash_data:  data blob for reflash
 *    [ in] block:         target block area to update
 *    [ in] delay_ms:      a short delay time in millisecond to wait for
 *                           the completion of flash access
 *                           for polling, set a value formatted with
 *                           [erase | write];
 *                           for ATTN-driven, set a '0' or 'RESP_IN_ATTN'
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_update_flash_block(struct tcm_dev *tcm_dev,
		struct tcm_reflash_data_blob *reflash_data,
		struct block_data *block, unsigned int delay_ms)
{
	int retval;
	unsigned int erase_delay_ms = (delay_ms >> 16) & 0xFFFF;
	unsigned int wr_blk_delay_ms = delay_ms & 0xFFFF;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!reflash_data) {
		LOGE("Invalid reflash data blob\n");
		return _EINVAL;
	}

	if (!block) {
		LOGE("Invalid block data\n");
		return _EINVAL;
	}

	/* reflash is not needed for the partition */
	retval = syna_tcm_check_flash_block(tcm_dev,
			reflash_data,
			block);
	if (retval < 0) {
		LOGE("Invalid %s area\n", AREA_ID_STR(block->id));
		return retval;
	}

	if (retval == DO_NONE)
		return 0;

	LOGN("Prepare to erase %s area\n", AREA_ID_STR(block->id));

	retval = syna_tcm_erase_flash_block(tcm_dev,
			reflash_data,
			block,
			erase_delay_ms);
	if (retval < 0) {
		LOGE("Fail to erase %s area\n", AREA_ID_STR(block->id));
		return retval;
	}

	LOGN("Prepare to update %s area\n", AREA_ID_STR(block->id));

	retval = syna_tcm_write_flash_block(tcm_dev,
			reflash_data,
			block,
			wr_blk_delay_ms);
	if (retval < 0) {
		LOGE("Fail to write %s area\n", AREA_ID_STR(block->id));
		return retval;
	}

	return 0;
}

/**
 * syna_tcm_do_reflash_tddi()
 *
 * Implement the sequence specific for MODE_TDDI_BOOTLOADER.
 *
 * @param
 *    [ in] tcm_dev:         the device handle
 *    [ in] reflash_data:    misc. data used for fw update
 *    [ in] type:            the area to update
 *    [ in] delay_ms:        a short delay time in millisecond to wait for
 *                           the completion of flash access
 *                           for polling, set a value formatted with
 *                           [erase | write];
 *                           for ATTN-driven, set a '0' or 'RESP_IN_ATTN'
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_do_reflash_tddi(struct tcm_dev *tcm_dev,
	struct tcm_reflash_data_blob *reflash_data,
	enum update_area type, unsigned int delay_ms)
{
	int retval = 0;
	int idx;
	unsigned int erase_delay_ms = (delay_ms >> 16) & 0xFFFF;
	unsigned int wr_blk_delay_ms = delay_ms & 0xFFFF;
	struct image_info *image_info;
	struct block_data *block;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!reflash_data) {
		LOGE("Invalid reflash_data blob\n");
		return _EINVAL;
	}

	image_info = &reflash_data->image_info;

	if (tcm_dev->dev_mode != MODE_TDDI_BOOTLOADER) {
		LOGE("Incorrect bootloader mode, 0x%02x, expected: 0x%02x\n",
			tcm_dev->dev_mode, MODE_TDDI_BOOTLOADER);
		return _EINVAL;
	}

	if (type == UPDATE_NONE)
		goto exit;

	/* Always mass erase all blocks, before writing the data */
	for (idx = 0; idx < AREA_MAX; idx++) {

		block = &image_info->data[idx];

		if (!block->available)
			continue;

		retval = syna_tcm_check_flash_block(tcm_dev,
				reflash_data,
				block);
		if (retval == DO_NONE)
			continue;

		LOGN("Prepare to erase %s area\n", AREA_ID_STR(block->id));

		retval = syna_tcm_erase_flash_block(tcm_dev,
				reflash_data,
				block,
				erase_delay_ms);
		if (retval < 0) {
			LOGE("Fail to erase %s area\n", AREA_ID_STR(block->id));
			goto exit;
		}
	}

	/* Write all the data to flash */
	for (idx = 0; idx < AREA_MAX; idx++) {

		block = &image_info->data[idx];

		if (!block->available)
			continue;

		retval = syna_tcm_check_flash_block(tcm_dev,
				reflash_data,
				block);
		if (retval == DO_NONE)
			continue;

		LOGN("Prepare to update %s area\n", AREA_ID_STR(block->id));

		retval = syna_tcm_write_flash_block(tcm_dev,
			reflash_data,
			block,
			wr_blk_delay_ms);
		if (retval < 0) {
			LOGE("Fail to update %s area\n",
				AREA_ID_STR(block->id));
			goto exit;
		}
	}

exit:
	return retval;
}

/**
 * syna_tcm_do_reflash_generic()
 *
 * Implement the generic sequence of fw update in MODE_BOOTLOADER.
 *
 * Typically, it is applied on most of discrete touch controllers
 *
 * @param
 *    [ in] tcm_dev:         the device handle
 *    [ in] reflash_data:    misc. data used for fw update
 *    [ in] type:            the area to update
 *    [ in] wait_delay_ms:   a short delay time in millisecond to wait for
 *                           the completion of flash access
 *                           for polling, set a value formatted with
 *                           [erase | write];
 *                           for ATTN-driven, set a '0' or 'RESP_IN_ATTN'
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
static int syna_tcm_do_reflash_generic(struct tcm_dev *tcm_dev,
		struct tcm_reflash_data_blob *reflash_data,
		enum update_area type, unsigned int wait_delay_ms)
{
	int retval = 0;
	struct block_data *block;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if (!reflash_data) {
		LOGE("Invalid reflash_data blob\n");
		return _EINVAL;
	}

	if (tcm_dev->dev_mode != MODE_BOOTLOADER) {
		LOGE("Incorrect bootloader mode, 0x%02x, expected: 0x%02x\n",
			tcm_dev->dev_mode, MODE_BOOTLOADER);
		return _EINVAL;
	}

	switch (type) {
	case UPDATE_FIRMWARE_CONFIG:
		block = &reflash_data->image_info.data[AREA_APP_CODE];

		retval = syna_tcm_update_flash_block(tcm_dev,
				reflash_data,
				block,
				wait_delay_ms);
		if (retval < 0) {
			LOGE("Fail to update application firmware\n");
			goto exit;
		}
	case UPDATE_CONFIG_ONLY:
		block = &reflash_data->image_info.data[AREA_APP_CONFIG];

		retval = syna_tcm_update_flash_block(tcm_dev,
				reflash_data,
				block,
				wait_delay_ms);
		if (retval < 0) {
			LOGE("Fail to update application config\n");
			goto exit;
		}
		break;
	case UPDATE_NONE:
	default:
		break;
	}
exit:
	return retval;
}

/**
 * syna_tcm_do_fw_update()
 *
 * The entry function to perform fw update upon TouchBoot.
 *
 * @param
 *    [ in] tcm_dev:         the device handle
 *    [ in] image:           binary data to write
 *    [ in] image_size:      size of data array
 *    [ in] wait_delay_ms:   a short delay time in millisecond to wait for
 *                           the completion of flash access
 *                           for polling, set a value formatted with
 *                           [erase | write];
 *                           for ATTN-driven, set a '0' or 'RESP_IN_ATTN'
 *    [ in] force_reflash:   '1' to do reflash anyway
 *                           '0' to compare ID info before doing reflash.
 *
 * @return
 *    on success, 0 or positive value; otherwise, negative value on error.
 */
int syna_tcm_do_fw_update(struct tcm_dev *tcm_dev,
		const unsigned char *image, unsigned int image_size,
		unsigned int wait_delay_ms, bool force_reflash)
{
	int retval;
	enum update_area type = UPDATE_NONE;
	struct tcm_reflash_data_blob reflash_data;
	int app_status;

	if (!tcm_dev) {
		LOGE("Invalid tcm device handle\n");
		return _EINVAL;
	}

	if ((!image) || (image_size == 0)) {
		LOGE("Invalid image data\n");
		return _EINVAL;
	}

	LOGN("Prepare to do reflash\n");

	syna_tcm_buf_init(&reflash_data.out);

	reflash_data.image = image;
	reflash_data.image_size = image_size;
	syna_pal_mem_set(&reflash_data.image_info, 0x00,
		sizeof(struct image_info));

	retval = syna_tcm_parse_fw_image(image, &reflash_data.image_info);
	if (retval < 0) {
		LOGE("Fail to parse firmware image\n");
		return retval;
	}

	LOGN("Start of reflash\n");

	ATOMIC_SET(tcm_dev->firmware_flashing, 1);

	app_status = syna_pal_le2_to_uint(tcm_dev->app_info.status);

	/* to forcedly update the firmware and config
	 *   - flag of 'force_reflash' has been set
	 *   - device stays in bootloader
	 *   - app firmware doesn't run properly
	 */
	force_reflash = force_reflash ||
		(IS_BOOTLOADER_MODE(tcm_dev->dev_mode)) ||
		(IS_APP_FW_MODE(tcm_dev->dev_mode) && (app_status != APP_STATUS_OK));

	if (force_reflash) {
		type = UPDATE_FIRMWARE_CONFIG;
		goto reflash;
	}

	type = (enum update_area)syna_tcm_compare_image_id_info(tcm_dev,
		&reflash_data);

	if (type == UPDATE_NONE)
		goto exit;

reflash:
	syna_tcm_buf_init(&reflash_data.out);

	/* set up flash access, and enter the bootloader mode */
	retval = syna_tcm_set_up_flash_access(tcm_dev, &reflash_data);
	if (retval < 0) {
		LOGE("Fail to set up flash access\n");
		goto exit;
	}

	/* perform the fw update */
	if (tcm_dev->dev_mode == MODE_BOOTLOADER) {
		retval = syna_tcm_do_reflash_generic(tcm_dev,
			&reflash_data,
			type,
			wait_delay_ms);
	} else if (tcm_dev->dev_mode == MODE_TDDI_BOOTLOADER) {
		retval = syna_tcm_do_reflash_tddi(tcm_dev,
			&reflash_data,
			type,
			wait_delay_ms);
	} else {
		LOGE("Incorrect bootloader mode, 0x%02x\n",
			tcm_dev->dev_mode);
		goto reset;
	}

	if (retval < 0) {
		LOGE("Fail to do firmware update\n");
		goto reset;
	}

	LOGN("End of reflash\n");

	retval = 0;
reset:
	retval = syna_tcm_reset(tcm_dev);
	if (retval < 0) {
		LOGE("Fail to do reset\n");
		goto exit;
	}

exit:
	ATOMIC_SET(tcm_dev->firmware_flashing, 0);

	syna_tcm_buf_release(&reflash_data.out);

	return retval;
}