ms_mspro.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570
  1. #include <asm/cacheflush.h>
  2. #include <linux/pagemap.h>
  3. #include "ms_misc.h"
  4. #include "ms_port.h"
  5. /*
  6. //Global and static variable definitions
  7. static MS_MSPRO_Card_Info_t _ms_mspro_info = {CARD_NONE_TYPE, //card_type
  8. MEMORY_STICK_ERROR, //media_type
  9. INTERFACE_SERIAL, //interface_mode
  10. 0, //blk_len
  11. 0, //blk_nums
  12. 0, //write_protected_flag
  13. 0, //read_only_flag
  14. 0, //inited_flag
  15. 0, //removed_flag
  16. 0, //init_retry
  17. 0, //raw_cid
  18. 55, //ms_clk_unit(defaule 55ns)
  19. NULL, //ms_mspro_power
  20. NULL, //ms_mspro_get_ins
  21. NULL //ms_mspro_io_release
  22. };
  23. */
  24. static unsigned long ms_save_hw_io_config;
  25. static unsigned long ms_save_hw_io_mult_config;
  26. static unsigned long ms_save_hw_reg_flag;
  27. unsigned char disable_port_switch = 0;
  28. unsigned ms_mspro_power_delay = 0;
  29. unsigned check_one_boot_block = 0;
  30. unsigned ms_force_write_two_times = 0;
  31. unsigned mspro_access_status_reg_after_read = 0;
  32. //extern unsigned ms_power_off_lost_data_flag;
  33. extern unsigned char card_in_event_status[CARD_MAX_UNIT];
  34. extern unsigned sdio_timeout_int_times;
  35. static char * ms_mspro_error_string[]={
  36. "MS_MSPRO_NO_ERROR",
  37. "MS_MSPRO_ERROR_TPC_FORMAT",
  38. "MS_MSPRO_ERROR_RDY_TIMEOUT",
  39. "MS_MSPRO_ERROR_INT_TIMEOUT",
  40. "MS_MSPRO_ERROR_DATA_CRC",
  41. "MS_MSPRO_ERROR_MEDIA_TYPE",
  42. "MS_MSPRO_ERROR_CMDNK",
  43. "MS_MSPRO_ERROR_CED",
  44. "MS_MSPRO_ERROR_FLASH_READ",
  45. "MS_MSPRO_ERROR_FLASH_WRITE",
  46. "MS_MSPRO_ERROR_FLASH_ERASE",
  47. "MS_MSPRO_ERROR_PARAMETER",
  48. "MS_MSPRO_ERROR_WRITE_PROTECTED",
  49. "MS_MSPRO_ERROR_READ_ONLY",
  50. "MS_ERROR_BOOT_SEARCH",
  51. "MS_ERROR_MEMORY_STICK_TYPE",
  52. "MS_ERROR_FORMAT_TYPE",
  53. "MS_ERROR_BLOCK_NUMBER_SIZE",
  54. "MS_ERROR_DISABLED_BLOCK",
  55. "MS_ERROR_NO_FREE_BLOCK",
  56. "MS_ERROR_LOGICAL_PHYSICAL_TABLE",
  57. "MS_ERROR_BOOT_IDI",
  58. "MSPRO_ERROR_MEDIA_BREAKDOWN",
  59. "MSPRO_ERROR_STARTUP_TIMEOUT",
  60. "MSPRO_ERROR_WRITE_DISABLED",
  61. #ifdef MS_MSPRO_HW_CONTROL
  62. "MS_MSPRO_ERROR_TIMEOUT",
  63. "MS_MSPRO_ERROR_UNSUPPORTED",
  64. #endif
  65. "MS_MSPRO_ERROR_NO_MEMORY",
  66. "MS_MSPRO_ERROR_NO_READ"
  67. };
  68. unsigned short mass_counter;
  69. //Functions only used in this source file]
  70. void ms_mspro_prepare_power(MS_MSPRO_Card_Info_t *ms_mspro_info);
  71. void ms_mspro_io_config(MS_MSPRO_Card_Info_t *ms_mspro_info);
  72. int ms_mspro_staff_init(MS_MSPRO_Card_Info_t *ms_mspro_info);
  73. int ms_mspro_cmd_test(MS_MSPRO_Card_Info_t *ms_mspro_info);
  74. int ms_mspro_media_type_identification(MS_MSPRO_Card_Info_t *ms_mspro_info);
  75. MS_MSPRO_Media_Type_t ms_mspro_check_media_type(MS_MSPRO_Card_Info_t *ms_mspro_info);
  76. int ms_mspro_interface_mode_switching(MS_MSPRO_Card_Info_t *ms_mspro_info, MS_MSPRO_Interface_Mode_t new_interface_mode);
  77. int ms_mspro_check_data_consistency(MS_MSPRO_Card_Info_t *ms_mspro_info);
  78. //Function implement start...
  79. //Return the string buf address of specific errcode
  80. char *ms_mspro_error_to_string(int errcode)
  81. {
  82. return ms_mspro_error_string[errcode];
  83. }
  84. #ifdef MS_MSPRO_HW_CONTROL
  85. int ms_mspro_wait_int_hw(MS_MSPRO_Card_Info_t *ms_mspro_info, MS_MSPRO_TPC_Packet_t * tpc_packet)
  86. {
  87. unsigned long temp = 0;
  88. unsigned long irq_config = 0;
  89. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  90. {
  91. ms_start_timer(MS_MSPRO_INT_TIMEOUT);
  92. do
  93. {
  94. irq_config = READ_CBUS_REG(SDIO_IRQ_CONFIG);
  95. temp = (irq_config >> 8) & 0x0000000F;
  96. if(temp)
  97. {
  98. ((MS_MSPRO_INT_Register_t *)&tpc_packet->int_reg)->CED = (temp&0x01)?1:0; //DAT0
  99. ((MS_MSPRO_INT_Register_t *)&tpc_packet->int_reg)->ERR = (temp&0x02)?1:0; //DAT1
  100. ((MS_MSPRO_INT_Register_t *)&tpc_packet->int_reg)->BREQ = (temp&0x04)?1:0; //DAT2
  101. ((MS_MSPRO_INT_Register_t *)&tpc_packet->int_reg)->CMDNK = (temp&0x08)?1:0; //DAT3
  102. break;
  103. }
  104. } while(!ms_check_timer());
  105. }
  106. else // Serial I/F
  107. {
  108. ms_start_timer(MS_MSPRO_INT_TIMEOUT);
  109. do
  110. {
  111. irq_config = READ_CBUS_REG(SDIO_IRQ_CONFIG);
  112. temp = (irq_config >> 8) & 0x00000001;
  113. if(temp)
  114. break;
  115. } while(!ms_check_timer());
  116. }
  117. if(ms_check_timeout())
  118. return MS_MSPRO_ERROR_INT_TIMEOUT;
  119. else
  120. return MS_MSPRO_NO_ERROR;
  121. }
  122. #endif
  123. #ifdef MS_MSPRO_SW_CONTROL
  124. int ms_mspro_wait_int_sw(MS_MSPRO_Card_Info_t *ms_mspro_info, MS_MSPRO_TPC_Packet_t * tpc_packet)
  125. {
  126. unsigned long temp = 0, data = 0, cnt = 0;
  127. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  128. {
  129. ms_start_timer(MS_MSPRO_INT_TIMEOUT);
  130. do
  131. {
  132. ms_clk_parallel_high();
  133. data = ms_get_dat0_3_value();
  134. ms_clk_parallel_low();
  135. if(data)
  136. {
  137. if(data != temp)
  138. {
  139. temp = data;
  140. cnt = 0;
  141. continue;
  142. }
  143. else
  144. {
  145. if(++cnt < 10)
  146. continue;
  147. }
  148. ((MS_MSPRO_INT_Register_t *)&tpc_packet->int_reg)->CED = (temp&0x01)?1:0; //DAT0
  149. ((MS_MSPRO_INT_Register_t *)&tpc_packet->int_reg)->ERR = (temp&0x02)?1:0; //DAT1
  150. ((MS_MSPRO_INT_Register_t *)&tpc_packet->int_reg)->BREQ = (temp&0x04)?1:0; //DAT2
  151. ((MS_MSPRO_INT_Register_t *)&tpc_packet->int_reg)->CMDNK = (temp&0x08)?1:0; //DAT3
  152. break;
  153. }
  154. } while(!ms_check_timer());
  155. }
  156. else // Serial I/F
  157. {
  158. ms_start_timer(MS_MSPRO_INT_TIMEOUT);
  159. do
  160. {
  161. ms_clk_serial_low();
  162. data = ms_get_dat0_value();
  163. ms_clk_serial_high();
  164. if(data)
  165. break;
  166. } while(!ms_check_timer());
  167. }
  168. if(ms_check_timeout())
  169. return MS_MSPRO_ERROR_INT_TIMEOUT;
  170. else
  171. return MS_MSPRO_NO_ERROR;
  172. }
  173. #endif
  174. int ms_mspro_wait_rdy(MS_MSPRO_Card_Info_t *ms_mspro_info, MS_MSPRO_TPC_Packet_t * tpc_packet)
  175. {
  176. unsigned long temp = 0, data;
  177. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  178. {
  179. ms_clk_parallel_high();
  180. ms_clk_parallel_low();
  181. ms_start_timer(MS_MSPRO_RDY_TIMEOUT);
  182. do
  183. {
  184. ms_clk_parallel_high();
  185. data = ms_get_dat0_value();
  186. temp <<= 1;
  187. temp |= data;
  188. ms_clk_parallel_low();
  189. if(((temp&0x3f) == 0x15)|| // 010101b
  190. ((temp&0x3f) == 0x2A)) // 101010b
  191. {
  192. break;
  193. }
  194. } while(!ms_check_timer());
  195. if(tpc_packet->TPC_cmd.trans_dir.bWrite) //Write packet
  196. {
  197. ms_set_bs_state(0);
  198. }
  199. else
  200. {
  201. ms_set_bs_state(3); //Read packet
  202. }
  203. ms_clk_parallel_high();
  204. ms_clk_parallel_low();
  205. ms_clk_parallel_high();
  206. ms_clk_parallel_low();
  207. }
  208. else
  209. {
  210. ms_start_timer(MS_MSPRO_RDY_TIMEOUT);
  211. do
  212. {
  213. ms_clk_serial_low();
  214. data = ms_get_dat0_value();
  215. temp <<= 1;
  216. temp |= data;
  217. ms_clk_serial_high();
  218. if(((temp&0x3f) == 0x15)|| // 010101b
  219. ((temp&0x3f) == 0x2A)) // 101010b
  220. {
  221. break;
  222. }
  223. } while(!ms_check_timer());
  224. if(tpc_packet->TPC_cmd.trans_dir.bWrite) //Write packet
  225. {
  226. ms_set_bs_state(0);
  227. }
  228. else
  229. {
  230. ms_set_bs_state(3); //Read packet
  231. }
  232. ms_clk_serial_low();
  233. ms_clk_serial_high();
  234. }
  235. if(ms_check_timeout())
  236. return MS_MSPRO_ERROR_RDY_TIMEOUT;
  237. else
  238. return MS_MSPRO_NO_ERROR;
  239. }
  240. int ms_mspro_write_tpc(MS_MSPRO_Card_Info_t *ms_mspro_info, MS_MSPRO_TPC_Packet_t * tpc_packet)
  241. {
  242. int i;
  243. unsigned long temp,tpc_cmd;
  244. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  245. {
  246. ms_clk_parallel_low();
  247. ms_set_bs_state(1); //set bus state to BS1
  248. ms_clk_parallel_high();
  249. ms_clk_parallel_low();
  250. ms_clk_parallel_high();
  251. ms_set_dat0_3_input(); //Z
  252. ms_clk_parallel_low();
  253. ms_clk_parallel_high();
  254. ms_clk_parallel_low();
  255. ms_set_dat0_3_output(); //X
  256. ms_set_dat0_3_value(0);
  257. ms_clk_parallel_high();
  258. ms_clk_parallel_low();
  259. temp = tpc_packet->TPC_cmd.format.code; //TPC code
  260. ms_set_dat0_3_value(temp);
  261. ms_clk_parallel_high();
  262. ms_clk_parallel_low();
  263. temp = tpc_packet->TPC_cmd.format.check_code; //TPC check code
  264. ms_set_dat0_3_value(temp);
  265. ms_clk_parallel_high();
  266. ms_clk_parallel_low();
  267. ms_set_bs_state(2); //set bus state to BS2
  268. ms_set_dat0_3_value(0);
  269. ms_clk_parallel_high();
  270. ms_clk_parallel_low();
  271. if (tpc_packet->TPC_cmd.trans_dir.bRead) //Read packet
  272. ms_set_dat0_3_input();
  273. ms_clk_parallel_high();
  274. ms_clk_parallel_low();
  275. }
  276. else
  277. {
  278. ms_clk_serial_high();
  279. ms_set_bs_state(1); //set bus state to BS1
  280. ms_clk_serial_low();
  281. ms_clk_serial_high();
  282. ms_set_dat0_output();
  283. tpc_cmd = tpc_packet->TPC_cmd.value;
  284. for(i=0; i<8; i++)
  285. {
  286. temp = (tpc_cmd >> (8 - i - 1)) & 0x01;
  287. ms_set_dat0_value(temp);
  288. if(i == 7)
  289. ms_set_bs_state(2); //set bus state to BS2
  290. ms_clk_serial_low();
  291. ms_clk_serial_high();
  292. }
  293. if (tpc_packet->TPC_cmd.trans_dir.bRead) //Read packet
  294. ms_set_dat0_input();
  295. }
  296. return MS_MSPRO_NO_ERROR;
  297. }
  298. int ms_mspro_read_data_line(MS_MSPRO_Card_Info_t *ms_mspro_info, MS_MSPRO_TPC_Packet_t * tpc_packet)
  299. {
  300. unsigned long data_cnt, temp = 0, loop_num, data;
  301. unsigned short crc16 = 0;
  302. int i;
  303. #ifdef MS_MSPRO_CRC_CHECK
  304. unsigned short crc_check = 0; int error=0;
  305. #endif
  306. unsigned char * data_buf = tpc_packet->param.in.buffer;
  307. loop_num = tpc_packet->param.in.count;
  308. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  309. {
  310. #ifdef AMLOGIC_CHIP_SUPPORT
  311. if((unsigned long)data_buf == 0x3400000)
  312. {
  313. for(data_cnt = 0; data_cnt < loop_num; data_cnt++)
  314. {
  315. temp = 0;
  316. for(i = 0; i < 2; i++)
  317. {
  318. ms_clk_parallel_high();
  319. data = ms_get_dat0_3_value();
  320. temp <<= 4;
  321. temp |= data;
  322. ms_clk_parallel_low();
  323. }
  324. WRITE_BYTE_TO_FIFO(temp);
  325. #ifdef MS_MSPRO_CRC_CHECK
  326. crc_check = (crc_check << 8) ^ ms_crc_table[((crc_check >> 8) ^ temp) & 0xff];
  327. #endif
  328. }
  329. }
  330. else
  331. #endif
  332. {
  333. for(data_cnt = 0; data_cnt < loop_num; data_cnt++)
  334. {
  335. temp = 0;
  336. for(i = 0; i < 2; i++)
  337. {
  338. ms_clk_parallel_high();
  339. data = ms_get_dat0_3_value();
  340. temp <<= 4;
  341. temp |= data;
  342. ms_clk_parallel_low();
  343. }
  344. *data_buf = temp;
  345. data_buf++;
  346. #ifdef MS_MSPRO_CRC_CHECK
  347. crc_check = (crc_check << 8) ^ ms_crc_table[((crc_check >> 8) ^ temp) & 0xff];
  348. #endif
  349. }
  350. }
  351. for(i = 0; i < 4; i++)
  352. {
  353. ms_clk_parallel_high();
  354. data = ms_get_dat0_3_value();
  355. crc16 <<= 4;
  356. crc16 |= data;
  357. ms_clk_parallel_low();
  358. if(data_cnt == 2)
  359. ms_set_bs_state(0);
  360. }
  361. }
  362. else // Serial I/F
  363. {
  364. #ifdef AMLOGIC_CHIP_SUPPORT
  365. if((unsigned long)data_buf == 0x3400000)
  366. {
  367. for(data_cnt = 0; data_cnt < loop_num; data_cnt++)
  368. {
  369. temp = 0;
  370. for(i = 0; i < 8; i++)
  371. {
  372. ms_clk_serial_low();
  373. data = ms_get_dat0_value();
  374. temp <<= 1;
  375. temp |= data;
  376. ms_clk_serial_high();
  377. }
  378. WRITE_BYTE_TO_FIFO(temp);
  379. #ifdef MS_MSPRO_CRC_CHECK
  380. crc_check = (crc_check << 8) ^ ms_crc_table[((crc_check >> 8) ^ temp) & 0xff];
  381. #endif
  382. }
  383. }
  384. else
  385. #endif
  386. {
  387. for(data_cnt = 0; data_cnt < loop_num; data_cnt++)
  388. {
  389. temp = 0;
  390. for(i = 0; i < 8; i++)
  391. {
  392. ms_clk_serial_low();
  393. data = ms_get_dat0_value();
  394. temp <<= 1;
  395. temp |= data;
  396. ms_clk_serial_high();
  397. }
  398. *data_buf = temp;
  399. data_buf++;
  400. #ifdef MS_MSPRO_CRC_CHECK
  401. crc_check = (crc_check << 8) ^ ms_crc_table[((crc_check >> 8) ^ temp) & 0xff];
  402. #endif
  403. }
  404. }
  405. for(i = 0; i < 16; i++)
  406. {
  407. if(i == 15)
  408. ms_set_bs_state(0);
  409. ms_clk_serial_low();
  410. data = ms_get_dat0_value();
  411. crc16 <<= 1;
  412. crc16 |= data;
  413. ms_clk_serial_high();
  414. }
  415. }
  416. #ifdef MS_MSPRO_CRC_CHECK
  417. if(crc16 != crc_check)
  418. {
  419. error = MS_MSPRO_ERROR_DATA_CRC;
  420. #ifdef MS_MSPRO_DEBUG
  421. Debug_Printf("#%s error occured in ms_mspro_read_data_line()!\n", ms_mspro_error_to_string(error));
  422. #endif
  423. return error;
  424. }
  425. #endif
  426. return MS_MSPRO_NO_ERROR;
  427. }
  428. int ms_mspro_write_data_line(MS_MSPRO_Card_Info_t *ms_mspro_info, MS_MSPRO_TPC_Packet_t * tpc_packet)
  429. {
  430. unsigned long data_cnt, loop_num, data;
  431. unsigned short crc16 = 0;
  432. int i;
  433. unsigned char * data_buf = tpc_packet->param.out.buffer;
  434. //unsigned char * org_buf = tpc_packet->param.out.buffer;
  435. loop_num = tpc_packet->param.out.count;
  436. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  437. {
  438. //Write data
  439. for(data_cnt = 0; data_cnt < loop_num; data_cnt++)
  440. {
  441. for(i = 1; i >= 0; i--)
  442. {
  443. data = (*data_buf >> (i<<2)) & 0x0F;
  444. ms_set_dat0_3_value(data);
  445. ms_clk_parallel_high();
  446. ms_clk_parallel_low();
  447. }
  448. crc16 = (crc16 << 8) ^ ms_crc_table[((crc16 >> 8) ^ *data_buf) & 0xFF];
  449. data_buf++;
  450. }
  451. //Caculate CRC16 value and write to line
  452. //crc16 = ms_cal_crc16(org_buf, tpc_packet->param.out.count);
  453. //Write CRC16
  454. for(i = 3; i >= 0; i--)
  455. {
  456. data = (crc16 >> (i<<2)) & 0x000F;
  457. ms_set_dat0_3_value(data);
  458. ms_clk_parallel_high();
  459. ms_clk_parallel_low();
  460. }
  461. ms_set_bs_state(3);
  462. ms_set_dat0_3_value(0);
  463. ms_clk_parallel_high();
  464. ms_clk_parallel_low();
  465. ms_set_dat0_3_input();
  466. ms_clk_parallel_high();
  467. ms_clk_parallel_low();
  468. }
  469. else // Serial I/F
  470. {
  471. //Write data
  472. for(data_cnt = 0; data_cnt < loop_num; data_cnt++)
  473. {
  474. for(i = 7; i >= 0; i--)
  475. {
  476. data = (*data_buf >> i) & 0x01;
  477. ms_set_dat0_value(data);
  478. ms_clk_serial_low();
  479. ms_clk_serial_high();
  480. }
  481. crc16 = (crc16 << 8) ^ ms_crc_table[((crc16 >> 8) ^ *data_buf) & 0xFF];
  482. data_buf++;
  483. }
  484. //Caculate CRC16 value and write to line
  485. //crc16 = ms_cal_crc16(org_buf, tpc_packet->param.out.count);
  486. //Write CRC16
  487. for(i = 15; i >= 0; i--)
  488. {
  489. data = (crc16 >> i) & 0x0001;
  490. ms_set_dat0_value(data);
  491. if(i == 0)
  492. {
  493. ms_set_bs_state(3);
  494. }
  495. ms_clk_serial_low();
  496. ms_clk_serial_high();
  497. }
  498. ms_set_dat0_input();
  499. }
  500. return MS_MSPRO_NO_ERROR;
  501. }
  502. #ifdef MS_MSPRO_HW_CONTROL
  503. int ms_mspro_packet_communicate_hw(MS_MSPRO_Card_Info_t *ms_mspro_info, MS_MSPRO_TPC_Packet_t * tpc_packet)
  504. {
  505. int num_res,i,cnt;
  506. unsigned char *data_buf;
  507. unsigned long cmd_arg,cmd_send,cmd_ext;
  508. unsigned long timeout, repeat_time;
  509. unsigned long status_irq;
  510. //unsigned long irq_config;
  511. MSHW_CMD_Send_Reg_t *cmd_send_reg;
  512. MSHW_Extension_Reg_t *cmd_ext_reg;
  513. SDIO_Status_IRQ_Reg_t *status_irq_reg;
  514. //MSHW_IRQ_Config_Reg_t *irq_config_reg;
  515. num_res = 0;
  516. data_buf = NULL;
  517. //check if TPC is valid
  518. if((tpc_packet->TPC_cmd.format.code ^ tpc_packet->TPC_cmd.format.check_code) != 0x0F)
  519. return MS_MSPRO_ERROR_TPC_FORMAT;
  520. cmd_arg = 0;
  521. cmd_send = 0;
  522. cmd_ext = 0;
  523. cmd_send_reg = (void *)&cmd_send;
  524. cmd_send_reg->tpc_data = tpc_packet->TPC_cmd.value;
  525. cmd_send_reg->use_int_window = 1;
  526. cmd_ext_reg = (void *)&cmd_ext;
  527. switch(tpc_packet->TPC_cmd.value)
  528. {
  529. case TPC_MS_READ_PAGE_DATA: //TPC_MSPRO_READ_LONG_DATA
  530. case TPC_MSPRO_READ_SHORT_DATA:
  531. //AV_invalidate_dcache();
  532. //inv_dcache_range((unsigned long)ms_mspro_buffer, ((unsigned long)ms_mspro_buffer + tpc_packet->param.in.count));
  533. //data_buf = ms_mspro_buffer;
  534. data_buf = ms_mspro_info->data_phy_buf;
  535. //data_buf = ms_mspro_info->dma_phy_buf;
  536. cmd_send_reg->have_long_data_read = 1;
  537. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  538. cmd_ext_reg->long_data_nums = tpc_packet->param.in.count * 8 + 16 - 4;
  539. else //INTERFACE_SERIAL
  540. cmd_ext_reg->long_data_nums = tpc_packet->param.in.count * 8 + 16 - 1;
  541. break;
  542. case TPC_MS_WRITE_PAGE_DATA: //TPC_MSPRO_WRITE_LONG_DATA
  543. case TPC_MSPRO_WRITE_SHORT_DATA:
  544. //if(ms_mspro_info->card_type == CARD_TYPE_MS)
  545. // data_buf = ms_mspro_buf->ms.page_buf;
  546. //else // CARD_TYPE_MSPRO
  547. // data_buf = ms_mspro_buf->mspro.sector_buf;
  548. //memcpy(data_buf, tpc_packet->param.out.buffer, tpc_packet->param.out.count);
  549. //data_buf = tpc_packet->param.out.buffer;
  550. dmac_map_area(ms_mspro_info->data_buf, tpc_packet->param.in.count, 1);
  551. if((tpc_packet->param.out.buffer >= ms_mspro_info->data_buf) &&
  552. (tpc_packet->param.out.buffer < ms_mspro_info->data_buf+sizeof(MS_MSPRO_Card_Info_t)))
  553. data_buf = ms_mspro_info->data_phy_buf +
  554. ((int)(tpc_packet->param.out.buffer) - (int)(ms_mspro_info->data_buf));
  555. else if((tpc_packet->param.out.buffer >= ms_mspro_info->ms_mspro_buf) &&
  556. (tpc_packet->param.out.buffer < ms_mspro_info->ms_mspro_buf+PAGE_CACHE_SIZE))
  557. data_buf = ms_mspro_info->ms_mspro_phy_buf +
  558. ((int)(tpc_packet->param.out.buffer) - (int)(ms_mspro_info->ms_mspro_buf));
  559. else if((tpc_packet->param.out.buffer >= ms_mspro_info->dma_buf) &&
  560. (tpc_packet->param.out.buffer < ms_mspro_info->dma_buf+256*512))
  561. data_buf = ms_mspro_info->dma_phy_buf +
  562. ((int)(tpc_packet->param.out.buffer) - (int)(ms_mspro_info->dma_buf));
  563. else{
  564. printk("ms_mspro_info->data_buf = %x, ms_mspro_info->data_phy_buf = %x\n"
  565. "ms_mspro_info->ms_mspro_buf = %x, ms_mspro_info->ms_mspro_phy_buf = %x\n"
  566. "ms_mspro_info->dma_buf = %x, ms_mspro_info->dma_phy_buf = %x\n"
  567. "tpc_packet->param.out.buffer = %x, data_buf = %x\n",
  568. (unsigned int)(ms_mspro_info->data_buf), (unsigned int)(ms_mspro_info->data_phy_buf),
  569. (unsigned int)(ms_mspro_info->ms_mspro_buf), (unsigned int)(ms_mspro_info->ms_mspro_phy_buf),
  570. (unsigned int)(ms_mspro_info->dma_buf), (unsigned int)(ms_mspro_info->dma_phy_buf),
  571. (unsigned int)(tpc_packet->param.out.buffer), (unsigned int)data_buf);
  572. dump_stack();
  573. BUG();
  574. }
  575. cmd_send_reg->have_long_data_write = 1;
  576. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  577. cmd_ext_reg->long_data_nums = tpc_packet->param.out.count * 8 + 16 - 4;
  578. else //INTERFACE_SERIAL
  579. cmd_ext_reg->long_data_nums = tpc_packet->param.out.count * 8 + 16 - 1;
  580. //AV_invalidate_dcache();
  581. //inv_dcache_range((unsigned long)tpc_packet->param.out.buffer, ((unsigned long)tpc_packet->param.out.buffer + tpc_packet->param.out.count));
  582. break;
  583. case TPC_MS_MSPRO_READ_REG:
  584. case TPC_MS_MSPRO_GET_INT:
  585. num_res = tpc_packet->param.in.count;
  586. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  587. cmd_send_reg->short_data_read_nums = tpc_packet->param.in.count * 8 + 16 - 4;
  588. else //INTERFACE_SERIAL
  589. cmd_send_reg->short_data_read_nums = tpc_packet->param.in.count * 8 + 16 - 1;
  590. break;
  591. case TPC_MS_MSPRO_WRITE_REG:
  592. case TPC_MS_MSPRO_SET_RW_REG_ADRS:
  593. case TPC_MS_MSPRO_SET_CMD:
  594. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  595. cmd_send_reg->short_data_write_nums = tpc_packet->param.out.count * 8 + 16 - 4;
  596. else //INTERFACE_SERIAL
  597. cmd_send_reg->short_data_write_nums = tpc_packet->param.out.count * 8 + 16 - 1;
  598. cnt = tpc_packet->param.out.count;
  599. for(i=0; i<4; i++)
  600. {
  601. if(cnt <= 0)
  602. break;
  603. cmd_arg |= (unsigned long)(tpc_packet->param.out.buffer[i]) << (24 - i * 8);
  604. cnt--;
  605. }
  606. if(cnt-- > 0)
  607. {
  608. cmd_ext_reg->short_data_ext |= tpc_packet->param.out.buffer[i] << 8;
  609. if(cnt)
  610. cmd_ext_reg->short_data_ext |= tpc_packet->param.out.buffer[i+1];
  611. }
  612. break;
  613. case TPC_MSPRO_EX_SET_CMD:
  614. return MS_MSPRO_ERROR_UNSUPPORTED;
  615. default:
  616. return MS_MSPRO_ERROR_UNSUPPORTED;
  617. }
  618. #define MS_MSPRO_CMD_BUSY_COUNT 7000//7ms
  619. #define MS_MSPRO_READ_BUSY_COUNT 20000//20ms
  620. #define MS_MSPRO_WRITE_BUSY_COUNT 20000//20ms
  621. #define MS_MSPRO_RETRY_COUNT 5
  622. repeat_time = 0;
  623. if(cmd_send_reg->have_long_data_write)
  624. timeout = MS_MSPRO_WRITE_BUSY_COUNT;
  625. else if(cmd_send_reg->have_long_data_read)
  626. timeout = MS_MSPRO_READ_BUSY_COUNT;
  627. else
  628. timeout = MS_MSPRO_CMD_BUSY_COUNT;
  629. //PACKET_RETRY:
  630. status_irq = 0;
  631. status_irq_reg = (void *)&status_irq;
  632. status_irq_reg->if_int = 1;
  633. status_irq_reg->cmd_int = 1;
  634. if(timeout > (ms_mspro_info->ms_clk_unit*0x1FFF)/1000)
  635. {
  636. status_irq_reg->timing_out_count = 0x1FFF;
  637. //sdio_timeout_int_times = (timeout*1000)/(ms_mspro_info->ms_clk_unit*0x1FFF);
  638. sdio_timeout_int_times = timeout/(ms_mspro_info->ms_clk_unit*0x1FFF/1000);
  639. }
  640. else
  641. {
  642. status_irq_reg->timing_out_count = (ms_mspro_info->ms_clk_unit)*1000;
  643. sdio_timeout_int_times = 1;
  644. }
  645. WRITE_CBUS_REG(SDIO_STATUS_IRQ, status_irq);
  646. WRITE_CBUS_REG(CMD_ARGUMENT, cmd_arg);
  647. WRITE_CBUS_REG(SDIO_EXTENSION, cmd_ext);
  648. if(data_buf != NULL)
  649. {
  650. WRITE_CBUS_REG(SDIO_M_ADDR, (unsigned long)data_buf);
  651. }
  652. init_completion(&sdio_int_complete);
  653. sdio_open_host_interrupt(SDIO_CMD_INT);
  654. sdio_open_host_interrupt(SDIO_TIMEOUT_INT);
  655. WRITE_CBUS_REG(CMD_SEND, cmd_send);
  656. //interruptible_sleep_on(&sdio_wait_event);
  657. wait_for_completion(&sdio_int_complete);
  658. /*timeout_count = 0;
  659. while(1)
  660. {
  661. status_irq = READ_CBUS_REG(SDIO_STATUS_IRQ);
  662. if(!status_irq_reg->cmd_busy && status_irq_reg->cmd_int)
  663. break;
  664. if((++timeout_count) > timeout)
  665. {
  666. irq_config_reg->soft_reset = 1;
  667. WRITE_CBUS_REG(SDIO_IRQ_CONFIG, irq_config);
  668. if((++repeat_time) > MS_MSPRO_RETRY_COUNT)
  669. {
  670. return MS_MSPRO_ERROR_TIMEOUT;
  671. }
  672. #ifdef MS_MSPRO_DEBUG
  673. //if(cmd_send_reg->have_long_data_write)
  674. // Debug_Printf("ms_mspro_packet_communicate() retry %d...\n", repeat_time);
  675. #endif
  676. goto PACKET_RETRY;
  677. }
  678. }*/
  679. if(sdio_timeout_int_times == 0)
  680. return MS_MSPRO_ERROR_TIMEOUT;
  681. status_irq = READ_CBUS_REG(SDIO_STATUS_IRQ);
  682. if(tpc_packet->TPC_cmd.trans_dir.bRead && !status_irq_reg->data_read_crc16_ok)
  683. return MS_MSPRO_ERROR_DATA_CRC;
  684. //if(tpc_packet->TPC_cmd.trans_dir.bWrite && !status_irq_reg->data_write_crc16_ok)
  685. // return MS_MSPRO_ERROR_DATA_CRC;
  686. if(num_res > 0)
  687. {
  688. unsigned long multi_config = 0;
  689. SDIO_Multi_Config_Reg_t *multi_config_reg = (void *)&multi_config;
  690. multi_config_reg->write_read_out_index = 1;
  691. WRITE_CBUS_REG(SDIO_MULT_CONFIG, multi_config);
  692. }
  693. while(num_res)
  694. {
  695. unsigned long data_temp = READ_CBUS_REG(CMD_ARGUMENT);
  696. tpc_packet->param.in.buffer[--num_res] = data_temp & 0xFF;
  697. if(num_res <= 0)
  698. break;
  699. tpc_packet->param.in.buffer[--num_res] = (data_temp >> 8) & 0xFF;
  700. if(num_res <= 0)
  701. break;
  702. tpc_packet->param.in.buffer[--num_res] = (data_temp >> 16) & 0xFF;
  703. if(num_res <= 0)
  704. break;
  705. tpc_packet->param.in.buffer[--num_res] = (data_temp >> 24) & 0xFF;
  706. }
  707. if(tpc_packet->TPC_cmd.trans_dir.bRead && data_buf)
  708. {
  709. #ifdef AMLOGIC_CHIP_SUPPORT
  710. if((unsigned long)tpc_packet->param.in.buffer == 0x3400000)
  711. {
  712. for(i=0; i<tpc_packet->param.in.count; i++)
  713. {
  714. WRITE_BYTE_TO_FIFO(data_buf[i]);
  715. }
  716. }
  717. else
  718. #endif
  719. {
  720. dmac_map_area(ms_mspro_info->data_buf, tpc_packet->param.in.count, 2);
  721. memcpy(tpc_packet->param.in.buffer, ms_mspro_info->data_buf, tpc_packet->param.in.count);
  722. dmac_map_area(ms_mspro_info->data_buf, tpc_packet->param.in.count, 2);
  723. }
  724. }
  725. dmac_map_area(ms_mspro_info->data_buf, tpc_packet->param.in.count, 1);
  726. return MS_MSPRO_NO_ERROR;
  727. }
  728. #endif
  729. #ifdef MS_MSPRO_SW_CONTROL
  730. int ms_mspro_packet_communicate_sw(MS_MSPRO_Card_Info_t *ms_mspro_info, MS_MSPRO_TPC_Packet_t * tpc_packet)
  731. {
  732. int ret;
  733. //check if TPC is valid
  734. if((tpc_packet->TPC_cmd.format.code ^ tpc_packet->TPC_cmd.format.check_code) != 0x0F)
  735. return MS_MSPRO_ERROR_TPC_FORMAT;
  736. //write TPC comand to data line
  737. ms_mspro_write_tpc(ms_mspro_info, tpc_packet);
  738. //write data or read response
  739. if (tpc_packet->TPC_cmd.trans_dir.bWrite) //Write packet
  740. {
  741. ms_mspro_write_data_line(ms_mspro_info, tpc_packet);
  742. ret = ms_mspro_wait_rdy(ms_mspro_info, tpc_packet);
  743. if(!ret)
  744. {
  745. if ((tpc_packet->TPC_cmd.value == TPC_MS_MSPRO_SET_CMD) || (tpc_packet->TPC_cmd.value == TPC_MSPRO_EX_SET_CMD))
  746. ret = ms_mspro_wait_int(ms_mspro_info, tpc_packet);
  747. }
  748. }
  749. else //Read packet
  750. {
  751. ret = ms_mspro_wait_rdy(ms_mspro_info, tpc_packet);
  752. if(!ret)
  753. {
  754. ret=ms_mspro_read_data_line(ms_mspro_info, tpc_packet);
  755. }
  756. }
  757. ms_set_bs_state(0);
  758. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  759. {
  760. ms_clk_parallel_high();
  761. ms_clk_parallel_low();
  762. }
  763. else // Serial I/F
  764. {
  765. ms_clk_serial_low();
  766. ms_clk_serial_high();
  767. }
  768. return ret;
  769. }
  770. #endif
  771. int ms_mspro_media_type_identification(MS_MSPRO_Card_Info_t *ms_mspro_info)
  772. {
  773. MS_MSPRO_TPC_Packet_t packet;
  774. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  775. int error;
  776. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //READ_REG: Status, Type, Catagory, Class
  777. packet.param.out.count = 4;
  778. ms_mspro_buf->ms.reg_set.read_addr = 0x02; //READ_ADRS = 0x02
  779. ms_mspro_buf->ms.reg_set.read_size = 0x06; //READ_SIZE = 0x06
  780. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  781. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  782. if(error)
  783. return error;
  784. packet.TPC_cmd.value = TPC_MS_MSPRO_READ_REG; //READ_REG
  785. packet.param.in.count = 6; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  786. packet.param.in.buffer = &ms_mspro_buf->ms.regs.Status_Reg0;
  787. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  788. if(error)
  789. return error;
  790. error = ms_media_type_identification(ms_mspro_info);
  791. if(error)
  792. {
  793. error = mspro_media_type_identification(ms_mspro_info);
  794. }
  795. #ifdef MS_MSPRO_DEBUG
  796. if(error)
  797. Debug_Printf("No any valid media type detected!\n");
  798. #endif
  799. return error;
  800. }
  801. int ms_mspro_interface_mode_switching(MS_MSPRO_Card_Info_t *ms_mspro_info, MS_MSPRO_Interface_Mode_t new_interface_mode)
  802. {
  803. MS_MSPRO_TPC_Packet_t packet;
  804. //unsigned char sys_para_reg;
  805. unsigned char* sys_para_reg = ms_mspro_info->data_buf;
  806. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  807. int error;
  808. if(disable_port_switch)
  809. return 0;
  810. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS; //READ_REG: Status, Type, Catagory, Class
  811. packet.param.out.count = 4;
  812. ms_mspro_buf->ms.reg_set.write_addr = 0x10; //WRITE_ADRS = 0x10
  813. ms_mspro_buf->ms.reg_set.write_size = 0x01; //WRITE_SIZE = 0x01
  814. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->ms.reg_set;
  815. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  816. if(error)
  817. return error;
  818. packet.TPC_cmd.value = TPC_MS_MSPRO_WRITE_REG; //WRITE_REG
  819. packet.param.out.count = 1; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  820. if(ms_mspro_info->card_type == CARD_TYPE_MS)
  821. { //Default 0x80: Serial I/F
  822. //sys_para_reg = (new_interface_mode==INTERFACE_PARALLEL) ? 0x88 : 0x80;
  823. *sys_para_reg = (new_interface_mode==INTERFACE_PARALLEL) ? 0x88 : 0x80;
  824. }
  825. else //CARD_TYPE_MSPRO
  826. { //Default 0x00: Parallel I/F
  827. //sys_para_reg = (new_interface_mode==INTERFACE_SERIAL) ? 0x80 : 0x00;
  828. *sys_para_reg = (new_interface_mode==INTERFACE_SERIAL) ? 0x80 : 0x00;
  829. }
  830. //packet.param.out.buffer[0] = sys_para_reg;
  831. packet.param.out.buffer = sys_para_reg;
  832. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  833. if(error)
  834. return error;
  835. ms_mspro_info->interface_mode = new_interface_mode;
  836. #ifdef MS_MSPRO_HW_CONTROL
  837. if(MS_WORK_MODE == CARD_HW_MODE)
  838. {
  839. unsigned long sdio_config = 0;
  840. SDIO_Config_Reg_t *config_reg = (void *)&sdio_config;
  841. sdio_config = READ_CBUS_REG(SDIO_CONFIG);
  842. if(new_interface_mode == INTERFACE_PARALLEL)
  843. {
  844. config_reg->bus_width = 1;
  845. config_reg->cmd_out_at_posedge = 1;
  846. }
  847. else // INTERFACE_SERIAL
  848. {
  849. config_reg->bus_width = 0;
  850. config_reg->cmd_out_at_posedge = 0;
  851. }
  852. WRITE_CBUS_REG(SDIO_CONFIG, sdio_config);
  853. }
  854. #endif
  855. return MS_MSPRO_NO_ERROR;
  856. }
  857. void ms_mspro_endian_convert(Endian_Type_t data_type, void * data)
  858. {
  859. unsigned temp;
  860. int i;
  861. int data_len = data_type;
  862. unsigned char * data_buf = data;
  863. for(i=0; i<(data_len/2); i++)
  864. {
  865. temp = data_buf[i];
  866. data_buf[i] = data_buf[data_len-i-1];
  867. data_buf[data_len-i-1] = temp;
  868. }
  869. }
  870. MS_MSPRO_Media_Type_t ms_mspro_check_media_type(MS_MSPRO_Card_Info_t *ms_mspro_info)
  871. {
  872. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  873. if((ms_mspro_buf->ms.regs.Category_Reg >= 0x01) && (ms_mspro_buf->ms.regs.Category_Reg <= 0x7F))
  874. return MEMORY_STICK_IO;
  875. if(ms_mspro_buf->ms.regs.Type_Reg == 0x01) // Memory Stick Pro
  876. {
  877. if(ms_mspro_buf->ms.regs.Category_Reg == 0x10)
  878. return MEMORY_STICK_PRO_IO;
  879. else if(ms_mspro_buf->ms.regs.Category_Reg == 0x00)
  880. {
  881. if((ms_mspro_buf->ms.regs.Class_Reg == 0x00) && (ms_mspro_buf->ms.boot_attribute_information.Device_Type == 0))
  882. return MEMORY_STICK_PRO;
  883. if(((ms_mspro_buf->ms.regs.Class_Reg == 0x00) || (ms_mspro_buf->ms.regs.Class_Reg == 0x01) || (ms_mspro_buf->ms.regs.Class_Reg == 0x02)) &&
  884. (ms_mspro_buf->ms.boot_attribute_information.Device_Type == 1))
  885. return MEMORY_STICK_PRO_ROM;
  886. if(((ms_mspro_buf->ms.regs.Class_Reg == 0x00) || (ms_mspro_buf->ms.regs.Class_Reg == 0x02)) &&
  887. (ms_mspro_buf->ms.boot_attribute_information.Device_Type == 2))
  888. return MEMORY_STICK_PRO_R;
  889. }
  890. }
  891. else if((ms_mspro_buf->ms.regs.Type_Reg == 0x00) || (ms_mspro_buf->ms.regs.Type_Reg == 0xFF)) // Memory Stick
  892. {
  893. if(ms_mspro_buf->ms.boot_attribute_information.Device_Type == 0)
  894. {
  895. if((ms_mspro_buf->ms.regs.Type_Reg == ms_mspro_buf->ms.regs.Category_Reg) && (ms_mspro_buf->ms.regs.Category_Reg == ms_mspro_buf->ms.regs.Class_Reg))
  896. {
  897. if(ms_mspro_buf->ms.boot_attribute_information.Parallel_Transfer_Supporting == 0)
  898. return MEMORY_STICK;
  899. if((ms_mspro_buf->ms.boot_attribute_information.Parallel_Transfer_Supporting == 1) && (ms_mspro_buf->ms.regs.Type_Reg == 0x00))
  900. return MEMORY_STICK_WITH_SP;
  901. }
  902. }
  903. else if(ms_mspro_buf->ms.boot_attribute_information.Device_Type == 1)
  904. {
  905. if(ms_mspro_buf->ms.regs.Type_Reg == ms_mspro_buf->ms.regs.Category_Reg)
  906. {
  907. if(ms_mspro_buf->ms.boot_attribute_information.Parallel_Transfer_Supporting == 0)
  908. {
  909. if((ms_mspro_buf->ms.regs.Type_Reg == 0x00) &&
  910. ((ms_mspro_buf->ms.regs.Class_Reg == 0x00) || (ms_mspro_buf->ms.regs.Class_Reg == 0x01) || (ms_mspro_buf->ms.regs.Class_Reg == 0x02)))
  911. return MEMORY_STICK_ROM;
  912. if((ms_mspro_buf->ms.regs.Type_Reg == 0xFF) &&
  913. ((ms_mspro_buf->ms.regs.Class_Reg == 0x01) || (ms_mspro_buf->ms.regs.Class_Reg == 0x02) || (ms_mspro_buf->ms.regs.Class_Reg == 0xFF)))
  914. return MEMORY_STICK_ROM;
  915. }
  916. if(ms_mspro_buf->ms.boot_attribute_information.Parallel_Transfer_Supporting == 1)
  917. {
  918. if((ms_mspro_buf->ms.regs.Type_Reg == 0x00) &&
  919. ((ms_mspro_buf->ms.regs.Class_Reg == 0x00) || (ms_mspro_buf->ms.regs.Class_Reg == 0x01) || (ms_mspro_buf->ms.regs.Class_Reg == 0x02)))
  920. return MEMORY_STICK_ROM_WITH_SP;
  921. }
  922. }
  923. }
  924. else if(ms_mspro_buf->ms.boot_attribute_information.Device_Type == 2)
  925. {
  926. if(ms_mspro_buf->ms.regs.Type_Reg == ms_mspro_buf->ms.regs.Category_Reg)
  927. {
  928. if((ms_mspro_buf->ms.boot_attribute_information.Parallel_Transfer_Supporting == 0) && (ms_mspro_buf->ms.regs.Class_Reg == 0x02))
  929. return MEMORY_STICK_R;
  930. if((ms_mspro_buf->ms.boot_attribute_information.Parallel_Transfer_Supporting == 1) && (ms_mspro_buf->ms.regs.Class_Reg == 0x02))
  931. return MEMORY_STICK_R_WITH_SP;
  932. }
  933. }
  934. }
  935. return MEMORY_STICK_ERROR;
  936. }
  937. int ms_mspro_init(MS_MSPRO_Card_Info_t *ms_mspro_info)
  938. {
  939. int error,i;
  940. int init_retry_flag = 0;
  941. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  942. unsigned long temp;
  943. ms_save_hw_io_config = 0;
  944. ms_save_hw_io_mult_config = 0;
  945. ms_save_hw_reg_flag = 0;
  946. if(ms_mspro_info->inited_flag && !ms_mspro_info->removed_flag)
  947. {
  948. #ifdef MS_MSPRO_HW_CONTROL
  949. if(MS_WORK_MODE == CARD_HW_MODE)
  950. ms_sdio_enable(ms_mspro_info->io_pad_type);
  951. #endif
  952. #ifdef MS_MSPRO_SW_CONTROL
  953. if(MS_WORK_MODE == CARD_SW_MODE)
  954. ms_gpio_enable(ms_mspro_info->io_pad_type);
  955. #endif
  956. error = ms_mspro_cmd_test(ms_mspro_info);
  957. if(!error)
  958. return error;
  959. }
  960. if(++ms_mspro_info->init_retry > MS_MSPRO_INIT_RETRY)
  961. return MSPRO_ERROR_MEDIA_BREAKDOWN;
  962. #ifdef MS_MSPRO_DEBUG
  963. Debug_Printf("\nMS/MSPRO initialization started......\n");
  964. #endif
  965. init_retry:
  966. error = ms_mspro_staff_init(ms_mspro_info);
  967. if(error)
  968. {
  969. #ifdef MS_MSPRO_DEBUG
  970. Debug_Printf("#%s error occured in ms_mspro_staff_init()!\n", ms_mspro_error_to_string(error));
  971. #endif
  972. return error;
  973. }
  974. error = ms_mspro_media_type_identification(ms_mspro_info);
  975. if(error)
  976. {
  977. #ifdef MS_MSPRO_DEBUG
  978. Debug_Printf("#%s error occured in ms_mspro_media_type_identification()!\n", ms_mspro_error_to_string(error));
  979. #endif
  980. return error;
  981. }
  982. ms_mspro_info->media_type = ms_mspro_check_media_type(ms_mspro_info);
  983. if(ms_mspro_info->card_type == CARD_TYPE_MS)
  984. {
  985. error = ms_search_boot_block(ms_mspro_info, ms_mspro_info->data_buf);
  986. if(error)
  987. {
  988. #ifdef MS_MSPRO_DEBUG
  989. Debug_Printf("#%s error occured in ms_search_boot_block()!\n", ms_mspro_error_to_string(error));
  990. #endif
  991. return error;
  992. }
  993. error = ms_check_boot_block(ms_mspro_info, ms_mspro_info->data_buf);
  994. if(error)
  995. {
  996. #ifdef MS_MSPRO_DEBUG
  997. Debug_Printf("#%s error occured in ms_check_boot_block()!\n", ms_mspro_error_to_string(error));
  998. #endif
  999. return error;
  1000. }
  1001. error = ms_check_disabled_block(ms_mspro_info, ms_mspro_info->data_buf);
  1002. if(error)
  1003. {
  1004. #ifdef MS_MSPRO_DEBUG
  1005. Debug_Printf("#%s error occured in ms_check_disabled_block()!\n", ms_mspro_error_to_string(error));
  1006. #endif
  1007. return error;
  1008. }
  1009. //error = ms_read_boot_idi(ms_mspro_buf->ms.page_buf);
  1010. //if(error)
  1011. //{
  1012. //#ifdef MS_MSPRO_DEBUG
  1013. //Debug_Printf("#%s error occured in ms_read_boot_idi()!\n", ms_mspro_error_to_string(error));
  1014. //#endif
  1015. // return error;
  1016. //}
  1017. if(ms_mspro_buf->ms.boot_attribute_information.Parallel_Transfer_Supporting && (!init_retry_flag))
  1018. {
  1019. error = ms_mspro_interface_mode_switching(ms_mspro_info, INTERFACE_PARALLEL);
  1020. if(error)
  1021. {
  1022. #ifdef MS_MSPRO_DEBUG
  1023. Debug_Printf("#%s error occured in ms_mspro_interface_mode_switching()!\n", ms_mspro_error_to_string(error));
  1024. #endif
  1025. return error;
  1026. }
  1027. error = ms_mspro_cmd_test(ms_mspro_info);
  1028. if(error)
  1029. {
  1030. #ifdef MS_MSPRO_DEBUG
  1031. Debug_Printf("This Memory Stick can not work in Parallel I/F mode, retry Serial I/F mode!\n");
  1032. #endif
  1033. error = ms_mspro_interface_mode_switching(ms_mspro_info, INTERFACE_SERIAL);
  1034. if(error)
  1035. {
  1036. ms_mspro_info->interface_mode = INTERFACE_SERIAL;
  1037. error = ms_mspro_cmd_test(ms_mspro_info);
  1038. }
  1039. if(error)
  1040. {
  1041. init_retry_flag = 1;
  1042. goto init_retry;
  1043. //#ifdef MS_MSPRO_DEBUG
  1044. // Debug_Printf("#%s error occured in ms_mspro_interface_mode_switching()!\n", ms_mspro_error_to_string(error));
  1045. //#endif
  1046. // return error;
  1047. }
  1048. }
  1049. }
  1050. for(i=0; i<MS_MAX_SEGMENT_NUMBERS*MS_BLOCKS_PER_SEGMENT; i++)
  1051. ms_mspro_buf->ms.logical_physical_table[i] = 0xFFFF;
  1052. for(i=0; i<MS_MAX_SEGMENT_NUMBERS*MS_MAX_FREE_BLOCKS_PER_SEGMENT; i++)
  1053. ms_mspro_buf->ms.free_block_table[i] = 0xFFFF;
  1054. if(ms_mspro_buf->ms.boot_area_protection_process_flag)
  1055. {
  1056. error = ms_boot_area_protection(ms_mspro_info, ms_mspro_info->data_buf);
  1057. if(error)
  1058. {
  1059. #ifdef MS_MSPRO_DEBUG
  1060. Debug_Printf("#%s error occured in ms_boot_area_protection()!\n", ms_mspro_error_to_string(error));
  1061. #endif
  1062. return error;
  1063. }
  1064. }
  1065. for(i=0; i<ms_mspro_buf->ms.boot_attribute_information.Block_Numbers/MS_BLOCKS_PER_SEGMENT; i++)
  1066. {
  1067. error = ms_logical_physical_table_creation(ms_mspro_info, i);
  1068. if(error)
  1069. {
  1070. #ifdef MS_MSPRO_DEBUG
  1071. Debug_Printf("#%s error occured in ms_logical_physical_table_creation()!\n", ms_mspro_error_to_string(error));
  1072. #endif
  1073. return error;
  1074. }
  1075. }
  1076. //temp = ms_mspro_buf->ms.boot_attribute_information.Page_Size;
  1077. //ms_mspro_info->blk_len = temp;
  1078. ms_mspro_info->blk_len = MS_PAGE_SIZE;
  1079. temp = ms_mspro_buf->ms.boot_attribute_information.Effective_Block_Numbers - 2;
  1080. temp *= (ms_mspro_buf->ms.boot_attribute_information.Block_Size*2);
  1081. ms_mspro_info->blk_nums = temp;
  1082. //if((ms_mspro_info->blk_nums <= MS_WRITE_ESPECIAL_CAPACITY_BLOCKS) && ms_power_off_lost_data_flag)
  1083. // ms_force_write_two_times = 1;
  1084. }
  1085. else if(ms_mspro_info->card_type == CARD_TYPE_MSPRO)
  1086. {
  1087. error = mspro_cpu_startup(ms_mspro_info);
  1088. if(error)
  1089. {
  1090. #ifdef MS_MSPRO_DEBUG
  1091. Debug_Printf("#%s error occured in mspro_cpu_startup()!\n", ms_mspro_error_to_string(error));
  1092. #endif
  1093. return error;
  1094. }
  1095. error = mspro_confirm_attribute_information(ms_mspro_info, ms_mspro_info->data_buf);
  1096. if(error)
  1097. {
  1098. #ifdef MS_MSPRO_DEBUG
  1099. Debug_Printf("#%s error occured in mspro_confirm_attribute_information()!\n", ms_mspro_error_to_string(error));
  1100. #endif
  1101. return error;
  1102. }
  1103. error = mspro_confirm_system_information(ms_mspro_info, ms_mspro_info->data_buf);
  1104. if(error)
  1105. {
  1106. #ifdef MS_MSPRO_DEBUG
  1107. Debug_Printf("#%s error occured in mspro_confirm_system_information()!\n", ms_mspro_error_to_string(error));
  1108. #endif
  1109. return error;
  1110. }
  1111. if(ms_mspro_buf->mspro.system_information.Interface_Type == 1 && (!init_retry_flag))
  1112. {
  1113. error = ms_mspro_interface_mode_switching(ms_mspro_info, INTERFACE_PARALLEL);
  1114. if(error)
  1115. {
  1116. #ifdef MS_MSPRO_DEBUG
  1117. Debug_Printf("#%s error occured in ms_mspro_interface_mode_switching()!\n", ms_mspro_error_to_string(error));
  1118. #endif
  1119. return error;
  1120. }
  1121. error = ms_mspro_cmd_test(ms_mspro_info);
  1122. if(error)
  1123. {
  1124. #ifdef MS_MSPRO_DEBUG
  1125. Debug_Printf("This Memory Stick Pro can not work in Parallel I/F mode, retry Serial I/F mode!\n");
  1126. #endif
  1127. error = ms_mspro_interface_mode_switching(ms_mspro_info, INTERFACE_SERIAL);
  1128. if(error)
  1129. {
  1130. ms_mspro_info->interface_mode = INTERFACE_SERIAL;
  1131. error = ms_mspro_cmd_test(ms_mspro_info);
  1132. }
  1133. if(error)
  1134. {
  1135. init_retry_flag =1;
  1136. goto init_retry;
  1137. //#ifdef MS_MSPRO_DEBUG
  1138. // Debug_Printf("#%s error occured in ms_mspro_interface_mode_switching()!\n", ms_mspro_error_to_string(error));
  1139. //#endif
  1140. // return error;
  1141. }
  1142. }
  1143. }
  1144. //temp = ms_mspro_buf->mspro.system_information.Page_Size;
  1145. //temp *= ms_mspro_buf->mspro.system_information.Unit_Size;
  1146. //ms_mspro_info->blk_len = temp;
  1147. ms_mspro_info->blk_len = MSPRO_SECTOR_SIZE;
  1148. temp = ms_mspro_buf->mspro.system_information.User_Area_Blocks;
  1149. temp *= ms_mspro_buf->mspro.system_information.Block_Size;
  1150. ms_mspro_info->blk_nums = temp;
  1151. }
  1152. else
  1153. {
  1154. error = MS_MSPRO_ERROR_MEDIA_TYPE;
  1155. #ifdef MS_MSPRO_DEBUG
  1156. Debug_Printf("#%s error occured in mspro_confirm_system_information()!\n", ms_mspro_error_to_string(error));
  1157. #endif
  1158. return error;
  1159. }
  1160. //error = ms_mspro_check_data_consistency();
  1161. //if(error)
  1162. //{
  1163. //#ifdef MS_MSPRO_DEBUG
  1164. // Debug_Printf("#%s error occured in ms_mspro_check_data_consistency()!\n", ms_mspro_error_to_string(error));
  1165. //#endif
  1166. // return error;
  1167. // }
  1168. #ifdef MS_MSPRO_DEBUG
  1169. Debug_Printf("ms_mspro_init() is completed successfully!\n");
  1170. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  1171. {
  1172. if(ms_mspro_info->card_type == CARD_TYPE_MS)
  1173. Debug_Printf("This Memory Stick card is working in Parallel I/F mode!\n\n");
  1174. else
  1175. Debug_Printf("This Memory Stick Pro card is working in Parallel I/F mode!\n\n");
  1176. }
  1177. else
  1178. {
  1179. if(ms_mspro_info->card_type == CARD_TYPE_MS)
  1180. Debug_Printf("This Memory Stick card is working in Serial I/F mode!\n\n");
  1181. else
  1182. Debug_Printf("This Memory Stick Pro card is working in Serial I/F mode!\n\n");
  1183. }
  1184. #endif
  1185. ms_mspro_info->inited_flag = 1;
  1186. ms_mspro_info->init_retry = 0;
  1187. ms_mspro_info->raw_cid = ms_mspro_info->card_type;
  1188. ms_save_hw_io_config = READ_CBUS_REG(SDIO_CONFIG);
  1189. ms_save_hw_io_mult_config = READ_CBUS_REG(SDIO_MULT_CONFIG);
  1190. ms_save_hw_reg_flag = 1;
  1191. return MS_MSPRO_NO_ERROR;
  1192. }
  1193. int ms_mspro_check_insert(MS_MSPRO_Card_Info_t *ms_mspro_info)
  1194. {
  1195. int level;
  1196. if(ms_mspro_info->ms_mspro_get_ins)
  1197. {
  1198. level = ms_mspro_info->ms_mspro_get_ins();
  1199. }
  1200. else
  1201. {
  1202. ms_set_ins_input()
  1203. level = ms_get_ins_value();
  1204. }
  1205. if(level)
  1206. {
  1207. if(ms_mspro_info->init_retry)
  1208. {
  1209. ms_mspro_power_off(ms_mspro_info);
  1210. ms_mspro_info->init_retry = 0;
  1211. }
  1212. if(ms_mspro_info->inited_flag)
  1213. {
  1214. ms_mspro_power_off(ms_mspro_info);
  1215. ms_mspro_info->removed_flag = 1;
  1216. ms_mspro_info->inited_flag = 0;
  1217. }
  1218. return 0;
  1219. }
  1220. else
  1221. {
  1222. return 1;
  1223. }
  1224. }
  1225. #ifdef MS_MSPRO_HW_CONTROL
  1226. int ms_mspro_read_data_hw(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long lba, unsigned long byte_cnt, unsigned char * data_buf)
  1227. {
  1228. int error,tracer,i;
  1229. unsigned long sector_nums, data_offset;
  1230. unsigned short logical_no,physical_no,page_no,last_seg_no,last_logical_no,page_nums;
  1231. unsigned long pages_per_block;
  1232. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  1233. MS_MSPRO_TPC_Packet_t packet;
  1234. unsigned char* buf = ms_mspro_info->data_buf;
  1235. if(byte_cnt == 0)
  1236. {
  1237. error = MS_MSPRO_ERROR_PARAMETER;
  1238. #ifdef MS_MSPRO_DEBUG
  1239. Debug_Printf("#%s error occured in ms_mspro_read_data()\n", ms_mspro_error_to_string(error));
  1240. #endif
  1241. return error;
  1242. }
  1243. ms_sdio_enable(ms_mspro_info->io_pad_type);
  1244. if(ms_mspro_info->card_type == CARD_TYPE_MSPRO)
  1245. {
  1246. sector_nums = (byte_cnt+MSPRO_SECTOR_SIZE-1)/MSPRO_SECTOR_SIZE;
  1247. error = mspro_read_user_sector(ms_mspro_info, lba, sector_nums, data_buf);
  1248. tracer=0;
  1249. while (1)
  1250. {
  1251. if (error == 0)
  1252. break;
  1253. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  1254. packet.param.out.count = 1;
  1255. packet.param.out.buffer = buf;
  1256. packet.param.out.buffer[0] = CMD_MSPRO_STOP;
  1257. if (ms_mspro_packet_communicate(ms_mspro_info, &packet))
  1258. {
  1259. if (ms_mspro_packet_communicate(ms_mspro_info, &packet))
  1260. {
  1261. if (ms_mspro_packet_communicate(ms_mspro_info, &packet))
  1262. {
  1263. ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1264. }
  1265. }
  1266. }
  1267. if (mass_counter == 0)
  1268. tracer++;
  1269. else
  1270. tracer=0;
  1271. if (tracer>3)
  1272. break;
  1273. lba += mass_counter;
  1274. sector_nums -= mass_counter;
  1275. data_buf += MSPRO_SECTOR_SIZE*mass_counter;
  1276. error = mspro_read_user_sector(ms_mspro_info, lba, sector_nums, data_buf);
  1277. }
  1278. if(error)
  1279. {
  1280. #ifdef MS_MSPRO_DEBUG
  1281. Debug_Printf("#%s error occured in mspro_read_user_sector()\n", ms_mspro_error_to_string(error));
  1282. #endif
  1283. return error;
  1284. }
  1285. }
  1286. else
  1287. {
  1288. data_offset = 0;
  1289. page_nums = (byte_cnt+MS_PAGE_SIZE-1)/MS_PAGE_SIZE;
  1290. last_seg_no = ms_mspro_buf->ms.boot_attribute_information.Block_Numbers;
  1291. last_seg_no = last_seg_no / MS_BLOCKS_PER_SEGMENT - 1;
  1292. last_logical_no = (last_seg_no+1)*MS_LOGICAL_SIZE_PER_SEGMENT-2-1;
  1293. pages_per_block = ms_mspro_buf->ms.boot_attribute_information.Block_Size;
  1294. pages_per_block *= 2;
  1295. while(page_nums)
  1296. {
  1297. logical_no = lba / pages_per_block;
  1298. if(logical_no > last_logical_no)
  1299. {
  1300. error = MS_MSPRO_ERROR_PARAMETER;
  1301. #ifdef MS_MSPRO_DEBUG
  1302. Debug_Printf("#%s error occured in ms_mspro_read_data()\n", ms_mspro_error_to_string(error));
  1303. #endif
  1304. return error;
  1305. }
  1306. physical_no = ms_mspro_buf->ms.logical_physical_table[logical_no];
  1307. if(physical_no >= ms_mspro_buf->ms.boot_attribute_information.Block_Numbers)
  1308. {
  1309. error = MS_MSPRO_ERROR_PARAMETER;
  1310. #ifdef MS_MSPRO_DEBUG
  1311. Debug_Printf("#%s error occured in ms_mspro_read_data()\n", ms_mspro_error_to_string(error));
  1312. #endif
  1313. return error;
  1314. }
  1315. page_no = lba - logical_no*pages_per_block;
  1316. if(page_nums > (pages_per_block - page_no))
  1317. {
  1318. for(i=0; i<(pages_per_block - page_no); i++)
  1319. {
  1320. error = ms_read_page(ms_mspro_info, physical_no, page_no++, data_buf);
  1321. if(error)
  1322. {
  1323. #ifdef MS_MSPRO_DEBUG
  1324. Debug_Printf("#%s error occured in ms_read_block()\n", ms_mspro_error_to_string(error));
  1325. #endif
  1326. return error;
  1327. }
  1328. #ifdef AMLOGIC_CHIP_SUPPORT
  1329. data_buf += ((unsigned long)data_buf == 0x3400000) ? 0 : MS_PAGE_SIZE;
  1330. #else
  1331. data_buf += MS_PAGE_SIZE;
  1332. #endif
  1333. lba++;
  1334. page_nums--;
  1335. }
  1336. }
  1337. else
  1338. {
  1339. for(i=0; i<page_nums; i++)
  1340. {
  1341. error = ms_read_page(ms_mspro_info, physical_no, page_no++, data_buf);
  1342. if(error)
  1343. {
  1344. #ifdef MS_MSPRO_DEBUG
  1345. Debug_Printf("#%s error occured in ms_read_block()\n", ms_mspro_error_to_string(error));
  1346. #endif
  1347. return error;
  1348. }
  1349. #ifdef AMLOGIC_CHIP_SUPPORT
  1350. data_buf += ((unsigned long)data_buf == 0x3400000) ? 0 : MS_PAGE_SIZE;
  1351. #else
  1352. data_buf += MS_PAGE_SIZE;
  1353. #endif
  1354. lba++;
  1355. page_nums--;
  1356. }
  1357. }
  1358. }
  1359. }
  1360. return MS_MSPRO_NO_ERROR;
  1361. }
  1362. #endif
  1363. #ifdef MS_MSPRO_SW_CONTROL
  1364. //#define MS_ONESECTOR_PERREAD
  1365. int ms_mspro_read_data_sw(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long lba, unsigned long byte_cnt, unsigned char * data_buf)
  1366. {
  1367. int error;
  1368. int retry = 0;
  1369. unsigned long sector_nums, data_offset;
  1370. unsigned short logical_no,physical_no,page_no,last_seg_no,last_logical_no,page_nums;
  1371. unsigned long pages_per_block;
  1372. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  1373. if(byte_cnt == 0)
  1374. {
  1375. error = MS_MSPRO_ERROR_PARAMETER;
  1376. #ifdef MS_MSPRO_DEBUG
  1377. Debug_Printf("#%s error occured in ms_mspro_read_data()\n", ms_mspro_error_to_string(error));
  1378. #endif
  1379. return error;
  1380. }
  1381. ms_mspro_io_config(ms_mspro_info);
  1382. if(ms_mspro_info->card_type == CARD_TYPE_MSPRO)
  1383. {
  1384. sector_nums = (byte_cnt+MSPRO_SECTOR_SIZE-1)/MSPRO_SECTOR_SIZE;
  1385. #ifdef MS_ONESECTOR_PERREAD
  1386. int i;
  1387. for(i=0;i<sector_nums;i++){
  1388. do {
  1389. error = mspro_read_user_sector(ms_mspro_info, lba+i, 1, data_buf+(MSPRO_SECTOR_SIZE*i));
  1390. } while(error && retry++<16);
  1391. if(error)
  1392. break;
  1393. }
  1394. #else
  1395. do {
  1396. error = mspro_read_user_sector(ms_mspro_info, lba, sector_nums, data_buf);
  1397. } while(error && retry++<16);
  1398. #endif
  1399. if(error)
  1400. {
  1401. #ifdef MS_MSPRO_DEBUG
  1402. Debug_Printf("#%s error occured in mspro_read_user_sector()\n", ms_mspro_error_to_string(error));
  1403. #endif
  1404. return error;
  1405. }
  1406. }
  1407. else
  1408. {
  1409. data_offset = 0;
  1410. page_nums = (byte_cnt+MS_PAGE_SIZE-1)/MS_PAGE_SIZE;
  1411. last_seg_no = ms_mspro_buf->ms.boot_attribute_information.Block_Numbers;
  1412. last_seg_no = last_seg_no / MS_BLOCKS_PER_SEGMENT - 1;
  1413. last_logical_no = (last_seg_no+1)*MS_LOGICAL_SIZE_PER_SEGMENT-2-1;
  1414. pages_per_block = ms_mspro_buf->ms.boot_attribute_information.Block_Size;
  1415. pages_per_block *= 2;
  1416. while(page_nums)
  1417. {
  1418. logical_no = lba / pages_per_block;
  1419. if(logical_no > last_logical_no)
  1420. {
  1421. error = MS_MSPRO_ERROR_PARAMETER;
  1422. #ifdef MS_MSPRO_DEBUG
  1423. Debug_Printf("#%s error occured in ms_mspro_read_data()\n", ms_mspro_error_to_string(error));
  1424. #endif
  1425. return error;
  1426. }
  1427. physical_no = ms_mspro_buf->ms.logical_physical_table[logical_no];
  1428. if(physical_no >= ms_mspro_buf->ms.boot_attribute_information.Block_Numbers)
  1429. {
  1430. error = MS_MSPRO_ERROR_PARAMETER;
  1431. #ifdef MS_MSPRO_DEBUG
  1432. Debug_Printf("#%s error occured in ms_mspro_read_data()\n", ms_mspro_error_to_string(error));
  1433. #endif
  1434. return error;
  1435. }
  1436. page_no = lba - logical_no*pages_per_block;
  1437. if(page_nums > (pages_per_block - page_no))
  1438. {
  1439. error = ms_read_block(ms_mspro_info, physical_no, page_no, (pages_per_block - page_no), data_buf+data_offset);
  1440. if(error)
  1441. {
  1442. #ifdef MS_MSPRO_DEBUG
  1443. Debug_Printf("#%s error occured in ms_read_block()\n", ms_mspro_error_to_string(error));
  1444. #endif
  1445. return error;
  1446. }
  1447. #ifdef AMLOGIC_CHIP_SUPPORT
  1448. data_offset += ((unsigned long)data_buf == 0x3400000) ? 0 : (pages_per_block - page_no)*MS_PAGE_SIZE;
  1449. #else
  1450. data_offset += (pages_per_block - page_no)*MS_PAGE_SIZE;
  1451. #endif
  1452. lba += (pages_per_block - page_no);
  1453. page_nums -= (pages_per_block - page_no);
  1454. }
  1455. else
  1456. {
  1457. error = ms_read_block(ms_mspro_info, physical_no, page_no, page_nums, data_buf+data_offset);
  1458. if(error)
  1459. {
  1460. #ifdef MS_MSPRO_DEBUG
  1461. Debug_Printf("#%s error occured in ms_read_block()\n", ms_mspro_error_to_string(error));
  1462. #endif
  1463. return error;
  1464. }
  1465. #ifdef AMLOGIC_CHIP_SUPPORT
  1466. data_offset += ((unsigned long)data_buf == 0x3400000) ? 0 : page_nums*MS_PAGE_SIZE;
  1467. #else
  1468. data_offset += page_nums*MS_PAGE_SIZE;
  1469. #endif
  1470. lba += page_nums;
  1471. page_nums -= page_nums;
  1472. }
  1473. }
  1474. }
  1475. return MS_MSPRO_NO_ERROR;
  1476. }
  1477. #endif
  1478. #ifdef MS_MSPRO_HW_CONTROL
  1479. int ms_mspro_write_data_hw(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long lba, unsigned long byte_cnt, unsigned char * data_buf)
  1480. {
  1481. int error,tracer,i,j,k;
  1482. unsigned long sector_nums, data_offset;
  1483. unsigned short logical_no,physical_no,page_no,seg_no,last_seg_no,last_logical_no,page_nums,free_blk_no;
  1484. unsigned long pages_per_block,free_table_index;
  1485. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  1486. MS_MSPRO_TPC_Packet_t packet;
  1487. unsigned char* buf = ms_mspro_info->data_buf;
  1488. if(byte_cnt == 0)
  1489. {
  1490. error = MS_MSPRO_ERROR_PARAMETER;
  1491. #ifdef MS_MSPRO_DEBUG
  1492. Debug_Printf("#%s error occured in ms_mspro_write_data()\n", ms_mspro_error_to_string(error));
  1493. #endif
  1494. return error;
  1495. }
  1496. if(ms_mspro_info->write_protected_flag)
  1497. {
  1498. error = MS_MSPRO_ERROR_WRITE_PROTECTED;
  1499. #ifdef MS_MSPRO_DEBUG
  1500. Debug_Printf("#%s error occured in ms_mspro_write_data()\n", ms_mspro_error_to_string(error));
  1501. #endif
  1502. return error;
  1503. }
  1504. if(ms_mspro_info->read_only_flag)
  1505. {
  1506. error = MS_MSPRO_ERROR_READ_ONLY;
  1507. #ifdef MS_MSPRO_DEBUG
  1508. Debug_Printf("#%s error occured in ms_mspro_write_data()\n", ms_mspro_error_to_string(error));
  1509. #endif
  1510. return error;
  1511. }
  1512. ms_sdio_enable(ms_mspro_info->io_pad_type);
  1513. if(ms_mspro_info->card_type == CARD_TYPE_MSPRO)
  1514. {
  1515. sector_nums = (byte_cnt+MSPRO_SECTOR_SIZE-1)/MSPRO_SECTOR_SIZE;
  1516. error = mspro_write_user_sector(ms_mspro_info, lba, sector_nums, data_buf);
  1517. tracer=0;
  1518. while (1)
  1519. {
  1520. if (error == 0)
  1521. break;
  1522. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_CMD; //SET_CMD
  1523. packet.param.out.count = 1;
  1524. packet.param.out.buffer = buf;
  1525. packet.param.out.buffer[0] = CMD_MSPRO_STOP;
  1526. if (ms_mspro_packet_communicate(ms_mspro_info, &packet))
  1527. {
  1528. if (ms_mspro_packet_communicate(ms_mspro_info, &packet))
  1529. {
  1530. if (ms_mspro_packet_communicate(ms_mspro_info, &packet))
  1531. {
  1532. ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1533. }
  1534. }
  1535. }
  1536. if (mass_counter == 0)
  1537. tracer++;
  1538. else
  1539. tracer=0;
  1540. if (tracer>3)
  1541. break;
  1542. lba += mass_counter;
  1543. sector_nums -= mass_counter;
  1544. data_buf += MSPRO_SECTOR_SIZE*mass_counter;
  1545. error = mspro_write_user_sector(ms_mspro_info, lba, sector_nums, data_buf);
  1546. }
  1547. if(error)
  1548. {
  1549. #ifdef MS_MSPRO_DEBUG
  1550. Debug_Printf("#%s error occured in mspro_write_user_sector()\n", ms_mspro_error_to_string(error));
  1551. #endif
  1552. return error;
  1553. }
  1554. }
  1555. else
  1556. {
  1557. data_offset = 0;
  1558. page_nums = (byte_cnt+MS_PAGE_SIZE-1)/MS_PAGE_SIZE;
  1559. last_seg_no = ms_mspro_buf->ms.boot_attribute_information.Block_Numbers;
  1560. last_seg_no = last_seg_no / MS_BLOCKS_PER_SEGMENT - 1;
  1561. last_logical_no = (last_seg_no+1)*MS_LOGICAL_SIZE_PER_SEGMENT-2-1;
  1562. pages_per_block = ms_mspro_buf->ms.boot_attribute_information.Block_Size;
  1563. pages_per_block *= 2;
  1564. while(page_nums)
  1565. {
  1566. logical_no = lba / pages_per_block;
  1567. if(logical_no > last_logical_no)
  1568. {
  1569. error = MS_MSPRO_ERROR_PARAMETER;
  1570. #ifdef MS_MSPRO_DEBUG
  1571. Debug_Printf("#%s error occured in ms_mspro_write_data()\n", ms_mspro_error_to_string(error));
  1572. #endif
  1573. return error;
  1574. }
  1575. physical_no = ms_mspro_buf->ms.logical_physical_table[logical_no];
  1576. if(physical_no >= ms_mspro_buf->ms.boot_attribute_information.Block_Numbers)
  1577. {
  1578. error = MS_MSPRO_ERROR_PARAMETER;
  1579. #ifdef MS_MSPRO_DEBUG
  1580. Debug_Printf("#%s error occured in ms_mspro_write_data()\n", ms_mspro_error_to_string(error));
  1581. #endif
  1582. return error;
  1583. }
  1584. page_no = lba - logical_no*pages_per_block;
  1585. seg_no = physical_no / MS_BLOCKS_PER_SEGMENT;
  1586. for(i=0; i<MS_MAX_SEGMENT_NUMBERS; i++)
  1587. {
  1588. free_table_index = seg_no*MS_MAX_FREE_BLOCKS_PER_SEGMENT+i;
  1589. free_blk_no = ms_mspro_buf->ms.free_block_table[free_table_index];
  1590. if(free_blk_no != 0xFFFF)
  1591. {
  1592. error = ms_read_extra_data(ms_mspro_info, free_blk_no, 0);
  1593. if(error)
  1594. continue;
  1595. if(((MS_Overwrite_Flag_Register_t *)&ms_mspro_buf->ms.regs.Overwrite_Flag_Reg)->UDST)
  1596. break;
  1597. }
  1598. }
  1599. if(i >= MS_MAX_SEGMENT_NUMBERS)
  1600. {
  1601. error = MS_ERROR_NO_FREE_BLOCK;
  1602. #ifdef MS_MSPRO_DEBUG
  1603. Debug_Printf("#%s error occured in ms_mspro_write_data()\n", ms_mspro_error_to_string(error));
  1604. #endif
  1605. return error;
  1606. }
  1607. error = ms_erase_block(ms_mspro_info, free_blk_no);
  1608. if(error)
  1609. {
  1610. #ifdef MS_MSPRO_DEBUG
  1611. Debug_Printf("#%s error occured in ms_erase_block()\n", ms_mspro_error_to_string(error));
  1612. #endif
  1613. return error;
  1614. }
  1615. for(j=0; j<page_no; j++)
  1616. {
  1617. error = ms_copy_page(ms_mspro_info, physical_no, j, free_blk_no, j, ms_mspro_info->data_buf);
  1618. if(error)
  1619. {
  1620. #ifdef MS_MSPRO_DEBUG
  1621. Debug_Printf("#%s error occured in ms_copy_page()\n", ms_mspro_error_to_string(error));
  1622. #endif
  1623. return error;
  1624. }
  1625. }
  1626. ms_mspro_buf->ms.regs.Logical_Address_Reg1 = (logical_no>>8) & 0xFF;
  1627. ms_mspro_buf->ms.regs.Logical_Address_Reg0 = logical_no & 0xFF;
  1628. if(page_nums > (pages_per_block - page_no))
  1629. {
  1630. for(i=page_no; i<pages_per_block; i++)
  1631. {
  1632. error = ms_write_page(ms_mspro_info, free_blk_no, i, data_buf+data_offset);
  1633. if(error)
  1634. {
  1635. #ifdef MS_MSPRO_DEBUG
  1636. Debug_Printf("#%s error occured in ms_write_page()\n", ms_mspro_error_to_string(error));
  1637. #endif
  1638. return error;
  1639. }
  1640. data_offset += MS_PAGE_SIZE;
  1641. }
  1642. // error = ms_write_block(free_blk_no, page_no, (pages_per_block - page_no), data_buf+data_offset);
  1643. // if(error)
  1644. // {
  1645. //#ifdef MS_MSPRO_DEBUG
  1646. // Debug_Printf("#%s error occured in ms_write_block()\n", ms_mspro_error_to_string(error));
  1647. //#endif
  1648. // return error;
  1649. // }
  1650. //data_offset += (pages_per_block - page_no)*MS_PAGE_SIZE;
  1651. lba += (pages_per_block - page_no);
  1652. page_nums -= (pages_per_block - page_no);
  1653. }
  1654. else
  1655. {
  1656. for(i=page_no; i<(page_no+page_nums); i++)
  1657. {
  1658. error = ms_write_page(ms_mspro_info, free_blk_no, i, data_buf+data_offset);
  1659. if(error)
  1660. {
  1661. #ifdef MS_MSPRO_DEBUG
  1662. Debug_Printf("#%s error occured in ms_write_page()\n", ms_mspro_error_to_string(error));
  1663. #endif
  1664. return error;
  1665. }
  1666. data_offset += MS_PAGE_SIZE;
  1667. }
  1668. // error = ms_write_block(free_blk_no, page_no, page_nums, data_buf+data_offset);
  1669. // if(error)
  1670. // {
  1671. //#ifdef MS_MSPRO_DEBUG
  1672. // Debug_Printf("#%s error occured in ms_write_block()\n", ms_mspro_error_to_string(error));
  1673. //#endif
  1674. // return error;
  1675. // }
  1676. //data_offset += page_nums*MS_PAGE_SIZE;
  1677. for(k=(page_no+page_nums); k<pages_per_block; k++)
  1678. {
  1679. error = ms_copy_page(ms_mspro_info, physical_no, k, free_blk_no, k, ms_mspro_info->data_buf);
  1680. if(error)
  1681. {
  1682. #ifdef MS_MSPRO_DEBUG
  1683. Debug_Printf("#%s error occured in ms_copy_page()\n", ms_mspro_error_to_string(error));
  1684. #endif
  1685. return error;
  1686. }
  1687. }
  1688. lba += page_nums;
  1689. page_nums -= page_nums;
  1690. }
  1691. ms_mspro_buf->ms.free_block_table[free_table_index] = physical_no;
  1692. ms_mspro_buf->ms.logical_physical_table[logical_no] = free_blk_no;
  1693. //error = ms_erase_block(physical_no);
  1694. //if(error)
  1695. //{
  1696. //#ifdef MS_MSPRO_DEBUG
  1697. // Debug_Printf("#%s error occured in ms_erase_block()\n", ms_mspro_error_to_string(error));
  1698. //#endif
  1699. // return error;
  1700. //}
  1701. }
  1702. }
  1703. return MS_MSPRO_NO_ERROR;
  1704. }
  1705. #endif
  1706. #ifdef MS_MSPRO_SW_CONTROL
  1707. int ms_mspro_write_data_sw(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long lba, unsigned long byte_cnt, unsigned char * data_buf)
  1708. {
  1709. int error,i,j,k;
  1710. unsigned long sector_nums, data_offset;
  1711. unsigned short logical_no,physical_no,page_no,seg_no,last_seg_no,last_logical_no,page_nums,free_blk_no;
  1712. unsigned long pages_per_block,free_table_index;
  1713. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  1714. if(byte_cnt == 0)
  1715. {
  1716. error = MS_MSPRO_ERROR_PARAMETER;
  1717. #ifdef MS_MSPRO_DEBUG
  1718. Debug_Printf("#%s error occured in ms_mspro_write_data()\n", ms_mspro_error_to_string(error));
  1719. #endif
  1720. return error;
  1721. }
  1722. if(ms_mspro_info->write_protected_flag)
  1723. {
  1724. error = MS_MSPRO_ERROR_WRITE_PROTECTED;
  1725. #ifdef MS_MSPRO_DEBUG
  1726. Debug_Printf("#%s error occured in ms_mspro_write_data()\n", ms_mspro_error_to_string(error));
  1727. #endif
  1728. return error;
  1729. }
  1730. if(ms_mspro_info->read_only_flag)
  1731. {
  1732. error = MS_MSPRO_ERROR_READ_ONLY;
  1733. #ifdef MS_MSPRO_DEBUG
  1734. Debug_Printf("#%s error occured in ms_mspro_write_data()\n", ms_mspro_error_to_string(error));
  1735. #endif
  1736. return error;
  1737. }
  1738. ms_mspro_io_config(ms_mspro_info);
  1739. if(ms_mspro_info->card_type == CARD_TYPE_MSPRO)
  1740. {
  1741. sector_nums = (byte_cnt+MSPRO_SECTOR_SIZE-1)/MSPRO_SECTOR_SIZE;
  1742. //error = mspro_erase_user_sector(lba, sector_nums);
  1743. //if(error)
  1744. // return error;
  1745. error = mspro_write_user_sector(ms_mspro_info, lba, sector_nums, data_buf);
  1746. if(error)
  1747. {
  1748. #ifdef MS_MSPRO_DEBUG
  1749. Debug_Printf("#%s error occured in mspro_write_user_sector()\n", ms_mspro_error_to_string(error));
  1750. #endif
  1751. return error;
  1752. }
  1753. }
  1754. else
  1755. {
  1756. data_offset = 0;
  1757. page_nums = (byte_cnt+MS_PAGE_SIZE-1)/MS_PAGE_SIZE;
  1758. last_seg_no = ms_mspro_buf->ms.boot_attribute_information.Block_Numbers;
  1759. last_seg_no = last_seg_no / MS_BLOCKS_PER_SEGMENT - 1;
  1760. last_logical_no = (last_seg_no+1)*MS_LOGICAL_SIZE_PER_SEGMENT-2-1;
  1761. pages_per_block = ms_mspro_buf->ms.boot_attribute_information.Block_Size;
  1762. pages_per_block *= 2;
  1763. while(page_nums)
  1764. {
  1765. logical_no = lba / pages_per_block;
  1766. if(logical_no > last_logical_no)
  1767. {
  1768. error = MS_MSPRO_ERROR_PARAMETER;
  1769. #ifdef MS_MSPRO_DEBUG
  1770. Debug_Printf("#%s error occured in ms_mspro_write_data()\n", ms_mspro_error_to_string(error));
  1771. #endif
  1772. return error;
  1773. }
  1774. physical_no = ms_mspro_buf->ms.logical_physical_table[logical_no];
  1775. if(physical_no >= ms_mspro_buf->ms.boot_attribute_information.Block_Numbers)
  1776. {
  1777. error = MS_MSPRO_ERROR_PARAMETER;
  1778. #ifdef MS_MSPRO_DEBUG
  1779. Debug_Printf("#%s error occured in ms_mspro_write_data()\n", ms_mspro_error_to_string(error));
  1780. #endif
  1781. return error;
  1782. }
  1783. page_no = lba - logical_no*pages_per_block;
  1784. seg_no = physical_no / MS_BLOCKS_PER_SEGMENT;
  1785. for(i=0; i<MS_MAX_SEGMENT_NUMBERS; i++)
  1786. {
  1787. free_table_index = seg_no*MS_MAX_FREE_BLOCKS_PER_SEGMENT+i;
  1788. free_blk_no = ms_mspro_buf->ms.free_block_table[free_table_index];
  1789. if(free_blk_no != 0xFFFF)
  1790. {
  1791. error = ms_read_extra_data(ms_mspro_info, free_blk_no, 0);
  1792. if(error)
  1793. continue;
  1794. if(((MS_Overwrite_Flag_Register_t *)&ms_mspro_buf->ms.regs.Overwrite_Flag_Reg)->UDST)
  1795. break;
  1796. }
  1797. }
  1798. if(i >= MS_MAX_SEGMENT_NUMBERS)
  1799. {
  1800. error = MS_ERROR_NO_FREE_BLOCK;
  1801. #ifdef MS_MSPRO_DEBUG
  1802. Debug_Printf("#%s error occured in ms_mspro_write_data()\n", ms_mspro_error_to_string(error));
  1803. #endif
  1804. return error;
  1805. }
  1806. error = ms_erase_block(ms_mspro_info, free_blk_no);
  1807. if(error)
  1808. {
  1809. #ifdef MS_MSPRO_DEBUG
  1810. Debug_Printf("#%s error occured in ms_erase_block()\n", ms_mspro_error_to_string(error));
  1811. #endif
  1812. return error;
  1813. }
  1814. for(j=0; j<page_no; j++)
  1815. {
  1816. error = ms_copy_page(ms_mspro_info, physical_no, j, free_blk_no, j, ms_mspro_info->data_buf);
  1817. if(error)
  1818. {
  1819. #ifdef MS_MSPRO_DEBUG
  1820. Debug_Printf("#%s error occured in ms_copy_page()\n", ms_mspro_error_to_string(error));
  1821. #endif
  1822. return error;
  1823. }
  1824. }
  1825. ms_mspro_buf->ms.regs.Logical_Address_Reg1 = (logical_no>>8) & 0xFF;
  1826. ms_mspro_buf->ms.regs.Logical_Address_Reg0 = logical_no & 0xFF;
  1827. if(page_nums > (pages_per_block - page_no))
  1828. {
  1829. //for(int i=page_no; i<pages_per_block; i++)
  1830. //{
  1831. // error = ms_write_page(free_blk_no, i, data_buf+data_offset);
  1832. // if(error)
  1833. // {
  1834. //#ifdef MS_MSPRO_DEBUG
  1835. // Debug_Printf("#%s error occured in ms_write_page()\n", ms_mspro_error_to_string(error));
  1836. //#endif
  1837. // return error;
  1838. // }
  1839. //
  1840. // data_offset += MS_PAGE_SIZE;
  1841. //}
  1842. error = ms_write_block(ms_mspro_info, free_blk_no, page_no, (pages_per_block - page_no), data_buf+data_offset);
  1843. if(error)
  1844. {
  1845. #ifdef MS_MSPRO_DEBUG
  1846. Debug_Printf("#%s error occured in ms_write_block()\n", ms_mspro_error_to_string(error));
  1847. #endif
  1848. return error;
  1849. }
  1850. data_offset += (pages_per_block - page_no)*MS_PAGE_SIZE;
  1851. lba += (pages_per_block - page_no);
  1852. page_nums -= (pages_per_block - page_no);
  1853. }
  1854. else
  1855. {
  1856. //for(int i=page_no; i<(page_no+page_nums); i++)
  1857. //{
  1858. // error = ms_write_page(free_blk_no, i, data_buf+data_offset);
  1859. // if(error)
  1860. // {
  1861. //#ifdef MS_MSPRO_DEBUG
  1862. // Debug_Printf("#%s error occured in ms_write_page()\n", ms_mspro_error_to_string(error));
  1863. //#endif
  1864. // return error;
  1865. // }
  1866. //
  1867. // data_offset += MS_PAGE_SIZE;
  1868. //}
  1869. error = ms_write_block(ms_mspro_info, free_blk_no, page_no, page_nums, data_buf+data_offset);
  1870. if(error)
  1871. {
  1872. #ifdef MS_MSPRO_DEBUG
  1873. Debug_Printf("#%s error occured in ms_write_block()\n", ms_mspro_error_to_string(error));
  1874. #endif
  1875. return error;
  1876. }
  1877. data_offset += page_nums*MS_PAGE_SIZE;
  1878. for(k=(page_no+page_nums); k<pages_per_block; k++)
  1879. {
  1880. error = ms_copy_page(ms_mspro_info, physical_no, k, free_blk_no, k, ms_mspro_info->data_buf);
  1881. if(error)
  1882. {
  1883. #ifdef MS_MSPRO_DEBUG
  1884. Debug_Printf("#%s error occured in ms_copy_page()\n", ms_mspro_error_to_string(error));
  1885. #endif
  1886. return error;
  1887. }
  1888. }
  1889. lba += page_nums;
  1890. page_nums -= page_nums;
  1891. }
  1892. ms_mspro_buf->ms.free_block_table[free_table_index] = physical_no;
  1893. ms_mspro_buf->ms.logical_physical_table[logical_no] = free_blk_no;
  1894. //error = ms_erase_block(physical_no);
  1895. //if(error)
  1896. //{
  1897. //#ifdef MS_MSPRO_DEBUG
  1898. // Debug_Printf("#%s error occured in ms_erase_block()\n", ms_mspro_error_to_string(error));
  1899. //#endif
  1900. // return error;
  1901. //}
  1902. }
  1903. }
  1904. return MS_MSPRO_NO_ERROR;
  1905. }
  1906. #endif
  1907. void ms_mspro_io_config(MS_MSPRO_Card_Info_t *ms_mspro_info)
  1908. {
  1909. int i;
  1910. ms_gpio_enable(ms_mspro_info->io_pad_type);
  1911. ms_set_clk_output();
  1912. ms_set_clk_high();
  1913. ms_set_dat0_3_input();
  1914. ms_set_bs_output();
  1915. ms_set_bs_state(0);
  1916. for(i=0; i<10; i++)
  1917. {
  1918. ms_clk_serial_low();
  1919. ms_clk_serial_high();
  1920. }
  1921. }
  1922. int ms_mspro_staff_init(MS_MSPRO_Card_Info_t *ms_mspro_info)
  1923. {
  1924. unsigned long sdio_config;
  1925. unsigned long sdio_multi_config;
  1926. SDIO_Multi_Config_Reg_t *multi_config_reg;
  1927. SDIO_Config_Reg_t *config_reg;
  1928. ms_mspro_prepare_power(ms_mspro_info);
  1929. ms_mspro_power_on(ms_mspro_info);
  1930. #ifdef MS_MSPRO_HW_CONTROL
  1931. if(MS_WORK_MODE == CARD_HW_MODE)
  1932. {
  1933. sdio_multi_config = (READ_CBUS_REG(SDIO_MULT_CONFIG) & 0x00000003);
  1934. multi_config_reg = (void *)&sdio_multi_config;
  1935. multi_config_reg->ms_enable = 1;
  1936. multi_config_reg->ms_sclk_always = 1;
  1937. WRITE_CBUS_REG(SDIO_MULT_CONFIG, sdio_multi_config);
  1938. sdio_config = 0;
  1939. config_reg = (void *)&sdio_config;
  1940. config_reg->cmd_clk_divide = 3;//aml_system_clk / (2*MS_MSPRO_TRANSFER_SLOWER_CLK) -1;
  1941. config_reg->m_endian = 3;
  1942. WRITE_CBUS_REG(SDIO_CONFIG, sdio_config);
  1943. ms_mspro_info->ms_clk_unit = 1000/MS_MSPRO_TRANSFER_CLK;
  1944. ms_sdio_enable(ms_mspro_info->io_pad_type);
  1945. }
  1946. #endif
  1947. #ifdef MS_MSPRO_SW_CONTROL
  1948. if(MS_WORK_MODE == CARD_SW_MODE)
  1949. ms_mspro_io_config(ms_mspro_info);
  1950. #endif
  1951. ms_mspro_info->card_type = CARD_NONE_TYPE;
  1952. ms_mspro_info->media_type = MEMORY_STICK_ERROR;
  1953. ms_mspro_info->interface_mode = INTERFACE_SERIAL;
  1954. ms_mspro_info->blk_len = 0;
  1955. ms_mspro_info->blk_nums = 0;
  1956. ms_mspro_info->write_protected_flag = 0;
  1957. ms_mspro_info->read_only_flag = 0;
  1958. ms_mspro_info->inited_flag = 0;
  1959. ms_mspro_info->removed_flag = 0;
  1960. return MS_MSPRO_NO_ERROR;
  1961. }
  1962. int ms_mspro_cmd_test(MS_MSPRO_Card_Info_t *ms_mspro_info)
  1963. {
  1964. MS_MSPRO_TPC_Packet_t packet;
  1965. int error;
  1966. MS_MSPRO_Card_Buffer_t *ms_mspro_buf = (MS_MSPRO_Card_Buffer_t *)(ms_mspro_info->ms_mspro_buf);
  1967. packet.TPC_cmd.value = TPC_MS_MSPRO_SET_RW_REG_ADRS;
  1968. packet.param.out.count = 4; //READ_ADRS,READ_SIZE,WRITE_ADRS,WRITE_SIZE
  1969. ms_mspro_buf->mspro.reg_set.write_addr = 0x11;
  1970. ms_mspro_buf->mspro.reg_set.write_size = 0x06;
  1971. packet.param.out.buffer = (unsigned char *)&ms_mspro_buf->mspro.reg_set;
  1972. error = ms_mspro_packet_communicate(ms_mspro_info, &packet);
  1973. return error;
  1974. }
  1975. void ms_mspro_power_on(MS_MSPRO_Card_Info_t *ms_mspro_info)
  1976. {
  1977. ms_delay_ms(ms_mspro_power_delay+1);
  1978. #ifdef MS_MSPRO_POWER_CONTROL
  1979. if(ms_mspro_info->ms_mspro_power)
  1980. {
  1981. ms_mspro_info->ms_mspro_power(0);
  1982. }
  1983. else
  1984. {
  1985. ms_set_disable();
  1986. }
  1987. ms_delay_ms(200);
  1988. if(ms_mspro_info->ms_mspro_power)
  1989. {
  1990. if(ms_mspro_check_insert(ms_mspro_info)) //ensure card wasn't removed at this time
  1991. {
  1992. ms_mspro_info->ms_mspro_power(1);
  1993. }
  1994. }
  1995. else
  1996. {
  1997. if(ms_mspro_check_insert(ms_mspro_info)) //ensure card wasn't removed at this time
  1998. {
  1999. ms_set_enable();
  2000. }
  2001. }
  2002. ms_delay_ms(200);
  2003. #else
  2004. ms_delay_ms(200);
  2005. #endif
  2006. }
  2007. void ms_mspro_power_off(MS_MSPRO_Card_Info_t *ms_mspro_info)
  2008. {
  2009. #ifdef MS_MSPRO_POWER_CONTROL
  2010. if(ms_mspro_info->ms_mspro_power)
  2011. {
  2012. ms_mspro_info->ms_mspro_power(0);
  2013. }
  2014. else
  2015. {
  2016. ms_set_disable();
  2017. }
  2018. #endif
  2019. }
  2020. //check data lines consistency
  2021. int ms_mspro_check_data_consistency(MS_MSPRO_Card_Info_t *ms_mspro_info)
  2022. {
  2023. int error;
  2024. unsigned char *mbr_buf = ms_mspro_info->data_buf;
  2025. //This card is working in parallel bus mode!
  2026. memset(mbr_buf, 0, ms_mspro_info->blk_len*2);
  2027. if(ms_mspro_info->interface_mode == INTERFACE_PARALLEL)
  2028. {
  2029. //read MBR information
  2030. error = ms_mspro_read_data(ms_mspro_info, 0, ms_mspro_info->blk_len*2, mbr_buf);
  2031. if(error)
  2032. {
  2033. //error! retry again!
  2034. error = ms_mspro_read_data(ms_mspro_info, 0, ms_mspro_info->blk_len, mbr_buf);
  2035. if(error)
  2036. return error;
  2037. }
  2038. error = ms_mspro_read_data(ms_mspro_info, 0, ms_mspro_info->blk_len*2, mbr_buf);
  2039. if(error)
  2040. {
  2041. //error! retry again!
  2042. error = ms_mspro_read_data(ms_mspro_info, 0, ms_mspro_info->blk_len, mbr_buf);
  2043. if(error)
  2044. return error;
  2045. }
  2046. //check MBR data consistency
  2047. if((mbr_buf[510] != 0x55) || (mbr_buf[511] != 0xAA))
  2048. {
  2049. //data consistency error! retry again!
  2050. error = ms_mspro_read_data(ms_mspro_info, 0, ms_mspro_info->blk_len, mbr_buf);
  2051. if(error)
  2052. return error;
  2053. //check MBR data consistency
  2054. if((mbr_buf[510] != 0x55) || (mbr_buf[511] != 0xAA))
  2055. {
  2056. #ifdef MS_MSPRO_DEBUG
  2057. Debug_Printf("MS/MSPRO data consistency error in Parallel I/F mode! Try Serial I/F mode...\n");
  2058. #endif
  2059. //error again! retry serial bus mode!
  2060. error = ms_mspro_interface_mode_switching(ms_mspro_info, INTERFACE_SERIAL);
  2061. if(error)
  2062. return error;
  2063. }
  2064. else
  2065. {
  2066. return MS_MSPRO_NO_ERROR;
  2067. }
  2068. }
  2069. else
  2070. {
  2071. return MS_MSPRO_NO_ERROR;
  2072. }
  2073. }
  2074. //This card is working in serial bus mode!
  2075. memset(mbr_buf, 0, ms_mspro_info->blk_len);
  2076. //read MBR information
  2077. error = ms_mspro_read_data(ms_mspro_info, 0, ms_mspro_info->blk_len, mbr_buf);
  2078. if(error)
  2079. {
  2080. //error! retry again!
  2081. error = ms_mspro_read_data(ms_mspro_info, 0, ms_mspro_info->blk_len, mbr_buf);
  2082. if(error)
  2083. return error;
  2084. }
  2085. //check MBR data consistency
  2086. if((mbr_buf[510] != 0x55) || (mbr_buf[511] != 0xAA))
  2087. {
  2088. //data consistency error! retry again!
  2089. error = ms_mspro_read_data(ms_mspro_info, 0, ms_mspro_info->blk_len, mbr_buf);
  2090. if(error)
  2091. return error;
  2092. //check MBR data consistency
  2093. if((mbr_buf[510] != 0x55) || (mbr_buf[511] != 0xAA))
  2094. {
  2095. return MS_MSPRO_ERROR_DATA_CRC;
  2096. }
  2097. }
  2098. return MS_MSPRO_NO_ERROR;
  2099. }
  2100. int ms_mspro_packet_communicate(MS_MSPRO_Card_Info_t *ms_mspro_info, MS_MSPRO_TPC_Packet_t * tpc_packet)
  2101. {
  2102. int error = 0;
  2103. #ifdef MS_MSPRO_HW_CONTROL
  2104. if(MS_WORK_MODE == CARD_HW_MODE)
  2105. error = ms_mspro_packet_communicate_hw(ms_mspro_info, tpc_packet);
  2106. #endif
  2107. #ifdef MS_MSPRO_SW_CONTROL
  2108. if(MS_WORK_MODE == CARD_SW_MODE)
  2109. error = ms_mspro_packet_communicate_sw(ms_mspro_info, tpc_packet);
  2110. #endif
  2111. return error;
  2112. }
  2113. int ms_mspro_read_data(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long lba, unsigned long byte_cnt, unsigned char * data_buf)
  2114. {
  2115. int error = 0;
  2116. #ifdef MS_MSPRO_HW_CONTROL
  2117. if(MS_WORK_MODE == CARD_HW_MODE)
  2118. {
  2119. if(ms_save_hw_reg_flag)
  2120. {
  2121. WRITE_CBUS_REG(SDIO_CONFIG, ms_save_hw_io_config);
  2122. WRITE_CBUS_REG(SDIO_MULT_CONFIG, ms_save_hw_io_mult_config);
  2123. }
  2124. error = ms_mspro_read_data_hw(ms_mspro_info, lba, byte_cnt, data_buf);
  2125. }
  2126. #endif
  2127. #ifdef MS_MSPRO_SW_CONTROL
  2128. if(MS_WORK_MODE == CARD_SW_MODE)
  2129. error = ms_mspro_read_data_sw(ms_mspro_info, lba, byte_cnt, data_buf);
  2130. #endif
  2131. return error;
  2132. }
  2133. int ms_mspro_wait_int(MS_MSPRO_Card_Info_t *ms_mspro_info, MS_MSPRO_TPC_Packet_t * tpc_packet)
  2134. {
  2135. int error = 0;
  2136. #ifdef MS_MSPRO_HW_CONTROL
  2137. if(MS_WORK_MODE == CARD_HW_MODE)
  2138. {
  2139. if(ms_save_hw_reg_flag)
  2140. {
  2141. WRITE_CBUS_REG(SDIO_CONFIG, ms_save_hw_io_config);
  2142. WRITE_CBUS_REG(SDIO_MULT_CONFIG, ms_save_hw_io_mult_config);
  2143. }
  2144. error = ms_mspro_wait_int_hw(ms_mspro_info, tpc_packet);
  2145. }
  2146. #endif
  2147. #ifdef MS_MSPRO_SW_CONTROL
  2148. if(MS_WORK_MODE == CARD_SW_MODE)
  2149. error = ms_mspro_wait_int_sw(ms_mspro_info, tpc_packet);
  2150. #endif
  2151. return error;
  2152. }
  2153. int ms_mspro_write_data(MS_MSPRO_Card_Info_t *ms_mspro_info, unsigned long lba, unsigned long byte_cnt, unsigned char * data_buf)
  2154. {
  2155. int error = 0;
  2156. #ifdef MS_MSPRO_HW_CONTROL
  2157. if(MS_WORK_MODE == CARD_HW_MODE)
  2158. {
  2159. if(ms_save_hw_reg_flag)
  2160. {
  2161. WRITE_CBUS_REG(SDIO_CONFIG, ms_save_hw_io_config);
  2162. WRITE_CBUS_REG(SDIO_MULT_CONFIG, ms_save_hw_io_mult_config);
  2163. }
  2164. error = ms_mspro_write_data_hw(ms_mspro_info, lba, byte_cnt, data_buf);
  2165. }
  2166. #endif
  2167. #ifdef MS_MSPRO_SW_CONTROL
  2168. if(MS_WORK_MODE == CARD_SW_MODE)
  2169. {
  2170. #ifdef AVOS
  2171. ms_delay_ms(2);
  2172. #endif
  2173. error = ms_mspro_write_data_sw(ms_mspro_info, lba, byte_cnt, data_buf);
  2174. }
  2175. #endif
  2176. return error;
  2177. }
  2178. void ms_mspro_exit(MS_MSPRO_Card_Info_t *ms_mspro_info)
  2179. {
  2180. if(ms_mspro_info->ms_mspro_io_release)
  2181. ms_mspro_info->ms_mspro_io_release();
  2182. check_one_boot_block = 0;
  2183. mspro_access_status_reg_after_read = 0;
  2184. }
  2185. void ms_mspro_prepare_init(MS_MSPRO_Card_Info_t *ms_mspro_info)
  2186. {
  2187. if(ms_mspro_power_register)
  2188. ms_mspro_info->ms_mspro_power = ms_mspro_power_register;
  2189. if(ms_mspro_ins_register)
  2190. ms_mspro_info->ms_mspro_get_ins = ms_mspro_ins_register;
  2191. if(ms_mspro_io_release_register)
  2192. ms_mspro_info->ms_mspro_io_release = ms_mspro_io_release_register;
  2193. }
  2194. void ms_mspro_prepare_power(MS_MSPRO_Card_Info_t *ms_mspro_info)
  2195. {
  2196. ms_gpio_enable(ms_mspro_info->io_pad_type);
  2197. ms_set_clk_output();
  2198. ms_set_clk_low();
  2199. ms_set_dat0_3_output();
  2200. ms_set_dat0_3_value(0);
  2201. ms_set_bs_output();
  2202. ms_set_bs_state(0);
  2203. }