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;
37 /***********flash***********/
38 FLASH_SECTOR_ERASE_CMD,
44 static struct chip_cmd ct360_cmd[] = {
45 {0x0f2a,RW_FLAG}, //fw version
47 {0x0fff,0xe1}, //fw checksum cmd
48 {0x0a0d,RW_FLAG}, //fw checksum val
50 {0x0f2b,0x00}, //chip sleep cmd
52 {0xf000,RW_FLAG}, //chip id cmd
54 /************flash*************/
55 {0x33,RW_FLAG}, //FLASH_SECTOR_ERASE_CMD
56 {8,RW_FLAG}, //FLASH_SECTOR_NUM
57 {2048,RW_FLAG}, //FLASH_SECTOR_SIZE
58 {RW_FLAG,RW_FLAG}, //FLASH_MASS_ERASE_CMD
61 static struct chip_cmd ct36x_cmd[] = {
62 {0x3fff,RW_FLAG}, //fw version
64 {0x8fff,0xe1}, //fw checksum cmd
65 {0x8e0e,RW_FLAG}, //fw checksum val
67 {0x8fff,0xaf}, //chip sleep cmd
69 {0xf000,RW_FLAG}, //chip id cmd
71 /************flash*************/
72 {0x30,RW_FLAG}, //FLASH_SECTOR_ERASE_CMD
73 {256,RW_FLAG}, //FLASH_SECTOR_NUM
74 {128,RW_FLAG}, //FLASH_SECTOR_SIZE
75 {0x33,RW_FLAG}, //FLASH_MASS_ERASE_CMD
79 unsigned int ct36x_cmd[4][2] = {
80 {0x3fff,0x00}, //fw version
82 {0x0fff,0xe1}, //fw checksum cmd
83 {0x0a0d,0x00}, //fw checksum val
85 {0x8fff,0xaf},//chip sleep cmd
88 unsigned int (*chip)[2] = ct36x_cmd;
92 static int chip_i2c_read(struct i2c_client *client, __u16 addr, __u8 *buf, __u16 len)
99 msgs.flags = 0x01; // 0x00: write 0x01:read
102 //#if(PLATFORM == ROCKCHIP)
103 msgs.scl_rate = TS_I2C_SPEED;
106 ret = i2c_transfer(client->adapter, &msgs, 1);
108 printk("___%s:i2c read error___\n",__func__);
114 static int chip_i2c_write(struct i2c_client *client, __u16 addr, __u8 *buf, __u16 len)
121 msgs.flags = 0x00; // 0x00: write 0x01:read
124 //#if(PLATFORM == ROCKCHIP)
125 msgs.scl_rate = TS_I2C_SPEED;
128 ret = i2c_transfer(client->adapter, &msgs, 1);
130 printk("___%s:i2c write error___\n",__func__);
137 static int chip_ram_write_1byte(unsigned short addr,unsigned char data)
139 struct i2c_client *client = ts_object->driver->client;
140 unsigned char buf[3];
146 buf[2] = addr & 0x00ff;
147 //printk("addr = %x,buf[0] = %x,buf[1] = %x,buf[2] = %x,data = %x\n",addr,buf[0],buf[1],buf[2],data);
148 ret = chip_i2c_write(client, client->addr, buf,3);
156 ret = chip_i2c_write(client, client->addr, buf,2);
161 static int chip_ram_read(unsigned short addr,unsigned char *rx_buf,unsigned short len)
163 struct i2c_client *client = ts_object->driver->client;
164 unsigned char buf[3];
170 buf[2] = addr & 0x00ff;
171 //printk("addr = %x,buf[0] = %x,buf[1] = %x,buf[2] = %x\n",addr,buf[0],buf[1],buf[2]);
172 ret = chip_i2c_write(client, client->addr, buf,3);
179 ret = chip_i2c_write(client, client->addr, buf,1);
186 ret = chip_i2c_read(client,client->addr,rx_buf,len);
191 int chip_get_fw_version(unsigned char *buf)
196 ret = chip_ram_read(chip[FW_VERSION].addr,buf,1);
201 int chip_get_chip_id(unsigned char *buf)
206 ret = chip_ram_read(chip[CHIP_ID_CMD].addr,buf,1);
212 int chip_enter_sleep_mode(void)
221 ret = chip_ram_write_1byte(chip[CHIP_SLEEP].addr,chip[CHIP_SLEEP].data);
226 int chip_function(enum cmd_index cmd_index,unsigned char *rx_buf,unsigned char len)
232 if(chip[cmd_index].data != RW_FLAG) //write
234 ret = chip_ram_write_1byte(chip[cmd_index].addr,chip[cmd_index].data);
238 ret = chip_ram_read(chip[cmd_index].addr,rx_buf,len);
244 /***************flash********************/
246 static int chip_flash_init(struct i2c_client *client)
248 unsigned char buf[2];
255 ret = chip_i2c_write(client, FLASH_I2C_ADDR, buf,2);
261 static int chip_read_bus_status(struct i2c_client *client,unsigned char *rx_buf)
263 unsigned char buf[1];
269 ret = chip_i2c_write(client, FLASH_I2C_ADDR, buf,1);
275 ret = chip_i2c_read(client,FLASH_I2C_ADDR,rx_buf,1);
280 static int chip_enter_idle_mode(struct i2c_client *client)
282 unsigned char buf[2];
289 ret = chip_i2c_write(client, FLASH_I2C_ADDR, buf,2);
295 int chip_solfware_reset(struct i2c_client *client)
297 unsigned char buf[2];
304 ret = chip_i2c_write(client, FLASH_I2C_ADDR, buf,2);
310 static int chip_erase_flash(struct i2c_client *client)
312 unsigned char buf[4];
317 if(chip[FLASH_MASS_ERASE_CMD].addr == 0x33)//ct36x mass erase
319 ret = chip_read_bus_status(client,buf);
322 printk("___i2c bus busy,bus_status = %d___\n",buf[0]);
326 buf[1] = chip[FLASH_MASS_ERASE_CMD].addr;
329 ret = chip_i2c_write(client, FLASH_I2C_ADDR, buf,4);
332 printk("vtl chip flash erase fail\n");
335 //printk("mass erase\n");
339 else //ct360/ct36x sector erase
341 for(sec = 0;sec < chip[FLASH_SECTOR_NUM].addr;sec++)
343 ret = chip_read_bus_status(client,buf);
346 printk("___i2c bus busy,bus_status = %x,sec = %d___\n",buf[0],sec);
349 sec_addr = sec * chip[FLASH_SECTOR_SIZE].addr;
351 buf[1] = chip[FLASH_SECTOR_ERASE_CMD].addr;
352 buf[2] = sec_addr >> 8;
353 buf[3] = sec_addr & 0x00ff;
354 ret = chip_i2c_write(client, FLASH_I2C_ADDR, buf,4);
357 printk("vtl chip flash erase fail\n");
363 //printk("sector erase\n");
368 extern unsigned char *gtpfw;
369 static int chip_set_code(unsigned int flash_addr, unsigned char *buf)
372 static unsigned char *binary_data = NULL;
374 if (binary_data == NULL) {
378 buf[2] = (flash_addr >> 8);
379 buf[3] = (flash_addr & 0xFF);
383 if ( (flash_addr == 160) || (flash_addr == 168) )
387 buf[i+6] = ~binary_data[flash_addr + i];
394 buf[i+6] = binary_data[flash_addr + i];
397 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;
401 static int chip_get_bin_checksum(void)
403 unsigned char buf[14];
406 unsigned short bin_checksum = 0;
410 cod = chip[FLASH_SECTOR_NUM].addr * (chip[FLASH_SECTOR_SIZE].addr/CHIP_FLASH_SOURCE_SIZE);
411 for(sec=0;sec<cod;sec++)
413 bin_checksum += chip_set_code(flash_addr,buf);
414 flash_addr += CHIP_FLASH_SOURCE_SIZE;
415 //printk("sec = %d\n",sec);
420 int chip_get_fwchksum(struct i2c_client *client,int *fwchksum)
422 unsigned char buf[2];
429 ret = chip_ram_write_1byte(chip[FW_CHECKSUM_CMD].addr,chip[FW_CHECKSUM_CMD].data);
435 ret = chip_ram_read(chip[FW_CHECKSUM_VAL].addr,buf,2);
436 *fwchksum = (buf[0]<<8)|buf[1];
437 //chip_solfware_reset(client);
442 static int chip_write_flash(struct i2c_client *client)
444 unsigned char buf[14];
446 unsigned char bus_status[1];
448 int sec,cod,sec_8byte_num;
455 buf[1] = CHIP_WRITE_FLASH_CMD;
456 sec_8byte_num = chip[FLASH_SECTOR_SIZE].addr/CHIP_FLASH_SOURCE_SIZE;
457 cod = chip[FLASH_SECTOR_NUM].addr * sec_8byte_num;
461 chip_set_code(flash_addr,buf);
462 flash_addr += CHIP_FLASH_SOURCE_SIZE;
464 ret = chip_read_bus_status(client,bus_status);
465 if(bus_status[0] != 0xaa)
467 printk("i2c bus busy,sec = %d,bus_status = %x\n",sec,bus_status[0]);
471 ret = chip_i2c_write(client,FLASH_I2C_ADDR, buf,14);
477 if(!(sec%sec_8byte_num))
487 int chip_get_checksum(struct i2c_client *client,int *bin_checksum,int *fw_checksum)
494 *bin_checksum = chip_get_bin_checksum();
495 chip_get_fwchksum(client,fw_checksum);
496 //printk("bin_checksum = 0x%x,fw_checksum = 0x%x\n",*bin_checksum,*fw_checksum);
500 int update(struct i2c_client *client)
502 unsigned char buf[20];
511 printk("___chip update start___\n");
512 ret = chip_enter_idle_mode(client);
517 ret = chip_read_bus_status(client,buf);
520 printk("___i2c bus busy,bus_status = %x___\n",buf[0]);
523 ret = chip_erase_flash(client);
526 printk("___erase flash fail___\n");
529 ret = chip_write_flash(client);
532 printk("___write flash fail___\n");
536 printk("___chip update end___\n");
542 int chip_update(struct i2c_client *client)
544 int bin_checksum = 0xff;
554 chip_get_checksum(client,&bin_checksum,&fw_checksum);
555 printk("bin_checksum = 0x%x,fw_checksum = 0x%x\n",bin_checksum,fw_checksum);
557 while((bin_checksum != fw_checksum) && (cnt--))
559 if(update(client) < 0)
564 chip_get_fwchksum(client,&fw_checksum);
565 printk("bin_checksum = %x,fw_checksum = %x,cnt = %d\n",bin_checksum,fw_checksum,cnt);
568 if(bin_checksum != fw_checksum)
576 int chip_update(struct i2c_client *client)
578 unsigned char buf[20];
579 int bin_checksum,fw_checksum,cnt;
588 bin_checksum = chip_get_bin_checksum();
589 chip_get_fwchksum(client,&fw_checksum);
590 printk("bin_checksum = %x,fw_checksum = %x\n",bin_checksum,fw_checksum);
592 while((bin_checksum != fw_checksum) && (cnt--))
595 printk("___chip update start___\n");
596 ret = chip_enter_idle_mode(client);
602 ret = chip_read_bus_status(client,buf);
605 printk("___i2c bus busy,bus_status = %x___\n",buf[0]);
609 ret = chip_erase_flash(client);
612 printk("___erase flash fail___\n");
616 ret = chip_write_flash(client);
619 printk("___write flash fail___\n");
624 //chip_solfware_reset(client);
625 ret = chip_get_fwchksum(client,&fw_checksum);
628 printk("___get fwchksum fail___\n");
632 printk("___chip update end___\n");
633 printk("bin_checksum = %x,fw_checksum = %x\n",bin_checksum,fw_checksum);
636 if(bin_checksum != fw_checksum)
644 int chip_get_chip_id(struct i2c_client *client,unsigned char *rx_buf)
646 unsigned char buf[3];
650 ret = chip_enter_idle_mode(client);
655 ret = chip_read_bus_status(client,buf);
658 printk("___i2c bus status = %x,ret = %d___\n",buf[0],ret);
664 buf[1] = CHIP_ID_ADDR>>8;
665 buf[2] = CHIP_ID_ADDR & 0x00ff;
666 ret = chip_i2c_write(client,0x01, buf,3);
673 ret = chip_i2c_write(client,0x01, buf,1);
679 ret = chip_i2c_read(client,0x01,rx_buf,1);
680 //chip_solfware_reset(client);
683 //printk("___chip ID = %d___\n",*rx_buf);
689 static int chip_read_infoblk(struct i2c_client *client)
691 unsigned char buf[20] = {0};
701 chip_i2c_write(client,0x7F, buf,5);
703 chip_i2c_read(client,0x7f, buf,14);
713 static int chip_erase_infoblk(struct i2c_client *client)
715 unsigned char buf[20]={0};
720 // info block erase command
724 chip_i2c_write(client, 0x7F, buf, 3);
727 ret = chip_read_bus_status(client,buf);
730 printk("___i2c bus status = %x,ret = %d___\n",buf[0],ret);
736 static int chip_write_infoblk(struct i2c_client *client)
739 unsigned char buf[20]={0};
741 unsigned int flash_addr;
747 // write info block 0
751 for ( cod = 0; cod < 16; cod++ ) {
754 buf[2] = (char)(flash_addr >> 8);
755 buf[3] = (char)(flash_addr & 0xFF);
757 if ( flash_addr == 0x0000 )
770 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;
772 chip_i2c_write(client, 0x7F, buf, 14);
781 static int chip_trim_info_init(struct i2c_client *client)
785 while(chip_read_infoblk(client) && (retry--))
787 chip_erase_infoblk(client);
788 chip_write_infoblk(client);
796 struct i2c_client *client;
797 unsigned char chip_id = 0xff;
803 ts_object = vtl_ts_get_object();
805 if(ts_object == NULL)
809 client = ts_object->driver->client;
812 for(retry = 0;retry<3;retry++)
814 ret = chip_get_chip_id(client,&chip_id);
815 printk("___chip ID = %d___cnt = %d\n",chip_id,retry);
818 case 1: { //chip: CT362, CT363, CT365, CT368, CT369
820 chip_trim_info_init(client);
823 case 2: { //chip: CT360
827 case 6: { //chip: CT362M, CT363M, CT365M, CT368M, CT369M
847 #if(CHIP_UPDATE_ENABLE)
848 if(chip_update(client)<0)
850 printk("___chip updata faile___\n");