mt_dump.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/delay.h> /* for mdely */
  4. #include <linux/irqflags.h> /* for mdely */
  5. /* #include <linux/ioport.h> *//* for */
  6. #include <linux/mm_types.h>
  7. /* #include <linux/kernel.h> *//* for __raw_readl ... */
  8. #include <asm/io.h> /* __raw_readl */
  9. #include <asm/arch_timer.h>
  10. #include <linux/mmc/host.h>
  11. #include <linux/mmc/mmc.h>
  12. #include <mt-plat/upmu_common.h>
  13. #include <mach/upmu_sw.h>
  14. #include <mach/upmu_hw.h>
  15. #include "mt_sd.h"
  16. #include "mt_dump.h"
  17. #include <mt-plat/sd_misc.h>
  18. #include <mt-plat/sync_write.h>
  19. #ifdef CONFIG_MTK_EMMC_SUPPORT
  20. #ifdef CONFIG_MTK_GPT_SCHEME_SUPPORT
  21. #include <mt-plat/partition.h>
  22. #endif
  23. #endif
  24. #ifndef FPGA_PLATFORM
  25. #ifdef CONFIG_MTK_LEGACY
  26. #include <mach/mt_clkmgr.h>
  27. #endif
  28. #endif
  29. #ifdef CONFIG_OF
  30. #include <linux/of.h>
  31. #include <linux/of_address.h>
  32. #include <linux/of_irq.h>
  33. #endif
  34. #ifdef MTK_MSDC_USE_CACHE
  35. unsigned int g_power_reset;
  36. #endif
  37. MODULE_LICENSE("GPL");
  38. /*--------------------------------------------------------------------------*/
  39. /* head file define */
  40. /*--------------------------------------------------------------------------*/
  41. /* some marco will be reuse with mmc subsystem */
  42. static int simp_mmc_init(int card_type, bool boot);
  43. static void simp_msdc_hw_init(void);
  44. static int module_init_emmc;
  45. static int module_init_sd;
  46. char test_kdump[] = { 6, 7, 3, 5, 'k', 'd', 'u', 'm', 'p', 't', 'e', 's', 't' };
  47. #define SIMP_SUCCESS (0)
  48. #define SIMP_FAILED (-1)
  49. /* the base address of sd card slot */
  50. #define BOOT_STORAGE_ID (0)
  51. #define EXTEND_STORAGE_ID (1)
  52. #define MSDC_CLKSRC (MSDC_CLKSRC_200M)
  53. static unsigned int clks[] = { 200000000 };
  54. #define BLK_LEN (512)
  55. #define MAX_SCLK (52000000)
  56. #define NORMAL_SCLK (25000000)
  57. #define MIN_SCLK (260000)
  58. #define MAX_DMA_CNT (64 * 1024 - 512)
  59. #ifndef CONFIG_OF
  60. static void __iomem *u_msdc_base[HOST_MAX_NUM] = { MSDC0_BASE, MSDC1_BASE, MSDC2_BASE, MSDC3_BASE };
  61. #endif
  62. static struct msdc_hw *p_msdc_hw[HOST_MAX_NUM] = { NULL, NULL, NULL, NULL };
  63. static struct simp_msdc_host g_msdc_host[2];
  64. static struct simp_msdc_card g_msdc_card[2];
  65. static struct simp_msdc_host *pmsdc_boot_host = &g_msdc_host[BOOT_STORAGE_ID];
  66. static struct simp_msdc_host *pmsdc_extend_host = &g_msdc_host[EXTEND_STORAGE_ID];
  67. static struct simp_mmc_host g_mmc_host[2];
  68. static struct simp_mmc_card g_mmc_card[2];
  69. static struct simp_mmc_host *pmmc_boot_host = &g_mmc_host[BOOT_STORAGE_ID];
  70. static struct simp_mmc_host *pmmc_extend_host = &g_mmc_host[EXTEND_STORAGE_ID];
  71. static void msdc_mdelay(u32 time);
  72. static void simp_msdc_dump_info(unsigned int id);
  73. static void simp_msdc_dump_register(struct simp_msdc_host *host);
  74. static void msdc_mdelay(u32 time)
  75. {
  76. u64 t_start = 0, t_end = 0;
  77. t_start = arch_counter_get_cntpct();
  78. t_end = t_start + time * 1000 * 1000 / 77;
  79. while (t_end > arch_counter_get_cntpct())
  80. cpu_relax();
  81. }
  82. static void simp_msdc_dump_register(struct simp_msdc_host *host)
  83. {
  84. void __iomem *base = host->base;
  85. int i = 0;
  86. unsigned int dbg_val1, dbg_val2, dbg_val3, dbg_val4;
  87. pr_err("R[00]=0x%x R[04]=0x%x R[08]=0x%x R[0C]=0x%x R[10]=0x%x R[14]=0x%x\n",
  88. sdr_read32(base + 0x00), sdr_read32(base + 0x04),
  89. sdr_read32(base + 0x08), sdr_read32(base + 0x0C),
  90. sdr_read32(base + 0x10), sdr_read32(base + 0x14));
  91. pr_err("R[30]=0x%x R[34]=0x%x R[38]=0x%x R[3C]=0x%x R[40]=0x%x R[44]=0x%x\n",
  92. sdr_read32(base + 0x30), sdr_read32(base + 0x34),
  93. sdr_read32(base + 0x38), sdr_read32(base + 0x3C),
  94. sdr_read32(base + 0x40), sdr_read32(base + 0x44));
  95. pr_err("R[48]=0x%x R[4C]=0x%x R[50]=0x%x R[54]=0x%x R[58]=0x%x R[5C]=0x%x\n",
  96. sdr_read32(base + 0x48), sdr_read32(base + 0x4C),
  97. sdr_read32(base + 0x50), sdr_read32(base + 0x54),
  98. sdr_read32(base + 0x58), sdr_read32(base + 0x5C));
  99. pr_err("R[60]=0x%x R[70]=0x%x R[74]=0x%x R[78]=0x%x R[7C]=0x%x R[80]=0x%x\n",
  100. sdr_read32(base + 0x60), sdr_read32(base + 0x70),
  101. sdr_read32(base + 0x74), sdr_read32(base + 0x78),
  102. sdr_read32(base + 0x7C), sdr_read32(base + 0x80));
  103. pr_err("R[84]=0x%x R[88]=0x%x R[8C]=0x%x R[90]=0x%x R[94]=0x%x R[98]=0x%x\n",
  104. sdr_read32(base + 0x84), sdr_read32(base + 0x88),
  105. sdr_read32(base + 0x8C), sdr_read32(base + 0x90),
  106. sdr_read32(base + 0x94), sdr_read32(base + 0x98));
  107. pr_err("R[9C]=0x%x R[A0]=0x%x R[A4]=0x%x R[A8]=0x%x R[B0]=0x%x R[B4]=0x%x\n",
  108. sdr_read32(base + 0x9C), sdr_read32(base + 0xA0),
  109. sdr_read32(base + 0xA4), sdr_read32(base + 0xA8),
  110. sdr_read32(base + 0xB0), sdr_read32(base + 0xB4));
  111. pr_err("R[B8]=0x%x R[C0]=0x%x R[C4]=0x%x R[C8]=0x%x R[CC]=0x%x R[D0]=0x%x\n",
  112. sdr_read32(base + 0xB8), sdr_read32(base + 0xC0),
  113. sdr_read32(base + 0xC4), sdr_read32(base + 0xC8),
  114. sdr_read32(base + 0xCC), sdr_read32(base + 0xD0));
  115. if (host->id == 0) {
  116. pr_err
  117. ("R[D4]=0x%x R[F0]=0x%x R[F8]=0x%x R[FC]=0x%x R[110]=0x%x\n",
  118. sdr_read32(base + 0xD4), sdr_read32(base + 0xF0),
  119. sdr_read32(base + 0xF8), sdr_read32(base + 0xFC),
  120. sdr_read32(base + 0x110));
  121. pr_err
  122. ("R[114]=0x%x R[118]=0x%x\n", sdr_read32(base + 0x114), sdr_read32(base + 0x118));
  123. }
  124. if (host->id == 0) {
  125. pr_err("R[%x]=0x%x R[%x]=0x%x R[%x]=0x%x R[%x]=0x%x R[%x]=0x%x R[%x]=0x%x\n",
  126. OFFSET_EMMC50_PAD_DS_TUNE, sdr_read32(EMMC50_PAD_DS_TUNE),
  127. OFFSET_EMMC50_PAD_CMD_TUNE, sdr_read32(EMMC50_PAD_CMD_TUNE),
  128. OFFSET_EMMC50_PAD_DAT01_TUNE, sdr_read32(EMMC50_PAD_DAT01_TUNE),
  129. OFFSET_EMMC50_PAD_DAT23_TUNE, sdr_read32(EMMC50_PAD_DAT23_TUNE),
  130. OFFSET_EMMC50_PAD_DAT45_TUNE, sdr_read32(EMMC50_PAD_DAT45_TUNE),
  131. OFFSET_EMMC50_PAD_DAT67_TUNE, sdr_read32(EMMC50_PAD_DAT67_TUNE));
  132. pr_err("R[%x]=0x%x R[%x]=0x%x R[%x]=0x%x R[%x]=0x%x R[%x]=0x%x R[%x]=0x%x\n",
  133. OFFSET_EMMC51_CFG0, sdr_read32(EMMC51_CFG0),
  134. OFFSET_EMMC50_CFG0, sdr_read32(EMMC50_CFG0),
  135. OFFSET_EMMC50_CFG1, sdr_read32(EMMC50_CFG1),
  136. OFFSET_EMMC50_CFG2, sdr_read32(EMMC50_CFG2),
  137. OFFSET_EMMC50_CFG3, sdr_read32(EMMC50_CFG3),
  138. OFFSET_EMMC50_CFG4, sdr_read32(EMMC50_CFG4));
  139. pr_err("R[%p]=0x%.8x\n", MSDC0_GPIO_MODE18_ADDR,
  140. sdr_read32(MSDC0_GPIO_MODE18_ADDR));
  141. pr_err("R[%p]=0x%.8x\n", MSDC0_GPIO_MODE19_ADDR,
  142. sdr_read32(MSDC0_GPIO_MODE19_ADDR));
  143. pr_err("R[%p]=0x%.8x\n", MSDC0_GPIO_IES_G5_ADDR,
  144. sdr_read32(MSDC0_GPIO_IES_G5_ADDR));
  145. pr_err("R[%p]=0x%.8x\n", MSDC0_GPIO_SMT_G5_ADDR,
  146. sdr_read32(MSDC0_GPIO_SMT_G5_ADDR));
  147. pr_err("R[%p]=0x%.8x\n", MSDC0_GPIO_TDSEL0_G5_ADDR,
  148. sdr_read32(MSDC0_GPIO_TDSEL0_G5_ADDR));
  149. pr_err("R[%p]=0x%.8x\n", MSDC0_GPIO_RDSEL0_G5_ADDR,
  150. sdr_read32(MSDC0_GPIO_RDSEL0_G5_ADDR));
  151. pr_err("R[%p]=0x%.8x\n", MSDC0_GPIO_DRV0_G5_ADDR,
  152. sdr_read32(MSDC0_GPIO_DRV0_G5_ADDR));
  153. pr_err("R[%p]=0x%.8x\n", MSDC0_GPIO_PUPD0_G5_ADDR,
  154. sdr_read32(MSDC0_GPIO_PUPD0_G5_ADDR));
  155. pr_err("R[%p]=0x%.8x\n", MSDC0_GPIO_PUPD1_G5_ADDR,
  156. sdr_read32(MSDC0_GPIO_PUPD1_G5_ADDR));
  157. } else if (host->id == 1) {
  158. pr_err("R[%p]=0x%.8x\n", MSDC1_GPIO_MODE17_ADDR,
  159. sdr_read32(MSDC1_GPIO_MODE17_ADDR));
  160. pr_err("R[%p]=0x%.8x\n", MSDC1_GPIO_MODE18_ADDR,
  161. sdr_read32(MSDC1_GPIO_MODE18_ADDR));
  162. pr_err("R[%p]=0x%.8x\n", MSDC1_GPIO_IES_G4_ADDR,
  163. sdr_read32(MSDC1_GPIO_IES_G4_ADDR));
  164. pr_err("R[%p]=0x%.8x\n", MSDC1_GPIO_SMT_G4_ADDR,
  165. sdr_read32(MSDC1_GPIO_SMT_G4_ADDR));
  166. pr_err("R[%p]=0x%.8x\n", MSDC1_GPIO_TDSEL0_G4_ADDR,
  167. sdr_read32(MSDC1_GPIO_TDSEL0_G4_ADDR));
  168. pr_err("R[%p]=0x%.8x\n", MSDC1_GPIO_RDSEL0_G4_ADDR,
  169. sdr_read32(MSDC1_GPIO_RDSEL0_G4_ADDR));
  170. pr_err("R[%p]=0x%.8x\n", MSDC1_GPIO_DRV0_G4_ADDR,
  171. sdr_read32(MSDC1_GPIO_DRV0_G4_ADDR));
  172. pr_err("R[%p]=0x%.8x\n", MSDC1_GPIO_PUPD0_G4_ADDR,
  173. sdr_read32(MSDC1_GPIO_PUPD0_G4_ADDR));
  174. }
  175. i = 0;
  176. while (i <= 0x27) {
  177. *(u32 *) (base + 0xa0) = i;
  178. dbg_val1 = *(u32 *) (base + 0xa4);
  179. *(u32 *) (base + 0xa0) = i + 1;
  180. dbg_val2 = *(u32 *) (base + 0xa4);
  181. *(u32 *) (base + 0xa0) = i + 2;
  182. dbg_val3 = *(u32 *) (base + 0xa4);
  183. *(u32 *) (base + 0xa0) = i + 3;
  184. dbg_val4 = *(u32 *) (base + 0xa4);
  185. pr_err
  186. ("R[a0]=0x%x R[a4]=0x%x R[a0]=0x%x R[a4]=0x%x R[a0]=0x%x R[a4]=0x%x R[a0]=0x%x R[a4]=0x%x\n",
  187. i, dbg_val1, (i + 1), dbg_val2, (i + 2), dbg_val3, (i + 3), dbg_val4);
  188. i += 4;
  189. }
  190. }
  191. static void simp_msdc_dump_info(unsigned int id)
  192. {
  193. if (id == pmsdc_boot_host->id)
  194. simp_msdc_dump_register(pmsdc_boot_host);
  195. if (id == pmsdc_extend_host->id)
  196. simp_msdc_dump_register(pmsdc_extend_host);
  197. }
  198. /* #define PERI_MSDC_SRCSEL (0xF100000c) */
  199. /* #define PDN_REG (0xF1000010) */
  200. static void simp_msdc_config_clksrc(struct simp_msdc_host *host, CLK_SOURCE_T clksrc)
  201. {
  202. host->clksrc = clksrc;
  203. host->clk = clks[clksrc];
  204. }
  205. static void simp_msdc_config_clock(struct simp_msdc_host *host, unsigned int hz)
  206. { /* no ddr */
  207. /* struct msdc_hw *hw = host->priv;*/
  208. void __iomem *base = host->base;
  209. u32 mode; /* use divisor or not */
  210. u32 div = 0;
  211. u32 sclk;
  212. u32 hclk = host->clk;
  213. u32 orig_clksrc = host->clksrc;
  214. if (hz >= hclk) {
  215. mode = 0x1; /* no divisor */
  216. sclk = hclk;
  217. } else {
  218. mode = 0x0; /* use divisor */
  219. if (hz >= (hclk >> 1)) {
  220. div = 0; /* mean div = 1/2 */
  221. sclk = hclk >> 1; /* sclk = clk / 2 */
  222. } else {
  223. div = (hclk + ((hz << 2) - 1)) / (hz << 2);
  224. sclk = (hclk >> 2) / div;
  225. }
  226. }
  227. host->sclk = sclk;
  228. /*pr_notice("clock<%d>\n",sclk);*/
  229. /* set clock mode and divisor */
  230. /*simp_msdc_config_clksrc(host, MSDC_CLKSRC_NONE);*/
  231. /* designer said: best way is wait clk stable while modify clk config bit */
  232. sdr_set_field(MSDC_CFG, MSDC_CFG_CKMOD | MSDC_CFG_CKDIV, (mode << 12) | (div % 0xfff));
  233. simp_msdc_config_clksrc(host, orig_clksrc);
  234. while (!(sdr_read32(MSDC_CFG) & MSDC_CFG_CKSTB))
  235. ;
  236. }
  237. static void msdc_set_timeout(struct simp_msdc_host *host, u32 ns, u32 clks)
  238. {
  239. void __iomem *base = host->base;
  240. u32 timeout, clk_ns;
  241. clk_ns = 1000000000UL / host->sclk;
  242. timeout = ns / clk_ns + clks;
  243. timeout = timeout >> 20; /* in 2^20 sclk cycle unit */
  244. timeout = timeout > 1 ? timeout - 1 : 0;
  245. timeout = timeout > 255 ? 255 : timeout;
  246. sdr_set_field(SDC_CFG, SDC_CFG_DTOC, timeout);
  247. }
  248. static unsigned int simp_mmc_power_up(struct simp_mmc_host *host, bool on)
  249. {
  250. #if MTK_MMC_DUMP_DBG
  251. pr_err("[%s]: on=%d, start\n", __func__, on);
  252. #endif
  253. #ifdef FPGA_PLATFORM
  254. if (on)
  255. hwPowerOn_fpga();
  256. else
  257. hwPowerDown_fpga();
  258. #else
  259. #if 0
  260. switch (host->mtk_host->id) {
  261. case 0:
  262. /* for emmc, host0 and host4 are mutually exclusive for emmc card */
  263. simp_msdc_ldo_power(on, MT6328_POWER_LDO_VEMC33, VOL_3300);
  264. break;
  265. case 1:
  266. /* for sd, makesure msdc host volt is the same as mt6589 IP internal LDO volt */
  267. simp_msdc_ldo_power(on, MT6328_POWER_LDO_VMC, VOL_3300);
  268. simp_msdc_ldo_power(on, MT6328_POWER_LDO_VMCH, VOL_3300);
  269. break;
  270. default:
  271. break;
  272. }
  273. #else
  274. switch (host->mtk_host->id) {
  275. case 0:
  276. if (on) {
  277. /* mt6331_upmu_set_rg_vemc33_vosel(1);
  278. mt6331_upmu_set_rg_vemc33_en(1); */
  279. msdc_power_set_field(REG_VEMC33_VOLSEL, MASK_VEMC33_VOLSEL,
  280. VEMC33_VOLSEL_3V3);
  281. msdc_power_set_field(REG_VEMC33_EN, MASK_VEMC33_EN, 0x1);
  282. } else {
  283. /* mt6331_upmu_set_rg_vemc33_en(0); */
  284. msdc_power_set_field(REG_VEMC33_EN, MASK_VEMC33_EN, 0x0);
  285. }
  286. break;
  287. case 1:
  288. if (on) {
  289. /*mt6331_upmu_set_rg_vmc_vosel(1);
  290. mt6331_upmu_set_rg_vmc_en(1);
  291. mt6331_upmu_set_rg_vmch_vosel(1);
  292. mt6331_upmu_set_rg_vmch_en(1); */
  293. msdc_power_set_field(REG_VMC_VOLSEL, MASK_VMC_VOLSEL, VMC_VOLSEL_3V3);
  294. msdc_power_set_field(REG_VMC_EN, MASK_VMC_EN, 0x1);
  295. msdc_power_set_field(REG_VMCH_VOLSEL, MASK_VMCH_VOLSEL, VMCH_VOLSEL_3V3);
  296. msdc_power_set_field(REG_VMCH_EN, MASK_VMCH_EN, 0x1);
  297. } else {
  298. /*mt6331_upmu_set_rg_vmc_en(0);
  299. mt6331_upmu_set_rg_vmch_en(0); */
  300. msdc_power_set_field(REG_VMC_EN, MASK_VMC_EN, 0x0);
  301. msdc_power_set_field(REG_VMCH_EN, MASK_VMCH_EN, 0x0);
  302. }
  303. break;
  304. default:
  305. break;
  306. }
  307. #endif
  308. #endif
  309. #if MTK_MMC_DUMP_DBG
  310. pr_err("[%s]: on=%d, end\n", __func__, on);
  311. #endif
  312. return SIMP_SUCCESS;
  313. }
  314. /* do not change to 1.8v, so cmd11 not used */
  315. static unsigned int simp_mmc_set_signal_voltage(struct simp_mmc_host *host, int volt, bool cmd11)
  316. {
  317. /* set mmc card voltage */
  318. return SIMP_SUCCESS;
  319. }
  320. #define clk_readl(addr) \
  321. readl(addr)
  322. #define clk_writel(addr, val) mt_reg_sync_writel(val, addr)
  323. #define clk_setl(addr, val) \
  324. mt_reg_sync_writel(clk_readl(addr) | (val), addr)
  325. #define clk_clrl(addr, val) \
  326. mt_reg_sync_writel(clk_readl(addr) & ~(val), addr)
  327. #if 1
  328. static unsigned int simp_mmc_enable_clk(struct simp_mmc_host *host)
  329. {
  330. /*check base address ,cause KE too early to initial it at msdc_drv_probe */
  331. if (NULL == apmixed_reg_base1 || NULL == topckgen_reg_base || NULL == pericfg_reg_base) {
  332. pr_err("apmixed_reg_base1=%p, topckgen_reg_base=%p,pericfg_reg_base=%p\n",
  333. apmixed_reg_base1, topckgen_reg_base, pericfg_reg_base);
  334. return SIMP_FAILED;
  335. }
  336. /* step1: open pll */
  337. clk_setl(apmixed_reg_base1 + MSDC_MSDCPLL_PWR_CON0_OFFSET, 0x3);
  338. msdc_mdelay(1);
  339. clk_setl(apmixed_reg_base1 + MSDC_MSDCPLL_PWR_CON0_OFFSET, 0x1);
  340. clk_setl(apmixed_reg_base1 + MSDC_MSDCPLL_CON1_OFFSET, 0x80000000);
  341. clk_setl(apmixed_reg_base1 + MSDC_MSDCPLL_CON0_OFFSET, 0x1);
  342. msdc_mdelay(1);
  343. /* step2: enable mux */
  344. mt_reg_sync_writel(0x01010100, topckgen_reg_base + MSDC_CLK_CFG_2_OFFSET);
  345. mt_reg_sync_writel(0x07020203, topckgen_reg_base + MSDC_CLK_CFG_3_OFFSET);
  346. mt_reg_sync_writel(0xFFFFFFFF, pericfg_reg_base + MSDC_PERI_PDN_CLR0_OFFSET);
  347. #if MTK_MMC_DUMP_DBG
  348. if (apmixed_reg_base1 && topckgen_reg_base) {
  349. pr_err(" MSDCPLL_PWR_CON0[0x%p][bit0~1 should be 2b'01]=0x%x",
  350. (apmixed_reg_base1 + MSDC_MSDCPLL_PWR_CON0_OFFSET),
  351. sdr_read32(apmixed_reg_base1 + MSDC_MSDCPLL_PWR_CON0_OFFSET));
  352. pr_err(" MSDCPLL_CON0 [0x%p][bit0 should be 1b'1]=0x%x",
  353. (apmixed_reg_base1 + MSDC_MSDCPLL_CON0_OFFSET),
  354. sdr_read32(apmixed_reg_base1 + MSDC_MSDCPLL_CON0_OFFSET));
  355. pr_err(" CLK_CFG_2 [0x%p][bit[31:24]should be 0x01]=0x%x",
  356. (topckgen_reg_base + MSDC_CLK_CFG_2_OFFSET),
  357. sdr_read32(topckgen_reg_base + MSDC_CLK_CFG_2_OFFSET));
  358. pr_err(" CLK_CFG_3 [0x%p][bit[15:0]should be 0x0202]=0x%x",
  359. (topckgen_reg_base + MSDC_CLK_CFG_3_OFFSET),
  360. sdr_read32(topckgen_reg_base + MSDC_CLK_CFG_3_OFFSET));
  361. pr_err(" PERI_PDN_STA0 [0x%p][bit13=msdc0, bit14=msdc1,0:on,1:off]=0x%x",
  362. (topckgen_reg_base + MSDC_PERI_PDN_STA0_OFFSET),
  363. sdr_read32(topckgen_reg_base + MSDC_PERI_PDN_STA0_OFFSET));
  364. } else
  365. pr_err(" apmixed_reg_base1 = %p, topckgen_reg_base = %p", apmixed_reg_base1,
  366. topckgen_reg_base);
  367. #endif
  368. return SIMP_SUCCESS;
  369. }
  370. #endif
  371. static unsigned int simp_mmc_hw_reset_for_init(struct simp_mmc_host *host)
  372. {
  373. void __iomem *base;
  374. base = host->mtk_host->base;
  375. if (0 == host->mtk_host->id) {
  376. /* check emmc card support HW Rst_n yes or not is the good way.
  377. * but if the card not support it , here just failed.
  378. * if the card support it, Rst_n function enable under DA driver,
  379. * pls see SDMMC_Download_BL_PostProcess_Internal() */
  380. /* 1ms pluse to trigger emmc enter pre-idle state */
  381. sdr_set_bits(EMMC_IOCON, EMMC_IOCON_BOOTRST);
  382. msdc_mdelay(1);
  383. sdr_clr_bits(EMMC_IOCON, EMMC_IOCON_BOOTRST);
  384. /* clock is need after Rst_n pull high, and the card need
  385. * clock to calculate time for tRSCA, tRSTH */
  386. sdr_set_bits(MSDC_CFG, MSDC_CFG_CKPDN);
  387. msdc_mdelay(1);
  388. /* not to close, enable clock free run under mt_dump */
  389. /*sdr_clr_bits(MSDC_CFG, MSDC_CFG_CKPDNT);*/
  390. }
  391. return SIMP_SUCCESS;
  392. }
  393. #if 0
  394. enum {
  395. RESP_NONE = 0,
  396. RESP_R1 = 1,
  397. RESP_R2 = 2,
  398. RESP_R3 = 3,
  399. RESP_R4 = 4,
  400. RESP_R5 = 5,
  401. RESP_R6 = 6,
  402. RESP_R7 = 7,
  403. RESP_R1B = 8
  404. };
  405. #endif
  406. static int msdc_rsp[] = {
  407. 0, /* RESP_NONE */
  408. 1, /* RESP_R1 */
  409. 2, /* RESP_R2 */
  410. 3, /* RESP_R3 */
  411. 4, /* RESP_R4 */
  412. 1, /* RESP_R5 */
  413. 1, /* RESP_R6 */
  414. 1, /* RESP_R7 */
  415. 7, /* RESP_R1b */
  416. };
  417. #define msdc_retrys(expr, retry, cnt, id) \
  418. do { \
  419. int backup = cnt; \
  420. while (retry) { \
  421. if (!(expr))\
  422. break; \
  423. if (cnt-- == 0) { \
  424. retry--;\
  425. msdc_mdelay(1);\
  426. cnt = backup; \
  427. } \
  428. } \
  429. if (retry == 0) \
  430. simp_msdc_dump_info(id); \
  431. WARN_ON(retry == 0); \
  432. } while (0)
  433. #define msdc_resetd(id) \
  434. do { \
  435. int retry = 3, cnt = 1000; \
  436. sdr_set_bits(MSDC_CFG, MSDC_CFG_RST); \
  437. msdc_retrys(sdr_read32(MSDC_CFG) & MSDC_CFG_RST, retry, cnt, id); \
  438. } while (0)
  439. #define msdc_clr_int() \
  440. do { \
  441. u32 val = sdr_read32(MSDC_INT); \
  442. sdr_write32(MSDC_INT, val); \
  443. } while (0)
  444. #define msdc_clr_fifo(id) \
  445. do { \
  446. int retry = 3, cnt = 1000; \
  447. sdr_set_bits(MSDC_FIFOCS, MSDC_FIFOCS_CLR); \
  448. msdc_retrys(sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_CLR, retry, cnt, id); \
  449. } while (0)
  450. #define msdc_reset_hw(id) \
  451. do { \
  452. msdc_resetd(id); \
  453. msdc_clr_fifo(id); \
  454. msdc_clr_int(); \
  455. } while (0)
  456. #define DBG_EVT_ALL (0xffffffff)
  457. static void simp_msdc_dma_stop(struct simp_msdc_host *host)
  458. {
  459. void __iomem *base = host->base;
  460. int retry = 500;
  461. int count = 1000;
  462. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP, 1);
  463. msdc_retrys((sdr_read32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS), retry, count, host->id);
  464. if (retry == 0) {
  465. pr_err
  466. ("## Failed to stop DMA! please check it here ##\n");
  467. }
  468. }
  469. static unsigned int simp_msdc_init(struct simp_mmc_host *mmc_host)
  470. {
  471. unsigned int ret = 0;
  472. void __iomem *base;
  473. struct simp_msdc_host *host = mmc_host->mtk_host;
  474. /*struct msdc_hw *hw;*/
  475. /* Todo1: struct msdc_hw in board.c */
  476. base = host->base;
  477. /* set to SD/MMC mode, the first step while operation msdc */
  478. sdr_set_field(MSDC_CFG, MSDC_CFG_MODE, 1);
  479. /* reset controller */
  480. msdc_resetd(host->id);
  481. sd_debug_zone[host->id] = DBG_EVT_ALL;
  482. simp_msdc_dma_stop(host);
  483. /* clear FIFO */
  484. msdc_clr_fifo(host->id);
  485. /* Disable & Clear all interrupts */
  486. msdc_clr_int();
  487. sdr_write32(MSDC_INTEN, 0);
  488. /* reset tuning parameter */
  489. sdr_write32(MSDC_PAD_TUNE0, 0x00000000);
  490. sdr_write32(MSDC_DAT_RDDLY0, 0x00000000);
  491. sdr_write32(MSDC_DAT_RDDLY1, 0x00000000);
  492. sdr_write32(MSDC_IOCON, 0x00000000);
  493. sdr_write32(MSDC_PATCH_BIT0, 0x003C000F);
  494. /* PIO mode */
  495. sdr_set_bits(MSDC_CFG, MSDC_CFG_PIO);
  496. /* sdio + inswkup */
  497. sdr_set_bits(SDC_CFG, SDC_CFG_SDIO);
  498. sdr_set_bits(SDC_CFG, SDC_CFG_INSWKUP);
  499. /* disable async fifo use interl delay */
  500. sdr_clr_bits(MSDC_PATCH_BIT2, MSDC_PB2_CFGCRCSTS);
  501. sdr_set_bits(MSDC_PATCH_BIT2, MSDC_PB2_CFGRESP);
  502. /* disable support 64G */
  503. sdr_set_bits(MSDC_PATCH_BIT2, MSDC_PB2_SUPPORT64G);
  504. /* set Driving SMT PUPD */
  505. switch (host->id) {
  506. case 0:
  507. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR, MSDC0_DRV_CMD_MASK, p_msdc_hw[0]->cmd_drv);
  508. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR, MSDC0_DRV_DSL_MASK, p_msdc_hw[0]->ds_drv);
  509. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR, MSDC0_DRV_CLK_MASK, p_msdc_hw[0]->clk_drv);
  510. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR, MSDC0_DRV_DAT_MASK, p_msdc_hw[0]->dat_drv);
  511. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR, MSDC0_DRV_RSTB_MASK, p_msdc_hw[0]->rst_drv);
  512. sdr_set_field(MSDC0_GPIO_SMT_G5_ADDR, MSDC0_SMT_ALL_MASK, 0x1F);
  513. sdr_set_field(MSDC0_GPIO_PUPD0_G5_ADDR, MSDC0_PUPD_CMD_DSL_CLK_DAT04_MASK,
  514. 0x11111661);
  515. sdr_set_field(MSDC0_GPIO_PUPD1_G5_ADDR, MSDC0_PUPD_DAT57_RSTB_MASK, 0x2111);
  516. break;
  517. case 1:
  518. sdr_set_field(MSDC1_GPIO_DRV0_G4_ADDR, MSDC1_DRV_CMD_MASK, p_msdc_hw[1]->cmd_drv);
  519. sdr_set_field(MSDC1_GPIO_DRV0_G4_ADDR, MSDC1_DRV_CLK_MASK, p_msdc_hw[1]->clk_drv);
  520. sdr_set_field(MSDC1_GPIO_DRV0_G4_ADDR, MSDC1_DRV_DAT_MASK, p_msdc_hw[1]->dat_drv);
  521. sdr_set_field(MSDC2_GPIO_SMT_G0_ADDR, MSDC2_SMT_ALL_MASK, 0x7);
  522. sdr_set_field(MSDC1_GPIO_PUPD0_G4_ADDR, MSDC1_PUPD_CMD_CLK_DAT_MASK, 0x222262);
  523. break;
  524. default:
  525. break;
  526. }
  527. /* set sampling edge */
  528. sdr_set_field(MSDC_IOCON, MSDC_IOCON_RSPL, 0); /* rising: 0 */
  529. sdr_set_field(MSDC_IOCON, MSDC_IOCON_R_D_SMPL, 0);
  530. /* write crc timeout detection */
  531. sdr_set_field(MSDC_PATCH_BIT0, 1 << 30, 1);
  532. /* Clock source select */
  533. simp_msdc_config_clksrc(host, host->clksrc);
  534. /* Bus width to 1 bit */
  535. sdr_set_field(SDC_CFG, SDC_CFG_BUSWIDTH, 0);
  536. /* make sure the clock is 260K */
  537. simp_msdc_config_clock(host, MIN_SCLK);
  538. /* Set Timeout 100ms */
  539. msdc_set_timeout(host, 100000000, 0);
  540. sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
  541. /* detect card */
  542. /* need to check card is insert [Fix me] */
  543. /* check write protection [Fix me] */
  544. #if 0
  545. /* simple test for clk output */
  546. sdr_write32(MSDC_PATCH_BIT0, 0xF3F);
  547. sdr_write32(MSDC_CFG, 0x10001013);
  548. sdr_write32(SDC_CFG, 0x0);
  549. sdr_write32(SDC_CMD, 0x0);
  550. sdr_write32(SDC_ARG, 0x0);
  551. /* dump register for debug */
  552. simp_msdc_dump_register(host);
  553. #endif
  554. return ret;
  555. }
  556. static void simp_mmc_set_bus_mode(struct simp_mmc_host *host, unsigned int mode)
  557. {
  558. /* mtk: msdc not support to modify bus mode */
  559. }
  560. /* =======================something for msdc cmd/data */
  561. #define CMD_WAIT_RETRY (0x8FFFFFFF)
  562. #define sdc_is_busy() (sdr_read32(SDC_STS) & SDC_STS_SDCBUSY)
  563. #define sdc_is_cmd_busy() (sdr_read32(SDC_STS) & SDC_STS_CMDBUSY)
  564. #define sdc_send_cmd(cmd, arg) \
  565. do { \
  566. sdr_write32(SDC_ARG, (arg)); \
  567. sdr_write32(SDC_CMD, (cmd)); \
  568. } while (0)
  569. int simp_offset = 0;
  570. u8 simp_ext_csd[512];
  571. static int simp_msdc_cmd(struct simp_msdc_host *host,
  572. unsigned int cmd, unsigned int raw,
  573. unsigned int arg, int rsptyp, unsigned int *resp)
  574. {
  575. int retry = 5000;
  576. void __iomem *base = host->base;
  577. unsigned int error = 0;
  578. unsigned int intsts = 0;
  579. unsigned int cmdsts = MSDC_INT_CMDRDY | MSDC_INT_CMDTMO | MSDC_INT_RSPCRCERR;
  580. /* wait before send command */
  581. if (cmd == CMD13) {
  582. while (retry--) {
  583. if (!sdc_is_cmd_busy())
  584. break;
  585. msdc_mdelay(1);
  586. }
  587. if (retry == 0) {
  588. error = 1;
  589. goto end;
  590. }
  591. } else {
  592. while (retry--) {
  593. if (!sdc_is_busy())
  594. break;
  595. msdc_mdelay(1);
  596. }
  597. if (retry == 0) {
  598. error = 2;
  599. goto end;
  600. }
  601. }
  602. if ((CMD17 == cmd || CMD18 == cmd ||
  603. CMD24 == cmd || CMD25 == cmd) && (host->card->type == MMC_TYPE_MMC))
  604. arg += simp_offset;
  605. sdc_send_cmd(raw, arg);
  606. #if MTK_MMC_DUMP_DBG
  607. pr_debug("cmd=0x%x, arg=0x%x\n", raw, arg);
  608. #endif
  609. /* polling to check the interrupt */
  610. retry = 5000; /*CMD_WAIT_RETRY; */
  611. while ((intsts & cmdsts) == 0) {
  612. intsts = sdr_read32(MSDC_INT);
  613. retry--;
  614. #if MTK_MMC_DUMP_DBG
  615. if (retry % 1000 == 0) {
  616. pr_debug("int cmd=0x%x, arg=0x%x, retry=0x%x, intsts=0x%x\n", raw, arg,
  617. retry, intsts);
  618. simp_msdc_dump_info(host->id);
  619. }
  620. #endif
  621. if (retry == 0) {
  622. error = 3;
  623. goto end;
  624. }
  625. msdc_mdelay(1);
  626. }
  627. intsts &= cmdsts;
  628. sdr_write32(MSDC_INT, intsts); /* clear interrupts */
  629. if (intsts & MSDC_INT_CMDRDY) {
  630. /* get the response */
  631. switch (rsptyp) {
  632. case RESP_NONE:
  633. break;
  634. case RESP_R2:
  635. *resp++ = sdr_read32(SDC_RESP3);
  636. *resp++ = sdr_read32(SDC_RESP2);
  637. *resp++ = sdr_read32(SDC_RESP1);
  638. *resp = sdr_read32(SDC_RESP0);
  639. break;
  640. default: /* Response types 1, 3, 4, 5, 6, 7(1b) */
  641. *resp = sdr_read32(SDC_RESP0);
  642. }
  643. #if MTK_MMC_DUMP_DBG
  644. pr_debug("msdc cmd<%d> arg<0x%x> resp<0x%x>Ready \r\n", cmd, arg, *resp);
  645. #endif
  646. } else {
  647. error = 4;
  648. goto end;
  649. }
  650. if (rsptyp == RESP_R1B) {
  651. retry = 9999;
  652. while ((sdr_read32(MSDC_PS) & MSDC_PS_DAT0) != MSDC_PS_DAT0) {
  653. retry--;
  654. if (retry % 5000 == 0) {
  655. pr_debug("int cmd=0x%x, arg=0x%x, retry=0x%x, intsts=0x%x\n",
  656. raw, arg, retry, intsts);
  657. simp_msdc_dump_info(host->id);
  658. }
  659. if (retry == 0) {
  660. error = 5;
  661. goto end;
  662. }
  663. msdc_mdelay(1);
  664. }
  665. #if MTK_MMC_DUMP_DBG
  666. pr_debug("msdc cmd<%d> done \r\n", cmd);
  667. #endif
  668. }
  669. end:
  670. if (error) {
  671. pr_err("cmd:%d,arg:0x%x,error=%d,intsts=0x%x\n", cmd, arg, error, intsts);
  672. simp_msdc_dump_info(host->id);
  673. }
  674. return error;
  675. }
  676. /* ======================= */
  677. static int simp_mmc_go_idle(struct simp_mmc_host *host)
  678. {
  679. int err = 0;
  680. unsigned int resp = 0;
  681. struct simp_msdc_host *phost = host->mtk_host;
  682. err = simp_msdc_cmd(phost, CMD0, CMD0_RAW, CMD0_ARG, RESP_NONE, &resp);
  683. return err;
  684. }
  685. static unsigned int simp_mmc_get_status(struct simp_mmc_host *host, unsigned int *status)
  686. {
  687. unsigned int resp = 0;
  688. unsigned int err = 0;
  689. struct simp_msdc_host *phost = host->mtk_host;
  690. unsigned int rca = 0;
  691. #ifdef MTK_MSDC_USE_CACHE
  692. if (g_power_reset)
  693. rca = phost->card->rca << 16;
  694. else
  695. rca = mtk_msdc_host[host->index]->mmc->card->rca << 16;
  696. #else
  697. rca = phost->card->rca << 16;
  698. #endif
  699. /* pr_debug("rca=0x%x, mtk_msdc_host[%d]->mmc->card->rca=0x%x,
  700. phost->card->rca=0x%x\n", rca, host->index,
  701. mtk_msdc_host[host->index]->mmc->card->rca, phost->card->rca); */
  702. err = simp_msdc_cmd(phost, CMD13, CMD13_RAW, rca, RESP_R1, &resp);
  703. *status = resp;
  704. return err;
  705. }
  706. static unsigned int simp_mmc_send_stop(struct simp_mmc_host *host)
  707. {
  708. unsigned int resp = 0;
  709. unsigned int err = 0;
  710. struct simp_msdc_host *phost = host->mtk_host;
  711. /* send command */
  712. err = simp_msdc_cmd(phost, CMD12, CMD12_RAW, 0, RESP_R1B, &resp);
  713. return err;
  714. }
  715. static int simp_mmc_send_op_cond(struct simp_mmc_host *host, unsigned int ocr, unsigned int *rocr)
  716. {
  717. int err = 0, i;
  718. unsigned int resp = 0;
  719. struct simp_msdc_host *phost = host->mtk_host;
  720. for (i = 500; i; i--) {
  721. err = simp_msdc_cmd(phost, CMD1, CMD1_RAW, ocr, RESP_R3, &resp);
  722. if (err)
  723. break;
  724. /* if we're just probing, do a single pass */
  725. if (ocr == 0)
  726. break;
  727. /* otherwise wait until reset completes */
  728. if (resp & MMC_CARD_BUSY)
  729. break;
  730. err = -ETIMEDOUT;
  731. msdc_mdelay(10);
  732. }
  733. if (rocr)
  734. *rocr = resp;
  735. if (i <= 400)
  736. pr_err("cmd1: resp(0x%x), i=%d\n", resp, i);
  737. return err;
  738. }
  739. static int simp_mmc_all_send_cid(struct simp_mmc_host *host, unsigned int *cid)
  740. {
  741. int err = 0;
  742. unsigned int resp[4] = { 0 };
  743. struct simp_msdc_host *phost = host->mtk_host;
  744. err = simp_msdc_cmd(phost, CMD2, CMD2_RAW, 0, RESP_R2, resp);
  745. #if MTK_MMC_DUMP_DBG
  746. pr_debug("resp: 0x%x 0x%x 0x%x 0x%x\n", resp[0], resp[1], resp[2], resp[3]);
  747. #endif
  748. memcpy(cid, resp, sizeof(u32) * 4);
  749. return 0;
  750. }
  751. static int simp_mmc_set_relative_addr(struct simp_mmc_card *card)
  752. {
  753. int err;
  754. unsigned int resp;
  755. struct simp_msdc_host *phost = card->host->mtk_host;
  756. err = simp_msdc_cmd(phost, CMD3, CMD3_RAW, card->rca << 16, RESP_R1, &resp);
  757. return err;
  758. }
  759. static int simp_mmc_send_csd(struct simp_mmc_card *card, unsigned int *csd)
  760. {
  761. int err;
  762. unsigned int resp[4] = { 0 };
  763. struct simp_msdc_host *phost = card->host->mtk_host;
  764. err = simp_msdc_cmd(phost, CMD9, CMD9_RAW, card->rca << 16, RESP_R2, resp);
  765. memcpy(csd, resp, sizeof(u32) * 4);
  766. return err;
  767. }
  768. static const unsigned int tran_exp[] = {
  769. 10000, 100000, 1000000, 10000000,
  770. 0, 0, 0, 0
  771. };
  772. static const unsigned char tran_mant[] = {
  773. 0, 10, 12, 13, 15, 20, 25, 30,
  774. 35, 40, 45, 50, 55, 60, 70, 80,
  775. };
  776. static const unsigned int tacc_exp[] = {
  777. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  778. };
  779. static const unsigned int tacc_mant[] = {
  780. 0, 10, 12, 13, 15, 20, 25, 30,
  781. 35, 40, 45, 50, 55, 60, 70, 80,
  782. };
  783. #define UNSTUFF_BITS(resp, start, size) \
  784. ({ \
  785. const int __size = size; \
  786. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  787. const int __off = 3 - ((start) / 32); \
  788. const int __shft = (start) & 31; \
  789. u32 __res; \
  790. __res = resp[__off] >> __shft; \
  791. if (__size + __shft > 32) \
  792. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  793. __res & __mask; \
  794. })
  795. static int simp_mmc_decode_csd(struct simp_mmc_card *card)
  796. {
  797. struct mmc_csd *csd = &card->csd;
  798. unsigned int e, m, a, b;
  799. u32 *resp = card->raw_csd;
  800. /*
  801. * We only understand CSD structure v1.1 and v1.2.
  802. * v1.2 has extra information in bits 15, 11 and 10.
  803. * We also support eMMC v4.4 & v4.41.
  804. */
  805. csd->structure = UNSTUFF_BITS(resp, 126, 2);
  806. if (csd->structure == 0) {
  807. pr_err("unrecognised CSD structure version %d\n", csd->structure);
  808. return -EINVAL;
  809. }
  810. csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
  811. m = UNSTUFF_BITS(resp, 115, 4);
  812. e = UNSTUFF_BITS(resp, 112, 3);
  813. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  814. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  815. m = UNSTUFF_BITS(resp, 99, 4);
  816. e = UNSTUFF_BITS(resp, 96, 3);
  817. csd->max_dtr = tran_exp[e] * tran_mant[m];
  818. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  819. e = UNSTUFF_BITS(resp, 47, 3);
  820. m = UNSTUFF_BITS(resp, 62, 12);
  821. csd->capacity = (1 + m) << (e + 2);
  822. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  823. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  824. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  825. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  826. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  827. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  828. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  829. if (csd->write_blkbits >= 9) {
  830. a = UNSTUFF_BITS(resp, 42, 5);
  831. b = UNSTUFF_BITS(resp, 37, 5);
  832. csd->erase_size = (a + 1) * (b + 1);
  833. csd->erase_size <<= csd->write_blkbits - 9;
  834. }
  835. return 0;
  836. }
  837. static int simp_mmc_select_card(struct simp_mmc_host *host, struct simp_mmc_card *card)
  838. {
  839. int err;
  840. unsigned int resp;
  841. struct simp_msdc_host *phost = host->mtk_host;
  842. err = simp_msdc_cmd(phost, CMD7, CMD7_RAW, card->rca << 16, RESP_R1, &resp);
  843. return 0;
  844. }
  845. /*
  846. * Mask off any voltages we don't support and select
  847. * the lowest voltage
  848. */
  849. static unsigned int simp_mmc_select_voltage(struct simp_mmc_host *host, unsigned int ocr)
  850. {
  851. #if 0
  852. int bit;
  853. ocr &= host->ocr_avail;
  854. bit = ffs(ocr);
  855. if (bit) {
  856. bit -= 1;
  857. ocr &= 3 << bit;
  858. mmc_host_clk_hold(host);
  859. host->ios.vdd = bit;
  860. mmc_set_ios(host);
  861. mmc_host_clk_release(host);
  862. } else {
  863. pr_warn("%s: host doesn't support card's voltages\n", mmc_hostname(host));
  864. ocr = 0;
  865. }
  866. #endif
  867. return ocr;
  868. }
  869. #define CAPACITY_2G (2 * 1024 * 1024 * 1024ULL)
  870. #ifdef CONFIG_MTK_EMMC_SUPPORT
  871. #if 0
  872. static u64 simp_msdc_get_user_capacity(struct simp_mmc_card *card)
  873. {
  874. u64 device_capacity = 0;
  875. u32 device_legacy_capacity = 0;
  876. if (card->csd.read_blkbits)
  877. device_legacy_capacity = card->csd.capacity * (2 << (card->csd.read_blkbits - 1));
  878. else {
  879. device_legacy_capacity = card->csd.capacity;
  880. /*pr_debug("XXX read_blkbits = 0 XXX\n"); */
  881. }
  882. device_capacity = (u64) (card->ext_csd.sectors) * 512 >
  883. device_legacy_capacity ? (u64) (card->ext_csd.sectors) * 512 : device_legacy_capacity;
  884. return device_capacity;
  885. }
  886. #endif
  887. #endif
  888. static void simp_emmc_cal_offset(struct simp_mmc_card *card)
  889. {
  890. #ifdef CONFIG_MTK_EMMC_SUPPORT
  891. #if 0
  892. u64 device_capacity = 0;
  893. simp_offset = MBR_START_ADDRESS_BYTE - (simp_ext_csd[EXT_CSD_BOOT_SIZE_MULT] * 128 * 1024
  894. + simp_ext_csd[EXT_CSD_BOOT_SIZE_MULT] * 128 * 1024
  895. + simp_ext_csd[EXT_CSD_RPMB_SIZE_MULT] * 128 * 1024
  896. + simp_ext_csd[EXT_CSD_GP1_SIZE_MULT +
  897. 2] * 256 * 256 +
  898. simp_ext_csd[EXT_CSD_GP1_SIZE_MULT + 1] * 256 +
  899. simp_ext_csd[EXT_CSD_GP1_SIZE_MULT + 0]
  900. + simp_ext_csd[EXT_CSD_GP2_SIZE_MULT +
  901. 2] * 256 * 256 +
  902. simp_ext_csd[EXT_CSD_GP2_SIZE_MULT + 1] * 256 +
  903. simp_ext_csd[EXT_CSD_GP2_SIZE_MULT + 0]
  904. + simp_ext_csd[EXT_CSD_GP3_SIZE_MULT +
  905. 2] * 256 * 256 +
  906. simp_ext_csd[EXT_CSD_GP3_SIZE_MULT + 1] * 256 +
  907. simp_ext_csd[EXT_CSD_GP3_SIZE_MULT + 0]
  908. + simp_ext_csd[EXT_CSD_GP4_SIZE_MULT +
  909. 2] * 256 * 256 +
  910. simp_ext_csd[EXT_CSD_GP4_SIZE_MULT + 1] * 256 +
  911. simp_ext_csd[EXT_CSD_GP4_SIZE_MULT + 0]);
  912. if (simp_offset < 0)
  913. pr_err("cal offset error(0x%d)\n", simp_offset);
  914. device_capacity = simp_msdc_get_user_capacity(card);
  915. if (device_capacity > CAPACITY_2G)
  916. simp_offset /= 512;
  917. #endif
  918. simp_offset = 0;
  919. /*pr_debug("emmc offset (0x%x)\n", simp_offset); */
  920. #endif /* end of CONFIG_MTK_EMMC_SUPPORT */
  921. }
  922. static int simp_msdc_pio_read(struct simp_msdc_host *host, unsigned int *ptr, unsigned int size);
  923. static void simp_msdc_set_blknum(struct simp_msdc_host *host, unsigned int blknum);
  924. static int simp_mmc_read_ext_csd(struct simp_mmc_host *host, struct simp_mmc_card *card)
  925. {
  926. int err = 0;
  927. unsigned int resp;
  928. struct simp_msdc_host *phost = host->mtk_host;
  929. void __iomem *base = phost->base;
  930. memset(simp_ext_csd, 0, 512);
  931. if (card->csd.mmca_vsn < CSD_SPEC_VER_4) {
  932. pr_debug("MSDC MMCA_VSN: %d. Skip EXT_CSD\n", card->csd.mmca_vsn);
  933. return 0;
  934. }
  935. msdc_clr_fifo(host->mtk_host->id);
  936. simp_msdc_set_blknum(phost, 1);
  937. msdc_set_timeout(phost, 100000000, 0);
  938. err = simp_msdc_cmd(phost, CMD8, CMD8_RAW_EMMC, 0, RESP_R1, &resp);
  939. if (err)
  940. goto out;
  941. err = simp_msdc_pio_read(phost, (unsigned int *)(simp_ext_csd), 512);
  942. if (err) {
  943. pr_err("pio read ext csd error(0x%d)\n", err);
  944. goto out;
  945. }
  946. out:
  947. return err;
  948. }
  949. static void simp_mmc_decode_ext_csd(struct simp_mmc_card *card)
  950. {
  951. card->ext_csd.sectors =
  952. simp_ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
  953. simp_ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
  954. simp_ext_csd[EXT_CSD_SEC_CNT + 2] << 16 | simp_ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  955. }
  956. static int simp_emmc_switch_bus(struct simp_mmc_host *host, struct simp_mmc_card *card)
  957. {
  958. struct simp_msdc_host *phost = host->mtk_host;
  959. unsigned int resp;
  960. return simp_msdc_cmd(phost, ACMD6, ACMD6_RAW_EMMC, ACMD6_ARG_EMMC, RESP_R1B, &resp);
  961. }
  962. static int simp_mmc_init_card(struct simp_mmc_host *host, unsigned int ocr,
  963. struct simp_mmc_card *oldcard)
  964. {
  965. int err = 0;
  966. unsigned int rocr;
  967. unsigned int cid[4];
  968. void __iomem *base;
  969. struct simp_mmc_card *card = host->card;
  970. base = host->mtk_host->base;
  971. /* Set correct bus mode for MMC before attempting init */
  972. simp_mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN); /* NULL func now */
  973. /* Initialization should be done at 3.3 V I/O voltage. */
  974. simp_mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0); /* NULL func now */
  975. /*
  976. * Since we're changing the OCR value, we seem to
  977. * need to tell some cards to go back to the idle
  978. * state. We wait 1ms to give cards time to
  979. * respond.
  980. * mmc_go_idle is needed for eMMC that are asleep
  981. */
  982. simp_mmc_go_idle(host);
  983. /* The extra bit indicates that we support high capacity */
  984. err = simp_mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
  985. if (err)
  986. goto err;
  987. err = simp_mmc_all_send_cid(host, cid);
  988. if (err)
  989. goto err;
  990. card->type = MMC_TYPE_MMC;
  991. card->rca = 1;
  992. host->mtk_host->card->rca = 1;
  993. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  994. /*
  995. * For native busses: set card RCA and quit open drain mode.
  996. */
  997. err = simp_mmc_set_relative_addr(card);
  998. if (err)
  999. goto err;
  1000. simp_mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  1001. /*
  1002. * Fetch CSD from card.
  1003. */
  1004. err = simp_mmc_send_csd(card, card->raw_csd);
  1005. if (err)
  1006. goto err;
  1007. err = simp_mmc_decode_csd(card);
  1008. if (err)
  1009. goto err;
  1010. #if 0
  1011. err = mmc_decode_csd(card);
  1012. if (err)
  1013. goto err;
  1014. err = mmc_decode_cid(card);
  1015. if (err)
  1016. goto err;
  1017. #endif
  1018. err = simp_mmc_select_card(host, card);
  1019. if (err)
  1020. goto err;
  1021. err = simp_mmc_read_ext_csd(host, card);
  1022. if (err)
  1023. goto err;
  1024. simp_mmc_decode_ext_csd(card);
  1025. simp_emmc_cal_offset(card);
  1026. if (simp_offset < 0)
  1027. goto err;
  1028. err = simp_emmc_switch_bus(host, card);
  1029. sdr_set_field(SDC_CFG, SDC_CFG_BUSWIDTH, 1); /* 1: 4 bits mode */
  1030. simp_msdc_config_clock(host->mtk_host, NORMAL_SCLK);
  1031. return SIMP_SUCCESS;
  1032. err:
  1033. return SIMP_FAILED;
  1034. }
  1035. #define ACMD41_RETRY (20)
  1036. static int simp_mmc_sd_init(struct simp_mmc_host *host)
  1037. {
  1038. struct simp_msdc_host *phost = host->mtk_host;
  1039. u32 ACMD41_ARG = 0;
  1040. u8 retry;
  1041. void __iomem *base;
  1042. unsigned int resp = 0;
  1043. int bRet = 0;
  1044. base = phost->base;
  1045. if (simp_msdc_cmd(phost, CMD0, CMD0_RAW, CMD0_ARG, RESP_NONE, &resp))
  1046. goto EXIT;
  1047. if (simp_msdc_cmd(phost, CMD8, CMD8_RAW, CMD8_ARG, RESP_R7, &resp)) {
  1048. /* SD v1.0 will not repsonse to CMD8, then clr HCS bit */
  1049. /*pr_debug("SD v1.0, clr HCS bit\n"); */
  1050. ACMD41_ARG = ACMD41_ARG_10;
  1051. } else if (resp == CMD8_ARG) {
  1052. /*pr_debug("SD v2.0, set HCS bit\n"); */
  1053. ACMD41_ARG = ACMD41_ARG_20;
  1054. }
  1055. retry = ACMD41_RETRY;
  1056. while (retry--) {
  1057. if (simp_msdc_cmd(phost, CMD55, CMD55_RAW, CMD55_ARG << 16, RESP_R1, &resp))
  1058. goto EXIT;
  1059. if (simp_msdc_cmd(phost, ACMD41, ACMD41_RAW, ACMD41_ARG, RESP_R3, &resp))
  1060. goto EXIT;
  1061. if (resp & R3_OCR_POWER_UP_BIT) {
  1062. phost->card->card_cap =
  1063. ((resp & R3_OCR_CARD_CAPACITY_BIT) ? high_capacity : standard_capacity);
  1064. if (phost->card->card_cap == standard_capacity)
  1065. pr_debug("just standard_capacity card!!\r\n");
  1066. break;
  1067. }
  1068. msdc_mdelay(1000 / ACMD41_RETRY);
  1069. }
  1070. if (simp_msdc_cmd(phost, CMD2, CMD2_RAW, CMD2_ARG, RESP_R2, &resp))
  1071. goto EXIT;
  1072. if (simp_msdc_cmd(phost, CMD3, CMD3_RAW, CMD3_ARG, RESP_R6, &resp))
  1073. goto EXIT;
  1074. /* save the rca */
  1075. phost->card->rca = (resp & 0xffff0000) >> 16; /* RCA[31:16] */
  1076. if (simp_msdc_cmd(phost, CMD9, CMD9_RAW, CMD9_ARG << 16, RESP_R2, &resp))
  1077. goto EXIT;
  1078. if (simp_msdc_cmd(phost, CMD13, CMD13_RAW, CMD13_ARG << 16, RESP_R1, &resp))
  1079. goto EXIT;
  1080. if (simp_msdc_cmd(phost, CMD7, CMD7_RAW, CMD7_ARG << 16, RESP_R1, &resp))
  1081. goto EXIT;
  1082. /* dump register for debug */
  1083. msdc_mdelay(10);
  1084. if (simp_msdc_cmd(phost, CMD55, CMD55_RAW, CMD55_ARG << 16, RESP_R1, &resp))
  1085. goto EXIT;
  1086. if (simp_msdc_cmd(phost, ACMD42, ACMD42_RAW, ACMD42_ARG, RESP_R1, &resp))
  1087. goto EXIT;
  1088. if (simp_msdc_cmd(phost, CMD55, CMD55_RAW, CMD55_ARG << 16, RESP_R1, &resp))
  1089. goto EXIT;
  1090. if (simp_msdc_cmd(phost, ACMD6, ACMD6_RAW, ACMD6_ARG, RESP_R1, &resp))
  1091. goto EXIT;
  1092. /* set host bus width to 4 */
  1093. sdr_set_field(SDC_CFG, SDC_CFG_BUSWIDTH, 1); /* 1: 4 bits mode */
  1094. simp_msdc_config_clock(phost, NORMAL_SCLK);
  1095. #if MTK_MMC_DUMP_DBG
  1096. pr_debug("sd card inited\n");
  1097. #endif
  1098. bRet = 1;
  1099. EXIT:
  1100. return bRet;
  1101. }
  1102. #ifdef MTK_MSDC_USE_CACHE
  1103. static int mmc_disable_cache(struct simp_mmc_host *host)
  1104. {
  1105. int err = 0;
  1106. unsigned int resp;
  1107. unsigned int status = 0;
  1108. int polling = MAX_POLLING_STATUS;
  1109. struct simp_msdc_host *phost = host->mtk_host;
  1110. do {
  1111. err = simp_mmc_get_status(host, &status);
  1112. if (err)
  1113. return -1;
  1114. if (R1_CURRENT_STATE(status) == 5 || R1_CURRENT_STATE(status) == 6)
  1115. simp_mmc_send_stop(host);
  1116. } while (R1_CURRENT_STATE(status) == 7 && polling--);
  1117. if (R1_CURRENT_STATE(status) == 7)
  1118. return -2;
  1119. err = simp_msdc_cmd(phost, ACMD6, ACMD6_RAW_EMMC, ACMD6_ARG_DISABLE_CACHE, RESP_R1B, &resp);
  1120. if (!err) {
  1121. polling = MAX_POLLING_STATUS;
  1122. do {
  1123. err = simp_mmc_get_status(host, &status);
  1124. if (err)
  1125. return -3;
  1126. } while (R1_CURRENT_STATE(status) == 7 && polling--);
  1127. if (status & 0xFDFFA000)
  1128. pr_err("msdc unexpected status 0x%x after switch", status);
  1129. if (status & R1_SWITCH_ERROR)
  1130. return -4;
  1131. }
  1132. return err;
  1133. }
  1134. #endif
  1135. static unsigned int simple_mmc_attach_sd(struct simp_mmc_host *host)
  1136. {
  1137. /* int err = SIMP_FAILED; */
  1138. /* power up host */
  1139. simp_mmc_power_up(host, 0);
  1140. msdc_mdelay(20);
  1141. simp_mmc_power_up(host, 1);
  1142. /* enable clock */
  1143. if (SIMP_FAILED == simp_mmc_enable_clk(host))
  1144. return SIMP_FAILED;
  1145. /*
  1146. * Some eMMCs (with VCCQ always on) may not be reset after power up, so
  1147. * do a hardware reset if possible.
  1148. */
  1149. simp_mmc_hw_reset_for_init(host);
  1150. /* power up card: Initialization should be done at 3.3 V I/O voltage. */
  1151. simp_mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
  1152. /* init msdc host */
  1153. simp_msdc_init(host);
  1154. simp_mmc_sd_init(host);
  1155. return SIMP_SUCCESS;
  1156. }
  1157. /* make clk & power always on */
  1158. static unsigned int simple_mmc_attach_mmc(struct simp_mmc_host *host)
  1159. {
  1160. int err = 0;
  1161. unsigned int ocr;
  1162. #ifdef MTK_MSDC_USE_CACHE
  1163. g_power_reset = 0;
  1164. /* turn off cache will trigger flushing of the cache data to non-volatile storage */
  1165. if (!mtk_msdc_host[host->index] ||
  1166. !mtk_msdc_host[host->index]->mmc || !mtk_msdc_host[host->index]->mmc->card) {
  1167. pr_err("[%s]: host/mmc/card is not existed\n", __func__);
  1168. } else if (mtk_msdc_host[host->index]->mmc->card->ext_csd.cache_ctrl & 0x1) {
  1169. /* enable clock */
  1170. if (SIMP_FAILED == simp_mmc_enable_clk(host))
  1171. return SIMP_FAILED;
  1172. /* init msdc host */
  1173. simp_msdc_init(host);
  1174. err = mmc_disable_cache(host);
  1175. if (err) {
  1176. pr_err("[%s]: failed to disable cache ops, err = %d\n", __func__, err);
  1177. simp_msdc_dump_register(host->mtk_host);
  1178. err = 0;
  1179. } else {
  1180. #if MTK_MMC_DUMP_DBG
  1181. pr_debug("[%s]: successfully disabled cache ops.\n", __func__);
  1182. #endif
  1183. }
  1184. } else {
  1185. #if MTK_MMC_DUMP_DBG
  1186. pr_debug("[%s]: cache is not enabled, no need to disable it\n", __func__);
  1187. #endif
  1188. }
  1189. #endif
  1190. /* power up host */
  1191. simp_mmc_power_up(host, 1);
  1192. msdc_mdelay(10);
  1193. #ifdef MTK_MSDC_USE_CACHE
  1194. g_power_reset = 1;
  1195. #endif
  1196. /*
  1197. * Some eMMCs (with VCCQ always on) may not be reset after power up, so
  1198. * do a hardware reset if possible.
  1199. */
  1200. simp_mmc_hw_reset_for_init(host);
  1201. /* power up card: Initialization should be done at 3.3 V I/O voltage. */
  1202. simp_mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
  1203. /* enable clock */
  1204. if (SIMP_FAILED == simp_mmc_enable_clk(host))
  1205. return SIMP_FAILED;
  1206. /* init msdc host */
  1207. simp_msdc_init(host);
  1208. /*=================== begin to init emmc card =======================*/
  1209. /* Set correct bus mode for MMC before attempting attach */
  1210. simp_mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
  1211. simp_mmc_go_idle(host);
  1212. err = simp_mmc_send_op_cond(host, 0, &ocr);
  1213. /*
  1214. * Sanity check the voltages that the card claims to
  1215. * support.
  1216. */
  1217. if (ocr & 0x7F) {
  1218. #if MTK_MMC_DUMP_DBG
  1219. pr_err
  1220. ("msdc0: card claims to support voltages below the defined range. These will be ignored.\n");
  1221. #endif
  1222. ocr &= ~0x7F;
  1223. }
  1224. host->ocr = simp_mmc_select_voltage(host, ocr);
  1225. /*
  1226. * Can we support the voltage of the card?
  1227. */
  1228. if (!host->ocr) {
  1229. pr_err("msdc0: card voltage not support\n");
  1230. err = -EINVAL;
  1231. goto err;
  1232. }
  1233. /*
  1234. * Detect and init the card.
  1235. */
  1236. err = simp_mmc_init_card(host, host->ocr, NULL);
  1237. if (err == SIMP_FAILED) {
  1238. pr_err("init eMMC failed\n");
  1239. goto err;
  1240. }
  1241. #if MTK_MMC_DUMP_DBG
  1242. pr_debug("init eMMC success\n");
  1243. #endif
  1244. /*=================== end mmc card init =============================*/
  1245. return SIMP_SUCCESS;
  1246. err:
  1247. return SIMP_FAILED;
  1248. }
  1249. static const unsigned g_freqs[] = { 300000, 260000, 200000, 100000 };
  1250. #define HOST_MIN_MCLK (260000)
  1251. static int emmc_init;
  1252. static int sd_init;
  1253. /* not use freq para */
  1254. static unsigned int simp_mmc_rescan_try_freq(struct simp_mmc_host *host, unsigned freq)
  1255. {
  1256. int err = SIMP_FAILED;
  1257. /* sd/emmc will support */
  1258. if (host->mtk_host->card->type == MMC_TYPE_MMC) {
  1259. #if MTK_MMC_DUMP_DBG
  1260. pr_debug("init emmc for ipanic dump\n");
  1261. #endif
  1262. err = simple_mmc_attach_mmc(host);
  1263. } else if (host->mtk_host->card->type == MMC_TYPE_SD) {
  1264. #if MTK_MMC_DUMP_DBG
  1265. pr_debug("init sd card\n");
  1266. #endif
  1267. err = simple_mmc_attach_sd(host);
  1268. }
  1269. return err;
  1270. }
  1271. static unsigned int simp_init_emmc(void)
  1272. {
  1273. int i = 0;
  1274. int ret = 0;
  1275. if (0 == module_init_emmc) {
  1276. simp_msdc_hw_init();
  1277. #ifdef CONFIG_MTK_EMMC_SUPPORT
  1278. if (SIMP_FAILED == simp_mmc_init(MSDC_EMMC, 1)) {
  1279. pr_err("init eMMC Failed ,line:%d\n", __LINE__);
  1280. ret = 1;
  1281. goto out;
  1282. }
  1283. #endif
  1284. }
  1285. for (i = 0; i < ARRAY_SIZE(g_freqs); i++) {
  1286. if (SIMP_SUCCESS ==
  1287. simp_mmc_rescan_try_freq(pmmc_boot_host,
  1288. max_t(unsigned, g_freqs[i], HOST_MIN_MCLK))) {
  1289. break;
  1290. }
  1291. if (g_freqs[i] <= HOST_MIN_MCLK) {
  1292. pr_err("failed to init eMMC, line:%d\n", __LINE__);
  1293. ret = 1;
  1294. }
  1295. }
  1296. if (0 == ret)
  1297. emmc_init = 1;
  1298. out:
  1299. return ret;
  1300. }
  1301. static unsigned int simp_init_sd(void)
  1302. {
  1303. int i = 0;
  1304. int ret = 0;
  1305. if (0 == module_init_sd) {
  1306. simp_msdc_hw_init();
  1307. if (SIMP_FAILED == simp_mmc_init(MSDC_SD, 0)) {
  1308. pr_err("init SD Failed ,line:%d\n", __LINE__);
  1309. ret = 1;
  1310. goto out;
  1311. }
  1312. }
  1313. for (i = 0; i < ARRAY_SIZE(g_freqs); i++) {
  1314. if (SIMP_SUCCESS ==
  1315. simp_mmc_rescan_try_freq(pmmc_extend_host,
  1316. max_t(unsigned, g_freqs[i], HOST_MIN_MCLK))) {
  1317. break;
  1318. }
  1319. if (g_freqs[i] <= HOST_MIN_MCLK) {
  1320. pr_err("failed to init eMMC, line:%d\n", __LINE__);
  1321. ret = 1;
  1322. }
  1323. }
  1324. if (0 == ret)
  1325. sd_init = 1;
  1326. out:
  1327. return ret;
  1328. }
  1329. unsigned int reset_boot_up_device(int type)
  1330. {
  1331. int ret = 0;
  1332. if (type == MMC_TYPE_MMC)
  1333. ret = simp_init_emmc();
  1334. else if (type == MMC_TYPE_SD)
  1335. ret = simp_init_sd();
  1336. else
  1337. ret = 1;
  1338. return ret;
  1339. }
  1340. EXPORT_SYMBOL(reset_boot_up_device);
  1341. #define MSDC_FIFO_SZ (128)
  1342. #define MSDC_FIFO_THD (64) /* (128) */
  1343. #define msdc_txfifocnt() ((sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_TXCNT) >> 16)
  1344. #define msdc_rxfifocnt() ((sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_RXCNT) >> 0)
  1345. #define msdc_fifo_write32(v) sdr_write32(MSDC_TXDATA, (v))
  1346. #define msdc_fifo_write8(v) sdr_write8(MSDC_TXDATA, (v))
  1347. #define msdc_fifo_read32() sdr_read32(MSDC_RXDATA)
  1348. #define msdc_fifo_read8() sdr_read8(MSDC_RXDATA)
  1349. static int simp_msdc_pio_write(struct simp_msdc_host *host, unsigned int *ptr, unsigned int size)
  1350. {
  1351. void __iomem *base = host->base;
  1352. unsigned int left = size;
  1353. unsigned int status = 0;
  1354. unsigned char *u8ptr;
  1355. int l_count = 0;
  1356. int err = 0;
  1357. int print_count = 2;
  1358. while (1) {
  1359. status = sdr_read32(MSDC_INT);
  1360. sdr_write32(MSDC_INT, status);
  1361. if (status & MSDC_INT_DATCRCERR) {
  1362. pr_err("[MSDC%d] DAT CRC error (0x%x), Left DAT: %d bytes\n",
  1363. host->id, status, left);
  1364. err = -5;
  1365. simp_msdc_dump_register(host);
  1366. break;
  1367. } else if (status & MSDC_INT_DATTMO) {
  1368. pr_err("[MSDC%d] DAT TMO error (0x%x), Left DAT: %d bytes\n",
  1369. host->id, status, left);
  1370. err = -110;
  1371. simp_msdc_dump_register(host);
  1372. break;
  1373. } else if (status & MSDC_INT_XFER_COMPL) {
  1374. break;
  1375. }
  1376. if (left == 0)
  1377. continue;
  1378. if ((left >= MSDC_FIFO_SZ) && (msdc_txfifocnt() == 0)) {
  1379. int count = MSDC_FIFO_SZ >> 2;
  1380. do {
  1381. msdc_fifo_write32(*ptr);
  1382. ptr++;
  1383. } while (--count);
  1384. left -= MSDC_FIFO_SZ;
  1385. } else if (left < MSDC_FIFO_SZ && msdc_txfifocnt() == 0) {
  1386. while (left > 3) {
  1387. msdc_fifo_write32(*ptr);
  1388. ptr++;
  1389. left -= 4;
  1390. }
  1391. u8ptr = (u8 *) ptr;
  1392. while (left) {
  1393. msdc_fifo_write8(*u8ptr);
  1394. u8ptr++;
  1395. left--;
  1396. }
  1397. } else {
  1398. status = sdr_read32(MSDC_INT);
  1399. if ((status & MSDC_INT_DATCRCERR) || (status & MSDC_INT_DATTMO)) {
  1400. if (status & MSDC_INT_DATCRCERR) {
  1401. pr_err
  1402. ("[MSDC%d] DAT CRC error (0x%x), Left DAT: %d bytes\n",
  1403. host->id, status, left);
  1404. err = -5;
  1405. }
  1406. if (status & MSDC_INT_DATTMO) {
  1407. pr_err
  1408. ("[MSDC%d] DAT TMO error (0x%x), Left DAT: %d bytes\n",
  1409. host->id, status, left);
  1410. err = -110;
  1411. }
  1412. simp_msdc_dump_register(host);
  1413. sdr_write32(MSDC_INT, status);
  1414. msdc_reset_hw(host->id);
  1415. return err;
  1416. }
  1417. }
  1418. l_count++;
  1419. if (l_count > 500) {
  1420. l_count = 0;
  1421. if (print_count > 0) {
  1422. pr_err("size= %d, left= %d.\r\n", size, left);
  1423. simp_msdc_dump_register(host);
  1424. print_count--;
  1425. }
  1426. }
  1427. }
  1428. return err;
  1429. }
  1430. static int simp_msdc_pio_read(struct simp_msdc_host *host, unsigned int *ptr, unsigned int size)
  1431. {
  1432. void __iomem *base = host->base;
  1433. unsigned int left = size;
  1434. unsigned int status = 0;
  1435. unsigned char *u8ptr;
  1436. int l_count = 0;
  1437. int err = 0;
  1438. int print_count = 2;
  1439. int done = 0;
  1440. while (1) {
  1441. status = sdr_read32(MSDC_INT);
  1442. sdr_write32(MSDC_INT, status);
  1443. if (status & MSDC_INT_DATCRCERR) {
  1444. pr_err("[MSDC%d] DAT CRC error (0x%x), Left DAT: %d bytes\n",
  1445. host->id, status, left);
  1446. err = -5;
  1447. simp_msdc_dump_register(host);
  1448. break;
  1449. } else if (status & MSDC_INT_DATTMO) {
  1450. pr_err("[MSDC%d] DAT TMO error (0x%x), Left DAT: %d bytes\n",
  1451. host->id, status, left);
  1452. err = -110;
  1453. simp_msdc_dump_register(host);
  1454. break;
  1455. } else if (status & MSDC_INT_XFER_COMPL) {
  1456. done = 1;
  1457. }
  1458. if (done && (left == 0))
  1459. break;
  1460. while (left) {
  1461. /* pr_err("left(%d)/FIFO(%d)\n", left,msdc_rxfifocnt()); */
  1462. if ((left >= MSDC_FIFO_THD) && (msdc_rxfifocnt() >= MSDC_FIFO_THD)) {
  1463. int count = MSDC_FIFO_THD >> 2;
  1464. do {
  1465. *ptr++ = msdc_fifo_read32();
  1466. } while (--count);
  1467. left -= MSDC_FIFO_THD;
  1468. } else if ((left < MSDC_FIFO_THD) && msdc_rxfifocnt() >= left) {
  1469. while (left > 3) {
  1470. *ptr++ = msdc_fifo_read32();
  1471. left -= 4;
  1472. }
  1473. u8ptr = (u8 *) ptr;
  1474. while (left) {
  1475. *u8ptr++ = msdc_fifo_read8();
  1476. left--;
  1477. }
  1478. } else {
  1479. status = sdr_read32(MSDC_INT);
  1480. if ((status & MSDC_INT_DATCRCERR) || (status & MSDC_INT_DATTMO)) {
  1481. if (status & MSDC_INT_DATCRCERR) {
  1482. pr_err
  1483. ("[MSDC%d] DAT CRC error (0x%x), Left DAT: %d bytes\n",
  1484. host->id, status, left);
  1485. err = -5;
  1486. }
  1487. if (status & MSDC_INT_DATTMO) {
  1488. pr_err
  1489. ("[MSDC%d] DAT TMO error (0x%x), Left DAT: %d bytes\n",
  1490. host->id, status, left);
  1491. err = -110;
  1492. }
  1493. simp_msdc_dump_register(host);
  1494. sdr_write32(MSDC_INT, status);
  1495. msdc_reset_hw(host->id);
  1496. return err;
  1497. }
  1498. }
  1499. /* timeout monitor */
  1500. l_count++;
  1501. if (l_count > 50000) {
  1502. l_count = 0;
  1503. if (print_count > 0) {
  1504. pr_err("size= %d, left= %d, done=%d. \r\n", size, left,
  1505. done);
  1506. simp_msdc_dump_register(host);
  1507. print_count--;
  1508. }
  1509. }
  1510. }
  1511. }
  1512. return err;
  1513. }
  1514. static void simp_msdc_set_blknum(struct simp_msdc_host *host, unsigned int blknum)
  1515. {
  1516. void __iomem *base = host->base;
  1517. sdr_write32(SDC_BLK_NUM, blknum);
  1518. }
  1519. static int simp_mmc_single_write(struct simp_mmc_host *host, unsigned int addr, void *buf,
  1520. unsigned int size)
  1521. {
  1522. unsigned int resp = 0;
  1523. unsigned int err = 0;
  1524. /* unsigned int intsts = 0; */
  1525. struct simp_msdc_host *phost = host->mtk_host;
  1526. void __iomem *base = phost->base;
  1527. if (size != 512) {
  1528. pr_err("emmc: write para error!\n");
  1529. return -1;
  1530. }
  1531. simp_msdc_set_blknum(phost, 1);
  1532. /* send command */
  1533. err = simp_msdc_cmd(phost, CMD24, CMD24_RAW, addr, RESP_R1, &resp);
  1534. /* write the data to FIFO */
  1535. err = simp_msdc_pio_write(phost, (unsigned int *)buf, 512);
  1536. if (err)
  1537. pr_err("write data: error(%d)\n", err);
  1538. /* make sure contents in fifo flushed to device */
  1539. BUG_ON(msdc_txfifocnt());
  1540. /* check and clear interrupt */
  1541. /* while ((intsts & MSDC_INT_XFER_COMPL) == 0 ) { */
  1542. /* intsts = sdr_read32(MSDC_INT); */
  1543. /* } */
  1544. /* sdr_set_bits(MSDC_INT, MSDC_INT_XFER_COMPL); */
  1545. return err;
  1546. }
  1547. static int simp_mmc_single_read(struct simp_mmc_host *host, unsigned int addr, void *buf,
  1548. unsigned int size)
  1549. {
  1550. unsigned int resp = 0;
  1551. unsigned int err = 0;
  1552. /* unsigned int intsts = 0; */
  1553. struct simp_msdc_host *phost = host->mtk_host;
  1554. /* unsigned int base = phost->base; */
  1555. if (size != 512) {
  1556. pr_err("emmc: read para error!\n");
  1557. return -1;
  1558. }
  1559. simp_msdc_set_blknum(phost, 1);
  1560. /* send command */
  1561. err = simp_msdc_cmd(phost, CMD17, CMD17_RAW, addr, RESP_R1, &resp);
  1562. /* read the data out */
  1563. err = simp_msdc_pio_read(phost, (unsigned int *)buf, 512);
  1564. if (err)
  1565. pr_err("read data: error(%d)\n", err);
  1566. /* check and clear interrupt */
  1567. /* while ((intsts & MSDC_INT_XFER_COMPL) == 0 ) { */
  1568. /* intsts = sdr_read32(MSDC_INT); */
  1569. /* } */
  1570. /* sdr_set_bits(MSDC_INT, MSDC_INT_XFER_COMPL); */
  1571. return err;
  1572. }
  1573. static int simp_mmc_multi_write(struct simp_mmc_host *host, unsigned int addr, void *buf,
  1574. unsigned int nblk)
  1575. {
  1576. unsigned int resp = 0;
  1577. unsigned int err = 0;
  1578. struct simp_msdc_host *phost = host->mtk_host;
  1579. void __iomem *base = phost->base;
  1580. simp_msdc_set_blknum(phost, nblk);
  1581. /* send command */
  1582. err = simp_msdc_cmd(phost, CMD25, CMD25_RAW, addr, RESP_R1, &resp);
  1583. /* write the data to FIFO */
  1584. err = simp_msdc_pio_write(phost, (unsigned int *)buf, 512 * nblk);
  1585. if (err)
  1586. pr_err("write data: error(%d)\n", err);
  1587. /* make sure contents in fifo flushed to device */
  1588. BUG_ON(msdc_txfifocnt());
  1589. /* check and clear interrupt */
  1590. simp_mmc_send_stop(host);
  1591. return err;
  1592. }
  1593. static int simp_mmc_multi_read(struct simp_mmc_host *host, unsigned int addr, void *buf,
  1594. unsigned int nblk)
  1595. {
  1596. unsigned int resp = 0;
  1597. unsigned int err = 0;
  1598. struct simp_msdc_host *phost = host->mtk_host;
  1599. simp_msdc_set_blknum(phost, nblk);
  1600. /* send command */
  1601. err = simp_msdc_cmd(phost, CMD18, CMD18_RAW, addr, RESP_R1, &resp);
  1602. /* read the data out */
  1603. err = simp_msdc_pio_read(phost, (unsigned int *)buf, 512 * nblk);
  1604. if (err)
  1605. pr_err("read data: error(%d)\n", err);
  1606. simp_mmc_send_stop(host);
  1607. return err;
  1608. }
  1609. /* card_type tell to use which host, will support PANIC dump info to emmc card
  1610. * and KDUMP info to sd card */
  1611. int msdc_init_panic(int dev)
  1612. {
  1613. return 1;
  1614. }
  1615. static int simp_mmc_get_host(int card_type, bool boot)
  1616. {
  1617. int index = 0;
  1618. for (; index < 2; ++index) {
  1619. if (p_msdc_hw[index]) {
  1620. if ((card_type == p_msdc_hw[index]->host_function)
  1621. && (boot == p_msdc_hw[index]->boot))
  1622. return index;
  1623. }
  1624. }
  1625. return HOST_MAX_NUM;
  1626. }
  1627. static int simp_mmc_init(int card_type, bool boot)
  1628. {
  1629. struct simp_mmc_host *host;
  1630. if (boot) {
  1631. /* init some struct */
  1632. pmmc_boot_host->mtk_host = pmsdc_boot_host;
  1633. pmmc_boot_host->card = &g_mmc_card[BOOT_STORAGE_ID];
  1634. pmmc_boot_host->card->host = pmmc_boot_host;
  1635. host = pmmc_boot_host;
  1636. memset(pmmc_boot_host->mtk_host, 0, sizeof(struct simp_msdc_host));
  1637. pmmc_boot_host->mtk_host->id = simp_mmc_get_host(card_type, boot);
  1638. if (pmmc_boot_host->mtk_host->id >= HOST_MAX_NUM)
  1639. goto out;
  1640. if (NULL == mtk_msdc_host[pmmc_boot_host->mtk_host->id])
  1641. goto out;
  1642. #ifdef CONFIG_OF
  1643. pmmc_boot_host->mtk_host->base =
  1644. (mtk_msdc_host[pmmc_boot_host->mtk_host->id])->base;
  1645. pr_notice("msdc @ 0x%p, id:%d\n", pmmc_boot_host->mtk_host->base,
  1646. pmmc_boot_host->mtk_host->id);
  1647. #else
  1648. pmmc_boot_host->mtk_host->base = u_msdc_base[pmmc_boot_host->mtk_host->id];
  1649. #endif
  1650. pmmc_boot_host->mtk_host->clksrc = MSDC_CLKSRC;
  1651. pmmc_boot_host->mtk_host->clk = clks[MSDC_CLKSRC];
  1652. pmmc_boot_host->mtk_host->card = &g_msdc_card[BOOT_STORAGE_ID];
  1653. /* not use now, may be delete */
  1654. memset(&g_msdc_card[BOOT_STORAGE_ID], 0, sizeof(struct simp_msdc_card));
  1655. g_msdc_card[BOOT_STORAGE_ID].type = MMC_TYPE_MMC;
  1656. g_msdc_card[BOOT_STORAGE_ID].file_system = _RAW_;
  1657. /* init host & card */
  1658. module_init_emmc = 1;
  1659. } else {
  1660. pmmc_extend_host->mtk_host = pmsdc_extend_host;
  1661. pmmc_extend_host->card = &g_mmc_card[EXTEND_STORAGE_ID];
  1662. pmmc_extend_host->card->host = pmmc_extend_host;
  1663. host = pmmc_extend_host;
  1664. memset(pmmc_extend_host->mtk_host, 0, sizeof(struct simp_msdc_host));
  1665. pmmc_extend_host->mtk_host->id = simp_mmc_get_host(card_type, boot);
  1666. if (pmmc_extend_host->mtk_host->id >= HOST_MAX_NUM)
  1667. goto out;
  1668. if (NULL == mtk_msdc_host[pmmc_extend_host->mtk_host->id])
  1669. goto out;
  1670. #ifdef CONFIG_OF
  1671. pmmc_extend_host->mtk_host->base =
  1672. (mtk_msdc_host[pmmc_extend_host->mtk_host->id])->base;
  1673. pr_notice("msdc @ 0x%p, id:%d\n", pmmc_extend_host->mtk_host->base,
  1674. pmmc_extend_host->mtk_host->id);
  1675. #else
  1676. pmmc_extend_host->mtk_host->base = u_msdc_base[pmmc_extend_host->mtk_host->id];
  1677. #endif
  1678. pmmc_extend_host->mtk_host->clksrc = MSDC_CLKSRC;
  1679. pmmc_extend_host->mtk_host->clk = clks[MSDC_CLKSRC];
  1680. pmmc_extend_host->mtk_host->card = &g_msdc_card[EXTEND_STORAGE_ID];
  1681. /* not use now, may be delete */
  1682. memset(&g_msdc_card[EXTEND_STORAGE_ID], 0, sizeof(struct simp_msdc_card));
  1683. g_msdc_card[EXTEND_STORAGE_ID].type = MMC_TYPE_SD;
  1684. g_msdc_card[EXTEND_STORAGE_ID].file_system = FAT32;
  1685. pmsdc_extend_host->card->card_cap = 1;
  1686. module_init_sd = 0;
  1687. }
  1688. return SIMP_SUCCESS;
  1689. out:
  1690. return SIMP_FAILED;
  1691. }
  1692. /*--------------------------------------------------------------------------*/
  1693. /* porting for panic dump interface */
  1694. /*--------------------------------------------------------------------------*/
  1695. #ifdef CONFIG_MTK_GPT_SCHEME_SUPPORT
  1696. static sector_t lp_start_sect = (sector_t) (-1);
  1697. static sector_t lp_nr_sects = (sector_t) (-1);
  1698. #endif
  1699. #ifdef CONFIG_MTK_EMMC_SUPPORT
  1700. #ifdef CONFIG_MTK_GPT_SCHEME_SUPPORT
  1701. static int simp_emmc_dump_write(unsigned char *buf, unsigned int len,
  1702. unsigned int offset, unsigned int dev)
  1703. {
  1704. /* maybe delete in furture */
  1705. unsigned int i;
  1706. unsigned int status = 0;
  1707. int polling = MAX_POLLING_STATUS;
  1708. unsigned long long l_start_offset;
  1709. unsigned int l_addr;
  1710. unsigned char *l_buf;
  1711. unsigned int ret = 1; /* != 0 means error occur */
  1712. int err = 0;
  1713. static int force;
  1714. if (0 != len % 512) {
  1715. /* emmc always in slot0 */
  1716. pr_err("debug: parameter error!\n");
  1717. return ret;
  1718. }
  1719. /* find the offset in emmc */
  1720. if (lp_start_sect == (sector_t) (-1) || lp_nr_sects == (sector_t) (-1)) {
  1721. pr_err("not find in scatter file error!\n");
  1722. return ret;
  1723. }
  1724. if (lp_nr_sects < (len >> 9)) {
  1725. pr_err("write operation oversize!\n");
  1726. return ret;
  1727. }
  1728. if (lp_nr_sects < (offset >> 9)) {
  1729. pr_err("write operation oversize!\n");
  1730. return ret;
  1731. }
  1732. if (lp_nr_sects < ((len + offset) >> 9)) {
  1733. pr_err("write operation oversize!\n");
  1734. return ret;
  1735. }
  1736. #if MTK_MMC_DUMP_DBG
  1737. pr_debug("write start sector = %llu, part size = %llu\n", (u64) lp_start_sect,
  1738. (u64) lp_nr_sects);
  1739. #endif
  1740. l_start_offset = (u64) offset + (u64) (lp_start_sect << 9);
  1741. #if MTK_MMC_DUMP_DBG
  1742. pr_debug("write start address = %llu\n", l_start_offset);
  1743. #endif
  1744. if (force == 0) {
  1745. simp_init_emmc();
  1746. force = 1;
  1747. }
  1748. for (i = 0; i < (len / 512); i++) {
  1749. /* code */
  1750. l_addr = (l_start_offset >> 9) + i; /*blk address */
  1751. l_buf = (buf + i * 512);
  1752. #if MTK_MMC_DUMP_DBG
  1753. pr_debug("l_start_offset =0x%x\n", l_addr);
  1754. #endif
  1755. /* add address check over expdb for each block */
  1756. if (l_addr >= (lp_start_sect + lp_nr_sects)) {
  1757. pr_err("write 512 Bytes address over boundary at 0x%x\n", l_addr);
  1758. return ret;
  1759. }
  1760. err = simp_mmc_single_write(pmmc_boot_host, l_addr, l_buf, 512);
  1761. if (err) {
  1762. pr_err("write 512 Bytes fail at 0x%x\n", l_addr);
  1763. return ret;
  1764. }
  1765. do {
  1766. simp_mmc_get_status(pmmc_boot_host, &status);
  1767. } while (R1_CURRENT_STATE(status) == 7 && polling--);
  1768. }
  1769. if (err == 0)
  1770. return 0;
  1771. else
  1772. return ret;
  1773. }
  1774. #else
  1775. static int simp_emmc_dump_write(unsigned char *buf, unsigned int len, unsigned int offset,
  1776. unsigned int dev)
  1777. {
  1778. /* maybe delete in furture */
  1779. unsigned int i;
  1780. unsigned int status = 0;
  1781. int polling = MAX_POLLING_STATUS;
  1782. unsigned int l_user_begin_num = 0;
  1783. unsigned int l_dest_num = 0;
  1784. unsigned long long l_start_offset;
  1785. unsigned int l_addr;
  1786. unsigned char *l_buf;
  1787. unsigned int ret = 1;
  1788. int err = 0;
  1789. if (0 != len % 512) {
  1790. /* emmc always in slot0 */
  1791. pr_err("debug: parameter error!\n");
  1792. return ret;
  1793. }
  1794. #if 0
  1795. pr_debug("write data:");
  1796. for (i = 0; i < 32; i++) {
  1797. pr_debug("0x%x", buf[i]);
  1798. if (0 == (i + 1) % 32)
  1799. pr_debug("\n");
  1800. }
  1801. #endif
  1802. /* find the offset in emmc */
  1803. for (i = 0; i < PART_NUM; i++) {
  1804. if ('m' == *(PartInfo[i].name) && 'b' == *(PartInfo[i].name + 1) &&
  1805. 'r' == *(PartInfo[i].name + 2)) {
  1806. l_user_begin_num = i;
  1807. }
  1808. if ('e' == *(PartInfo[i].name) && 'x' == *(PartInfo[i].name + 1) &&
  1809. 'p' == *(PartInfo[i].name + 2) && 'd' == *(PartInfo[i].name + 3) &&
  1810. 'b' == *(PartInfo[i].name + 4)) {
  1811. l_dest_num = i;
  1812. }
  1813. }
  1814. if (l_user_begin_num >= PART_NUM && l_dest_num >= PART_NUM) {
  1815. pr_err("not find in scatter file error!\n");
  1816. return ret;
  1817. }
  1818. if (PartInfo[l_dest_num].size < (len + offset)) {
  1819. pr_err("write operation oversize!\n");
  1820. return ret;
  1821. }
  1822. #if MTK_MMC_DUMP_DBG
  1823. pr_debug("write start address=%llu\n",
  1824. PartInfo[l_dest_num].start_address - PartInfo[l_user_begin_num].start_address);
  1825. #endif
  1826. l_start_offset =
  1827. (u64) offset + PartInfo[l_dest_num].start_address -
  1828. PartInfo[l_user_begin_num].start_address;
  1829. if (emmc_init == 0) {
  1830. if (simp_init_emmc() != 0)
  1831. return ret;
  1832. }
  1833. for (i = 0; i < (len / 512); i++) {
  1834. /* code */
  1835. l_addr = (l_start_offset >> 9) + i; /* blk address */
  1836. l_buf = (buf + i * 512);
  1837. #if MTK_MMC_DUMP_DBG
  1838. pr_debug("l_start_offset = 0x%x\n", l_addr);
  1839. #endif
  1840. err = simp_mmc_single_write(pmmc_boot_host, l_addr, l_buf, 512);
  1841. do {
  1842. simp_mmc_get_status(pmmc_boot_host, &status);
  1843. } while (R1_CURRENT_STATE(status) == 7 && polling--);
  1844. }
  1845. if (err == 0)
  1846. return 0;
  1847. else
  1848. return ret;
  1849. }
  1850. #endif /* end of CONFIG_MTK_EMMC_SUPPORT */
  1851. #endif /* end of CONFIG_MTK_EMMC_SUPPORT */
  1852. static int simp_sd_dump_write(unsigned char *buf, unsigned int len, unsigned int offset,
  1853. unsigned int dev)
  1854. {
  1855. /* unsigned int i; */
  1856. unsigned int l_addr;
  1857. unsigned char *l_buf;
  1858. int polling = MAX_POLLING_STATUS;
  1859. unsigned int status = 0;
  1860. /* unsigned int l_start_offset; */
  1861. unsigned int ret = SIMP_FAILED;
  1862. int err = 0;
  1863. if (0 != len % 512) {
  1864. /* emmc always in slot0 */
  1865. pr_err("debug: parameter error!\n");
  1866. return ret;
  1867. }
  1868. #if 0
  1869. pr_debug("write data:");
  1870. for (i = 0; i < 32; i++) {
  1871. pr_debug("0x%x", buf[i]);
  1872. if (0 == (i + 1) % 32)
  1873. pr_debug("\n");
  1874. }
  1875. #endif
  1876. /* l_start_offset = offset; */
  1877. l_buf = buf;
  1878. if (pmsdc_extend_host->card->card_cap == standard_capacity)
  1879. l_addr = offset << 9;
  1880. else
  1881. l_addr = offset;
  1882. #if MTK_MMC_DUMP_DBG
  1883. pr_err("l_start_offset = 0x%x len = %d buf<0x%p>\n", l_addr, len, l_buf);
  1884. #endif
  1885. if (len == 512)
  1886. err = simp_mmc_single_write(pmmc_extend_host, l_addr, l_buf, 512);
  1887. else
  1888. err = simp_mmc_multi_write(pmmc_extend_host, l_addr, l_buf, len / 512);
  1889. do {
  1890. simp_mmc_get_status(pmmc_extend_host, &status);
  1891. } while (R1_CURRENT_STATE(status) == 7 && polling--);
  1892. if (err == 0)
  1893. ret = SIMP_SUCCESS;
  1894. return ret;
  1895. }
  1896. static int sd_dump_read(unsigned char *buf, unsigned int len, unsigned int offset)
  1897. {
  1898. /* unsigned int i; */
  1899. unsigned int l_addr;
  1900. unsigned char *l_buf;
  1901. /* unsigned int l_start_offset; */
  1902. unsigned int ret = SIMP_FAILED;
  1903. int err = 0;
  1904. #if MTK_MMC_DUMP_DBG
  1905. pr_debug("1 l_start_offset = 0x%x len =0%x\n", l_addr, len);
  1906. #endif
  1907. if (0 != len % 512) {
  1908. pr_err("debug: parameter error!\n");
  1909. return ret;
  1910. }
  1911. if (sd_init == 0) {
  1912. if (simp_init_sd() != 0)
  1913. return ret;
  1914. }
  1915. /* l_start_offset = offset; */
  1916. l_buf = buf;
  1917. #if MTK_MMC_DUMP_DBG
  1918. pr_debug("l_start_offset = 0x%x len = %d\n", offset, len);
  1919. #endif
  1920. if (pmsdc_extend_host->card->card_cap == standard_capacity)
  1921. l_addr = offset << 9;
  1922. else
  1923. l_addr = offset;
  1924. if (len == 512)
  1925. err = simp_mmc_single_read(pmmc_extend_host, l_addr, l_buf, 512);
  1926. else
  1927. err = simp_mmc_multi_read(pmmc_extend_host, l_addr, l_buf, len / 512);
  1928. #if 0
  1929. pr_debug("read data:");
  1930. for (i = 0; i < 32; i++) {
  1931. pr_debug("0x%x", buf[i]);
  1932. if (0 == (i + 1) % 32)
  1933. pr_debug("\n");
  1934. }
  1935. #endif
  1936. if (err == 0)
  1937. ret = SIMP_SUCCESS;
  1938. return ret;
  1939. }
  1940. int card_dump_func_write(unsigned char *buf, unsigned int len, unsigned long long offset, int dev)
  1941. {
  1942. int ret = SIMP_FAILED;
  1943. /* local_irq_disable(); */
  1944. /* preempt_disable(); */
  1945. unsigned int sec_offset = 0;
  1946. #if MTK_MMC_DUMP_DBG
  1947. pr_debug("card_dump_func_write len<%d> addr<%lld> type<%d>\n", len, offset, dev);
  1948. #endif
  1949. if (offset % 512) {
  1950. pr_err("Address isn't 512 alignment!\n");
  1951. return SIMP_FAILED;
  1952. }
  1953. sec_offset = offset / 512;
  1954. switch (dev) {
  1955. case DUMP_INTO_BOOT_CARD_IPANIC:
  1956. #ifdef CONFIG_MTK_EMMC_SUPPORT
  1957. ret = simp_emmc_dump_write(buf, len, (unsigned int)offset, dev);
  1958. #endif
  1959. break;
  1960. case DUMP_INTO_BOOT_CARD_KDUMP:
  1961. break;
  1962. case DUMP_INTO_EXTERN_CARD:
  1963. ret = simp_sd_dump_write(buf, len, sec_offset, dev);
  1964. break;
  1965. default:
  1966. pr_err("unknown card type, error!\n");
  1967. break;
  1968. }
  1969. return ret;
  1970. }
  1971. EXPORT_SYMBOL(card_dump_func_write);
  1972. #ifdef CONFIG_MTK_EMMC_SUPPORT
  1973. #define SD_FALSE (-1)
  1974. #define SD_TRUE (0)
  1975. #define DEBUG_MMC_IOCTL (0)
  1976. #ifdef CONFIG_MTK_GPT_SCHEME_SUPPORT
  1977. static int emmc_dump_read(unsigned char *buf, unsigned int len, unsigned int offset,
  1978. unsigned int slot)
  1979. {
  1980. /* maybe delete in furture */
  1981. struct msdc_ioctl msdc_ctl;
  1982. unsigned int i;
  1983. unsigned long long l_start_offset = 0;
  1984. unsigned int ret = SD_FALSE;
  1985. if ((0 != slot) || (0 != offset % 512) || (0 != len % 512)) {
  1986. /* emmc always in slot0 */
  1987. pr_err("debug: slot is not use for emmc!\n");
  1988. return ret;
  1989. }
  1990. if (0 != len % 512) {
  1991. /* emmc always in slot0 */
  1992. pr_err("debug: parameter error!\n");
  1993. return ret;
  1994. }
  1995. /* find the offset in emmc */
  1996. if (lp_start_sect == (sector_t) (-1) || lp_nr_sects == (sector_t) (-1)) {
  1997. pr_err("not find in scatter file error!\n");
  1998. return ret;
  1999. }
  2000. if (lp_nr_sects < ((len + offset) >> 9)) {
  2001. pr_err("write operation oversize!\n");
  2002. return ret;
  2003. }
  2004. #if MTK_MMC_DUMP_DBG
  2005. pr_debug("read start sector = %llu, part size = %llu\n", (u64) lp_start_sect,
  2006. (u64) lp_nr_sects);
  2007. #endif
  2008. l_start_offset = (u64) offset + (u64) (lp_start_sect << 9);
  2009. #if MTK_MMC_DUMP_DBG
  2010. pr_debug("read start address = %llu\n", l_start_offset);
  2011. #endif
  2012. msdc_ctl.partition = 0;
  2013. msdc_ctl.iswrite = 0;
  2014. msdc_ctl.host_num = slot;
  2015. msdc_ctl.opcode = MSDC_CARD_DUNM_FUNC;
  2016. msdc_ctl.total_size = MAX_DMA_CNT;
  2017. msdc_ctl.trans_type = 0;
  2018. msdc_ctl.address = l_start_offset >> 9;
  2019. msdc_ctl.buffer = (u32 *) buf;
  2020. for (i = 0; i < (len / MAX_DMA_CNT); i++) {
  2021. #if DEBUG_MMC_IOCTL
  2022. pr_debug("l_start_offset = 0x%x\n", msdc_ctl.address);
  2023. #endif
  2024. msdc_ctl.result = simple_sd_ioctl_rw(&msdc_ctl);
  2025. msdc_ctl.address += MAX_DMA_CNT >> 9;
  2026. msdc_ctl.buffer = (u32 *) (buf + MAX_DMA_CNT * (i + 1));
  2027. }
  2028. msdc_ctl.total_size = len % MAX_DMA_CNT;
  2029. if (0 != msdc_ctl.total_size) {
  2030. #if DEBUG_MMC_IOCTL
  2031. pr_debug("l_start_offset = 0x%x\n", msdc_ctl.address);
  2032. #endif
  2033. msdc_ctl.result = simple_sd_ioctl_rw(&msdc_ctl);
  2034. }
  2035. #if DEBUG_MMC_IOCTL
  2036. pr_debug("read data:");
  2037. for (i = 0; i < 32; i++) {
  2038. pr_debug("0x%x", buf[i]);
  2039. if (0 == (i + 1) % 32)
  2040. pr_debug("\n");
  2041. }
  2042. #endif
  2043. return SD_TRUE;
  2044. }
  2045. #else
  2046. static int emmc_dump_read(unsigned char *buf, unsigned int len, unsigned int offset,
  2047. unsigned int slot)
  2048. {
  2049. /* maybe delete in furture */
  2050. struct msdc_ioctl msdc_ctl;
  2051. unsigned int i;
  2052. unsigned int l_user_begin_num = 0;
  2053. unsigned int l_dest_num = 0;
  2054. unsigned long long l_start_offset = 0;
  2055. unsigned int ret = SD_FALSE;
  2056. if ((0 != slot) || (0 != offset % 512) || (0 != len % 512)) {
  2057. /* emmc always in slot0 */
  2058. pr_err("debug: slot is not use for emmc!\n");
  2059. return ret;
  2060. }
  2061. /* find the offset in emmc */
  2062. for (i = 0; i < PART_NUM; i++) {
  2063. /* for (i = 0; i < 1; i++) { */
  2064. if ('m' == *(PartInfo[i].name) && 'b' == *(PartInfo[i].name + 1) &&
  2065. 'r' == *(PartInfo[i].name + 2)) {
  2066. l_user_begin_num = i;
  2067. }
  2068. if ('e' == *(PartInfo[i].name) && 'x' == *(PartInfo[i].name + 1) &&
  2069. 'p' == *(PartInfo[i].name + 2) && 'd' == *(PartInfo[i].name + 3) &&
  2070. 'b' == *(PartInfo[i].name + 4)) {
  2071. l_dest_num = i;
  2072. }
  2073. }
  2074. #if DEBUG_MMC_IOCTL
  2075. pr_debug("l_user_begin_num = %d l_dest_num = %d\n", l_user_begin_num, l_dest_num);
  2076. #endif
  2077. if (l_user_begin_num >= PART_NUM && l_dest_num >= PART_NUM) {
  2078. pr_err("not find in scatter file error!\n");
  2079. return ret;
  2080. }
  2081. if (PartInfo[l_dest_num].size < (len + offset)) {
  2082. pr_err("read operation oversize!\n");
  2083. return ret;
  2084. }
  2085. #if DEBUG_MMC_IOCTL
  2086. pr_debug("read start address=0x%llx\n",
  2087. PartInfo[l_dest_num].start_address - PartInfo[l_user_begin_num].start_address);
  2088. #endif
  2089. l_start_offset =
  2090. offset + PartInfo[l_dest_num].start_address - PartInfo[l_user_begin_num].start_address;
  2091. msdc_ctl.partition = 0;
  2092. msdc_ctl.iswrite = 0;
  2093. msdc_ctl.host_num = slot;
  2094. msdc_ctl.opcode = MSDC_CARD_DUNM_FUNC;
  2095. msdc_ctl.total_size = 512;
  2096. msdc_ctl.trans_type = 0;
  2097. for (i = 0; i < (len / 512); i++) {
  2098. /* code */
  2099. msdc_ctl.address = (l_start_offset >> 9) + i; /* blk address */
  2100. msdc_ctl.buffer = (u32 *) (buf + i * 512);
  2101. #if DEBUG_MMC_IOCTL
  2102. pr_debug("l_start_offset = 0x%x\n", msdc_ctl.address);
  2103. #endif
  2104. msdc_ctl.result = simple_sd_ioctl_rw(&msdc_ctl);
  2105. }
  2106. #if DEBUG_MMC_IOCTL
  2107. pr_debug("read data:");
  2108. for (i = 0; i < 32; i++) {
  2109. pr_debug("0x%x", buf[i]);
  2110. if (0 == (i + 1) % 32)
  2111. pr_debug("\n");
  2112. }
  2113. #endif
  2114. return SD_TRUE;
  2115. }
  2116. #endif
  2117. #endif
  2118. int card_dump_func_read(unsigned char *buf, unsigned int len, unsigned long long offset, int dev)
  2119. {
  2120. /* unsigned int l_slot; */
  2121. unsigned int ret = SIMP_FAILED;
  2122. unsigned int sec_offset = 0;
  2123. #if MTK_MMC_DUMP_DBG
  2124. pr_debug("card_dump_func_read len<%d> addr<%lld> type<%d>\n", len, offset, dev);
  2125. #endif
  2126. if (offset % 512) {
  2127. pr_err("Address isn't 512 alignment!\n");
  2128. return SIMP_FAILED;
  2129. }
  2130. sec_offset = offset / 512;
  2131. switch (dev) {
  2132. case DUMP_INTO_BOOT_CARD_IPANIC:
  2133. #ifdef CONFIG_MTK_EMMC_SUPPORT
  2134. ret = emmc_dump_read(buf, len, (unsigned int)offset, dev);
  2135. #endif
  2136. break;
  2137. case DUMP_INTO_BOOT_CARD_KDUMP:
  2138. break;
  2139. case DUMP_INTO_EXTERN_CARD:
  2140. ret = sd_dump_read(buf, len, sec_offset);
  2141. break;
  2142. default:
  2143. pr_err("unknown card type, error!\n");
  2144. break;
  2145. }
  2146. return ret;
  2147. }
  2148. EXPORT_SYMBOL(card_dump_func_read);
  2149. int has_mt_dump_support(void)
  2150. {
  2151. return 1;
  2152. }
  2153. EXPORT_SYMBOL(has_mt_dump_support);
  2154. /*--------------------------------------------------------------------------*/
  2155. /* porting for kdump interface */
  2156. /*--------------------------------------------------------------------------*/
  2157. /*--------------------------------------------------------------------------*/
  2158. /* module init/exit */
  2159. /*--------------------------------------------------------------------------*/
  2160. static void simp_msdc_hw_init(void)
  2161. {
  2162. if (mtk_msdc_host[0])
  2163. p_msdc_hw[0] = mtk_msdc_host[0]->hw;
  2164. if (mtk_msdc_host[1])
  2165. p_msdc_hw[1] = mtk_msdc_host[1]->hw;
  2166. }
  2167. static int __init emmc_dump_init(void)
  2168. {
  2169. simp_msdc_hw_init();
  2170. #ifdef CONFIG_MTK_EMMC_SUPPORT
  2171. simp_mmc_init(MSDC_EMMC, 1);
  2172. #endif
  2173. simp_mmc_init(MSDC_SD, 0);
  2174. #if MTK_MMC_DUMP_DBG
  2175. pr_debug("EMMC/SD dump init\n");
  2176. #endif
  2177. return 0;
  2178. }
  2179. static void __exit emmc_dump_exit(void)
  2180. {
  2181. }
  2182. module_init(emmc_dump_init);
  2183. module_exit(emmc_dump_exit);
  2184. #ifdef CONFIG_MTK_EMMC_SUPPORT
  2185. #ifdef CONFIG_MTK_GPT_SCHEME_SUPPORT
  2186. /* @partition_ready_flag,
  2187. * = 0: partition init not ready
  2188. * = 1: partition init is done and succeed
  2189. * = -1: there is no expdb partition
  2190. */
  2191. static int partition_ready_flag;
  2192. int get_emmc_dump_status(void)
  2193. {
  2194. return partition_ready_flag;
  2195. }
  2196. void get_emmc_dump_info(struct work_struct *work)
  2197. {
  2198. struct hd_struct *lp_hd_struct = NULL;
  2199. lp_hd_struct = get_part("expdb");
  2200. if (likely(lp_hd_struct)) {
  2201. lp_start_sect = lp_hd_struct->start_sect;
  2202. lp_nr_sects = lp_hd_struct->nr_sects;
  2203. put_part(lp_hd_struct);
  2204. partition_ready_flag = 1;
  2205. pr_err("get expdb info\n");
  2206. } else {
  2207. lp_start_sect = (sector_t) (-1);
  2208. lp_nr_sects = (sector_t) (-1);
  2209. partition_ready_flag = -1;
  2210. pr_err("There is no expdb info\n");
  2211. }
  2212. partition_ready_flag = 1;
  2213. }
  2214. static struct delayed_work get_dump_info;
  2215. static int __init init_get_dump_work(void)
  2216. {
  2217. INIT_DELAYED_WORK(&get_dump_info, get_emmc_dump_info);
  2218. schedule_delayed_work(&get_dump_info, 100);
  2219. return 0;
  2220. }
  2221. late_initcall_sync(init_get_dump_work);
  2222. #endif
  2223. #endif