ms_protocol.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094
  1. #include "ms_port.h"
  2. #include "ms_misc.h"
  3. #include "ms_mspro.h"
  4. #include "ms_protocol.h"
  5. extern unsigned check_one_boot_block;
  6. int ms_media_type_identification(MS_MSPRO_Card_Info_t *ms_mspro_info)
  7. {
  8. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  9. if((ms_mspro_buf->ms.regs.Type_Reg >= 0x01) && (ms_mspro_buf->ms.regs.Type_Reg <= 0xFE))
  10. return MS_MSPRO_ERROR_MEDIA_TYPE;
  11. if(ms_mspro_buf->ms.regs.Type_Reg != 0x00)
  12. {
  13. if((ms_mspro_buf->ms.regs.Category_Reg == 0x00) ||
  14. ((ms_mspro_buf->ms.regs.Category_Reg >= 0x80) && (ms_mspro_buf->ms.regs.Category_Reg <= 0xFE)))
  15. return MS_MSPRO_ERROR_MEDIA_TYPE;
  16. if((ms_mspro_buf->ms.regs.Category_Reg >= 0x01) && (ms_mspro_buf->ms.regs.Category_Reg <= 0x7F))
  17. {
  18. ms_mspro_info->card_type = CARD_TYPE_MS; //Memory Stick I/O expanded module
  19. return MS_MSPRO_NO_ERROR;
  20. }
  21. if((ms_mspro_buf->ms.regs.Class_Reg == 0x00) ||
  22. ((ms_mspro_buf->ms.regs.Class_Reg >= 0x04) && (ms_mspro_buf->ms.regs.Class_Reg <= 0xFE)))
  23. return MS_MSPRO_ERROR_MEDIA_TYPE;
  24. }
  25. else
  26. {
  27. if(ms_mspro_buf->ms.regs.Category_Reg >= 0x80)
  28. return MS_MSPRO_ERROR_MEDIA_TYPE;
  29. if(ms_mspro_buf->ms.regs.Category_Reg >= 0x01)
  30. {
  31. ms_mspro_info->card_type = CARD_TYPE_MS; //Memory Stick I/O expanded module
  32. return MS_MSPRO_NO_ERROR;
  33. }
  34. if(ms_mspro_buf->ms.regs.Class_Reg >= 0x04)
  35. return MS_MSPRO_ERROR_MEDIA_TYPE;
  36. }
  37. if((ms_mspro_buf->ms.regs.Class_Reg >= 0x01) && (ms_mspro_buf->ms.regs.Class_Reg <= 0x03)) //always in Write_Protected status
  38. ms_mspro_info->read_only_flag = 0x01;
  39. else
  40. ms_mspro_info->write_protected_flag = ((MS_Status_Register0_t*)&ms_mspro_buf->ms.regs.Status_Reg0)->WP;
  41. ms_mspro_info->card_type = CARD_TYPE_MS;
  42. return MS_MSPRO_NO_ERROR;
  43. }
  44. int ms_search_boot_block(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned char * data_buf)
  45. {
  46. int i;
  47. int error;
  48. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  49. ms_mspro_buf->ms.boot_area_protection_process_flag = 0;
  50. ms_mspro_buf->ms.boot_block_nums = 0;
  51. for(i=0; i<=12; i++)
  52. {
  53. error = ms_read_page(ms_mspro_info, i, 0, data_buf);
  54. if(error)
  55. continue;
  56. if(!((MS_Overwrite_Flag_Register_t*)&ms_mspro_buf->ms.regs.Overwrite_Flag_Reg)->BKST)
  57. continue;
  58. if(((MS_Management_Flag_Register_t*)&ms_mspro_buf->ms.regs.Management_Flag_Reg)->SYSFLG)
  59. {
  60. ms_mspro_buf->ms.boot_area_protection_process_flag = 1;
  61. continue;
  62. }
  63. if(!((data_buf[0]==0x00) && (data_buf[1]==0x01)))
  64. {
  65. ms_mspro_buf->ms.boot_area_protection_process_flag = 1;
  66. continue;
  67. }
  68. //Boot Block Found!
  69. ms_mspro_buf->ms.boot_block_no[ms_mspro_buf->ms.boot_block_nums] = i;
  70. ms_mspro_buf->ms.boot_block_nums++;
  71. if((ms_mspro_buf->ms.boot_block_nums == 1)&&(check_one_boot_block == 1))
  72. break;
  73. if(ms_mspro_buf->ms.boot_block_nums >= 2)
  74. break;
  75. }
  76. if((ms_mspro_buf->ms.boot_block_nums == 1)&&(check_one_boot_block == 0))
  77. {
  78. check_one_boot_block = 1;
  79. }
  80. if((ms_mspro_buf->ms.boot_block_nums == 2) && (ms_mspro_buf->ms.boot_block_no[0] > ms_mspro_buf->ms.boot_block_no[1]))
  81. {
  82. i = ms_mspro_buf->ms.boot_block_no[0];
  83. ms_mspro_buf->ms.boot_block_no[0] = ms_mspro_buf->ms.boot_block_no[1];
  84. ms_mspro_buf->ms.boot_block_no[1] = i;
  85. }
  86. if(ms_mspro_buf->ms.boot_block_nums == 0)
  87. return MS_ERROR_BOOT_SEARCH;
  88. else
  89. return MS_MSPRO_NO_ERROR;
  90. }
  91. int ms_check_boot_block(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned char * data_buf)
  92. {
  93. int error;
  94. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  95. // Acquire Boot & Attribute Information form effective Boot Block
  96. error = ms_read_page(ms_mspro_info, ms_mspro_buf->ms.boot_block_no[0], 0, data_buf);
  97. if(error)
  98. return error;
  99. // header
  100. memcpy(&ms_mspro_buf->ms.boot_header.Block_ID, data_buf, 2);
  101. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, (unsigned char*)&ms_mspro_buf->ms.boot_header.Block_ID) ;
  102. ms_mspro_buf->ms.boot_header.Data_Entry = data_buf[188] ;
  103. // system entry
  104. memcpy(&ms_mspro_buf->ms.boot_system_entry, &data_buf[MS_BOOT_HEADER_SIZE], MS_BOOT_SYSTEM_ENTRY_SIZE);
  105. ms_mspro_endian_convert(ENDIAN_TYPE_DWORD, (unsigned char*)&ms_mspro_buf->ms.boot_system_entry.Disabled_Block_Start_Address);
  106. ms_mspro_endian_convert(ENDIAN_TYPE_DWORD, (unsigned char*)&ms_mspro_buf->ms.boot_system_entry.Disabled_Block_Data_Size);
  107. ms_mspro_endian_convert(ENDIAN_TYPE_DWORD, (unsigned char*)&ms_mspro_buf->ms.boot_system_entry.CIS_IDI_Start_Address);
  108. ms_mspro_endian_convert(ENDIAN_TYPE_DWORD, (unsigned char*)&ms_mspro_buf->ms.boot_system_entry.CIS_IDI_Data_Size);
  109. // boot & attribute information
  110. memcpy(&ms_mspro_buf->ms.boot_attribute_information, &data_buf[MS_BOOT_HEADER_SIZE+MS_BOOT_SYSTEM_ENTRY_SIZE], MS_BOOT_ATTRIBUTE_INFORMATION_SIZE);
  111. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, (unsigned char*)&ms_mspro_buf->ms.boot_attribute_information.Block_Size);
  112. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, (unsigned char*)&ms_mspro_buf->ms.boot_attribute_information.Block_Numbers);
  113. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, (unsigned char*)&ms_mspro_buf->ms.boot_attribute_information.Effective_Block_Numbers);
  114. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, (unsigned char*)&ms_mspro_buf->ms.boot_attribute_information.Page_Size);
  115. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, (unsigned char*)&ms_mspro_buf->ms.boot_attribute_information.Assembly_Date_Time.AD);
  116. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, (unsigned char*)&ms_mspro_buf->ms.boot_attribute_information.Memory_Manufacturer_Code);
  117. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, (unsigned char*)&ms_mspro_buf->ms.boot_attribute_information.Memory_Device_Code);
  118. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, (unsigned char*)&ms_mspro_buf->ms.boot_attribute_information.Implemented_Capacity);
  119. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, (unsigned char*)&ms_mspro_buf->ms.boot_attribute_information.Controller_Number);
  120. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, (unsigned char*)&ms_mspro_buf->ms.boot_attribute_information.Controller_Function);
  121. ms_mspro_endian_convert(ENDIAN_TYPE_WORD, (unsigned char*)&ms_mspro_buf->ms.boot_attribute_information.Format_Unique_Value6);
  122. // check Memory Stick Class
  123. if(ms_mspro_buf->ms.boot_attribute_information.Memory_Stick_Class != 1)
  124. return MS_ERROR_MEMORY_STICK_TYPE;
  125. // Confirm "Device Type", the value of "Parallel transfer support", and the register value of acquired from "Media Type Indeification Process"
  126. if(ms_mspro_buf->ms.regs.Type_Reg == 0x00)
  127. {
  128. if(ms_mspro_buf->ms.regs.Category_Reg == 0x00)
  129. {
  130. if(ms_mspro_buf->ms.regs.Class_Reg >= 0x04)
  131. error = MS_ERROR_MEMORY_STICK_TYPE;
  132. else if(ms_mspro_buf->ms.boot_attribute_information.Parallel_Transfer_Supporting & 0xFE) // != 0,1
  133. error = MS_ERROR_MEMORY_STICK_TYPE;
  134. else if(ms_mspro_buf->ms.regs.Class_Reg == 0x00)
  135. {
  136. if ((ms_mspro_buf->ms.boot_attribute_information.Device_Type != 0) &&
  137. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 1) &&
  138. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 3))
  139. error = MS_ERROR_MEMORY_STICK_TYPE;
  140. }
  141. else if(ms_mspro_buf->ms.regs.Class_Reg == 0x01)
  142. {
  143. if ((ms_mspro_buf->ms.boot_attribute_information.Device_Type != 1) &&
  144. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 3))
  145. error = MS_ERROR_MEMORY_STICK_TYPE;
  146. }
  147. else if(ms_mspro_buf->ms.regs.Class_Reg == 0x02)
  148. {
  149. if ((ms_mspro_buf->ms.boot_attribute_information.Device_Type != 1) &&
  150. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 2) &&
  151. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 3))
  152. error = MS_ERROR_MEMORY_STICK_TYPE;
  153. }
  154. else if(ms_mspro_buf->ms.regs.Class_Reg == 0x03)
  155. {
  156. if ((ms_mspro_buf->ms.boot_attribute_information.Device_Type != 1) &&
  157. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 2) &&
  158. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 3))
  159. error = MS_ERROR_MEMORY_STICK_TYPE;
  160. }
  161. }
  162. else if(ms_mspro_buf->ms.regs.Category_Reg >= 0x80)
  163. error = MS_ERROR_MEMORY_STICK_TYPE;
  164. }
  165. else if(ms_mspro_buf->ms.regs.Type_Reg == 0x01)
  166. {
  167. if(ms_mspro_buf->ms.regs.Category_Reg == 0x00)
  168. {
  169. if(ms_mspro_buf->ms.regs.Class_Reg >= 0x04)
  170. error = MS_ERROR_MEMORY_STICK_TYPE;
  171. else if(ms_mspro_buf->ms.regs.Class_Reg == 0x00)
  172. {
  173. if ((ms_mspro_buf->ms.boot_attribute_information.Device_Type != 0) &&
  174. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 1) &&
  175. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 2) &&
  176. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 3))
  177. error = MS_ERROR_MEMORY_STICK_TYPE;
  178. }
  179. else if(ms_mspro_buf->ms.regs.Class_Reg == 0x01)
  180. {
  181. if ((ms_mspro_buf->ms.boot_attribute_information.Device_Type != 1) &&
  182. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 3))
  183. error = MS_ERROR_MEMORY_STICK_TYPE;
  184. }
  185. else if(ms_mspro_buf->ms.regs.Class_Reg == 0x02)
  186. {
  187. if ((ms_mspro_buf->ms.boot_attribute_information.Device_Type != 1) &&
  188. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 2) &&
  189. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 3))
  190. error = MS_ERROR_MEMORY_STICK_TYPE;
  191. }
  192. else if(ms_mspro_buf->ms.regs.Class_Reg == 0x03)
  193. {
  194. if ((ms_mspro_buf->ms.boot_attribute_information.Device_Type != 1) &&
  195. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 2) &&
  196. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 3))
  197. error = MS_ERROR_MEMORY_STICK_TYPE;
  198. }
  199. }
  200. }
  201. else if((ms_mspro_buf->ms.regs.Type_Reg == 0x02) && (ms_mspro_buf->ms.regs.Type_Reg <= 0xFE))
  202. {
  203. error = MS_ERROR_MEMORY_STICK_TYPE;
  204. }
  205. else if(ms_mspro_buf->ms.regs.Type_Reg == 0xFF)
  206. {
  207. if(ms_mspro_buf->ms.regs.Category_Reg == 0x00)
  208. error = MS_ERROR_MEMORY_STICK_TYPE;
  209. else if((ms_mspro_buf->ms.regs.Category_Reg >= 0x80) && (ms_mspro_buf->ms.regs.Category_Reg <= 0xFE))
  210. error = MS_ERROR_MEMORY_STICK_TYPE;
  211. else if(ms_mspro_buf->ms.regs.Category_Reg == 0xFF)
  212. {
  213. if(ms_mspro_buf->ms.regs.Class_Reg == 0x00)
  214. error = MS_ERROR_MEMORY_STICK_TYPE;
  215. else if((ms_mspro_buf->ms.regs.Class_Reg >= 0x04) && (ms_mspro_buf->ms.regs.Class_Reg <= 0xFE))
  216. error = MS_ERROR_MEMORY_STICK_TYPE;
  217. else if(ms_mspro_buf->ms.boot_attribute_information.Parallel_Transfer_Supporting != 0)
  218. error = MS_ERROR_MEMORY_STICK_TYPE;
  219. else if(ms_mspro_buf->ms.regs.Class_Reg == 0x01)
  220. {
  221. if ((ms_mspro_buf->ms.boot_attribute_information.Device_Type != 1) &&
  222. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 3))
  223. error = MS_ERROR_MEMORY_STICK_TYPE;
  224. }
  225. else if(ms_mspro_buf->ms.regs.Class_Reg == 0x02)
  226. {
  227. if ((ms_mspro_buf->ms.boot_attribute_information.Device_Type != 1) &&
  228. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 2) &&
  229. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 3))
  230. error = MS_ERROR_MEMORY_STICK_TYPE;
  231. }
  232. else if(ms_mspro_buf->ms.regs.Class_Reg == 0x03)
  233. {
  234. if ((ms_mspro_buf->ms.boot_attribute_information.Device_Type != 1) &&
  235. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 2) &&
  236. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 3))
  237. error = MS_ERROR_MEMORY_STICK_TYPE;
  238. }
  239. else if(ms_mspro_buf->ms.regs.Class_Reg == 0xFF)
  240. {
  241. if ((ms_mspro_buf->ms.boot_attribute_information.Device_Type != 0) &&
  242. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 1) &&
  243. (ms_mspro_buf->ms.boot_attribute_information.Device_Type != 3))
  244. error = MS_ERROR_MEMORY_STICK_TYPE;
  245. }
  246. }
  247. }
  248. if(error)
  249. return error;
  250. // check Format Type
  251. if(ms_mspro_buf->ms.boot_attribute_information.Format_Type != 1)
  252. return MS_ERROR_FORMAT_TYPE;
  253. // Confirm number blocks, effective blocks and block size
  254. if(!(( //4MB
  255. (ms_mspro_buf->ms.boot_attribute_information.Block_Numbers == 0x0200) &&
  256. (ms_mspro_buf->ms.boot_attribute_information.Effective_Block_Numbers == 0x01f0) &&
  257. (ms_mspro_buf->ms.boot_attribute_information.Block_Size == 0x08)
  258. ) ||
  259. ( //8MB
  260. (ms_mspro_buf->ms.boot_attribute_information.Block_Numbers == 0x0400) &&
  261. (ms_mspro_buf->ms.boot_attribute_information.Effective_Block_Numbers == 0x03e0) &&
  262. (ms_mspro_buf->ms.boot_attribute_information.Block_Size == 0x08)
  263. ) ||
  264. ( //16MB
  265. (ms_mspro_buf->ms.boot_attribute_information.Block_Numbers == 0x0400) &&
  266. (ms_mspro_buf->ms.boot_attribute_information.Effective_Block_Numbers == 0x03e0) &&
  267. (ms_mspro_buf->ms.boot_attribute_information.Block_Size == 0x10)
  268. ) ||
  269. ( //32MB
  270. (ms_mspro_buf->ms.boot_attribute_information.Block_Numbers == 0x0800) &&
  271. (ms_mspro_buf->ms.boot_attribute_information.Effective_Block_Numbers == 0x07c0) &&
  272. (ms_mspro_buf->ms.boot_attribute_information.Block_Size == 0x10)
  273. ) ||
  274. ( //64MB
  275. (ms_mspro_buf->ms.boot_attribute_information.Block_Numbers == 0x1000) &&
  276. (ms_mspro_buf->ms.boot_attribute_information.Effective_Block_Numbers == 0x0f80) &&
  277. (ms_mspro_buf->ms.boot_attribute_information.Block_Size == 0x10)
  278. )||
  279. ( //128MB
  280. (ms_mspro_buf->ms.boot_attribute_information.Block_Numbers == 0x2000) &&
  281. (ms_mspro_buf->ms.boot_attribute_information.Effective_Block_Numbers == 0x1f00) &&
  282. (ms_mspro_buf->ms.boot_attribute_information.Block_Size == 0x10)
  283. )
  284. ))
  285. {
  286. return MS_ERROR_BLOCK_NUMBER_SIZE;
  287. }
  288. if((ms_mspro_buf->ms.boot_attribute_information.Controller_Function & 0xFF00) == 0x1000)
  289. {
  290. #ifdef MS_MSPRO_DEBUG
  291. Debug_Printf("Memory Stick MagicGate compliant media.\n");
  292. #endif
  293. }
  294. return MS_MSPRO_NO_ERROR;
  295. }
  296. int ms_check_disabled_block(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned char * data_buf)
  297. {
  298. int error;
  299. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  300. unsigned short block_no,i;
  301. // Acquire Boot & Attribute Information form effective Boot Block
  302. error = ms_read_page(ms_mspro_info, ms_mspro_buf->ms.boot_block_no[0], 1, data_buf);
  303. if(error)
  304. return MS_ERROR_DISABLED_BLOCK;
  305. // big-endian
  306. for(i=0; i<MS_MAX_DISABLED_BLOCKS; i++)
  307. {
  308. block_no = data_buf[i*2+0] << 8;
  309. block_no |= data_buf[i*2+1];
  310. ms_mspro_buf->ms.disabled_block_data.disabled_block_table[i] = block_no;
  311. if (block_no == 0xFFFF)
  312. break;
  313. }
  314. ms_mspro_buf->ms.disabled_block_data.disabled_block_nums = i;
  315. return MS_MSPRO_NO_ERROR;
  316. }
  317. int ms_boot_area_protection(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned char * data_buf)
  318. {
  319. int error,i,j,k;
  320. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  321. unsigned char last_block_no = 0;
  322. unsigned long pages_per_block = ms_mspro_buf->ms.boot_attribute_information.Block_Size*2;
  323. if(ms_mspro_buf->ms.boot_block_nums == 0)
  324. return MS_ERROR_BOOT_SEARCH;
  325. else if(ms_mspro_buf->ms.boot_block_nums == 1)
  326. last_block_no = ms_mspro_buf->ms.boot_block_no[0];
  327. else if(ms_mspro_buf->ms.boot_block_nums == 2)
  328. last_block_no = ms_mspro_buf->ms.boot_block_no[1];
  329. for(i=0; i<=last_block_no; i++)
  330. {
  331. if(ms_mspro_buf->ms.boot_block_nums == 1)
  332. {
  333. if(i == ms_mspro_buf->ms.boot_block_no[0])
  334. continue;
  335. }
  336. else if(ms_mspro_buf->ms.boot_block_nums == 2)
  337. {
  338. if((i == ms_mspro_buf->ms.boot_block_no[0]) || (i == ms_mspro_buf->ms.boot_block_no[1]))
  339. continue;
  340. }
  341. error = ms_read_page(ms_mspro_info, i, 0, data_buf);
  342. if(error)
  343. continue;
  344. if(((MS_Overwrite_Flag_Register_t*)&ms_mspro_buf->ms.regs.Overwrite_Flag_Reg)->BKST == 0)
  345. continue;
  346. if(!((ms_mspro_buf->ms.regs.Logical_Address_Reg1 == 0xFF) && (ms_mspro_buf->ms.regs.Logical_Address_Reg0 == 0xFF)))
  347. {
  348. // copy the contents of this block to User Area (alternative block)
  349. for(j=(last_block_no+1); j<(MS_BLOCKS_PER_SEGMENT-2); j++)
  350. {
  351. error = ms_read_page(ms_mspro_info, j, 0, data_buf);
  352. if(error)
  353. continue;
  354. if((ms_mspro_buf->ms.regs.Logical_Address_Reg1 == 0xFF) && (ms_mspro_buf->ms.regs.Logical_Address_Reg0 == 0xFF))
  355. {
  356. for(k=0; k<pages_per_block; k++)
  357. {
  358. error = ms_copy_page(ms_mspro_info, i, k, j, k, data_buf);
  359. if(error)
  360. break;
  361. }
  362. if(error)
  363. continue;
  364. else
  365. break;
  366. }
  367. }
  368. }
  369. ms_overwrite_extra_data(ms_mspro_info, i, 0, 0x7F);
  370. }
  371. return MS_MSPRO_NO_ERROR;
  372. }
  373. #if 0
  374. int ms_read_boot_idi(unsigned char * data_buf)
  375. {
  376. int error;
  377. if(ms_mspro_buf->ms.boot_system_entry.CIS_IDI_Data_Type_ID != 0x0A)
  378. return MS_ERROR_BOOT_IDI;
  379. // Acquire Boot IDI Information
  380. error = ms_read_page(ms_mspro_buf->ms.boot_block_no[0], 2, data_buf);
  381. if(error)
  382. return error;
  383. memcpy(&ms_mspro_buf->ms.boot_idi, data_buf+MS_BOOT_CIS_SIZE, ms_mspro_buf->ms.boot_system_entry.CIS_IDI_Data_Size);
  384. return MS_MSPRO_NO_ERROR;
  385. }
  386. #endif
  387. static unsigned short logical_physical_table[MS_LOGICAL_SIZE_PER_SEGMENT], free_table[MS_BLOCKS_PER_SEGMENT];
  388. int ms_logical_physical_table_creation(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned short seg_no)
  389. {
  390. int error;
  391. unsigned char US;
  392. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  393. //unsigned short logical_physical_table[MS_LOGICAL_SIZE_PER_SEGMENT], free_table[MS_BLOCKS_PER_SEGMENT];
  394. unsigned long physical_blk_no,last_seg_no;
  395. unsigned short free_table_data_nums,logical_address;
  396. unsigned short i;
  397. unsigned short start_logical_addr,end_logical_addr;
  398. if(seg_no == 0)
  399. {
  400. start_logical_addr = 0;
  401. end_logical_addr = MS_LOGICAL_SIZE_PER_SEGMENT-2-1;
  402. }
  403. else
  404. {
  405. start_logical_addr = seg_no*MS_LOGICAL_SIZE_PER_SEGMENT-2;
  406. end_logical_addr = (seg_no+1)*MS_LOGICAL_SIZE_PER_SEGMENT-2-1;
  407. }
  408. last_seg_no = ms_mspro_buf->ms.boot_attribute_information.Block_Numbers/MS_BLOCKS_PER_SEGMENT-1;
  409. // Logical Address / Physical Block Number Corresponding information creation process
  410. for(i=0; i<MS_LOGICAL_SIZE_PER_SEGMENT; i++)
  411. logical_physical_table[i] = 0xFFFF;
  412. for(i=0; i<MS_BLOCKS_PER_SEGMENT; i++)
  413. free_table[i] = 0xFFFF;
  414. free_table_data_nums = 0;
  415. for(physical_blk_no=(seg_no*MS_BLOCKS_PER_SEGMENT); physical_blk_no<((seg_no+1)*MS_BLOCKS_PER_SEGMENT); physical_blk_no++)
  416. {
  417. if((ms_mspro_buf->ms.boot_block_nums == 1) && (physical_blk_no == ms_mspro_buf->ms.boot_block_no[0]))
  418. continue;
  419. if((ms_mspro_buf->ms.boot_block_nums == 2) && ((physical_blk_no == ms_mspro_buf->ms.boot_block_no[0]) || (physical_blk_no == ms_mspro_buf->ms.boot_block_no[1])))
  420. continue;
  421. for(i=0; i<ms_mspro_buf->ms.disabled_block_data.disabled_block_nums; i++)
  422. {
  423. if(physical_blk_no == ms_mspro_buf->ms.disabled_block_data.disabled_block_table[i])
  424. break;
  425. }
  426. if(i != ms_mspro_buf->ms.disabled_block_data.disabled_block_nums)
  427. continue;
  428. error = ms_read_extra_data(ms_mspro_info, physical_blk_no, 0);
  429. if(error)
  430. {
  431. if(error == MS_MSPRO_ERROR_FLASH_READ)
  432. ms_overwrite_extra_data(ms_mspro_info, physical_blk_no, 0, ms_mspro_buf->ms.regs.Overwrite_Flag_Reg&0x7F);
  433. continue;
  434. }
  435. if((seg_no == last_seg_no) &&
  436. !((MS_Management_Flag_Register_t*)(&ms_mspro_buf->ms.regs.Management_Flag_Reg))->ATFLG)
  437. {
  438. ms_erase_block(ms_mspro_info, physical_blk_no);
  439. }
  440. if(((MS_Overwrite_Flag_Register_t*)(&ms_mspro_buf->ms.regs.Overwrite_Flag_Reg))->BKST)
  441. {
  442. unsigned char PS = ((MS_Overwrite_Flag_Register_t*)(&ms_mspro_buf->ms.regs.Overwrite_Flag_Reg))->PGST1<<1 | ((MS_Overwrite_Flag_Register_t*)(&ms_mspro_buf->ms.regs.Overwrite_Flag_Reg))->PGST0;
  443. if((PS != 0) && (PS != 3))
  444. {
  445. ms_overwrite_extra_data(ms_mspro_info, physical_blk_no, 0, ms_mspro_buf->ms.regs.Overwrite_Flag_Reg&0x7F);
  446. continue;
  447. }
  448. logical_address = ms_mspro_buf->ms.regs.Logical_Address_Reg1<<8 | ms_mspro_buf->ms.regs.Logical_Address_Reg0;
  449. if((logical_address == 0xFFFF) || (logical_address < start_logical_addr) || (logical_address > end_logical_addr))
  450. {
  451. ms_erase_block(ms_mspro_info, physical_blk_no);
  452. free_table[free_table_data_nums++] = physical_blk_no;
  453. continue;
  454. }
  455. if(logical_physical_table[logical_address-start_logical_addr] == 0xFFFF)
  456. {
  457. logical_physical_table[logical_address-start_logical_addr] = physical_blk_no;
  458. continue;
  459. }
  460. US = ((MS_Overwrite_Flag_Register_t*)(&ms_mspro_buf->ms.regs.Overwrite_Flag_Reg))->UDST;
  461. error = ms_read_extra_data(ms_mspro_info, physical_blk_no, 0);
  462. if(error)
  463. {
  464. continue;
  465. }
  466. if(US == ((MS_Overwrite_Flag_Register_t*)(&ms_mspro_buf->ms.regs.Overwrite_Flag_Reg))->UDST)
  467. {
  468. if(physical_blk_no < logical_physical_table[logical_address-start_logical_addr])
  469. {
  470. ms_erase_block(ms_mspro_info, physical_blk_no);
  471. free_table[free_table_data_nums++] = physical_blk_no;
  472. }
  473. else
  474. {
  475. ms_erase_block(ms_mspro_info, logical_physical_table[logical_address-start_logical_addr]);
  476. free_table[free_table_data_nums++] = logical_physical_table[logical_address-start_logical_addr];
  477. logical_physical_table[logical_address-start_logical_addr] = physical_blk_no;
  478. }
  479. }
  480. else
  481. {
  482. if(US == 0)
  483. {
  484. ms_erase_block(ms_mspro_info, logical_physical_table[logical_address-start_logical_addr]);
  485. free_table[free_table_data_nums++] = logical_physical_table[logical_address-start_logical_addr];
  486. logical_physical_table[logical_address-start_logical_addr] = physical_blk_no;
  487. }
  488. else
  489. {
  490. ms_erase_block(ms_mspro_info, physical_blk_no);
  491. free_table[free_table_data_nums++] = physical_blk_no;
  492. }
  493. }
  494. }
  495. }
  496. // Logical Address Confirmation Process
  497. for(logical_address=start_logical_addr; logical_address<=end_logical_addr; logical_address++)
  498. {
  499. if(((seg_no == last_seg_no) && (free_table_data_nums < 2)) ||
  500. ((seg_no != last_seg_no) && (free_table_data_nums < 1)))
  501. break;
  502. if(logical_physical_table[logical_address-start_logical_addr] == 0xFFFF)
  503. {
  504. error = ms_read_extra_data(ms_mspro_info, free_table[free_table_data_nums-1], 0);
  505. if(error)
  506. {
  507. free_table_data_nums--;
  508. }
  509. else
  510. {
  511. ms_mspro_buf->ms.regs.Logical_Address_Reg1 = (logical_address >> 8) & 0xFF;
  512. ms_mspro_buf->ms.regs.Logical_Address_Reg0 = logical_address & 0xFF;
  513. error = ms_write_extra_data(ms_mspro_info, free_table[free_table_data_nums-1], 0);
  514. logical_physical_table[logical_address-start_logical_addr] = free_table[free_table_data_nums-1];
  515. free_table[free_table_data_nums-1] = 0xFFFF;
  516. free_table_data_nums--;
  517. }
  518. }
  519. }
  520. for(logical_address=start_logical_addr; logical_address<=end_logical_addr; logical_address++)
  521. {
  522. ms_mspro_buf->ms.logical_physical_table[logical_address] = logical_physical_table[logical_address-start_logical_addr];
  523. }
  524. for(i=0; i<MS_MAX_FREE_BLOCKS_PER_SEGMENT; i++)
  525. {
  526. ms_mspro_buf->ms.free_block_table[seg_no*MS_MAX_FREE_BLOCKS_PER_SEGMENT+i] = free_table[i];
  527. }
  528. for(logical_address=start_logical_addr; logical_address<=end_logical_addr; logical_address++)
  529. {
  530. if(logical_physical_table[logical_address-start_logical_addr] == 0xFFFF)
  531. {
  532. #ifdef MS_MSPRO_DEBUG
  533. Debug_Printf("Error logical block %d, segment %d\n", logical_address-start_logical_addr, seg_no);
  534. #endif
  535. //return MS_ERROR_LOGICAL_PHYSICAL_TABLE;
  536. }
  537. }
  538. return MS_MSPRO_NO_ERROR;
  539. }
  540. int ms_read_page(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long block_addr, unsigned char page_addr, unsigned char * data_buf)
  541. {
  542. MS_MSPRO_TPC_Packet_t packet;
  543. MS_MSPRO_INT_Register_t * pIntReg;
  544. MS_Status_Register1_t * pStatusReg1;
  545. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  546. int error;
  547. unsigned char* buf = ms_mspro_info->data_buf;
  548. #ifdef MS_MSPRO_HW_CONTROL
  549. if(MS_WORK_MODE == CARD_HW_MODE)
  550. {
  551. if((ms_mspro_buf->ms.reg_set.read_addr != 0x01) ||
  552. (ms_mspro_buf->ms.reg_set.read_size != 0x10) ||
  553. (ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  554. (ms_mspro_buf->ms.reg_set.write_size != 0x06))
  555. {
  556. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  557. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  558. ms_mspro_buf->ms.reg_set.read_addr = 0x01;
  559. ms_mspro_buf->ms.reg_set.read_size = 0x10;
  560. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  561. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  562. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  563. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  564. if(error)
  565. return error;
  566. }
  567. }
  568. #endif
  569. #ifdef MS_MSPRO_SW_CONTROL
  570. if(MS_WORK_MODE == CARD_SW_MODE)
  571. {
  572. if((ms_mspro_buf->ms.reg_set.read_addr != 0x01) ||
  573. (ms_mspro_buf->ms.reg_set.read_size != 0x19) ||
  574. (ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  575. (ms_mspro_buf->ms.reg_set.write_size != 0x06))
  576. {
  577. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  578. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  579. ms_mspro_buf->ms.reg_set.read_addr = 0x01;
  580. ms_mspro_buf->ms.reg_set.read_size = 0x19;
  581. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  582. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  583. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  584. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  585. if(error)
  586. return error;
  587. }
  588. }
  589. #endif
  590. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  591. packet.param.out.count = 6; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  592. ms_mspro_buf->ms.regs.System_Parameter_Reg = (ms_mspro_info->interface_mode==INTERFACE_PARALLEL) ? 0x88 : 0x80;
  593. ms_mspro_buf->ms.regs.Block_Address_Reg2 = (block_addr >> 16) & 0xFF;
  594. ms_mspro_buf->ms.regs.Block_Address_Reg1 = (block_addr >> 8) & 0xFF;
  595. ms_mspro_buf->ms.regs.Block_Address_Reg0 = block_addr & 0xFF;
  596. ms_mspro_buf->ms.regs.CMD_Parameter_Reg = 0x20;
  597. ms_mspro_buf->ms.regs.Page_Address_Reg = page_addr;
  598. packet.param.out.buffer = &ms_mspro_buf->ms.regs.System_Parameter_Reg;
  599. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  600. if(error)
  601. return error;
  602. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  603. packet.param.out.count = 1;
  604. packet.param.out.buffer = buf;
  605. packet.param.out.buffer[0] = CMD_MS_BLOCK_READ;
  606. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  607. if(error)
  608. return error;
  609. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  610. if(error)
  611. return error;
  612. packet.TPC_cmd.value = TPC_MS_MSPRO_READ_REG; //READ_REG
  613. #ifdef MS_MSPRO_HW_CONTROL
  614. if(MS_WORK_MODE == CARD_HW_MODE)
  615. packet.param.in.count = 0x10; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  616. #endif
  617. #ifdef MS_MSPRO_SW_CONTROL
  618. if(MS_WORK_MODE == CARD_SW_MODE)
  619. packet.param.in.count = 0x19; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  620. #endif
  621. packet.param.in.buffer = &ms_mspro_buf->ms.regs.INT_Reg;
  622. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  623. if(error)
  624. return error;
  625. #ifdef MS_MSPRO_HW_CONTROL
  626. if(MS_WORK_MODE == CARD_HW_MODE)
  627. {
  628. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  629. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  630. ms_mspro_buf->ms.reg_set.read_addr = 0x01+0x10;
  631. ms_mspro_buf->ms.reg_set.read_size = 0x19-0x10;
  632. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  633. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  634. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  635. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  636. if(error)
  637. return error;
  638. packet.TPC_cmd.value = TPC_MS_MSPRO_READ_REG; //READ_REG
  639. packet.param.in.count = 0x19-0x10; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  640. packet.param.in.buffer = &ms_mspro_buf->ms.regs.Block_Address_Reg2;
  641. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  642. if(error)
  643. return error;
  644. }
  645. #endif
  646. //get INT register
  647. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  648. {
  649. ms_mspro_buf->ms.regs.INT_Reg = packet.int_reg;
  650. }
  651. //check command status
  652. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->ms.regs.INT_Reg;
  653. if(pIntReg->CMDNK) //BLOCK_READ CMD can not be excuted
  654. return MS_MSPRO_ERROR_CMDNK;
  655. pStatusReg1 = (MS_Status_Register1_t*)&ms_mspro_buf->ms.regs.Status_Reg1;
  656. if(!(pIntReg->CED & pIntReg->BREQ))
  657. return MS_MSPRO_ERROR_FLASH_READ;
  658. if(pIntReg->ERR &&
  659. (pStatusReg1->UCDT | pStatusReg1->UCEX | pStatusReg1->UCFG))
  660. return MS_MSPRO_ERROR_FLASH_READ;
  661. packet.TPC_cmd.value = TPC_MS_READ_PAGE_DATA; //READ_PAGE_DATA
  662. packet.param.in.count = MS_PAGE_SIZE; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  663. packet.param.in.buffer = data_buf;
  664. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  665. if(error)
  666. return error;
  667. return MS_MSPRO_NO_ERROR;
  668. }
  669. #ifdef MS_WRITE_PATTERN_1
  670. //pattern 1
  671. int ms_write_page(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long block_addr, unsigned char page_addr, unsigned char * data_buf)
  672. {
  673. MS_MSPRO_TPC_Packet_t packet;
  674. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  675. int error;
  676. unsigned char* buf = ms_mspro_info->data_buf;
  677. #ifdef MS_MSPRO_HW_CONTROL
  678. if(MS_WORK_MODE == CARD_HW_MODE)
  679. {
  680. if((ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  681. (ms_mspro_buf->ms.reg_set.write_size != 0x06))
  682. {
  683. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  684. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  685. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  686. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  687. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  688. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  689. if(error)
  690. return error;
  691. }
  692. }
  693. #endif
  694. #ifdef MS_MSPRO_SW_CONTROL
  695. if(MS_WORK_MODE == CARD_SW_MODE)
  696. {
  697. if((ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  698. (ms_mspro_buf->ms.reg_set.write_size != 0x0A))
  699. {
  700. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  701. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  702. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  703. ms_mspro_buf->ms.reg_set.write_size = 0x0A;
  704. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  705. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  706. if(error)
  707. return error;
  708. }
  709. }
  710. #endif
  711. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  712. #ifdef MS_MSPRO_HW_CONTROL
  713. if(MS_WORK_MODE == CARD_HW_MODE)
  714. packet.param.out.count = 0x06; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  715. #endif
  716. #ifdef MS_MSPRO_SW_CONTROL
  717. if(MS_WORK_MODE == CARD_SW_MODE)
  718. packet.param.out.count = 0x0A; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  719. #endif
  720. ms_mspro_buf->ms.regs.System_Parameter_Reg = (ms_mspro_info->interface_mode==INTERFACE_PARALLEL) ? 0x88 : 0x80;
  721. ms_mspro_buf->ms.regs.Block_Address_Reg2 = (block_addr >> 16) & 0xFF;
  722. ms_mspro_buf->ms.regs.Block_Address_Reg1 = (block_addr >> 8) & 0xFF;
  723. ms_mspro_buf->ms.regs.Block_Address_Reg0 = block_addr & 0xFF;
  724. ms_mspro_buf->ms.regs.CMD_Parameter_Reg = 0x20;
  725. ms_mspro_buf->ms.regs.Page_Address_Reg = page_addr;
  726. packet.param.out.buffer = &ms_mspro_buf->ms.regs.System_Parameter_Reg;
  727. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  728. if(error)
  729. return error;
  730. #ifdef MS_MSPRO_HW_CONTROL
  731. if(MS_WORK_MODE == CARD_HW_MODE)
  732. {
  733. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  734. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  735. ms_mspro_buf->ms.reg_set.write_addr = 0x10+0x06;
  736. ms_mspro_buf->ms.reg_set.write_size = 0x0A-0x06;
  737. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  738. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  739. if(error)
  740. return error;
  741. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  742. packet.param.out.count = 0x0A-0x06; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  743. packet.param.out.buffer = &ms_mspro_buf->ms.regs.Overwrite_Flag_Reg;
  744. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  745. if(error)
  746. return error;
  747. }
  748. #endif
  749. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  750. packet.param.out.count = 1;
  751. packet.param.out.buffer = buf;
  752. packet.param.out.buffer[0] = CMD_MS_BLOCK_WRITE;
  753. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  754. if(error)
  755. return error;
  756. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  757. if(error)
  758. return error;
  759. //get INT register
  760. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  761. {
  762. ms_mspro_buf->ms.regs.INT_Reg = packet.int_reg;
  763. }
  764. else
  765. {
  766. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  767. packet.param.in.count = 1;
  768. packet.param.in.buffer = &ms_mspro_buf->ms.regs.INT_Reg;
  769. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  770. if(error)
  771. return error;
  772. }
  773. //check command status
  774. MS_MSPRO_INT_Register_t * pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->ms.regs.INT_Reg;
  775. if(pIntReg->CMDNK) //BLOCK_READ CMD can not be excuted
  776. return MS_MSPRO_ERROR_CMDNK;
  777. if(!pIntReg->BREQ) //BLOCK_READ CMD can not be excuted
  778. return MS_MSPRO_ERROR_FLASH_WRITE;
  779. packet.TPC_cmd.value = TPC_MS_WRITE_PAGE_DATA; //WRITE_PAGE_DATA
  780. packet.param.out.count = MS_PAGE_SIZE; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  781. packet.param.out.buffer = data_buf;
  782. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  783. if(error)
  784. return error;
  785. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  786. if(error)
  787. return error;
  788. //get INT register
  789. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  790. {
  791. ms_mspro_buf->ms.regs.INT_Reg = packet.int_reg;
  792. }
  793. else
  794. {
  795. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  796. packet.param.in.count = 1;
  797. packet.param.in.buffer = &ms_mspro_buf->ms.regs.INT_Reg;
  798. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  799. if(error)
  800. return error;
  801. }
  802. //check command status
  803. if(pIntReg->CED && pIntReg->ERR)
  804. return MS_MSPRO_ERROR_FLASH_WRITE;
  805. return MS_MSPRO_NO_ERROR;
  806. }
  807. #endif //MS_WRITE_PATTERN_1
  808. #ifdef MS_WRITE_PATTERN_2
  809. //pattern 2
  810. int ms_write_page(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long block_addr, unsigned char page_addr, unsigned char * data_buf)
  811. {
  812. MS_MSPRO_TPC_Packet_t packet;
  813. MS_MSPRO_INT_Register_t * pIntReg;
  814. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  815. int error;
  816. unsigned char buf[4] = {0,0,0,0};
  817. #ifdef MS_MSPRO_HW_CONTROL
  818. if(MS_WORK_MODE == CARD_HW_MODE)
  819. {
  820. if((ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  821. (ms_mspro_buf->ms.reg_set.write_size != 0x06))
  822. {
  823. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  824. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  825. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  826. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  827. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  828. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  829. if(error)
  830. return error;
  831. }
  832. }
  833. #endif
  834. #ifdef MS_MSPRO_SW_CONTROL
  835. if(MS_WORK_MODE == CARD_SW_MODE)
  836. {
  837. if((ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  838. (ms_mspro_buf->ms.reg_set.write_size != 0x0A))
  839. {
  840. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  841. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  842. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  843. ms_mspro_buf->ms.reg_set.write_size = 0x0A;
  844. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  845. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  846. if(error)
  847. return error;
  848. }
  849. }
  850. #endif
  851. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  852. #ifdef MS_MSPRO_HW_CONTROL
  853. if(MS_WORK_MODE == CARD_HW_MODE)
  854. packet.param.out.count = 0x06; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  855. #endif
  856. #ifdef MS_MSPRO_SW_CONTROL
  857. if(MS_WORK_MODE == CARD_SW_MODE)
  858. packet.param.out.count = 0x0A; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  859. #endif
  860. ms_mspro_buf->ms.regs.System_Parameter_Reg = (ms_mspro_info->interface_mode==INTERFACE_PARALLEL) ? 0x88 : 0x80;
  861. ms_mspro_buf->ms.regs.Block_Address_Reg2 = (block_addr >> 16) & 0xFF;
  862. ms_mspro_buf->ms.regs.Block_Address_Reg1 = (block_addr >> 8) & 0xFF;
  863. ms_mspro_buf->ms.regs.Block_Address_Reg0 = block_addr & 0xFF;
  864. ms_mspro_buf->ms.regs.CMD_Parameter_Reg = 0x20;
  865. ms_mspro_buf->ms.regs.Page_Address_Reg = page_addr;
  866. packet.param.out.buffer = &ms_mspro_buf->ms.regs.System_Parameter_Reg;
  867. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  868. if(error)
  869. return error;
  870. #ifdef MS_MSPRO_HW_CONTROL
  871. if(MS_WORK_MODE == CARD_HW_MODE)
  872. {
  873. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  874. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  875. ms_mspro_buf->ms.reg_set.write_addr = 0x10+0x06;
  876. ms_mspro_buf->ms.reg_set.write_size = 0x0A-0x06;
  877. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  878. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  879. if(error)
  880. return error;
  881. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  882. packet.param.out.count = 0x0A-0x06; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  883. packet.param.out.buffer = &ms_mspro_buf->ms.regs.Overwrite_Flag_Reg;
  884. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  885. if(error)
  886. return error;
  887. }
  888. #endif
  889. packet.TPC_cmd.value = TPC_MS_WRITE_PAGE_DATA; //WRITE_PAGE_DATA
  890. packet.param.out.count = MS_PAGE_SIZE; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  891. packet.param.out.buffer = data_buf;
  892. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  893. if(error)
  894. return error;
  895. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  896. packet.param.out.count = 1;
  897. packet.param.out.buffer = buf;
  898. packet.param.out.buffer[0] = CMD_MS_BLOCK_WRITE;
  899. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  900. if(error)
  901. return error;
  902. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  903. if(error)
  904. return error;
  905. //get INT register
  906. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  907. {
  908. ms_mspro_buf->ms.regs.INT_Reg = packet.int_reg;
  909. }
  910. else
  911. {
  912. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  913. packet.param.in.count = 1;
  914. packet.param.in.buffer = &ms_mspro_buf->ms.regs.INT_Reg;
  915. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  916. if(error)
  917. return error;
  918. }
  919. //check command status
  920. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->ms.regs.INT_Reg;
  921. if(pIntReg->CMDNK) //BLOCK_READ CMD can not be excuted
  922. return MS_MSPRO_ERROR_CMDNK;
  923. if(pIntReg->CED && pIntReg->ERR)
  924. return MS_MSPRO_ERROR_FLASH_WRITE;
  925. return MS_MSPRO_NO_ERROR;
  926. }
  927. #endif //MS_WRITE_PATTERN_2
  928. int ms_copy_page(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long source_block_addr, unsigned char source_page_addr, unsigned long dest_block_addr, unsigned char dest_page_addr, unsigned char * data_buf)
  929. {
  930. MS_MSPRO_TPC_Packet_t packet;
  931. MS_MSPRO_INT_Register_t * pIntReg;
  932. MS_Status_Register1_t * pStatusReg1;
  933. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  934. int error;
  935. unsigned char* buf = ms_mspro_info->data_buf;
  936. #ifdef MS_MSPRO_HW_CONTROL
  937. if(MS_WORK_MODE == CARD_HW_MODE)
  938. {
  939. if((ms_mspro_buf->ms.reg_set.read_addr != 0x01) ||
  940. (ms_mspro_buf->ms.reg_set.read_size != 0x10) ||
  941. (ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  942. (ms_mspro_buf->ms.reg_set.write_size != 0x06))
  943. {
  944. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  945. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  946. ms_mspro_buf->ms.reg_set.read_addr = 0x01;
  947. ms_mspro_buf->ms.reg_set.read_size = 0x10;
  948. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  949. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  950. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  951. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  952. if(error)
  953. return error;
  954. }
  955. }
  956. #endif
  957. #ifdef MS_MSPRO_SW_CONTROL
  958. if(MS_WORK_MODE == CARD_SW_MODE)
  959. {
  960. if((ms_mspro_buf->ms.reg_set.read_addr != 0x01) ||
  961. (ms_mspro_buf->ms.reg_set.read_size != 0x19) ||
  962. (ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  963. (ms_mspro_buf->ms.reg_set.write_size != 0x06))
  964. {
  965. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  966. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  967. ms_mspro_buf->ms.reg_set.read_addr = 0x01;
  968. ms_mspro_buf->ms.reg_set.read_size = 0x19;
  969. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  970. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  971. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  972. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  973. if(error)
  974. return error;
  975. }
  976. }
  977. #endif
  978. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  979. packet.param.out.count = 6; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  980. ms_mspro_buf->ms.regs.System_Parameter_Reg = (ms_mspro_info->interface_mode==INTERFACE_PARALLEL) ? 0x88 : 0x80;
  981. ms_mspro_buf->ms.regs.Block_Address_Reg2 = (source_block_addr >> 16) & 0xFF;
  982. ms_mspro_buf->ms.regs.Block_Address_Reg1 = (source_block_addr >> 8) & 0xFF;
  983. ms_mspro_buf->ms.regs.Block_Address_Reg0 = source_block_addr & 0xFF;
  984. ms_mspro_buf->ms.regs.CMD_Parameter_Reg = 0x20;
  985. ms_mspro_buf->ms.regs.Page_Address_Reg = source_page_addr;
  986. packet.param.out.buffer = &ms_mspro_buf->ms.regs.System_Parameter_Reg;
  987. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  988. if(error)
  989. return error;
  990. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  991. packet.param.out.count = 1;
  992. packet.param.out.buffer = buf;
  993. packet.param.out.buffer[0] = CMD_MS_BLOCK_READ;
  994. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  995. if(error)
  996. return error;
  997. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  998. if(error)
  999. return error;
  1000. packet.TPC_cmd.value = TPC_MS_MSPRO_READ_REG; //READ_REG
  1001. #ifdef MS_MSPRO_HW_CONTROL
  1002. if(MS_WORK_MODE == CARD_HW_MODE)
  1003. packet.param.in.count = 0x10; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1004. #endif
  1005. #ifdef MS_MSPRO_SW_CONTROL
  1006. if(MS_WORK_MODE == CARD_SW_MODE)
  1007. packet.param.in.count = 0x19; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1008. #endif
  1009. packet.param.in.buffer = &ms_mspro_buf->ms.regs.INT_Reg;
  1010. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1011. if(error)
  1012. return error;
  1013. #ifdef MS_MSPRO_HW_CONTROL
  1014. if(MS_WORK_MODE == CARD_HW_MODE)
  1015. {
  1016. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1017. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1018. ms_mspro_buf->ms.reg_set.read_addr = 0x01+0x10;
  1019. ms_mspro_buf->ms.reg_set.read_size = 0x19-0x10;
  1020. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  1021. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  1022. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1023. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1024. if(error)
  1025. return error;
  1026. packet.TPC_cmd.value = TPC_MS_MSPRO_READ_REG; //READ_REG
  1027. packet.param.in.count = 0x19-0x10; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1028. packet.param.in.buffer = &ms_mspro_buf->ms.regs.Block_Address_Reg2;
  1029. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1030. if(error)
  1031. return error;
  1032. }
  1033. #endif
  1034. //get INT register
  1035. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  1036. {
  1037. ms_mspro_buf->ms.regs.INT_Reg = packet.int_reg;
  1038. }
  1039. //check command status
  1040. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->ms.regs.INT_Reg;
  1041. if(pIntReg->CMDNK) //BLOCK_READ CMD can not be excuted
  1042. return MS_MSPRO_ERROR_CMDNK;
  1043. if(!(pIntReg->CED & pIntReg->BREQ))
  1044. return MS_MSPRO_ERROR_FLASH_READ;
  1045. if(pIntReg->ERR)
  1046. {
  1047. packet.TPC_cmd.value = TPC_MS_READ_PAGE_DATA; //READ_PAGE_DATA
  1048. packet.param.in.count = MS_PAGE_SIZE; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1049. packet.param.in.buffer = data_buf;
  1050. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1051. if(error)
  1052. return error;
  1053. pStatusReg1 = (MS_Status_Register1_t*)&ms_mspro_buf->ms.regs.Status_Reg1;
  1054. if(pStatusReg1->UCDT | pStatusReg1->UCEX | pStatusReg1->UCFG)
  1055. return MS_MSPRO_ERROR_FLASH_READ;
  1056. packet.TPC_cmd.value = TPC_MS_WRITE_PAGE_DATA; //WRITE_PAGE_DATA
  1057. packet.param.out.count = MS_PAGE_SIZE; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1058. packet.param.out.buffer = data_buf;
  1059. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1060. if(error)
  1061. return error;
  1062. }
  1063. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1064. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1065. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  1066. #ifdef MS_MSPRO_HW_CONTROL
  1067. if(MS_WORK_MODE == CARD_HW_MODE)
  1068. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  1069. #endif
  1070. #ifdef MS_MSPRO_SW_CONTROL
  1071. if(MS_WORK_MODE == CARD_SW_MODE)
  1072. ms_mspro_buf->ms.reg_set.write_size = 0x0A;
  1073. #endif
  1074. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1075. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1076. if(error)
  1077. return error;
  1078. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  1079. #ifdef MS_MSPRO_HW_CONTROL
  1080. if(MS_WORK_MODE == CARD_HW_MODE)
  1081. packet.param.out.count = 0x06; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1082. #endif
  1083. #ifdef MS_MSPRO_SW_CONTROL
  1084. if(MS_WORK_MODE == CARD_SW_MODE)
  1085. packet.param.out.count = 0x0A; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1086. #endif
  1087. ms_mspro_buf->ms.regs.System_Parameter_Reg = (ms_mspro_info->interface_mode==INTERFACE_PARALLEL) ? 0x88 : 0x80;
  1088. ms_mspro_buf->ms.regs.Block_Address_Reg2 = (dest_block_addr >> 16) & 0xFF;
  1089. ms_mspro_buf->ms.regs.Block_Address_Reg1 = (dest_block_addr >> 8) & 0xFF;
  1090. ms_mspro_buf->ms.regs.Block_Address_Reg0 = dest_block_addr & 0xFF;
  1091. ms_mspro_buf->ms.regs.CMD_Parameter_Reg = 0x20;
  1092. ms_mspro_buf->ms.regs.Page_Address_Reg = dest_page_addr;
  1093. packet.param.out.buffer = &ms_mspro_buf->ms.regs.System_Parameter_Reg;
  1094. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1095. if(error)
  1096. return error;
  1097. #ifdef MS_MSPRO_HW_CONTROL
  1098. if(MS_WORK_MODE == CARD_HW_MODE)
  1099. {
  1100. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1101. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1102. ms_mspro_buf->ms.reg_set.write_addr = 0x10+0x06;
  1103. ms_mspro_buf->ms.reg_set.write_size = 0x0A-0x06;
  1104. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1105. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1106. if(error)
  1107. return error;
  1108. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  1109. packet.param.out.count = 0x0A-0x06; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1110. packet.param.out.buffer = &ms_mspro_buf->ms.regs.Overwrite_Flag_Reg;
  1111. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1112. if(error)
  1113. return error;
  1114. }
  1115. #endif
  1116. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  1117. packet.param.out.count = 1;
  1118. packet.param.out.buffer = buf;
  1119. packet.param.out.buffer[0] = CMD_MS_BLOCK_WRITE;
  1120. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1121. if(error)
  1122. return error;
  1123. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  1124. if(error)
  1125. return error;
  1126. //get INT register
  1127. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  1128. {
  1129. ms_mspro_buf->ms.regs.INT_Reg = packet.int_reg;
  1130. }
  1131. else
  1132. {
  1133. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  1134. packet.param.in.count = 1;
  1135. packet.param.in.buffer = &ms_mspro_buf->ms.regs.INT_Reg;
  1136. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1137. if(error)
  1138. return error;
  1139. }
  1140. //check command status
  1141. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->ms.regs.INT_Reg;
  1142. if(pIntReg->CMDNK) //BLOCK_READ CMD can not be excuted
  1143. return MS_MSPRO_ERROR_CMDNK;
  1144. if(pIntReg->CED && pIntReg->ERR)
  1145. return MS_MSPRO_ERROR_FLASH_WRITE;
  1146. return MS_MSPRO_NO_ERROR;
  1147. }
  1148. int ms_read_block(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long block_addr, unsigned char page_addr, unsigned short page_nums, unsigned char * data_buf)
  1149. {
  1150. MS_MSPRO_TPC_Packet_t packet;
  1151. MS_MSPRO_INT_Register_t * pIntReg;
  1152. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  1153. unsigned long data_offset = 0;
  1154. int error;
  1155. unsigned char* buf = ms_mspro_info->data_buf;
  1156. if(page_nums == 0)
  1157. return MS_MSPRO_ERROR_PARAMETER;
  1158. if((ms_mspro_buf->ms.reg_set.read_addr != 0x16) ||
  1159. (ms_mspro_buf->ms.reg_set.read_size != 0x08) ||
  1160. (ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  1161. (ms_mspro_buf->ms.reg_set.write_size != 0x06))
  1162. {
  1163. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1164. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1165. ms_mspro_buf->ms.reg_set.read_addr = 0x16;
  1166. ms_mspro_buf->ms.reg_set.read_size = 0x08;
  1167. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  1168. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  1169. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1170. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1171. if(error)
  1172. return error;
  1173. }
  1174. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  1175. packet.param.out.count = 6; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1176. ms_mspro_buf->ms.regs.System_Parameter_Reg = (ms_mspro_info->interface_mode==INTERFACE_PARALLEL) ? 0x88 : 0x80;
  1177. ms_mspro_buf->ms.regs.Block_Address_Reg2 = (block_addr >> 16) & 0xFF;
  1178. ms_mspro_buf->ms.regs.Block_Address_Reg1 = (block_addr >> 8) & 0xFF;
  1179. ms_mspro_buf->ms.regs.Block_Address_Reg0 = block_addr & 0xFF;
  1180. ms_mspro_buf->ms.regs.CMD_Parameter_Reg = 0x00;
  1181. ms_mspro_buf->ms.regs.Page_Address_Reg = page_addr;
  1182. packet.param.out.buffer = &ms_mspro_buf->ms.regs.System_Parameter_Reg;
  1183. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1184. if(error)
  1185. return error;
  1186. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  1187. packet.param.out.count = 1;
  1188. packet.param.out.buffer = buf;
  1189. packet.param.out.buffer[0] = CMD_MS_BLOCK_READ;
  1190. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1191. if(error)
  1192. return error;
  1193. while(1)
  1194. {
  1195. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  1196. if(error)
  1197. return error;
  1198. //get INT register
  1199. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  1200. {
  1201. ms_mspro_buf->ms.regs.INT_Reg = packet.int_reg;
  1202. }
  1203. else
  1204. {
  1205. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  1206. packet.param.in.count = 1;
  1207. packet.param.in.buffer = &ms_mspro_buf->ms.regs.INT_Reg;
  1208. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1209. if(error)
  1210. return error;
  1211. }
  1212. //check command status
  1213. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->ms.regs.INT_Reg;
  1214. if(pIntReg->CMDNK)
  1215. { //BLOCK_READ CMD can not be excuted
  1216. return MS_MSPRO_ERROR_CMDNK;
  1217. }
  1218. if(pIntReg->CED & pIntReg->ERR & pIntReg->BREQ)
  1219. {
  1220. error = MS_MSPRO_ERROR_FLASH_READ;
  1221. break;
  1222. }
  1223. else if(pIntReg->CED & pIntReg->BREQ)
  1224. {
  1225. error = MS_MSPRO_NO_ERROR;
  1226. break;
  1227. }
  1228. page_nums--;
  1229. if(page_nums > 0)
  1230. {
  1231. packet.TPC_cmd.value = TPC_MS_MSPRO_READ_REG; //READ_REG
  1232. packet.param.in.count = 0x08; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1233. packet.param.in.buffer = &ms_mspro_buf->ms.regs.Overwrite_Flag_Reg;
  1234. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1235. if(error)
  1236. return error;
  1237. packet.TPC_cmd.value = TPC_MS_READ_PAGE_DATA; //READ_PAGE_DATA
  1238. packet.param.in.count = MS_PAGE_SIZE; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1239. packet.param.in.buffer = data_buf+data_offset;
  1240. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1241. if(error)
  1242. return error;
  1243. #ifdef AMLOGIC_CHIP_SUPPORT
  1244. data_offset += ((unsigned long)data_buf == 0x3400000) ? 0 : MS_PAGE_SIZE;
  1245. #else
  1246. data_offset += MS_PAGE_SIZE;
  1247. #endif
  1248. }
  1249. else //page_nums == 0
  1250. {
  1251. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  1252. packet.param.out.count = 1;
  1253. packet.param.out.buffer = buf;
  1254. packet.param.out.buffer[0] = CMD_MS_BLOCK_END;
  1255. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1256. if(error)
  1257. return error;
  1258. }
  1259. }
  1260. if(error == MS_MSPRO_NO_ERROR)
  1261. {
  1262. packet.TPC_cmd.value = TPC_MS_MSPRO_READ_REG; //READ_REG
  1263. packet.param.in.count = 0x08; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1264. packet.param.in.buffer = &ms_mspro_buf->ms.regs.Overwrite_Flag_Reg;
  1265. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1266. if(error)
  1267. return error;
  1268. packet.TPC_cmd.value = TPC_MS_READ_PAGE_DATA; //READ_PAGE_DATA
  1269. packet.param.in.count = MS_PAGE_SIZE; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1270. packet.param.in.buffer = data_buf+data_offset;
  1271. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1272. if(error)
  1273. return error;
  1274. }
  1275. return MS_MSPRO_NO_ERROR;
  1276. }
  1277. int ms_write_block(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long block_addr, unsigned char page_addr, unsigned short page_nums, unsigned char * data_buf)
  1278. {
  1279. MS_MSPRO_TPC_Packet_t packet;
  1280. MS_MSPRO_INT_Register_t * pIntReg;
  1281. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  1282. unsigned long data_offset = 0;
  1283. int error;
  1284. unsigned char* buf = ms_mspro_info->data_buf;
  1285. if(page_nums == 0)
  1286. return MS_MSPRO_ERROR_PARAMETER;
  1287. #ifdef MS_MSPRO_HW_CONTROL
  1288. if(MS_WORK_MODE == CARD_HW_MODE)
  1289. {
  1290. if((ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  1291. (ms_mspro_buf->ms.reg_set.write_size != 0x06))
  1292. {
  1293. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1294. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1295. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  1296. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  1297. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1298. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1299. if(error)
  1300. return error;
  1301. }
  1302. }
  1303. #endif
  1304. #ifdef MS_MSPRO_SW_CONTROL
  1305. if(MS_WORK_MODE == CARD_SW_MODE)
  1306. {
  1307. if((ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  1308. (ms_mspro_buf->ms.reg_set.write_size != 0x0A))
  1309. {
  1310. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1311. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1312. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  1313. ms_mspro_buf->ms.reg_set.write_size = 0x0A;
  1314. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1315. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1316. if(error)
  1317. return error;
  1318. }
  1319. }
  1320. #endif
  1321. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  1322. #ifdef MS_MSPRO_HW_CONTROL
  1323. if(MS_WORK_MODE == CARD_HW_MODE)
  1324. packet.param.out.count = 0x06; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1325. #endif
  1326. #ifdef MS_MSPRO_SW_CONTROL
  1327. if(MS_WORK_MODE == CARD_SW_MODE)
  1328. packet.param.out.count = 0x0A; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1329. #endif
  1330. ms_mspro_buf->ms.regs.System_Parameter_Reg = (ms_mspro_info->interface_mode==INTERFACE_PARALLEL) ? 0x88 : 0x80;
  1331. ms_mspro_buf->ms.regs.Block_Address_Reg2 = (block_addr >> 16) & 0xFF;
  1332. ms_mspro_buf->ms.regs.Block_Address_Reg1 = (block_addr >> 8) & 0xFF;
  1333. ms_mspro_buf->ms.regs.Block_Address_Reg0 = block_addr & 0xFF;
  1334. ms_mspro_buf->ms.regs.CMD_Parameter_Reg = 0x00;
  1335. ms_mspro_buf->ms.regs.Page_Address_Reg = page_addr;
  1336. packet.param.out.buffer = &ms_mspro_buf->ms.regs.System_Parameter_Reg;
  1337. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1338. if(error)
  1339. return error;
  1340. #ifdef MS_MSPRO_HW_CONTROL
  1341. if(MS_WORK_MODE == CARD_HW_MODE)
  1342. {
  1343. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1344. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1345. ms_mspro_buf->ms.reg_set.write_addr = 0x10+0x06;
  1346. ms_mspro_buf->ms.reg_set.write_size = 0x0A-0x06;
  1347. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1348. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1349. if(error)
  1350. return error;
  1351. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  1352. packet.param.out.count = 0x0A-0x06; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1353. packet.param.out.buffer = &ms_mspro_buf->ms.regs.Overwrite_Flag_Reg;
  1354. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1355. if(error)
  1356. return error;
  1357. }
  1358. #endif
  1359. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  1360. packet.param.out.count = 1;
  1361. packet.param.out.buffer = buf;
  1362. packet.param.out.buffer[0] = CMD_MS_BLOCK_WRITE;
  1363. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1364. if(error)
  1365. return error;
  1366. while(1)
  1367. {
  1368. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  1369. if(error)
  1370. return error;
  1371. //get INT register
  1372. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  1373. {
  1374. ms_mspro_buf->ms.regs.INT_Reg = packet.int_reg;
  1375. }
  1376. else
  1377. {
  1378. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  1379. packet.param.in.count = 1;
  1380. packet.param.in.buffer = &ms_mspro_buf->ms.regs.INT_Reg;
  1381. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1382. if(error)
  1383. return error;
  1384. }
  1385. //check command status
  1386. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->ms.regs.INT_Reg;
  1387. if(pIntReg->CMDNK) //BLOCK_READ CMD can not be excuted
  1388. return MS_MSPRO_ERROR_CMDNK;
  1389. if(pIntReg->CED & pIntReg->ERR)
  1390. return MS_MSPRO_ERROR_FLASH_WRITE;
  1391. else if(pIntReg->CED)
  1392. break;
  1393. if(page_nums > 0)
  1394. {
  1395. page_nums--;
  1396. packet.TPC_cmd.value = TPC_MS_WRITE_PAGE_DATA; //WRITE_PAGE_DATA
  1397. packet.param.out.count = MS_PAGE_SIZE; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1398. packet.param.out.buffer = data_buf+data_offset;
  1399. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1400. if(error)
  1401. return error;
  1402. data_offset += MS_PAGE_SIZE;
  1403. }
  1404. else //page_nums == 0
  1405. {
  1406. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  1407. packet.param.out.count = 1;
  1408. packet.param.out.buffer = buf;
  1409. packet.param.out.buffer[0] = CMD_MS_BLOCK_END;
  1410. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1411. if(error)
  1412. return error;
  1413. }
  1414. }
  1415. return MS_MSPRO_NO_ERROR;
  1416. }
  1417. int ms_erase_block(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long block_addr)
  1418. {
  1419. MS_MSPRO_TPC_Packet_t packet;
  1420. MS_MSPRO_INT_Register_t * pIntReg;
  1421. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  1422. int error;
  1423. unsigned char* buf = ms_mspro_info->data_buf;
  1424. if((ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  1425. (ms_mspro_buf->ms.reg_set.write_size != 0x04))
  1426. {
  1427. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1428. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1429. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  1430. ms_mspro_buf->ms.reg_set.write_size = 0x04;
  1431. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1432. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1433. if(error)
  1434. return error;
  1435. }
  1436. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  1437. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1438. ms_mspro_buf->ms.regs.System_Parameter_Reg = (ms_mspro_info->interface_mode==INTERFACE_PARALLEL) ? 0x88 : 0x80;
  1439. ms_mspro_buf->ms.regs.Block_Address_Reg2 = (block_addr >> 16) & 0xFF;
  1440. ms_mspro_buf->ms.regs.Block_Address_Reg1 = (block_addr >> 8) & 0xFF;
  1441. ms_mspro_buf->ms.regs.Block_Address_Reg0 = block_addr & 0xFF;
  1442. packet.param.out.buffer = &ms_mspro_buf->ms.regs.System_Parameter_Reg;
  1443. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1444. if(error)
  1445. return error;
  1446. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  1447. packet.param.out.count = 1;
  1448. packet.param.out.buffer = buf;
  1449. packet.param.out.buffer[0] = CMD_MS_BLOCK_ERASE;
  1450. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1451. if(error)
  1452. return error;
  1453. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  1454. if(error)
  1455. return error;
  1456. //get INT register
  1457. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  1458. {
  1459. ms_mspro_buf->ms.regs.INT_Reg = packet.int_reg;
  1460. }
  1461. else
  1462. {
  1463. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  1464. packet.param.in.count = 1;
  1465. packet.param.in.buffer = &ms_mspro_buf->ms.regs.INT_Reg;
  1466. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1467. if(error)
  1468. return error;
  1469. }
  1470. //check command status
  1471. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->ms.regs.INT_Reg;
  1472. if(pIntReg->CMDNK) //BLOCK_READ CMD can not be excuted
  1473. return MS_MSPRO_ERROR_CMDNK;
  1474. if(pIntReg->CED & pIntReg->ERR)
  1475. return MS_MSPRO_ERROR_FLASH_ERASE;
  1476. else if(!pIntReg->CED)
  1477. return MS_MSPRO_ERROR_CED;
  1478. return MS_MSPRO_NO_ERROR;
  1479. }
  1480. int ms_read_extra_data(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long block_addr, unsigned char page_addr)
  1481. {
  1482. MS_MSPRO_TPC_Packet_t packet;
  1483. MS_MSPRO_INT_Register_t * pIntReg;
  1484. MS_Status_Register1_t * pStatusReg1;
  1485. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  1486. int error;
  1487. unsigned char* buf = ms_mspro_info->data_buf;
  1488. #ifdef MS_MSPRO_HW_CONTROL
  1489. if(MS_WORK_MODE == CARD_HW_MODE)
  1490. {
  1491. if((ms_mspro_buf->ms.reg_set.read_addr != 0x01) ||
  1492. (ms_mspro_buf->ms.reg_set.read_size != 0x10) ||
  1493. (ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  1494. (ms_mspro_buf->ms.reg_set.write_size != 0x06))
  1495. {
  1496. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1497. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1498. ms_mspro_buf->ms.reg_set.read_addr = 0x01;
  1499. ms_mspro_buf->ms.reg_set.read_size = 0x10;
  1500. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  1501. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  1502. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1503. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1504. if(error)
  1505. return error;
  1506. }
  1507. }
  1508. #endif
  1509. #ifdef MS_MSPRO_SW_CONTROL
  1510. if(MS_WORK_MODE == CARD_SW_MODE)
  1511. {
  1512. if((ms_mspro_buf->ms.reg_set.read_addr != 0x01) ||
  1513. (ms_mspro_buf->ms.reg_set.read_size != 0x19) ||
  1514. (ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  1515. (ms_mspro_buf->ms.reg_set.write_size != 0x06))
  1516. {
  1517. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1518. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1519. ms_mspro_buf->ms.reg_set.read_addr = 0x01;
  1520. ms_mspro_buf->ms.reg_set.read_size = 0x19;
  1521. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  1522. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  1523. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1524. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1525. if(error)
  1526. return error;
  1527. }
  1528. }
  1529. #endif
  1530. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  1531. packet.param.out.count = 6; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1532. ms_mspro_buf->ms.regs.System_Parameter_Reg = (ms_mspro_info->interface_mode==INTERFACE_PARALLEL) ? 0x88 : 0x80;
  1533. ms_mspro_buf->ms.regs.Block_Address_Reg2 = (block_addr >> 16) & 0xFF;
  1534. ms_mspro_buf->ms.regs.Block_Address_Reg1 = (block_addr >> 8) & 0xFF;
  1535. ms_mspro_buf->ms.regs.Block_Address_Reg0 = block_addr & 0xFF;
  1536. ms_mspro_buf->ms.regs.CMD_Parameter_Reg = 0x40;
  1537. ms_mspro_buf->ms.regs.Page_Address_Reg = page_addr;
  1538. packet.param.out.buffer = &ms_mspro_buf->ms.regs.System_Parameter_Reg;
  1539. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1540. if(error)
  1541. return error;
  1542. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  1543. packet.param.out.count = 1;
  1544. packet.param.out.buffer = buf;
  1545. packet.param.out.buffer[0] = CMD_MS_BLOCK_READ;
  1546. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1547. if(error)
  1548. return error;
  1549. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  1550. if(error)
  1551. return error;
  1552. packet.TPC_cmd.value = TPC_MS_MSPRO_READ_REG; //READ_REG
  1553. #ifdef MS_MSPRO_HW_CONTROL
  1554. if(MS_WORK_MODE == CARD_HW_MODE)
  1555. packet.param.in.count = 0x10; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1556. #endif
  1557. #ifdef MS_MSPRO_SW_CONTROL
  1558. if(MS_WORK_MODE == CARD_SW_MODE)
  1559. packet.param.in.count = 0x19; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1560. #endif
  1561. packet.param.in.buffer = &ms_mspro_buf->ms.regs.INT_Reg;
  1562. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1563. if(error)
  1564. return error;
  1565. #ifdef MS_MSPRO_HW_CONTROL
  1566. if(MS_WORK_MODE == CARD_HW_MODE)
  1567. {
  1568. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1569. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1570. ms_mspro_buf->ms.reg_set.read_addr = 0x01+0x10;
  1571. ms_mspro_buf->ms.reg_set.read_size = 0x19-0x10;
  1572. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  1573. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  1574. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1575. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1576. if(error)
  1577. return error;
  1578. packet.TPC_cmd.value = TPC_MS_MSPRO_READ_REG; //READ_REG
  1579. packet.param.in.count = 0x19-0x10; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1580. packet.param.in.buffer = &ms_mspro_buf->ms.regs.Block_Address_Reg2;
  1581. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1582. if(error)
  1583. return error;
  1584. }
  1585. #endif
  1586. //get INT register
  1587. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  1588. {
  1589. ms_mspro_buf->ms.regs.INT_Reg = packet.int_reg;
  1590. }
  1591. //check command status
  1592. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->ms.regs.INT_Reg;
  1593. if(pIntReg->CMDNK) //BLOCK_READ CMD can not be excuted
  1594. return MS_MSPRO_ERROR_CMDNK;
  1595. pStatusReg1 = (MS_Status_Register1_t*)&ms_mspro_buf->ms.regs.Status_Reg1;
  1596. if((pIntReg->CED & pIntReg->ERR) &&
  1597. (pStatusReg1->UCEX | pStatusReg1->UCFG))
  1598. return MS_MSPRO_ERROR_FLASH_READ;
  1599. return MS_MSPRO_NO_ERROR;
  1600. }
  1601. int ms_write_extra_data(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long block_addr, unsigned char page_addr)
  1602. {
  1603. MS_MSPRO_TPC_Packet_t packet;
  1604. MS_MSPRO_INT_Register_t * pIntReg;
  1605. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  1606. int error;
  1607. unsigned char* buf = ms_mspro_info->data_buf;
  1608. #ifdef MS_MSPRO_HW_CONTROL
  1609. if(MS_WORK_MODE == CARD_HW_MODE)
  1610. {
  1611. if((ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  1612. (ms_mspro_buf->ms.reg_set.write_size != 0x06))
  1613. {
  1614. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1615. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1616. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  1617. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1618. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1619. if(error)
  1620. return error;
  1621. }
  1622. }
  1623. #endif
  1624. #ifdef MS_MSPRO_SW_CONTROL
  1625. if(MS_WORK_MODE == CARD_SW_MODE)
  1626. {
  1627. if((ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  1628. (ms_mspro_buf->ms.reg_set.write_size != 0x0A))
  1629. {
  1630. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1631. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1632. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  1633. ms_mspro_buf->ms.reg_set.write_size = 0x0A;
  1634. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1635. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1636. if(error)
  1637. return error;
  1638. }
  1639. }
  1640. #endif
  1641. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  1642. #ifdef MS_MSPRO_HW_CONTROL
  1643. if(MS_WORK_MODE == CARD_HW_MODE)
  1644. packet.param.out.count = 0x06; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1645. #endif
  1646. #ifdef MS_MSPRO_SW_CONTROL
  1647. if(MS_WORK_MODE == CARD_SW_MODE)
  1648. packet.param.out.count = 0x0A; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1649. #endif
  1650. ms_mspro_buf->ms.regs.System_Parameter_Reg = (ms_mspro_info->interface_mode==INTERFACE_PARALLEL) ? 0x88 : 0x80;
  1651. ms_mspro_buf->ms.regs.Block_Address_Reg2 = (block_addr >> 16) & 0xFF;
  1652. ms_mspro_buf->ms.regs.Block_Address_Reg1 = (block_addr >> 8) & 0xFF;
  1653. ms_mspro_buf->ms.regs.Block_Address_Reg0 = block_addr & 0xFF;
  1654. ms_mspro_buf->ms.regs.CMD_Parameter_Reg = 0x40;
  1655. ms_mspro_buf->ms.regs.Page_Address_Reg = page_addr;
  1656. packet.param.out.buffer = &ms_mspro_buf->ms.regs.System_Parameter_Reg;
  1657. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1658. if(error)
  1659. return error;
  1660. #ifdef MS_MSPRO_HW_CONTROL
  1661. if(MS_WORK_MODE == CARD_HW_MODE)
  1662. {
  1663. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1664. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1665. ms_mspro_buf->ms.reg_set.write_addr = 0x10+0x06;
  1666. ms_mspro_buf->ms.reg_set.write_size = 0x0A-0x06;
  1667. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1668. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1669. if(error)
  1670. return error;
  1671. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  1672. packet.param.out.count = 0x0A-0x06; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1673. packet.param.out.buffer = &ms_mspro_buf->ms.regs.Overwrite_Flag_Reg;
  1674. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1675. if(error)
  1676. return error;
  1677. }
  1678. #endif
  1679. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  1680. packet.param.out.count = 1;
  1681. packet.param.out.buffer = buf;
  1682. packet.param.out.buffer[0] = CMD_MS_BLOCK_WRITE;
  1683. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1684. if(error)
  1685. return error;
  1686. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  1687. if(error)
  1688. return error;
  1689. //get INT register
  1690. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  1691. {
  1692. ms_mspro_buf->ms.regs.INT_Reg = packet.int_reg;
  1693. }
  1694. else
  1695. {
  1696. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  1697. packet.param.in.count = 1;
  1698. packet.param.in.buffer = &ms_mspro_buf->ms.regs.INT_Reg;
  1699. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1700. if(error)
  1701. return error;
  1702. }
  1703. //check command status
  1704. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->ms.regs.INT_Reg;
  1705. if(pIntReg->CMDNK) //BLOCK_READ CMD can not be excuted
  1706. return MS_MSPRO_ERROR_CMDNK;
  1707. if(pIntReg->CED && pIntReg->ERR)
  1708. return MS_MSPRO_ERROR_FLASH_WRITE;
  1709. else if(!pIntReg->CED)
  1710. return MS_MSPRO_ERROR_CED;
  1711. return MS_MSPRO_NO_ERROR;
  1712. }
  1713. int ms_overwrite_extra_data(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long block_addr, unsigned char page_addr, unsigned char mask_data)
  1714. {
  1715. MS_MSPRO_TPC_Packet_t packet;
  1716. MS_MSPRO_INT_Register_t * pIntReg;
  1717. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  1718. int error;
  1719. unsigned char* buf = ms_mspro_info->data_buf;
  1720. #ifdef MS_MSPRO_HW_CONTROL
  1721. if(MS_WORK_MODE == CARD_HW_MODE)
  1722. {
  1723. if((ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  1724. (ms_mspro_buf->ms.reg_set.write_size != 0x06))
  1725. {
  1726. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1727. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1728. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  1729. ms_mspro_buf->ms.reg_set.write_size = 0x06;
  1730. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1731. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1732. if(error)
  1733. return error;
  1734. }
  1735. }
  1736. #endif
  1737. #ifdef MS_MSPRO_SW_CONTROL
  1738. if(MS_WORK_MODE == CARD_SW_MODE)
  1739. {
  1740. if((ms_mspro_buf->ms.reg_set.write_addr != 0x10) ||
  1741. (ms_mspro_buf->ms.reg_set.write_size != 0x07))
  1742. {
  1743. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1744. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1745. ms_mspro_buf->ms.reg_set.write_addr = 0x10;
  1746. ms_mspro_buf->ms.reg_set.write_size = 0x07;
  1747. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1748. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1749. if(error)
  1750. return error;
  1751. }
  1752. }
  1753. #endif
  1754. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  1755. #ifdef MS_MSPRO_HW_CONTROL
  1756. if(MS_WORK_MODE == CARD_HW_MODE)
  1757. packet.param.out.count = 0x06; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1758. #endif
  1759. #ifdef MS_MSPRO_SW_CONTROL
  1760. if(MS_WORK_MODE == CARD_SW_MODE)
  1761. packet.param.out.count = 0x07; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1762. #endif
  1763. ms_mspro_buf->ms.regs.System_Parameter_Reg = (ms_mspro_info->interface_mode==INTERFACE_PARALLEL) ? 0x88 : 0x80;
  1764. ms_mspro_buf->ms.regs.Block_Address_Reg2 = (block_addr >> 16) & 0xFF;
  1765. ms_mspro_buf->ms.regs.Block_Address_Reg1 = (block_addr >> 8) & 0xFF;
  1766. ms_mspro_buf->ms.regs.Block_Address_Reg0 = block_addr & 0xFF;
  1767. ms_mspro_buf->ms.regs.CMD_Parameter_Reg = 0x80;
  1768. ms_mspro_buf->ms.regs.Page_Address_Reg = page_addr;
  1769. ms_mspro_buf->ms.regs.Overwrite_Flag_Reg = mask_data;
  1770. packet.param.out.buffer = &ms_mspro_buf->ms.regs.System_Parameter_Reg;
  1771. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1772. if(error)
  1773. return error;
  1774. #ifdef MS_MSPRO_HW_CONTROL
  1775. if(MS_WORK_MODE == CARD_HW_MODE)
  1776. {
  1777. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //WRITE_REG: Status, Type, Catagory, Class
  1778. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1779. ms_mspro_buf->ms.reg_set.write_addr = 0x10+0x06;
  1780. ms_mspro_buf->ms.reg_set.write_size = 0x07-0x06;
  1781. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  1782. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1783. if(error)
  1784. return error;
  1785. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  1786. packet.param.out.count = 0x07-0x06; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1787. packet.param.out.buffer = &ms_mspro_buf->ms.regs.Overwrite_Flag_Reg;
  1788. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1789. if(error)
  1790. return error;
  1791. }
  1792. #endif
  1793. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  1794. packet.param.out.count = 1;
  1795. packet.param.out.buffer = buf;
  1796. packet.param.out.buffer[0] = CMD_MS_BLOCK_WRITE;
  1797. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1798. if(error)
  1799. return error;
  1800. error = ms_mspro_wait_int(ms_mspro_info, &packet);
  1801. if(error)
  1802. return error;
  1803. //get INT register
  1804. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  1805. {
  1806. ms_mspro_buf->ms.regs.INT_Reg = packet.int_reg;
  1807. }
  1808. else
  1809. {
  1810. packet.TPC_cmd.value = TPC_MS_MSPRO_GET_INT; //SET_CMD
  1811. packet.param.in.count = 1;
  1812. packet.param.in.buffer = &ms_mspro_buf->ms.regs.INT_Reg;
  1813. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1814. if(error)
  1815. return error;
  1816. }
  1817. //check command status
  1818. pIntReg = (MS_MSPRO_INT_Register_t*)&ms_mspro_buf->ms.regs.INT_Reg;
  1819. if(pIntReg->CMDNK) //BLOCK_READ CMD can not be excuted
  1820. return MS_MSPRO_ERROR_CMDNK;
  1821. if(pIntReg->CED && pIntReg->ERR)
  1822. return MS_MSPRO_ERROR_FLASH_WRITE;
  1823. else if(!pIntReg->CED)
  1824. return MS_MSPRO_ERROR_CED;
  1825. return MS_MSPRO_NO_ERROR;
  1826. }
  1827. int ms_sleep(void)
  1828. {
  1829. return 0;
  1830. }
  1831. int ms_clear_buffer(void)
  1832. {
  1833. return 0;
  1834. }
  1835. int ms_flash_stop(void)
  1836. {
  1837. return 0;
  1838. }
  1839. int ms_reset(MS_MSPRO_Card_Info_t *ms_mspro_info)
  1840. {
  1841. MS_MSPRO_TPC_Packet_t packet;
  1842. int error;
  1843. unsigned char* buf = ms_mspro_info->data_buf;
  1844. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  1845. packet.param.out.count = 1;
  1846. packet.param.out.buffer = buf;
  1847. packet.param.out.buffer[0] = CMD_MS_RESET;
  1848. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1849. if(error)
  1850. return error;
  1851. return 0;
  1852. }