1 /*
2  *
3  * FocalTech fts TouchScreen driver.
4  *
5  * Copyright (c) 2012-2019, Focaltech Ltd. All rights reserved.
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17 
18 /*****************************************************************************
19 *
20 * File Name: focaltech_upgrade_ft5422.c
21 *
22 * Author: Focaltech Driver Team
23 *
24 * Created: 2016-08-15
25 *
26 * Abstract:
27 *
28 * Reference:
29 *
30 *****************************************************************************/
31 /*****************************************************************************
32 * 1.Included header files
33 *****************************************************************************/
34 #include "../focaltech_flash.h"
35 
36 /*****************************************************************************
37 * Static function prototypes
38 *****************************************************************************/
fts_ft5422_init(u8 * buf,u32 len)39 static int fts_ft5422_init(u8 *buf, u32 len)
40 {
41     if ((!buf) || (len < FTS_MIN_LEN)) {
42         FTS_ERROR("upg is null/fw length fail");
43         return -EINVAL;
44     }
45 
46     upgrade_func_ft5422.fwveroff = len - 2;
47     return 0;
48 }
49 
50 /************************************************************************
51 * Name: fts_ft5422_upgrade
52 * Brief:
53 * Input:
54 * Output:
55 * Return: return 0 if success, otherwise return error code
56 ***********************************************************************/
fts_ft5422_upgrade(u8 * buf,u32 len)57 static int fts_ft5422_upgrade(u8 *buf, u32 len)
58 {
59     int ret = 0;
60     u32 start_addr = 0;
61     u8 cmd[4] = { 0 };
62     int ecc_in_host = 0;
63     int ecc_in_tp = 0;
64 
65     if (NULL == buf) {
66         FTS_ERROR("fw buf is null");
67         return -EINVAL;
68     }
69 
70     if ((len < FTS_MIN_LEN) || (len > (60 * 1024))) {
71         FTS_ERROR("fw buffer len(%x) fail", len);
72         return -EINVAL;
73     }
74 
75     /* enter into upgrade environment */
76     ret = fts_fwupg_enter_into_boot();
77     if (ret < 0) {
78         FTS_ERROR("enter into pramboot/bootloader fail,ret=%d", ret);
79         goto fw_reset;
80     }
81 
82     cmd[0] = FTS_CMD_FLASH_MODE;
83     cmd[1] = FLASH_MODE_UPGRADE_VALUE;
84     ret = fts_write(cmd, 2);
85     if (ret < 0) {
86         FTS_ERROR("upgrade mode(09) cmd write fail");
87         goto fw_reset;
88     }
89 
90     cmd[0] = FTS_CMD_DATA_LEN;
91     cmd[1] = BYTE_OFF_16(len);
92     cmd[2] = BYTE_OFF_8(len);
93     cmd[3] = BYTE_OFF_0(len);
94     ret = fts_write(cmd, FTS_CMD_DATA_LEN_LEN);
95     if (ret < 0) {
96         FTS_ERROR("data len cmd write fail");
97         goto fw_reset;
98     }
99 
100     ret = fts_fwupg_erase(FTS_REASE_APP_DELAY);
101     if (ret < 0) {
102         FTS_ERROR("erase cmd write fail");
103         goto fw_reset;
104     }
105 
106     /* write app */
107     start_addr = upgrade_func_ft5422.appoff;
108     ecc_in_host = fts_flash_write_buf(start_addr, buf, len, 1);
109     if (ecc_in_host < 0 ) {
110         FTS_ERROR("lcd initial code write fail");
111         goto fw_reset;
112     }
113 
114     /* ecc */
115     ecc_in_tp = fts_fwupg_ecc_cal(start_addr, len);
116     if (ecc_in_tp < 0 ) {
117         FTS_ERROR("ecc read fail");
118         goto fw_reset;
119     }
120 
121     FTS_INFO("ecc in tp:%x, host:%x", ecc_in_tp, ecc_in_host);
122     if (ecc_in_tp != ecc_in_host) {
123         FTS_ERROR("ecc check fail");
124         goto fw_reset;
125     }
126 
127     FTS_INFO("upgrade success, reset to normal boot");
128     ret = fts_fwupg_reset_in_boot();
129     if (ret < 0) {
130         FTS_ERROR("reset to normal boot fail");
131     }
132 
133     msleep(200);
134     return 0;
135 
136 fw_reset:
137     FTS_INFO("upgrade fail, reset to normal boot");
138     ret = fts_fwupg_reset_in_boot();
139     if (ret < 0) {
140         FTS_ERROR("reset to normal boot fail");
141     }
142     return -EIO;
143 }
144 
145 struct upgrade_func upgrade_func_ft5422 = {
146     .ctype = {0x02},
147     .fwveroff = 0x0000,
148     .fwcfgoff = 0xD780,
149     .appoff = 0x0000,
150     .pramboot_supported = false,
151     .hid_supported = true,
152     .init = fts_ft5422_init,
153     .upgrade = fts_ft5422_upgrade,
154 };
155