octeontx_hsmmc.c 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019 Marvell International Ltd.
  4. */
  5. #include <clk.h>
  6. #include <cpu_func.h>
  7. #include <dm.h>
  8. #include <dm/device-internal.h>
  9. #include <dm/lists.h>
  10. #include <env.h>
  11. #include <errno.h>
  12. #include <fdtdec.h>
  13. #include <log.h>
  14. #include <malloc.h>
  15. #include <memalign.h>
  16. #include <mmc.h>
  17. #include <part.h>
  18. #include <pci.h>
  19. #include <pci_ids.h>
  20. #include <power/regulator.h>
  21. #include <time.h>
  22. #include <watchdog.h>
  23. #include <asm/io.h>
  24. #include <linux/delay.h>
  25. #include <linux/kernel.h>
  26. #include <linux/libfdt.h>
  27. #if defined(CONFIG_ARCH_OCTEON)
  28. #include <mach/octeon-model.h>
  29. #include <mach/cvmx-regs.h>
  30. #include <mach/cvmx-mio-emm-defs.h>
  31. #else
  32. #include <asm/arch/board.h>
  33. #include <asm/arch/clock.h>
  34. #include <asm/arch/csrs/csrs-mio_emm.h>
  35. #endif
  36. #include "octeontx_hsmmc.h"
  37. /* Use dummy implementation for MIPS Octeon to always return false */
  38. #if defined(CONFIG_ARCH_OCTEON)
  39. #define otx_is_soc(ver) 0
  40. #endif
  41. #define MMC_TIMEOUT_SHORT 20 /* in ms */
  42. #define MMC_TIMEOUT_LONG 1000
  43. #define MMC_TIMEOUT_ERASE 10000
  44. #define MMC_DEFAULT_DATA_IN_TAP 10
  45. #define MMC_DEFAULT_CMD_IN_TAP 10
  46. #define MMC_DEFAULT_CMD_OUT_TAP 39
  47. #define MMC_DEFAULT_DATA_OUT_TAP 39
  48. #define MMC_DEFAULT_HS200_CMD_IN_TAP 24
  49. #define MMC_DEFAULT_HS200_DATA_IN_TAP 24
  50. #define MMC_DEFAULT_HS200_CMD_OUT_TAP (otx_is_soc(CN95XX) ? 10 : 5)
  51. #define MMC_DEFAULT_HS200_DATA_OUT_TAP (otx_is_soc(CN95XX) ? 10 : 5)
  52. #define MMC_DEFAULT_HS400_CMD_OUT_TAP (otx_is_soc(CN95XX) ? 10 : 5)
  53. #define MMC_DEFAULT_HS400_DATA_OUT_TAP (otx_is_soc(CN95XX) ? 5 : 3)
  54. #define MMC_DEFAULT_HS200_CMD_OUT_DLY 800 /* Delay in ps */
  55. #define MMC_DEFAULT_HS200_DATA_OUT_DLY 800 /* Delay in ps */
  56. #define MMC_DEFAULT_HS400_CMD_OUT_DLY 800 /* Delay in ps */
  57. #define MMC_DEFAULT_HS400_DATA_OUT_DLY 400 /* Delay in ps */
  58. #define MMC_DEFAULT_SD_UHS_SDR104_CMD_OUT_TAP MMC_DEFAULT_HS200_CMD_OUT_TAP
  59. #define MMC_DEFAULT_SD_UHS_SDR104_DATA_OUT_TAP MMC_DEFAULT_HS200_DATA_OUT_TAP
  60. #define MMC_LEGACY_DEFAULT_CMD_OUT_TAP 39
  61. #define MMC_LEGACY_DEFAULT_DATA_OUT_TAP 39
  62. #define MMC_SD_LEGACY_DEFAULT_CMD_OUT_TAP 63
  63. #define MMC_SD_LEGACY_DEFAULT_DATA_OUT_TAP 63
  64. #define MMC_HS_CMD_OUT_TAP 32
  65. #define MMC_HS_DATA_OUT_TAP 32
  66. #define MMC_SD_HS_CMD_OUT_TAP 26
  67. #define MMC_SD_HS_DATA_OUT_TAP 26
  68. #define MMC_SD_UHS_SDR25_CMD_OUT_TAP 26
  69. #define MMC_SD_UHS_SDR25_DATA_OUT_TAP 26
  70. #define MMC_SD_UHS_SDR50_CMD_OUT_TAP 26
  71. #define MMC_SD_UHS_SDR50_DATA_OUT_TAP 26
  72. #define MMC_DEFAULT_TAP_DELAY 4
  73. #define TOTAL_NO_OF_TAPS 512
  74. static void octeontx_mmc_switch_to(struct mmc *mmc);
  75. static void set_wdog(struct mmc *mmc, u64 us);
  76. static void do_switch(struct mmc *mmc, union mio_emm_switch emm_switch);
  77. static int octeontx_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  78. struct mmc_data *data);
  79. static int octeontx_mmc_configure_delay(struct mmc *mmc);
  80. static int octeontx_mmc_calibrate_delay(struct mmc *mmc);
  81. #if !defined(CONFIG_ARCH_OCTEON)
  82. static int octeontx2_mmc_calc_delay(struct mmc *mmc, int delay);
  83. static void octeontx_mmc_set_timing(struct mmc *mmc);
  84. static int octeontx_mmc_set_input_bus_timing(struct mmc *mmc);
  85. static int octeontx_mmc_set_output_bus_timing(struct mmc *mmc);
  86. #endif
  87. static bool host_probed;
  88. /**
  89. * Get the slot data structure from a MMC data structure
  90. */
  91. static inline struct octeontx_mmc_slot *mmc_to_slot(struct mmc *mmc)
  92. {
  93. return container_of(mmc, struct octeontx_mmc_slot, mmc);
  94. }
  95. static inline struct octeontx_mmc_host *mmc_to_host(struct mmc *mmc)
  96. {
  97. return mmc_to_slot(mmc)->host;
  98. }
  99. static inline struct octeontx_mmc_slot *dev_to_mmc_slot(struct udevice *dev)
  100. {
  101. return dev_get_priv(dev);
  102. }
  103. static inline struct mmc *dev_to_mmc(struct udevice *dev)
  104. {
  105. return &((struct octeontx_mmc_slot *)dev_get_priv(dev))->mmc;
  106. }
  107. #ifdef DEBUG
  108. const char *mmc_reg_str(u64 reg)
  109. {
  110. if (reg == MIO_EMM_DMA_CFG())
  111. return "MIO_EMM_DMA_CFG";
  112. if (reg == MIO_EMM_DMA_ADR())
  113. return "MIO_EMM_DMA_ADR";
  114. if (reg == MIO_EMM_DMA_INT())
  115. return "MIO_EMM_DMA_INT";
  116. if (reg == MIO_EMM_CFG())
  117. return "MIO_EMM_CFG";
  118. if (reg == MIO_EMM_MODEX(0))
  119. return "MIO_EMM_MODE0";
  120. if (reg == MIO_EMM_MODEX(1))
  121. return "MIO_EMM_MODE1";
  122. if (reg == MIO_EMM_MODEX(2))
  123. return "MIO_EMM_MODE2";
  124. if (reg == MIO_EMM_MODEX(3))
  125. return "MIO_EMM_MODE3";
  126. if (reg == MIO_EMM_IO_CTL())
  127. return "MIO_EMM_IO_CTL";
  128. if (reg == MIO_EMM_SWITCH())
  129. return "MIO_EMM_SWITCH";
  130. if (reg == MIO_EMM_DMA())
  131. return "MIO_EMM_DMA";
  132. if (reg == MIO_EMM_CMD())
  133. return "MIO_EMM_CMD";
  134. if (reg == MIO_EMM_RSP_STS())
  135. return "MIO_EMM_RSP_STS";
  136. if (reg == MIO_EMM_RSP_LO())
  137. return "MIO_EMM_RSP_LO";
  138. if (reg == MIO_EMM_RSP_HI())
  139. return "MIO_EMM_RSP_HI";
  140. if (reg == MIO_EMM_INT())
  141. return "MIO_EMM_INT";
  142. if (reg == MIO_EMM_WDOG())
  143. return "MIO_EMM_WDOG";
  144. if (reg == MIO_EMM_DMA_ARG())
  145. return "MIO_EMM_DMA_ARG";
  146. if (IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
  147. if (reg == MIO_EMM_SAMPLE())
  148. return "MIO_EMM_SAMPLE";
  149. }
  150. if (reg == MIO_EMM_STS_MASK())
  151. return "MIO_EMM_STS_MASK";
  152. if (reg == MIO_EMM_RCA())
  153. return "MIO_EMM_RCA";
  154. if (reg == MIO_EMM_BUF_IDX())
  155. return "MIO_EMM_BUF_IDX";
  156. if (reg == MIO_EMM_BUF_DAT())
  157. return "MIO_EMM_BUF_DAT";
  158. if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
  159. if (reg == MIO_EMM_CALB())
  160. return "MIO_EMM_CALB";
  161. if (reg == MIO_EMM_TAP())
  162. return "MIO_EMM_TAP";
  163. if (reg == MIO_EMM_TIMING())
  164. return "MIO_EMM_TIMING";
  165. if (reg == MIO_EMM_DEBUG())
  166. return "MIO_EMM_DEBUG";
  167. }
  168. return "UNKNOWN";
  169. }
  170. #endif
  171. static void octeontx_print_rsp_sts(struct mmc *mmc)
  172. {
  173. #ifdef DEBUG
  174. union mio_emm_rsp_sts emm_rsp_sts;
  175. const struct octeontx_mmc_host *host = mmc_to_host(mmc);
  176. static const char * const ctype_xor_str[] = {
  177. "No data",
  178. "Read data into Dbuf",
  179. "Write data from Dbuf",
  180. "Reserved"
  181. };
  182. static const char * const rtype_xor_str[] = {
  183. "No response",
  184. "R1, 48 bits",
  185. "R2, 136 bits",
  186. "R3, 48 bits",
  187. "R4, 48 bits",
  188. "R5, 48 bits",
  189. "Reserved 6",
  190. "Reserved 7"
  191. };
  192. emm_rsp_sts.u = readq(host->base_addr + MIO_EMM_RSP_STS());
  193. printf("\nMIO_EMM_RSP_STS: 0x%016llx\n", emm_rsp_sts.u);
  194. printf(" 60-61: bus_id: %u\n", emm_rsp_sts.s.bus_id);
  195. printf(" 59: cmd_val: %s\n",
  196. emm_rsp_sts.s.cmd_val ? "yes" : "no");
  197. printf(" 58: switch_val: %s\n",
  198. emm_rsp_sts.s.switch_val ? "yes" : "no");
  199. printf(" 57: dma_val: %s\n",
  200. emm_rsp_sts.s.dma_val ? "yes" : "no");
  201. printf(" 56: dma_pend: %s\n",
  202. emm_rsp_sts.s.dma_pend ? "yes" : "no");
  203. printf(" 28: dbuf_err: %s\n",
  204. emm_rsp_sts.s.dbuf_err ? "yes" : "no");
  205. printf(" 23: dbuf: %u\n", emm_rsp_sts.s.dbuf);
  206. printf(" 22: blk_timeout: %s\n",
  207. emm_rsp_sts.s.blk_timeout ? "yes" : "no");
  208. printf(" 21: blk_crc_err: %s\n",
  209. emm_rsp_sts.s.blk_crc_err ? "yes" : "no");
  210. printf(" 20: rsp_busybit: %s\n",
  211. emm_rsp_sts.s.rsp_busybit ? "yes" : "no");
  212. printf(" 19: stp_timeout: %s\n",
  213. emm_rsp_sts.s.stp_timeout ? "yes" : "no");
  214. printf(" 18: stp_crc_err: %s\n",
  215. emm_rsp_sts.s.stp_crc_err ? "yes" : "no");
  216. printf(" 17: stp_bad_sts: %s\n",
  217. emm_rsp_sts.s.stp_bad_sts ? "yes" : "no");
  218. printf(" 16: stp_val: %s\n",
  219. emm_rsp_sts.s.stp_val ? "yes" : "no");
  220. printf(" 15: rsp_timeout: %s\n",
  221. emm_rsp_sts.s.rsp_timeout ? "yes" : "no");
  222. printf(" 14: rsp_crc_err: %s\n",
  223. emm_rsp_sts.s.rsp_crc_err ? "yes" : "no");
  224. printf(" 13: rsp_bad_sts: %s\n",
  225. emm_rsp_sts.s.rsp_bad_sts ? "yes" : "no");
  226. printf(" 12: rsp_val: %s\n",
  227. emm_rsp_sts.s.rsp_val ? "yes" : "no");
  228. printf(" 9-11: rsp_type: %s\n",
  229. rtype_xor_str[emm_rsp_sts.s.rsp_type]);
  230. printf(" 7-8: cmd_type: %s\n",
  231. ctype_xor_str[emm_rsp_sts.s.cmd_type]);
  232. printf(" 1-6: cmd_idx: %u\n",
  233. emm_rsp_sts.s.cmd_idx);
  234. printf(" 0: cmd_done: %s\n",
  235. emm_rsp_sts.s.cmd_done ? "yes" : "no");
  236. #endif
  237. }
  238. static inline u64 read_csr(struct mmc *mmc, u64 reg)
  239. {
  240. const struct octeontx_mmc_host *host = mmc_to_host(mmc);
  241. u64 value = readq(host->base_addr + reg);
  242. #ifdef DEBUG_CSR
  243. printf(" %s: %s(0x%p) => 0x%llx\n", __func__,
  244. mmc_reg_str(reg), host->base_addr + reg,
  245. value);
  246. #endif
  247. return value;
  248. }
  249. /**
  250. * Writes to a CSR register
  251. *
  252. * @param[in] mmc pointer to mmc data structure
  253. * @param reg register offset
  254. * @param value value to write to register
  255. */
  256. static inline void write_csr(struct mmc *mmc, u64 reg, u64 value)
  257. {
  258. const struct octeontx_mmc_host *host = mmc_to_host(mmc);
  259. void *addr = host->base_addr + reg;
  260. #ifdef DEBUG_CSR
  261. printf(" %s: %s(0x%p) <= 0x%llx\n", __func__, mmc_reg_str(reg),
  262. addr, value);
  263. #endif
  264. writeq(value, addr);
  265. }
  266. #ifdef DEBUG
  267. static void mmc_print_status(u32 status)
  268. {
  269. #ifdef DEBUG_STATUS
  270. static const char * const state[] = {
  271. "Idle", /* 0 */
  272. "Ready", /* 1 */
  273. "Ident", /* 2 */
  274. "Standby", /* 3 */
  275. "Tran", /* 4 */
  276. "Data", /* 5 */
  277. "Receive", /* 6 */
  278. "Program", /* 7 */
  279. "Dis", /* 8 */
  280. "Btst", /* 9 */
  281. "Sleep", /* 10 */
  282. "reserved", /* 11 */
  283. "reserved", /* 12 */
  284. "reserved", /* 13 */
  285. "reserved", /* 14 */
  286. "reserved" /* 15 */ };
  287. if (status & R1_APP_CMD)
  288. puts("MMC ACMD\n");
  289. if (status & R1_SWITCH_ERROR)
  290. puts("MMC switch error\n");
  291. if (status & R1_READY_FOR_DATA)
  292. puts("MMC ready for data\n");
  293. printf("MMC %s state\n", state[R1_CURRENT_STATE(status)]);
  294. if (status & R1_ERASE_RESET)
  295. puts("MMC erase reset\n");
  296. if (status & R1_WP_ERASE_SKIP)
  297. puts("MMC partial erase due to write protected blocks\n");
  298. if (status & R1_CID_CSD_OVERWRITE)
  299. puts("MMC CID/CSD overwrite error\n");
  300. if (status & R1_ERROR)
  301. puts("MMC undefined device error\n");
  302. if (status & R1_CC_ERROR)
  303. puts("MMC device error\n");
  304. if (status & R1_CARD_ECC_FAILED)
  305. puts("MMC internal ECC failed to correct data\n");
  306. if (status & R1_ILLEGAL_COMMAND)
  307. puts("MMC illegal command\n");
  308. if (status & R1_COM_CRC_ERROR)
  309. puts("MMC CRC of previous command failed\n");
  310. if (status & R1_LOCK_UNLOCK_FAILED)
  311. puts("MMC sequence or password error in lock/unlock device command\n");
  312. if (status & R1_CARD_IS_LOCKED)
  313. puts("MMC device locked by host\n");
  314. if (status & R1_WP_VIOLATION)
  315. puts("MMC attempt to program write protected block\n");
  316. if (status & R1_ERASE_PARAM)
  317. puts("MMC invalid selection of erase groups for erase\n");
  318. if (status & R1_ERASE_SEQ_ERROR)
  319. puts("MMC error in sequence of erase commands\n");
  320. if (status & R1_BLOCK_LEN_ERROR)
  321. puts("MMC block length error\n");
  322. if (status & R1_ADDRESS_ERROR)
  323. puts("MMC address misalign error\n");
  324. if (status & R1_OUT_OF_RANGE)
  325. puts("MMC address out of range\n");
  326. #endif
  327. }
  328. #endif
  329. #if !defined(CONFIG_ARCH_OCTEON)
  330. /**
  331. * Print out all of the register values where mmc is optional
  332. *
  333. * @param mmc MMC device (can be NULL)
  334. * @param host Pointer to host data structure (can be NULL if mmc is !NULL)
  335. */
  336. static void octeontx_mmc_print_registers2(struct mmc *mmc,
  337. struct octeontx_mmc_host *host)
  338. {
  339. struct octeontx_mmc_slot *slot = mmc ? mmc->priv : NULL;
  340. union mio_emm_dma_cfg emm_dma_cfg;
  341. union mio_emm_dma_adr emm_dma_adr;
  342. union mio_emm_dma_int emm_dma_int;
  343. union mio_emm_cfg emm_cfg;
  344. union mio_emm_modex emm_mode;
  345. union mio_emm_switch emm_switch;
  346. union mio_emm_dma emm_dma;
  347. union mio_emm_cmd emm_cmd;
  348. union mio_emm_rsp_sts emm_rsp_sts;
  349. union mio_emm_rsp_lo emm_rsp_lo;
  350. union mio_emm_rsp_hi emm_rsp_hi;
  351. union mio_emm_int emm_int;
  352. union mio_emm_wdog emm_wdog;
  353. union mio_emm_sample emm_sample;
  354. union mio_emm_calb emm_calb;
  355. union mio_emm_tap emm_tap;
  356. union mio_emm_timing emm_timing;
  357. union mio_emm_io_ctl io_ctl;
  358. union mio_emm_debug emm_debug;
  359. union mio_emm_sts_mask emm_sts_mask;
  360. union mio_emm_rca emm_rca;
  361. int bus;
  362. static const char * const bus_width_str[] = {
  363. "1-bit data bus (power on)",
  364. "4-bit data bus",
  365. "8-bit data bus",
  366. "reserved (3)",
  367. "reserved (4)",
  368. "4-bit data bus (dual data rate)",
  369. "8-bit data bus (dual data rate)",
  370. "reserved (7)",
  371. "reserved (8)",
  372. "invalid (9)",
  373. "invalid (10)",
  374. "invalid (11)",
  375. "invalid (12)",
  376. "invalid (13)",
  377. "invalid (14)",
  378. "invalid (15)",
  379. };
  380. static const char * const ctype_xor_str[] = {
  381. "No data",
  382. "Read data into Dbuf",
  383. "Write data from Dbuf",
  384. "Reserved"
  385. };
  386. static const char * const rtype_xor_str[] = {
  387. "No response",
  388. "R1, 48 bits",
  389. "R2, 136 bits",
  390. "R3, 48 bits",
  391. "R4, 48 bits",
  392. "R5, 48 bits",
  393. "Reserved 6",
  394. "Reserved 7"
  395. };
  396. if (!host && mmc)
  397. host = mmc_to_host(mmc);
  398. if (mmc)
  399. printf("%s: bus id: %u\n", __func__, slot->bus_id);
  400. emm_dma_cfg.u = readq(host->base_addr + MIO_EMM_DMA_CFG());
  401. printf("MIO_EMM_DMA_CFG: 0x%016llx\n",
  402. emm_dma_cfg.u);
  403. printf(" 63: en: %s\n",
  404. emm_dma_cfg.s.en ? "enabled" : "disabled");
  405. printf(" 62: rw: %s\n",
  406. emm_dma_cfg.s.rw ? "write" : "read");
  407. printf(" 61: clr: %s\n",
  408. emm_dma_cfg.s.clr ? "clear" : "not clear");
  409. printf(" 59: swap32: %s\n",
  410. emm_dma_cfg.s.swap32 ? "yes" : "no");
  411. printf(" 58: swap16: %s\n",
  412. emm_dma_cfg.s.swap16 ? "yes" : "no");
  413. printf(" 57: swap8: %s\n",
  414. emm_dma_cfg.s.swap8 ? "yes" : "no");
  415. printf(" 56: endian: %s\n",
  416. emm_dma_cfg.s.endian ? "little" : "big");
  417. printf(" 36-55: size: %u\n",
  418. emm_dma_cfg.s.size);
  419. emm_dma_adr.u = readq(host->base_addr + MIO_EMM_DMA_ADR());
  420. printf("MIO_EMM_DMA_ADR: 0x%016llx\n", emm_dma_adr.u);
  421. printf(" 0-49: adr: 0x%llx\n",
  422. (u64)emm_dma_adr.s.adr);
  423. emm_dma_int.u = readq(host->base_addr + MIO_EMM_DMA_INT());
  424. printf("\nMIO_EMM_DMA_INT: 0x%016llx\n",
  425. emm_dma_int.u);
  426. printf(" 1: FIFO: %s\n",
  427. emm_dma_int.s.fifo ? "yes" : "no");
  428. printf(" 0: Done: %s\n",
  429. emm_dma_int.s.done ? "yes" : "no");
  430. emm_cfg.u = readq(host->base_addr + MIO_EMM_CFG());
  431. printf("\nMIO_EMM_CFG: 0x%016llx\n",
  432. emm_cfg.u);
  433. printf(" 3: bus_ena3: %s\n",
  434. emm_cfg.s.bus_ena & 0x08 ? "yes" : "no");
  435. printf(" 2: bus_ena2: %s\n",
  436. emm_cfg.s.bus_ena & 0x04 ? "yes" : "no");
  437. printf(" 1: bus_ena1: %s\n",
  438. emm_cfg.s.bus_ena & 0x02 ? "yes" : "no");
  439. printf(" 0: bus_ena0: %s\n",
  440. emm_cfg.s.bus_ena & 0x01 ? "yes" : "no");
  441. for (bus = 0; bus < 4; bus++) {
  442. emm_mode.u = readq(host->base_addr + MIO_EMM_MODEX(bus));
  443. printf("\nMIO_EMM_MODE%u: 0x%016llx\n",
  444. bus, emm_mode.u);
  445. if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
  446. printf(" 50: hs400_timing: %s\n",
  447. emm_mode.s.hs400_timing ? "yes" : "no");
  448. printf(" 49: hs200_timing: %s\n",
  449. emm_mode.s.hs200_timing ? "yes" : "no");
  450. }
  451. printf(" 48: hs_timing: %s\n",
  452. emm_mode.s.hs_timing ? "yes" : "no");
  453. printf(" 40-42: bus_width: %s\n",
  454. bus_width_str[emm_mode.s.bus_width]);
  455. printf(" 32-35: power_class %u\n",
  456. emm_mode.s.power_class);
  457. printf(" 16-31: clk_hi: %u\n",
  458. emm_mode.s.clk_hi);
  459. printf(" 0-15: clk_lo: %u\n",
  460. emm_mode.s.clk_lo);
  461. }
  462. emm_switch.u = readq(host->base_addr + MIO_EMM_SWITCH());
  463. printf("\nMIO_EMM_SWITCH: 0x%016llx\n", emm_switch.u);
  464. printf(" 60-61: bus_id: %u\n", emm_switch.s.bus_id);
  465. printf(" 59: switch_exe: %s\n",
  466. emm_switch.s.switch_exe ? "yes" : "no");
  467. printf(" 58: switch_err0: %s\n",
  468. emm_switch.s.switch_err0 ? "yes" : "no");
  469. printf(" 57: switch_err1: %s\n",
  470. emm_switch.s.switch_err1 ? "yes" : "no");
  471. printf(" 56: switch_err2: %s\n",
  472. emm_switch.s.switch_err2 ? "yes" : "no");
  473. printf(" 48: hs_timing: %s\n",
  474. emm_switch.s.hs_timing ? "yes" : "no");
  475. printf(" 42-40: bus_width: %s\n",
  476. bus_width_str[emm_switch.s.bus_width]);
  477. printf(" 32-35: power_class: %u\n",
  478. emm_switch.s.power_class);
  479. printf(" 16-31: clk_hi: %u\n",
  480. emm_switch.s.clk_hi);
  481. printf(" 0-15: clk_lo: %u\n", emm_switch.s.clk_lo);
  482. emm_dma.u = readq(host->base_addr + MIO_EMM_DMA());
  483. printf("\nMIO_EMM_DMA: 0x%016llx\n", emm_dma.u);
  484. printf(" 60-61: bus_id: %u\n", emm_dma.s.bus_id);
  485. printf(" 59: dma_val: %s\n",
  486. emm_dma.s.dma_val ? "yes" : "no");
  487. printf(" 58: sector: %s mode\n",
  488. emm_dma.s.sector ? "sector" : "byte");
  489. printf(" 57: dat_null: %s\n",
  490. emm_dma.s.dat_null ? "yes" : "no");
  491. printf(" 51-56: thres: %u\n", emm_dma.s.thres);
  492. printf(" 50: rel_wr: %s\n",
  493. emm_dma.s.rel_wr ? "yes" : "no");
  494. printf(" 49: rw: %s\n",
  495. emm_dma.s.rw ? "write" : "read");
  496. printf(" 48: multi: %s\n",
  497. emm_dma.s.multi ? "yes" : "no");
  498. printf(" 32-47: block_cnt: %u\n",
  499. emm_dma.s.block_cnt);
  500. printf(" 0-31: card_addr: 0x%x\n",
  501. emm_dma.s.card_addr);
  502. emm_cmd.u = readq(host->base_addr + MIO_EMM_CMD());
  503. printf("\nMIO_EMM_CMD: 0x%016llx\n", emm_cmd.u);
  504. printf("\n 62: skip_busy: %s\n",
  505. emm_cmd.s.skip_busy ? "yes" : "no");
  506. printf(" 60-61: bus_id: %u\n", emm_cmd.s.bus_id);
  507. printf(" 59: cmd_val: %s\n",
  508. emm_cmd.s.cmd_val ? "yes" : "no");
  509. printf(" 55: dbuf: %u\n", emm_cmd.s.dbuf);
  510. printf(" 49-54: offset: %u\n", emm_cmd.s.offset);
  511. printf(" 41-42: ctype_xor: %s\n",
  512. ctype_xor_str[emm_cmd.s.ctype_xor]);
  513. printf(" 38-40: rtype_xor: %s\n",
  514. rtype_xor_str[emm_cmd.s.rtype_xor]);
  515. printf(" 32-37: cmd_idx: %u\n", emm_cmd.s.cmd_idx);
  516. printf(" 0-31: arg: 0x%x\n", emm_cmd.s.arg);
  517. emm_rsp_sts.u = readq(host->base_addr + MIO_EMM_RSP_STS());
  518. printf("\nMIO_EMM_RSP_STS: 0x%016llx\n", emm_rsp_sts.u);
  519. printf(" 60-61: bus_id: %u\n", emm_rsp_sts.s.bus_id);
  520. printf(" 59: cmd_val: %s\n",
  521. emm_rsp_sts.s.cmd_val ? "yes" : "no");
  522. printf(" 58: switch_val: %s\n",
  523. emm_rsp_sts.s.switch_val ? "yes" : "no");
  524. printf(" 57: dma_val: %s\n",
  525. emm_rsp_sts.s.dma_val ? "yes" : "no");
  526. printf(" 56: dma_pend: %s\n",
  527. emm_rsp_sts.s.dma_pend ? "yes" : "no");
  528. printf(" 28: dbuf_err: %s\n",
  529. emm_rsp_sts.s.dbuf_err ? "yes" : "no");
  530. printf(" 23: dbuf: %u\n", emm_rsp_sts.s.dbuf);
  531. printf(" 22: blk_timeout: %s\n",
  532. emm_rsp_sts.s.blk_timeout ? "yes" : "no");
  533. printf(" 21: blk_crc_err: %s\n",
  534. emm_rsp_sts.s.blk_crc_err ? "yes" : "no");
  535. printf(" 20: rsp_busybit: %s\n",
  536. emm_rsp_sts.s.rsp_busybit ? "yes" : "no");
  537. printf(" 19: stp_timeout: %s\n",
  538. emm_rsp_sts.s.stp_timeout ? "yes" : "no");
  539. printf(" 18: stp_crc_err: %s\n",
  540. emm_rsp_sts.s.stp_crc_err ? "yes" : "no");
  541. printf(" 17: stp_bad_sts: %s\n",
  542. emm_rsp_sts.s.stp_bad_sts ? "yes" : "no");
  543. printf(" 16: stp_val: %s\n",
  544. emm_rsp_sts.s.stp_val ? "yes" : "no");
  545. printf(" 15: rsp_timeout: %s\n",
  546. emm_rsp_sts.s.rsp_timeout ? "yes" : "no");
  547. printf(" 14: rsp_crc_err: %s\n",
  548. emm_rsp_sts.s.rsp_crc_err ? "yes" : "no");
  549. printf(" 13: rsp_bad_sts: %s\n",
  550. emm_rsp_sts.s.rsp_bad_sts ? "yes" : "no");
  551. printf(" 12: rsp_val: %s\n",
  552. emm_rsp_sts.s.rsp_val ? "yes" : "no");
  553. printf(" 9-11: rsp_type: %s\n",
  554. rtype_xor_str[emm_rsp_sts.s.rsp_type]);
  555. printf(" 7-8: cmd_type: %s\n",
  556. ctype_xor_str[emm_rsp_sts.s.cmd_type]);
  557. printf(" 1-6: cmd_idx: %u\n",
  558. emm_rsp_sts.s.cmd_idx);
  559. printf(" 0: cmd_done: %s\n",
  560. emm_rsp_sts.s.cmd_done ? "yes" : "no");
  561. emm_rsp_lo.u = readq(host->base_addr + MIO_EMM_RSP_LO());
  562. printf("\nMIO_EMM_RSP_STS_LO: 0x%016llx\n", emm_rsp_lo.u);
  563. emm_rsp_hi.u = readq(host->base_addr + MIO_EMM_RSP_HI());
  564. printf("\nMIO_EMM_RSP_STS_HI: 0x%016llx\n", emm_rsp_hi.u);
  565. emm_int.u = readq(host->base_addr + MIO_EMM_INT());
  566. printf("\nMIO_EMM_INT: 0x%016llx\n", emm_int.u);
  567. printf(" 6: switch_err: %s\n",
  568. emm_int.s.switch_err ? "yes" : "no");
  569. printf(" 5: switch_done: %s\n",
  570. emm_int.s.switch_done ? "yes" : "no");
  571. printf(" 4: dma_err: %s\n",
  572. emm_int.s.dma_err ? "yes" : "no");
  573. printf(" 3: cmd_err: %s\n",
  574. emm_int.s.cmd_err ? "yes" : "no");
  575. printf(" 2: dma_done: %s\n",
  576. emm_int.s.dma_done ? "yes" : "no");
  577. printf(" 1: cmd_done: %s\n",
  578. emm_int.s.cmd_done ? "yes" : "no");
  579. printf(" 0: buf_done: %s\n",
  580. emm_int.s.buf_done ? "yes" : "no");
  581. emm_wdog.u = readq(host->base_addr + MIO_EMM_WDOG());
  582. printf("\nMIO_EMM_WDOG: 0x%016llx (%u)\n",
  583. emm_wdog.u, emm_wdog.s.clk_cnt);
  584. if (IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
  585. emm_sample.u = readq(host->base_addr + MIO_EMM_SAMPLE());
  586. printf("\nMIO_EMM_SAMPLE: 0x%016llx\n",
  587. emm_sample.u);
  588. printf(" 16-25: cmd_cnt: %u\n",
  589. emm_sample.s.cmd_cnt);
  590. printf(" 0-9: dat_cnt: %u\n",
  591. emm_sample.s.dat_cnt);
  592. }
  593. emm_sts_mask.u = readq(host->base_addr + MIO_EMM_STS_MASK());
  594. printf("\nMIO_EMM_STS_MASK: 0x%016llx\n", emm_sts_mask.u);
  595. emm_rca.u = readq(host->base_addr + MIO_EMM_RCA());
  596. printf("\nMIO_EMM_RCA: 0x%016llx\n", emm_rca.u);
  597. printf(" 0-15: card_rca: 0x%04x\n",
  598. emm_rca.s.card_rca);
  599. if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
  600. emm_calb.u = readq(host->base_addr + MIO_EMM_CALB());
  601. printf("\nMIO_EMM_CALB: 0x%016llx\n",
  602. emm_calb.u);
  603. printf(" 0: start: %u\n",
  604. emm_calb.s.start);
  605. emm_tap.u = readq(host->base_addr + MIO_EMM_TAP());
  606. printf("\nMIO_EMM_TAP: 0x%016llx\n",
  607. emm_tap.u);
  608. printf(" 7-0: delay: %u\n", emm_tap.s.delay);
  609. emm_timing.u = readq(host->base_addr + MIO_EMM_TIMING());
  610. printf("\nMIO_EMM_TIMING: 0x%016llx\n",
  611. emm_timing.u);
  612. printf(" 53-48: cmd_in_tap: %u\n",
  613. emm_timing.s.cmd_in_tap);
  614. printf(" 37-32: cmd_out_tap: %u\n",
  615. emm_timing.s.cmd_out_tap);
  616. printf(" 21-16: data_in_tap: %u\n",
  617. emm_timing.s.data_in_tap);
  618. printf(" 5-0: data_out_tap: %u\n",
  619. emm_timing.s.data_out_tap);
  620. io_ctl.u = readq(host->base_addr + MIO_EMM_IO_CTL());
  621. printf("\nMIO_IO_CTL: 0x%016llx\n", io_ctl.u);
  622. printf(" 3-2: drive: %u (%u mA)\n",
  623. io_ctl.s.drive, 2 << io_ctl.s.drive);
  624. printf(" 0: slew: %u %s\n", io_ctl.s.slew,
  625. io_ctl.s.slew ? "high" : "low");
  626. emm_debug.u = readq(host->base_addr + MIO_EMM_DEBUG());
  627. printf("\nMIO_EMM_DEBUG: 0x%016llx\n",
  628. emm_debug.u);
  629. printf(" 21: rdsync_rst 0x%x\n",
  630. emm_debug.s.rdsync_rst);
  631. printf(" 20: emmc_clk_disable 0x%x\n",
  632. emm_debug.s.emmc_clk_disable);
  633. printf(" 19-16: dma_sm: 0x%x\n",
  634. emm_debug.s.dma_sm);
  635. printf(" 15-12: data_sm: 0x%x\n",
  636. emm_debug.s.data_sm);
  637. printf(" 11-8: cmd_sm: 0x%x\n",
  638. emm_debug.s.cmd_sm);
  639. printf(" 0: clk_on: 0x%x\n",
  640. emm_debug.s.clk_on);
  641. }
  642. puts("\n");
  643. }
  644. /**
  645. * Print out all of the register values
  646. *
  647. * @param mmc MMC device
  648. */
  649. static void octeontx_mmc_print_registers(struct mmc *mmc)
  650. {
  651. #ifdef DEBUG_REGISTERS
  652. const int print = 1;
  653. #else
  654. const int print = 0;
  655. #endif
  656. if (print)
  657. octeontx_mmc_print_registers2(mmc, mmc_to_host(mmc));
  658. }
  659. #else
  660. static void octeontx_mmc_print_registers(struct mmc *mmc)
  661. {
  662. return;
  663. }
  664. #endif
  665. static const struct octeontx_sd_mods octeontx_cr_types[] = {
  666. { {0, 0}, {0, 0}, {0, 0} }, /* CMD0 */
  667. { {0, 3}, {0, 3}, {0, 0} }, /* CMD1 */
  668. { {0, 2}, {0, 2}, {0, 0} }, /* CMD2 */
  669. { {0, 1}, {0, 3}, {0, 0} }, /* CMD3 SD_CMD_SEND_RELATIVE_ADDR 0, 2 */
  670. { {0, 0}, {0, 0}, {0, 0} }, /* CMD4 */
  671. { {0, 1}, {0, 1}, {0, 0} }, /* CMD5 */
  672. { {0, 1}, {1, 1}, {0, 1} }, /*
  673. * CMD6 SD_CMD_SWITCH_FUNC 1,0
  674. * (ACMD) SD_APP_SET_BUS_WIDTH
  675. */
  676. { {0, 1}, {0, 1}, {0, 0} }, /* CMD7 */
  677. { {1, 1}, {0, 3}, {0, 0} }, /* CMD8 SD_CMD_SEND_IF_COND 1,2 */
  678. { {0, 2}, {0, 2}, {0, 0} }, /* CMD9 */
  679. { {0, 2}, {0, 2}, {0, 0} }, /* CMD10 */
  680. { {1, 1}, {0, 1}, {1, 1} }, /* CMD11 SD_CMD_SWITCH_UHS18V 1,0 */
  681. { {0, 1}, {0, 1}, {0, 0} }, /* CMD12 */
  682. { {0, 1}, {0, 1}, {1, 3} }, /* CMD13 (ACMD)) SD_CMD_APP_SD_STATUS 1,2 */
  683. { {1, 1}, {1, 1}, {0, 0} }, /* CMD14 */
  684. { {0, 0}, {0, 0}, {0, 0} }, /* CMD15 */
  685. { {0, 1}, {0, 1}, {0, 0} }, /* CMD16 */
  686. { {1, 1}, {1, 1}, {0, 0} }, /* CMD17 */
  687. { {1, 1}, {1, 1}, {0, 0} }, /* CMD18 */
  688. { {3, 1}, {3, 1}, {0, 0} }, /* CMD19 */
  689. { {2, 1}, {0, 0}, {0, 0} }, /* CMD20 */ /* SD 2,0 */
  690. { {0, 0}, {0, 0}, {0, 0} }, /* CMD21 */
  691. { {0, 0}, {0, 0}, {1, 1} }, /* CMD22 (ACMD) SD_APP_SEND_NUM_WR_BLKS 1,0 */
  692. { {0, 1}, {0, 1}, {0, 1} }, /* CMD23 */ /* SD ACMD 1,0 */
  693. { {2, 1}, {2, 1}, {2, 1} }, /* CMD24 */
  694. { {2, 1}, {2, 1}, {2, 1} }, /* CMD25 */
  695. { {2, 1}, {2, 1}, {2, 1} }, /* CMD26 */
  696. { {2, 1}, {2, 1}, {2, 1} }, /* CMD27 */
  697. { {0, 1}, {0, 1}, {0, 1} }, /* CMD28 */
  698. { {0, 1}, {0, 1}, {0, 1} }, /* CMD29 */
  699. { {1, 1}, {1, 1}, {1, 1} }, /* CMD30 */
  700. { {1, 1}, {1, 1}, {1, 1} }, /* CMD31 */
  701. { {0, 0}, {0, 1}, {0, 0} }, /* CMD32 SD_CMD_ERASE_WR_BLK_START 0,1 */
  702. { {0, 0}, {0, 1}, {0, 0} }, /* CMD33 SD_CMD_ERASE_WR_BLK_END 0,1 */
  703. { {0, 0}, {0, 0}, {0, 0} }, /* CMD34 */
  704. { {0, 1}, {0, 1}, {0, 1} }, /* CMD35 */
  705. { {0, 1}, {0, 1}, {0, 1} }, /* CMD36 */
  706. { {0, 0}, {0, 0}, {0, 0} }, /* CMD37 */
  707. { {0, 1}, {0, 1}, {0, 1} }, /* CMD38 */
  708. { {0, 4}, {0, 4}, {0, 4} }, /* CMD39 */
  709. { {0, 5}, {0, 5}, {0, 5} }, /* CMD40 */
  710. { {0, 0}, {0, 0}, {0, 3} }, /* CMD41 (ACMD) SD_CMD_APP_SEND_OP_COND 0,3 */
  711. { {2, 1}, {2, 1}, {2, 1} }, /* CMD42 */
  712. { {0, 0}, {0, 0}, {0, 0} }, /* CMD43 */
  713. { {0, 0}, {0, 0}, {0, 0} }, /* CMD44 */
  714. { {0, 0}, {0, 0}, {0, 0} }, /* CMD45 */
  715. { {0, 0}, {0, 0}, {0, 0} }, /* CMD46 */
  716. { {0, 0}, {0, 0}, {0, 0} }, /* CMD47 */
  717. { {0, 0}, {1, 0}, {0, 0} }, /* CMD48 SD_CMD_READ_EXTR_SINGLE */
  718. { {0, 0}, {2, 0}, {0, 0} }, /* CMD49 SD_CMD_WRITE_EXTR_SINGLE */
  719. { {0, 0}, {0, 0}, {0, 0} }, /* CMD50 */
  720. { {0, 0}, {0, 0}, {1, 1} }, /* CMD51 (ACMD) SD_CMD_APP_SEND_SCR 1,1 */
  721. { {0, 0}, {0, 0}, {0, 0} }, /* CMD52 */
  722. { {0, 0}, {0, 0}, {0, 0} }, /* CMD53 */
  723. { {0, 0}, {0, 0}, {0, 0} }, /* CMD54 */
  724. { {0, 1}, {0, 1}, {0, 1} }, /* CMD55 */
  725. { {0xff, 0xff}, {0xff, 0xff}, {0xff, 0xff} }, /* CMD56 */
  726. { {0, 0}, {0, 0}, {0, 0} }, /* CMD57 */
  727. { {0, 0}, {0, 3}, {0, 3} }, /* CMD58 SD_CMD_SPI_READ_OCR 0,3 */
  728. { {0, 0}, {0, 1}, {0, 0} }, /* CMD59 SD_CMD_SPI_CRC_ON_OFF 0,1 */
  729. { {0, 0}, {0, 0}, {0, 0} }, /* CMD60 */
  730. { {0, 0}, {0, 0}, {0, 0} }, /* CMD61 */
  731. { {0, 0}, {0, 0}, {0, 0} }, /* CMD62 */
  732. { {0, 0}, {0, 0}, {0, 0} } /* CMD63 */
  733. };
  734. /**
  735. * Returns XOR values needed for SD commands and other quirks
  736. *
  737. * @param mmc mmc device
  738. * @param cmd command information
  739. *
  740. * @return octeontx_mmc_cr_mods data structure with various quirks and flags
  741. */
  742. static struct octeontx_mmc_cr_mods
  743. octeontx_mmc_get_cr_mods(struct mmc *mmc, const struct mmc_cmd *cmd,
  744. const struct mmc_data *data)
  745. {
  746. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  747. struct octeontx_mmc_cr_mods cr = {0, 0};
  748. const struct octeontx_sd_mods *sdm =
  749. &octeontx_cr_types[cmd->cmdidx & 0x3f];
  750. u8 c = sdm->mmc.c, r = sdm->mmc.r;
  751. u8 desired_ctype = 0;
  752. if (IS_MMC(mmc)) {
  753. #ifdef MMC_SUPPORTS_TUNING
  754. if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
  755. if (cmd->resp_type == MMC_RSP_R1)
  756. cr.rtype_xor = 1;
  757. if (data && data->flags & MMC_DATA_READ)
  758. cr.ctype_xor = 1;
  759. }
  760. #endif
  761. return cr;
  762. }
  763. if (cmd->cmdidx == 56)
  764. c = (cmd->cmdarg & 1) ? 1 : 2;
  765. if (data) {
  766. if (data->flags & MMC_DATA_READ)
  767. desired_ctype = 1;
  768. else if (data->flags & MMC_DATA_WRITE)
  769. desired_ctype = 2;
  770. }
  771. cr.ctype_xor = c ^ desired_ctype;
  772. if (slot->is_acmd)
  773. cr.rtype_xor = r ^ sdm->sdacmd.r;
  774. else
  775. cr.rtype_xor = r ^ sdm->sd.r;
  776. debug("%s(%s): mmc c: %d, mmc r: %d, desired c: %d, xor c: %d, xor r: %d\n",
  777. __func__, mmc->dev->name, c, r, desired_ctype,
  778. cr.ctype_xor, cr.rtype_xor);
  779. return cr;
  780. }
  781. /**
  782. * Keep track of switch commands internally
  783. */
  784. static void octeontx_mmc_track_switch(struct mmc *mmc, u32 cmd_arg)
  785. {
  786. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  787. u8 how = (cmd_arg >> 24) & 3;
  788. u8 where = (u8)(cmd_arg >> 16);
  789. u8 val = (u8)(cmd_arg >> 8);
  790. slot->want_switch = slot->cached_switch;
  791. if (slot->is_acmd)
  792. return;
  793. if (how != 3)
  794. return;
  795. switch (where) {
  796. case EXT_CSD_BUS_WIDTH:
  797. slot->want_switch.s.bus_width = val;
  798. break;
  799. case EXT_CSD_POWER_CLASS:
  800. slot->want_switch.s.power_class = val;
  801. break;
  802. case EXT_CSD_HS_TIMING:
  803. slot->want_switch.s.hs_timing = 0;
  804. #if !defined(CONFIG_ARCH_OCTEON)
  805. slot->want_switch.s.hs200_timing = 0;
  806. slot->want_switch.s.hs400_timing = 0;
  807. #endif
  808. switch (val & 0xf) {
  809. case 0:
  810. break;
  811. case 1:
  812. slot->want_switch.s.hs_timing = 1;
  813. break;
  814. #if !defined(CONFIG_ARCH_OCTEON)
  815. case 2:
  816. if (!slot->is_asim && !slot->is_emul)
  817. slot->want_switch.s.hs200_timing = 1;
  818. break;
  819. case 3:
  820. if (!slot->is_asim && !slot->is_emul)
  821. slot->want_switch.s.hs400_timing = 1;
  822. break;
  823. #endif
  824. default:
  825. pr_err("%s(%s): Unsupported timing mode 0x%x\n",
  826. __func__, mmc->dev->name, val & 0xf);
  827. break;
  828. }
  829. break;
  830. default:
  831. break;
  832. }
  833. }
  834. static int octeontx_mmc_print_rsp_errors(struct mmc *mmc,
  835. union mio_emm_rsp_sts rsp_sts)
  836. {
  837. bool err = false;
  838. const char *name = mmc->dev->name;
  839. if (rsp_sts.s.acc_timeout) {
  840. pr_warn("%s(%s): acc_timeout\n", __func__, name);
  841. err = true;
  842. }
  843. if (rsp_sts.s.dbuf_err) {
  844. pr_warn("%s(%s): dbuf_err\n", __func__, name);
  845. err = true;
  846. }
  847. if (rsp_sts.s.blk_timeout) {
  848. pr_warn("%s(%s): blk_timeout\n", __func__, name);
  849. err = true;
  850. }
  851. if (rsp_sts.s.blk_crc_err) {
  852. pr_warn("%s(%s): blk_crc_err\n", __func__, name);
  853. err = true;
  854. }
  855. if (rsp_sts.s.stp_timeout) {
  856. pr_warn("%s(%s): stp_timeout\n", __func__, name);
  857. err = true;
  858. }
  859. if (rsp_sts.s.stp_crc_err) {
  860. pr_warn("%s(%s): stp_crc_err\n", __func__, name);
  861. err = true;
  862. }
  863. if (rsp_sts.s.stp_bad_sts) {
  864. pr_warn("%s(%s): stp_bad_sts\n", __func__, name);
  865. err = true;
  866. }
  867. if (err)
  868. pr_warn(" rsp_sts: 0x%llx\n", rsp_sts.u);
  869. return err ? -1 : 0;
  870. }
  871. /**
  872. * Starts a DMA operation for block read/write
  873. *
  874. * @param mmc mmc device
  875. * @param write true if write operation
  876. * @param clear true to clear DMA operation
  877. * @param adr source or destination DMA address
  878. * @param size size in blocks
  879. * @param timeout timeout in ms
  880. */
  881. static void octeontx_mmc_start_dma(struct mmc *mmc, bool write,
  882. bool clear, u32 block, dma_addr_t adr,
  883. u32 size, int timeout)
  884. {
  885. const struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  886. union mio_emm_dma_cfg emm_dma_cfg;
  887. union mio_emm_dma_adr emm_dma_adr;
  888. union mio_emm_dma emm_dma;
  889. /* Clear any interrupts */
  890. write_csr(mmc, MIO_EMM_DMA_INT(),
  891. read_csr(mmc, MIO_EMM_DMA_INT()));
  892. emm_dma_cfg.u = 0;
  893. emm_dma_cfg.s.en = 1;
  894. emm_dma_cfg.s.rw = !!write;
  895. emm_dma_cfg.s.clr = !!clear;
  896. emm_dma_cfg.s.size = ((u64)(size * mmc->read_bl_len) / 8) - 1;
  897. #if __BYTE_ORDER != __BIG_ENDIAN
  898. emm_dma_cfg.s.endian = 1;
  899. #endif
  900. emm_dma_adr.u = 0;
  901. emm_dma_adr.s.adr = adr;
  902. write_csr(mmc, MIO_EMM_DMA_ADR(), emm_dma_adr.u);
  903. write_csr(mmc, MIO_EMM_DMA_CFG(), emm_dma_cfg.u);
  904. emm_dma.u = 0;
  905. emm_dma.s.bus_id = slot->bus_id;
  906. emm_dma.s.dma_val = 1;
  907. emm_dma.s.rw = !!write;
  908. emm_dma.s.sector = mmc->high_capacity ? 1 : 0;
  909. if (size > 1 && ((IS_SD(mmc) && (mmc->scr[0] & 2)) || !IS_SD(mmc)))
  910. emm_dma.s.multi = 1;
  911. else
  912. emm_dma.s.multi = 0;
  913. emm_dma.s.block_cnt = size;
  914. if (!mmc->high_capacity)
  915. block *= mmc->read_bl_len;
  916. emm_dma.s.card_addr = block;
  917. debug("%s(%s): card address: 0x%x, size: %d, multi: %d\n",
  918. __func__, mmc->dev->name, block, size, emm_dma.s.multi);
  919. if (timeout > 0)
  920. timeout = (timeout * 1000) - 1000;
  921. set_wdog(mmc, timeout);
  922. debug(" Writing 0x%llx to mio_emm_dma\n", emm_dma.u);
  923. write_csr(mmc, MIO_EMM_DMA(), emm_dma.u);
  924. }
  925. /**
  926. * Waits for a DMA operation to complete
  927. *
  928. * @param mmc mmc device
  929. * @param timeout timeout in ms
  930. *
  931. * @return 0 for success (could be DMA errors), -ETIMEDOUT on timeout
  932. */
  933. /**
  934. * Cleanup DMA engine after a failure
  935. *
  936. * @param mmc mmc device
  937. * @param rsp_sts rsp status
  938. */
  939. static void octeontx_mmc_cleanup_dma(struct mmc *mmc,
  940. union mio_emm_rsp_sts rsp_sts)
  941. {
  942. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  943. union mio_emm_dma emm_dma;
  944. ulong start;
  945. int retries = 3;
  946. do {
  947. debug("%s(%s): rsp_sts: 0x%llx, rsp_lo: 0x%llx, dma_int: 0x%llx\n",
  948. __func__, mmc->dev->name, rsp_sts.u,
  949. read_csr(mmc, MIO_EMM_RSP_LO()),
  950. read_csr(mmc, MIO_EMM_DMA_INT()));
  951. emm_dma.u = read_csr(mmc, MIO_EMM_DMA());
  952. emm_dma.s.dma_val = 1;
  953. emm_dma.s.dat_null = 1;
  954. emm_dma.s.bus_id = slot->bus_id;
  955. write_csr(mmc, MIO_EMM_DMA(), emm_dma.u);
  956. start = get_timer(0);
  957. do {
  958. rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
  959. WATCHDOG_RESET();
  960. } while (get_timer(start) < 100 &&
  961. (rsp_sts.s.dma_val || rsp_sts.s.dma_pend));
  962. } while (retries-- >= 0 && rsp_sts.s.dma_pend);
  963. if (rsp_sts.s.dma_val)
  964. pr_err("%s(%s): Error: could not clean up DMA. RSP_STS: 0x%llx, RSP_LO: 0x%llx\n",
  965. __func__, mmc->dev->name, rsp_sts.u,
  966. read_csr(mmc, MIO_EMM_RSP_LO()));
  967. debug(" rsp_sts after clearing up DMA: 0x%llx\n",
  968. read_csr(mmc, MIO_EMM_RSP_STS()));
  969. }
  970. /**
  971. * Waits for a DMA operation to complete
  972. *
  973. * @param mmc mmc device
  974. * @param timeout timeout in ms
  975. * @param verbose true to print out error information
  976. *
  977. * @return 0 for success (could be DMA errors), -ETIMEDOUT on timeout
  978. * or -EIO if IO error.
  979. */
  980. static int octeontx_mmc_wait_dma(struct mmc *mmc, bool write, ulong timeout,
  981. bool verbose)
  982. {
  983. struct octeontx_mmc_host *host = mmc_to_host(mmc);
  984. ulong start_time = get_timer(0);
  985. union mio_emm_dma_int emm_dma_int;
  986. union mio_emm_rsp_sts rsp_sts;
  987. union mio_emm_dma emm_dma;
  988. bool timed_out = false;
  989. bool err = false;
  990. debug("%s(%s, %lu, %d), delay: %uus\n", __func__, mmc->dev->name,
  991. timeout, verbose, host->dma_wait_delay);
  992. udelay(host->dma_wait_delay);
  993. do {
  994. emm_dma_int.u = read_csr(mmc, MIO_EMM_DMA_INT());
  995. rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
  996. if (write) {
  997. if ((rsp_sts.s.dma_pend && !rsp_sts.s.dma_val) ||
  998. rsp_sts.s.blk_timeout ||
  999. rsp_sts.s.stp_timeout ||
  1000. rsp_sts.s.rsp_timeout) {
  1001. err = true;
  1002. #ifdef DEBUG
  1003. debug("%s: f1\n", __func__);
  1004. octeontx_mmc_print_rsp_errors(mmc, rsp_sts);
  1005. #endif
  1006. break;
  1007. }
  1008. } else {
  1009. if (rsp_sts.s.blk_crc_err ||
  1010. (rsp_sts.s.dma_pend && !rsp_sts.s.dma_val)) {
  1011. err = true;
  1012. #if defined(DEBUG)
  1013. octeontx_mmc_print_rsp_errors(mmc, rsp_sts);
  1014. #endif
  1015. break;
  1016. }
  1017. }
  1018. if (rsp_sts.s.dma_pend) {
  1019. /*
  1020. * If this is set then an error has occurred.
  1021. * Try and restart the DMA operation.
  1022. */
  1023. emm_dma.u = read_csr(mmc, MIO_EMM_DMA());
  1024. if (verbose) {
  1025. pr_err("%s(%s): DMA pending error: rsp_sts: 0x%llx, dma_int: 0x%llx, emm_dma: 0x%llx\n",
  1026. __func__, mmc->dev->name, rsp_sts.u,
  1027. emm_dma_int.u, emm_dma.u);
  1028. octeontx_print_rsp_sts(mmc);
  1029. debug(" MIO_EMM_DEBUG: 0x%llx\n",
  1030. read_csr(mmc, MIO_EMM_DEBUG()));
  1031. pr_err("%s: Trying DMA resume...\n", __func__);
  1032. }
  1033. emm_dma.s.dma_val = 1;
  1034. emm_dma.s.dat_null = 1;
  1035. write_csr(mmc, MIO_EMM_DMA(), emm_dma.u);
  1036. udelay(10);
  1037. } else if (!rsp_sts.s.dma_val && emm_dma_int.s.done) {
  1038. break;
  1039. }
  1040. WATCHDOG_RESET();
  1041. timed_out = (get_timer(start_time) > timeout);
  1042. } while (!timed_out);
  1043. if (timed_out || err) {
  1044. if (verbose) {
  1045. pr_err("%s(%s): MMC DMA %s after %lu ms, rsp_sts: 0x%llx, dma_int: 0x%llx, rsp_sts_lo: 0x%llx, emm_dma: 0x%llx\n",
  1046. __func__, mmc->dev->name,
  1047. timed_out ? "timed out" : "error",
  1048. get_timer(start_time), rsp_sts.u,
  1049. emm_dma_int.u,
  1050. read_csr(mmc, MIO_EMM_RSP_LO()),
  1051. read_csr(mmc, MIO_EMM_DMA()));
  1052. octeontx_print_rsp_sts(mmc);
  1053. }
  1054. if (rsp_sts.s.dma_pend)
  1055. octeontx_mmc_cleanup_dma(mmc, rsp_sts);
  1056. } else {
  1057. write_csr(mmc, MIO_EMM_DMA_INT(),
  1058. read_csr(mmc, MIO_EMM_DMA_INT()));
  1059. }
  1060. return timed_out ? -ETIMEDOUT : (err ? -EIO : 0);
  1061. }
  1062. /**
  1063. * Read blocks from the MMC/SD device
  1064. *
  1065. * @param mmc mmc device
  1066. * @param cmd command
  1067. * @param data data for read
  1068. * @param verbose true to print out error information
  1069. *
  1070. * @return number of blocks read or 0 if error
  1071. */
  1072. static int octeontx_mmc_read_blocks(struct mmc *mmc, struct mmc_cmd *cmd,
  1073. struct mmc_data *data, bool verbose)
  1074. {
  1075. struct octeontx_mmc_host *host = mmc_to_host(mmc);
  1076. union mio_emm_rsp_sts rsp_sts;
  1077. dma_addr_t dma_addr = (dma_addr_t)dm_pci_virt_to_mem(host->dev,
  1078. data->dest);
  1079. ulong count;
  1080. ulong blkcnt = data->blocks;
  1081. ulong start = cmd->cmdarg;
  1082. int timeout = 1000 + blkcnt * 20;
  1083. bool timed_out = false;
  1084. bool multi_xfer = cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK;
  1085. debug("%s(%s): dest: %p, dma address: 0x%llx, blkcnt: %lu, start: %lu\n",
  1086. __func__, mmc->dev->name, data->dest, dma_addr, blkcnt, start);
  1087. debug("%s: rsp_sts: 0x%llx\n", __func__,
  1088. read_csr(mmc, MIO_EMM_RSP_STS()));
  1089. /* use max timeout for multi-block transfers */
  1090. /* timeout = 0; */
  1091. /*
  1092. * If we have a valid SD card in the slot, we set the response bit
  1093. * mask to check for CRC errors and timeouts only.
  1094. * Otherwise, use the default power on reset value.
  1095. */
  1096. write_csr(mmc, MIO_EMM_STS_MASK(),
  1097. IS_SD(mmc) ? 0x00b00000ull : 0xe4390080ull);
  1098. invalidate_dcache_range((u64)data->dest,
  1099. (u64)data->dest + blkcnt * data->blocksize);
  1100. if (multi_xfer) {
  1101. octeontx_mmc_start_dma(mmc, false, false, start, dma_addr,
  1102. blkcnt, timeout);
  1103. timed_out = !!octeontx_mmc_wait_dma(mmc, false, timeout,
  1104. verbose);
  1105. rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
  1106. if (timed_out || rsp_sts.s.dma_val || rsp_sts.s.dma_pend) {
  1107. if (!verbose)
  1108. return 0;
  1109. pr_err("%s(%s): Error: DMA timed out. rsp_sts: 0x%llx, emm_int: 0x%llx, dma_int: 0x%llx, rsp_lo: 0x%llx\n",
  1110. __func__, mmc->dev->name, rsp_sts.u,
  1111. read_csr(mmc, MIO_EMM_INT()),
  1112. read_csr(mmc, MIO_EMM_DMA_INT()),
  1113. read_csr(mmc, MIO_EMM_RSP_LO()));
  1114. pr_err("%s: block count: %lu, start: 0x%lx\n",
  1115. __func__, blkcnt, start);
  1116. octeontx_mmc_print_registers(mmc);
  1117. return 0;
  1118. }
  1119. } else {
  1120. count = blkcnt;
  1121. timeout = 1000;
  1122. do {
  1123. octeontx_mmc_start_dma(mmc, false, false, start,
  1124. dma_addr, 1, timeout);
  1125. dma_addr += mmc->read_bl_len;
  1126. start++;
  1127. timed_out = !!octeontx_mmc_wait_dma(mmc, false,
  1128. timeout, verbose);
  1129. rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
  1130. if (timed_out || rsp_sts.s.dma_val ||
  1131. rsp_sts.s.dma_pend) {
  1132. if (verbose) {
  1133. pr_err("%s: Error: DMA timed out. rsp_sts: 0x%llx, emm_int: 0x%llx, dma_int: 0x%llx, rsp_lo: 0x%llx\n",
  1134. __func__, rsp_sts.u,
  1135. read_csr(mmc, MIO_EMM_INT()),
  1136. read_csr(mmc, MIO_EMM_DMA_INT()),
  1137. read_csr(mmc, MIO_EMM_RSP_LO()));
  1138. pr_err("%s: block count: 1, start: 0x%lx\n",
  1139. __func__, start);
  1140. octeontx_mmc_print_registers(mmc);
  1141. }
  1142. return blkcnt - count;
  1143. }
  1144. WATCHDOG_RESET();
  1145. } while (--count);
  1146. }
  1147. #ifdef DEBUG
  1148. debug("%s(%s): Read %lu (0x%lx) blocks starting at block %u (0x%x) to address %p (dma address 0x%llx)\n",
  1149. __func__, mmc->dev->name, blkcnt, blkcnt,
  1150. cmd->cmdarg, cmd->cmdarg, data->dest,
  1151. dm_pci_virt_to_mem(host->dev, data->dest));
  1152. print_buffer(0, data->dest, 1, 0x200, 0);
  1153. #endif
  1154. return blkcnt;
  1155. }
  1156. static int octeontx_mmc_poll_ready(struct mmc *mmc, ulong timeout)
  1157. {
  1158. ulong start;
  1159. struct mmc_cmd cmd;
  1160. int err;
  1161. bool not_ready = false;
  1162. memset(&cmd, 0, sizeof(cmd));
  1163. cmd.cmdidx = MMC_CMD_SEND_STATUS;
  1164. cmd.cmdarg = mmc->rca << 16;
  1165. cmd.resp_type = MMC_RSP_R1;
  1166. start = get_timer(0);
  1167. do {
  1168. err = octeontx_mmc_send_cmd(mmc, &cmd, NULL);
  1169. if (err) {
  1170. pr_err("%s(%s): MMC command error: %d; Retry...\n",
  1171. __func__, mmc->dev->name, err);
  1172. not_ready = true;
  1173. } else if (cmd.response[0] & R1_READY_FOR_DATA) {
  1174. return 0;
  1175. }
  1176. WATCHDOG_RESET();
  1177. } while (get_timer(start) < timeout);
  1178. if (not_ready)
  1179. pr_err("%s(%s): MMC command error; Retry timeout\n",
  1180. __func__, mmc->dev->name);
  1181. return -ETIMEDOUT;
  1182. }
  1183. static ulong octeontx_mmc_write_blocks(struct mmc *mmc, struct mmc_cmd *cmd,
  1184. struct mmc_data *data)
  1185. {
  1186. struct octeontx_mmc_host *host = mmc_to_host(mmc);
  1187. ulong start = cmd->cmdarg;
  1188. ulong blkcnt = data->blocks;
  1189. dma_addr_t dma_addr;
  1190. union mio_emm_rsp_sts rsp_sts;
  1191. union mio_emm_sts_mask emm_sts_mask;
  1192. ulong timeout;
  1193. int count;
  1194. bool timed_out = false;
  1195. bool multi_xfer = (blkcnt > 1) &&
  1196. ((IS_SD(mmc) && mmc->scr[0] & 2) || !IS_SD(mmc));
  1197. octeontx_mmc_switch_to(mmc);
  1198. emm_sts_mask.u = 0;
  1199. emm_sts_mask.s.sts_msk = R1_BLOCK_WRITE_MASK;
  1200. write_csr(mmc, MIO_EMM_STS_MASK(), emm_sts_mask.u);
  1201. if (octeontx_mmc_poll_ready(mmc, 10000)) {
  1202. pr_err("%s(%s): Ready timed out\n", __func__, mmc->dev->name);
  1203. return 0;
  1204. }
  1205. flush_dcache_range((u64)data->src,
  1206. (u64)data->src + blkcnt * mmc->write_bl_len);
  1207. dma_addr = (u64)dm_pci_virt_to_mem(host->dev, (void *)data->src);
  1208. if (multi_xfer) {
  1209. timeout = 5000 + 100 * blkcnt;
  1210. octeontx_mmc_start_dma(mmc, true, false, start, dma_addr,
  1211. blkcnt, timeout);
  1212. timed_out = !!octeontx_mmc_wait_dma(mmc, true, timeout, true);
  1213. rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
  1214. if (timed_out || rsp_sts.s.dma_val || rsp_sts.s.dma_pend) {
  1215. pr_err("%s(%s): Error: multi-DMA timed out after %lums. rsp_sts: 0x%llx, emm_int: 0x%llx, emm_dma_int: 0x%llx, rsp_sts_lo: 0x%llx, emm_dma: 0x%llx\n",
  1216. __func__, mmc->dev->name, timeout,
  1217. rsp_sts.u,
  1218. read_csr(mmc, MIO_EMM_INT()),
  1219. read_csr(mmc, MIO_EMM_DMA_INT()),
  1220. read_csr(mmc, MIO_EMM_RSP_LO()),
  1221. read_csr(mmc, MIO_EMM_DMA()));
  1222. return 0;
  1223. }
  1224. } else {
  1225. timeout = 5000;
  1226. count = blkcnt;
  1227. do {
  1228. octeontx_mmc_start_dma(mmc, true, false, start,
  1229. dma_addr, 1, timeout);
  1230. dma_addr += mmc->read_bl_len;
  1231. start++;
  1232. timed_out = !!octeontx_mmc_wait_dma(mmc, true, timeout,
  1233. true);
  1234. rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
  1235. if (timed_out || rsp_sts.s.dma_val ||
  1236. rsp_sts.s.dma_pend) {
  1237. pr_err("%s(%s): Error: single-DMA timed out after %lums. rsp_sts: 0x%llx, emm_int: 0x%llx, emm_dma_int: 0x%llx, rsp_sts_lo: 0x%llx, emm_dma: 0x%llx\n",
  1238. __func__, mmc->dev->name, timeout,
  1239. rsp_sts.u,
  1240. read_csr(mmc, MIO_EMM_RSP_STS()),
  1241. read_csr(mmc, MIO_EMM_DMA_INT()),
  1242. read_csr(mmc, MIO_EMM_RSP_LO()),
  1243. read_csr(mmc, MIO_EMM_DMA()));
  1244. return blkcnt - count;
  1245. }
  1246. WATCHDOG_RESET();
  1247. } while (--count);
  1248. }
  1249. return blkcnt;
  1250. }
  1251. /**
  1252. * Send a command to the eMMC/SD device
  1253. *
  1254. * @param mmc mmc device
  1255. * @param cmd cmd to send and response
  1256. * @param data additional data
  1257. * @param flags
  1258. * @return 0 for success, otherwise error
  1259. */
  1260. static int octeontx_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
  1261. struct mmc_data *data)
  1262. {
  1263. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  1264. const char *name = slot->dev->name;
  1265. struct octeontx_mmc_cr_mods mods = {0, 0};
  1266. union mio_emm_rsp_sts rsp_sts;
  1267. union mio_emm_cmd emm_cmd;
  1268. union mio_emm_rsp_lo rsp_lo;
  1269. union mio_emm_buf_idx emm_buf_idx;
  1270. union mio_emm_buf_dat emm_buf_dat;
  1271. ulong start;
  1272. int i;
  1273. ulong blkcnt;
  1274. /**
  1275. * This constant has a 1 bit for each command which should have a short
  1276. * timeout and a 0 for each bit with a long timeout. Currently the
  1277. * following commands have a long timeout:
  1278. * CMD6, CMD17, CMD18, CMD24, CMD25, CMD32, CMD33, CMD35, CMD36 and
  1279. * CMD38.
  1280. */
  1281. static const u64 timeout_short = 0xFFFFFFA4FCF9FFDFull;
  1282. uint timeout;
  1283. if (cmd->cmdidx == MMC_CMD_SEND_EXT_CSD) {
  1284. union mio_emm_rca emm_rca;
  1285. emm_rca.u = 0;
  1286. emm_rca.s.card_rca = mmc->rca;
  1287. write_csr(mmc, MIO_EMM_RCA(), emm_rca.u);
  1288. }
  1289. if (timeout_short & (1ull << cmd->cmdidx))
  1290. timeout = MMC_TIMEOUT_SHORT;
  1291. else if (cmd->cmdidx == MMC_CMD_SWITCH && IS_SD(mmc))
  1292. timeout = 2560;
  1293. else if (cmd->cmdidx == MMC_CMD_ERASE)
  1294. timeout = MMC_TIMEOUT_ERASE;
  1295. else
  1296. timeout = MMC_TIMEOUT_LONG;
  1297. debug("%s(%s): cmd idx: %u, arg: 0x%x, resp type: 0x%x, timeout: %u\n",
  1298. __func__, name, cmd->cmdidx, cmd->cmdarg, cmd->resp_type,
  1299. timeout);
  1300. if (data)
  1301. debug(" data: addr: %p, flags: 0x%x, blocks: %u, blocksize: %u\n",
  1302. data->dest, data->flags, data->blocks, data->blocksize);
  1303. octeontx_mmc_switch_to(mmc);
  1304. /* Clear any interrupts */
  1305. write_csr(mmc, MIO_EMM_INT(), read_csr(mmc, MIO_EMM_INT()));
  1306. /*
  1307. * We need to override the default command types and response types
  1308. * when dealing with SD cards.
  1309. */
  1310. mods = octeontx_mmc_get_cr_mods(mmc, cmd, data);
  1311. /* Handle block read/write/stop operations */
  1312. switch (cmd->cmdidx) {
  1313. case MMC_CMD_GO_IDLE_STATE:
  1314. slot->tuned = false;
  1315. slot->hs200_tuned = false;
  1316. slot->hs400_tuned = false;
  1317. break;
  1318. case MMC_CMD_STOP_TRANSMISSION:
  1319. return 0;
  1320. case MMC_CMD_READ_MULTIPLE_BLOCK:
  1321. case MMC_CMD_READ_SINGLE_BLOCK:
  1322. pr_debug("%s(%s): Reading blocks\n", __func__, name);
  1323. blkcnt = octeontx_mmc_read_blocks(mmc, cmd, data, true);
  1324. return (blkcnt > 0) ? 0 : -1;
  1325. case MMC_CMD_WRITE_MULTIPLE_BLOCK:
  1326. case MMC_CMD_WRITE_SINGLE_BLOCK:
  1327. blkcnt = octeontx_mmc_write_blocks(mmc, cmd, data);
  1328. return (blkcnt > 0) ? 0 : -1;
  1329. case MMC_CMD_SELECT_CARD:
  1330. /* Set the RCA register (is it set automatically?) */
  1331. if (IS_SD(mmc)) {
  1332. union mio_emm_rca emm_rca;
  1333. emm_rca.u = 0;
  1334. emm_rca.s.card_rca = (cmd->cmdarg >> 16);
  1335. write_csr(mmc, MIO_EMM_RCA(), emm_rca.u);
  1336. debug("%s: Set SD relative address (RCA) to 0x%x\n",
  1337. __func__, emm_rca.s.card_rca);
  1338. }
  1339. break;
  1340. case MMC_CMD_SWITCH:
  1341. if (!data && !slot->is_acmd)
  1342. octeontx_mmc_track_switch(mmc, cmd->cmdarg);
  1343. break;
  1344. }
  1345. emm_cmd.u = 0;
  1346. emm_cmd.s.cmd_val = 1;
  1347. emm_cmd.s.bus_id = slot->bus_id;
  1348. emm_cmd.s.cmd_idx = cmd->cmdidx;
  1349. emm_cmd.s.arg = cmd->cmdarg;
  1350. emm_cmd.s.ctype_xor = mods.ctype_xor;
  1351. emm_cmd.s.rtype_xor = mods.rtype_xor;
  1352. if (data && data->blocks == 1 && data->blocksize != 512) {
  1353. emm_cmd.s.offset =
  1354. 64 - ((data->blocks * data->blocksize) / 8);
  1355. debug("%s: offset set to %u\n", __func__, emm_cmd.s.offset);
  1356. }
  1357. if (data && data->flags & MMC_DATA_WRITE) {
  1358. u8 *src = (u8 *)data->src;
  1359. if (!src) {
  1360. pr_err("%s(%s): Error: data source for cmd 0x%x is NULL!\n",
  1361. __func__, name, cmd->cmdidx);
  1362. return -1;
  1363. }
  1364. if (data->blocksize > 512) {
  1365. pr_err("%s(%s): Error: data for cmd 0x%x exceeds 512 bytes\n",
  1366. __func__, name, cmd->cmdidx);
  1367. return -1;
  1368. }
  1369. #ifdef DEBUG
  1370. debug("%s: Sending %d bytes data\n", __func__, data->blocksize);
  1371. print_buffer(0, src, 1, data->blocksize, 0);
  1372. #endif
  1373. emm_buf_idx.u = 0;
  1374. emm_buf_idx.s.inc = 1;
  1375. write_csr(mmc, MIO_EMM_BUF_IDX(), emm_buf_idx.u);
  1376. for (i = 0; i < (data->blocksize + 7) / 8; i++) {
  1377. memcpy(&emm_buf_dat.u, src, sizeof(emm_buf_dat.u));
  1378. write_csr(mmc, MIO_EMM_BUF_DAT(),
  1379. cpu_to_be64(emm_buf_dat.u));
  1380. src += sizeof(emm_buf_dat.u);
  1381. }
  1382. write_csr(mmc, MIO_EMM_BUF_IDX(), 0);
  1383. }
  1384. debug("%s(%s): Sending command %u (emm_cmd: 0x%llx)\n", __func__,
  1385. name, cmd->cmdidx, emm_cmd.u);
  1386. set_wdog(mmc, timeout * 1000);
  1387. write_csr(mmc, MIO_EMM_CMD(), emm_cmd.u);
  1388. /* Wait for command to finish or time out */
  1389. start = get_timer(0);
  1390. do {
  1391. rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
  1392. WATCHDOG_RESET();
  1393. } while (!rsp_sts.s.cmd_done && !rsp_sts.s.rsp_timeout &&
  1394. (get_timer(start) < timeout + 10));
  1395. octeontx_mmc_print_rsp_errors(mmc, rsp_sts);
  1396. if (rsp_sts.s.rsp_timeout || !rsp_sts.s.cmd_done) {
  1397. debug("%s(%s): Error: command %u(0x%x) timed out. rsp_sts: 0x%llx\n",
  1398. __func__, name, cmd->cmdidx, cmd->cmdarg, rsp_sts.u);
  1399. octeontx_mmc_print_registers(mmc);
  1400. return -ETIMEDOUT;
  1401. }
  1402. if (rsp_sts.s.rsp_crc_err) {
  1403. debug("%s(%s): RSP CRC error, rsp_sts: 0x%llx, cmdidx: %u, arg: 0x%08x\n",
  1404. __func__, name, rsp_sts.u, cmd->cmdidx, cmd->cmdarg);
  1405. octeontx_mmc_print_registers(mmc);
  1406. return -1;
  1407. }
  1408. if (slot->bus_id != rsp_sts.s.bus_id) {
  1409. pr_warn("%s(%s): bus id mismatch, got %d, expected %d for command 0x%x(0x%x)\n",
  1410. __func__, name,
  1411. rsp_sts.s.bus_id, slot->bus_id,
  1412. cmd->cmdidx, cmd->cmdarg);
  1413. goto error;
  1414. }
  1415. if (rsp_sts.s.rsp_bad_sts) {
  1416. rsp_lo.u = read_csr(mmc, MIO_EMM_RSP_LO());
  1417. debug("%s: Bad response for bus id %d, cmd id %d:\n"
  1418. " rsp_timeout: %d\n"
  1419. " rsp_bad_sts: %d\n"
  1420. " rsp_crc_err: %d\n",
  1421. __func__, slot->bus_id, cmd->cmdidx,
  1422. rsp_sts.s.rsp_timeout,
  1423. rsp_sts.s.rsp_bad_sts,
  1424. rsp_sts.s.rsp_crc_err);
  1425. if (rsp_sts.s.rsp_type == 1 && rsp_sts.s.rsp_bad_sts) {
  1426. debug(" Response status: 0x%llx\n",
  1427. (rsp_lo.u >> 8) & 0xffffffff);
  1428. #ifdef DEBUG
  1429. mmc_print_status((rsp_lo.u >> 8) & 0xffffffff);
  1430. #endif
  1431. }
  1432. goto error;
  1433. }
  1434. if (rsp_sts.s.cmd_idx != cmd->cmdidx) {
  1435. debug("%s(%s): Command response index %d does not match command index %d\n",
  1436. __func__, name, rsp_sts.s.cmd_idx, cmd->cmdidx);
  1437. octeontx_print_rsp_sts(mmc);
  1438. debug("%s: rsp_lo: 0x%llx\n", __func__,
  1439. read_csr(mmc, MIO_EMM_RSP_LO()));
  1440. goto error;
  1441. }
  1442. slot->is_acmd = (cmd->cmdidx == MMC_CMD_APP_CMD);
  1443. if (!cmd->resp_type & MMC_RSP_PRESENT)
  1444. debug(" Response type: 0x%x, no response expected\n",
  1445. cmd->resp_type);
  1446. /* Get the response if present */
  1447. if (rsp_sts.s.rsp_val && (cmd->resp_type & MMC_RSP_PRESENT)) {
  1448. union mio_emm_rsp_hi rsp_hi;
  1449. rsp_lo.u = read_csr(mmc, MIO_EMM_RSP_LO());
  1450. switch (rsp_sts.s.rsp_type) {
  1451. case 1:
  1452. case 3:
  1453. case 4:
  1454. case 5:
  1455. cmd->response[0] = (rsp_lo.u >> 8) & 0xffffffffull;
  1456. debug(" response: 0x%08x\n",
  1457. cmd->response[0]);
  1458. cmd->response[1] = 0;
  1459. cmd->response[2] = 0;
  1460. cmd->response[3] = 0;
  1461. break;
  1462. case 2:
  1463. cmd->response[3] = rsp_lo.u & 0xffffffff;
  1464. cmd->response[2] = (rsp_lo.u >> 32) & 0xffffffff;
  1465. rsp_hi.u = read_csr(mmc, MIO_EMM_RSP_HI());
  1466. cmd->response[1] = rsp_hi.u & 0xffffffff;
  1467. cmd->response[0] = (rsp_hi.u >> 32) & 0xffffffff;
  1468. debug(" response: 0x%08x 0x%08x 0x%08x 0x%08x\n",
  1469. cmd->response[0], cmd->response[1],
  1470. cmd->response[2], cmd->response[3]);
  1471. break;
  1472. default:
  1473. pr_err("%s(%s): Unknown response type 0x%x for command %d, arg: 0x%x, rsp_sts: 0x%llx\n",
  1474. __func__, name, rsp_sts.s.rsp_type, cmd->cmdidx,
  1475. cmd->cmdarg, rsp_sts.u);
  1476. return -1;
  1477. }
  1478. } else {
  1479. debug(" Response not expected\n");
  1480. }
  1481. if (data && data->flags & MMC_DATA_READ) {
  1482. u8 *dest = (u8 *)data->dest;
  1483. if (!dest) {
  1484. pr_err("%s(%s): Error, destination buffer NULL!\n",
  1485. __func__, mmc->dev->name);
  1486. goto error;
  1487. }
  1488. if (data->blocksize > 512) {
  1489. printf("%s(%s): Error: data size %u exceeds 512\n",
  1490. __func__, mmc->dev->name,
  1491. data->blocksize);
  1492. goto error;
  1493. }
  1494. emm_buf_idx.u = 0;
  1495. emm_buf_idx.s.inc = 1;
  1496. write_csr(mmc, MIO_EMM_BUF_IDX(), emm_buf_idx.u);
  1497. for (i = 0; i < (data->blocksize + 7) / 8; i++) {
  1498. emm_buf_dat.u = read_csr(mmc, MIO_EMM_BUF_DAT());
  1499. emm_buf_dat.u = be64_to_cpu(emm_buf_dat.u);
  1500. memcpy(dest, &emm_buf_dat.u, sizeof(emm_buf_dat.u));
  1501. dest += sizeof(emm_buf_dat.u);
  1502. }
  1503. write_csr(mmc, MIO_EMM_BUF_IDX(), 0);
  1504. #ifdef DEBUG
  1505. debug("%s: Received %d bytes data\n", __func__,
  1506. data->blocksize);
  1507. print_buffer(0, data->dest, 1, data->blocksize, 0);
  1508. #endif
  1509. }
  1510. return 0;
  1511. error:
  1512. #ifdef DEBUG
  1513. octeontx_mmc_print_registers(mmc);
  1514. #endif
  1515. return -1;
  1516. }
  1517. static int octeontx_mmc_dev_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
  1518. struct mmc_data *data)
  1519. {
  1520. return octeontx_mmc_send_cmd(dev_to_mmc(dev), cmd, data);
  1521. }
  1522. #ifdef MMC_SUPPORTS_TUNING
  1523. static int octeontx_mmc_test_cmd(struct mmc *mmc, u32 opcode, int *statp)
  1524. {
  1525. struct mmc_cmd cmd;
  1526. int err;
  1527. memset(&cmd, 0, sizeof(cmd));
  1528. debug("%s(%s, %u, %p)\n", __func__, mmc->dev->name, opcode, statp);
  1529. cmd.cmdidx = opcode;
  1530. cmd.resp_type = MMC_RSP_R1;
  1531. cmd.cmdarg = mmc->rca << 16;
  1532. err = octeontx_mmc_send_cmd(mmc, &cmd, NULL);
  1533. if (err)
  1534. debug("%s(%s, %u) returned %d\n", __func__,
  1535. mmc->dev->name, opcode, err);
  1536. if (statp)
  1537. *statp = cmd.response[0];
  1538. return err;
  1539. }
  1540. static int octeontx_mmc_test_get_ext_csd(struct mmc *mmc, u32 opcode,
  1541. int *statp)
  1542. {
  1543. struct mmc_cmd cmd;
  1544. struct mmc_data data;
  1545. int err;
  1546. u8 ext_csd[MMC_MAX_BLOCK_LEN];
  1547. debug("%s(%s, %u, %p)\n", __func__, mmc->dev->name, opcode, statp);
  1548. memset(&cmd, 0, sizeof(cmd));
  1549. cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
  1550. cmd.resp_type = MMC_RSP_R1;
  1551. cmd.cmdarg = 0;
  1552. data.dest = (char *)ext_csd;
  1553. data.blocks = 1;
  1554. data.blocksize = MMC_MAX_BLOCK_LEN;
  1555. data.flags = MMC_DATA_READ;
  1556. err = octeontx_mmc_send_cmd(mmc, &cmd, &data);
  1557. if (statp)
  1558. *statp = cmd.response[0];
  1559. return err;
  1560. }
  1561. /**
  1562. * Wrapper to set the MIO_EMM_TIMING register
  1563. *
  1564. * @param mmc pointer to mmc data structure
  1565. * @param emm_timing New emm_timing register value
  1566. *
  1567. * On some devices it is possible that changing the data out value can
  1568. * cause a glitch on an internal fifo. This works around this problem
  1569. * by performing a soft-reset immediately before setting the timing register.
  1570. *
  1571. * Note: this function should not be called from any function that
  1572. * performs DMA or block operations since not all registers are
  1573. * preserved.
  1574. */
  1575. static void octeontx_mmc_set_emm_timing(struct mmc *mmc,
  1576. union mio_emm_timing emm_timing)
  1577. {
  1578. union mio_emm_cfg emm_cfg;
  1579. struct octeontx_mmc_slot *slot = mmc->priv;
  1580. union mio_emm_debug emm_debug;
  1581. debug("%s(%s, 0x%llx) din: %u\n", __func__, mmc->dev->name,
  1582. emm_timing.u, emm_timing.s.data_in_tap);
  1583. udelay(1);
  1584. if (slot->host->tap_requires_noclk) {
  1585. /* Turn off the clock */
  1586. emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
  1587. emm_debug.s.emmc_clk_disable = 1;
  1588. write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
  1589. udelay(1);
  1590. emm_debug.s.rdsync_rst = 1;
  1591. write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
  1592. }
  1593. emm_cfg.u = read_csr(mmc, MIO_EMM_CFG());
  1594. emm_cfg.s.bus_ena = 1 << 3;
  1595. write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
  1596. udelay(1);
  1597. write_csr(mmc, MIO_EMM_TIMING(), emm_timing.u);
  1598. udelay(1);
  1599. if (slot->host->tap_requires_noclk) {
  1600. /* Turn on the clock */
  1601. emm_debug.s.rdsync_rst = 0;
  1602. write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
  1603. udelay(1);
  1604. emm_debug.s.emmc_clk_disable = 0;
  1605. write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
  1606. udelay(1);
  1607. }
  1608. emm_cfg.s.bus_ena = 1 << mmc_to_slot(mmc)->bus_id;
  1609. write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
  1610. }
  1611. static const u8 octeontx_hs400_tuning_block[512] = {
  1612. 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
  1613. 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
  1614. 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
  1615. 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
  1616. 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
  1617. 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
  1618. 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
  1619. 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
  1620. 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
  1621. 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
  1622. 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
  1623. 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
  1624. 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
  1625. 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
  1626. 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
  1627. 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
  1628. 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
  1629. 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
  1630. 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
  1631. 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
  1632. 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
  1633. 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
  1634. 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
  1635. 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
  1636. 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
  1637. 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
  1638. 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
  1639. 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
  1640. 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
  1641. 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
  1642. 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
  1643. 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
  1644. 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
  1645. 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc,
  1646. 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
  1647. 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
  1648. 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
  1649. 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
  1650. 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
  1651. 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
  1652. 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00,
  1653. 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc,
  1654. 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff,
  1655. 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
  1656. 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
  1657. 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
  1658. 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
  1659. 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
  1660. 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00,
  1661. 0x00, 0xff, 0x00, 0xff, 0x55, 0xaa, 0x55, 0xaa,
  1662. 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff,
  1663. 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff,
  1664. 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd,
  1665. 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb,
  1666. 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff,
  1667. 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff,
  1668. 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00,
  1669. 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
  1670. 0x01, 0xfe, 0x01, 0xfe, 0xcc, 0xcc, 0xcc, 0xff,
  1671. 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee,
  1672. 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd,
  1673. 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff,
  1674. 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff,
  1675. 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee,
  1676. };
  1677. /**
  1678. * Perform tuning in HS400 mode
  1679. *
  1680. * @param[in] mmc mmc data structure
  1681. *
  1682. * @ret 0 for success, otherwise error
  1683. */
  1684. static int octeontx_tune_hs400(struct mmc *mmc)
  1685. {
  1686. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  1687. struct mmc_cmd cmd;
  1688. struct mmc_data data;
  1689. union mio_emm_timing emm_timing;
  1690. u8 buffer[mmc->read_bl_len];
  1691. int tap_adj;
  1692. int err = -1;
  1693. int tap;
  1694. int run = 0;
  1695. int start_run = -1;
  1696. int best_run = 0;
  1697. int best_start = -1;
  1698. bool prev_ok = false;
  1699. char env_name[64];
  1700. char how[MAX_NO_OF_TAPS + 1] = "";
  1701. if (slot->hs400_tuning_block == -1)
  1702. return 0;
  1703. /* The eMMC standard disables all tuning support when operating in
  1704. * DDR modes like HS400. The problem with this is that there are
  1705. * many cases where the HS200 tuning does not work for HS400 mode.
  1706. * In order to perform this tuning, while in HS200 a block is written
  1707. * to a block specified in the device tree (marvell,hs400-tuning-block)
  1708. * which is used for tuning in this function by repeatedly reading
  1709. * this block and comparing the data and return code. This function
  1710. * chooses the data input tap in the middle of the longest run of
  1711. * successful read operations.
  1712. */
  1713. emm_timing = slot->hs200_taps;
  1714. debug("%s(%s): Start ci: %d, co: %d, di: %d, do: %d\n",
  1715. __func__, mmc->dev->name, emm_timing.s.cmd_in_tap,
  1716. emm_timing.s.cmd_out_tap, emm_timing.s.data_in_tap,
  1717. emm_timing.s.data_out_tap);
  1718. memset(buffer, 0xdb, sizeof(buffer));
  1719. snprintf(env_name, sizeof(env_name), "emmc%d_data_in_tap_hs400",
  1720. slot->bus_id);
  1721. tap = env_get_ulong(env_name, 10, -1L);
  1722. if (tap >= 0 && tap < MAX_NO_OF_TAPS) {
  1723. printf("Overriding data input tap for HS400 mode to %d\n", tap);
  1724. emm_timing.s.data_in_tap = tap;
  1725. octeontx_mmc_set_emm_timing(mmc, emm_timing);
  1726. return 0;
  1727. }
  1728. for (tap = 0; tap <= MAX_NO_OF_TAPS; tap++, prev_ok = !err) {
  1729. if (tap < MAX_NO_OF_TAPS) {
  1730. debug("%s: Testing data in tap %d\n", __func__, tap);
  1731. emm_timing.s.data_in_tap = tap;
  1732. octeontx_mmc_set_emm_timing(mmc, emm_timing);
  1733. cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
  1734. cmd.cmdarg = slot->hs400_tuning_block;
  1735. cmd.resp_type = MMC_RSP_R1;
  1736. data.dest = (void *)buffer;
  1737. data.blocks = 1;
  1738. data.blocksize = mmc->read_bl_len;
  1739. data.flags = MMC_DATA_READ;
  1740. err = !octeontx_mmc_read_blocks(mmc, &cmd, &data,
  1741. false);
  1742. if (err || memcmp(buffer, octeontx_hs400_tuning_block,
  1743. sizeof(buffer))) {
  1744. #ifdef DEBUG
  1745. if (!err) {
  1746. debug("%s: data mismatch. Read:\n",
  1747. __func__);
  1748. print_buffer(0, buffer, 1,
  1749. sizeof(buffer), 0);
  1750. debug("\nExpected:\n");
  1751. print_buffer(0,
  1752. octeontx_hs400_tuning_block, 1,
  1753. sizeof(octeontx_hs400_tuning_block),
  1754. 0);
  1755. } else {
  1756. debug("%s: Error %d reading block\n",
  1757. __func__, err);
  1758. }
  1759. #endif
  1760. err = -EINVAL;
  1761. } else {
  1762. debug("%s: tap %d good\n", __func__, tap);
  1763. }
  1764. how[tap] = "-+"[!err];
  1765. } else {
  1766. err = -EINVAL;
  1767. }
  1768. if (!err) {
  1769. if (!prev_ok)
  1770. start_run = tap;
  1771. } else if (prev_ok) {
  1772. run = tap - 1 - start_run;
  1773. if (start_run >= 0 && run > best_run) {
  1774. best_start = start_run;
  1775. best_run = run;
  1776. }
  1777. }
  1778. }
  1779. how[tap - 1] = '\0';
  1780. if (best_start < 0) {
  1781. printf("%s(%s): %lldMHz tuning failed for HS400\n",
  1782. __func__, mmc->dev->name, slot->clock / 1000000);
  1783. return -EINVAL;
  1784. }
  1785. tap = best_start + best_run / 2;
  1786. snprintf(env_name, sizeof(env_name), "emmc%d_data_in_tap_adj_hs400",
  1787. slot->bus_id);
  1788. tap_adj = env_get_ulong(env_name, 10, slot->hs400_tap_adj);
  1789. /*
  1790. * Keep it in range and if out of range force it back in with a small
  1791. * buffer.
  1792. */
  1793. if (best_run > 3) {
  1794. tap = tap + tap_adj;
  1795. if (tap >= best_start + best_run)
  1796. tap = best_start + best_run - 2;
  1797. if (tap <= best_start)
  1798. tap = best_start + 2;
  1799. }
  1800. how[tap] = '@';
  1801. debug("Tuning: %s\n", how);
  1802. debug("%s(%s): HS400 tap: best run start: %d, length: %d, tap: %d\n",
  1803. __func__, mmc->dev->name, best_start, best_run, tap);
  1804. slot->hs400_taps = slot->hs200_taps;
  1805. slot->hs400_taps.s.data_in_tap = tap;
  1806. slot->hs400_tuned = true;
  1807. if (env_get_yesno("emmc_export_hs400_taps") > 0) {
  1808. debug("%s(%s): Exporting HS400 taps\n",
  1809. __func__, mmc->dev->name);
  1810. env_set_ulong("emmc_timing_tap", slot->host->timing_taps);
  1811. snprintf(env_name, sizeof(env_name),
  1812. "emmc%d_hs400_data_in_tap_debug",
  1813. slot->bus_id);
  1814. env_set(env_name, how);
  1815. snprintf(env_name, sizeof(env_name),
  1816. "emmc%d_hs400_data_in_tap_val",
  1817. slot->bus_id);
  1818. env_set_ulong(env_name, tap);
  1819. snprintf(env_name, sizeof(env_name),
  1820. "emmc%d_hs400_data_in_tap_start",
  1821. slot->bus_id);
  1822. env_set_ulong(env_name, best_start);
  1823. snprintf(env_name, sizeof(env_name),
  1824. "emmc%d_hs400_data_in_tap_end",
  1825. slot->bus_id);
  1826. env_set_ulong(env_name, best_start + best_run);
  1827. snprintf(env_name, sizeof(env_name),
  1828. "emmc%d_hs400_cmd_in_tap",
  1829. slot->bus_id);
  1830. env_set_ulong(env_name, slot->hs400_taps.s.cmd_in_tap);
  1831. snprintf(env_name, sizeof(env_name),
  1832. "emmc%d_hs400_cmd_out_tap",
  1833. slot->bus_id);
  1834. env_set_ulong(env_name, slot->hs400_taps.s.cmd_out_tap);
  1835. snprintf(env_name, sizeof(env_name),
  1836. "emmc%d_hs400_cmd_out_delay",
  1837. slot->bus_id);
  1838. env_set_ulong(env_name, slot->cmd_out_hs400_delay);
  1839. snprintf(env_name, sizeof(env_name),
  1840. "emmc%d_hs400_data_out_tap",
  1841. slot->bus_id);
  1842. env_set_ulong(env_name, slot->hs400_taps.s.data_out_tap);
  1843. snprintf(env_name, sizeof(env_name),
  1844. "emmc%d_hs400_data_out_delay",
  1845. slot->bus_id);
  1846. env_set_ulong(env_name, slot->data_out_hs400_delay);
  1847. } else {
  1848. debug("%s(%s): HS400 environment export disabled\n",
  1849. __func__, mmc->dev->name);
  1850. }
  1851. octeontx_mmc_set_timing(mmc);
  1852. return 0;
  1853. }
  1854. struct adj {
  1855. const char *name;
  1856. u8 mask_shift;
  1857. int (*test)(struct mmc *mmc, u32 opcode, int *error);
  1858. u32 opcode;
  1859. bool ddr_only;
  1860. bool hs200_only;
  1861. bool not_hs200_only;
  1862. u8 num_runs;
  1863. };
  1864. struct adj adj[] = {
  1865. { "CMD_IN", 48, octeontx_mmc_test_cmd, MMC_CMD_SEND_STATUS,
  1866. false, false, false, 2, },
  1867. /* { "CMD_OUT", 32, octeontx_mmc_test_cmd, MMC_CMD_SEND_STATUS, },*/
  1868. { "DATA_IN(HS200)", 16, mmc_send_tuning,
  1869. MMC_CMD_SEND_TUNING_BLOCK_HS200, false, true, false, 2, },
  1870. { "DATA_IN", 16, octeontx_mmc_test_get_ext_csd, 0, false, false,
  1871. true, 2, },
  1872. /* { "DATA_OUT", 0, octeontx_mmc_test_cmd, 0, true, false},*/
  1873. { NULL, },
  1874. };
  1875. /**
  1876. * Perform tuning tests to find optimal timing
  1877. *
  1878. * @param mmc mmc device
  1879. * @param adj parameter to tune
  1880. * @param opcode command opcode to use
  1881. *
  1882. * @return 0 for success, -1 if tuning failed
  1883. */
  1884. static int octeontx_mmc_adjust_tuning(struct mmc *mmc, struct adj *adj,
  1885. u32 opcode)
  1886. {
  1887. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  1888. union mio_emm_timing timing;
  1889. union mio_emm_debug emm_debug;
  1890. int tap;
  1891. int err = -1;
  1892. int run = 0;
  1893. int count;
  1894. int start_run = -1;
  1895. int best_run = 0;
  1896. int best_start = -1;
  1897. bool prev_ok = false;
  1898. u64 tap_status = 0;
  1899. const int tap_adj = slot->hs200_tap_adj;
  1900. char how[MAX_NO_OF_TAPS + 1] = "";
  1901. bool is_hs200 = mmc->selected_mode == MMC_HS_200;
  1902. debug("%s(%s, %s, %d), hs200: %d\n", __func__, mmc->dev->name,
  1903. adj->name, opcode, is_hs200);
  1904. octeontx_mmc_set_emm_timing(mmc,
  1905. is_hs200 ? slot->hs200_taps : slot->taps);
  1906. #ifdef DEBUG
  1907. if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
  1908. printf("%s(%s): Before tuning %s, opcode: %d\n",
  1909. __func__, mmc->dev->name, adj->name, opcode);
  1910. octeontx_mmc_print_registers2(mmc, NULL);
  1911. }
  1912. #endif
  1913. /*
  1914. * The algorithm to find the optimal timing is to start
  1915. * at the end and work backwards and select the second
  1916. * value that passes. Each test is repeated twice.
  1917. */
  1918. for (tap = 0; tap <= MAX_NO_OF_TAPS; tap++, prev_ok = !err) {
  1919. if (tap < MAX_NO_OF_TAPS) {
  1920. if (slot->host->tap_requires_noclk) {
  1921. /* Turn off the clock */
  1922. emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
  1923. emm_debug.s.emmc_clk_disable = 1;
  1924. write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
  1925. udelay(1);
  1926. emm_debug.s.rdsync_rst = 1;
  1927. write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
  1928. udelay(1);
  1929. }
  1930. timing.u = read_csr(mmc, MIO_EMM_TIMING());
  1931. timing.u &= ~(0x3full << adj->mask_shift);
  1932. timing.u |= (u64)tap << adj->mask_shift;
  1933. write_csr(mmc, MIO_EMM_TIMING(), timing.u);
  1934. debug("%s(%s): Testing ci: %d, co: %d, di: %d, do: %d\n",
  1935. __func__, mmc->dev->name, timing.s.cmd_in_tap,
  1936. timing.s.cmd_out_tap, timing.s.data_in_tap,
  1937. timing.s.data_out_tap);
  1938. if (slot->host->tap_requires_noclk) {
  1939. /* Turn off the clock */
  1940. emm_debug.s.rdsync_rst = 0;
  1941. write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
  1942. udelay(1);
  1943. emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
  1944. emm_debug.s.emmc_clk_disable = 0;
  1945. write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
  1946. udelay(1);
  1947. }
  1948. for (count = 0; count < 2; count++) {
  1949. err = adj->test(mmc, opcode, NULL);
  1950. if (err) {
  1951. debug("%s(%s, %s): tap %d failed, count: %d, rsp_sts: 0x%llx, rsp_lo: 0x%llx\n",
  1952. __func__, mmc->dev->name,
  1953. adj->name, tap, count,
  1954. read_csr(mmc,
  1955. MIO_EMM_RSP_STS()),
  1956. read_csr(mmc,
  1957. MIO_EMM_RSP_LO()));
  1958. debug("%s(%s, %s): tap: %d, do: %d, di: %d, co: %d, ci: %d\n",
  1959. __func__, mmc->dev->name,
  1960. adj->name, tap,
  1961. timing.s.data_out_tap,
  1962. timing.s.data_in_tap,
  1963. timing.s.cmd_out_tap,
  1964. timing.s.cmd_in_tap);
  1965. break;
  1966. }
  1967. debug("%s(%s, %s): tap %d passed, count: %d, rsp_sts: 0x%llx, rsp_lo: 0x%llx\n",
  1968. __func__, mmc->dev->name, adj->name, tap,
  1969. count,
  1970. read_csr(mmc, MIO_EMM_RSP_STS()),
  1971. read_csr(mmc, MIO_EMM_RSP_LO()));
  1972. }
  1973. tap_status |= (u64)(!err) << tap;
  1974. how[tap] = "-+"[!err];
  1975. } else {
  1976. /*
  1977. * Putting the end+1 case in the loop simplifies
  1978. * logic, allowing 'prev_ok' to process a sweet
  1979. * spot in tuning which extends to the wall.
  1980. */
  1981. err = -EINVAL;
  1982. }
  1983. if (!err) {
  1984. /*
  1985. * If no CRC/etc errors in the response, but previous
  1986. * failed, note the start of a new run.
  1987. */
  1988. debug(" prev_ok: %d\n", prev_ok);
  1989. if (!prev_ok)
  1990. start_run = tap;
  1991. } else if (prev_ok) {
  1992. run = tap - 1 - start_run;
  1993. /* did we just exit a wider sweet spot? */
  1994. if (start_run >= 0 && run > best_run) {
  1995. best_start = start_run;
  1996. best_run = run;
  1997. }
  1998. }
  1999. }
  2000. how[tap - 1] = '\0';
  2001. if (best_start < 0) {
  2002. printf("%s(%s, %s): %lldMHz tuning %s failed\n", __func__,
  2003. mmc->dev->name, adj->name, slot->clock / 1000000,
  2004. adj->name);
  2005. return -EINVAL;
  2006. }
  2007. tap = best_start + best_run / 2;
  2008. debug(" tap %d is center, start: %d, run: %d\n", tap,
  2009. best_start, best_run);
  2010. if (is_hs200) {
  2011. slot->hs200_taps.u &= ~(0x3full << adj->mask_shift);
  2012. slot->hs200_taps.u |= (u64)tap << adj->mask_shift;
  2013. } else {
  2014. slot->taps.u &= ~(0x3full << adj->mask_shift);
  2015. slot->taps.u |= (u64)tap << adj->mask_shift;
  2016. }
  2017. if (best_start < 0) {
  2018. printf("%s(%s, %s): %lldMHz tuning %s failed\n", __func__,
  2019. mmc->dev->name, adj->name, slot->clock / 1000000,
  2020. adj->name);
  2021. return -EINVAL;
  2022. }
  2023. tap = best_start + best_run / 2;
  2024. if (is_hs200 && (tap + tap_adj >= 0) && (tap + tap_adj < 64) &&
  2025. tap_status & (1ULL << (tap + tap_adj))) {
  2026. debug("Adjusting tap from %d by %d to %d\n",
  2027. tap, tap_adj, tap + tap_adj);
  2028. tap += tap_adj;
  2029. }
  2030. how[tap] = '@';
  2031. debug("%s/%s %d/%d/%d %s\n", mmc->dev->name,
  2032. adj->name, best_start, tap, best_start + best_run, how);
  2033. if (is_hs200) {
  2034. slot->hs200_taps.u &= ~(0x3full << adj->mask_shift);
  2035. slot->hs200_taps.u |= (u64)tap << adj->mask_shift;
  2036. } else {
  2037. slot->taps.u &= ~(0x3full << adj->mask_shift);
  2038. slot->taps.u |= (u64)tap << adj->mask_shift;
  2039. }
  2040. #ifdef DEBUG
  2041. if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
  2042. debug("%s(%s, %s): After successful tuning\n",
  2043. __func__, mmc->dev->name, adj->name);
  2044. debug("%s(%s, %s): tap: %d, new do: %d, di: %d, co: %d, ci: %d\n",
  2045. __func__, mmc->dev->name, adj->name, tap,
  2046. slot->taps.s.data_out_tap,
  2047. slot->taps.s.data_in_tap,
  2048. slot->taps.s.cmd_out_tap,
  2049. slot->taps.s.cmd_in_tap);
  2050. debug("%s(%s, %s): tap: %d, new do HS200: %d, di: %d, co: %d, ci: %d\n",
  2051. __func__, mmc->dev->name, adj->name, tap,
  2052. slot->hs200_taps.s.data_out_tap,
  2053. slot->hs200_taps.s.data_in_tap,
  2054. slot->hs200_taps.s.cmd_out_tap,
  2055. slot->hs200_taps.s.cmd_in_tap);
  2056. }
  2057. #endif
  2058. octeontx_mmc_set_timing(mmc);
  2059. if (is_hs200 && env_get_yesno("emmc_export_hs200_taps")) {
  2060. char env_name[64];
  2061. env_set_ulong("emmc_timing_tap", slot->host->timing_taps);
  2062. switch (opcode) {
  2063. case MMC_CMD_SEND_TUNING_BLOCK:
  2064. snprintf(env_name, sizeof(env_name),
  2065. "emmc%d_hs200_data_in_tap_debug",
  2066. slot->bus_id);
  2067. env_set(env_name, how);
  2068. snprintf(env_name, sizeof(env_name),
  2069. "emmc%d_hs200_data_in_tap_val", slot->bus_id);
  2070. env_set_ulong(env_name, tap);
  2071. snprintf(env_name, sizeof(env_name),
  2072. "emmc%d_hs200_data_in_tap_start",
  2073. slot->bus_id);
  2074. env_set_ulong(env_name, best_start);
  2075. snprintf(env_name, sizeof(env_name),
  2076. "emmc%d_hs200_data_in_tap_end",
  2077. slot->bus_id);
  2078. env_set_ulong(env_name, best_start + best_run);
  2079. break;
  2080. case MMC_CMD_SEND_STATUS:
  2081. snprintf(env_name, sizeof(env_name),
  2082. "emmc%d_hs200_cmd_in_tap_debug",
  2083. slot->bus_id);
  2084. env_set(env_name, how);
  2085. snprintf(env_name, sizeof(env_name),
  2086. "emmc%d_hs200_cmd_in_tap_val", slot->bus_id);
  2087. env_set_ulong(env_name, tap);
  2088. snprintf(env_name, sizeof(env_name),
  2089. "emmc%d_hs200_cmd_in_tap_start",
  2090. slot->bus_id);
  2091. env_set_ulong(env_name, best_start);
  2092. snprintf(env_name, sizeof(env_name),
  2093. "emmc%d_hs200_cmd_in_tap_end",
  2094. slot->bus_id);
  2095. env_set_ulong(env_name, best_start + best_run);
  2096. break;
  2097. default:
  2098. snprintf(env_name, sizeof(env_name),
  2099. "emmc%d_hs200_data_out_tap", slot->bus_id);
  2100. env_set_ulong(env_name, slot->data_out_hs200_delay);
  2101. snprintf(env_name, sizeof(env_name),
  2102. "emmc%d_hs200_cmd_out_tap", slot->bus_id);
  2103. env_set_ulong(env_name, slot->cmd_out_hs200_delay);
  2104. break;
  2105. }
  2106. }
  2107. return 0;
  2108. }
  2109. static int octeontx_mmc_execute_tuning(struct udevice *dev, u32 opcode)
  2110. {
  2111. struct mmc *mmc = dev_to_mmc(dev);
  2112. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  2113. union mio_emm_timing emm_timing;
  2114. int err;
  2115. struct adj *a;
  2116. bool is_hs200;
  2117. char env_name[64];
  2118. pr_info("%s re-tuning, opcode 0x%x\n", dev->name, opcode);
  2119. if (slot->is_asim || slot->is_emul)
  2120. return 0;
  2121. is_hs200 = (mmc->selected_mode == MMC_HS_200);
  2122. if (is_hs200) {
  2123. slot->hs200_tuned = false;
  2124. slot->hs400_tuned = false;
  2125. } else {
  2126. slot->tuned = false;
  2127. }
  2128. octeontx_mmc_set_output_bus_timing(mmc);
  2129. octeontx_mmc_set_input_bus_timing(mmc);
  2130. emm_timing.u = read_csr(mmc, MIO_EMM_TIMING());
  2131. if (mmc->selected_mode == MMC_HS_200) {
  2132. slot->hs200_taps.s.cmd_out_tap = emm_timing.s.cmd_out_tap;
  2133. slot->hs200_taps.s.data_out_tap = emm_timing.s.data_out_tap;
  2134. } else {
  2135. slot->taps.s.cmd_out_tap = emm_timing.s.cmd_out_tap;
  2136. slot->taps.s.data_out_tap = emm_timing.s.data_out_tap;
  2137. }
  2138. octeontx_mmc_set_input_bus_timing(mmc);
  2139. octeontx_mmc_set_output_bus_timing(mmc);
  2140. for (a = adj; a->name; a++) {
  2141. ulong in_tap;
  2142. if (!strcmp(a->name, "CMD_IN")) {
  2143. snprintf(env_name, sizeof(env_name),
  2144. "emmc%d_cmd_in_tap", slot->bus_id);
  2145. in_tap = env_get_ulong(env_name, 10, (ulong)-1);
  2146. if (in_tap != (ulong)-1) {
  2147. if (mmc->selected_mode == MMC_HS_200 ||
  2148. a->hs200_only) {
  2149. slot->hs200_taps.s.cmd_in_tap = in_tap;
  2150. slot->hs400_taps.s.cmd_in_tap = in_tap;
  2151. } else {
  2152. slot->taps.s.cmd_in_tap = in_tap;
  2153. }
  2154. continue;
  2155. }
  2156. } else if (a->hs200_only &&
  2157. !strcmp(a->name, "DATA_IN(HS200)")) {
  2158. snprintf(env_name, sizeof(env_name),
  2159. "emmc%d_data_in_tap_hs200", slot->bus_id);
  2160. in_tap = env_get_ulong(env_name, 10, (ulong)-1);
  2161. if (in_tap != (ulong)-1) {
  2162. debug("%s(%s): Overriding HS200 data in tap to %d\n",
  2163. __func__, dev->name, (int)in_tap);
  2164. slot->hs200_taps.s.data_in_tap = in_tap;
  2165. continue;
  2166. }
  2167. } else if (!a->hs200_only && !strcmp(a->name, "DATA_IN")) {
  2168. snprintf(env_name, sizeof(env_name),
  2169. "emmc%d_data_in_tap", slot->bus_id);
  2170. in_tap = env_get_ulong(env_name, 10, (ulong)-1);
  2171. if (in_tap != (ulong)-1) {
  2172. debug("%s(%s): Overriding non-HS200 data in tap to %d\n",
  2173. __func__, dev->name, (int)in_tap);
  2174. slot->taps.s.data_in_tap = in_tap;
  2175. continue;
  2176. }
  2177. }
  2178. debug("%s(%s): Testing: %s, mode: %s, opcode: %u\n", __func__,
  2179. dev->name, a->name, mmc_mode_name(mmc->selected_mode),
  2180. opcode);
  2181. /* Skip DDR only test when not in DDR mode */
  2182. if (a->ddr_only && !mmc->ddr_mode) {
  2183. debug("%s(%s): Skipping %s due to non-DDR mode\n",
  2184. __func__, dev->name, a->name);
  2185. continue;
  2186. }
  2187. /* Skip hs200 tests in non-hs200 mode and
  2188. * non-hs200 tests in hs200 mode
  2189. */
  2190. if (is_hs200) {
  2191. if (a->not_hs200_only) {
  2192. debug("%s(%s): Skipping %s\n", __func__,
  2193. dev->name, a->name);
  2194. continue;
  2195. }
  2196. } else {
  2197. if (a->hs200_only) {
  2198. debug("%s(%s): Skipping %s\n", __func__,
  2199. dev->name, a->name);
  2200. continue;
  2201. }
  2202. }
  2203. err = octeontx_mmc_adjust_tuning(mmc, a, a->opcode ?
  2204. a->opcode : opcode);
  2205. if (err) {
  2206. pr_err("%s(%s, %u): tuning %s failed\n", __func__,
  2207. dev->name, opcode, a->name);
  2208. return err;
  2209. }
  2210. }
  2211. octeontx_mmc_set_timing(mmc);
  2212. if (is_hs200)
  2213. slot->hs200_tuned = true;
  2214. else
  2215. slot->tuned = true;
  2216. if (slot->hs400_tuning_block != -1) {
  2217. struct mmc_cmd cmd;
  2218. struct mmc_data data;
  2219. u8 buffer[mmc->read_bl_len];
  2220. cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
  2221. cmd.cmdarg = slot->hs400_tuning_block;
  2222. cmd.resp_type = MMC_RSP_R1;
  2223. data.dest = (void *)buffer;
  2224. data.blocks = 1;
  2225. data.blocksize = mmc->read_bl_len;
  2226. data.flags = MMC_DATA_READ;
  2227. err = octeontx_mmc_read_blocks(mmc, &cmd, &data, true) != 1;
  2228. if (err) {
  2229. printf("%s: Cannot read HS400 tuning block %u\n",
  2230. dev->name, slot->hs400_tuning_block);
  2231. return err;
  2232. }
  2233. if (memcmp(buffer, octeontx_hs400_tuning_block,
  2234. sizeof(buffer))) {
  2235. debug("%s(%s): Writing new HS400 tuning block to block %d\n",
  2236. __func__, dev->name, slot->hs400_tuning_block);
  2237. cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
  2238. data.src = (void *)octeontx_hs400_tuning_block;
  2239. data.flags = MMC_DATA_WRITE;
  2240. err = !octeontx_mmc_write_blocks(mmc, &cmd, &data);
  2241. if (err) {
  2242. printf("%s: Cannot write HS400 tuning block %u\n",
  2243. dev->name, slot->hs400_tuning_block);
  2244. return -EINVAL;
  2245. }
  2246. }
  2247. }
  2248. return 0;
  2249. }
  2250. #else /* MMC_SUPPORTS_TUNING */
  2251. static void octeontx_mmc_set_emm_timing(struct mmc *mmc,
  2252. union mio_emm_timing emm_timing)
  2253. {
  2254. }
  2255. #endif /* MMC_SUPPORTS_TUNING */
  2256. /**
  2257. * Calculate the clock period with rounding up
  2258. *
  2259. * @param mmc mmc device
  2260. * @return clock period in system clocks for clk_lo + clk_hi
  2261. */
  2262. static u32 octeontx_mmc_calc_clk_period(struct mmc *mmc)
  2263. {
  2264. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  2265. struct octeontx_mmc_host *host = slot->host;
  2266. if (mmc->clock)
  2267. return DIV_ROUND_UP(host->sys_freq, mmc->clock);
  2268. return 0;
  2269. }
  2270. static int octeontx_mmc_set_ios(struct udevice *dev)
  2271. {
  2272. struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
  2273. struct mmc *mmc = &slot->mmc;
  2274. struct octeontx_mmc_host *host = slot->host;
  2275. union mio_emm_switch emm_switch;
  2276. union mio_emm_modex mode;
  2277. uint clock;
  2278. int bus_width = 0;
  2279. int clk_period = 0;
  2280. int power_class = 10;
  2281. int err = 0;
  2282. bool is_hs200 = false;
  2283. bool is_hs400 = false;
  2284. debug("%s(%s): Entry\n", __func__, dev->name);
  2285. debug(" clock: %u, bus width: %u, mode: %u\n", mmc->clock,
  2286. mmc->bus_width, mmc->selected_mode);
  2287. debug(" host caps: 0x%x, card caps: 0x%x\n", mmc->host_caps,
  2288. mmc->card_caps);
  2289. octeontx_mmc_switch_to(mmc);
  2290. clock = mmc->clock;
  2291. if (!clock)
  2292. clock = mmc->cfg->f_min;
  2293. switch (mmc->bus_width) {
  2294. case 8:
  2295. bus_width = 2;
  2296. break;
  2297. case 4:
  2298. bus_width = 1;
  2299. break;
  2300. case 1:
  2301. bus_width = 0;
  2302. break;
  2303. default:
  2304. pr_warn("%s(%s): Invalid bus width %d, defaulting to 1\n",
  2305. __func__, dev->name, mmc->bus_width);
  2306. bus_width = 0;
  2307. }
  2308. /* DDR is available for 4/8 bit bus width */
  2309. if (mmc->ddr_mode && bus_width)
  2310. bus_width |= 4;
  2311. debug("%s: sys_freq: %llu\n", __func__, host->sys_freq);
  2312. clk_period = octeontx_mmc_calc_clk_period(mmc);
  2313. emm_switch.u = 0;
  2314. emm_switch.s.bus_width = bus_width;
  2315. emm_switch.s.power_class = power_class;
  2316. emm_switch.s.clk_hi = clk_period / 2;
  2317. emm_switch.s.clk_lo = clk_period / 2;
  2318. debug("%s: last mode: %d, mode: %d, last clock: %u, clock: %u, ddr: %d\n",
  2319. __func__, slot->last_mode, mmc->selected_mode,
  2320. slot->last_clock, mmc->clock, mmc->ddr_mode);
  2321. switch (mmc->selected_mode) {
  2322. case MMC_LEGACY:
  2323. break;
  2324. case MMC_HS:
  2325. case SD_HS:
  2326. case MMC_HS_52:
  2327. emm_switch.s.hs_timing = 1;
  2328. break;
  2329. case MMC_HS_200:
  2330. is_hs200 = true;
  2331. fallthrough;
  2332. case UHS_SDR12:
  2333. case UHS_SDR25:
  2334. case UHS_SDR50:
  2335. case UHS_SDR104:
  2336. #if !defined(CONFIG_ARCH_OCTEON)
  2337. emm_switch.s.hs200_timing = 1;
  2338. #endif
  2339. break;
  2340. case MMC_HS_400:
  2341. is_hs400 = true;
  2342. fallthrough;
  2343. case UHS_DDR50:
  2344. case MMC_DDR_52:
  2345. #if !defined(CONFIG_ARCH_OCTEON)
  2346. emm_switch.s.hs400_timing = 1;
  2347. #endif
  2348. break;
  2349. default:
  2350. pr_err("%s(%s): Unsupported mode 0x%x\n", __func__, dev->name,
  2351. mmc->selected_mode);
  2352. return -1;
  2353. }
  2354. emm_switch.s.bus_id = slot->bus_id;
  2355. if (!is_hs200 && !is_hs400 &&
  2356. (mmc->selected_mode != slot->last_mode ||
  2357. mmc->clock != slot->last_clock) &&
  2358. !mmc->ddr_mode) {
  2359. slot->tuned = false;
  2360. slot->last_mode = mmc->selected_mode;
  2361. slot->last_clock = mmc->clock;
  2362. }
  2363. if (CONFIG_IS_ENABLED(MMC_VERBOSE)) {
  2364. debug("%s(%s): Setting bus mode to %s\n", __func__, dev->name,
  2365. mmc_mode_name(mmc->selected_mode));
  2366. } else {
  2367. debug("%s(%s): Setting bus mode to 0x%x\n", __func__, dev->name,
  2368. mmc->selected_mode);
  2369. }
  2370. #if !defined(CONFIG_ARCH_OCTEON)
  2371. debug(" Trying switch 0x%llx w%d hs:%d hs200:%d hs400:%d\n",
  2372. emm_switch.u, emm_switch.s.bus_width, emm_switch.s.hs_timing,
  2373. emm_switch.s.hs200_timing, emm_switch.s.hs400_timing);
  2374. #endif
  2375. set_wdog(mmc, 1000);
  2376. do_switch(mmc, emm_switch);
  2377. mdelay(100);
  2378. mode.u = read_csr(mmc, MIO_EMM_MODEX(slot->bus_id));
  2379. #if !defined(CONFIG_ARCH_OCTEON)
  2380. debug("%s(%s): mode: 0x%llx w:%d, hs:%d, hs200:%d, hs400:%d\n",
  2381. __func__, dev->name, mode.u, mode.s.bus_width,
  2382. mode.s.hs_timing, mode.s.hs200_timing, mode.s.hs400_timing);
  2383. #endif
  2384. err = octeontx_mmc_configure_delay(mmc);
  2385. #ifdef MMC_SUPPORTS_TUNING
  2386. if (!err && mmc->selected_mode == MMC_HS_400 && !slot->hs400_tuned) {
  2387. debug("%s: Tuning HS400 mode\n", __func__);
  2388. err = octeontx_tune_hs400(mmc);
  2389. }
  2390. #endif
  2391. return err;
  2392. }
  2393. /**
  2394. * Gets the status of the card detect pin
  2395. */
  2396. static int octeontx_mmc_get_cd(struct udevice *dev)
  2397. {
  2398. struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
  2399. int val = 1;
  2400. if (dm_gpio_is_valid(&slot->cd_gpio)) {
  2401. val = dm_gpio_get_value(&slot->cd_gpio);
  2402. val ^= slot->cd_inverted;
  2403. }
  2404. debug("%s(%s): cd: %d\n", __func__, dev->name, val);
  2405. return val;
  2406. }
  2407. /**
  2408. * Gets the status of the write protect pin
  2409. */
  2410. static int octeontx_mmc_get_wp(struct udevice *dev)
  2411. {
  2412. struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
  2413. int val = 0;
  2414. if (dm_gpio_is_valid(&slot->wp_gpio)) {
  2415. val = dm_gpio_get_value(&slot->wp_gpio);
  2416. val ^= slot->wp_inverted;
  2417. }
  2418. debug("%s(%s): wp: %d\n", __func__, dev->name, val);
  2419. return val;
  2420. }
  2421. #if defined(CONFIG_ARCH_OCTEON)
  2422. static int octeontx_mmc_configure_delay(struct mmc *mmc)
  2423. {
  2424. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  2425. union mio_emm_sample emm_sample;
  2426. debug("%s(%s)\n", __func__, mmc->dev->name);
  2427. emm_sample.u = 0;
  2428. emm_sample.s.cmd_cnt = slot->cmd_cnt;
  2429. emm_sample.s.dat_cnt = slot->dat_cnt;
  2430. write_csr(mmc, MIO_EMM_SAMPLE(), emm_sample.u);
  2431. return 0;
  2432. }
  2433. static void octeontx_mmc_io_drive_setup(struct mmc *mmc)
  2434. {
  2435. }
  2436. #else
  2437. static void octeontx_mmc_set_timing(struct mmc *mmc)
  2438. {
  2439. union mio_emm_timing timing;
  2440. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  2441. switch (mmc->selected_mode) {
  2442. case MMC_HS_200:
  2443. timing = slot->hs200_taps;
  2444. break;
  2445. case MMC_HS_400:
  2446. timing = slot->hs400_tuned ?
  2447. slot->hs400_taps : slot->hs200_taps;
  2448. break;
  2449. default:
  2450. timing = slot->taps;
  2451. break;
  2452. }
  2453. debug("%s(%s):\n cmd_in_tap: %u\n cmd_out_tap: %u\n data_in_tap: %u\n data_out_tap: %u\n",
  2454. __func__, mmc->dev->name, timing.s.cmd_in_tap,
  2455. timing.s.cmd_out_tap, timing.s.data_in_tap,
  2456. timing.s.data_out_tap);
  2457. octeontx_mmc_set_emm_timing(mmc, timing);
  2458. }
  2459. static int octeontx_mmc_configure_delay(struct mmc *mmc)
  2460. {
  2461. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  2462. struct octeontx_mmc_host *host __maybe_unused = slot->host;
  2463. bool __maybe_unused is_hs200;
  2464. bool __maybe_unused is_hs400;
  2465. debug("%s(%s)\n", __func__, mmc->dev->name);
  2466. if (IS_ENABLED(CONFIG_ARCH_OCTEON) ||
  2467. IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
  2468. union mio_emm_sample emm_sample;
  2469. emm_sample.u = 0;
  2470. emm_sample.s.cmd_cnt = slot->cmd_cnt;
  2471. emm_sample.s.dat_cnt = slot->dat_cnt;
  2472. write_csr(mmc, MIO_EMM_SAMPLE(), emm_sample.u);
  2473. } else {
  2474. is_hs200 = (mmc->selected_mode == MMC_HS_200);
  2475. is_hs400 = (mmc->selected_mode == MMC_HS_400);
  2476. if ((is_hs200 && slot->hs200_tuned) ||
  2477. (is_hs400 && slot->hs400_tuned) ||
  2478. (!is_hs200 && !is_hs400 && slot->tuned)) {
  2479. octeontx_mmc_set_output_bus_timing(mmc);
  2480. } else {
  2481. int half = MAX_NO_OF_TAPS / 2;
  2482. int dout, cout;
  2483. switch (mmc->selected_mode) {
  2484. case MMC_LEGACY:
  2485. if (IS_SD(mmc)) {
  2486. cout = MMC_SD_LEGACY_DEFAULT_CMD_OUT_TAP;
  2487. dout = MMC_SD_LEGACY_DEFAULT_DATA_OUT_TAP;
  2488. } else {
  2489. cout = MMC_LEGACY_DEFAULT_CMD_OUT_TAP;
  2490. dout = MMC_LEGACY_DEFAULT_DATA_OUT_TAP;
  2491. }
  2492. break;
  2493. case MMC_HS:
  2494. cout = MMC_HS_CMD_OUT_TAP;
  2495. dout = MMC_HS_DATA_OUT_TAP;
  2496. break;
  2497. case SD_HS:
  2498. case UHS_SDR12:
  2499. case UHS_SDR25:
  2500. case UHS_SDR50:
  2501. cout = MMC_SD_HS_CMD_OUT_TAP;
  2502. dout = MMC_SD_HS_DATA_OUT_TAP;
  2503. break;
  2504. case UHS_SDR104:
  2505. case UHS_DDR50:
  2506. case MMC_HS_52:
  2507. case MMC_DDR_52:
  2508. cout = MMC_DEFAULT_CMD_OUT_TAP;
  2509. dout = MMC_DEFAULT_DATA_OUT_TAP;
  2510. break;
  2511. case MMC_HS_200:
  2512. cout = -1;
  2513. dout = -1;
  2514. if (host->timing_calibrated) {
  2515. cout = octeontx2_mmc_calc_delay(
  2516. mmc, slot->cmd_out_hs200_delay);
  2517. dout = octeontx2_mmc_calc_delay(
  2518. mmc,
  2519. slot->data_out_hs200_delay);
  2520. debug("%s(%s): Calibrated HS200/HS400 cmd out delay: %dps tap: %d, data out delay: %d, tap: %d\n",
  2521. __func__, mmc->dev->name,
  2522. slot->cmd_out_hs200_delay, cout,
  2523. slot->data_out_hs200_delay, dout);
  2524. } else {
  2525. cout = MMC_DEFAULT_HS200_CMD_OUT_TAP;
  2526. dout = MMC_DEFAULT_HS200_DATA_OUT_TAP;
  2527. }
  2528. is_hs200 = true;
  2529. break;
  2530. case MMC_HS_400:
  2531. cout = -1;
  2532. dout = -1;
  2533. if (host->timing_calibrated) {
  2534. if (slot->cmd_out_hs400_delay)
  2535. cout = octeontx2_mmc_calc_delay(
  2536. mmc,
  2537. slot->cmd_out_hs400_delay);
  2538. if (slot->data_out_hs400_delay)
  2539. dout = octeontx2_mmc_calc_delay(
  2540. mmc,
  2541. slot->data_out_hs400_delay);
  2542. debug("%s(%s): Calibrated HS200/HS400 cmd out delay: %dps tap: %d, data out delay: %d, tap: %d\n",
  2543. __func__, mmc->dev->name,
  2544. slot->cmd_out_hs400_delay, cout,
  2545. slot->data_out_hs400_delay, dout);
  2546. } else {
  2547. cout = MMC_DEFAULT_HS400_CMD_OUT_TAP;
  2548. dout = MMC_DEFAULT_HS400_DATA_OUT_TAP;
  2549. }
  2550. is_hs400 = true;
  2551. break;
  2552. default:
  2553. pr_err("%s(%s): Invalid mode %d\n", __func__,
  2554. mmc->dev->name, mmc->selected_mode);
  2555. return -1;
  2556. }
  2557. debug("%s(%s): Not tuned, hs200: %d, hs200 tuned: %d, hs400: %d, hs400 tuned: %d, tuned: %d\n",
  2558. __func__, mmc->dev->name, is_hs200,
  2559. slot->hs200_tuned,
  2560. is_hs400, slot->hs400_tuned, slot->tuned);
  2561. /* Set some defaults */
  2562. if (is_hs200) {
  2563. slot->hs200_taps.u = 0;
  2564. slot->hs200_taps.s.cmd_out_tap = cout;
  2565. slot->hs200_taps.s.data_out_tap = dout;
  2566. slot->hs200_taps.s.cmd_in_tap = half;
  2567. slot->hs200_taps.s.data_in_tap = half;
  2568. } else if (is_hs400) {
  2569. slot->hs400_taps.u = 0;
  2570. slot->hs400_taps.s.cmd_out_tap = cout;
  2571. slot->hs400_taps.s.data_out_tap = dout;
  2572. slot->hs400_taps.s.cmd_in_tap = half;
  2573. slot->hs400_taps.s.data_in_tap = half;
  2574. } else {
  2575. slot->taps.u = 0;
  2576. slot->taps.s.cmd_out_tap = cout;
  2577. slot->taps.s.data_out_tap = dout;
  2578. slot->taps.s.cmd_in_tap = half;
  2579. slot->taps.s.data_in_tap = half;
  2580. }
  2581. }
  2582. if (is_hs200)
  2583. debug("%s(%s): hs200 taps: ci: %u, co: %u, di: %u, do: %u\n",
  2584. __func__, mmc->dev->name,
  2585. slot->hs200_taps.s.cmd_in_tap,
  2586. slot->hs200_taps.s.cmd_out_tap,
  2587. slot->hs200_taps.s.data_in_tap,
  2588. slot->hs200_taps.s.data_out_tap);
  2589. else if (is_hs400)
  2590. debug("%s(%s): hs400 taps: ci: %u, co: %u, di: %u, do: %u\n",
  2591. __func__, mmc->dev->name,
  2592. slot->hs400_taps.s.cmd_in_tap,
  2593. slot->hs400_taps.s.cmd_out_tap,
  2594. slot->hs400_taps.s.data_in_tap,
  2595. slot->hs400_taps.s.data_out_tap);
  2596. else
  2597. debug("%s(%s): taps: ci: %u, co: %u, di: %u, do: %u\n",
  2598. __func__, mmc->dev->name, slot->taps.s.cmd_in_tap,
  2599. slot->taps.s.cmd_out_tap,
  2600. slot->taps.s.data_in_tap,
  2601. slot->taps.s.data_out_tap);
  2602. octeontx_mmc_set_timing(mmc);
  2603. debug("%s: Done\n", __func__);
  2604. }
  2605. return 0;
  2606. }
  2607. /**
  2608. * Set the IO drive strength and slew
  2609. *
  2610. * @param mmc mmc device
  2611. */
  2612. static void octeontx_mmc_io_drive_setup(struct mmc *mmc)
  2613. {
  2614. if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
  2615. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  2616. union mio_emm_io_ctl io_ctl;
  2617. if (slot->drive < 0 || slot->slew < 0)
  2618. return;
  2619. io_ctl.u = 0;
  2620. io_ctl.s.drive = slot->drive;
  2621. io_ctl.s.slew = slot->slew;
  2622. write_csr(mmc, MIO_EMM_IO_CTL(), io_ctl.u);
  2623. }
  2624. }
  2625. #endif
  2626. /**
  2627. * Sets the MMC watchdog timer in microseconds
  2628. *
  2629. * @param mmc mmc device
  2630. * @param us timeout in microseconds, 0 for maximum timeout
  2631. */
  2632. static void set_wdog(struct mmc *mmc, u64 us)
  2633. {
  2634. union mio_emm_wdog wdog;
  2635. u64 val;
  2636. val = (us * mmc->clock) / 1000000;
  2637. if (val >= (1 << 26) || !us) {
  2638. if (us)
  2639. pr_debug("%s: warning: timeout %llu exceeds max value %llu, truncating\n",
  2640. __func__, us,
  2641. (u64)(((1ULL << 26) - 1) * 1000000ULL) /
  2642. mmc->clock);
  2643. val = (1 << 26) - 1;
  2644. }
  2645. wdog.u = 0;
  2646. wdog.s.clk_cnt = val;
  2647. write_csr(mmc, MIO_EMM_WDOG(), wdog.u);
  2648. }
  2649. /**
  2650. * Print switch errors
  2651. *
  2652. * @param mmc mmc device
  2653. */
  2654. static void check_switch_errors(struct mmc *mmc)
  2655. {
  2656. union mio_emm_switch emm_switch;
  2657. emm_switch.u = read_csr(mmc, MIO_EMM_SWITCH());
  2658. if (emm_switch.s.switch_err0)
  2659. pr_err("%s: Switch power class error\n", mmc->cfg->name);
  2660. if (emm_switch.s.switch_err1)
  2661. pr_err("%s: Switch HS timing error\n", mmc->cfg->name);
  2662. if (emm_switch.s.switch_err2)
  2663. pr_err("%s: Switch bus width error\n", mmc->cfg->name);
  2664. }
  2665. static void do_switch(struct mmc *mmc, union mio_emm_switch emm_switch)
  2666. {
  2667. union mio_emm_rsp_sts rsp_sts;
  2668. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  2669. int bus_id = emm_switch.s.bus_id;
  2670. ulong start;
  2671. if (emm_switch.s.bus_id != 0) {
  2672. emm_switch.s.bus_id = 0;
  2673. write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
  2674. udelay(100);
  2675. emm_switch.s.bus_id = bus_id;
  2676. }
  2677. debug("%s(%s, 0x%llx)\n", __func__, mmc->dev->name, emm_switch.u);
  2678. write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
  2679. start = get_timer(0);
  2680. do {
  2681. rsp_sts.u = read_csr(mmc, MIO_EMM_RSP_STS());
  2682. if (!rsp_sts.s.switch_val)
  2683. break;
  2684. udelay(100);
  2685. } while (get_timer(start) < 10);
  2686. if (rsp_sts.s.switch_val) {
  2687. pr_warn("%s(%s): Warning: writing 0x%llx to emm_switch timed out, status: 0x%llx\n",
  2688. __func__, mmc->dev->name, emm_switch.u, rsp_sts.u);
  2689. }
  2690. slot->cached_switch = emm_switch;
  2691. check_switch_errors(mmc);
  2692. slot->cached_switch.u = emm_switch.u;
  2693. debug("%s: emm_switch: 0x%llx, rsp_lo: 0x%llx\n",
  2694. __func__, read_csr(mmc, MIO_EMM_SWITCH()),
  2695. read_csr(mmc, MIO_EMM_RSP_LO()));
  2696. }
  2697. /**
  2698. * Calibrates the delay based on the internal clock
  2699. *
  2700. * @param mmc Pointer to mmc data structure
  2701. *
  2702. * @return 0 for success or -ETIMEDOUT on error
  2703. *
  2704. * NOTE: On error a default value will be calculated.
  2705. */
  2706. #if defined(CONFIG_ARCH_OCTEON)
  2707. static int octeontx_mmc_set_input_bus_timing(struct mmc *mmc)
  2708. {
  2709. return 0;
  2710. }
  2711. static int octeontx_mmc_set_output_bus_timing(struct mmc *mmc)
  2712. {
  2713. return 0;
  2714. }
  2715. static int octeontx_mmc_calibrate_delay(struct mmc *mmc)
  2716. {
  2717. return 0;
  2718. }
  2719. #else
  2720. /**
  2721. * Given a delay in ps, return the tap delay count
  2722. *
  2723. * @param mmc mmc data structure
  2724. * @param delay delay in picoseconds
  2725. *
  2726. * @return Number of tap cycles or error if -1
  2727. */
  2728. static int octeontx2_mmc_calc_delay(struct mmc *mmc, int delay)
  2729. {
  2730. struct octeontx_mmc_host *host = mmc_to_host(mmc);
  2731. if (host->is_asim || host->is_emul)
  2732. return 63;
  2733. if (!host->timing_taps) {
  2734. pr_err("%s(%s): Error: host timing not calibrated\n",
  2735. __func__, mmc->dev->name);
  2736. return -1;
  2737. }
  2738. debug("%s(%s, %d) timing taps: %llu\n", __func__, mmc->dev->name,
  2739. delay, host->timing_taps);
  2740. return min_t(int, DIV_ROUND_UP(delay, host->timing_taps), 63);
  2741. }
  2742. static int octeontx_mmc_calibrate_delay(struct mmc *mmc)
  2743. {
  2744. union mio_emm_calb emm_calb;
  2745. union mio_emm_tap emm_tap;
  2746. union mio_emm_cfg emm_cfg;
  2747. union mio_emm_io_ctl emm_io_ctl;
  2748. union mio_emm_switch emm_switch;
  2749. union mio_emm_wdog emm_wdog;
  2750. union mio_emm_sts_mask emm_sts_mask;
  2751. union mio_emm_debug emm_debug;
  2752. union mio_emm_timing emm_timing;
  2753. struct octeontx_mmc_host *host = mmc_to_host(mmc);
  2754. ulong start;
  2755. u8 bus_id, bus_ena;
  2756. debug("%s: Calibrating delay\n", __func__);
  2757. if (host->is_asim || host->is_emul) {
  2758. debug(" No calibration for ASIM\n");
  2759. return 0;
  2760. }
  2761. emm_tap.u = 0;
  2762. if (host->calibrate_glitch) {
  2763. emm_tap.s.delay = MMC_DEFAULT_TAP_DELAY;
  2764. } else {
  2765. /* Save registers */
  2766. emm_cfg.u = read_csr(mmc, MIO_EMM_CFG());
  2767. emm_io_ctl.u = read_csr(mmc, MIO_EMM_IO_CTL());
  2768. emm_switch.u = read_csr(mmc, MIO_EMM_SWITCH());
  2769. emm_wdog.u = read_csr(mmc, MIO_EMM_WDOG());
  2770. emm_sts_mask.u = read_csr(mmc, MIO_EMM_STS_MASK());
  2771. emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
  2772. emm_timing.u = read_csr(mmc, MIO_EMM_TIMING());
  2773. bus_ena = emm_cfg.s.bus_ena;
  2774. bus_id = emm_switch.s.bus_id;
  2775. emm_cfg.s.bus_ena = 0;
  2776. write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
  2777. udelay(1);
  2778. emm_cfg.s.bus_ena = 1ULL << 3;
  2779. write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
  2780. mdelay(1);
  2781. emm_calb.u = 0;
  2782. write_csr(mmc, MIO_EMM_CALB(), emm_calb.u);
  2783. emm_calb.s.start = 1;
  2784. write_csr(mmc, MIO_EMM_CALB(), emm_calb.u);
  2785. start = get_timer(0);
  2786. /* This should only take 3 microseconds */
  2787. do {
  2788. udelay(5);
  2789. emm_tap.u = read_csr(mmc, MIO_EMM_TAP());
  2790. } while (!emm_tap.s.delay && get_timer(start) < 10);
  2791. emm_calb.s.start = 0;
  2792. write_csr(mmc, MIO_EMM_CALB(), emm_calb.u);
  2793. emm_cfg.s.bus_ena = 0;
  2794. write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
  2795. udelay(1);
  2796. /* Restore registers */
  2797. emm_cfg.s.bus_ena = bus_ena;
  2798. write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
  2799. if (host->tap_requires_noclk) {
  2800. /* Turn off the clock */
  2801. emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
  2802. emm_debug.s.emmc_clk_disable = 1;
  2803. write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
  2804. udelay(1);
  2805. emm_debug.s.rdsync_rst = 1;
  2806. write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
  2807. udelay(1);
  2808. }
  2809. write_csr(mmc, MIO_EMM_TIMING(), emm_timing.u);
  2810. if (host->tap_requires_noclk) {
  2811. /* Turn the clock back on */
  2812. udelay(1);
  2813. emm_debug.s.rdsync_rst = 0;
  2814. write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
  2815. udelay(1);
  2816. emm_debug.s.emmc_clk_disable = 0;
  2817. write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
  2818. }
  2819. udelay(1);
  2820. write_csr(mmc, MIO_EMM_IO_CTL(), emm_io_ctl.u);
  2821. bus_id = emm_switch.s.bus_id;
  2822. emm_switch.s.bus_id = 0;
  2823. write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
  2824. emm_switch.s.bus_id = bus_id;
  2825. write_csr(mmc, MIO_EMM_SWITCH(), emm_switch.u);
  2826. write_csr(mmc, MIO_EMM_WDOG(), emm_wdog.u);
  2827. write_csr(mmc, MIO_EMM_STS_MASK(), emm_sts_mask.u);
  2828. write_csr(mmc, MIO_EMM_RCA(), mmc->rca);
  2829. write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
  2830. if (!emm_tap.s.delay) {
  2831. pr_err("%s: Error: delay calibration failed, timed out.\n",
  2832. __func__);
  2833. /* Set to default value if timed out */
  2834. emm_tap.s.delay = MMC_DEFAULT_TAP_DELAY;
  2835. return -ETIMEDOUT;
  2836. }
  2837. }
  2838. /* Round up */
  2839. host->timing_taps = (10 * 1000 * emm_tap.s.delay) / TOTAL_NO_OF_TAPS;
  2840. debug("%s(%s): timing taps: %llu, delay: %u\n",
  2841. __func__, mmc->dev->name, host->timing_taps, emm_tap.s.delay);
  2842. host->timing_calibrated = true;
  2843. return 0;
  2844. }
  2845. static int octeontx_mmc_set_input_bus_timing(struct mmc *mmc)
  2846. {
  2847. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  2848. if (IS_ENABLED(CONFIG_ARCH_OCTEONTX)) {
  2849. union mio_emm_sample sample;
  2850. sample.u = 0;
  2851. sample.s.cmd_cnt = slot->cmd_clk_skew;
  2852. sample.s.dat_cnt = slot->dat_clk_skew;
  2853. write_csr(mmc, MIO_EMM_SAMPLE(), sample.u);
  2854. } else {
  2855. union mio_emm_timing timing;
  2856. timing.u = read_csr(mmc, MIO_EMM_TIMING());
  2857. if (mmc->selected_mode == MMC_HS_200) {
  2858. if (slot->hs200_tuned) {
  2859. timing.s.cmd_in_tap =
  2860. slot->hs200_taps.s.cmd_in_tap;
  2861. timing.s.data_in_tap =
  2862. slot->hs200_taps.s.data_in_tap;
  2863. } else {
  2864. pr_warn("%s(%s): Warning: hs200 timing not tuned\n",
  2865. __func__, mmc->dev->name);
  2866. timing.s.cmd_in_tap =
  2867. MMC_DEFAULT_HS200_CMD_IN_TAP;
  2868. timing.s.data_in_tap =
  2869. MMC_DEFAULT_HS200_DATA_IN_TAP;
  2870. }
  2871. } else if (mmc->selected_mode == MMC_HS_400) {
  2872. if (slot->hs400_tuned) {
  2873. timing.s.cmd_in_tap =
  2874. slot->hs400_taps.s.cmd_in_tap;
  2875. timing.s.data_in_tap =
  2876. slot->hs400_taps.s.data_in_tap;
  2877. } else if (slot->hs200_tuned) {
  2878. timing.s.cmd_in_tap =
  2879. slot->hs200_taps.s.cmd_in_tap;
  2880. timing.s.data_in_tap =
  2881. slot->hs200_taps.s.data_in_tap;
  2882. } else {
  2883. pr_warn("%s(%s): Warning: hs400 timing not tuned\n",
  2884. __func__, mmc->dev->name);
  2885. timing.s.cmd_in_tap =
  2886. MMC_DEFAULT_HS200_CMD_IN_TAP;
  2887. timing.s.data_in_tap =
  2888. MMC_DEFAULT_HS200_DATA_IN_TAP;
  2889. }
  2890. } else if (slot->tuned) {
  2891. timing.s.cmd_in_tap = slot->taps.s.cmd_in_tap;
  2892. timing.s.data_in_tap = slot->taps.s.data_in_tap;
  2893. } else {
  2894. timing.s.cmd_in_tap = MMC_DEFAULT_CMD_IN_TAP;
  2895. timing.s.data_in_tap = MMC_DEFAULT_DATA_IN_TAP;
  2896. }
  2897. octeontx_mmc_set_emm_timing(mmc, timing);
  2898. }
  2899. return 0;
  2900. }
  2901. /**
  2902. * Sets the default bus timing for the current mode.
  2903. *
  2904. * @param mmc mmc data structure
  2905. *
  2906. * @return 0 for success, error otherwise
  2907. */
  2908. static int octeontx_mmc_set_output_bus_timing(struct mmc *mmc)
  2909. {
  2910. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  2911. union mio_emm_timing timing;
  2912. int cout_bdelay, dout_bdelay;
  2913. unsigned int cout_delay, dout_delay;
  2914. char env_name[32];
  2915. if (IS_ENABLED(CONFIG_ARCH_OCTEONTX))
  2916. return 0;
  2917. debug("%s(%s)\n", __func__, mmc->dev->name);
  2918. if (slot->is_asim || slot->is_emul)
  2919. return 0;
  2920. octeontx_mmc_calibrate_delay(mmc);
  2921. if (mmc->clock < 26000000) {
  2922. cout_delay = 5000;
  2923. dout_delay = 5000;
  2924. } else if (mmc->clock <= 52000000) {
  2925. cout_delay = 2500;
  2926. dout_delay = 2500;
  2927. } else if (!mmc_is_mode_ddr(mmc->selected_mode)) {
  2928. cout_delay = slot->cmd_out_hs200_delay;
  2929. dout_delay = slot->data_out_hs200_delay;
  2930. } else {
  2931. cout_delay = slot->cmd_out_hs400_delay;
  2932. dout_delay = slot->data_out_hs400_delay;
  2933. }
  2934. snprintf(env_name, sizeof(env_name), "mmc%d_hs200_dout_delay_ps",
  2935. slot->bus_id);
  2936. dout_delay = env_get_ulong(env_name, 10, dout_delay);
  2937. debug("%s: dout_delay: %u\n", __func__, dout_delay);
  2938. cout_bdelay = octeontx2_mmc_calc_delay(mmc, cout_delay);
  2939. dout_bdelay = octeontx2_mmc_calc_delay(mmc, dout_delay);
  2940. debug("%s: cmd output delay: %u, data output delay: %u, cmd bdelay: %d, data bdelay: %d, clock: %d\n",
  2941. __func__, cout_delay, dout_delay, cout_bdelay, dout_bdelay,
  2942. mmc->clock);
  2943. if (cout_bdelay < 0 || dout_bdelay < 0) {
  2944. pr_err("%s: Error: could not calculate command and/or data clock skew\n",
  2945. __func__);
  2946. return -1;
  2947. }
  2948. timing.u = read_csr(mmc, MIO_EMM_TIMING());
  2949. timing.s.cmd_out_tap = cout_bdelay;
  2950. timing.s.data_out_tap = dout_bdelay;
  2951. if (mmc->selected_mode == MMC_HS_200) {
  2952. slot->hs200_taps.s.cmd_out_tap = cout_bdelay;
  2953. slot->hs200_taps.s.data_out_tap = dout_bdelay;
  2954. } else if (mmc->selected_mode == MMC_HS_400) {
  2955. slot->hs400_taps.s.cmd_out_tap = cout_bdelay;
  2956. slot->hs400_taps.s.data_out_tap = dout_bdelay;
  2957. } else {
  2958. slot->taps.s.cmd_out_tap = cout_bdelay;
  2959. slot->taps.s.data_out_tap = dout_bdelay;
  2960. }
  2961. octeontx_mmc_set_emm_timing(mmc, timing);
  2962. debug("%s(%s): bdelay: %d/%d, clock: %d, ddr: %s, timing taps: %llu, do: %d, di: %d, co: %d, ci: %d\n",
  2963. __func__, mmc->dev->name, cout_bdelay, dout_bdelay, mmc->clock,
  2964. mmc->ddr_mode ? "yes" : "no",
  2965. mmc_to_host(mmc)->timing_taps,
  2966. timing.s.data_out_tap,
  2967. timing.s.data_in_tap,
  2968. timing.s.cmd_out_tap,
  2969. timing.s.cmd_in_tap);
  2970. return 0;
  2971. }
  2972. #endif
  2973. static void octeontx_mmc_set_clock(struct mmc *mmc)
  2974. {
  2975. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  2976. uint clock;
  2977. clock = min(mmc->cfg->f_max, (uint)slot->clock);
  2978. clock = max(mmc->cfg->f_min, clock);
  2979. debug("%s(%s): f_min: %u, f_max: %u, clock: %u\n", __func__,
  2980. mmc->dev->name, mmc->cfg->f_min, mmc->cfg->f_max, clock);
  2981. slot->clock = clock;
  2982. mmc->clock = clock;
  2983. }
  2984. /**
  2985. * This switches I/O power as needed when switching between slots.
  2986. *
  2987. * @param mmc mmc data structure
  2988. */
  2989. static void octeontx_mmc_switch_io(struct mmc *mmc)
  2990. {
  2991. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  2992. struct octeontx_mmc_host *host = slot->host;
  2993. struct mmc *last_mmc = host->last_mmc;
  2994. static struct udevice *last_reg;
  2995. union mio_emm_cfg emm_cfg;
  2996. int bus;
  2997. static bool initialized;
  2998. /* First time? */
  2999. if (!initialized || mmc != host->last_mmc) {
  3000. struct mmc *ommc;
  3001. /* Switch to bus 3 which is unused */
  3002. emm_cfg.u = read_csr(mmc, MIO_EMM_CFG());
  3003. emm_cfg.s.bus_ena = 1 << 3;
  3004. write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
  3005. /* Turn off all other I/O interfaces with first initialization
  3006. * if at least one supply was found.
  3007. */
  3008. for (bus = 0; bus <= OCTEONTX_MAX_MMC_SLOT; bus++) {
  3009. ommc = &host->slots[bus].mmc;
  3010. /* Handle self case later */
  3011. if (ommc == mmc || !ommc->vqmmc_supply)
  3012. continue;
  3013. /* Skip if we're not switching regulators */
  3014. if (last_reg == mmc->vqmmc_supply)
  3015. continue;
  3016. /* Turn off other regulators */
  3017. if (ommc->vqmmc_supply != mmc->vqmmc_supply)
  3018. regulator_set_enable(ommc->vqmmc_supply, false);
  3019. }
  3020. /* Turn ourself on */
  3021. if (mmc->vqmmc_supply && last_reg != mmc->vqmmc_supply)
  3022. regulator_set_enable(mmc->vqmmc_supply, true);
  3023. mdelay(1); /* Settle time */
  3024. /* Switch to new bus */
  3025. emm_cfg.s.bus_ena = 1 << slot->bus_id;
  3026. write_csr(mmc, MIO_EMM_CFG(), emm_cfg.u);
  3027. last_reg = mmc->vqmmc_supply;
  3028. initialized = true;
  3029. return;
  3030. }
  3031. /* No change in device */
  3032. if (last_mmc == mmc)
  3033. return;
  3034. if (!last_mmc) {
  3035. pr_warn("%s(%s): No previous slot detected in IO slot switch!\n",
  3036. __func__, mmc->dev->name);
  3037. return;
  3038. }
  3039. debug("%s(%s): last: %s, supply: %p\n", __func__, mmc->dev->name,
  3040. last_mmc->dev->name, mmc->vqmmc_supply);
  3041. /* The supply is the same so we do nothing */
  3042. if (last_mmc->vqmmc_supply == mmc->vqmmc_supply)
  3043. return;
  3044. /* Turn off the old slot I/O supply */
  3045. if (last_mmc->vqmmc_supply) {
  3046. debug("%s(%s): Turning off IO to %s, supply: %s\n",
  3047. __func__, mmc->dev->name, last_mmc->dev->name,
  3048. last_mmc->vqmmc_supply->name);
  3049. regulator_set_enable(last_mmc->vqmmc_supply, false);
  3050. }
  3051. /* Turn on the new slot I/O supply */
  3052. if (mmc->vqmmc_supply) {
  3053. debug("%s(%s): Turning on IO to slot %d, supply: %s\n",
  3054. __func__, mmc->dev->name, slot->bus_id,
  3055. mmc->vqmmc_supply->name);
  3056. regulator_set_enable(mmc->vqmmc_supply, true);
  3057. }
  3058. /* Allow power to settle */
  3059. mdelay(1);
  3060. }
  3061. /**
  3062. * Called to switch between mmc devices
  3063. *
  3064. * @param mmc new mmc device
  3065. */
  3066. static void octeontx_mmc_switch_to(struct mmc *mmc)
  3067. {
  3068. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  3069. struct octeontx_mmc_slot *old_slot;
  3070. struct octeontx_mmc_host *host = slot->host;
  3071. union mio_emm_switch emm_switch;
  3072. union mio_emm_sts_mask emm_sts_mask;
  3073. union mio_emm_rca emm_rca;
  3074. if (slot->bus_id == host->last_slotid)
  3075. return;
  3076. debug("%s(%s) switching from slot %d to slot %d\n", __func__,
  3077. mmc->dev->name, host->last_slotid, slot->bus_id);
  3078. octeontx_mmc_switch_io(mmc);
  3079. if (host->last_slotid >= 0 && slot->valid) {
  3080. old_slot = &host->slots[host->last_slotid];
  3081. old_slot->cached_switch.u = read_csr(mmc, MIO_EMM_SWITCH());
  3082. old_slot->cached_rca.u = read_csr(mmc, MIO_EMM_RCA());
  3083. }
  3084. if (mmc->rca)
  3085. write_csr(mmc, MIO_EMM_RCA(), mmc->rca);
  3086. emm_switch = slot->cached_switch;
  3087. do_switch(mmc, emm_switch);
  3088. emm_rca.u = 0;
  3089. emm_rca.s.card_rca = mmc->rca;
  3090. write_csr(mmc, MIO_EMM_RCA(), emm_rca.u);
  3091. mdelay(100);
  3092. set_wdog(mmc, 100000);
  3093. if (octeontx_mmc_set_output_bus_timing(mmc) ||
  3094. octeontx_mmc_set_input_bus_timing(mmc))
  3095. pr_err("%s(%s): Error setting bus timing\n", __func__,
  3096. mmc->dev->name);
  3097. octeontx_mmc_io_drive_setup(mmc);
  3098. emm_sts_mask.u = 0;
  3099. emm_sts_mask.s.sts_msk = 1 << 7 | 1 << 22 | 1 << 23 | 1 << 19;
  3100. write_csr(mmc, MIO_EMM_STS_MASK(), emm_sts_mask.u);
  3101. host->last_slotid = slot->bus_id;
  3102. host->last_mmc = mmc;
  3103. mdelay(10);
  3104. }
  3105. /**
  3106. * Perform initial timing configuration
  3107. *
  3108. * @param mmc mmc device
  3109. *
  3110. * @return 0 for success
  3111. *
  3112. * NOTE: This will need to be updated when new silicon comes out
  3113. */
  3114. static int octeontx_mmc_init_timing(struct mmc *mmc)
  3115. {
  3116. union mio_emm_timing timing;
  3117. if (mmc_to_slot(mmc)->is_asim || mmc_to_slot(mmc)->is_emul)
  3118. return 0;
  3119. debug("%s(%s)\n", __func__, mmc->dev->name);
  3120. timing.u = 0;
  3121. timing.s.cmd_out_tap = MMC_DEFAULT_CMD_OUT_TAP;
  3122. timing.s.data_out_tap = MMC_DEFAULT_DATA_OUT_TAP;
  3123. timing.s.cmd_in_tap = MMC_DEFAULT_CMD_IN_TAP;
  3124. timing.s.data_in_tap = MMC_DEFAULT_DATA_IN_TAP;
  3125. octeontx_mmc_set_emm_timing(mmc, timing);
  3126. return 0;
  3127. }
  3128. /**
  3129. * Perform low-level initialization
  3130. *
  3131. * @param mmc mmc device
  3132. *
  3133. * @return 0 for success, error otherwise
  3134. */
  3135. static int octeontx_mmc_init_lowlevel(struct mmc *mmc)
  3136. {
  3137. struct octeontx_mmc_slot *slot = mmc_to_slot(mmc);
  3138. struct octeontx_mmc_host *host = slot->host;
  3139. union mio_emm_switch emm_switch;
  3140. u32 clk_period;
  3141. debug("%s(%s): lowlevel init for slot %d\n", __func__,
  3142. mmc->dev->name, slot->bus_id);
  3143. host->emm_cfg.s.bus_ena &= ~(1 << slot->bus_id);
  3144. write_csr(mmc, MIO_EMM_CFG(), host->emm_cfg.u);
  3145. udelay(100);
  3146. host->emm_cfg.s.bus_ena |= 1 << slot->bus_id;
  3147. write_csr(mmc, MIO_EMM_CFG(), host->emm_cfg.u);
  3148. udelay(10);
  3149. slot->clock = mmc->cfg->f_min;
  3150. octeontx_mmc_set_clock(&slot->mmc);
  3151. if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
  3152. if (host->cond_clock_glitch) {
  3153. union mio_emm_debug emm_debug;
  3154. emm_debug.u = read_csr(mmc, MIO_EMM_DEBUG());
  3155. emm_debug.s.clk_on = 1;
  3156. write_csr(mmc, MIO_EMM_DEBUG(), emm_debug.u);
  3157. }
  3158. octeontx_mmc_calibrate_delay(&slot->mmc);
  3159. }
  3160. clk_period = octeontx_mmc_calc_clk_period(mmc);
  3161. emm_switch.u = 0;
  3162. emm_switch.s.power_class = 10;
  3163. emm_switch.s.clk_lo = clk_period / 2;
  3164. emm_switch.s.clk_hi = clk_period / 2;
  3165. emm_switch.s.bus_id = slot->bus_id;
  3166. debug("%s: Performing switch\n", __func__);
  3167. do_switch(mmc, emm_switch);
  3168. slot->cached_switch.u = emm_switch.u;
  3169. if (!IS_ENABLED(CONFIG_ARCH_OCTEONTX))
  3170. octeontx_mmc_init_timing(mmc);
  3171. set_wdog(mmc, 1000000); /* Set to 1 second */
  3172. write_csr(mmc, MIO_EMM_STS_MASK(), 0xe4390080ull);
  3173. write_csr(mmc, MIO_EMM_RCA(), 1);
  3174. mdelay(10);
  3175. debug("%s: done\n", __func__);
  3176. return 0;
  3177. }
  3178. /**
  3179. * Translates a voltage number to bits in MMC register
  3180. *
  3181. * @param voltage voltage in microvolts
  3182. *
  3183. * @return MMC register value for voltage
  3184. */
  3185. static u32 xlate_voltage(u32 voltage)
  3186. {
  3187. u32 volt = 0;
  3188. /* Convert to millivolts. Only necessary on ARM Octeon TX/TX2 */
  3189. if (!IS_ENABLED(CONFIG_ARCH_OCTEON))
  3190. voltage /= 1000;
  3191. if (voltage >= 1650 && voltage <= 1950)
  3192. volt |= MMC_VDD_165_195;
  3193. if (voltage >= 2000 && voltage <= 2100)
  3194. volt |= MMC_VDD_20_21;
  3195. if (voltage >= 2100 && voltage <= 2200)
  3196. volt |= MMC_VDD_21_22;
  3197. if (voltage >= 2200 && voltage <= 2300)
  3198. volt |= MMC_VDD_22_23;
  3199. if (voltage >= 2300 && voltage <= 2400)
  3200. volt |= MMC_VDD_23_24;
  3201. if (voltage >= 2400 && voltage <= 2500)
  3202. volt |= MMC_VDD_24_25;
  3203. if (voltage >= 2500 && voltage <= 2600)
  3204. volt |= MMC_VDD_25_26;
  3205. if (voltage >= 2600 && voltage <= 2700)
  3206. volt |= MMC_VDD_26_27;
  3207. if (voltage >= 2700 && voltage <= 2800)
  3208. volt |= MMC_VDD_27_28;
  3209. if (voltage >= 2800 && voltage <= 2900)
  3210. volt |= MMC_VDD_28_29;
  3211. if (voltage >= 2900 && voltage <= 3000)
  3212. volt |= MMC_VDD_29_30;
  3213. if (voltage >= 3000 && voltage <= 3100)
  3214. volt |= MMC_VDD_30_31;
  3215. if (voltage >= 3100 && voltage <= 3200)
  3216. volt |= MMC_VDD_31_32;
  3217. if (voltage >= 3200 && voltage <= 3300)
  3218. volt |= MMC_VDD_32_33;
  3219. if (voltage >= 3300 && voltage <= 3400)
  3220. volt |= MMC_VDD_33_34;
  3221. if (voltage >= 3400 && voltage <= 3500)
  3222. volt |= MMC_VDD_34_35;
  3223. if (voltage >= 3500 && voltage <= 3600)
  3224. volt |= MMC_VDD_35_36;
  3225. return volt;
  3226. }
  3227. /**
  3228. * Check if a slot is valid in the device tree
  3229. *
  3230. * @param dev slot device to check
  3231. *
  3232. * @return true if status reports "ok" or "okay" or if no status,
  3233. * false otherwise.
  3234. */
  3235. static bool octeontx_mmc_get_valid(struct udevice *dev)
  3236. {
  3237. const char *stat = ofnode_read_string(dev_ofnode(dev), "status");
  3238. if (!stat || !strncmp(stat, "ok", 2))
  3239. return true;
  3240. else
  3241. return false;
  3242. }
  3243. /**
  3244. * Reads slot configuration from the device tree
  3245. *
  3246. * @param dev slot device
  3247. *
  3248. * @return 0 on success, otherwise error
  3249. */
  3250. static int octeontx_mmc_get_config(struct udevice *dev)
  3251. {
  3252. struct octeontx_mmc_slot *slot = dev_to_mmc_slot(dev);
  3253. uint voltages[2];
  3254. uint low, high;
  3255. char env_name[32];
  3256. int err;
  3257. ofnode node = dev_ofnode(dev);
  3258. int bus_width = 1;
  3259. ulong new_max_freq;
  3260. debug("%s(%s)", __func__, dev->name);
  3261. slot->cfg.name = dev->name;
  3262. slot->cfg.f_max = ofnode_read_s32_default(dev_ofnode(dev),
  3263. "max-frequency",
  3264. 26000000);
  3265. snprintf(env_name, sizeof(env_name), "mmc_max_frequency%d",
  3266. slot->bus_id);
  3267. new_max_freq = env_get_ulong(env_name, 10, slot->cfg.f_max);
  3268. debug("Reading %s, got %lu\n", env_name, new_max_freq);
  3269. if (new_max_freq != slot->cfg.f_max) {
  3270. printf("Overriding device tree MMC maximum frequency %u to %lu\n",
  3271. slot->cfg.f_max, new_max_freq);
  3272. slot->cfg.f_max = new_max_freq;
  3273. }
  3274. slot->cfg.f_min = 400000;
  3275. slot->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
  3276. if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
  3277. slot->hs400_tuning_block =
  3278. ofnode_read_s32_default(dev_ofnode(dev),
  3279. "marvell,hs400-tuning-block",
  3280. -1);
  3281. debug("%s(%s): mmc HS400 tuning block: %d\n", __func__,
  3282. dev->name, slot->hs400_tuning_block);
  3283. slot->hs200_tap_adj =
  3284. ofnode_read_s32_default(dev_ofnode(dev),
  3285. "marvell,hs200-tap-adjust", 0);
  3286. debug("%s(%s): hs200-tap-adjust: %d\n", __func__, dev->name,
  3287. slot->hs200_tap_adj);
  3288. slot->hs400_tap_adj =
  3289. ofnode_read_s32_default(dev_ofnode(dev),
  3290. "marvell,hs400-tap-adjust", 0);
  3291. debug("%s(%s): hs400-tap-adjust: %d\n", __func__, dev->name,
  3292. slot->hs400_tap_adj);
  3293. }
  3294. err = ofnode_read_u32_array(dev_ofnode(dev), "voltage-ranges",
  3295. voltages, 2);
  3296. if (err) {
  3297. slot->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
  3298. } else {
  3299. low = xlate_voltage(voltages[0]);
  3300. high = xlate_voltage(voltages[1]);
  3301. debug(" low voltage: 0x%x (%u), high: 0x%x (%u)\n",
  3302. low, voltages[0], high, voltages[1]);
  3303. if (low > high || !low || !high) {
  3304. pr_err("Invalid MMC voltage range [%u-%u] specified for %s\n",
  3305. low, high, dev->name);
  3306. return -1;
  3307. }
  3308. slot->cfg.voltages = 0;
  3309. do {
  3310. slot->cfg.voltages |= low;
  3311. low <<= 1;
  3312. } while (low <= high);
  3313. }
  3314. debug("%s: config voltages: 0x%x\n", __func__, slot->cfg.voltages);
  3315. slot->slew = ofnode_read_s32_default(node, "cavium,clk-slew", -1);
  3316. slot->drive = ofnode_read_s32_default(node, "cavium,drv-strength", -1);
  3317. gpio_request_by_name(dev, "cd-gpios", 0, &slot->cd_gpio, GPIOD_IS_IN);
  3318. slot->cd_inverted = ofnode_read_bool(node, "cd-inverted");
  3319. gpio_request_by_name(dev, "wp-gpios", 0, &slot->wp_gpio, GPIOD_IS_IN);
  3320. slot->wp_inverted = ofnode_read_bool(node, "wp-inverted");
  3321. if (slot->cfg.voltages & MMC_VDD_165_195) {
  3322. slot->is_1_8v = true;
  3323. slot->is_3_3v = false;
  3324. } else if (slot->cfg.voltages & (MMC_VDD_30_31 | MMC_VDD_31_32 |
  3325. MMC_VDD_33_34 | MMC_VDD_34_35 |
  3326. MMC_VDD_35_36)) {
  3327. slot->is_1_8v = false;
  3328. slot->is_3_3v = true;
  3329. }
  3330. bus_width = ofnode_read_u32_default(node, "bus-width", 1);
  3331. /* Note fall-through */
  3332. switch (bus_width) {
  3333. case 8:
  3334. slot->cfg.host_caps |= MMC_MODE_8BIT;
  3335. case 4:
  3336. slot->cfg.host_caps |= MMC_MODE_4BIT;
  3337. case 1:
  3338. slot->cfg.host_caps |= MMC_MODE_1BIT;
  3339. break;
  3340. }
  3341. if (ofnode_read_bool(node, "no-1-8-v")) {
  3342. slot->is_3_3v = true;
  3343. slot->is_1_8v = false;
  3344. if (!(slot->cfg.voltages & (MMC_VDD_32_33 | MMC_VDD_33_34)))
  3345. pr_warn("%s(%s): voltages indicate 3.3v but 3.3v not supported\n",
  3346. __func__, dev->name);
  3347. }
  3348. if (ofnode_read_bool(node, "mmc-ddr-3-3v")) {
  3349. slot->is_3_3v = true;
  3350. slot->is_1_8v = false;
  3351. if (!(slot->cfg.voltages & (MMC_VDD_32_33 | MMC_VDD_33_34)))
  3352. pr_warn("%s(%s): voltages indicate 3.3v but 3.3v not supported\n",
  3353. __func__, dev->name);
  3354. }
  3355. if (ofnode_read_bool(node, "cap-sd-highspeed") ||
  3356. ofnode_read_bool(node, "cap-mmc-highspeed") ||
  3357. ofnode_read_bool(node, "sd-uhs-sdr25"))
  3358. slot->cfg.host_caps |= MMC_MODE_HS;
  3359. if (slot->cfg.f_max >= 50000000 &&
  3360. slot->cfg.host_caps & MMC_MODE_HS)
  3361. slot->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  3362. if (ofnode_read_bool(node, "sd-uhs-sdr50"))
  3363. slot->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
  3364. if (ofnode_read_bool(node, "sd-uhs-ddr50"))
  3365. slot->cfg.host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz |
  3366. MMC_MODE_DDR_52MHz;
  3367. if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
  3368. if (!slot->is_asim && !slot->is_emul) {
  3369. if (ofnode_read_bool(node, "mmc-hs200-1_8v"))
  3370. slot->cfg.host_caps |= MMC_MODE_HS200 |
  3371. MMC_MODE_HS_52MHz;
  3372. if (ofnode_read_bool(node, "mmc-hs400-1_8v"))
  3373. slot->cfg.host_caps |= MMC_MODE_HS400 |
  3374. MMC_MODE_HS_52MHz |
  3375. MMC_MODE_HS200 |
  3376. MMC_MODE_DDR_52MHz;
  3377. slot->cmd_out_hs200_delay =
  3378. ofnode_read_u32_default(node,
  3379. "marvell,cmd-out-hs200-dly",
  3380. MMC_DEFAULT_HS200_CMD_OUT_DLY);
  3381. debug("%s(%s): HS200 cmd out delay: %d\n",
  3382. __func__, dev->name, slot->cmd_out_hs200_delay);
  3383. slot->data_out_hs200_delay =
  3384. ofnode_read_u32_default(node,
  3385. "marvell,data-out-hs200-dly",
  3386. MMC_DEFAULT_HS200_DATA_OUT_DLY);
  3387. debug("%s(%s): HS200 data out delay: %d\n",
  3388. __func__, dev->name, slot->data_out_hs200_delay);
  3389. slot->cmd_out_hs400_delay =
  3390. ofnode_read_u32_default(node,
  3391. "marvell,cmd-out-hs400-dly",
  3392. MMC_DEFAULT_HS400_CMD_OUT_DLY);
  3393. debug("%s(%s): HS400 cmd out delay: %d\n",
  3394. __func__, dev->name, slot->cmd_out_hs400_delay);
  3395. slot->data_out_hs400_delay =
  3396. ofnode_read_u32_default(node,
  3397. "marvell,data-out-hs400-dly",
  3398. MMC_DEFAULT_HS400_DATA_OUT_DLY);
  3399. debug("%s(%s): HS400 data out delay: %d\n",
  3400. __func__, dev->name, slot->data_out_hs400_delay);
  3401. }
  3402. }
  3403. slot->disable_ddr = ofnode_read_bool(node, "marvell,disable-ddr");
  3404. slot->non_removable = ofnode_read_bool(node, "non-removable");
  3405. slot->cmd_clk_skew = ofnode_read_u32_default(node,
  3406. "cavium,cmd-clk-skew", 0);
  3407. slot->dat_clk_skew = ofnode_read_u32_default(node,
  3408. "cavium,dat-clk-skew", 0);
  3409. debug("%s(%s): host caps: 0x%x\n", __func__,
  3410. dev->name, slot->cfg.host_caps);
  3411. return 0;
  3412. }
  3413. /**
  3414. * Probes a MMC slot
  3415. *
  3416. * @param dev mmc device
  3417. *
  3418. * @return 0 for success, error otherwise
  3419. */
  3420. static int octeontx_mmc_slot_probe(struct udevice *dev)
  3421. {
  3422. struct octeontx_mmc_slot *slot;
  3423. struct mmc *mmc;
  3424. int err;
  3425. debug("%s(%s)\n", __func__, dev->name);
  3426. if (!host_probed) {
  3427. pr_err("%s(%s): Error: host not probed yet\n",
  3428. __func__, dev->name);
  3429. }
  3430. slot = dev_to_mmc_slot(dev);
  3431. mmc = &slot->mmc;
  3432. mmc->dev = dev;
  3433. slot->valid = false;
  3434. if (!octeontx_mmc_get_valid(dev)) {
  3435. debug("%s(%s): slot is invalid\n", __func__, dev->name);
  3436. return -ENODEV;
  3437. }
  3438. debug("%s(%s): Getting config\n", __func__, dev->name);
  3439. err = octeontx_mmc_get_config(dev);
  3440. if (err) {
  3441. pr_err("probe(%s): Error getting config\n", dev->name);
  3442. return err;
  3443. }
  3444. debug("%s(%s): mmc bind, mmc: %p\n", __func__, dev->name, &slot->mmc);
  3445. err = mmc_bind(dev, &slot->mmc, &slot->cfg);
  3446. if (err) {
  3447. pr_err("%s(%s): Error binding mmc\n", __func__, dev->name);
  3448. return -1;
  3449. }
  3450. /* For some reason, mmc_bind always assigns priv to the device */
  3451. slot->mmc.priv = slot;
  3452. debug("%s(%s): lowlevel init\n", __func__, dev->name);
  3453. err = octeontx_mmc_init_lowlevel(mmc);
  3454. if (err) {
  3455. pr_err("probe(%s): Low-level init failed\n", dev->name);
  3456. return err;
  3457. }
  3458. slot->valid = true;
  3459. debug("%s(%s):\n"
  3460. " base address : %p\n"
  3461. " bus id : %d\n", __func__, dev->name,
  3462. slot->base_addr, slot->bus_id);
  3463. return err;
  3464. }
  3465. /**
  3466. * MMC slot driver operations
  3467. */
  3468. static const struct dm_mmc_ops octeontx_hsmmc_ops = {
  3469. .send_cmd = octeontx_mmc_dev_send_cmd,
  3470. .set_ios = octeontx_mmc_set_ios,
  3471. .get_cd = octeontx_mmc_get_cd,
  3472. .get_wp = octeontx_mmc_get_wp,
  3473. #ifdef MMC_SUPPORTS_TUNING
  3474. .execute_tuning = octeontx_mmc_execute_tuning,
  3475. #endif
  3476. };
  3477. static const struct udevice_id octeontx_hsmmc_ids[] = {
  3478. { .compatible = "mmc-slot" },
  3479. { }
  3480. };
  3481. U_BOOT_DRIVER(octeontx_hsmmc_slot) = {
  3482. .name = "octeontx_hsmmc_slot",
  3483. .id = UCLASS_MMC,
  3484. .of_match = of_match_ptr(octeontx_hsmmc_ids),
  3485. .probe = octeontx_mmc_slot_probe,
  3486. .ops = &octeontx_hsmmc_ops,
  3487. };
  3488. /*****************************************************************
  3489. * PCI host driver
  3490. *
  3491. * The PCI host driver contains the resources used by all of the
  3492. * slot drivers.
  3493. *
  3494. * The slot drivers are pseudo drivers.
  3495. */
  3496. /**
  3497. * Probe the MMC host controller
  3498. *
  3499. * @param dev mmc host controller device
  3500. *
  3501. * @return 0 for success, -1 on error
  3502. */
  3503. static int octeontx_mmc_host_probe(struct udevice *dev)
  3504. {
  3505. struct octeontx_mmc_host *host = dev_get_priv(dev);
  3506. union mio_emm_int emm_int;
  3507. struct clk clk;
  3508. int ret;
  3509. u8 rev;
  3510. debug("%s(%s): Entry host: %p\n", __func__, dev->name, host);
  3511. if (!octeontx_mmc_get_valid(dev)) {
  3512. debug("%s(%s): mmc host not valid\n", __func__, dev->name);
  3513. return -ENODEV;
  3514. }
  3515. memset(host, 0, sizeof(*host));
  3516. /* Octeon TX & TX2 use PCI based probing */
  3517. if (device_is_compatible(dev, "cavium,thunder-8890-mmc")) {
  3518. host->base_addr = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0,
  3519. PCI_REGION_MEM);
  3520. if (!host->base_addr) {
  3521. pr_err("%s: Error: MMC base address not found\n",
  3522. __func__);
  3523. return -1;
  3524. }
  3525. } else {
  3526. host->base_addr = dev_remap_addr(dev);
  3527. }
  3528. host->dev = dev;
  3529. debug("%s(%s): Base address: %p\n", __func__, dev->name,
  3530. host->base_addr);
  3531. if (!dev_has_ofnode(dev)) {
  3532. pr_err("%s: No device tree information found\n", __func__);
  3533. return -1;
  3534. }
  3535. host->node = dev_ofnode(dev);
  3536. host->last_slotid = -1;
  3537. #if !defined(CONFIG_ARCH_OCTEON)
  3538. if (otx_is_platform(PLATFORM_ASIM))
  3539. host->is_asim = true;
  3540. if (otx_is_platform(PLATFORM_EMULATOR))
  3541. host->is_emul = true;
  3542. #endif
  3543. host->dma_wait_delay =
  3544. ofnode_read_u32_default(dev_ofnode(dev),
  3545. "marvell,dma-wait-delay", 1);
  3546. /* Force reset of eMMC */
  3547. writeq(0, host->base_addr + MIO_EMM_CFG());
  3548. debug("%s: Clearing MIO_EMM_CFG\n", __func__);
  3549. udelay(100);
  3550. emm_int.u = readq(host->base_addr + MIO_EMM_INT());
  3551. debug("%s: Writing 0x%llx to MIO_EMM_INT\n", __func__, emm_int.u);
  3552. writeq(emm_int.u, host->base_addr + MIO_EMM_INT());
  3553. debug("%s(%s): Getting I/O clock\n", __func__, dev->name);
  3554. ret = clk_get_by_index(dev, 0, &clk);
  3555. if (ret < 0)
  3556. return ret;
  3557. ret = clk_enable(&clk);
  3558. if (ret)
  3559. return ret;
  3560. host->sys_freq = clk_get_rate(&clk);
  3561. debug("%s(%s): I/O clock %llu\n", __func__, dev->name, host->sys_freq);
  3562. if (IS_ENABLED(CONFIG_ARCH_OCTEONTX2)) {
  3563. /* Flags for issues to work around */
  3564. dm_pci_read_config8(dev, PCI_REVISION_ID, &rev);
  3565. if (otx_is_soc(CN96XX)) {
  3566. debug("%s: CN96XX revision %d\n", __func__, rev);
  3567. switch (rev) {
  3568. case 0:
  3569. host->calibrate_glitch = true;
  3570. host->cond_clock_glitch = true;
  3571. break;
  3572. case 1:
  3573. break;
  3574. case 2:
  3575. break;
  3576. case 0x10: /* C0 */
  3577. host->hs400_skew_needed = true;
  3578. debug("HS400 skew support enabled\n");
  3579. fallthrough;
  3580. default:
  3581. debug("CN96XX rev C0+ detected\n");
  3582. host->tap_requires_noclk = true;
  3583. break;
  3584. }
  3585. } else if (otx_is_soc(CN95XX)) {
  3586. if (!rev)
  3587. host->cond_clock_glitch = true;
  3588. }
  3589. }
  3590. host_probed = true;
  3591. return 0;
  3592. }
  3593. /**
  3594. * This performs some initial setup before a probe occurs.
  3595. *
  3596. * @param dev: MMC slot device
  3597. *
  3598. * @return 0 for success, -1 on failure
  3599. *
  3600. * Do some pre-initialization before probing a slot.
  3601. */
  3602. static int octeontx_mmc_host_child_pre_probe(struct udevice *dev)
  3603. {
  3604. struct octeontx_mmc_host *host = dev_get_priv(dev_get_parent(dev));
  3605. struct octeontx_mmc_slot *slot;
  3606. struct mmc_uclass_priv *upriv;
  3607. ofnode node = dev_ofnode(dev);
  3608. u32 bus_id;
  3609. char name[16];
  3610. int err;
  3611. debug("%s(%s) Pre-Probe\n", __func__, dev->name);
  3612. if (ofnode_read_u32(node, "reg", &bus_id)) {
  3613. pr_err("%s(%s): Error: \"reg\" not found in device tree\n",
  3614. __func__, dev->name);
  3615. return -1;
  3616. }
  3617. if (bus_id > OCTEONTX_MAX_MMC_SLOT) {
  3618. pr_err("%s(%s): Error: \"reg\" out of range of 0..%d\n",
  3619. __func__, dev->name, OCTEONTX_MAX_MMC_SLOT);
  3620. return -1;
  3621. }
  3622. slot = &host->slots[bus_id];
  3623. dev_set_priv(dev, slot);
  3624. slot->host = host;
  3625. slot->bus_id = bus_id;
  3626. slot->dev = dev;
  3627. slot->base_addr = host->base_addr;
  3628. slot->is_asim = host->is_asim;
  3629. slot->is_emul = host->is_emul;
  3630. snprintf(name, sizeof(name), "octeontx-mmc%d", bus_id);
  3631. err = device_set_name(dev, name);
  3632. /* FIXME: This code should not be needed */
  3633. if (!dev_get_uclass_priv(dev)) {
  3634. debug("%s(%s): Allocating uclass priv\n", __func__,
  3635. dev->name);
  3636. upriv = calloc(1, sizeof(struct mmc_uclass_priv));
  3637. if (!upriv)
  3638. return -ENOMEM;
  3639. /*
  3640. * FIXME: This is not allowed
  3641. * dev_set_uclass_priv(dev, upriv);
  3642. * uclass_set_priv(dev->uclass, upriv);
  3643. */
  3644. } else {
  3645. upriv = dev_get_uclass_priv(dev);
  3646. }
  3647. upriv->mmc = &slot->mmc;
  3648. debug("%s: uclass priv: %p, mmc: %p\n", dev->name, upriv, upriv->mmc);
  3649. debug("%s: ret: %d\n", __func__, err);
  3650. return err;
  3651. }
  3652. static const struct udevice_id octeontx_hsmmc_host_ids[] = {
  3653. { .compatible = "cavium,thunder-8890-mmc" },
  3654. { .compatible = "cavium,octeon-7360-mmc" },
  3655. { }
  3656. };
  3657. U_BOOT_DRIVER(octeontx_hsmmc_host) = {
  3658. .name = "octeontx_hsmmc_host",
  3659. /* FIXME: Why is this not UCLASS_MMC? */
  3660. .id = UCLASS_MISC,
  3661. .of_match = of_match_ptr(octeontx_hsmmc_host_ids),
  3662. .probe = octeontx_mmc_host_probe,
  3663. .priv_auto = sizeof(struct octeontx_mmc_host),
  3664. .child_pre_probe = octeontx_mmc_host_child_pre_probe,
  3665. .flags = DM_FLAG_PRE_RELOC,
  3666. };
  3667. static struct pci_device_id octeontx_mmc_supported[] = {
  3668. { PCI_VDEVICE(CAVIUM, PCI_DEVICE_ID_CAVIUM_EMMC) },
  3669. { },
  3670. };
  3671. U_BOOT_PCI_DEVICE(octeontx_hsmmc_host, octeontx_mmc_supported);