2 #include <linux/types.h>
4 #include <linux/delay.h>
9 #define FLASH_I2C_ADDR 0X7F
10 #define CHIP_ID_ADDR 0xf000
13 #define CHIP_WRITE_FLASH_CMD 0x55
14 #define CHIP_FLASH_SOURCE_SIZE 8
16 #define TB1_USE_F402 0
24 static struct ts_info * ts_object = NULL;
25 static struct chip_cmd (*chip) = NULL;
28 static unsigned char binary_data[] = {
30 #include "f307&f317&f316_CT363S_01_V10_F7E9_140212.dat"
32 #include "lx--js77_97_CT365_V01_E7DA_130419.dat"
48 /***********flash***********/
49 FLASH_SECTOR_ERASE_CMD,
55 static struct chip_cmd ct360_cmd[] = {
56 {0x0f2a,RW_FLAG}, //fw version
58 {0x0fff,0xe1}, //fw checksum cmd
59 {0x0a0d,RW_FLAG}, //fw checksum val
61 {0x0f2b,0x00}, //chip sleep cmd
63 {0xf000,RW_FLAG}, //chip id cmd
65 /************flash*************/
66 {0x33,RW_FLAG}, //FLASH_SECTOR_ERASE_CMD
67 {8,RW_FLAG}, //FLASH_SECTOR_NUM
68 {2048,RW_FLAG}, //FLASH_SECTOR_SIZE
69 {RW_FLAG,RW_FLAG}, //FLASH_MASS_ERASE_CMD
72 static struct chip_cmd ct36x_cmd[] = {
73 {0x3fff,RW_FLAG}, //fw version
75 {0x8fff,0xe1}, //fw checksum cmd
76 {0x8e0e,RW_FLAG}, //fw checksum val
78 {0x8fff,0xaf}, //chip sleep cmd
80 {0xf000,RW_FLAG}, //chip id cmd
82 /************flash*************/
83 {0x30,RW_FLAG}, //FLASH_SECTOR_ERASE_CMD
84 {256,RW_FLAG}, //FLASH_SECTOR_NUM
85 {128,RW_FLAG}, //FLASH_SECTOR_SIZE
86 {0x33,RW_FLAG}, //FLASH_MASS_ERASE_CMD
90 unsigned int ct36x_cmd[4][2] = {
91 {0x3fff,0x00}, //fw version
93 {0x0fff,0xe1}, //fw checksum cmd
94 {0x0a0d,0x00}, //fw checksum val
96 {0x8fff,0xaf},//chip sleep cmd
99 unsigned int (*chip)[2] = ct36x_cmd;
103 static int chip_i2c_read(struct i2c_client *client, __u16 addr, __u8 *buf, __u16 len)
110 msgs.flags = 0x01; // 0x00: write 0x01:read
113 //#if(PLATFORM == ROCKCHIP)
114 msgs.scl_rate = TS_I2C_SPEED;
117 ret = i2c_transfer(client->adapter, &msgs, 1);
119 printk("___%s:i2c read error___\n",__func__);
125 static int chip_i2c_write(struct i2c_client *client, __u16 addr, __u8 *buf, __u16 len)
132 msgs.flags = 0x00; // 0x00: write 0x01:read
135 //#if(PLATFORM == ROCKCHIP)
136 msgs.scl_rate = TS_I2C_SPEED;
139 ret = i2c_transfer(client->adapter, &msgs, 1);
141 printk("___%s:i2c write error___\n",__func__);
148 static int chip_ram_write_1byte(unsigned short addr,unsigned char data)
150 struct i2c_client *client = ts_object->driver->client;
151 unsigned char buf[3];
157 buf[2] = addr & 0x00ff;
158 //printk("addr = %x,buf[0] = %x,buf[1] = %x,buf[2] = %x,data = %x\n",addr,buf[0],buf[1],buf[2],data);
159 ret = chip_i2c_write(client, client->addr, buf,3);
167 ret = chip_i2c_write(client, client->addr, buf,2);
172 static int chip_ram_read(unsigned short addr,unsigned char *rx_buf,unsigned short len)
174 struct i2c_client *client = ts_object->driver->client;
175 unsigned char buf[3];
181 buf[2] = addr & 0x00ff;
182 //printk("addr = %x,buf[0] = %x,buf[1] = %x,buf[2] = %x\n",addr,buf[0],buf[1],buf[2]);
183 ret = chip_i2c_write(client, client->addr, buf,3);
190 ret = chip_i2c_write(client, client->addr, buf,1);
197 ret = chip_i2c_read(client,client->addr,rx_buf,len);
202 int chip_get_fw_version(unsigned char *buf)
207 ret = chip_ram_read(chip[FW_VERSION].addr,buf,1);
212 int chip_get_chip_id(unsigned char *buf)
217 ret = chip_ram_read(chip[CHIP_ID_CMD].addr,buf,1);
223 int chip_enter_sleep_mode(void)
232 ret = chip_ram_write_1byte(chip[CHIP_SLEEP].addr,chip[CHIP_SLEEP].data);
237 int chip_function(enum cmd_index cmd_index,unsigned char *rx_buf,unsigned char len)
243 if(chip[cmd_index].data != RW_FLAG) //write
245 ret = chip_ram_write_1byte(chip[cmd_index].addr,chip[cmd_index].data);
249 ret = chip_ram_read(chip[cmd_index].addr,rx_buf,len);
255 /***************flash********************/
257 static int chip_flash_init(struct i2c_client *client)
259 unsigned char buf[2];
266 ret = chip_i2c_write(client, FLASH_I2C_ADDR, buf,2);
271 static int chip_read_bus_status(struct i2c_client *client,unsigned char *rx_buf)
273 unsigned char buf[1];
279 ret = chip_i2c_write(client, FLASH_I2C_ADDR, buf,1);
285 ret = chip_i2c_read(client,FLASH_I2C_ADDR,rx_buf,1);
290 static int chip_enter_idle_mode(struct i2c_client *client)
292 unsigned char buf[2];
299 ret = chip_i2c_write(client, FLASH_I2C_ADDR, buf,2);
305 int chip_solfware_reset(struct i2c_client *client)
307 unsigned char buf[2];
314 ret = chip_i2c_write(client, FLASH_I2C_ADDR, buf,2);
320 static int chip_erase_flash(struct i2c_client *client)
322 unsigned char buf[4];
327 if(chip[FLASH_MASS_ERASE_CMD].addr == 0x33)//ct36x mass erase
329 ret = chip_read_bus_status(client,buf);
332 printk("___i2c bus busy,bus_status = %d___\n",buf[0]);
336 buf[1] = chip[FLASH_MASS_ERASE_CMD].addr;
339 ret = chip_i2c_write(client, FLASH_I2C_ADDR, buf,4);
342 printk("vtl chip flash erase fail\n");
345 //printk("mass erase\n");
349 else //ct360/ct36x sector erase
351 for(sec = 0;sec < chip[FLASH_SECTOR_NUM].addr;sec++)
353 ret = chip_read_bus_status(client,buf);
356 printk("___i2c bus busy,bus_status = %x,sec = %d___\n",buf[0],sec);
359 sec_addr = sec * chip[FLASH_SECTOR_SIZE].addr;
361 buf[1] = chip[FLASH_SECTOR_ERASE_CMD].addr;
362 buf[2] = sec_addr >> 8;
363 buf[3] = sec_addr & 0x00ff;
364 ret = chip_i2c_write(client, FLASH_I2C_ADDR, buf,4);
367 printk("vtl chip flash erase fail\n");
373 //printk("sector erase\n");
378 static int chip_set_code(unsigned int flash_addr, unsigned char *buf)
382 buf[2] = (flash_addr >> 8);
383 buf[3] = (flash_addr & 0xFF);
387 if ( (flash_addr == 160) || (flash_addr == 168) )
391 buf[i+6] = ~binary_data[flash_addr + i];
398 buf[i+6] = binary_data[flash_addr + i];
401 buf[5] = ~(buf[2]+buf[3]+buf[4]+buf[6]+buf[7]+buf[8]+buf[9]+buf[10]+buf[11]+buf[12]+buf[13]) + 1;
405 static int chip_get_bin_checksum(void)
407 unsigned char buf[14];
410 unsigned short bin_checksum = 0;
414 cod = chip[FLASH_SECTOR_NUM].addr * (chip[FLASH_SECTOR_SIZE].addr/CHIP_FLASH_SOURCE_SIZE);
415 for(sec=0;sec<cod;sec++)
417 bin_checksum += chip_set_code(flash_addr,buf);
418 flash_addr += CHIP_FLASH_SOURCE_SIZE;
419 //printk("sec = %d\n",sec);
424 int chip_get_fwchksum(struct i2c_client *client,int *fwchksum)
426 unsigned char buf[2];
433 ret = chip_ram_write_1byte(chip[FW_CHECKSUM_CMD].addr,chip[FW_CHECKSUM_CMD].data);
439 ret = chip_ram_read(chip[FW_CHECKSUM_VAL].addr,buf,2);
440 *fwchksum = (buf[0]<<8)|buf[1];
441 //chip_solfware_reset(client);
446 static int chip_write_flash(struct i2c_client *client)
448 unsigned char buf[14];
449 unsigned char bus_status[1];
450 int sec,cod,sec_8byte_num;
457 buf[1] = CHIP_WRITE_FLASH_CMD;
458 sec_8byte_num = chip[FLASH_SECTOR_SIZE].addr/CHIP_FLASH_SOURCE_SIZE;
459 cod = chip[FLASH_SECTOR_NUM].addr * sec_8byte_num;
463 chip_set_code(flash_addr,buf);
464 flash_addr += CHIP_FLASH_SOURCE_SIZE;
466 ret = chip_read_bus_status(client,bus_status);
467 if(bus_status[0] != 0xaa)
469 printk("i2c bus busy,sec = %d,bus_status = %x\n",sec,bus_status[0]);
473 ret = chip_i2c_write(client,FLASH_I2C_ADDR, buf,14);
479 if(!(sec%sec_8byte_num))
489 int chip_get_checksum(struct i2c_client *client,int *bin_checksum,int *fw_checksum)
496 *bin_checksum = chip_get_bin_checksum();
497 chip_get_fwchksum(client,fw_checksum);
498 //printk("bin_checksum = 0x%x,fw_checksum = 0x%x\n",*bin_checksum,*fw_checksum);
502 int update(struct i2c_client *client)
504 unsigned char buf[20];
513 printk("___chip update start___\n");
514 ret = chip_enter_idle_mode(client);
519 ret = chip_read_bus_status(client,buf);
522 printk("___i2c bus busy,bus_status = %x___\n",buf[0]);
525 ret = chip_erase_flash(client);
528 printk("___erase flash fail___\n");
531 ret = chip_write_flash(client);
534 printk("___write flash fail___\n");
538 printk("___chip update end___\n");
544 int chip_update(struct i2c_client *client)
546 int bin_checksum = 0xff;
556 chip_get_checksum(client,&bin_checksum,&fw_checksum);
557 printk("bin_checksum = 0x%x,fw_checksum = 0x%x\n",bin_checksum,fw_checksum);
559 while((bin_checksum != fw_checksum) && (cnt--))
561 if(update(client) < 0)
566 chip_get_fwchksum(client,&fw_checksum);
567 printk("bin_checksum = %x,fw_checksum = %x,cnt = %d\n",bin_checksum,fw_checksum,cnt);
570 if(bin_checksum != fw_checksum)
578 int chip_update(struct i2c_client *client)
580 unsigned char buf[20];
581 int bin_checksum,fw_checksum,cnt;
590 bin_checksum = chip_get_bin_checksum();
591 chip_get_fwchksum(client,&fw_checksum);
592 printk("bin_checksum = %x,fw_checksum = %x\n",bin_checksum,fw_checksum);
594 while((bin_checksum != fw_checksum) && (cnt--))
597 printk("___chip update start___\n");
598 ret = chip_enter_idle_mode(client);
604 ret = chip_read_bus_status(client,buf);
607 printk("___i2c bus busy,bus_status = %x___\n",buf[0]);
611 ret = chip_erase_flash(client);
614 printk("___erase flash fail___\n");
618 ret = chip_write_flash(client);
621 printk("___write flash fail___\n");
626 //chip_solfware_reset(client);
627 ret = chip_get_fwchksum(client,&fw_checksum);
630 printk("___get fwchksum fail___\n");
634 printk("___chip update end___\n");
635 printk("bin_checksum = %x,fw_checksum = %x\n",bin_checksum,fw_checksum);
638 if(bin_checksum != fw_checksum)
646 int chip_get_chip_id(struct i2c_client *client,unsigned char *rx_buf)
648 unsigned char buf[3];
652 ret = chip_enter_idle_mode(client);
657 ret = chip_read_bus_status(client,buf);
660 printk("___i2c bus status = %x,ret = %d___\n",buf[0],ret);
666 buf[1] = CHIP_ID_ADDR>>8;
667 buf[2] = CHIP_ID_ADDR & 0x00ff;
668 ret = chip_i2c_write(client,0x01, buf,3);
675 ret = chip_i2c_write(client,0x01, buf,1);
681 ret = chip_i2c_read(client,0x01,rx_buf,1);
682 //chip_solfware_reset(client);
685 //printk("___chip ID = %d___\n",*rx_buf);
691 static int chip_read_infoblk(struct i2c_client *client)
693 unsigned char buf[20] = {0};
703 chip_i2c_write(client,0x7F, buf,5);
705 chip_i2c_read(client,0x7f, buf,14);
715 static int chip_erase_infoblk(struct i2c_client *client)
717 unsigned char buf[20]={0};
722 // info block erase command
726 chip_i2c_write(client, 0x7F, buf, 3);
729 ret = chip_read_bus_status(client,buf);
732 printk("___i2c bus status = %x,ret = %d___\n",buf[0],ret);
738 static int chip_write_infoblk(struct i2c_client *client)
741 unsigned char buf[20]={0};
743 unsigned int flash_addr;
749 // write info block 0
753 for ( cod = 0; cod < 16; cod++ ) {
756 buf[2] = (char)(flash_addr >> 8);
757 buf[3] = (char)(flash_addr & 0xFF);
759 if ( flash_addr == 0x0000 )
772 buf[5] = (~(buf[2]+buf[3]+buf[4]+buf[6]+buf[7]+buf[8]+buf[9]+buf[10]+buf[11]+buf[12]+buf[13]))+1;
774 chip_i2c_write(client, 0x7F, buf, 14);
783 static int chip_trim_info_init(struct i2c_client *client)
787 while(chip_read_infoblk(client) && (retry--))
789 chip_erase_infoblk(client);
790 chip_write_infoblk(client);
798 struct i2c_client *client;
799 unsigned char chip_id = 0xff;
805 ts_object = vtl_ts_get_object();
807 if(ts_object == NULL)
811 client = ts_object->driver->client;
814 for(retry = 0;retry<3;retry++)
816 ret = chip_get_chip_id(client,&chip_id);
817 printk("___chip ID = %d___cnt = %d\n",chip_id,retry);
820 case 1: { //chip: CT362, CT363, CT365, CT368, CT369
822 chip_trim_info_init(client);
825 case 2: { //chip: CT360
829 case 6: { //chip: CT362M, CT363M, CT365M, CT368M, CT369M
849 #if(CHIP_UPDATE_ENABLE)
850 if(chip_update(client)<0)
852 printk("___chip updata faile___\n");