msdc.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2018. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. *
  31. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #include "msdc.h"
  36. #if defined(MMC_MSDC_DRV_CTP)
  37. #include <common.h>
  38. #include "api.h" //For invocation cache_clean_invalidate()
  39. #include "cache_api.h" //For invocation cache_clean_invalidate()
  40. #endif
  41. #if defined(MMC_MSDC_DRV_LK)
  42. #include <kernel/event.h>
  43. #include <platform/mt_irq.h>
  44. #endif
  45. #if defined(MMC_MSDC_DRV_CTP)
  46. #include "gpio.h"
  47. #if defined(MSDC_USE_DCM)
  48. #include "dcm.h"
  49. #endif
  50. #if !defined(FPGA_PLATFORM)
  51. #include "pmic.h"
  52. #include "clock_manager.h"
  53. #endif
  54. #endif
  55. static int msdc_rsp[] = {
  56. 0, /* RESP_NONE */
  57. 1, /* RESP_R1 */
  58. 2, /* RESP_R2 */
  59. 3, /* RESP_R3 */
  60. 4, /* RESP_R4 */
  61. 1, /* RESP_R5 */
  62. 1, /* RESP_R6 */
  63. 1, /* RESP_R7 */
  64. 7, /* RESP_R1b */
  65. };
  66. static msdc_priv_t msdc_priv[MSDC_MAX_NUM];
  67. void msdc_dump_card_status(u32 card_status)
  68. {
  69. #if MSDC_DEBUG
  70. static char *state[] = {
  71. "Idle", /* 0 */
  72. "Ready", /* 1 */
  73. "Ident", /* 2 */
  74. "Stby", /* 3 */
  75. "Tran", /* 4 */
  76. "Data", /* 5 */
  77. "Rcv", /* 6 */
  78. "Prg", /* 7 */
  79. "Dis", /* 8 */
  80. "Ina", /* 9 */
  81. "Sleep", /* 10 */
  82. "Reserved", /* 11 */
  83. "Reserved", /* 12 */
  84. "Reserved", /* 13 */
  85. "Reserved", /* 14 */
  86. "I/O mode", /* 15 */
  87. };
  88. if (card_status & R1_OUT_OF_RANGE)
  89. MSG(INF, "\t[CARD_STATUS] Out of Range\n");
  90. if (card_status & R1_ADDRESS_ERROR)
  91. MSG(INF, "\t[CARD_STATUS] Address Error\n");
  92. if (card_status & R1_BLOCK_LEN_ERROR)
  93. MSG(INF, "\t[CARD_STATUS] Block Len Error\n");
  94. if (card_status & R1_ERASE_SEQ_ERROR)
  95. MSG(INF, "\t[CARD_STATUS] Erase Seq Error\n");
  96. if (card_status & R1_ERASE_PARAM)
  97. MSG(INF, "\t[CARD_STATUS] Erase Param\n");
  98. if (card_status & R1_WP_VIOLATION)
  99. MSG(INF, "\t[CARD_STATUS] WP Violation\n");
  100. if (card_status & R1_CARD_IS_LOCKED)
  101. MSG(INF, "\t[CARD_STATUS] Card is Locked\n");
  102. if (card_status & R1_LOCK_UNLOCK_FAILED)
  103. MSG(INF, "\t[CARD_STATUS] Lock/Unlock Failed\n");
  104. if (card_status & R1_COM_CRC_ERROR)
  105. MSG(INF, "\t[CARD_STATUS] Command CRC Error\n");
  106. if (card_status & R1_ILLEGAL_COMMAND)
  107. MSG(INF, "\t[CARD_STATUS] Illegal Command\n");
  108. if (card_status & R1_CARD_ECC_FAILED)
  109. MSG(INF, "\t[CARD_STATUS] Card ECC Failed\n");
  110. if (card_status & R1_CC_ERROR)
  111. MSG(INF, "\t[CARD_STATUS] CC Error\n");
  112. if (card_status & R1_ERROR)
  113. MSG(INF, "\t[CARD_STATUS] Error\n");
  114. if (card_status & R1_UNDERRUN)
  115. MSG(INF, "\t[CARD_STATUS] Underrun\n");
  116. if (card_status & R1_OVERRUN)
  117. MSG(INF, "\t[CARD_STATUS] Overrun\n");
  118. if (card_status & R1_CID_CSD_OVERWRITE)
  119. MSG(INF, "\t[CARD_STATUS] CID/CSD Overwrite\n");
  120. if (card_status & R1_WP_ERASE_SKIP)
  121. MSG(INF, "\t[CARD_STATUS] WP Eraser Skip\n");
  122. if (card_status & R1_CARD_ECC_DISABLED)
  123. MSG(INF, "\t[CARD_STATUS] Card ECC Disabled\n");
  124. if (card_status & R1_ERASE_RESET)
  125. MSG(INF, "\t[CARD_STATUS] Erase Reset\n");
  126. if (card_status & R1_READY_FOR_DATA)
  127. MSG(INF, "\t[CARD_STATUS] Ready for Data\n");
  128. if (card_status & R1_SWITCH_ERROR)
  129. MSG(INF, "\t[CARD_STATUS] Switch error\n");
  130. if (card_status & R1_URGENT_BKOPS)
  131. MSG(INF, "\t[CARD_STATUS] Urgent background operations\n");
  132. if (card_status & R1_APP_CMD)
  133. MSG(INF, "\t[CARD_STATUS] App Command\n");
  134. MSG(INF, "\t[CARD_STATUS] '%s' State\n",
  135. state[R1_CURRENT_STATE(card_status)]);
  136. #endif
  137. }
  138. void msdc_dump_ocr_reg(u32 resp)
  139. {
  140. #if MSDC_DEBUG
  141. if (resp & (1 << 7))
  142. MSG(INF, "\t[OCR] Low Voltage Range\n");
  143. if (resp & (1 << 15))
  144. MSG(INF, "\t[OCR] 2.7-2.8 volt\n");
  145. if (resp & (1 << 16))
  146. MSG(INF, "\t[OCR] 2.8-2.9 volt\n");
  147. if (resp & (1 << 17))
  148. MSG(INF, "\t[OCR] 2.9-3.0 volt\n");
  149. if (resp & (1 << 18))
  150. MSG(INF, "\t[OCR] 3.0-3.1 volt\n");
  151. if (resp & (1 << 19))
  152. MSG(INF, "\t[OCR] 3.1-3.2 volt\n");
  153. if (resp & (1 << 20))
  154. MSG(INF, "\t[OCR] 3.2-3.3 volt\n");
  155. if (resp & (1 << 21))
  156. MSG(INF, "\t[OCR] 3.3-3.4 volt\n");
  157. if (resp & (1 << 22))
  158. MSG(INF, "\t[OCR] 3.4-3.5 volt\n");
  159. if (resp & (1 << 23))
  160. MSG(INF, "\t[OCR] 3.5-3.6 volt\n");
  161. if (resp & (1 << 24))
  162. MSG(INF, "\t[OCR] Switching to 1.8V Accepted (S18A)\n");
  163. if (resp & (1 << 30))
  164. MSG(INF, "\t[OCR] Card Capacity Status (CCS)\n");
  165. if (resp & (1UL << 31))
  166. MSG(INF, "\t[OCR] Card Power Up Status (Idle)\n");
  167. else
  168. MSG(INF, "\t[OCR] Card Power Up Status (Busy)\n");
  169. #endif
  170. }
  171. void msdc_dump_io_resp(u32 resp)
  172. {
  173. #if MSDC_DEBUG
  174. u32 flags = (resp >> 8) & 0xFF;
  175. char *state[] = {"DIS", "CMD", "TRN", "RFU"};
  176. if (flags & (1 << 7))
  177. MSG(INF, "\t[IO] COM_CRC_ERR\n");
  178. if (flags & (1 << 6))
  179. MSG(INF, "\t[IO] Illgal command\n");
  180. if (flags & (1 << 3))
  181. MSG(INF, "\t[IO] Error\n");
  182. if (flags & (1 << 2))
  183. MSG(INF, "\t[IO] RFU\n");
  184. if (flags & (1 << 1))
  185. MSG(INF, "\t[IO] Function number error\n");
  186. if (flags & (1 << 0))
  187. MSG(INF, "\t[IO] Out of range\n");
  188. MSG(INF, "[IO] State: %s, Data:0x%x\n", state[(resp >> 12) & 0x3], resp & 0xFF);
  189. #endif
  190. }
  191. void msdc_dump_rca_resp(u32 resp)
  192. {
  193. #if MSDC_DEBUG
  194. u32 card_status = (((resp >> 15) & 0x1) << 23) |
  195. (((resp >> 14) & 0x1) << 22) |
  196. (((resp >> 13) & 0x1) << 19) |
  197. (resp & 0x1fff);
  198. MSG(INF, "\t[RCA] 0x%x\n", resp >> 16);
  199. msdc_dump_card_status(card_status);
  200. #endif
  201. }
  202. static void msdc_dump_dbg_register(struct mmc_host *host)
  203. {
  204. u32 base = host->base;
  205. u32 i;
  206. for (i = 0; i <= 0x3c; i++) {
  207. MSDC_WRITE32(MSDC_DBG_SEL, i);
  208. MSG(INF, "[SD%d]SW_DBG_SEL: write reg[%x] to 0x%x\n", host->id, OFFSET_MSDC_DBG_SEL, i);
  209. MSG(INF, "[SD%d]SW_DBG_OUT: read reg[%x] to 0x%x\n", host->id, OFFSET_MSDC_DBG_OUT, MSDC_READ32(MSDC_DBG_OUT));
  210. }
  211. MSDC_WRITE32(MSDC_DBG_SEL, 0);
  212. }
  213. void msdc_dump_register(struct mmc_host *host)
  214. {
  215. u32 base = host->base;
  216. MSDC_UNUSED(base);
  217. MSG(INF, "[SD%d] Reg[%x] MSDC_CFG = 0x%x\n", host->id, OFFSET_MSDC_CFG, MSDC_READ32(MSDC_CFG));
  218. MSG(INF, "[SD%d] Reg[%x] MSDC_IOCON = 0x%x\n", host->id, OFFSET_MSDC_IOCON, MSDC_READ32(MSDC_IOCON));
  219. MSG(INF, "[SD%d] Reg[%x] MSDC_PS = 0x%x\n", host->id, OFFSET_MSDC_PS, MSDC_READ32(MSDC_PS));
  220. MSG(INF, "[SD%d] Reg[%x] MSDC_INT = 0x%x\n", host->id, OFFSET_MSDC_INT, MSDC_READ32(MSDC_INT));
  221. MSG(INF, "[SD%d] Reg[%x] MSDC_INTEN = 0x%x\n", host->id, OFFSET_MSDC_INTEN, MSDC_READ32(MSDC_INTEN));
  222. MSG(INF, "[SD%d] Reg[%x] MSDC_FIFOCS = 0x%x\n", host->id, OFFSET_MSDC_FIFOCS, MSDC_READ32(MSDC_FIFOCS));
  223. MSG(INF, "[SD%d] Reg[%x] MSDC_TXDATA = not read\n", host->id, OFFSET_MSDC_TXDATA);
  224. MSG(INF, "[SD%d] Reg[%x] MSDC_RXDATA = not read\n", host->id, OFFSET_MSDC_RXDATA);
  225. MSG(INF, "[SD%d] Reg[%x] SDC_CFG = 0x%x\n", host->id, OFFSET_SDC_CFG, MSDC_READ32(SDC_CFG));
  226. MSG(INF, "[SD%d] Reg[%x] SDC_CMD = 0x%x\n", host->id, OFFSET_SDC_CMD, MSDC_READ32(SDC_CMD));
  227. MSG(INF, "[SD%d] Reg[%x] SDC_ARG = 0x%x\n", host->id, OFFSET_SDC_ARG, MSDC_READ32(SDC_ARG));
  228. MSG(INF, "[SD%d] Reg[%x] SDC_STS = 0x%x\n", host->id, OFFSET_SDC_STS, MSDC_READ32(SDC_STS));
  229. MSG(INF, "[SD%d] Reg[%x] SDC_RESP0 = 0x%x\n", host->id, OFFSET_SDC_RESP0, MSDC_READ32(SDC_RESP0));
  230. MSG(INF, "[SD%d] Reg[%x] SDC_RESP1 = 0x%x\n", host->id, OFFSET_SDC_RESP1, MSDC_READ32(SDC_RESP1));
  231. MSG(INF, "[SD%d] Reg[%x] SDC_RESP2 = 0x%x\n", host->id, OFFSET_SDC_RESP2, MSDC_READ32(SDC_RESP2));
  232. MSG(INF, "[SD%d] Reg[%x] SDC_RESP3 = 0x%x\n", host->id, OFFSET_SDC_RESP3, MSDC_READ32(SDC_RESP3));
  233. MSG(INF, "[SD%d] Reg[%x] SDC_BLK_NUM = 0x%x\n", host->id, OFFSET_SDC_BLK_NUM, MSDC_READ32(SDC_BLK_NUM));
  234. MSG(INF, "[SD%d] Reg[%x] SDC_VOL_CHG = 0x%x\n", host->id, OFFSET_SDC_VOL_CHG, MSDC_READ32(SDC_VOL_CHG));
  235. MSG(INF, "[SD%d] Reg[%x] SDC_CSTS = 0x%x\n", host->id, OFFSET_SDC_CSTS, MSDC_READ32(SDC_CSTS));
  236. MSG(INF, "[SD%d] Reg[%x] SDC_CSTS_EN = 0x%x\n", host->id, OFFSET_SDC_CSTS_EN, MSDC_READ32(SDC_CSTS_EN));
  237. MSG(INF, "[SD%d] Reg[%x] SDC_DATCRC_STS = 0x%x\n", host->id, OFFSET_SDC_DCRC_STS, MSDC_READ32(SDC_DCRC_STS));
  238. MSG(INF, "[SD%d] Reg[%x] EMMC_CFG0 = 0x%x\n", host->id, OFFSET_EMMC_CFG0, MSDC_READ32(EMMC_CFG0));
  239. MSG(INF, "[SD%d] Reg[%x] EMMC_CFG1 = 0x%x\n", host->id, OFFSET_EMMC_CFG1, MSDC_READ32(EMMC_CFG1));
  240. MSG(INF, "[SD%d] Reg[%x] EMMC_STS = 0x%x\n", host->id, OFFSET_EMMC_STS, MSDC_READ32(EMMC_STS));
  241. MSG(INF, "[SD%d] Reg[%x] EMMC_IOCON = 0x%x\n", host->id, OFFSET_EMMC_IOCON, MSDC_READ32(EMMC_IOCON));
  242. MSG(INF, "[SD%d] Reg[%x] SDC_ACMD_RESP = 0x%x\n", host->id, OFFSET_SDC_ACMD_RESP, MSDC_READ32(SDC_ACMD_RESP));
  243. MSG(INF, "[SD%d] Reg[%x] SDC_ACMD19_TRG = 0x%x\n", host->id, OFFSET_SDC_ACMD19_TRG, MSDC_READ32(SDC_ACMD19_TRG));
  244. MSG(INF, "[SD%d] Reg[%x] SDC_ACMD19_STS = 0x%x\n", host->id, OFFSET_SDC_ACMD19_STS, MSDC_READ32(SDC_ACMD19_STS));
  245. /* Cooment out reading this register since it cause system hang in CTP
  246. MSG(INF, "[SD%d] Reg[%x] DMA_SA_HIGH4BIT= 0x%x\n", host->id, OFFSET_MSDC_DMA_SA_HIGH, MSDC_READ32(OFFSET_MSDC_DMA_SA_HIGH));
  247. */
  248. MSG(INF, "[SD%d] Reg[%x] DMA_SA = 0x%x\n", host->id, OFFSET_MSDC_DMA_SA, MSDC_READ32(MSDC_DMA_SA));
  249. MSG(INF, "[SD%d] Reg[%x] DMA_CA = 0x%x\n", host->id, OFFSET_MSDC_DMA_CA, MSDC_READ32(MSDC_DMA_CA));
  250. MSG(INF, "[SD%d] Reg[%x] DMA_CTRL = 0x%x\n", host->id, OFFSET_MSDC_DMA_CTRL, MSDC_READ32(MSDC_DMA_CTRL));
  251. MSG(INF, "[SD%d] Reg[%x] DMA_CFG = 0x%x\n", host->id, OFFSET_MSDC_DMA_CFG, MSDC_READ32(MSDC_DMA_CFG));
  252. MSG(INF, "[SD%d] Reg[%x] SW_DBG_SEL = 0x%x\n", host->id, OFFSET_MSDC_DBG_SEL, MSDC_READ32(MSDC_DBG_SEL));
  253. MSG(INF, "[SD%d] Reg[%x] SW_DBG_OUT = 0x%x\n", host->id, OFFSET_MSDC_DBG_OUT, MSDC_READ32(MSDC_DBG_OUT));
  254. MSG(INF, "[SD%d] Reg[%x] PATCH_BIT0 = 0x%x\n", host->id, OFFSET_MSDC_PATCH_BIT0,MSDC_READ32(MSDC_PATCH_BIT0));
  255. MSG(INF, "[SD%d] Reg[%x] PATCH_BIT1 = 0x%x\n", host->id, OFFSET_MSDC_PATCH_BIT1,MSDC_READ32(MSDC_PATCH_BIT1));
  256. MSG(INF, "[SD%d] Reg[%x] PATCH_BIT2 = 0x%x\n", host->id, OFFSET_MSDC_PATCH_BIT2,MSDC_READ32(MSDC_PATCH_BIT2));
  257. MSG(INF, "[SD%d] Reg[%x] PAD_TUNE0 = 0x%x\n", host->id, OFFSET_MSDC_PAD_TUNE0, MSDC_READ32(MSDC_PAD_TUNE0));
  258. MSG(INF, "[SD%d] Reg[%x] PAD_TUNE1 = 0x%x\n", host->id, OFFSET_MSDC_PAD_TUNE1, MSDC_READ32(MSDC_PAD_TUNE1));
  259. MSG(INF, "[SD%d] Reg[%x] DAT_RD_DLY0 = 0x%x\n", host->id, OFFSET_MSDC_DAT_RDDLY0,MSDC_READ32(MSDC_DAT_RDDLY0));
  260. MSG(INF, "[SD%d] Reg[%x] DAT_RD_DLY1 = 0x%x\n", host->id, OFFSET_MSDC_DAT_RDDLY1,MSDC_READ32(MSDC_DAT_RDDLY1));
  261. MSG(INF, "[SD%d] Reg[%x] HW_DBG_SEL = 0x%x\n", host->id, OFFSET_MSDC_HW_DBG, MSDC_READ32(MSDC_HW_DBG));
  262. MSG(INF, "[SD%d] Reg[%x] MAIN_VER = 0x%x\n", host->id, OFFSET_MSDC_VERSION, MSDC_READ32(MSDC_VERSION));
  263. if (host->base_top) {
  264. #if !defined(FPGA_PLATFORM)
  265. MSG(INF, "[SD%d] Reg[%x] EMMC_TOP_CONTROL = 0x%x\n", host->id, OFFSET_EMMC_TOP_CONTROL, MSDC_READ32(EMMC_TOP_CONTROL));
  266. MSG(INF, "[SD%d] Reg[%x] EMMC50_TOP_CMD = 0x%x\n", host->id, OFFSET_EMMC_TOP_CMD, MSDC_READ32(EMMC_TOP_CMD));
  267. MSG(INF, "[SD%d] Reg[%x] TOP_EMMC50_PAD_CTL0 = 0x%x\n", host->id, OFFSET_TOP_EMMC50_PAD_CTL0, MSDC_READ32(TOP_EMMC50_PAD_CTL0));
  268. MSG(INF, "[SD%d] Reg[%x] TOP_EMMC50_PAD_DS_TUNE = 0x%x\n", host->id, OFFSET_TOP_EMMC50_PAD_DS_TUNE, MSDC_READ32(TOP_EMMC50_PAD_DS_TUNE));
  269. MSG(INF, "[SD%d] Reg[%x] TOP_EMMC50_PAD_DAT0_TUNE = 0x%x\n", host->id, OFFSET_TOP_EMMC50_PAD_DAT0_TUNE, MSDC_READ32(TOP_EMMC50_PAD_DAT0_TUNE));
  270. MSG(INF, "[SD%d] Reg[%x] TOP_EMMC50_PAD_DAT1_TUNE = 0x%x\n", host->id, OFFSET_TOP_EMMC50_PAD_DAT1_TUNE, MSDC_READ32(TOP_EMMC50_PAD_DAT1_TUNE));
  271. MSG(INF, "[SD%d] Reg[%x] TOP_EMMC50_PAD_DAT2_TUNE = 0x%x\n", host->id, OFFSET_TOP_EMMC50_PAD_DAT2_TUNE, MSDC_READ32(TOP_EMMC50_PAD_DAT2_TUNE));
  272. MSG(INF, "[SD%d] Reg[%x] TOP_EMMC50_PAD_DAT3_TUNE = 0x%x\n", host->id, OFFSET_TOP_EMMC50_PAD_DAT3_TUNE, MSDC_READ32(TOP_EMMC50_PAD_DAT3_TUNE));
  273. MSG(INF, "[SD%d] Reg[%x] TOP_EMMC50_PAD_DAT4_TUNE = 0x%x\n", host->id, OFFSET_TOP_EMMC50_PAD_DAT4_TUNE, MSDC_READ32(TOP_EMMC50_PAD_DAT4_TUNE));
  274. MSG(INF, "[SD%d] Reg[%x] TOP_EMMC50_PAD_DAT5_TUNE = 0x%x\n", host->id, OFFSET_TOP_EMMC50_PAD_DAT5_TUNE, MSDC_READ32(TOP_EMMC50_PAD_DAT5_TUNE));
  275. MSG(INF, "[SD%d] Reg[%x] TOP_EMMC50_PAD_DAT6_TUNE = 0x%x\n", host->id, OFFSET_TOP_EMMC50_PAD_DAT6_TUNE, MSDC_READ32(TOP_EMMC50_PAD_DAT6_TUNE));
  276. MSG(INF, "[SD%d] Reg[%x] TOP_EMMC50_PAD_DAT7_TUNE = 0x%x\n", host->id, OFFSET_TOP_EMMC50_PAD_DAT7_TUNE, MSDC_READ32(TOP_EMMC50_PAD_DAT7_TUNE));
  277. #endif
  278. } else if (host->id != 1) {
  279. MSG(INF, "[SD%d] Reg[%x] EMMC50_PAD_CTL0 = 0x%x\n", host->id, OFFSET_EMMC50_PAD_CTL0, MSDC_READ32(EMMC50_PAD_CTL0));
  280. MSG(INF, "[SD%d] Reg[%x] EMMC50_PAD_DS_CTL0 = 0x%x\n", host->id, OFFSET_EMMC50_PAD_DS_CTL0, MSDC_READ32(EMMC50_PAD_DS_CTL0));
  281. MSG(INF, "[SD%d] Reg[%x] EMMC50_PAD_DS_TUNE = 0x%x\n", host->id, OFFSET_EMMC50_PAD_DS_TUNE, MSDC_READ32(EMMC50_PAD_DS_TUNE));
  282. MSG(INF, "[SD%d] Reg[%x] EMMC50_PAD_CMD_TUNE = 0x%x\n", host->id, OFFSET_EMMC50_PAD_CMD_TUNE, MSDC_READ32(EMMC50_PAD_CMD_TUNE));
  283. MSG(INF, "[SD%d] Reg[%x] EMMC50_PAD_DAT01_TUNE = 0x%x\n", host->id, OFFSET_EMMC50_PAD_DAT01_TUNE, MSDC_READ32(EMMC50_PAD_DAT01_TUNE));
  284. MSG(INF, "[SD%d] Reg[%x] EMMC50_PAD_DAT23_TUNE = 0x%x\n", host->id, OFFSET_EMMC50_PAD_DAT23_TUNE, MSDC_READ32(EMMC50_PAD_DAT23_TUNE));
  285. MSG(INF, "[SD%d] Reg[%x] EMMC50_PAD_DAT45_TUNE = 0x%x\n", host->id, OFFSET_EMMC50_PAD_DAT45_TUNE, MSDC_READ32(EMMC50_PAD_DAT45_TUNE));
  286. MSG(INF, "[SD%d] Reg[%x] EMMC50_PAD_DAT67_TUNE = 0x%x\n", host->id, OFFSET_EMMC50_PAD_DAT67_TUNE, MSDC_READ32(EMMC50_PAD_DAT67_TUNE));
  287. }
  288. if (host->id != 1) {
  289. MSG(INF, "[SD%d] Reg[%x] EMMC51_CFG0 = 0x%x\n", host->id, OFFSET_EMMC51_CFG0, MSDC_READ32(EMMC51_CFG0));
  290. MSG(INF, "[SD%d] Reg[%x] EMMC50_CFG0 = 0x%x\n", host->id, OFFSET_EMMC50_CFG0, MSDC_READ32(EMMC50_CFG0));
  291. MSG(INF, "[SD%d] Reg[%x] EMMC50_CFG1 = 0x%x\n", host->id, OFFSET_EMMC50_CFG1, MSDC_READ32(EMMC50_CFG1));
  292. MSG(INF, "[SD%d] Reg[%x] EMMC50_CFG2 = 0x%x\n", host->id, OFFSET_EMMC50_CFG2, MSDC_READ32(EMMC50_CFG2));
  293. MSG(INF, "[SD%d] Reg[%x] EMMC50_CFG3 = 0x%x\n", host->id, OFFSET_EMMC50_CFG3, MSDC_READ32(EMMC50_CFG3));
  294. MSG(INF, "[SD%d] Reg[%x] EMMC50_CFG4 = 0x%x\n", host->id, OFFSET_EMMC50_CFG4, MSDC_READ32(EMMC50_CFG4));
  295. }
  296. MSG(INF, "[SD%d] Reg[%x] SDC_FIFO_CFG = 0x%x\n", host->id, OFFSET_SDC_FIFO_CFG, MSDC_READ32(SDC_FIFO_CFG));
  297. msdc_dump_dbg_register(host);
  298. }
  299. #if defined(MMC_MSDC_DRV_CTP)
  300. #define HS400_BACKUP_REG_NUM (42)
  301. static struct msdc_reg_control hs400_backup_reg_list[HS400_BACKUP_REG_NUM] = {
  302. //{addr, mask, value, default value, func},
  303. {(MSDC0_BASE + OFFSET_MSDC_PATCH_BIT1), (MSDC_PB1_WRDAT_CRCS_TA_CNTR), 0x0, 0x1, NULL},//0xB4[2:0],
  304. {(MSDC0_BASE + OFFSET_MSDC_PATCH_BIT0), (MSDC_PB0_INT_DAT_LATCH_CK_SEL), 0x0, 0x0, NULL},//0xB0[9:7]
  305. {(MSDC0_BASE + OFFSET_MSDC_IOCON), (MSDC_IOCON_R_D_SMPL), 0x0, 0x0, NULL},//0x04[2:2]
  306. {(MSDC0_BASE + OFFSET_MSDC_PAD_TUNE0), (MSDC_PAD_TUNE0_DATRRDLY), 0x0, 0x0, NULL},//0xEC[12:8]
  307. {(MSDC0_BASE + OFFSET_MSDC_IOCON), (MSDC_IOCON_DDLSEL), 0x0, 0x0, NULL},//0x04[3:3]
  308. {(MSDC0_BASE + OFFSET_MSDC_DAT_RDDLY0), (MSDC_DAT_RDDLY0_D3), 0x0, 0x0, NULL},//0xF0[4:0]
  309. {(MSDC0_BASE + OFFSET_MSDC_DAT_RDDLY0), (MSDC_DAT_RDDLY0_D2), 0x0, 0x0, NULL},//0xF0[12:8]
  310. {(MSDC0_BASE + OFFSET_MSDC_DAT_RDDLY0), (MSDC_DAT_RDDLY0_D1), 0x0, 0x0, NULL},//0xF0[20:16]
  311. {(MSDC0_BASE + OFFSET_MSDC_DAT_RDDLY0), (MSDC_DAT_RDDLY0_D0), 0x0, 0x0, NULL},//0xF0[28:24]
  312. {(MSDC0_BASE + OFFSET_MSDC_DAT_RDDLY1), (MSDC_DAT_RDDLY1_D7), 0x0, 0x0, NULL},//0xF4[4:0]
  313. {(MSDC0_BASE + OFFSET_MSDC_DAT_RDDLY1), (MSDC_DAT_RDDLY1_D6), 0x0, 0x0, NULL},//0xF4[12:8]
  314. {(MSDC0_BASE + OFFSET_MSDC_DAT_RDDLY1), (MSDC_DAT_RDDLY1_D5), 0x0, 0x0, NULL},//0xF4[20:16]
  315. {(MSDC0_BASE + OFFSET_MSDC_DAT_RDDLY1), (MSDC_DAT_RDDLY1_D4), 0x0, 0x0, NULL},//0xF4[28:24]
  316. {(MSDC0_BASE + OFFSET_MSDC_IOCON), (MSDC_IOCON_R_D_SMPL_SEL), 0x0, 0x0, NULL},//0x04[5:5]
  317. {(MSDC0_BASE + OFFSET_MSDC_IOCON), (MSDC_IOCON_R_D0SPL), 0x0, 0x0, NULL},//0x04[16:16]
  318. {(MSDC0_BASE + OFFSET_MSDC_IOCON), (MSDC_IOCON_W_D_SMPL), 0x0, 0x0, NULL},//0x04[8:8]
  319. {(MSDC0_BASE + OFFSET_MSDC_PAD_TUNE0), (MSDC_PAD_TUNE0_DATWRDLY), 0x0, 0x0, NULL},//0xEC[4:0]
  320. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT01_TUNE), (MSDC_EMMC50_PAD_DAT0_RXDLY3SEL), 0x0, 0x0, NULL},//0x190[0:0]
  321. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT01_TUNE), (MSDC_EMMC50_PAD_DAT1_RXDLY3SEL), 0x0, 0x0, NULL},//0x190[16:16]
  322. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT23_TUNE), (MSDC_EMMC50_PAD_DAT2_RXDLY3SEL), 0x0, 0x0, NULL},//0x194[0:0]
  323. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT23_TUNE), (MSDC_EMMC50_PAD_DAT3_RXDLY3SEL), 0x0, 0x0, NULL},//0x194[16:16]
  324. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT45_TUNE), (MSDC_EMMC50_PAD_DAT4_RXDLY3SEL), 0x0, 0x0, NULL},//0x198[0:0]
  325. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT45_TUNE), (MSDC_EMMC50_PAD_DAT5_RXDLY3SEL), 0x0, 0x0, NULL},//0x198[16:16]
  326. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT67_TUNE), (MSDC_EMMC50_PAD_DAT6_RXDLY3SEL), 0x0, 0x0, NULL},//0x19C[0:0]
  327. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT67_TUNE), (MSDC_EMMC50_PAD_DAT7_RXDLY3SEL), 0x0, 0x0, NULL},//0x19C[16:16]
  328. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT01_TUNE), (MSDC_EMMC50_PAD_DAT0_RXDLY3), 0x0, 0x0, NULL},//0x190[5:1]
  329. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT01_TUNE), (MSDC_EMMC50_PAD_DAT1_RXDLY3), 0x0, 0x0, NULL},//0x190[21:17]
  330. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT23_TUNE), (MSDC_EMMC50_PAD_DAT2_RXDLY3), 0x0, 0x0, NULL},//0x194[5:1]
  331. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT23_TUNE), (MSDC_EMMC50_PAD_DAT3_RXDLY3), 0x0, 0x0, NULL},//0x194[21:17]
  332. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT45_TUNE), (MSDC_EMMC50_PAD_DAT4_RXDLY3), 0x0, 0x0, NULL},//0x198[5:1]
  333. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT45_TUNE), (MSDC_EMMC50_PAD_DAT5_RXDLY3), 0x0, 0x0, NULL},//0x198[21:17]
  334. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT67_TUNE), (MSDC_EMMC50_PAD_DAT6_RXDLY3), 0x0, 0x0, NULL},//0x19C[5:1]
  335. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT67_TUNE), (MSDC_EMMC50_PAD_DAT7_RXDLY3), 0x0, 0x0, NULL},//0x19C[21:17]
  336. /* _HQA asked cmd line delay 8 and dat line delay 4 under hs400 mode */
  337. {(MSDC0_BASE + OFFSET_EMMC50_PAD_CMD_TUNE), (MSDC_EMMC50_PAD_CMD_TUNE_TXDLY), 0x0, 0x8, NULL},//0x190[5:1]
  338. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT01_TUNE), (MSDC_EMMC50_PAD_DAT0_TXDLY), 0x0, 0x4, NULL},//0x190[5:1]
  339. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT01_TUNE), (MSDC_EMMC50_PAD_DAT1_TXDLY), 0x0, 0x4, NULL},//0x190[21:17]
  340. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT23_TUNE), (MSDC_EMMC50_PAD_DAT2_TXDLY), 0x0, 0x4, NULL},//0x194[5:1]
  341. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT23_TUNE), (MSDC_EMMC50_PAD_DAT3_TXDLY), 0x0, 0x4, NULL},//0x194[21:17]
  342. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT45_TUNE), (MSDC_EMMC50_PAD_DAT4_TXDLY), 0x0, 0x4, NULL},//0x198[5:1]
  343. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT45_TUNE), (MSDC_EMMC50_PAD_DAT5_TXDLY), 0x0, 0x4, NULL},//0x198[21:17]
  344. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT67_TUNE), (MSDC_EMMC50_PAD_DAT6_TXDLY), 0x0, 0x4, NULL},//0x19C[5:1]
  345. {(MSDC0_BASE + OFFSET_EMMC50_PAD_DAT67_TUNE), (MSDC_EMMC50_PAD_DAT7_TXDLY), 0x0, 0x4, NULL},//0x19C[21:17]
  346. };
  347. /* need reset some register while switch to hs400 mode with emmc50
  348. * do stress test need change mode from hs400 to others, so need backup if switched */
  349. int msdc_register_partial_backup_and_reset(struct mmc_host* host)
  350. {
  351. int i = 0, err = 0;
  352. for (i = 0; i < HS400_BACKUP_REG_NUM; i++) {
  353. MSDC_GET_FIELD(hs400_backup_reg_list[i].addr, hs400_backup_reg_list[i].mask, hs400_backup_reg_list[i].value);
  354. MSDC_SET_FIELD(hs400_backup_reg_list[i].addr, hs400_backup_reg_list[i].mask, hs400_backup_reg_list[i].default_value);
  355. if (hs400_backup_reg_list[i].restore_func) {
  356. err = hs400_backup_reg_list[i].restore_func(0);
  357. if (err) {
  358. msdc_pr_err("[%s]: failed to restore reg[0x%x][0x%x], expected value[0x%x], actual value[0x%x] err=0x%x",
  359. __func__, hs400_backup_reg_list[i].addr, hs400_backup_reg_list[i].mask, hs400_backup_reg_list[i].default_value, MSDC_READ32(hs400_backup_reg_list[i].addr), err);
  360. }
  361. }
  362. }
  363. return 0;
  364. }
  365. int msdc_register_partial_restore(struct mmc_host* host)
  366. {
  367. int i = 0, err = 0;
  368. for (i = 0; i < HS400_BACKUP_REG_NUM; i++) {
  369. MSDC_SET_FIELD(hs400_backup_reg_list[i].addr, hs400_backup_reg_list[i].mask, hs400_backup_reg_list[i].value);
  370. if (hs400_backup_reg_list[i].restore_func) {
  371. err = hs400_backup_reg_list[i].restore_func(1);
  372. if (err) {
  373. msdc_pr_err("[%s]:failed to restore reg[0x%x][0x%x], expected value[0x%x], actual value[0x%x] err=0x%x",
  374. __func__, hs400_backup_reg_list[i].addr, hs400_backup_reg_list[i].mask, hs400_backup_reg_list[i].value, MSDC_READ32(hs400_backup_reg_list[i].addr), err);
  375. }
  376. }
  377. }
  378. return 0;
  379. }
  380. #endif
  381. static void msdc_dump_info(struct mmc_host *host)
  382. {
  383. // 1: dump msdc hw register
  384. msdc_dump_register(host);
  385. // 2: For designer
  386. msdc_dump_dbg_register(host);
  387. // 3: check msdc clock gate and clock source
  388. msdc_dump_clock_sts(host);
  389. // 4: check msdc pmic ldo
  390. msdc_dump_ldo_sts(host);
  391. // 5: check msdc gpio
  392. msdc_dump_padctl_by_id(host->id);
  393. }
  394. void msdc_clr_fifo(struct mmc_host *host)
  395. {
  396. u32 base = host->base;
  397. MSDC_CLR_FIFO();
  398. }
  399. void msdc_reset(struct mmc_host *host)
  400. {
  401. u32 base = host->base;
  402. MSDC_RESET();
  403. }
  404. void msdc_abort(struct mmc_host *host)
  405. {
  406. u32 base = host->base;
  407. MSG(INF, "[SD%d] Abort: MSDC_FIFOCS=%xh MSDC_PS=%xh SDC_STS=%xh\n",
  408. host->id, MSDC_READ32(MSDC_FIFOCS), MSDC_READ32(MSDC_PS), MSDC_READ32(SDC_STS));
  409. /* reset controller */
  410. msdc_reset(host);
  411. /* clear fifo */
  412. msdc_clr_fifo(host);
  413. /* make sure txfifo and rxfifo are empty */
  414. if (MSDC_TXFIFOCNT() != 0 || MSDC_RXFIFOCNT() != 0) {
  415. MSG(INF, "[SD%d] Abort: TXFIFO(%d), RXFIFO(%d) != 0\n",
  416. host->id, MSDC_TXFIFOCNT(), MSDC_RXFIFOCNT());
  417. }
  418. /* clear all interrupts */
  419. MSDC_WRITE32(MSDC_INT, MSDC_READ32(MSDC_INT));
  420. }
  421. void msdc_set_signal_burst(struct mmc_host *host, int on)
  422. {
  423. u32 base = host->base;
  424. if (on)
  425. MSDC_SET_FIELD(MSDC_PATCH_BIT1, MSDC_PB1_SINGLE_BURST, 1);
  426. else
  427. MSDC_SET_FIELD(MSDC_PATCH_BIT1, MSDC_PB1_SINGLE_BURST, 0);
  428. }
  429. void msdc_enable_dcm(struct mmc_host *host, int on, u8 div1, u8 div2)
  430. {
  431. u32 base = host->base;
  432. /* Attention: write 0 is enable, write 1 is disable */
  433. MSDC_SET_FIELD(MSDC_PATCH_BIT1, MSDC_PB1_DCM_EN, !!on);
  434. }
  435. void msdc_set_axi_burst_len(struct mmc_host *host, u8 len)
  436. {
  437. u32 base = host->base;
  438. /* set axi burst len */
  439. MSDC_SET_FIELD(EMMC50_CFG2, MSDC_EMMC50_CFG2_AXI_SET_LEN, len);
  440. }
  441. void msdc_set_axi_outstanding(struct mmc_host *host, u8 rw, u8 num)
  442. {
  443. u32 base = host->base;
  444. /* set axi outstanding num */
  445. if (rw == 0) /* read */
  446. MSDC_SET_FIELD(EMMC50_CFG2, MSDC_EMMC50_CFG2_AXI_RD_OUTS_NUM, num);
  447. else /* write */
  448. MSDC_SET_FIELD(EMMC50_CFG3, MSDC_EMMC50_CFG3_OUTS_WR, num);
  449. }
  450. void msdc_set_startbit(struct mmc_host *host, u8 start_bit)
  451. {
  452. u32 base = host->base;
  453. msdc_priv_t *priv = (msdc_priv_t*)host->priv;
  454. /* set start bit */
  455. MSG(INF, "msdc_set_startbit %d\n", (int)start_bit);
  456. MSDC_SET_FIELD(MSDC_CFG, MSDC_CFG_START_BIT, start_bit);
  457. priv->start_bit = start_bit;
  458. }
  459. void msdc_set_smpl(struct mmc_host *host, u8 HS400, u8 mode, u8 type)
  460. {
  461. u32 base = host->base;
  462. msdc_priv_t *priv = (msdc_priv_t*)host->priv;
  463. if (type == TYPE_CMD_RESP_EDGE) {
  464. #if defined(MMC_MSDC_DRV_CTP) || defined(SLT)
  465. if (HS400) {
  466. // eMMC5.0 only output resp at CLK pin, so no need to select DS pin
  467. MSDC_SET_FIELD(EMMC50_CFG0, MSDC_EMMC50_CFG_PADCMD_LATCHCK, 0); //latch cmd resp at CLK pin
  468. MSDC_SET_FIELD(EMMC50_CFG0, MSDC_EMMC50_CFG_CMD_RESP_SEL, 0);//latch cmd resp
  469. // 0: from delay path for eMMC4.5
  470. // 1: from FIFO path for eMMC5.0
  471. }
  472. #endif
  473. if (mode == MSDC_SMPL_RISING || mode == MSDC_SMPL_FALLING) {
  474. #if 0 // HS400 tune MSDC_EMMC50_CFG_CMD_EDGE_SEL use DS latch, but now no DS latch
  475. if (HS400) {
  476. MSDC_SET_FIELD(EMMC50_CFG0, MSDC_EMMC50_CFG_CMD_EDGE_SEL, mode);
  477. } else {
  478. MSDC_SET_FIELD(MSDC_IOCON, MSDC_IOCON_RSPL, mode);
  479. }
  480. #else
  481. MSDC_SET_FIELD(MSDC_IOCON, MSDC_IOCON_RSPL, mode);
  482. #endif
  483. priv->rsmpl = mode;
  484. } else {
  485. msdc_pr_err("[%s]: SD%d invalid resp latch parm: HS400=%d, type=%d, mode=%d\n", __func__, host->id, HS400, type, mode);
  486. }
  487. } else if (type == TYPE_WRITE_CRC_EDGE) {
  488. /* FIX ME, test if always using DS pin can work */
  489. #if defined(MMC_MSDC_DRV_CTP) || defined(SLT)
  490. if (HS400)
  491. MSDC_SET_FIELD(EMMC50_CFG0, MSDC_EMMC50_CFG_CRC_STS_SEL, 1);//latch write crc status at DS pin
  492. else
  493. #endif
  494. MSDC_SET_FIELD(EMMC50_CFG0, MSDC_EMMC50_CFG_CRC_STS_SEL, 0);//latch write crc status at CLK pin
  495. if (mode == MSDC_SMPL_RISING || mode == MSDC_SMPL_FALLING) {
  496. #if defined(MMC_MSDC_DRV_CTP)
  497. if (HS400)
  498. MSDC_SET_FIELD(EMMC50_CFG0, MSDC_EMMC50_CFG_CRC_STS_EDGE, mode);
  499. else
  500. #endif
  501. MSDC_SET_FIELD(MSDC_PATCH_BIT2, MSDC_PB2_CFGCRCSTSEDGE, mode);
  502. } else {
  503. msdc_pr_err("[%s]: SD%d invalid wcrc latch parm: HS400=%d, type=%d, mode=%d\n", __func__, host->id, HS400, type, mode);
  504. }
  505. } else if (type == TYPE_READ_DATA_EDGE) {
  506. #if defined(MMC_MSDC_DRV_CTP) || defined(SLT)
  507. if (HS400) {
  508. //for HS400, start bit is output both on rising and falling edge
  509. msdc_set_startbit(host, START_AT_RISING_AND_FALLING);
  510. priv->start_bit = START_AT_RISING_AND_FALLING;
  511. } else
  512. #endif
  513. {
  514. //for the other mode, start bit is only output on rising edge. but DDR50 can try falling edge if error casued by pad delay
  515. if (host->card && mmc_card_ddr(host->card)) {
  516. msdc_set_startbit(host, mode);
  517. priv->start_bit = mode;
  518. } else {
  519. msdc_set_startbit(host, START_AT_RISING);
  520. priv->start_bit = START_AT_RISING;
  521. }
  522. }
  523. if (mode == MSDC_SMPL_RISING || mode == MSDC_SMPL_FALLING) {
  524. MSDC_SET_FIELD(MSDC_IOCON, MSDC_IOCON_R_D_SMPL_SEL, 0);
  525. MSDC_SET_FIELD(MSDC_PATCH_BIT0, MSDC_PB0_RD_DAT_SEL, mode);
  526. } else {
  527. msdc_pr_err("[%s]: SD%d invalid read latch parm: HS400=%d, type=%d, mode=%d\n", __func__, host->id, HS400, type, mode);
  528. }
  529. }
  530. }
  531. static u32 msdc_cal_timeout(struct mmc_host *host, u64 ns, u32 clks, u32 clkunit)
  532. {
  533. u32 timeout, clk_ns;
  534. clk_ns = 1000000000UL / host->cur_bus_clk;
  535. timeout = ns / clk_ns + clks;
  536. timeout = timeout / clkunit;
  537. return timeout;
  538. }
  539. void msdc_set_timeout(struct mmc_host *host, u64 ns, u32 clks)
  540. {
  541. u32 base = host->base;
  542. u32 timeout, clk_ns;
  543. u32 mode = 0;
  544. if (host->cur_bus_clk == 0) {
  545. timeout = 0;
  546. } else {
  547. clk_ns = 1000000000UL / host->cur_bus_clk;
  548. timeout = (ns + clk_ns - 1) / clk_ns + clks;
  549. timeout = (timeout + (1 << TMO_IN_CLK_2POWER) - 1) >> TMO_IN_CLK_2POWER; /* in 1048576 sclk cycle unit */
  550. MSDC_GET_FIELD(MSDC_CFG, MSDC_CFG_CKMOD, mode);
  551. timeout = mode >= 2 ? timeout * 2 : timeout; //DDR mode will double the clk cycles for data timeout
  552. timeout = timeout > 1 ? timeout - 1 : 0;
  553. timeout = timeout > 255 ? 255 : timeout;
  554. }
  555. MSDC_SET_FIELD(SDC_CFG, SDC_CFG_DTOC, timeout);
  556. MSG(OPS, "[SD%d] Set read data timeout: %dus %dclks -> %d x 1048576 cycles, mode:%d, clk_freq=%dKHz\n",
  557. host->id, (u32)(ns/1000), clks, timeout + 1, mode, (host->cur_bus_clk / 1000));
  558. }
  559. void msdc_set_blklen(struct mmc_host *host, u32 blklen)
  560. {
  561. msdc_priv_t *priv = (msdc_priv_t*)host->priv;
  562. host->blklen = blklen;
  563. priv->cfg.blklen = blklen;
  564. msdc_clr_fifo(host);
  565. }
  566. void msdc_set_blknum(struct mmc_host *host, u32 blknum)
  567. {
  568. u32 base = host->base;
  569. MSDC_WRITE32(SDC_BLK_NUM, blknum);
  570. }
  571. void msdc_set_dmode(struct mmc_host *host, int mode)
  572. {
  573. msdc_priv_t *priv = (msdc_priv_t*)host->priv;
  574. #if defined(MSDC_ENABLE_DMA_MODE)
  575. priv->cfg.mode = mode;
  576. #endif
  577. if (mode == MSDC_MODE_PIO) {
  578. host->blk_read = msdc_pio_bread;
  579. host->blk_write = msdc_pio_bwrite;
  580. #if defined(MSDC_ENABLE_DMA_MODE)
  581. } else {
  582. host->blk_read = msdc_dma_bread;
  583. host->blk_write = msdc_dma_bwrite;
  584. #endif
  585. }
  586. }
  587. int msdc_get_dmode(struct mmc_host *host)
  588. {
  589. msdc_priv_t *priv = (msdc_priv_t*)host->priv;
  590. int mode = priv->cfg.mode;
  591. return mode;
  592. }
  593. void msdc_set_pio_bits(struct mmc_host *host, int bits)
  594. {
  595. msdc_priv_t *priv = (msdc_priv_t*)host->priv;
  596. priv->pio_bits = bits;
  597. }
  598. void msdc_set_autocmd(struct mmc_host *host, int cmd, int on)
  599. {
  600. msdc_priv_t *priv = (msdc_priv_t*)host->priv;
  601. if (on) {
  602. priv->autocmd |= cmd;
  603. } else {
  604. priv->autocmd &= ~cmd;
  605. }
  606. }
  607. void msdc_set_reliable_write(struct mmc_host *host, int on)
  608. {
  609. msdc_priv_t *priv = (msdc_priv_t*)host->priv;
  610. if (on) {
  611. priv->cmd23_flags |= MSDC_RELIABLE_WRITE;
  612. } else {
  613. priv->cmd23_flags &= ~MSDC_RELIABLE_WRITE;
  614. }
  615. }
  616. void msdc_set_autocmd23_feature(struct mmc_host *host, int on)
  617. {
  618. u32 base = host->base;
  619. if (on) {
  620. MSDC_SET_FIELD(MSDC_PATCH_BIT0, MSDC_PB0_BLKNUM_SEL, 0);
  621. } else {
  622. MSDC_SET_FIELD(MSDC_PATCH_BIT0, MSDC_PB0_BLKNUM_SEL, 1);
  623. }
  624. }
  625. int msdc_send_cmd(struct mmc_host *host, struct mmc_command *cmd)
  626. {
  627. msdc_priv_t *priv = (msdc_priv_t*)host->priv;
  628. u32 base = host->base;
  629. u32 opcode = cmd->opcode;
  630. u32 rsptyp;
  631. u32 rawcmd;
  632. u32 timeout = cmd->timeout;
  633. u32 error = MMC_ERR_NONE;
  634. u32 blknum;
  635. #ifdef FEATURE_MMC_CMDQ
  636. u32 cmdq_reg_setting=0;
  637. if (opcode == MMC_SET_QUEUE_CONTEXT) {
  638. cmd->rsptyp = RESP_R1;
  639. cmdq_reg_setting=(MMC_SET_QUEUE_CONTEXT<<1)|1;
  640. } else if (opcode == MMC_SET_QUEUE_ADDRESS) {
  641. cmd->rsptyp = RESP_R1;
  642. cmdq_reg_setting=(MMC_SET_QUEUE_ADDRESS<<1)|1;
  643. } else if (opcode == MMC_WRITE_REQUESTED_QUEUE || opcode == MMC_READ_REQUESTED_QUEUE) {
  644. cmd->rsptyp = RESP_R1;
  645. } else if ( opcode==MMC_CMD_SEND_STATUS ) {
  646. cmd->rsptyp = RESP_R1;
  647. if ( MSDC_READ32(EMMC51_CFG0)&0x1 ) {
  648. cmdq_reg_setting=(MMC_CMD_SEND_STATUS<<1)|1;
  649. }
  650. } else if (opcode == 62) { //Light: To be clarified
  651. //Chiachun: for SS vendor command
  652. cmd->rsptyp = RESP_R1;
  653. }
  654. #if !defined(FEATURE_MMC_USE_EMMC51_CFG0_FOR_CMD44_45)
  655. cmdq_reg_setting=0;
  656. #endif
  657. #endif
  658. rsptyp=cmd->rsptyp;
  659. /* rawcmd :
  660. * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 |
  661. * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode
  662. */
  663. rawcmd = (opcode & ~(SD_CMD_BIT | SD_CMD_APP_BIT)) |
  664. msdc_rsp[rsptyp] << 7 | host->blklen << 16;
  665. if (opcode == MMC_CMD_WRITE_MULTIPLE_BLOCK) {
  666. rawcmd |= ((2 << 11) | (1 << 13));
  667. if (priv->autocmd & MSDC_AUTOCMD12) {
  668. rawcmd |= (1 << 28);
  669. } else if (priv->autocmd & MSDC_AUTOCMD23) {
  670. rawcmd |= (2 << 28);
  671. }
  672. } else if (opcode == MMC_CMD_WRITE_BLOCK || opcode == MMC_CMD50) {
  673. rawcmd |= ((1 << 11) | (1 << 13));
  674. } else if (opcode == MMC_CMD_READ_MULTIPLE_BLOCK) {
  675. rawcmd |= (2 << 11);
  676. if (priv->autocmd & MSDC_AUTOCMD12) {
  677. rawcmd |= (1 << 28);
  678. } else if (priv->autocmd & MSDC_AUTOCMD23) {
  679. rawcmd |= (2 << 28);
  680. }
  681. } else if (opcode == MMC_CMD_READ_SINGLE_BLOCK ||
  682. opcode == SD_ACMD_SEND_SCR ||
  683. opcode == SD_CMD_SWITCH ||
  684. opcode == MMC_CMD_SEND_EXT_CSD ||
  685. opcode == MMC_CMD_SEND_WRITE_PROT ||
  686. opcode == MMC_CMD_SEND_WRITE_PROT_TYPE ||
  687. opcode == MMC_CMD21) {
  688. rawcmd |= (1 << 11);
  689. } else if (opcode == MMC_CMD_STOP_TRANSMISSION) {
  690. rawcmd |= (1 << 14);
  691. rawcmd &= ~(0x0FFF << 16);
  692. #ifdef FEATURE_MMC_SDIO
  693. } else if (opcode == SD_IO_RW_EXTENDED) {
  694. if (cmd->arg & 0x80000000) /* R/W flag */
  695. rawcmd |= (1 << 13);
  696. if ((cmd->arg & 0x08000000) && ((cmd->arg & 0x1FF) > 1))
  697. rawcmd |= (2 << 11); /* multiple block mode */
  698. else
  699. rawcmd |= (1 << 11);
  700. } else if (opcode == SD_IO_RW_DIRECT) {
  701. if ((cmd->arg & 0x80000000) && ((cmd->arg >> 9) & 0x1FFFF))/* I/O abt */
  702. rawcmd |= (1 << 14);
  703. #endif
  704. #if defined(FEATURE_MMC_SDCARD) || defined(FEATURE_MMC_SDIO)
  705. } else if (opcode == SD_CMD_VOL_SWITCH) {
  706. rawcmd |= (1 << 30);
  707. } else if (opcode == SD_CMD_SEND_TUNING_BLOCK) {
  708. rawcmd |= (1 << 11); /* CHECKME */
  709. if (priv->autocmd & MSDC_AUTOCMD19)
  710. rawcmd |= (3 << 28);
  711. #endif
  712. #if 0
  713. } else if (opcode == MMC_CMD_GO_IRQ_STATE) {
  714. rawcmd |= (1 << 15);
  715. #endif
  716. #ifdef FEATURE_MMC_CMDQ
  717. } else if (opcode == MMC_READ_REQUESTED_QUEUE) {
  718. rawcmd |= (2 << 11);
  719. } else if (opcode == MMC_WRITE_REQUESTED_QUEUE) {
  720. rawcmd |= ((2 << 11) | (1 << 13));
  721. //} else if ( (opcode==MMC_CMD_SEND_STATUS) && (cmd->arg&SEND_QUEUE_STATUS_SQS_BIT_SHIFT) ) {
  722. #endif
  723. } else if (opcode == MMC_CMD_PROGRAM_CID) {
  724. rawcmd |= ((1 << 11) | (1 << 13));
  725. }
  726. /* autocmd23 with packed cmd, this feature is conflict with data tag, reliable write, and force flush cache */
  727. if (host->card && mmc_card_mmc(host->card)) {
  728. blknum = MSDC_READ32(SDC_BLK_NUM);
  729. if (priv->autocmd & MSDC_AUTOCMD23) {
  730. #if MSDC_USE_DATA_TAG
  731. blknum |= (1 << 29);
  732. blknum &= ~(1 << 30);
  733. #endif
  734. #if MSDC_USE_RELIABLE_WRITE
  735. if (opcode == MMC_CMD_WRITE_MULTIPLE_BLOCK) {
  736. blknum |= (1 << 31);
  737. blknum &= ~(1 << 30);
  738. } else {
  739. blknum &= ~(1 << 31);
  740. }
  741. #endif
  742. #if MSDC_USE_FORCE_FLUSH
  743. blknum |= (1 << 24);
  744. blknum &= ~(1 << 30);
  745. #endif
  746. #if MSDC_USE_PACKED_CMD
  747. blknum &= ~0xffff;
  748. blknum |= (1 << 30);
  749. #endif
  750. }
  751. if (priv->cmd23_flags & MSDC_RELIABLE_WRITE) {
  752. blknum |= (1 << 31);
  753. blknum &= ~(1 << 30);
  754. }
  755. MSDC_WRITE32(SDC_BLK_NUM, blknum);
  756. }
  757. MSG(CMD, "[SD%d] CMD(%d): ARG(0x%x), RAW(0x%x), BLK_NUM(0x%x) RSP(%d)\n",
  758. host->id, (opcode & ~(SD_CMD_BIT | SD_CMD_APP_BIT)), cmd->arg, rawcmd, MSDC_READ32(SDC_BLK_NUM), rsptyp);
  759. #ifdef FEATURE_MMC_CMDQ
  760. if ( cmdq_reg_setting ) {
  761. //cmdq_reg_setting|=(0x6a<<22)|(0x6a<<12)|(msdc_rsp[rsptyp]<<7);
  762. cmdq_reg_setting|=(msdc_rsp[rsptyp]<<7);
  763. //MSG(INF, "busy status: %x\n", MSDC_READ32(SDC_STS));
  764. if (SDC_IS_CMD_BUSY()) {
  765. WAIT_COND(!SDC_IS_CMD_BUSY(), cmd->timeout, timeout);
  766. if (timeout == 0) {
  767. error = MMC_ERR_TIMEOUT;
  768. msdc_pr_err("[SD%d] CMD(%d): SDC_IS_CMD_BUSY timeout\n",
  769. host->id, (opcode & ~(SD_CMD_BIT | SD_CMD_APP_BIT)));
  770. goto end;
  771. }
  772. }
  773. MSDC_SET_FIELD(EMMC51_CFG0, 0xfff, (cmdq_reg_setting));
  774. SDC_SEND_CMD(0, cmd->arg);
  775. goto end;
  776. }
  777. #endif
  778. if (opcode == MMC_CMD_SEND_STATUS) {
  779. if (SDC_IS_CMD_BUSY()) {
  780. WAIT_COND(!SDC_IS_CMD_BUSY(), cmd->timeout, timeout);
  781. if (timeout == 0) {
  782. error = MMC_ERR_TIMEOUT;
  783. msdc_pr_err("[SD%d] CMD(%d): SDC_IS_CMD_BUSY timeout\n",
  784. host->id, (opcode & ~(SD_CMD_BIT | SD_CMD_APP_BIT)));
  785. goto end;
  786. }
  787. }
  788. } else {
  789. if (SDC_IS_BUSY()) {
  790. WAIT_COND(!SDC_IS_BUSY(), 1000, timeout);
  791. if (timeout == 0) {
  792. error = MMC_ERR_TIMEOUT;
  793. msdc_pr_err("[SD%d] CMD(%d): SDC_IS_BUSY timeout\n",
  794. host->id, (opcode & ~(SD_CMD_BIT | SD_CMD_APP_BIT)));
  795. goto end;
  796. }
  797. }
  798. }
  799. #ifdef FEATURE_MMC_CMDQ
  800. MSDC_SET_FIELD(EMMC51_CFG0, 0xfff, 0);
  801. #endif
  802. SDC_SEND_CMD(rawcmd, cmd->arg);
  803. end:
  804. cmd->error = error;
  805. return error;
  806. }
  807. int msdc_wait_rsp(struct mmc_host *host, struct mmc_command *cmd)
  808. {
  809. u32 base = host->base;
  810. u32 rsptyp = cmd->rsptyp;
  811. u32 status;
  812. u32 opcode = (cmd->opcode & ~(SD_CMD_BIT | SD_CMD_APP_BIT));
  813. u32 error = MMC_ERR_NONE;
  814. u32 wints = MSDC_INT_CMDTMO | MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR |
  815. MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO |
  816. MSDC_INT_ACMD19_DONE;
  817. MSDC_UNUSED(opcode);
  818. #if defined(FEATURE_MMC_SDIO)
  819. wints |= MSDC_INT_SDIOIRQ;
  820. #endif
  821. if (cmd->opcode == MMC_CMD_GO_IRQ_STATE)
  822. wints |= MSDC_INT_MMCIRQ;
  823. status = msdc_intr_wait(host, wints);
  824. #if defined(FEATURE_MMC_SDIO)
  825. if (status & MSDC_INT_SDIOIRQ) {
  826. if (mmc_card_sdio(host->card)) {
  827. struct sdio_func *func = host->card->io_func[0];
  828. if (func->irq_handler)
  829. func->irq_handler(func);
  830. }
  831. }
  832. #endif
  833. if (status == 0) {
  834. error = MMC_ERR_TIMEOUT;
  835. goto end;
  836. }
  837. if ((status & MSDC_INT_RSPCRCERR) || (status & MSDC_INT_ACMDCRCERR)) {
  838. error = MMC_ERR_BADCRC;
  839. MSG(RSP, "[SD%d] CMD(%d): RSP(%d) ERR(BADCRC)\n",
  840. host->id, opcode, cmd->rsptyp);
  841. } else if ((status & MSDC_INT_CMDTMO) || (status & MSDC_INT_ACMDTMO)) {
  842. error = MMC_ERR_TIMEOUT;
  843. MSG(RSP, "[SD%d] CMD(%d): RSP(%d) ERR(CMDTO) AUTO(%d)\n",
  844. host->id, opcode, cmd->rsptyp, status & MSDC_INT_ACMDTMO ? 1: 0);
  845. } else if ((status & MSDC_INT_CMDRDY) || (status & MSDC_INT_ACMDRDY) ||
  846. (status & MSDC_INT_ACMD19_DONE)) {
  847. switch (rsptyp) {
  848. case RESP_NONE:
  849. MSG(RSP, "[SD%d] CMD(%d): RSP(%d)\n", host->id, opcode, rsptyp);
  850. break;
  851. case RESP_R2:
  852. {
  853. u32 *resp = &cmd->resp[0];
  854. *resp++ = MSDC_READ32(SDC_RESP3);
  855. *resp++ = MSDC_READ32(SDC_RESP2);
  856. *resp++ = MSDC_READ32(SDC_RESP1);
  857. *resp++ = MSDC_READ32(SDC_RESP0);
  858. MSG(RSP, "[SD%d] CMD(%d): RSP(%d) = 0x%x 0x%x 0x%x 0x%x\n",
  859. host->id, opcode, cmd->rsptyp, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
  860. break;
  861. }
  862. default: /* Response types 1, 3, 4, 5, 6, 7(1b) */
  863. if ((status & MSDC_INT_ACMDRDY) || (status & MSDC_INT_ACMD19_DONE))
  864. cmd->resp[0] = MSDC_READ32(SDC_ACMD_RESP);
  865. else
  866. cmd->resp[0] = MSDC_READ32(SDC_RESP0);
  867. MSG(RSP, "[SD%d] CMD(%d): RSP(%d) = 0x%x AUTO(%d)\n", host->id, opcode,
  868. cmd->rsptyp, cmd->resp[0],
  869. ((status & MSDC_INT_ACMDRDY) || (status & MSDC_INT_ACMD19_DONE)) ? 1 : 0);
  870. break;
  871. }
  872. } else {
  873. error = MMC_ERR_INVALID;
  874. MSG(INF, "[SD%d] CMD(%d): RSP(%d) ERR(INVALID), Status:%x\n",
  875. host->id, opcode, cmd->rsptyp, status);
  876. }
  877. end:
  878. if (rsptyp == RESP_R1B) {
  879. while ((MSDC_READ32(MSDC_PS) & 0x10000) != 0x10000);
  880. }
  881. #if MSDC_DEBUG
  882. if ((error == MMC_ERR_NONE) && (MSG_EVT_MASK & MSG_EVT_RSP)) {
  883. switch(cmd->rsptyp) {
  884. case RESP_R1:
  885. case RESP_R1B:
  886. msdc_dump_card_status(cmd->resp[0]);
  887. break;
  888. case RESP_R3:
  889. msdc_dump_ocr_reg(cmd->resp[0]);
  890. break;
  891. case RESP_R5:
  892. msdc_dump_io_resp(cmd->resp[0]);
  893. break;
  894. case RESP_R6:
  895. msdc_dump_rca_resp(cmd->resp[0]);
  896. break;
  897. }
  898. }
  899. #endif
  900. cmd->error = error;
  901. if (cmd->opcode == MMC_CMD_APP_CMD && error == MMC_ERR_NONE) {
  902. host->app_cmd = 1;
  903. host->app_cmd_arg = cmd->arg;
  904. } else {
  905. host->app_cmd = 0;
  906. }
  907. #ifdef FEATURE_MMC_CMDQ
  908. if ( (opcode == MMC_SET_QUEUE_CONTEXT) ||
  909. (opcode == MMC_SET_QUEUE_ADDRESS) ||
  910. (opcode == MMC_CMD_SEND_STATUS) ) {
  911. //MSDC_WRITE32(EMMC51_CFG0, 0);
  912. }
  913. #endif
  914. #ifdef FEATURE_MMC_CMDQ
  915. //MSDC_SET_FIELD(EMMC51_CFG0, 0xfff, 0);
  916. #endif
  917. return error;
  918. }
  919. int msdc_cmd(struct mmc_host *host, struct mmc_command *cmd)
  920. {
  921. int err;
  922. err = msdc_send_cmd(host, cmd);
  923. if (err != MMC_ERR_NONE)
  924. return err;
  925. err = msdc_wait_rsp(host, cmd);
  926. if (err == MMC_ERR_BADCRC) {
  927. u32 base = host->base;
  928. u32 tmp = MSDC_READ32(SDC_CMD);
  929. /* check if data is used by the command or not */
  930. if (tmp & SDC_CMD_DTYP) {
  931. msdc_abort_handler(host, 1);
  932. }
  933. #if defined(FEATURE_MMC_CM_TUNING)
  934. //Light: For CMD17/18/24/25, tuning may have been done by
  935. // msdc_abort_handler()->msdc_get_card_status()->msdc_cmd() for CMD13->msdc_tune_cmdrsp().
  936. // This means that 2nd invocation of msdc_tune_cmdrsp() occurs here!
  937. //--> To Do: consider if 2nd invocation can be avoid
  938. if (host->app_cmd != 2) { //Light 20121225, to prevent recursive call path: msdc_tune_cmdrsp->msdc_app_cmd->msdc_cmd->msdc_tune_cmdrsp
  939. err = msdc_tune_cmdrsp(host, cmd);
  940. if (err != MMC_ERR_NONE) {
  941. msdc_pr_err("[%s:%d]tune cmd fail\n", __func__, __LINE__);
  942. }
  943. }
  944. /* After tuning, erase sequence will error */
  945. if ((cmd->opcode == MMC_CMD_ERASE_GROUP_START) || (cmd->opcode == MMC_CMD_ERASE_GROUP_END) ||
  946. (cmd->opcode == MMC_CMD_ERASE_WR_BLK_START) || (cmd->opcode == MMC_CMD_ERASE_WR_BLK_END)) {
  947. err = MMC_ERR_ERASE_SEQ;
  948. }
  949. #endif
  950. }
  951. return err;
  952. }
  953. int msdc_cmd_stop(struct mmc_host *host, struct mmc_command *cmd)
  954. {
  955. struct mmc_command stop;
  956. u32 err;
  957. if (mmc_card_mmc(host->card) && cmd && (cmd->opcode == 18))
  958. stop.rsptyp = RESP_R1;
  959. else
  960. stop.rsptyp = RESP_R1B;
  961. stop.opcode = MMC_CMD_STOP_TRANSMISSION;
  962. stop.arg = 0;
  963. stop.retries = CMD_RETRIES;
  964. stop.timeout = CMD_TIMEOUT;
  965. err = msdc_cmd(host, &stop);
  966. #if defined(FEATURE_MMC_POWER_ON_WP) || defined(MTK_EMMC_SUPPORT_OTP)
  967. if ((err == MMC_ERR_NONE) && (stop.resp[0] & R1_WP_VIOLATION)) {
  968. err = MMC_ERR_WP_VIOLATION;
  969. }
  970. #endif
  971. return err;
  972. }
  973. #if defined(FEATURE_MMC_SDIO)
  974. int msdc_cmd_io_abort(struct mmc_host *host)
  975. {
  976. struct mmc_command abort;
  977. memset(&abort, 0, sizeof(struct mmc_command));
  978. abort.opcode = SD_IO_RW_DIRECT;
  979. abort.arg = 0x80000000; /* write */
  980. abort.arg |= 0 << 28; /* function 0 */
  981. abort.arg |= SDIO_CCCR_ABORT << 9; /* address */
  982. abort.arg |= 0; /* abort function 0 */
  983. abort.rsptyp = RESP_R1B;
  984. abort.retries = CMD_RETRIES;
  985. abort.timeout = CMD_TIMEOUT;
  986. return msdc_cmd(host, &abort);
  987. }
  988. #endif
  989. static int msdc_get_card_status(struct mmc_host *host, u32 *status)
  990. {
  991. int err;
  992. struct mmc_command cmd;
  993. cmd.opcode = MMC_CMD_SEND_STATUS;
  994. cmd.arg = host->card->rca << 16;
  995. cmd.rsptyp = RESP_R1;
  996. cmd.retries = CMD_RETRIES;
  997. cmd.timeout = CMD_TIMEOUT;
  998. err = msdc_cmd(host, &cmd);
  999. if (err == MMC_ERR_NONE) {
  1000. *status = cmd.resp[0];
  1001. }
  1002. return err;
  1003. }
  1004. #if defined(FEATURE_MMC_POWER_ON_WP) || defined(MTK_EMMC_SUPPORT_OTP)
  1005. int msdc_get_err_from_card_status(struct mmc_host *host)
  1006. {
  1007. u32 status;
  1008. int err = msdc_get_card_status(host, &status);
  1009. if (err == MMC_ERR_NONE) {
  1010. //*status = cmd.resp[0];
  1011. if (status & R1_WP_VIOLATION)
  1012. err = MMC_ERR_WP_VIOLATION;
  1013. }
  1014. return err;
  1015. }
  1016. #endif
  1017. int msdc_abort_handler(struct mmc_host *host, int abort_card)
  1018. {
  1019. u32 status = 0;
  1020. u32 state = 0;
  1021. u32 err;
  1022. if (!host->card)
  1023. return 0;
  1024. while (state != 4) { // until status to "tran"; //20130125 Comment out by Light
  1025. msdc_abort(host);
  1026. err = msdc_get_card_status(host, &status);
  1027. //To do: move the following 2 if clause into msdc_get_card_status() or write as a function
  1028. #if 0 //Light: turn if off before I verify it
  1029. //#if defined(MMC_MSDC_DRV_CTP)
  1030. if (err == MMC_ERR_BADCRC) {
  1031. msdc_pr_err("[%s:%d]cmd13 crc error\n", __func__, __LINE__);
  1032. msdc_tune_update_cmdrsp(host, count++);
  1033. if (count >= 512)
  1034. count = 0;
  1035. }
  1036. if (err == MMC_ERR_TIMEOUT) {
  1037. msdc_pr_err("[%s:%d]cmd13 timeout\n", __func__, __LINE__);
  1038. msdc_tune_update_cmdrsp(host, count++);
  1039. if (count >= 512)
  1040. count = 0;
  1041. }
  1042. #else
  1043. if (err != MMC_ERR_NONE) {
  1044. msdc_pr_err("[%s:%d]cmd13 fail\n", __func__, __LINE__);
  1045. goto out;
  1046. }
  1047. #endif
  1048. state = R1_CURRENT_STATE(status);
  1049. #if MMC_DEBUG
  1050. mmc_dump_card_status(status);
  1051. #endif
  1052. MSG(INF, "check card state<%d>\n", state);
  1053. if (state == 5 || state == 6) {
  1054. if (abort_card) {
  1055. MSG(INF, "state<%d> need cmd12 to stop\n", state);
  1056. err=msdc_cmd_stop(host, NULL);
  1057. //To do: move the following 2 if clause into msdc_cmd_stop() or write as a function
  1058. #if 0 //Light: turn if off before I verify it
  1059. //#if defined(MMC_MSDC_DRV_CTP)
  1060. if (err == MMC_ERR_BADCRC) {
  1061. msdc_pr_err("[%s:%d]cmd12 crc error\n", __func__, __LINE__);
  1062. msdc_tune_update_cmdrsp(host, count++);
  1063. if (count >= 512)
  1064. count = 0;
  1065. continue;
  1066. }
  1067. if (err == MMC_ERR_TIMEOUT) {
  1068. msdc_pr_err("[%s:%d]cmd12 timeout\n", __func__, __LINE__);
  1069. msdc_tune_update_cmdrsp(host, count++);
  1070. if (count >= 512)
  1071. count = 0;
  1072. continue;
  1073. }
  1074. #else
  1075. if (err != MMC_ERR_NONE) {
  1076. msdc_pr_err("[%s:%d]cmd12 fail\n", __func__, __LINE__);
  1077. goto out;
  1078. }
  1079. #endif
  1080. }
  1081. //break; //20130125 Light
  1082. } else if (state == 7) { // busy in programing
  1083. MSG(INF, "state<%d> card is busy\n", state);
  1084. mdelay(100);
  1085. } else if (state != 4) {
  1086. MSG(INF, "state<%d> ??? \n", state);
  1087. goto out;
  1088. }
  1089. }
  1090. msdc_abort(host);
  1091. return 0;
  1092. out:
  1093. msdc_pr_err("[SD%d] data abort failed\n",host->id);
  1094. return 1;
  1095. }
  1096. void msdc_intr_unmask(struct mmc_host *host, u32 bits)
  1097. {
  1098. u32 base = host->base;
  1099. u32 val;
  1100. val = MSDC_READ32(MSDC_INTEN);
  1101. val |= bits;
  1102. MSDC_WRITE32(MSDC_INTEN, val);
  1103. }
  1104. void msdc_intr_mask(struct mmc_host *host, u32 bits)
  1105. {
  1106. u32 base = host->base;
  1107. u32 val;
  1108. val = MSDC_READ32(MSDC_INTEN);
  1109. val &= ~bits;
  1110. MSDC_WRITE32(MSDC_INTEN, val);
  1111. }
  1112. static int msdc_app_cmd(struct mmc_host *host)
  1113. {
  1114. struct mmc_command appcmd;
  1115. int err = MMC_ERR_NONE;
  1116. int retries = 10;
  1117. appcmd.opcode = MMC_CMD_APP_CMD;
  1118. appcmd.arg = host->app_cmd_arg;
  1119. appcmd.rsptyp = RESP_R1;
  1120. appcmd.retries = CMD_RETRIES;
  1121. appcmd.timeout = CMD_TIMEOUT;
  1122. do {
  1123. err = msdc_cmd(host, &appcmd);
  1124. if (err == MMC_ERR_NONE)
  1125. break;
  1126. } while (retries--);
  1127. return err;
  1128. }
  1129. #if defined(MSDC_ENABLE_DMA_MODE)
  1130. int msdc_dma_send_sandisk_fwid(struct mmc_host *host, uchar *buf,u32 opcode, ulong nblks)
  1131. {
  1132. struct mmc_command cmd;
  1133. struct mmc_data data;
  1134. BUG_ON(nblks > host->max_phys_segs);
  1135. //MSG(OPS, "[SD%d] Read data %d blks from 0x%x\n", host->id, nblks, src);
  1136. /* send read command */
  1137. cmd.opcode = opcode;
  1138. cmd.rsptyp = RESP_R1;
  1139. cmd.arg = 0; //src;
  1140. cmd.retries = 0;
  1141. cmd.timeout = CMD_TIMEOUT;
  1142. data.blks = nblks;
  1143. data.buf = (u8*)buf;
  1144. data.timeout = 100; /* 100ms */
  1145. return msdc_dma_transfer(host, &cmd, &data);
  1146. }
  1147. #endif
  1148. int msdc_pio_read(struct mmc_host *host, u32 *ptr, u32 size)
  1149. {
  1150. int err = MMC_ERR_NONE;
  1151. #if defined(MMC_MSDC_DRV_CTP)
  1152. u8 *ptr8 = (u8 *)ptr;
  1153. u16 *ptr16 = (u16 *)ptr;
  1154. u32 pio_bits = ((msdc_priv_t*)host->priv)->pio_bits;
  1155. #else
  1156. u32 pio_bits = 32;
  1157. #endif
  1158. u32 base = host->base;
  1159. u32 ints = MSDC_INT_DATCRCERR | MSDC_INT_DATTMO | MSDC_INT_XFER_COMPL;
  1160. //u32 timeout = 100000;
  1161. u32 status;
  1162. u32 totalsz = size;
  1163. u8 done = 0;
  1164. u32 size_per_round;
  1165. u32 dcrc;
  1166. u8* u8ptr;
  1167. #if defined(FEATURE_MMC_SDIO)
  1168. ints |= MSDC_INT_SDIOIRQ;
  1169. #endif
  1170. while (1) {
  1171. #if defined(MSDC_USE_IRQ)
  1172. //For CTP only
  1173. DisableIRQ();
  1174. status = msdc_irq_sts[host->id];
  1175. msdc_irq_sts[host->id] &= ~ints;
  1176. EnableIRQ();
  1177. #else
  1178. status = MSDC_READ32(MSDC_INT);
  1179. MSDC_WRITE32(MSDC_INT, status);
  1180. #if defined(FEATURE_MMC_SDIO)
  1181. if (status & MSDC_INT_SDIOIRQ) {
  1182. MSG(INF, "(%s)INT status:0x%x\n", __func__, status);
  1183. if ( (host->id == 2) || (host->id == 3) ) {
  1184. mmc_sdio_proc_pending_irqs(host->card);
  1185. //sdio_read_pending_irq(host->card->io_func[0]);
  1186. }
  1187. }
  1188. #endif
  1189. #endif
  1190. if (status & ~ints) {
  1191. MSG(WRN, "[SD%d]<CHECKME> Unexpected INT(0x%x)\n",
  1192. host->id, status);
  1193. }
  1194. if (status & MSDC_INT_DATCRCERR) {
  1195. MSDC_GET_FIELD(SDC_DCRC_STS, SDC_DCRC_STS_POS|SDC_DCRC_STS_NEG, dcrc);
  1196. msdc_pr_err("[SD%d] DAT CRC error (0x%x), Left:%d/%d bytes, RXFIFO:%d,dcrc:0x%x\n",
  1197. host->id, status, size, totalsz, MSDC_RXFIFOCNT(),dcrc);
  1198. err = MMC_ERR_BADCRC;
  1199. break;
  1200. } else if (status & MSDC_INT_DATTMO) {
  1201. msdc_pr_err("[SD%d] DAT TMO error (0x%x), Left: %d/%d bytes, RXFIFO:%d\n",
  1202. host->id, status, size, totalsz, MSDC_RXFIFOCNT());
  1203. err = MMC_ERR_TIMEOUT;
  1204. break;
  1205. } else if (status & MSDC_INT_ACMDCRCERR) {
  1206. MSDC_GET_FIELD(SDC_DCRC_STS, SDC_DCRC_STS_POS|SDC_DCRC_STS_NEG, dcrc);
  1207. msdc_pr_err("[SD%d] AUTOCMD CRC error (0x%x), Left:%d/%d bytes, RXFIFO:%d,dcrc:0x%x\n",
  1208. host->id, status, size, totalsz, MSDC_RXFIFOCNT(),dcrc);
  1209. err = MMC_ERR_ACMD_RSPCRC;
  1210. break;
  1211. } else if (status & MSDC_INT_XFER_COMPL) {
  1212. done = 1;
  1213. }
  1214. if (size == 0 && done)
  1215. break;
  1216. /* Note. RXFIFO count would be aligned to 4-bytes alignment size */
  1217. //if ((size >= MSDC_FIFO_THD) && (MSDC_RXFIFOCNT() >= MSDC_FIFO_THD))
  1218. if (size > 0) {
  1219. int left;
  1220. if ( (size >= MSDC_FIFO_THD) && (MSDC_RXFIFOCNT() >= MSDC_FIFO_THD) )
  1221. left = MSDC_FIFO_THD;
  1222. else if ( (size < MSDC_FIFO_THD) && (MSDC_RXFIFOCNT() >= size) )
  1223. left = size;
  1224. else
  1225. continue;
  1226. size_per_round = left;
  1227. #if defined(MMC_MSDC_DRV_CTP)
  1228. if (pio_bits == 8) {
  1229. do {
  1230. #ifdef MTK_MSDC_DUMP_FIFO
  1231. MSG(INF, "0x%x ",MSDC_FIFO_READ8());
  1232. #else
  1233. *ptr8++ = MSDC_FIFO_READ8();
  1234. #endif
  1235. left--;
  1236. } while (left);
  1237. } else if (pio_bits == 16) {
  1238. do {
  1239. if (left > 1) {
  1240. #ifdef MTK_MSDC_DUMP_FIFO
  1241. MSG(INF, "0x%x ", MSDC_FIFO_READ16());
  1242. #else
  1243. *ptr16++ = MSDC_FIFO_READ16();
  1244. #endif
  1245. left-=2;
  1246. } else {
  1247. u8ptr = (u8*)ptr;
  1248. while (left--) {
  1249. #ifdef MTK_MSDC_DUMP_FIFO
  1250. MSG(INF, "0x%x ",MSDC_FIFO_READ8());
  1251. #else
  1252. *u8ptr++ = MSDC_FIFO_READ8();
  1253. #endif
  1254. }
  1255. }
  1256. } while (left);
  1257. } else
  1258. #endif
  1259. { //if (pio_bits==32 )
  1260. do {
  1261. if (left > 3) {
  1262. #ifdef MTK_MSDC_DUMP_FIFO
  1263. MSG(INF, "0x%x ", MSDC_FIFO_READ32());
  1264. #else
  1265. *ptr++ = MSDC_FIFO_READ32();
  1266. #endif
  1267. left-=4;
  1268. } else {
  1269. u8ptr = (u8*)ptr;
  1270. while (left--) {
  1271. #ifdef MTK_MSDC_DUMP_FIFO
  1272. MSG(INF, "0x%x ", MSDC_FIFO_READ8());
  1273. #else
  1274. *u8ptr++ = MSDC_FIFO_READ8();
  1275. #endif
  1276. }
  1277. }
  1278. } while (left);
  1279. }
  1280. size -= size_per_round;
  1281. //MSG(FIO, "[SD%d] Read %d bytes, RXFIFOCNT: %d, Left: %d/%d\n",
  1282. // host->id, size_per_round, MSDC_RXFIFOCNT(), size, totalsz);
  1283. }
  1284. }
  1285. if (err != MMC_ERR_NONE) {
  1286. msdc_abort(host); /* reset internal fifo and state machine */
  1287. msdc_pr_err("[SD%d] %d-bit PIO Read Error (%d)\n", host->id,
  1288. pio_bits, err);
  1289. }
  1290. return err;
  1291. }
  1292. int msdc_pio_write(struct mmc_host *host, u32 *ptr, u32 size)
  1293. {
  1294. int err = MMC_ERR_NONE;
  1295. u8 *ptr8=(u8 *)ptr;
  1296. u32 base = host->base;
  1297. u32 ints = MSDC_INT_DATCRCERR | MSDC_INT_DATTMO | MSDC_INT_XFER_COMPL;
  1298. //u32 timeout = 250000;
  1299. u32 status;
  1300. #if defined(MMC_MSDC_DRV_CTP)
  1301. u32 pio_bits = ((msdc_priv_t*)host->priv)->pio_bits;
  1302. #else
  1303. u32 pio_bits = 32;
  1304. #endif
  1305. u32 size_per_round;
  1306. MSDC_UNUSED(pio_bits);
  1307. #if defined(FEATURE_MMC_SDIO)
  1308. ints |= MSDC_INT_SDIOIRQ;
  1309. #endif
  1310. while (1) {
  1311. #if defined(MSDC_USE_IRQ)
  1312. //For CTP only
  1313. DisableIRQ();
  1314. status = msdc_irq_sts[host->id];
  1315. msdc_irq_sts[host->id] &= ~ints;
  1316. EnableIRQ();
  1317. #else
  1318. status = MSDC_READ32(MSDC_INT);
  1319. MSDC_WRITE32(MSDC_INT, status);
  1320. #if defined(FEATURE_MMC_SDIO)
  1321. if (status & MSDC_INT_SDIOIRQ) {
  1322. MSG(INF, "(%s)INT status:0x%x\n", __func__, status);
  1323. if ( (host->id == 2) || (host->id == 3) ) {
  1324. mmc_sdio_proc_pending_irqs(host->card);
  1325. //sdio_read_pending_irq(host->card->io_func[0]);
  1326. }
  1327. }
  1328. #endif
  1329. #endif
  1330. if (status & ~ints) {
  1331. MSG(WRN, "[SD%d]<CHECKME> Unexpected INT(0x%x)\n",
  1332. host->id, status);
  1333. }
  1334. if (status & MSDC_INT_DATCRCERR) {
  1335. msdc_pr_err("[SD%d] DAT CRC error (0x%x), Left DAT: %d bytes\n",
  1336. host->id, status, size);
  1337. err = MMC_ERR_BADCRC;
  1338. break;
  1339. } else if (status & MSDC_INT_DATTMO) {
  1340. msdc_pr_err("[SD%d] DAT TMO error (0x%x), Left DAT: %d bytes, MSDC_FIFOCS=%xh\n",
  1341. host->id, status, size, MSDC_READ32(MSDC_FIFOCS));
  1342. err = MMC_ERR_TIMEOUT;
  1343. break;
  1344. } else if (status & MSDC_INT_ACMDCRCERR) {
  1345. msdc_pr_err("[SD%d] AUTO CMD CRC error (0x%x), Left DAT: %d bytes\n",
  1346. host->id, status, size);
  1347. err = MMC_ERR_ACMD_RSPCRC;
  1348. break;
  1349. } else if (status & MSDC_INT_XFER_COMPL) {
  1350. if (size == 0) {
  1351. MSG(OPS, "[SD%d] all data flushed to card\n", host->id);
  1352. break;
  1353. } else {
  1354. MSG(WRN, "[SD%d]<CHECKME> XFER_COMPL before all data written\n",
  1355. host->id);
  1356. }
  1357. }
  1358. if (size == 0)
  1359. continue;
  1360. if (MSDC_TXFIFOCNT() == 0) {
  1361. int left;
  1362. #if defined(MMC_MSDC_DRV_CTP)
  1363. if ( pio_bits==32 ) {
  1364. if ( size >= MSDC_FIFO_THD )
  1365. left = MSDC_FIFO_THD;
  1366. else
  1367. left = size;
  1368. } else
  1369. #endif
  1370. {
  1371. if ( size >= MSDC_FIFO_SZ )
  1372. left = MSDC_FIFO_SZ;
  1373. else
  1374. left = size;
  1375. }
  1376. size_per_round = left;
  1377. #if defined(MMC_MSDC_DRV_CTP)
  1378. if (pio_bits == 8) {
  1379. do {
  1380. MSDC_FIFO_WRITE8(*ptr8);
  1381. ptr8++;
  1382. left--;
  1383. } while (left);
  1384. } else if (pio_bits == 16) {
  1385. do {
  1386. if (left > 1) {
  1387. MSDC_FIFO_WRITE16(*(u16*)ptr8);
  1388. ptr8+=2;
  1389. left-=2;
  1390. } else {
  1391. while (left--) {
  1392. MSDC_FIFO_WRITE8(*ptr8);
  1393. ptr8++;
  1394. }
  1395. }
  1396. } while (left);
  1397. } else
  1398. #endif
  1399. { //if ( write_unit==4 )
  1400. do {
  1401. if (left > 3) {
  1402. MSDC_FIFO_WRITE32(*(u32*)ptr8);
  1403. ptr8+=4;
  1404. left-=4;
  1405. } else {
  1406. while (left--) {
  1407. MSDC_FIFO_WRITE8(*ptr8);
  1408. ptr8++;
  1409. }
  1410. }
  1411. } while (left);
  1412. }
  1413. size -= size_per_round;
  1414. }
  1415. }
  1416. if (err != MMC_ERR_NONE) {
  1417. msdc_abort(host); /* reset internal fifo and state machine */
  1418. MSG(OPS, "[SD%d] %d-bit PIO Write Error (%d)\n", host->id,
  1419. pio_bits, err);
  1420. }
  1421. return err;
  1422. }
  1423. #if defined(FEATURE_EMCP)
  1424. int msdc_pio_get_sandisk_fwid(struct mmc_host *host, uchar *dst)
  1425. {
  1426. u32 blksz = host->blklen;
  1427. int err = MMC_ERR_NONE, derr = MMC_ERR_NONE;
  1428. struct mmc_command cmd;
  1429. ulong *ptr = (ulong *)dst;
  1430. //MSG(OPS, "[SD%d] Read data %d bytes from 0x%x\n", host->id, nblks * blksz, src);
  1431. msdc_clr_fifo(host);
  1432. msdc_set_blknum(host, 1);
  1433. msdc_set_blklen(host, blksz);
  1434. msdc_set_timeout(host, 100000000, 0);
  1435. /* send read command */
  1436. cmd.opcode = MMC_CMD21;
  1437. cmd.rsptyp = RESP_R1;
  1438. cmd.arg = 0;
  1439. cmd.retries = 0;
  1440. cmd.timeout = CMD_TIMEOUT;
  1441. err = msdc_cmd(host, &cmd);
  1442. if (err != MMC_ERR_NONE)
  1443. goto done;
  1444. err = derr = msdc_pio_read(host, (u32*)ptr, 1 * blksz);
  1445. done:
  1446. if (err != MMC_ERR_NONE) {
  1447. if (derr != MMC_ERR_NONE) {
  1448. msdc_pr_err("[SD%d] Read data error (%d)\n", host->id, derr);
  1449. msdc_abort_handler(host, 1);
  1450. } else {
  1451. msdc_pr_err("[SD%d] Read error (%d)\n", host->id, err);
  1452. }
  1453. }
  1454. return (derr == MMC_ERR_NONE) ? err : derr;
  1455. }
  1456. int msdc_pio_send_sandisk_fwid(struct mmc_host *host,uchar *src)
  1457. {
  1458. int err = MMC_ERR_NONE, derr = MMC_ERR_NONE;
  1459. u32 blksz = host->blklen;
  1460. struct mmc_command cmd;
  1461. ulong *ptr = (ulong *)src;
  1462. //MSG(OPS, "[SD%d] Write data %d bytes to 0x%x\n", host->id, nblks * blksz, dst);
  1463. msdc_clr_fifo(host);
  1464. msdc_set_blknum(host, 1);
  1465. msdc_set_blklen(host, blksz);
  1466. /* No need since MSDC always waits 8 cycles for write data timeout */
  1467. /* send write command */
  1468. cmd.opcode = MMC_CMD50;
  1469. cmd.rsptyp = RESP_R1;
  1470. cmd.arg = 0;
  1471. cmd.retries = 0;
  1472. cmd.timeout = CMD_TIMEOUT;
  1473. err = msdc_cmd(host, &cmd);
  1474. if (err != MMC_ERR_NONE)
  1475. goto done;
  1476. err = derr = msdc_pio_write(host, (u32*)ptr, 1 * blksz);
  1477. done:
  1478. if (err != MMC_ERR_NONE) {
  1479. if (derr != MMC_ERR_NONE) {
  1480. msdc_pr_err("[SD%d] Write data error (%d)\n", host->id, derr);
  1481. msdc_abort_handler(host, 1);
  1482. } else {
  1483. msdc_pr_err("[SD%d] Write error (%d)\n", host->id, err);
  1484. }
  1485. }
  1486. return (derr == MMC_ERR_NONE) ? err : derr;
  1487. }
  1488. #endif
  1489. int msdc_pio_bread(struct mmc_host *host, uchar *dst, ulong src, ulong nblks)
  1490. {
  1491. msdc_priv_t *priv = (msdc_priv_t*)host->priv;
  1492. u32 blksz = host->blklen;
  1493. int err = MMC_ERR_NONE, derr = MMC_ERR_NONE, cmd_err = MMC_ERR_NONE;
  1494. int multi;
  1495. struct mmc_command cmd;
  1496. ulong *ptr = (ulong *)dst;
  1497. MSG(OPS, "[SD%d] Read data %d bytes from 0x%x\n", host->id, (unsigned int)nblks * blksz, src);
  1498. multi = nblks > 1 ? 1 : 0;
  1499. msdc_clr_fifo(host);
  1500. msdc_set_blknum(host, nblks);
  1501. msdc_set_blklen(host, blksz);
  1502. msdc_set_timeout(host, 100000000, 0);
  1503. /* send read command */
  1504. cmd.opcode = multi ? MMC_CMD_READ_MULTIPLE_BLOCK : MMC_CMD_READ_SINGLE_BLOCK;
  1505. /* CMD23 with length only 1 */
  1506. if (priv->autocmd & MSDC_AUTOCMD23)
  1507. cmd.opcode = MMC_CMD_READ_MULTIPLE_BLOCK;
  1508. cmd.rsptyp = RESP_R1;
  1509. cmd.arg = src;
  1510. cmd.retries = 0;
  1511. cmd.timeout = CMD_TIMEOUT;
  1512. host->cmd = &cmd;
  1513. err = msdc_cmd(host, &cmd);
  1514. if (err != MMC_ERR_NONE)
  1515. goto done;
  1516. derr = msdc_pio_read(host, (u32*)ptr, nblks * blksz);
  1517. if (derr != MMC_ERR_NONE)
  1518. goto done;
  1519. if (multi && (priv->autocmd == 0)) {
  1520. cmd_err = msdc_cmd_stop(host, &cmd);
  1521. }
  1522. done:
  1523. if (err != MMC_ERR_NONE) {
  1524. /* msdc_cmd will do cmd tuning flow, so if enter here, cmd maybe timeout.
  1525. * need reset host */
  1526. //Light: msdc_abort_handler() combined from preloader/LK and CTP can not meet this purpose,
  1527. // so call msdc_abort() directly
  1528. //msdc_abort_handler(host, 0);
  1529. msdc_abort(host);
  1530. return err; // high level will retry
  1531. }
  1532. if (derr != MMC_ERR_NONE) {
  1533. /* crc error find in data transfer. need reset host & send cmd12 */
  1534. /* if autocmd crc occur, will enter here too */
  1535. msdc_abort_handler(host, 1);
  1536. return derr;
  1537. }
  1538. if (cmd_err != MMC_ERR_NONE) {
  1539. /* msdc_cmd will do cmd tuning flow, so if enter here, cmd maybe timeout
  1540. * need reset host */
  1541. //Light: msdc_abort_handler() combined from preloader/LK and CTP can not meet this purpose,
  1542. // so call msdc_abort() directly
  1543. //msdc_abort_handler(host, 0);
  1544. msdc_abort(host);
  1545. }
  1546. return MMC_ERR_NONE;
  1547. }
  1548. int msdc_pio_bwrite(struct mmc_host *host, ulong dst, uchar *src, ulong nblks)
  1549. {
  1550. msdc_priv_t *priv = (msdc_priv_t*)host->priv;
  1551. u32 base = host->base;
  1552. int err = MMC_ERR_NONE, derr = MMC_ERR_NONE, cmd_err = MMC_ERR_NONE;
  1553. int multi;
  1554. u32 blksz = host->blklen;
  1555. struct mmc_command cmd;
  1556. ulong *ptr = (ulong *)src;
  1557. MSG(OPS, "[SD%d] Write data %d bytes to 0x%x\n", host->id, (unsigned int)nblks * blksz, dst);
  1558. multi = nblks > 1 ? 1 : 0;
  1559. msdc_clr_fifo(host);
  1560. msdc_set_blknum(host, nblks);
  1561. msdc_set_blklen(host, blksz);
  1562. /* send write command */
  1563. cmd.opcode = multi ? MMC_CMD_WRITE_MULTIPLE_BLOCK : MMC_CMD_WRITE_BLOCK;
  1564. /* CMD23 with length only 1 */
  1565. if (priv->autocmd & MSDC_AUTOCMD23)
  1566. cmd.opcode = MMC_CMD_WRITE_MULTIPLE_BLOCK;
  1567. cmd.rsptyp = RESP_R1;
  1568. cmd.arg = dst;
  1569. cmd.retries = 0;
  1570. cmd.timeout = CMD_TIMEOUT;
  1571. err = msdc_cmd(host, &cmd);
  1572. if (err != MMC_ERR_NONE)
  1573. goto done;
  1574. host->cmd = &cmd;
  1575. derr = msdc_pio_write(host, (u32*)ptr, nblks * blksz);
  1576. if (multi && (priv->autocmd == 0)) {
  1577. cmd_err = msdc_cmd_stop(host, &cmd);
  1578. }
  1579. #if defined(FEATURE_MMC_POWER_ON_WP) || defined(MTK_EMMC_SUPPORT_OTP)
  1580. else if (multi && (priv->autocmd & MSDC_AUTOCMD12)) {
  1581. if (MSDC_READ32(SDC_ACMD_RESP) & R1_WP_VIOLATION) {
  1582. err = MMC_ERR_WP_VIOLATION;
  1583. goto done;
  1584. }
  1585. }
  1586. err = msdc_get_err_from_card_status(host);
  1587. #endif
  1588. done:
  1589. if (err != MMC_ERR_NONE) {
  1590. /* msdc_cmd will do cmd tuning flow, so if enter here, cmd maybe timeout.
  1591. * need reset host */
  1592. //Light: msdc_abort_handler() combined from preloader/LK and CTP can not meet this purpose,
  1593. // so call msdc_abort() directly
  1594. //msdc_abort_handler(host, 0);
  1595. msdc_abort(host);
  1596. return err; // high level will retry
  1597. }
  1598. if (derr != MMC_ERR_NONE) {
  1599. /* crc error find in data transfer. need reset host & send cmd12 */
  1600. /* if autocmd crc occur, will enter here too */
  1601. msdc_abort_handler(host, 1);
  1602. return derr;
  1603. }
  1604. if (cmd_err != MMC_ERR_NONE) {
  1605. /* msdc_cmd will do cmd tuning flow, so if enter here, cmd maybe timeout
  1606. * need reset host */
  1607. //Light: msdc_abort_handler() combined from preloader/LK and CTP can not meet this purpose,
  1608. // so call msdc_abort() directly
  1609. //msdc_abort_handler(host, 0);
  1610. msdc_abort(host);
  1611. return MMC_ERR_FAILED; // high level will retry
  1612. }
  1613. return MMC_ERR_NONE;
  1614. }
  1615. void msdc_reset_timing_register(struct mmc_host *host)
  1616. {
  1617. u32 base = host->base;
  1618. MSDC_WRITE32(MSDC_IOCON, 0x00000000);
  1619. MSDC_WRITE32(MSDC_DAT_RDDLY0, 0x00000000);
  1620. MSDC_WRITE32(MSDC_DAT_RDDLY1, 0x00000000);
  1621. MSDC_WRITE32(MSDC_DAT_RDDLY2, 0x00000000);
  1622. MSDC_WRITE32(MSDC_DAT_RDDLY3, 0x00000000);
  1623. MSDC_WRITE32(MSDC_PATCH_BIT0, MSDC_PB0_DEFAULT);
  1624. MSDC_WRITE32(MSDC_PATCH_BIT1, MSDC_PB1_DEFAULT);
  1625. MSDC_WRITE32(MSDC_PATCH_BIT2, MSDC_PB2_DEFAULT);
  1626. #ifndef FEATURE_MMC_TUNING_EDGE_ONLY_WHEN_HIGH_SPEED
  1627. /* DE comment: clear SDC_FIFO_CFG[24:25] can make
  1628. * tune(modify EMMC_TOP_CONTROL & EMMC_TOP_CMD) better
  1629. */
  1630. MSDC_SET_FIELD(SDC_FIFO_CFG, SDC_FIFO_CFG_WR_VALID_SEL, 0);
  1631. MSDC_SET_FIELD(SDC_FIFO_CFG, SDC_FIFO_CFG_RD_VALID_SEL, 0);
  1632. #endif
  1633. MSDC_WRITE32(MSDC_PAD_TUNE0, 0);
  1634. MSDC_WRITE32(MSDC_PAD_TUNE1, 0);
  1635. }
  1636. void msdc_init_tune_path(struct mmc_host *host, int hs400)
  1637. {
  1638. u32 base = host->base;
  1639. MSDC_SET_FIELD(MSDC_PATCH_BIT2, MSDC_PB2_RESPWAITCNT, 3);
  1640. MSDC_CLR_BIT32(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_RXDLYSEL);
  1641. MSDC_CLR_BIT32(MSDC_IOCON, MSDC_IOCON_DDLSEL);
  1642. MSDC_CLR_BIT32(MSDC_IOCON, MSDC_IOCON_R_D_SMPL_SEL);
  1643. #if !defined(FPGA_PLATFORM)
  1644. MSDC_CLR_BIT32(MSDC_IOCON, MSDC_IOCON_R_D_SMPL);
  1645. MSDC_CLR_BIT32(MSDC_IOCON, MSDC_IOCON_R_D_SMPL_SEL);
  1646. #else
  1647. MSDC_SET_BIT32(MSDC_IOCON, MSDC_IOCON_R_D_SMPL);
  1648. MSDC_CLR_BIT32(MSDC_PATCH_BIT0, MSDC_PB0_RD_DAT_SEL);
  1649. #endif
  1650. #if defined(MMC_MSDC_DRV_CTP) || defined(SLT)
  1651. if (hs400) {
  1652. MSDC_CLR_BIT32(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_DATRRDLYSEL);
  1653. MSDC_CLR_BIT32(MSDC_PAD_TUNE1, MSDC_PAD_TUNE1_DATRRDLY2SEL);
  1654. } else
  1655. #endif
  1656. {
  1657. MSDC_SET_BIT32(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_DATRRDLYSEL);
  1658. MSDC_CLR_BIT32(MSDC_PAD_TUNE1, MSDC_PAD_TUNE1_DATRRDLY2SEL);
  1659. }
  1660. #if defined(MMC_MSDC_DRV_CTP) || defined(SLT)
  1661. if (hs400)
  1662. MSDC_CLR_BIT32(MSDC_PATCH_BIT2, MSDC_PB2_CFGCRCSTS);
  1663. else
  1664. #endif
  1665. MSDC_SET_BIT32(MSDC_PATCH_BIT2, MSDC_PB2_CFGCRCSTS);
  1666. MSDC_CLR_BIT32(MSDC_IOCON, MSDC_IOCON_W_D_SMPL_SEL);
  1667. MSDC_CLR_BIT32(MSDC_PATCH_BIT2, MSDC_PB2_CFGRESP);
  1668. MSDC_SET_BIT32(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_CMDRRDLYSEL);
  1669. MSDC_CLR_BIT32(MSDC_PAD_TUNE1, MSDC_PAD_TUNE1_CMDRRDLY2SEL);
  1670. if (host->id != 1)
  1671. MSDC_CLR_BIT32(EMMC50_CFG0, MSDC_EMMC50_CFG_CMD_RESP_SEL);
  1672. MSDC_SET_FIELD(MSDC_PATCH_BIT1, MSDC_PB0_CKGEN_MSDC_DLY_SEL, 0);
  1673. MSDC_SET_FIELD(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR,
  1674. CMD_RSP_TA_CNTR_DEFAULT);
  1675. MSDC_SET_FIELD(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR,
  1676. WRDAT_CRCS_TA_CNTR_DEFAULT);
  1677. MSDC_SET_FIELD(MSDC_PATCH_BIT1, MSDC_PB1_GET_BUSY_MA,
  1678. BUSY_MA_DEFAULT);
  1679. #if !defined(FPGA_PLATFORM)
  1680. #if defined(MMC_MSDC_DRV_CTP)
  1681. if (hs400) {
  1682. MSDC_SET_FIELD(MSDC_PATCH_BIT2, MSDC_PB2_CRCSTSENSEL,
  1683. CRCSTSENSEL_HS400_DEFAULT);
  1684. MSDC_SET_FIELD(MSDC_PATCH_BIT2, MSDC_PB2_RESPSTENSEL,
  1685. RESPSTENSEL_HS400_DEFAULT);
  1686. } else
  1687. #endif
  1688. {
  1689. MSDC_SET_FIELD(MSDC_PATCH_BIT2, MSDC_PB2_CRCSTSENSEL,
  1690. CRCSTSENSEL_HS_DEFAULT);
  1691. MSDC_SET_FIELD(MSDC_PATCH_BIT2, MSDC_PB2_RESPSTENSEL,
  1692. RESPSTENSEL_HS_DEFAULT);
  1693. }
  1694. #else
  1695. if (!hs400) {
  1696. MSDC_SET_FIELD(MSDC_PATCH_BIT2, MSDC_PB2_CRCSTSENSEL,
  1697. CRCSTSENSEL_FPGA_DEFAULT);
  1698. }
  1699. #endif
  1700. MSDC_SET_BIT32(MSDC_PATCH_BIT1, MSDC_PB1_DDR_CMD_FIX_SEL);
  1701. /* DDR50 mode */
  1702. MSDC_SET_BIT32(MSDC_PATCH_BIT2, MSDC_PB2_DDR50_SEL);
  1703. /* set SDC_RX_ENHANCE_EN for async-fifo RX tune */
  1704. if (!host->base_top)
  1705. MSDC_SET_FIELD(SDC_ADV_CFG0, SDC_ADV_CFG0_SDC_RX_ENH_EN, 1);
  1706. else
  1707. MSDC_SET_FIELD(EMMC_TOP_CONTROL, SDC_RX_ENH_EN, 1);
  1708. }
  1709. void msdc_config_clock(struct mmc_host *host, int ddr, u32 hz, u32 hs_timing)
  1710. {
  1711. msdc_priv_t *priv = host->priv;
  1712. u32 base = host->base;
  1713. u32 mode, hs400_div_dis = 0;
  1714. u32 div;
  1715. u32 sclk;
  1716. u32 orig_clksrc = host->pll_mux_clk;
  1717. if (hz >= host->f_max) {
  1718. hz = host->f_max;
  1719. } else if (hz < host->f_min) {
  1720. hz = host->f_min;
  1721. }
  1722. #if defined(MMC_MSDC_DRV_CTP) || defined(SLT)
  1723. if (hs_timing & EXT_CSD_HS_TIMEING_HS400) {
  1724. mode = 0x3; /* HS400 mode */
  1725. host->pll_mux_clk = MSDC0_CLKSRC_DEFAULT;
  1726. host->src_clk = msdc_get_hclk(host->id, MSDC0_CLKSRC_DEFAULT);
  1727. if (hz >= host->src_clk / 2) {
  1728. hs400_div_dis = 1;
  1729. div = 0;
  1730. sclk = host->src_clk >> 1; // use 400Mhz source
  1731. } else {
  1732. hs400_div_dis = 0;
  1733. if (hz >= (host->src_clk >> 2)) {
  1734. div = 0; /* mean div = 1/2 */
  1735. sclk = host->src_clk >> 2; /* sclk = clk/div/2. 2: internal divisor */
  1736. } else {
  1737. div = (host->src_clk + ((hz << 2) - 1)) / (hz << 2);
  1738. sclk = (host->src_clk >> 2) / div;
  1739. div = (div >> 1); /* since there is 1/2 internal divisor */
  1740. }
  1741. }
  1742. } else
  1743. #endif
  1744. if (ddr) {
  1745. mode = 0x2; /* ddr mode and use divisor */
  1746. if (hz >= (host->src_clk >> 2)) {
  1747. div = 0; /* mean div = 1/2 */
  1748. sclk = host->src_clk >> 2; /* sclk = clk/div/2. 2: internal divisor */
  1749. } else {
  1750. div = (host->src_clk + ((hz << 2) - 1)) / (hz << 2);
  1751. sclk = (host->src_clk >> 2) / div;
  1752. div = (div >> 1); /* since there is 1/2 internal divisor */
  1753. }
  1754. #if !defined(FPGA_PLATFORM)
  1755. } else if (hz >= host->src_clk) {
  1756. mode = 0x1; /* no divisor and divisor is ignored */
  1757. div = 0;
  1758. sclk = host->src_clk;
  1759. #endif
  1760. } else {
  1761. mode = 0x0; /* use divisor */
  1762. if (hz >= (host->src_clk >> 1)) {
  1763. div = 0; /* mean div = 1/2 */
  1764. sclk = host->src_clk >> 1; /* sclk = clk / 2 */
  1765. } else {
  1766. div = (host->src_clk + ((hz << 2) - 1)) / (hz << 2);
  1767. sclk = (host->src_clk >> 2) / div;
  1768. }
  1769. }
  1770. host->cur_bus_clk = sclk;
  1771. /* set clock mode and divisor */
  1772. MSDC_SET_FIELD(MSDC_CFG, MSDC_CFG_CKMOD_HS400 | MSDC_CFG_CKMOD | MSDC_CFG_CKDIV,
  1773. (hs400_div_dis << (MSDC_CFG_CKMOD_BITS + MSDC_CFG_CKDIV_BITS)) |
  1774. (mode << MSDC_CFG_CKDIV_BITS) | div);
  1775. msdc_config_clksrc(host, orig_clksrc);
  1776. /* wait clock stable */
  1777. while (!(MSDC_READ32(MSDC_CFG) & MSDC_CFG_CKSTB));
  1778. #if defined(MMC_MSDC_DRV_CTP) || defined(SLT)
  1779. if (hs_timing & EXT_CSD_HS_TIMEING_HS400) {
  1780. msdc_set_smpl(host, 1, priv->rsmpl, TYPE_CMD_RESP_EDGE);
  1781. msdc_set_smpl(host, 1, priv->rdsmpl, TYPE_READ_DATA_EDGE);
  1782. msdc_set_smpl(host, 1, priv->wdsmpl, TYPE_WRITE_CRC_EDGE);
  1783. } else
  1784. #endif
  1785. {
  1786. #if !defined(FPGA_PLATFORM)
  1787. msdc_set_smpl(host, 0, priv->rsmpl, TYPE_CMD_RESP_EDGE);
  1788. #else
  1789. if (hz < 1000000)
  1790. msdc_set_smpl(host, 0, MSDC_SMPL_RISING, TYPE_CMD_RESP_EDGE);
  1791. else
  1792. msdc_set_smpl(host, 0, priv->rsmpl, TYPE_CMD_RESP_EDGE);
  1793. #endif
  1794. #if !defined(FPGA_PLATFORM)
  1795. msdc_set_smpl(host, 0, priv->rdsmpl, TYPE_READ_DATA_EDGE);
  1796. #else
  1797. if (hz < 1000000)
  1798. msdc_set_smpl(host, 0, MSDC_SMPL_RISING, TYPE_READ_DATA_EDGE);
  1799. //msdc_set_smpl(host, 0, MSDC_SMPL_FALLING, TYPE_READ_DATA_EDGE);
  1800. else
  1801. msdc_set_smpl(host, 0, MSDC_SMPL_RISING, TYPE_READ_DATA_EDGE);
  1802. //msdc_set_smpl(host, 0, MSDC_SMPL_FALLING, TYPE_READ_DATA_EDGE);
  1803. #endif
  1804. msdc_set_smpl(host, 0, priv->wdsmpl, TYPE_WRITE_CRC_EDGE);
  1805. }
  1806. if (mode==2 || mode==3) {
  1807. MSDC_CLR_BIT32(MSDC_PATCH_BIT0, MSDC_PB0_RD_DAT_SEL);
  1808. } else {
  1809. MSDC_SET_BIT32(MSDC_PATCH_BIT0, MSDC_PB0_RD_DAT_SEL);
  1810. }
  1811. if (mode == 2) {
  1812. MSDC_SET_BIT32(MSDC_IOCON, MSDC_IOCON_DDR50CKD);
  1813. } else {
  1814. MSDC_CLR_BIT32(MSDC_IOCON, MSDC_IOCON_DDR50CKD);
  1815. }
  1816. msdc_init_tune_path(host, (mode ==3) ? 1 : 0);
  1817. msdc_pr_err("[SD%d] SET_CLK(%dkHz): SCLK(%dkHz) MODE(%d) DDR(%d) DIV(%d) DS(%d) RS(%d)\n",
  1818. host->id, hz/1000, sclk/1000, mode, ddr > 0 ? 1 : 0, div,
  1819. msdc_cap[host->id].data_edge, msdc_cap[host->id].cmd_edge);
  1820. }
  1821. void msdc_config_bus(struct mmc_host *host, u32 width)
  1822. {
  1823. u32 base = host->base;
  1824. u32 val = MSDC_READ32(SDC_CFG);
  1825. val &= ~SDC_CFG_BUSWIDTH;
  1826. switch (width) {
  1827. case HOST_BUS_WIDTH_1:
  1828. val |= (MSDC_BUS_1BITS << 16);
  1829. break;
  1830. case HOST_BUS_WIDTH_4:
  1831. val |= (MSDC_BUS_4BITS << 16);
  1832. break;
  1833. case HOST_BUS_WIDTH_8:
  1834. val |= (MSDC_BUS_8BITS << 16);
  1835. break;
  1836. default:
  1837. val |= (MSDC_BUS_1BITS << 16);
  1838. break;
  1839. }
  1840. MSDC_WRITE32(SDC_CFG, val);
  1841. MSG(INF, "[SD%d] Bus Width: %d\n", host->id, width);
  1842. }
  1843. #if defined(FEATURE_MMC_UHS1)
  1844. int msdc_switch_volt(struct mmc_host *host, int volt)
  1845. {
  1846. u32 base = host->base;
  1847. int err = MMC_ERR_FAILED;
  1848. u32 timeout = 1000;
  1849. u32 status;
  1850. u32 bus_clk = host->cur_bus_clk;
  1851. /* make sure SDC is not busy (TBC) */
  1852. WAIT_COND(!SDC_IS_BUSY(), timeout, timeout);
  1853. if (timeout == 0) {
  1854. err = MMC_ERR_TIMEOUT;
  1855. goto out;
  1856. }
  1857. /* check if CMD/DATA lines both 0 */
  1858. if ((MSDC_READ32(MSDC_PS) & ((1 << 24) | (0xF << 16))) == 0) {
  1859. /* change signal from 3.3v to 1.8v */
  1860. msdc_host_power(host, 1, VOL_1800);
  1861. mdelay(5);
  1862. /* start to detect volt change by providing 1.8v signal to card */
  1863. MSDC_SET_FIELD(SDC_VOL_CHG, SDC_VOL_CHG_CNT, 500);
  1864. MSDC_SET_BIT32(MSDC_CFG, MSDC_CFG_BV18SDT);
  1865. /* wait at max. 1ms */
  1866. mdelay(1);
  1867. while ((status = MSDC_READ32(MSDC_CFG)) & MSDC_CFG_BV18SDT);
  1868. if (status & MSDC_CFG_BV18PSS)
  1869. err = MMC_ERR_NONE;
  1870. else
  1871. msdc_pr_err("[%s] sd%d v18 switch failed, MSDC_CFG=0x%x\n", __func__, host->id, status);
  1872. }
  1873. out:
  1874. return err;
  1875. }
  1876. #endif
  1877. void msdc_reset_tune_counter(struct mmc_host *host)
  1878. {
  1879. host->time_read = 0;
  1880. }
  1881. #include "msdc_tune_async.c"
  1882. #if defined(MMC_MSDC_DRV_CTP)
  1883. void msdc_tune_update_cmdrsp(struct mmc_host *host, u32 count)
  1884. {
  1885. u32 base = host->base;
  1886. u32 sel = 0;
  1887. u32 rsmpl,cur_rsmpl, orig_rsmpl;
  1888. u32 rrdly,cur_rrdly, orig_rrdly;
  1889. u32 cntr,cur_cntr,orig_cmdrtc;
  1890. u32 dl_cksel, cur_dl_cksel, orig_dl_cksel;
  1891. u32 times = 0;
  1892. u8 hs400 = 0, orig_clkmode;
  1893. MSG(INF, "cur_bus_clk = %d\n", host->cur_bus_clk);
  1894. if (host->cur_bus_clk > 100000000) {
  1895. sel = 1;
  1896. }
  1897. MSDC_GET_FIELD(MSDC_IOCON, MSDC_IOCON_RSPL, orig_rsmpl);
  1898. MSDC_GET_FIELD(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_CMDRRDLY, orig_rrdly);
  1899. MSDC_GET_FIELD(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR, orig_cmdrtc);
  1900. MSDC_GET_FIELD(MSDC_PATCH_BIT0, MSDC_PB0_INT_DAT_LATCH_CK_SEL, orig_dl_cksel);
  1901. MSDC_GET_FIELD(MSDC_CFG, MSDC_CFG_CKMOD, orig_clkmode);
  1902. hs400 = (orig_clkmode == 3) ? 1 : 0;
  1903. dl_cksel = 0;
  1904. cntr = 0;
  1905. rrdly = 0;
  1906. if (sel == 1) {
  1907. if (count >= 8 * 64 && count < 8 * 8 * 64) {
  1908. dl_cksel = count % 8;
  1909. cur_dl_cksel = (orig_dl_cksel + dl_cksel + 1) % 8;
  1910. MSDC_SET_FIELD(MSDC_PATCH_BIT0, MSDC_PB0_INT_DAT_LATCH_CK_SEL, cur_dl_cksel);
  1911. count = count % (8 * 64);
  1912. }
  1913. if (count >= 64 && count < 8 * 64) {
  1914. cntr = count % 8;
  1915. cur_cntr = (orig_cmdrtc + cntr + 1) % 8;
  1916. MSDC_SET_FIELD(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR, cur_cntr);
  1917. count = count % 64;
  1918. }
  1919. }
  1920. if (count >= 2 && count < 64) {
  1921. rrdly = count % 32;
  1922. cur_rrdly = (orig_rrdly + rrdly + 1) % 32;
  1923. MSDC_SET_FIELD(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_CMDRRDLY, cur_rrdly);
  1924. count = (count > 32 ? 1 : 0);
  1925. }
  1926. if (count < 2) {
  1927. cur_rsmpl = (orig_rsmpl + count) % 2;
  1928. msdc_set_smpl(host, hs400, cur_rsmpl, TYPE_CMD_RESP_EDGE);
  1929. }
  1930. }
  1931. #endif
  1932. #if defined(MMC_MSDC_DRV_CTP)
  1933. int msdc_tune_rw_hs400(struct mmc_host *host, uchar *dst, ulong src, ulong nblks, unsigned int rw)
  1934. {
  1935. u32 ds_dly1 = 0, ds_dly3 = 0, orig_ds_dly1 = 0, orig_ds_dly3 = 0;
  1936. u32 ds_dly1_count, ds_dly3_count = 0;
  1937. int result = MMC_ERR_READTUNEFAIL;
  1938. #if MSDC_TUNE_LOG
  1939. u32 times = 0;
  1940. #endif
  1941. u32 base = host->base;
  1942. if (host->id != 0) {
  1943. return result;
  1944. }
  1945. MSG(INF, "[tune][%s:%d] start hs400 read tune\n", __func__, __LINE__);
  1946. MSDC_GET_FIELD(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY1, orig_ds_dly1);
  1947. MSDC_GET_FIELD(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY3, orig_ds_dly3);
  1948. ds_dly3 = orig_ds_dly3;
  1949. ds_dly1 = orig_ds_dly1;
  1950. do {
  1951. if (ds_dly3 >= 31) {
  1952. ds_dly3 = 0;
  1953. } else {
  1954. ds_dly3 += 1;
  1955. }
  1956. MSDC_SET_FIELD(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY3, ds_dly3);
  1957. ds_dly1_count = 0;
  1958. do {
  1959. if (ds_dly1 == 0) {
  1960. ds_dly1 = 31;
  1961. } else {
  1962. ds_dly1 -= 1;
  1963. }
  1964. MSDC_SET_FIELD(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY1, ds_dly1);
  1965. /* resend the r/w command */
  1966. if (rw == 0) {
  1967. result = host->blk_read(host, dst, src, nblks);
  1968. } else if (rw == 1) {
  1969. result = host->blk_write(host, (ulong) dst, (uchar *) src, nblks);
  1970. }
  1971. #if MSDC_TUNE_LOG
  1972. /* for debugging */
  1973. {
  1974. times++;
  1975. if (rw == 0) {
  1976. MSG(INF, "[SD%d] <TUNE_BREAD_%d><%s><cmd%d:0x%x> ret=%d, DS_DLY1=%d, DS_DLY3=%d\n",
  1977. host->id, times, result == MMC_ERR_NONE ? "PASS" : "FAIL", (nblks == 1 ? 17 : 18), (unsigned int)dst,
  1978. result, ds_dly1, ds_dly3);
  1979. } else if (rw == 1) {
  1980. MSG(INF, "[SD%d] <TUNE_BEWRITE_%d><%s><cmd%d:0x%x> ret=%d, DS_DLY1=%d, DS_DLY3=%d\n",
  1981. host->id, times, result == MMC_ERR_NONE ? "PASS" : "FAIL", (nblks == 1 ? 24 : 25), (unsigned int)dst,
  1982. result, ds_dly1, ds_dly3);
  1983. }
  1984. }
  1985. #endif
  1986. if (result == MMC_ERR_CMDTUNEFAIL || result == MMC_ERR_CMD_RSPCRC)
  1987. goto done;
  1988. if (result == MMC_ERR_NONE) {
  1989. goto done;
  1990. }
  1991. } while (++ds_dly1_count < 32);
  1992. } while (++ds_dly3_count < 32);
  1993. done:
  1994. return result;
  1995. }
  1996. #endif
  1997. #if defined(FEATURE_MMC_SDCARD) && defined(FEATURE_MMC_UHS1)
  1998. int msdc_tune_uhs1(struct mmc_host *host, struct mmc_card *card)
  1999. {
  2000. u32 base = host->base;
  2001. u32 status;
  2002. int i;
  2003. int err = MMC_ERR_FAILED;
  2004. struct mmc_command cmd;
  2005. cmd.opcode = SD_CMD_SEND_TUNING_BLOCK;
  2006. cmd.arg = 0;
  2007. cmd.rsptyp = RESP_R1;
  2008. cmd.retries = CMD_RETRIES;
  2009. cmd.timeout = 0xFFFFFFFF;
  2010. msdc_set_timeout(host, 100000000, 0);
  2011. msdc_set_autocmd(host, MSDC_AUTOCMD19, 1);
  2012. for (i = 0; i < 13; i++) {
  2013. /* Note. select a pad to be tuned. msdc only tries 32 times to tune the
  2014. * pad since there is only 32 tuning steps for a pad.
  2015. */
  2016. MSDC_SET_FIELD(SDC_ACMD19_TRG, SDC_ACMD19_TRG_TUNESEL, i);
  2017. /* Note. autocmd19 will only trigger done interrupt and won't trigger
  2018. * autocmd timeout and crc error interrupt. (autocmd19 is a special command
  2019. * and is different from autocmd12 and autocmd23.
  2020. */
  2021. err = msdc_cmd(host, &cmd);
  2022. if (err != MMC_ERR_NONE)
  2023. goto out;
  2024. /* read and check acmd19 sts. bit-1: success, bit-0: fail */
  2025. status = MSDC_READ32(SDC_ACMD19_STS);
  2026. if (!status) {
  2027. msdc_pr_err("[SD%d] ACMD19_TRG(%d), STS(0x%x) Failed\n", host->id, i,
  2028. status);
  2029. err = MMC_ERR_FAILED;
  2030. goto out;
  2031. }
  2032. }
  2033. err = MMC_ERR_NONE;
  2034. out:
  2035. msdc_set_autocmd(host, MSDC_AUTOCMD19, 0);
  2036. return err;
  2037. }
  2038. int msdc_tune_hs200(struct mmc_host *host, struct mmc_card *card)
  2039. {
  2040. return 0;
  2041. }
  2042. int msdc_tune_hs400(struct mmc_host *host, struct mmc_card *card)
  2043. {
  2044. return 0;
  2045. }
  2046. #endif
  2047. #if defined(FEATURE_MMC_CARD_DETECT)
  2048. void msdc_card_detect(struct mmc_host *host, int on)
  2049. {
  2050. u32 base = host->base;
  2051. if ((msdc_cap[host->id].flags & MSDC_CD_PIN_EN) == 0) {
  2052. MSDC_CARD_DETECTION_OFF();
  2053. return;
  2054. }
  2055. if (on) {
  2056. MSDC_SET_FIELD(MSDC_PS, MSDC_PS_CDDEBOUNCE, DEFAULT_DEBOUNCE);
  2057. MSDC_CARD_DETECTION_ON();
  2058. } else {
  2059. MSDC_CARD_DETECTION_OFF();
  2060. MSDC_SET_FIELD(MSDC_PS, MSDC_PS_CDDEBOUNCE, 0);
  2061. }
  2062. }
  2063. int msdc_card_avail(struct mmc_host *host)
  2064. {
  2065. u32 base = host->base;
  2066. u32 sts, avail = 0;
  2067. if ((msdc_cap[host->id].flags & MSDC_REMOVABLE) == 0)
  2068. return 1;
  2069. if (msdc_cap[host->id].flags & MSDC_CD_PIN_EN) {
  2070. MSDC_GET_FIELD(MSDC_PS, MSDC_PS_CDSTS, sts);
  2071. avail = sts == 0 ? 1 : 0;
  2072. }
  2073. return avail;
  2074. }
  2075. #endif
  2076. #if defined(MMC_MSDC_DRV_CTP)
  2077. int msdc_card_protected(struct mmc_host *host)
  2078. {
  2079. u32 base = host->base;
  2080. u32 prot;
  2081. if (msdc_cap[host->id].flags & MSDC_WP_PIN_EN) {
  2082. MSDC_GET_FIELD(MSDC_PS, MSDC_PS_WP, prot);
  2083. } else {
  2084. prot = 0;
  2085. }
  2086. return prot;
  2087. }
  2088. #endif
  2089. #if defined(MMC_MSDC_DRV_CTP) || defined(MMC_MSDC_DRV_LK)
  2090. void msdc_hard_reset(struct mmc_host *host)
  2091. {
  2092. msdc_card_power(host, 0);
  2093. mdelay(10);
  2094. msdc_card_power(host, 1);
  2095. mdelay(10);
  2096. }
  2097. void msdc_soft_reset(struct mmc_host *host)
  2098. {
  2099. u32 base = host->base;
  2100. u32 tmo = 0x0000ffff;
  2101. MSDC_RESET();
  2102. MSDC_SET_FIELD(MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP, 1);
  2103. WAIT_COND((MSDC_READ32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS) == 0, 0xFFFF, tmo);
  2104. if (tmo == 0) {
  2105. MSG(DMA, "[SD%d] MSDC_DMA_CFG_STS != inactive\n", host->id);
  2106. }
  2107. MSDC_CLR_FIFO();
  2108. }
  2109. #endif
  2110. void msdc_emmc_hard_reset(struct mmc_host *host)
  2111. {
  2112. u32 base = host->base;
  2113. MSDC_SET_BIT32(EMMC_IOCON, EMMC_IOCON_BOOTRST);
  2114. //mt_set_gpio_out(PAD_MSDC0_RSTB,GPIO_OUT_ZERO);
  2115. mdelay(10);
  2116. MSDC_CLR_BIT32(EMMC_IOCON, EMMC_IOCON_BOOTRST);
  2117. //mt_set_gpio_out(PAD_MSDC0_RSTB,GPIO_OUT_ONE);
  2118. }
  2119. #ifdef FEATURE_MMC_BOOT_MODE
  2120. int msdc_emmc_boot_start(struct mmc_host *host, u32 hz, int ddr, int mode, int ackdis, u8 hostbuswidth, u64 size)
  2121. {
  2122. int err = MMC_ERR_NONE;
  2123. u32 sts;
  2124. u32 base = host->base;
  2125. u32 tmo = 0xFFFFFFFF;
  2126. u32 acktmo, dattmo;
  2127. u64 acktime, dattime;
  2128. u64 expect_dattime;
  2129. u32 test_timer1;
  2130. u32 test_timer2;
  2131. MSDC_RESET();
  2132. MSDC_CLR_FIFO();
  2133. msdc_set_blklen(host, 512);
  2134. msdc_set_blknum(host, size/512);
  2135. msdc_config_bus(host, hostbuswidth);
  2136. msdc_config_clksrc(host, MSDC0_CLKSRC_26MHZ);
  2137. msdc_config_clock(host, (ddr ? MMC_STATE_DDR : 0), hz, 0);
  2138. //MSDC_SET_FIELD(MSDC_DMA_CFG,3 << 12,0x2);
  2139. //MSDC_SET_FIELD(MSDC_DMA_CFG,3 << 8,0x1);
  2140. /* requires 74 clocks/1ms before CMD0 */
  2141. MSDC_SET_BIT32(MSDC_CFG, MSDC_CFG_CKPDN);
  2142. mdelay(2);
  2143. MSDC_CLR_BIT32(MSDC_CFG, MSDC_CFG_CKPDN);
  2144. /* configure boot timeout value */
  2145. WAIT_COND(SDC_IS_BUSY() == 0, tmo, tmo);
  2146. acktime = 50 * 1000 * 1000ULL;
  2147. dattime = 1000 * 1000 * 1000ULL;
  2148. acktmo = msdc_cal_timeout(host, acktime, 0, 1<<EMMC_BOOT_TMO_IN_CLK_2POWER); /* 50ms MT6583 MSDC IP eMMC boot timeout unit change to 2^16*/
  2149. expect_dattime = size * 8 / hostbuswidth;
  2150. if (ddr)
  2151. expect_dattime /= 2;
  2152. expect_dattime *= (1000 * 1000 * 1000ULL) / hz;
  2153. if (expect_dattime > dattime)
  2154. dattime = expect_dattime * 2;
  2155. dattmo = msdc_cal_timeout(host, dattime, 0, 1<<EMMC_BOOT_TMO_IN_CLK_2POWER); /* 1sec */
  2156. if (acktmo == 0) acktmo = 1;
  2157. if (dattmo == 0) dattmo = 1;
  2158. acktmo = acktmo > 0xFFE ? 0xFFE : acktmo;
  2159. dattmo = dattmo > 0xFFFFE ? 0xFFFFE : dattmo;
  2160. MSG(INF, "[SD%d] EMMC BOOT ACK timeout: %d ms (clkcnt: %d)(host->cur_bus_clk = %d)\n", host->id,
  2161. (acktmo * 65536) / (host->cur_bus_clk / 1000), acktmo, host->cur_bus_clk);
  2162. MSG(INF, "[SD%d] EMMC BOOT DAT timeout: %d ms (clkcnt: %d)\n", host->id,
  2163. (dattmo * 65536) / (host->cur_bus_clk / 1000), dattmo);
  2164. MSG(INF, "[SD%d] EMMC BOOT ACK %s on host\n", host->id, (ackdis ? "disabled" : "enabled"));
  2165. MSDC_SET_BIT32(EMMC_CFG0, EMMC_CFG0_BOOTSUPP);
  2166. MSDC_SET_FIELD(EMMC_CFG0, EMMC_CFG0_BOOTACKDIS, ackdis);
  2167. MSDC_SET_FIELD(EMMC_CFG0, EMMC_CFG0_BOOTMODE, mode);
  2168. MSDC_SET_FIELD(EMMC_CFG1, EMMC_CFG1_BOOTACKTMC, acktmo);
  2169. MSDC_SET_FIELD(EMMC_CFG1, EMMC_CFG1_BOOTDATTMC, dattmo);
  2170. if (mode == EMMC_BOOT_RST_CMD_MODE) {
  2171. MSDC_WRITE32(SDC_ARG, 0xFFFFFFFA);
  2172. } else {
  2173. MSDC_WRITE32(SDC_ARG, 0);
  2174. }
  2175. MSDC_WRITE32(SDC_CMD, 0x02001000); /* bit[12]: 1 multiple block read, 0: single block read */
  2176. #if 0 //init timer to test MT6583 ACK/DAT timeour modification test case
  2177. MSDC_WRITE32(0x10008040,0x31);
  2178. MSDC_WRITE32(0x10008044,0x0);
  2179. test_timer1 = MSDC_READ32(0x10008048);//init timer to test MT6583 ACK/DAT timeour modification test case
  2180. #endif
  2181. MSDC_SET_BIT32(EMMC_CFG0, EMMC_CFG0_BOOTSTART);
  2182. WAIT_COND((MSDC_READ32(EMMC_STS) & EMMC_STS_BOOTUPSTATE) == EMMC_STS_BOOTUPSTATE, tmo, tmo);
  2183. if (!ackdis) {
  2184. do {
  2185. sts = MSDC_READ32(EMMC_STS);
  2186. if (sts == 0)
  2187. continue;
  2188. MSDC_WRITE32(EMMC_STS, sts); /* write 1 to clear */
  2189. /* if ack is error, hw will first set bootackrcv bit, then set bootackerr bit
  2190. * so the best way is check EMMC_STS_BOOTACKERR bit after EMMC_STS_BOOTACKRCV bit set*/
  2191. if (sts & EMMC_STS_BOOTACKERR) {
  2192. msdc_pr_err("[%s]: [SD%d] EMMC_STS(0x%x): boot up ack error\n", __func__, host->id, sts);
  2193. err = MMC_ERR_BADCRC;
  2194. goto out;
  2195. } else if (sts & EMMC_STS_BOOTACKRCV) {
  2196. MSG(INF, "[%s]: [SD%d] EMMC_STS(0x%x): boot ack received\n", __func__,host->id, sts);
  2197. break;
  2198. } else if (sts & EMMC_STS_BOOTACKTMO) {
  2199. #if 0
  2200. test_timer2 = MSDC_READ32(0x10008048);
  2201. test_timer1 = (test_timer2 - test_timer1) /6000;
  2202. msdc_pr_err("[SD%d] EMMC_STS(%x): boot up ack timeout(%d ms)\n", host->id, sts,test_timer1);
  2203. //test MT6583 ACK/DAT timeour modification test case
  2204. #endif
  2205. msdc_pr_err("[%s]: [SD%d] EMMC_STS(0x%x): boot up ack timeout\n", __func__,host->id, sts);
  2206. err = MMC_ERR_TIMEOUT;
  2207. goto out;
  2208. } else if (sts & EMMC_STS_BOOTUPSTATE) {
  2209. //msdc_pr_info("[%s]: [SD%d] EMMC_STS(%x): boot up mode state\n", __func__, host->id, sts);
  2210. } else {
  2211. msdc_pr_err("[%s]: [SD%d] EMMC_STS(0x%x): boot up unexpected\n", __func__,host->id, sts);
  2212. }
  2213. } while (1);
  2214. }
  2215. //MSG(INF, "ackdis(%d) err(%d)\n",ackdis,err);
  2216. /* check if data received */
  2217. do {
  2218. sts = MSDC_READ32(EMMC_STS);
  2219. if (sts == 0)
  2220. continue;
  2221. if (sts & EMMC_STS_BOOTDATRCV) {
  2222. MSG(INF, "[%s]: [SD%d] EMMC_STS(0x%x): boot dat received\n", __func__,host->id, sts);
  2223. break;
  2224. }
  2225. if (sts & EMMC_STS_BOOTCRCERR) {
  2226. msdc_pr_err("[%s]: [SD%d] EMMC_STS(0x%x): boot up data crc error\n", __func__,host->id, sts);
  2227. err = MMC_ERR_BADCRC;
  2228. goto out;
  2229. } else if (sts & EMMC_STS_BOOTDATTMO) {
  2230. #if 0
  2231. test_timer2 = MSDC_READ32(0x10008048);
  2232. test_timer1 = (test_timer2 - test_timer1) /6000;
  2233. msdc_pr_err("[%s]: [SD%d] EMMC_STS(%x): boot up data timeout(%d s)\n", __func__,host->id, sts,test_timer1);
  2234. //test MT6583 ACK/DAT timeour modification test case
  2235. #endif
  2236. msdc_pr_err("[%s]: [SD%d] EMMC_STS(0x%x): boot up data timeout\n", __func__,host->id, sts);
  2237. err = MMC_ERR_TIMEOUT;
  2238. goto out;
  2239. }
  2240. } while (1);
  2241. out:
  2242. return err;
  2243. }
  2244. void msdc_emmc_boot_stop(struct mmc_host *host)
  2245. {
  2246. u32 base = host->base;
  2247. u32 tmo = 0xFFFFFFFF;
  2248. /* Step5. stop the boot mode */
  2249. MSDC_WRITE32(SDC_ARG, 0x00000000);
  2250. MSDC_WRITE32(SDC_CMD, 0x00001000);
  2251. MSDC_SET_FIELD(EMMC_CFG0, EMMC_CFG0_BOOTWDLY, 2);
  2252. MSDC_SET_BIT32(EMMC_CFG0, EMMC_CFG0_BOOTSTOP);
  2253. WAIT_COND((MSDC_READ32(EMMC_STS) & EMMC_STS_BOOTUPSTATE) == 0, tmo, tmo);
  2254. /* Step6. */
  2255. MSDC_CLR_BIT32(EMMC_CFG0, EMMC_CFG0_BOOTSUPP);
  2256. /* Step7. clear EMMC_STS bits */
  2257. MSDC_WRITE32(EMMC_STS, MSDC_READ32(EMMC_STS));
  2258. }
  2259. int msdc_emmc_boot_read(struct mmc_host *host, u64 size, u32 *to, int read_mode)
  2260. {
  2261. int err = MMC_ERR_NONE;
  2262. int derr = MMC_ERR_NONE;
  2263. u32 sts;
  2264. u64 totalsz = size;
  2265. u32 base = host->base;
  2266. u64 left_sz, xfer_sz;
  2267. msdc_priv_t *priv = (msdc_priv_t*)host->priv;
  2268. struct dma_config *cfg = &priv->cfg;
  2269. BUG_ON((read_mode < MSDC_MODE_PIO) && (read_mode > MSDC_MODE_DMA_DESC));
  2270. if (read_mode == MSDC_MODE_PIO) {
  2271. MSDC_SET_BIT32(MSDC_CFG, MSDC_CFG_PIO);
  2272. while (size) {
  2273. sts = MSDC_READ32(EMMC_STS);
  2274. if (sts & EMMC_STS_BOOTCRCERR) {
  2275. msdc_pr_err("[SD%d] EMMC_STS(0x%x): boot up data crc error\n", host->id, sts);
  2276. err = MMC_ERR_BADCRC;
  2277. goto out;
  2278. } else if (sts & EMMC_STS_BOOTDATTMO) {
  2279. msdc_pr_err("[SD%d] EMMC_STS(0x%x): boot up data timeout error\n", host->id, sts);
  2280. err = MMC_ERR_TIMEOUT;
  2281. goto out;
  2282. }
  2283. /* Note. RXFIFO count would be aligned to 4-bytes alignment size */
  2284. if ((size >= MSDC_FIFO_THD) && (MSDC_RXFIFOCNT() >= MSDC_FIFO_THD)) {
  2285. int left = MSDC_FIFO_THD >> 2;
  2286. do {
  2287. #ifdef MTK_MSDC_DUMP_FIFO
  2288. MSG(FIO, "0x%x ",MSDC_FIFO_READ32());
  2289. #else
  2290. *to++ = MSDC_FIFO_READ32();
  2291. #endif
  2292. } while (--left);
  2293. size -= MSDC_FIFO_THD;
  2294. MSG(FIO, "[SD%d] Read %d bytes, RXFIFOCNT: %d, Left: %d/%d\n",
  2295. host->id, MSDC_FIFO_THD, MSDC_RXFIFOCNT(), size, totalsz);
  2296. } else if ((size < MSDC_FIFO_THD) && MSDC_RXFIFOCNT() >= size) {
  2297. while (size) {
  2298. if (size > 3) {
  2299. #ifdef MTK_MSDC_DUMP_FIFO
  2300. MSG(FIO, "0x%x ", MSDC_FIFO_READ32());
  2301. #else
  2302. *to++ = MSDC_FIFO_READ32();
  2303. #endif
  2304. size -= 4;
  2305. } else {
  2306. #ifdef MTK_MSDC_DUMP_FIFO
  2307. MSG(FIO, "0x%x ", MSDC_FIFO_READ32());
  2308. #else
  2309. u32 val = MSDC_FIFO_READ32();
  2310. memcpy(to, &val, size);
  2311. #endif
  2312. size = 0;
  2313. }
  2314. }
  2315. MSG(FIO, "[SD%d] Read left bytes, RXFIFOCNT: %d, Left: %d/%d\n",
  2316. host->id, MSDC_RXFIFOCNT(), (u32)size, totalsz);
  2317. }
  2318. }
  2319. out:
  2320. if (err) {
  2321. msdc_pr_err("[SD%d] EMMC_BOOT: read boot code fail(%d), FIFOCNT=%d\n",
  2322. host->id, err, MSDC_RXFIFOCNT());
  2323. }
  2324. } else {
  2325. //MSDC_CLR_BIT32(MSDC_CFG, MSDC_CFG_PIO);
  2326. cfg->mode = read_mode;
  2327. left_sz = size;
  2328. if (read_mode == MSDC_MODE_DMA_BASIC) {
  2329. cfg->inboot = 1;
  2330. xfer_sz = left_sz > MAX_DMA_CNT ? MAX_DMA_CNT : left_sz;
  2331. //msdc_set_blknum(host, xfer_sz/512);
  2332. } else {
  2333. xfer_sz = left_sz;
  2334. }
  2335. while (left_sz) {
  2336. u32 base = host->base;
  2337. cfg->xfersz = xfer_sz;
  2338. //MSG(FIO, "to (0x%x) xfer_sz(0x%x)\n",to,xfer_sz);
  2339. if (cfg->mode == MSDC_MODE_DMA_BASIC) {
  2340. cfg->sglen = 1;
  2341. cfg->sg[0].addr = (u32)to;
  2342. cfg->sg[0].len = xfer_sz;
  2343. msdc_flush_membuf(to, xfer_sz);
  2344. } else {
  2345. cfg->sglen = msdc_sg_init(cfg->sg, to, xfer_sz);
  2346. cfg->flags |= DMA_FLAG_EN_CHKSUM;
  2347. }
  2348. MSDC_DMA_ON();
  2349. //MSG(FIO, "xfer_sz(%d),left_sz(%d)\n", (u32)xfer_sz, (u32)left_sz);*/
  2350. msdc_dma_config(host, cfg);
  2351. if (left_sz - xfer_sz != 0)
  2352. MSDC_SET_FIELD(MSDC_DMA_CTRL, MSDC_DMA_CTRL_LASTBUF, 0);
  2353. msdc_dma_start(host);
  2354. err = derr = msdc_dma_wait_done(host, 0xFFFFFFFF);
  2355. msdc_dma_stop(host);
  2356. msdc_flush_membuf(to, xfer_sz);
  2357. if (err != MMC_ERR_NONE)
  2358. goto done;
  2359. to = (u32 *)((u8 *)to + xfer_sz);
  2360. left_sz -= xfer_sz;
  2361. /* left_sz > 0 only when in basic dma mode */
  2362. if (left_sz) {
  2363. xfer_sz = (xfer_sz > left_sz) ? left_sz : xfer_sz;
  2364. }
  2365. }
  2366. done:
  2367. if (derr != MMC_ERR_NONE) {
  2368. msdc_pr_err("[SD%d] EMMC boot read error(%d)\n", host->id,derr);
  2369. msdc_abort_handler(host, 1);
  2370. }
  2371. }
  2372. return err;
  2373. }
  2374. void msdc_emmc_boot_reset(struct mmc_host *host, int reset)
  2375. {
  2376. u32 base = host->base;
  2377. u32 wints = MSDC_INT_CMDRDY | MSDC_INT_CMDTMO;
  2378. u32 l_arg, l_cmd, status;
  2379. u32 tmo = 0xffffffff;
  2380. switch (reset) {
  2381. case EMMC_BOOT_PWR_RESET:
  2382. msdc_hard_reset(host);
  2383. break;
  2384. case EMMC_BOOT_RST_N_SIG:
  2385. if (msdc_cap[host->id].flags & MSDC_RST_PIN_EN) {
  2386. /* set n_reset pin to low */
  2387. MSDC_SET_BIT32(EMMC_IOCON, EMMC_IOCON_BOOTRST);
  2388. /* tRSTW (RST_n pulse width) at least 1us */
  2389. mdelay(1);
  2390. /* set n_reset pin to high, mark this line if do boot ACK & boot DAT timeout test */
  2391. MSDC_CLR_BIT32(EMMC_IOCON, EMMC_IOCON_BOOTRST);
  2392. /* tRSCA (RST_n to command time) at least 200us,
  2393. tRSTH (RST_n high period) at least 1us */
  2394. MSDC_SET_BIT32(MSDC_CFG, MSDC_CFG_CKPDN);
  2395. mdelay(1);
  2396. MSDC_CLR_BIT32(MSDC_CFG, MSDC_CFG_CKPDN);
  2397. }
  2398. break;
  2399. case EMMC_BOOT_PRE_IDLE_CMD:
  2400. /* bring emmc to pre-idle mode by software reset command. (MMCv4.41)*/
  2401. SDC_SEND_CMD(0x0, 0xF0F0F0F0);
  2402. /* read SDC_ARG & SDC_CMD for avoid buffered register */
  2403. l_arg = MSDC_READ32(SDC_ARG);
  2404. l_cmd = MSDC_READ32(SDC_CMD);
  2405. /* check cmd0 is send */
  2406. status = msdc_intr_wait(host, wints);
  2407. if (status & MSDC_INT_CMDTMO) {
  2408. msdc_pr_err("[SD%d] CMD0:ERR(CMDTO)\n", host->id);
  2409. }
  2410. mdelay(1); //need delay to make sure pre-idle
  2411. break;
  2412. }
  2413. }
  2414. #endif
  2415. int msdc_init(int id, struct mmc_host *host, int clksrc, int mode)
  2416. {
  2417. #if (MSDC_MAX_NUM == 1)
  2418. u32 baddr[] = {MSDC0_BASE};
  2419. u32 top_baddr[] = {MSDC0_TOP_BASE};
  2420. #elif (MSDC_MAX_NUM == 2)
  2421. u32 baddr[] = {MSDC0_BASE, MSDC1_BASE};
  2422. u32 top_baddr[] = {MSDC0_TOP_BASE, MSDC1_TOP_BASE};
  2423. #elif (MSDC_MAX_NUM >= 3)
  2424. u32 baddr[] = {MSDC0_BASE, MSDC1_BASE, MSDC2_BASE};
  2425. u32 top_baddr[] = {MSDC0_TOP_BASE, MSDC1_TOP_BASE, 0};
  2426. #endif
  2427. u32 base = baddr[id];
  2428. u32 top_base = top_baddr[id];
  2429. msdc_priv_t *priv;
  2430. struct dma_config *cfg;
  2431. msdc_pr_info("[SD%d] Host controller intialization start\n", id);
  2432. clksrc = (clksrc == -1) ? msdc_cap[id].clk_src : clksrc;
  2433. priv = &msdc_priv[id];
  2434. cfg = &priv->cfg;
  2435. memset(priv, 0, sizeof(msdc_priv_t));
  2436. host->id = id;
  2437. host->base = base;
  2438. #if !defined(FPGA_PLATFORM)
  2439. host->base_top = top_base;
  2440. #else
  2441. host->base_top = 0;
  2442. #endif
  2443. msdc_config_clksrc(host, clksrc);
  2444. host->f_max = MSDC_MAX_SCLK;;
  2445. host->f_min = MSDC_MIN_SCLK;
  2446. host->blkbits= MMC_BLOCK_BITS;
  2447. host->blklen = 0;
  2448. host->priv = (void*)priv;
  2449. host->caps = MMC_CAP_MULTIWRITE;
  2450. if (msdc_cap[id].flags & MSDC_HIGHSPEED)
  2451. host->caps |= (MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED);
  2452. #if defined(FEATURE_MMC_UHS1)
  2453. if (msdc_cap[id].flags & MSDC_UHS1)
  2454. host->caps |= MMC_CAP_SD_UHS1;
  2455. #endif
  2456. if (msdc_cap[id].flags & MSDC_DDR)
  2457. host->caps |= MMC_CAP_DDR;
  2458. if (msdc_cap[id].data_pins == 4)
  2459. host->caps |= MMC_CAP_4_BIT_DATA;
  2460. if (msdc_cap[id].data_pins == 8)
  2461. host->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
  2462. if (msdc_cap[id].flags & MSDC_HS200)
  2463. host->caps |= MMC_CAP_EMMC_HS200;
  2464. if (msdc_cap[id].flags & MSDC_HS400)
  2465. host->caps |= MMC_CAP_EMMC_HS400;
  2466. host->ocr_avail = MMC_VDD_27_36;
  2467. /* msdc0 only support 1.8 IO */
  2468. if (host->caps & (MMC_CAP_EMMC_HS200 | MMC_CAP_EMMC_HS400))
  2469. host->ocr_avail |= MMC_VDD_165_195;
  2470. host->max_hw_segs = MAX_DMA_TRAN_SIZE / 512;
  2471. host->max_phys_segs = MAX_DMA_TRAN_SIZE / 512;
  2472. host->max_seg_size = MAX_DMA_TRAN_SIZE;
  2473. host->max_blk_size = 2048;
  2474. host->max_blk_count = 65535;
  2475. host->app_cmd = 0;
  2476. host->app_cmd_arg = 0;
  2477. priv->rdsmpl = msdc_cap[id].data_edge;
  2478. priv->wdsmpl = msdc_cap[id].data_edge;
  2479. priv->rsmpl = msdc_cap[id].cmd_edge;
  2480. #if defined(MSDC_ENABLE_DMA_MODE)
  2481. cfg->sg = &priv->sg[0];
  2482. cfg->burstsz = MSDC_BRUST_64B;
  2483. cfg->flags = DMA_FLAG_NONE;
  2484. cfg->mode = mode;
  2485. cfg->inboot = 0;
  2486. msdc_init_gpd_bd(host);
  2487. priv->alloc_bd = 0;
  2488. priv->alloc_gpd = 0;
  2489. priv->active_head = NULL;
  2490. priv->active_tail = NULL;
  2491. #endif
  2492. // set current power level: VOL_1800 or VOL_3300
  2493. #if defined(USE_SDIO_1V8)
  2494. if (host->id != 0) {
  2495. host->cur_pwr = VOL_1800;
  2496. } else
  2497. #endif
  2498. {
  2499. #if defined(FPGA_PLATFORM)
  2500. #if MSDC_USE_EMMC45_POWER
  2501. host->cur_pwr = VOL_1800;
  2502. #else
  2503. host->cur_pwr = VOL_3300;
  2504. #endif
  2505. #else
  2506. if (host->id == 0)
  2507. host->cur_pwr = VOL_1800;
  2508. else
  2509. host->cur_pwr = VOL_3300;
  2510. #endif
  2511. }
  2512. msdc_power(host, MMC_POWER_ON);
  2513. #if defined(MMC_MSDC_DRV_PRELOADER)
  2514. /* reset eMMC after power on */
  2515. if (host->id == 0)
  2516. msdc_emmc_hard_reset(host);
  2517. #endif
  2518. /* set to SD/MMC mode */
  2519. MSDC_SET_FIELD(MSDC_CFG, MSDC_CFG_MODE, MSDC_SDMMC);
  2520. MSDC_SET_BIT32(MSDC_CFG, MSDC_CFG_PIO);
  2521. MSDC_RESET();
  2522. MSDC_CLR_FIFO();
  2523. MSDC_CLR_INT();
  2524. /* enable SDIO mode. it's must otherwise sdio command failed */
  2525. MSDC_SET_BIT32(SDC_CFG, SDC_CFG_SDIO);
  2526. /* disable detect SDIO device interupt function */
  2527. MSDC_CLR_BIT32(SDC_CFG, SDC_CFG_SDIOIDE);
  2528. /* enable wake up events */
  2529. #if defined(MMC_MSDC_DRV_CTP)
  2530. MSDC_SET_BIT32(SDC_CFG, SDC_CFG_INSWKUP);
  2531. #endif
  2532. /* reset tuning parameter */
  2533. msdc_reset_timing_register(host);
  2534. msdc_init_tune_path(host, 0);
  2535. /* Disable support 64G */
  2536. MSDC_CLR_BIT32(MSDC_PATCH_BIT2, MSDC_PB2_SUPPORT64G);
  2537. #if !defined(FPGA_PLATFORM)
  2538. msdc_gpio_and_pad_init(host);
  2539. #endif
  2540. /* disable boot function, else eMMC intialization may be failed after BROM ops. */
  2541. MSDC_CLR_BIT32(EMMC_CFG0, EMMC_CFG0_BOOTSUPP);
  2542. /* set sampling edge */
  2543. MSDC_SET_FIELD(MSDC_IOCON, MSDC_IOCON_RSPL, msdc_cap[host->id].cmd_edge);
  2544. MSDC_SET_FIELD(MSDC_IOCON, MSDC_IOCON_R_D_SMPL, msdc_cap[host->id].data_edge);
  2545. /* write crc timeout detection */
  2546. MSDC_SET_FIELD(MSDC_PATCH_BIT0, 1 << 30, 1);
  2547. #if defined(MMC_MSDC_DRV_CTP)
  2548. #if (MSDC_USE_FORCE_FLUSH || MSDC_USE_RELIABLE_WRITE || MSDC_USE_DATA_TAG || MSDC_USE_PACKED_CMD)
  2549. MSDC_SET_FIELD(MSDC_PATCH_BIT0, MSDC_PB0_BLKNUM_SEL, 0);
  2550. #else
  2551. MSDC_SET_FIELD(MSDC_PATCH_BIT0, MSDC_PB0_BLKNUM_SEL, 1);
  2552. #endif
  2553. #endif
  2554. msdc_set_startbit(host, START_AT_RISING);
  2555. msdc_config_clksrc(host, clksrc);
  2556. msdc_config_bus(host, HOST_BUS_WIDTH_1);
  2557. msdc_config_clock(host, 0, MSDC_MIN_SCLK, 0);
  2558. msdc_set_dmode(host, mode);
  2559. msdc_set_pio_bits(host, 32);
  2560. /* disable sdio interrupt by default. sdio interrupt enable upon request */
  2561. msdc_intr_unmask(host, 0x0001FF7B);
  2562. msdc_irq_init(host);
  2563. msdc_set_timeout(host, 100000000UL, 0);
  2564. #if defined(FEATURE_MMC_CARD_DETECT)
  2565. msdc_card_detect(host, 1);
  2566. #endif
  2567. #if defined(MSDC_USE_DCM) && defined(MMC_MSDC_DRV_CTP)
  2568. #if !defined(MTKDRV_DCM)
  2569. #error please turn on DCM driver before enable MSDC DCM
  2570. #endif
  2571. dcm_disable(ALL_DCM);
  2572. dcm_enable(MSDC_DCM);
  2573. #endif
  2574. if ((host->id == 0) || (host->id == 1)) {
  2575. /* disable SDIO func */
  2576. MSDC_SET_FIELD(SDC_CFG, SDC_CFG_SDIO, 0);
  2577. MSDC_SET_FIELD(SDC_CFG, SDC_CFG_SDIOIDE, 0);
  2578. MSDC_SET_FIELD(SDC_CFG, SDC_CFG_INSWKUP, 0);
  2579. }
  2580. msdc_pr_info("[SD%d] Host controller intialization done\n", id);
  2581. return 0;
  2582. }
  2583. #if defined(MSDC_WITH_DEINIT)
  2584. int msdc_deinit(struct mmc_host *host)
  2585. {
  2586. u32 base = host->base;
  2587. #if defined(FEATURE_MMC_CARD_DETECT)
  2588. msdc_card_detect(host, 0);
  2589. #endif
  2590. msdc_intr_mask(host, 0x0001FFFB);
  2591. msdc_irq_deinit(host);
  2592. MSDC_RESET();
  2593. MSDC_CLR_FIFO();
  2594. MSDC_CLR_INT();
  2595. msdc_power(host, MMC_POWER_OFF);
  2596. return 0;
  2597. }
  2598. #endif
  2599. int msdc_polling_CD_interrupt(struct mmc_host *host)
  2600. {
  2601. u32 base = host->base;
  2602. u32 intsts;
  2603. intsts = MSDC_READ32(MSDC_INT);
  2604. MSDC_WRITE32(MSDC_INT, intsts);
  2605. //msdc_pr_info("SDIO INT(0x%x)\n",intsts);
  2606. if (intsts & MSDC_INT_CDSC)
  2607. return 1;
  2608. else
  2609. return 0;
  2610. }