focaltech_upgrade_ft3518.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. * File Name: focaltech_upgrade_ft5452.c
  20. *
  21. * Author: Focaltech Driver Team
  22. *
  23. * Created: 2016-08-15
  24. *
  25. * Abstract:
  26. *
  27. * Reference:
  28. *
  29. *****************************************************************************/
  30. /*****************************************************************************
  31. * 1.Included header files
  32. *****************************************************************************/
  33. #include "../focaltech_flash.h"
  34. /************************************************************************
  35. * Name: fts_ft5452_upgrade
  36. * Brief:
  37. * Input:
  38. * Output:
  39. * Return: return 0 if success, otherwise return error code
  40. ***********************************************************************/
  41. static int fts_ft5452_upgrade(u8 *buf, u32 len)
  42. {
  43. int ret = 0;
  44. u32 start_addr = 0;
  45. u8 cmd[4] = { 0 };
  46. int ecc_in_host = 0;
  47. int ecc_in_tp = 0;
  48. int i = 0;
  49. u8 wbuf[7] = { 0 };
  50. u8 reg_val[4] = {0};
  51. if (NULL == buf) {
  52. FTS_ERROR("fw buf is null");
  53. return -EINVAL;
  54. }
  55. if ((len < FTS_MIN_LEN) || (len > (60 * 1024))) {
  56. FTS_ERROR("fw buffer len(%x) fail", len);
  57. return -EINVAL;
  58. }
  59. /* enter into upgrade environment */
  60. ret = fts_fwupg_enter_into_boot();
  61. if (ret < 0) {
  62. FTS_ERROR("enter into pramboot/bootloader fail,ret=%d", ret);
  63. goto fw_reset;
  64. }
  65. cmd[0] = FTS_CMD_FLASH_MODE;
  66. cmd[1] = FLASH_MODE_UPGRADE_VALUE;
  67. ret = fts_write(cmd, 2);
  68. if (ret < 0) {
  69. FTS_ERROR("upgrade mode(09) cmd write fail");
  70. goto fw_reset;
  71. }
  72. cmd[0] = FTS_CMD_DATA_LEN;
  73. cmd[1] = BYTE_OFF_16(len);
  74. cmd[2] = BYTE_OFF_8(len);
  75. cmd[3] = BYTE_OFF_0(len);
  76. ret = fts_write(cmd, FTS_CMD_DATA_LEN_LEN);
  77. if (ret < 0) {
  78. FTS_ERROR("data len cmd write fail");
  79. goto fw_reset;
  80. }
  81. ret = fts_fwupg_erase(FTS_REASE_APP_DELAY);
  82. if (ret < 0) {
  83. FTS_ERROR("erase cmd write fail");
  84. goto fw_reset;
  85. }
  86. /* write app */
  87. start_addr = upgrade_func_ft5452.appoff;
  88. ecc_in_host = fts_flash_write_buf(start_addr, buf, len, 1);
  89. if (ecc_in_host < 0 ) {
  90. FTS_ERROR("lcd initial code write fail");
  91. goto fw_reset;
  92. }
  93. FTS_INFO( "**********read out checksum**********");
  94. /* check sum init */
  95. wbuf[0] = FTS_CMD_ECC_INIT;
  96. ret = fts_write(wbuf, 1);
  97. if (ret < 0) {
  98. FTS_ERROR("ecc init cmd write fail");
  99. return ret;
  100. }
  101. /* send commond to start checksum */
  102. wbuf[0] = FTS_CMD_ECC_CAL;
  103. wbuf[1] = BYTE_OFF_16(start_addr);
  104. wbuf[2] = BYTE_OFF_8(start_addr);
  105. wbuf[3] = BYTE_OFF_0(start_addr);
  106. wbuf[4] = BYTE_OFF_16(len);
  107. wbuf[5] = BYTE_OFF_8(len);
  108. wbuf[6] = BYTE_OFF_0(len);
  109. FTS_DEBUG("ecc calc startaddr:0x%04x, len:%d", start_addr, len);
  110. ret = fts_write(wbuf, 7);
  111. if (ret < 0) {
  112. FTS_ERROR("ecc calc cmd write fail");
  113. return ret;
  114. }
  115. msleep(len / 256);
  116. /* read status if check sum is finished */
  117. for (i = 0; i < FTS_RETRIES_ECC_CAL; i++) {
  118. wbuf[0] = FTS_CMD_FLASH_STATUS;
  119. reg_val[0] = reg_val[1] = 0x00;
  120. fts_read(wbuf, 1, reg_val, 2);
  121. FTS_DEBUG("[UPGRADE]: reg_val[0]=%02x reg_val[0]=%02x!!", reg_val[0], reg_val[1]);
  122. if ((0xF0 == reg_val[0]) && (0x55 == reg_val[1])) {
  123. break;
  124. }
  125. msleep(FTS_RETRIES_DELAY_ECC_CAL);
  126. }
  127. /* read out check sum */
  128. wbuf[0] = FTS_CMD_ECC_READ;
  129. ret = fts_read(wbuf, 1, reg_val, 1);
  130. if (ret < 0) {
  131. FTS_ERROR( "ecc read cmd write fail");
  132. return ret;
  133. }
  134. ecc_in_tp = reg_val[0];
  135. FTS_INFO("ecc in tp:%x, host:%x", ecc_in_tp, ecc_in_host);
  136. if (ecc_in_tp != ecc_in_host) {
  137. FTS_ERROR("ecc check fail");
  138. goto fw_reset;
  139. }
  140. FTS_INFO("upgrade success, reset to normal boot");
  141. ret = fts_fwupg_reset_in_boot();
  142. if (ret < 0) {
  143. FTS_ERROR("reset to normal boot fail");
  144. }
  145. msleep(200);
  146. return 0;
  147. fw_reset:
  148. FTS_INFO("upgrade fail, reset to normal boot");
  149. ret = fts_fwupg_reset_in_boot();
  150. if (ret < 0) {
  151. FTS_ERROR("reset to normal boot fail");
  152. }
  153. return -EIO;
  154. }
  155. struct upgrade_func upgrade_func_ft5452 = {
  156. .ctype = {0x81},
  157. .fwveroff = 0x010E,
  158. .fwcfgoff = 0x1FFB0,
  159. .appoff = 0x0000,
  160. .pramboot_supported = false,
  161. .hid_supported = true,
  162. .upgrade = fts_ft5452_upgrade,
  163. };
  164. #define FTS_DELAY_ERASE_PAGE_2K 80
  165. #define FTS_SIZE_PAGE_2K 2048
  166. /************************************************************************
  167. * Name: fts_ft5652_upgrade
  168. * Brief:
  169. * Input:
  170. * Output:
  171. * Return: return 0 if success, otherwise return error code
  172. **********************************************************************/
  173. static int fts_ft5652_upgrade(u8 *buf, u32 len)
  174. {
  175. int ret = 0;
  176. u32 start_addr = 0;
  177. u8 cmd[4] = { 0 };
  178. u32 delay = 0;
  179. int ecc_in_host = 0;
  180. int ecc_in_tp = 0;
  181. if ((buf == NULL) || (len < FTS_MIN_LEN)) {
  182. FTS_ERROR("buffer/len(%x) is invalid", len);
  183. return -EINVAL;
  184. }
  185. /* enter into upgrade environment */
  186. ret = fts_fwupg_enter_into_boot();
  187. if (ret < 0) {
  188. FTS_ERROR("enter into pramboot/bootloader fail,ret=%d", ret);
  189. goto fw_reset;
  190. }
  191. cmd[0] = FTS_CMD_APP_DATA_LEN_INCELL;
  192. cmd[1] = BYTE_OFF_16(len);
  193. cmd[2] = BYTE_OFF_8(len);
  194. cmd[3] = BYTE_OFF_0(len);
  195. ret = fts_write(cmd, FTS_CMD_DATA_LEN_LEN);
  196. if (ret < 0) {
  197. FTS_ERROR("data len cmd write fail");
  198. goto fw_reset;
  199. }
  200. cmd[0] = FTS_CMD_FLASH_MODE;
  201. cmd[1] = FLASH_MODE_UPGRADE_VALUE;
  202. ret = fts_write(cmd, 2);
  203. if (ret < 0) {
  204. FTS_ERROR("upgrade mode(09) cmd write fail");
  205. goto fw_reset;
  206. }
  207. delay = FTS_DELAY_ERASE_PAGE_2K * (len / FTS_SIZE_PAGE_2K);
  208. ret = fts_fwupg_erase(delay);
  209. if (ret < 0) {
  210. FTS_ERROR("erase cmd write fail");
  211. goto fw_reset;
  212. }
  213. /* write app */
  214. start_addr = upgrade_func_ft5652.appoff;
  215. ecc_in_host = fts_flash_write_buf(start_addr, buf, len, 1);
  216. if (ecc_in_host < 0) {
  217. FTS_ERROR("flash write fail");
  218. goto fw_reset;
  219. }
  220. /* ecc */
  221. ecc_in_tp = fts_fwupg_ecc_cal(start_addr, len);
  222. if (ecc_in_tp < 0) {
  223. FTS_ERROR("ecc read fail");
  224. goto fw_reset;
  225. }
  226. FTS_INFO("ecc in tp:%x, host:%x", ecc_in_tp, ecc_in_host);
  227. if (ecc_in_tp != ecc_in_host) {
  228. FTS_ERROR("ecc check fail");
  229. goto fw_reset;
  230. }
  231. FTS_INFO("upgrade success, reset to normal boot");
  232. ret = fts_fwupg_reset_in_boot();
  233. if (ret < 0)
  234. FTS_ERROR("reset to normal boot fail");
  235. msleep(200);
  236. return 0;
  237. fw_reset:
  238. FTS_INFO("upgrade fail, reset to normal boot");
  239. ret = fts_fwupg_reset_in_boot();
  240. if (ret < 0)
  241. FTS_ERROR("reset to normal boot fail");
  242. return -EIO;
  243. }
  244. struct upgrade_func upgrade_func_ft5652 = {
  245. .ctype = {0x88},
  246. .fwveroff = 0x010E,
  247. .fwcfgoff = 0x1F80,
  248. .appoff = 0x0000,
  249. .upgspec_version = UPGRADE_SPEC_V_1_0,
  250. .pramboot_supported = false,
  251. .hid_supported = true,
  252. .upgrade = fts_ft5652_upgrade,
  253. };