mspro_protocol.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. #include "ms_port.h"
  2. #include "ms_misc.h"
  3. #include "ms_mspro.h"
  4. #include "mspro_protocol.h"
  5. unsigned char read_sector_type;
  6. extern unsigned short mass_counter;
  7. extern unsigned mspro_access_status_reg_after_read;
  8. int mspro_media_type_identification(MS_MSPRO_Card_Info_t *ms_mspro_info)
  9. {
  10. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  11. if (ms_mspro_buf->mspro.regs.Type_Reg != 0x01)
  12. return MS_MSPRO_ERROR_MEDIA_TYPE;
  13. if (ms_mspro_buf->mspro.regs.Category_Reg != 0x00)
  14. return MS_MSPRO_ERROR_MEDIA_TYPE;
  15. if (ms_mspro_buf->mspro.regs.Class_Reg == 0x00)
  16. {
  17. ms_mspro_info->write_protected_flag = ((MSPRO_Status_Register_t*)&ms_mspro_buf->mspro.regs.Status_Reg)->WP;
  18. ms_mspro_info->card_type = CARD_TYPE_MSPRO;
  19. return MS_MSPRO_NO_ERROR;
  20. }
  21. else
  22. {
  23. if (ms_mspro_buf->mspro.regs.Class_Reg & 0x03) //Class_Reg = 0x01/0x02/0x03
  24. {
  25. ms_mspro_info->read_only_flag = 0x01;
  26. ms_mspro_info->card_type = CARD_TYPE_MSPRO;
  27. return MS_MSPRO_NO_ERROR;
  28. }
  29. else
  30. {
  31. return MS_MSPRO_ERROR_MEDIA_TYPE;
  32. }
  33. }
  34. }
  35. int mspro_cpu_startup(MS_MSPRO_Card_Info_t *ms_mspro_info)
  36. {
  37. MS_MSPRO_TPC_Packet_t packet;
  38. MS_MSPRO_INT_Register_t * pIntReg;
  39. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  40. int error;
  41. int retry_cnt = 0;
  42. do
  43. {
  44. ms_delay_ms(10);
  45. retry_cnt++;
  46. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  47. packet.param.in.count = 1;
  48. packet.param.in.buffer = &ms_mspro_buf->mspro.regs.INT_Reg;
  49. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  50. if(error)
  51. continue;
  52. //check command status
  53. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->mspro.regs.INT_Reg;
  54. if(!pIntReg->CED)
  55. continue;
  56. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  57. packet.param.in.count = 1;
  58. packet.param.in.buffer = &ms_mspro_buf->mspro.regs.INT_Reg;
  59. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  60. if(error)
  61. continue;
  62. if(pIntReg->ERR)
  63. {
  64. if(pIntReg->CMDNK)
  65. {
  66. ms_mspro_info->write_protected_flag = 1;
  67. break;
  68. }
  69. else
  70. {
  71. error = MSPRO_ERROR_MEDIA_BREAKDOWN;
  72. return error;
  73. }
  74. }
  75. else
  76. {
  77. break;
  78. }
  79. } while(retry_cnt < MSPRO_STARTUP_TIMEOUT);
  80. if(retry_cnt >= MSPRO_STARTUP_TIMEOUT)
  81. return MSPRO_ERROR_STARTUP_TIMEOUT;
  82. else
  83. return MS_MSPRO_NO_ERROR;
  84. }
  85. int mspro_confirm_attribute_information(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned char * data_buf)
  86. {
  87. int error,i;
  88. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  89. unsigned char entry_cnt = 0, entry_index = 0;
  90. // read attribute information
  91. error = mspro_read_attribute_sector(ms_mspro_info, 0, 1, data_buf);
  92. if(error)
  93. return error;
  94. // save attribute information area
  95. memcpy(&ms_mspro_buf->mspro.attribute_information_area, data_buf, MSRPO_ATTRIBUTE_INFOMATION_SIZE);
  96. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, &ms_mspro_buf->mspro.attribute_information_area.Signature_Code);
  97. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, &ms_mspro_buf->mspro.attribute_information_area.Version_Information);
  98. if(ms_mspro_buf->mspro.attribute_information_area.Signature_Code != 0xA5C3)
  99. return MSPRO_ERROR_MEDIA_BREAKDOWN;
  100. // check device information entry count
  101. entry_cnt = ms_mspro_buf->mspro.attribute_information_area.Device_Information_Entry_Count;
  102. if((entry_cnt < 1) || (entry_cnt > MSRPO_MAX_DEVICE_INFORMATION_ENTRY))
  103. return MSPRO_ERROR_MEDIA_BREAKDOWN;
  104. // check device information entry
  105. memcpy(&ms_mspro_buf->mspro.device_information_entry, data_buf+MSRPO_ATTRIBUTE_INFOMATION_SIZE, entry_cnt*MSRPO_DEVICE_INFORMATION_ENTRY_SIZE);
  106. error = MSPRO_ERROR_MEDIA_BREAKDOWN;
  107. for(i=0; i<entry_cnt; i++)
  108. {
  109. ms_mspro_endian_convert(ENDIAN_TYPE_DWORD, &ms_mspro_buf->mspro.device_information_entry[i].Address);
  110. ms_mspro_endian_convert(ENDIAN_TYPE_DWORD, &ms_mspro_buf->mspro.device_information_entry[i].Size);
  111. if(ms_mspro_buf->mspro.device_information_entry[i].Device_Information_ID != MSPRO_DEVID_SYSTEM_INFORMATION)
  112. continue;
  113. if(ms_mspro_buf->mspro.device_information_entry[i].Size != MSPRO_SYSTEM_INFORMATION_SIZE)
  114. break;
  115. if(ms_mspro_buf->mspro.device_information_entry[i].Address < 0x1A0)
  116. break;
  117. if((ms_mspro_buf->mspro.device_information_entry[i].Address + ms_mspro_buf->mspro.device_information_entry[i].Size) > 0x8000)
  118. break;
  119. entry_index = i;
  120. error = MS_MSPRO_NO_ERROR;
  121. }
  122. if(error)
  123. return error;
  124. ms_mspro_buf->mspro.system_entry_index = entry_index;
  125. return MS_MSPRO_NO_ERROR;
  126. }
  127. int mspro_confirm_system_information(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned char * data_buf)
  128. {
  129. int error;
  130. unsigned short sector_addr, data_offset, data_size;
  131. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  132. sector_addr = ms_mspro_buf->mspro.device_information_entry[ms_mspro_buf->mspro.system_entry_index].Address / MSPRO_SECTOR_SIZE;
  133. data_offset = ms_mspro_buf->mspro.device_information_entry[ms_mspro_buf->mspro.system_entry_index].Address % MSPRO_SECTOR_SIZE;
  134. data_size = ms_mspro_buf->mspro.device_information_entry[ms_mspro_buf->mspro.system_entry_index].Size;
  135. // read system information
  136. error = mspro_read_attribute_sector(ms_mspro_info, sector_addr, 1, data_buf);
  137. if(error)
  138. return error;
  139. // save system information
  140. memcpy(&ms_mspro_buf->mspro.system_information, data_buf+data_offset, MSPRO_SECTOR_SIZE-data_offset);
  141. if(data_size > (MSPRO_SECTOR_SIZE-data_offset))
  142. {
  143. // read system information
  144. error = mspro_read_attribute_sector(ms_mspro_info, sector_addr+1, 1, data_buf);
  145. if(error)
  146. return error;
  147. // save system information
  148. data_offset = data_size-(MSPRO_SECTOR_SIZE-data_offset)-1;
  149. memcpy((unsigned char *)&ms_mspro_buf->mspro.system_information+data_offset, data_buf, data_size-(MSPRO_SECTOR_SIZE-data_offset));
  150. }
  151. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, &ms_mspro_buf->mspro.system_information.Block_Size);
  152. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, &ms_mspro_buf->mspro.system_information.Total_Blocks);
  153. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, &ms_mspro_buf->mspro.system_information.User_Area_Blocks);
  154. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, &ms_mspro_buf->mspro.system_information.Page_Size);
  155. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, &ms_mspro_buf->mspro.system_information.Assembly_Date.Year);
  156. ms_mspro_endian_convert(ENDIAN_TYPE_DWORD, &ms_mspro_buf->mspro.system_information.Serial_Number);
  157. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, &ms_mspro_buf->mspro.system_information.Memory_Maker_Code);
  158. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, &ms_mspro_buf->mspro.system_information.Memory_Model_Code);
  159. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, &ms_mspro_buf->mspro.system_information.Controller_Number);
  160. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, &ms_mspro_buf->mspro.system_information.Controller_Function);
  161. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, &ms_mspro_buf->mspro.system_information.Start_Sector);
  162. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, &ms_mspro_buf->mspro.system_information.Unit_Size);
  163. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, &ms_mspro_buf->mspro.system_information.Controller_Code);
  164. // check system information
  165. if(ms_mspro_buf->mspro.system_information.Memory_Stick_Class != 2)
  166. return MSPRO_ERROR_MEDIA_BREAKDOWN;
  167. if(ms_mspro_buf->mspro.system_information.Device_Type == 0)
  168. {
  169. if(ms_mspro_info->read_only_flag)
  170. ms_mspro_info->read_only_flag = 0;
  171. }
  172. else if((ms_mspro_buf->mspro.system_information.Device_Type == 0x01) ||
  173. (ms_mspro_buf->mspro.system_information.Device_Type == 0x02) ||
  174. (ms_mspro_buf->mspro.system_information.Device_Type == 0x03))
  175. {
  176. if(!ms_mspro_info->read_only_flag)
  177. ms_mspro_info->read_only_flag = 1;
  178. }
  179. else
  180. {
  181. error = MSPRO_ERROR_MEDIA_BREAKDOWN;
  182. return error;
  183. }
  184. if((ms_mspro_buf->mspro.system_information.Memory_Stick_Sub_Class & 0xC0) != 0x00)
  185. {
  186. ms_mspro_info->write_protected_flag = 1;
  187. }
  188. return MS_MSPRO_NO_ERROR;
  189. }
  190. int mspro_recognize_file_system()
  191. {
  192. return MS_MSPRO_NO_ERROR;
  193. }
  194. int mspro_read_user_sector(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long sector_addr, unsigned short sector_count, unsigned char * data_buf)
  195. {
  196. MS_MSPRO_TPC_Packet_t packet;
  197. MS_MSPRO_INT_Register_t * pIntReg;
  198. unsigned long data_offset = 0;
  199. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  200. int error;
  201. unsigned char* buf = ms_mspro_info->data_buf;
  202. mass_counter=0;
  203. if(sector_count == 0)
  204. return MS_MSPRO_ERROR_PARAMETER;
  205. if((ms_mspro_buf->mspro.reg_set.write_addr != 0x11) ||
  206. (ms_mspro_buf->mspro.reg_set.write_size != 0x06))
  207. {
  208. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  209. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  210. ms_mspro_buf->mspro.reg_set.write_addr = 0x11;
  211. ms_mspro_buf->mspro.reg_set.write_size = 0x06;
  212. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->mspro.reg_set;
  213. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  214. if(error)
  215. return error;
  216. }
  217. #ifdef MS_MSPRO_HW_CONTROL
  218. if(MS_WORK_MODE == CARD_HW_MODE)
  219. {
  220. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  221. packet.param.out.count = 6; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  222. ms_mspro_buf->mspro.regs.Data_Count_Reg1 = (sector_count>>8) & 0xFF;
  223. ms_mspro_buf->mspro.regs.Data_Count_Reg0 = sector_count & 0xFF;
  224. ms_mspro_buf->mspro.regs.parameters.data.Data_Address_Reg3 = (sector_addr>>24) & 0xFF;
  225. ms_mspro_buf->mspro.regs.parameters.data.Data_Address_Reg2 = (sector_addr>>16) & 0xFF;
  226. ms_mspro_buf->mspro.regs.parameters.data.Data_Address_Reg1 = (sector_addr>>8) & 0xFF;
  227. ms_mspro_buf->mspro.regs.parameters.data.Data_Address_Reg0 = sector_addr & 0xFF;
  228. packet.param.out.buffer = &ms_mspro_buf->mspro.regs.Data_Count_Reg1;
  229. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  230. if(error)
  231. return error;
  232. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  233. packet.param.out.count = 1;
  234. packet.param.out.buffer = buf;
  235. packet.param.out.buffer[0] = read_sector_type ? CMD_MSPRO_READ_ATRB : CMD_MSPRO_READ_DATA;
  236. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  237. if(error)
  238. return error;
  239. }
  240. #endif
  241. #ifdef MS_MSPRO_SW_CONTROL
  242. if(MS_WORK_MODE == CARD_SW_MODE)
  243. {
  244. packet.TPC_cmd.value = TPC_MSPRO_EX_SET_CMD; //EX_SET_CMD
  245. packet.param.out.count = 7;
  246. packet.param.out.buffer = buf;
  247. packet.param.out.buffer[0] = CMD_MSPRO_READ_DATA;
  248. packet.param.out.buffer[1] = (sector_count>>8) & 0xFF;
  249. packet.param.out.buffer[2] = sector_count & 0xFF;
  250. packet.param.out.buffer[3] = (sector_addr>>24) & 0xFF;
  251. packet.param.out.buffer[4] = (sector_addr>>16) & 0xFF;
  252. packet.param.out.buffer[5] = (sector_addr>>8) & 0xFF;
  253. packet.param.out.buffer[6] = sector_addr & 0xFF;
  254. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  255. if(error)
  256. return error;
  257. }
  258. #endif
  259. while(1)
  260. {
  261. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  262. if(error)
  263. return error;
  264. //get INT register
  265. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  266. {
  267. ms_mspro_buf->mspro.regs.INT_Reg = packet.int_reg;
  268. }
  269. else
  270. {
  271. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  272. packet.param.in.count = 1;
  273. packet.param.in.buffer = &ms_mspro_buf->mspro.regs.INT_Reg;
  274. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  275. if(error)
  276. return error;
  277. }
  278. //check command status
  279. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->mspro.regs.INT_Reg;
  280. if(pIntReg->CMDNK)
  281. { //BLOCK_READ CMD can not be excuted
  282. return MS_MSPRO_ERROR_CMDNK;
  283. }
  284. if(pIntReg->ERR && !pIntReg->BREQ) // BREQ = 1 or ERR & BREQ = 1
  285. {
  286. return MS_MSPRO_ERROR_FLASH_READ;
  287. }
  288. if(pIntReg->CED)
  289. {
  290. if(mass_counter == 0)
  291. {
  292. return MS_MSPRO_ERROR_NO_READ;
  293. }
  294. else if(mass_counter != sector_count)
  295. {
  296. mspro_access_status_reg_after_read = 1;
  297. }
  298. break;
  299. }
  300. if (mass_counter == sector_count)
  301. {
  302. mass_counter = 0;
  303. return MS_MSPRO_ERROR_TIMEOUT;
  304. }
  305. packet.TPC_cmd.value = TPC_MSPRO_READ_LONG_DATA; //READ_LONG_DATA
  306. packet.param.in.count = MSPRO_SECTOR_SIZE; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  307. packet.param.in.buffer = data_buf+data_offset;
  308. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  309. if(error)
  310. return error;
  311. mass_counter++;
  312. #ifdef AMLOGIC_CHIP_SUPPORT
  313. data_offset += ((unsigned long)data_buf == 0x3400000) ? 0 : MSPRO_SECTOR_SIZE;
  314. #else
  315. data_offset += MSPRO_SECTOR_SIZE;
  316. #endif
  317. }
  318. //for reset some m2 card state after read user sector
  319. if(mspro_access_status_reg_after_read)
  320. {
  321. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //READ_REG: Status, Type, Catagory, Class
  322. packet.param.out.count = 4;
  323. ms_mspro_buf->ms.reg_set.read_addr = 0x02; //READ_ADRS = 0x02
  324. ms_mspro_buf->ms.reg_set.read_size = 0x01; //READ_SIZE = 0x06
  325. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  326. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  327. if(error)
  328. return error;
  329. packet.TPC_cmd.value = TPC_MS_MSPRO_READ_REG; //READ_REG
  330. packet.param.in.count = 1; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  331. packet.param.in.buffer = &ms_mspro_buf->ms.regs.Status_Reg0;
  332. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  333. if(error)
  334. return error;
  335. }
  336. return MS_MSPRO_NO_ERROR;
  337. }
  338. int mspro_write_user_sector(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long sector_addr, unsigned short sector_count, unsigned char * data_buf)
  339. {
  340. MS_MSPRO_TPC_Packet_t packet;
  341. MS_MSPRO_INT_Register_t * pIntReg;
  342. unsigned long data_offset = 0;
  343. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  344. int error;
  345. unsigned char* buf = ms_mspro_info->data_buf;
  346. mass_counter=0;
  347. if(sector_count == 0)
  348. return MS_MSPRO_ERROR_PARAMETER;
  349. if((ms_mspro_buf->mspro.reg_set.write_addr != 0x11) ||
  350. (ms_mspro_buf->mspro.reg_set.write_size != 0x06))
  351. {
  352. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  353. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  354. ms_mspro_buf->mspro.reg_set.write_addr = 0x11;
  355. ms_mspro_buf->mspro.reg_set.write_size = 0x06;
  356. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->mspro.reg_set;
  357. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  358. if(error)
  359. return error;
  360. }
  361. #ifdef MS_MSPRO_HW_CONTROL
  362. if(MS_WORK_MODE == CARD_HW_MODE)
  363. {
  364. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  365. packet.param.out.count = 6; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  366. ms_mspro_buf->mspro.regs.Data_Count_Reg1 = (sector_count>>8) & 0xFF;
  367. ms_mspro_buf->mspro.regs.Data_Count_Reg0 = sector_count & 0xFF;
  368. ms_mspro_buf->mspro.regs.parameters.data.Data_Address_Reg3 = (sector_addr>>24) & 0xFF;
  369. ms_mspro_buf->mspro.regs.parameters.data.Data_Address_Reg2 = (sector_addr>>16) & 0xFF;
  370. ms_mspro_buf->mspro.regs.parameters.data.Data_Address_Reg1 = (sector_addr>>8) & 0xFF;
  371. ms_mspro_buf->mspro.regs.parameters.data.Data_Address_Reg0 = sector_addr & 0xFF;
  372. packet.param.out.buffer = &ms_mspro_buf->mspro.regs.Data_Count_Reg1;
  373. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  374. if(error)
  375. return error;
  376. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  377. packet.param.out.count = 1;
  378. packet.param.out.buffer = buf;
  379. packet.param.out.buffer[0] = CMD_MSPRO_WRITE_DATA;
  380. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  381. if(error)
  382. return error;
  383. }
  384. #endif
  385. #ifdef MS_MSPRO_SW_CONTROL
  386. if(MS_WORK_MODE == CARD_SW_MODE)
  387. {
  388. packet.TPC_cmd.value = TPC_MSPRO_EX_SET_CMD; //EX_SET_CMD
  389. packet.param.out.count = 7;
  390. packet.param.out.buffer = buf;
  391. packet.param.out.buffer[0] = CMD_MSPRO_WRITE_DATA;
  392. packet.param.out.buffer[1] = (sector_count>>8) & 0xFF;
  393. packet.param.out.buffer[2] = sector_count & 0xFF;
  394. packet.param.out.buffer[3] = (sector_addr>>24) & 0xFF;
  395. packet.param.out.buffer[4] = (sector_addr>>16) & 0xFF;
  396. packet.param.out.buffer[5] = (sector_addr>>8) & 0xFF;
  397. packet.param.out.buffer[6] = sector_addr & 0xFF;
  398. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  399. if(error)
  400. return error;
  401. }
  402. #endif
  403. while(1)
  404. {
  405. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  406. if(error)
  407. return error;
  408. //get INT register
  409. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  410. {
  411. ms_mspro_buf->mspro.regs.INT_Reg = packet.int_reg;
  412. }
  413. else
  414. {
  415. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  416. packet.param.in.count = 1;
  417. packet.param.in.buffer = &ms_mspro_buf->mspro.regs.INT_Reg;
  418. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  419. if(error)
  420. return error;
  421. }
  422. //check command status
  423. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->mspro.regs.INT_Reg;
  424. if(pIntReg->CED && pIntReg->ERR && pIntReg->CMDNK)
  425. {
  426. return MSPRO_ERROR_WRITE_DISABLED;
  427. }
  428. else if (pIntReg->CMDNK)
  429. {
  430. return MS_MSPRO_ERROR_CMDNK;
  431. }
  432. if(pIntReg->ERR && pIntReg->CED)
  433. {
  434. return MS_MSPRO_ERROR_FLASH_WRITE;
  435. }
  436. if(pIntReg->CED)
  437. {
  438. break;
  439. }
  440. packet.TPC_cmd.value = TPC_MSPRO_WRITE_LONG_DATA; //WRITE_LONG_DATA
  441. packet.param.out.count = MSPRO_SECTOR_SIZE; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  442. packet.param.out.buffer = data_buf+data_offset;
  443. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  444. if(error)
  445. return error;
  446. mass_counter++;
  447. data_offset += MSPRO_SECTOR_SIZE;
  448. }
  449. return MS_MSPRO_NO_ERROR;
  450. }
  451. int mspro_erase_user_sector(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long sector_addr, unsigned short sector_count)
  452. {
  453. MS_MSPRO_TPC_Packet_t packet;
  454. MS_MSPRO_INT_Register_t * pIntReg;
  455. //unsigned char report_buf[MSPRO_REPORT_SIZE];
  456. unsigned long all_sectorts = 0, processed_sectors = 0;
  457. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  458. int error;
  459. unsigned char* buf = ms_mspro_info->data_buf;
  460. unsigned char* report_buf = ms_mspro_info->data_buf;
  461. memset(report_buf, 0, MSPRO_REPORT_SIZE);
  462. if(sector_count == 0)
  463. return MS_MSPRO_ERROR_PARAMETER;
  464. #ifdef MS_MSPRO_HW_CONTROL
  465. if(MS_WORK_MODE == CARD_HW_MODE)
  466. {
  467. if((ms_mspro_buf->mspro.reg_set.write_addr != 0x11) ||
  468. (ms_mspro_buf->mspro.reg_set.write_size != 0x06))
  469. {
  470. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  471. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  472. ms_mspro_buf->mspro.reg_set.write_addr = 0x11;
  473. ms_mspro_buf->mspro.reg_set.write_size = 0x06;
  474. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->mspro.reg_set;
  475. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  476. if(error)
  477. return error;
  478. }
  479. }
  480. #endif
  481. #ifdef MS_MSPRO_SW_CONTROL
  482. if(MS_WORK_MODE == CARD_SW_MODE)
  483. {
  484. if((ms_mspro_buf->mspro.reg_set.write_addr != 0x11) ||
  485. (ms_mspro_buf->mspro.reg_set.write_size != 0x07))
  486. {
  487. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  488. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  489. ms_mspro_buf->mspro.reg_set.write_addr = 0x11;
  490. ms_mspro_buf->mspro.reg_set.write_size = 0x07;
  491. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->mspro.reg_set;
  492. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  493. if(error)
  494. return error;
  495. }
  496. }
  497. #endif
  498. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  499. #ifdef MS_MSPRO_HW_CONTROL
  500. if(MS_WORK_MODE == CARD_HW_MODE)
  501. packet.param.out.count = 6; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  502. #endif
  503. #ifdef MS_MSPRO_SW_CONTROL
  504. if(MS_WORK_MODE == CARD_SW_MODE)
  505. packet.param.out.count = 7; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  506. #endif
  507. ms_mspro_buf->mspro.regs.Data_Count_Reg1 = (sector_count>>8) & 0xFF;
  508. ms_mspro_buf->mspro.regs.Data_Count_Reg0 = sector_count & 0xFF;
  509. ms_mspro_buf->mspro.regs.parameters.data.Data_Address_Reg3 = (sector_addr>>24) & 0xFF;
  510. ms_mspro_buf->mspro.regs.parameters.data.Data_Address_Reg2 = (sector_addr>>16) & 0xFF;
  511. ms_mspro_buf->mspro.regs.parameters.data.Data_Address_Reg1 = (sector_addr>>8) & 0xFF;
  512. ms_mspro_buf->mspro.regs.parameters.data.Data_Address_Reg0 = sector_addr & 0xFF;
  513. ms_mspro_buf->mspro.regs.parameters.data.TPC_Pamameter_Reg = MSPRO_REPORT_TYPE;
  514. packet.param.out.buffer = &ms_mspro_buf->mspro.regs.Data_Count_Reg1;
  515. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  516. if(error)
  517. return error;
  518. #ifdef MS_MSPRO_HW_CONTROL
  519. if(MS_WORK_MODE == CARD_HW_MODE)
  520. {
  521. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  522. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  523. ms_mspro_buf->mspro.reg_set.write_addr = 0x17;
  524. ms_mspro_buf->mspro.reg_set.write_size = 0x01;
  525. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->mspro.reg_set;
  526. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  527. if(error)
  528. return error;
  529. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG: Status, Type, Catagory, Class
  530. packet.param.out.count = 1; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  531. ms_mspro_buf->mspro.regs.parameters.data.TPC_Pamameter_Reg = MSPRO_REPORT_TYPE;
  532. packet.param.out.buffer = &ms_mspro_buf->mspro.regs.parameters.data.TPC_Pamameter_Reg;
  533. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  534. if(error)
  535. return error;
  536. }
  537. #endif
  538. //why not use EXT_SET_CMD here? Because EXT_SET_CMD can only support 6 bytes register parameters
  539. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  540. packet.param.out.count = 1;
  541. packet.param.out.buffer = buf;
  542. packet.param.out.buffer[0] = CMD_MSPRO_ERASE;
  543. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  544. if(error)
  545. return error;
  546. while(1)
  547. {
  548. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  549. if(error)
  550. return error;
  551. //get INT register
  552. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  553. {
  554. ms_mspro_buf->mspro.regs.INT_Reg = packet.int_reg;
  555. }
  556. else
  557. {
  558. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  559. packet.param.in.count = 1;
  560. packet.param.in.buffer = &ms_mspro_buf->mspro.regs.INT_Reg;
  561. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  562. if(error)
  563. return error;
  564. }
  565. //check command status
  566. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->mspro.regs.INT_Reg;
  567. if(pIntReg->CMDNK)
  568. { //BLOCK_READ CMD can not be excuted
  569. return MS_MSPRO_ERROR_CMDNK;
  570. }
  571. if(pIntReg->ERR && pIntReg->CED)
  572. {
  573. return MS_MSPRO_ERROR_FLASH_ERASE;
  574. }
  575. if(pIntReg->CED)
  576. {
  577. break;
  578. }
  579. packet.TPC_cmd.value = TPC_MSPRO_READ_SHORT_DATA; //READ_SHORT_DATA
  580. packet.param.in.count = MSPRO_REPORT_SIZE; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  581. packet.param.in.buffer = report_buf;
  582. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  583. if(error)
  584. return error;
  585. all_sectorts = (report_buf[0]<<24) | (report_buf[1]<<16) | (report_buf[2]<<8) | report_buf[3];
  586. processed_sectors = (report_buf[4]<<24) | (report_buf[5]<<16) | (report_buf[6]<<8) | report_buf[7];
  587. }
  588. if(all_sectorts != processed_sectors)
  589. return MS_MSPRO_ERROR_FLASH_ERASE;
  590. else
  591. return MS_MSPRO_NO_ERROR;
  592. }
  593. int mspro_read_attribute_sector(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long sector_addr, unsigned short sector_count, unsigned char * data_buf)
  594. {
  595. MS_MSPRO_INT_Register_t * pIntReg;
  596. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  597. #ifdef MS_MSPRO_HW_CONTROL
  598. if(MS_WORK_MODE == CARD_HW_MODE)
  599. {
  600. int error,tracer;
  601. read_sector_type=1;
  602. error = mspro_read_user_sector(ms_mspro_info, sector_addr, sector_count, data_buf);
  603. tracer=0;
  604. while (1)
  605. {
  606. if (error == 0)
  607. break;
  608. if (mass_counter == 0)
  609. tracer++;
  610. else
  611. tracer=0;
  612. if (tracer>3)
  613. break;
  614. sector_addr += mass_counter;
  615. sector_count -= mass_counter;
  616. data_buf += MSPRO_SECTOR_SIZE*mass_counter;
  617. error = mspro_read_user_sector(ms_mspro_info, sector_addr, sector_count, data_buf);
  618. }
  619. read_sector_type=0;
  620. return error;
  621. }
  622. #endif
  623. #ifdef MS_MSPRO_SW_CONTROL
  624. if(MS_WORK_MODE == CARD_SW_MODE)
  625. {
  626. MS_MSPRO_TPC_Packet_t packet;
  627. unsigned long data_offset = 0;
  628. int error;
  629. unsigned char* buf = ms_mspro_info->data_buf;
  630. if(sector_count == 0)
  631. return MS_MSPRO_ERROR_PARAMETER;
  632. if((ms_mspro_buf->mspro.reg_set.write_addr != 0x11) ||
  633. (ms_mspro_buf->mspro.reg_set.write_size != 0x06))
  634. {
  635. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  636. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  637. ms_mspro_buf->mspro.reg_set.write_addr = 0x11;
  638. ms_mspro_buf->mspro.reg_set.write_size = 0x06;
  639. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->mspro.reg_set;
  640. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  641. if(error)
  642. return error;
  643. }
  644. packet.TPC_cmd.value = TPC_MSPRO_EX_SET_CMD; //EX_SET_CMD
  645. packet.param.out.count = 7;
  646. packet.param.out.buffer = buf;
  647. packet.param.out.buffer[0] = CMD_MSPRO_READ_ATRB;
  648. packet.param.out.buffer[1] = 0;
  649. packet.param.out.buffer[2] = sector_count;
  650. packet.param.out.buffer[3] = 0;
  651. packet.param.out.buffer[4] = 0;
  652. packet.param.out.buffer[5] = 0;
  653. packet.param.out.buffer[6] = sector_addr;
  654. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  655. if(error)
  656. return error;
  657. while(1)
  658. {
  659. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  660. if(error)
  661. return error;
  662. //get INT register
  663. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  664. {
  665. ms_mspro_buf->mspro.regs.INT_Reg = packet.int_reg;
  666. }
  667. else
  668. {
  669. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  670. packet.param.in.count = 1;
  671. packet.param.in.buffer = &ms_mspro_buf->mspro.regs.INT_Reg;
  672. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  673. if(error)
  674. return error;
  675. }
  676. //check command status
  677. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->mspro.regs.INT_Reg;
  678. if(pIntReg->CMDNK)
  679. { //BLOCK_READ CMD can not be excuted
  680. return MS_MSPRO_ERROR_CMDNK;
  681. }
  682. if(pIntReg->ERR && !pIntReg->BREQ)
  683. {
  684. return MS_MSPRO_ERROR_FLASH_READ;
  685. }
  686. if(pIntReg->CED)
  687. {
  688. break;
  689. }
  690. packet.TPC_cmd.value = TPC_MSPRO_READ_LONG_DATA; //READ_LONG_DATA
  691. packet.param.in.count = MSPRO_SECTOR_SIZE; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  692. packet.param.in.buffer = data_buf+data_offset;
  693. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  694. if(error)
  695. return error;
  696. data_offset += MSPRO_SECTOR_SIZE;
  697. }
  698. return MS_MSPRO_NO_ERROR;
  699. }
  700. #endif
  701. return 0;
  702. }
  703. int mspro_read_information_block()
  704. {
  705. return 0;
  706. }
  707. int mspro_update_imformation_block()
  708. {
  709. return 0;
  710. }
  711. int mspro_format()
  712. {
  713. return 0;
  714. }
  715. int mspro_sleep()
  716. {
  717. return 0;
  718. }