sd.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. *
  10. * MediaTek Inc. (C) 2010. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. *
  31. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver's
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #include <linux/module.h>
  36. #include <linux/delay.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/of.h>
  42. #include <linux/mmc/host.h>
  43. #include <linux/mmc/mmc.h>
  44. #include <linux/mmc/sd.h>
  45. #include <linux/mmc/sdio.h>
  46. #include <asm/mach-ralink/ralink_regs.h>
  47. #include "board.h"
  48. #include "dbg.h"
  49. #include "mt6575_sd.h"
  50. //#define IRQ_SDC 14 //MT7620 /*FIXME*/
  51. #ifdef CONFIG_SOC_MT7621
  52. #define RALINK_SYSCTL_BASE 0xbe000000
  53. #define RALINK_MSDC_BASE 0xbe130000
  54. #else
  55. #define RALINK_SYSCTL_BASE 0xb0000000
  56. #define RALINK_MSDC_BASE 0xb0130000
  57. #endif
  58. #define IRQ_SDC 22 /*FIXME*/
  59. #define DRV_NAME "mtk-sd"
  60. #if defined(CONFIG_SOC_MT7620)
  61. #define HOST_MAX_MCLK (48000000) /* +/- by chhung */
  62. #elif defined(CONFIG_SOC_MT7621)
  63. #define HOST_MAX_MCLK (50000000) /* +/- by chhung */
  64. #endif
  65. #define HOST_MIN_MCLK (260000)
  66. #define HOST_MAX_BLKSZ (2048)
  67. #define MSDC_OCR_AVAIL (MMC_VDD_28_29 | MMC_VDD_29_30 | MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33)
  68. #define GPIO_PULL_DOWN (0)
  69. #define GPIO_PULL_UP (1)
  70. #if 0 /* --- by chhung */
  71. #define MSDC_CLKSRC_REG (0xf100000C)
  72. #define PDN_REG (0xF1000010)
  73. #endif /* end of --- */
  74. #define DEFAULT_DEBOUNCE (8) /* 8 cycles */
  75. #define DEFAULT_DTOC (40) /* data timeout counter. 65536x40 sclk. */
  76. #define CMD_TIMEOUT (HZ / 10) /* 100ms */
  77. #define DAT_TIMEOUT (HZ / 2 * 5) /* 500ms x5 */
  78. #define MAX_DMA_CNT (64 * 1024 - 512) /* a single transaction for WIFI may be 50K*/
  79. #define MAX_GPD_NUM (1 + 1) /* one null gpd */
  80. #define MAX_BD_NUM (1024)
  81. #define MAX_BD_PER_GPD (MAX_BD_NUM)
  82. #define MAX_HW_SGMTS (MAX_BD_NUM)
  83. #define MAX_PHY_SGMTS (MAX_BD_NUM)
  84. #define MAX_SGMT_SZ (MAX_DMA_CNT)
  85. #define MAX_REQ_SZ (MAX_SGMT_SZ * 8)
  86. static int cd_active_low = 1;
  87. //=================================
  88. #define PERI_MSDC0_PDN (15)
  89. //#define PERI_MSDC1_PDN (16)
  90. //#define PERI_MSDC2_PDN (17)
  91. //#define PERI_MSDC3_PDN (18)
  92. #if 0 /* --- by chhung */
  93. /* gate means clock power down */
  94. static int g_clk_gate = 0;
  95. #define msdc_gate_clock(id) \
  96. do { \
  97. g_clk_gate &= ~(1 << ((id) + PERI_MSDC0_PDN)); \
  98. } while (0)
  99. /* not like power down register. 1 means clock on. */
  100. #define msdc_ungate_clock(id) \
  101. do { \
  102. g_clk_gate |= 1 << ((id) + PERI_MSDC0_PDN); \
  103. } while (0)
  104. // do we need sync object or not
  105. void msdc_clk_status(int *status)
  106. {
  107. *status = g_clk_gate;
  108. }
  109. #endif /* end of --- */
  110. /* +++ by chhung */
  111. struct msdc_hw msdc0_hw = {
  112. .clk_src = 0,
  113. .flags = MSDC_CD_PIN_EN | MSDC_REMOVABLE,
  114. // .flags = MSDC_WP_PIN_EN | MSDC_CD_PIN_EN | MSDC_REMOVABLE,
  115. };
  116. /* end of +++ */
  117. static int msdc_rsp[] = {
  118. 0, /* RESP_NONE */
  119. 1, /* RESP_R1 */
  120. 2, /* RESP_R2 */
  121. 3, /* RESP_R3 */
  122. 4, /* RESP_R4 */
  123. 1, /* RESP_R5 */
  124. 1, /* RESP_R6 */
  125. 1, /* RESP_R7 */
  126. 7, /* RESP_R1b */
  127. };
  128. #define msdc_txfifocnt() ((sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_TXCNT) >> 16)
  129. #define msdc_rxfifocnt() ((sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_RXCNT) >> 0)
  130. #define msdc_fifo_write32(v) sdr_write32(MSDC_TXDATA, (v))
  131. #define msdc_fifo_write8(v) sdr_write8(MSDC_TXDATA, (v))
  132. #define msdc_fifo_read32() sdr_read32(MSDC_RXDATA)
  133. #define msdc_fifo_read8() sdr_read8(MSDC_RXDATA)
  134. #define msdc_dma_on() sdr_clr_bits(MSDC_CFG, MSDC_CFG_PIO)
  135. #define msdc_retry(expr, retry, cnt) \
  136. do { \
  137. int backup = cnt; \
  138. while (retry) { \
  139. if (!(expr)) \
  140. break; \
  141. if (cnt-- == 0) { \
  142. retry--; mdelay(1); cnt = backup; \
  143. } \
  144. } \
  145. WARN_ON(retry == 0); \
  146. } while (0)
  147. static void msdc_reset_hw(struct msdc_host *host)
  148. {
  149. void __iomem *base = host->base;
  150. sdr_set_bits(MSDC_CFG, MSDC_CFG_RST);
  151. while (sdr_read32(MSDC_CFG) & MSDC_CFG_RST)
  152. cpu_relax();
  153. }
  154. #define msdc_clr_int() \
  155. do { \
  156. volatile u32 val = sdr_read32(MSDC_INT); \
  157. sdr_write32(MSDC_INT, val); \
  158. } while (0)
  159. #define msdc_clr_fifo() \
  160. do { \
  161. int retry = 3, cnt = 1000; \
  162. sdr_set_bits(MSDC_FIFOCS, MSDC_FIFOCS_CLR); \
  163. msdc_retry(sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_CLR, retry, cnt); \
  164. } while (0)
  165. #define msdc_irq_save(val) \
  166. do { \
  167. val = sdr_read32(MSDC_INTEN); \
  168. sdr_clr_bits(MSDC_INTEN, val); \
  169. } while (0)
  170. #define msdc_irq_restore(val) \
  171. do { \
  172. sdr_set_bits(MSDC_INTEN, val); \
  173. } while (0)
  174. /* clock source for host: global */
  175. #if defined(CONFIG_SOC_MT7620)
  176. static u32 hclks[] = {48000000}; /* +/- by chhung */
  177. #elif defined(CONFIG_SOC_MT7621)
  178. static u32 hclks[] = {50000000}; /* +/- by chhung */
  179. #endif
  180. //============================================
  181. // the power for msdc host controller: global
  182. // always keep the VMC on.
  183. //============================================
  184. #define msdc_vcore_on(host) \
  185. do { \
  186. INIT_MSG("[+]VMC ref. count<%d>", ++host->pwr_ref); \
  187. (void)hwPowerOn(MT65XX_POWER_LDO_VMC, VOL_3300, "SD"); \
  188. } while (0)
  189. #define msdc_vcore_off(host) \
  190. do { \
  191. INIT_MSG("[-]VMC ref. count<%d>", --host->pwr_ref); \
  192. (void)hwPowerDown(MT65XX_POWER_LDO_VMC, "SD"); \
  193. } while (0)
  194. //====================================
  195. // the vdd output for card: global
  196. // always keep the VMCH on.
  197. //====================================
  198. #define msdc_vdd_on(host) \
  199. do { \
  200. (void)hwPowerOn(MT65XX_POWER_LDO_VMCH, VOL_3300, "SD"); \
  201. } while (0)
  202. #define msdc_vdd_off(host) \
  203. do { \
  204. (void)hwPowerDown(MT65XX_POWER_LDO_VMCH, "SD"); \
  205. } while (0)
  206. #define sdc_is_busy() (sdr_read32(SDC_STS) & SDC_STS_SDCBUSY)
  207. #define sdc_is_cmd_busy() (sdr_read32(SDC_STS) & SDC_STS_CMDBUSY)
  208. #define sdc_send_cmd(cmd, arg) \
  209. do { \
  210. sdr_write32(SDC_ARG, (arg)); \
  211. sdr_write32(SDC_CMD, (cmd)); \
  212. } while (0)
  213. // can modify to read h/w register.
  214. //#define is_card_present(h) ((sdr_read32(MSDC_PS) & MSDC_PS_CDSTS) ? 0 : 1);
  215. #define is_card_present(h) (((struct msdc_host *)(h))->card_inserted)
  216. /* +++ by chhung */
  217. #ifndef __ASSEMBLY__
  218. #define PHYSADDR(a) (((unsigned long)(a)) & 0x1fffffff)
  219. #else
  220. #define PHYSADDR(a) ((a) & 0x1fffffff)
  221. #endif
  222. /* end of +++ */
  223. static unsigned int msdc_do_command(struct msdc_host *host,
  224. struct mmc_command *cmd,
  225. int tune,
  226. unsigned long timeout);
  227. static int msdc_tune_cmdrsp(struct msdc_host *host, struct mmc_command *cmd);
  228. #ifdef MT6575_SD_DEBUG
  229. static void msdc_dump_card_status(struct msdc_host *host, u32 status)
  230. {
  231. /* N_MSG is currently a no-op */
  232. #if 0
  233. static char *state[] = {
  234. "Idle", /* 0 */
  235. "Ready", /* 1 */
  236. "Ident", /* 2 */
  237. "Stby", /* 3 */
  238. "Tran", /* 4 */
  239. "Data", /* 5 */
  240. "Rcv", /* 6 */
  241. "Prg", /* 7 */
  242. "Dis", /* 8 */
  243. "Reserved", /* 9 */
  244. "Reserved", /* 10 */
  245. "Reserved", /* 11 */
  246. "Reserved", /* 12 */
  247. "Reserved", /* 13 */
  248. "Reserved", /* 14 */
  249. "I/O mode", /* 15 */
  250. };
  251. #endif
  252. if (status & R1_OUT_OF_RANGE)
  253. N_MSG(RSP, "[CARD_STATUS] Out of Range");
  254. if (status & R1_ADDRESS_ERROR)
  255. N_MSG(RSP, "[CARD_STATUS] Address Error");
  256. if (status & R1_BLOCK_LEN_ERROR)
  257. N_MSG(RSP, "[CARD_STATUS] Block Len Error");
  258. if (status & R1_ERASE_SEQ_ERROR)
  259. N_MSG(RSP, "[CARD_STATUS] Erase Seq Error");
  260. if (status & R1_ERASE_PARAM)
  261. N_MSG(RSP, "[CARD_STATUS] Erase Param");
  262. if (status & R1_WP_VIOLATION)
  263. N_MSG(RSP, "[CARD_STATUS] WP Violation");
  264. if (status & R1_CARD_IS_LOCKED)
  265. N_MSG(RSP, "[CARD_STATUS] Card is Locked");
  266. if (status & R1_LOCK_UNLOCK_FAILED)
  267. N_MSG(RSP, "[CARD_STATUS] Lock/Unlock Failed");
  268. if (status & R1_COM_CRC_ERROR)
  269. N_MSG(RSP, "[CARD_STATUS] Command CRC Error");
  270. if (status & R1_ILLEGAL_COMMAND)
  271. N_MSG(RSP, "[CARD_STATUS] Illegal Command");
  272. if (status & R1_CARD_ECC_FAILED)
  273. N_MSG(RSP, "[CARD_STATUS] Card ECC Failed");
  274. if (status & R1_CC_ERROR)
  275. N_MSG(RSP, "[CARD_STATUS] CC Error");
  276. if (status & R1_ERROR)
  277. N_MSG(RSP, "[CARD_STATUS] Error");
  278. if (status & R1_UNDERRUN)
  279. N_MSG(RSP, "[CARD_STATUS] Underrun");
  280. if (status & R1_OVERRUN)
  281. N_MSG(RSP, "[CARD_STATUS] Overrun");
  282. if (status & R1_CID_CSD_OVERWRITE)
  283. N_MSG(RSP, "[CARD_STATUS] CID/CSD Overwrite");
  284. if (status & R1_WP_ERASE_SKIP)
  285. N_MSG(RSP, "[CARD_STATUS] WP Eraser Skip");
  286. if (status & R1_CARD_ECC_DISABLED)
  287. N_MSG(RSP, "[CARD_STATUS] Card ECC Disabled");
  288. if (status & R1_ERASE_RESET)
  289. N_MSG(RSP, "[CARD_STATUS] Erase Reset");
  290. if (status & R1_READY_FOR_DATA)
  291. N_MSG(RSP, "[CARD_STATUS] Ready for Data");
  292. if (status & R1_SWITCH_ERROR)
  293. N_MSG(RSP, "[CARD_STATUS] Switch error");
  294. if (status & R1_APP_CMD)
  295. N_MSG(RSP, "[CARD_STATUS] App Command");
  296. N_MSG(RSP, "[CARD_STATUS] '%s' State", state[R1_CURRENT_STATE(status)]);
  297. }
  298. static void msdc_dump_ocr_reg(struct msdc_host *host, u32 resp)
  299. {
  300. if (resp & (1 << 7))
  301. N_MSG(RSP, "[OCR] Low Voltage Range");
  302. if (resp & (1 << 15))
  303. N_MSG(RSP, "[OCR] 2.7-2.8 volt");
  304. if (resp & (1 << 16))
  305. N_MSG(RSP, "[OCR] 2.8-2.9 volt");
  306. if (resp & (1 << 17))
  307. N_MSG(RSP, "[OCR] 2.9-3.0 volt");
  308. if (resp & (1 << 18))
  309. N_MSG(RSP, "[OCR] 3.0-3.1 volt");
  310. if (resp & (1 << 19))
  311. N_MSG(RSP, "[OCR] 3.1-3.2 volt");
  312. if (resp & (1 << 20))
  313. N_MSG(RSP, "[OCR] 3.2-3.3 volt");
  314. if (resp & (1 << 21))
  315. N_MSG(RSP, "[OCR] 3.3-3.4 volt");
  316. if (resp & (1 << 22))
  317. N_MSG(RSP, "[OCR] 3.4-3.5 volt");
  318. if (resp & (1 << 23))
  319. N_MSG(RSP, "[OCR] 3.5-3.6 volt");
  320. if (resp & (1 << 24))
  321. N_MSG(RSP, "[OCR] Switching to 1.8V Accepted (S18A)");
  322. if (resp & (1 << 30))
  323. N_MSG(RSP, "[OCR] Card Capacity Status (CCS)");
  324. if (resp & (1 << 31))
  325. N_MSG(RSP, "[OCR] Card Power Up Status (Idle)");
  326. else
  327. N_MSG(RSP, "[OCR] Card Power Up Status (Busy)");
  328. }
  329. static void msdc_dump_rca_resp(struct msdc_host *host, u32 resp)
  330. {
  331. u32 status = (((resp >> 15) & 0x1) << 23) |
  332. (((resp >> 14) & 0x1) << 22) |
  333. (((resp >> 13) & 0x1) << 19) |
  334. (resp & 0x1fff);
  335. N_MSG(RSP, "[RCA] 0x%.4x", resp >> 16);
  336. msdc_dump_card_status(host, status);
  337. }
  338. static void msdc_dump_io_resp(struct msdc_host *host, u32 resp)
  339. {
  340. u32 flags = (resp >> 8) & 0xFF;
  341. #if 0
  342. char *state[] = {"DIS", "CMD", "TRN", "RFU"};
  343. #endif
  344. if (flags & (1 << 7))
  345. N_MSG(RSP, "[IO] COM_CRC_ERR");
  346. if (flags & (1 << 6))
  347. N_MSG(RSP, "[IO] Illgal command");
  348. if (flags & (1 << 3))
  349. N_MSG(RSP, "[IO] Error");
  350. if (flags & (1 << 2))
  351. N_MSG(RSP, "[IO] RFU");
  352. if (flags & (1 << 1))
  353. N_MSG(RSP, "[IO] Function number error");
  354. if (flags & (1 << 0))
  355. N_MSG(RSP, "[IO] Out of range");
  356. N_MSG(RSP, "[IO] State: %s, Data:0x%x", state[(resp >> 12) & 0x3], resp & 0xFF);
  357. }
  358. #endif
  359. static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks)
  360. {
  361. void __iomem *base = host->base;
  362. u32 timeout, clk_ns;
  363. host->timeout_ns = ns;
  364. host->timeout_clks = clks;
  365. clk_ns = 1000000000UL / host->sclk;
  366. timeout = ns / clk_ns + clks;
  367. timeout = timeout >> 16; /* in 65536 sclk cycle unit */
  368. timeout = timeout > 1 ? timeout - 1 : 0;
  369. timeout = timeout > 255 ? 255 : timeout;
  370. sdr_set_field(SDC_CFG, SDC_CFG_DTOC, timeout);
  371. N_MSG(OPS, "Set read data timeout: %dns %dclks -> %d x 65536 cycles",
  372. ns, clks, timeout + 1);
  373. }
  374. static void msdc_tasklet_card(struct work_struct *work)
  375. {
  376. struct msdc_host *host = (struct msdc_host *)container_of(work,
  377. struct msdc_host, card_delaywork.work);
  378. void __iomem *base = host->base;
  379. u32 inserted;
  380. u32 status = 0;
  381. //u32 change = 0;
  382. spin_lock(&host->lock);
  383. status = sdr_read32(MSDC_PS);
  384. if (cd_active_low)
  385. inserted = (status & MSDC_PS_CDSTS) ? 0 : 1;
  386. else
  387. inserted = (status & MSDC_PS_CDSTS) ? 1 : 0;
  388. if (host->mmc->caps & MMC_CAP_NEEDS_POLL)
  389. inserted = 1;
  390. #if 0
  391. change = host->card_inserted ^ inserted;
  392. host->card_inserted = inserted;
  393. if (change && !host->suspend) {
  394. if (inserted)
  395. host->mmc->f_max = HOST_MAX_MCLK; // work around
  396. mmc_detect_change(host->mmc, msecs_to_jiffies(20));
  397. }
  398. #else /* Make sure: handle the last interrupt */
  399. host->card_inserted = inserted;
  400. if (!host->suspend) {
  401. host->mmc->f_max = HOST_MAX_MCLK;
  402. mmc_detect_change(host->mmc, msecs_to_jiffies(20));
  403. }
  404. IRQ_MSG("card found<%s>", inserted ? "inserted" : "removed");
  405. #endif
  406. spin_unlock(&host->lock);
  407. }
  408. #if 0 /* --- by chhung */
  409. /* For E2 only */
  410. static u8 clk_src_bit[4] = {
  411. 0, 3, 5, 7
  412. };
  413. static void msdc_select_clksrc(struct msdc_host *host, unsigned char clksrc)
  414. {
  415. u32 val;
  416. void __iomem *base = host->base;
  417. BUG_ON(clksrc > 3);
  418. INIT_MSG("set clock source to <%d>", clksrc);
  419. val = sdr_read32(MSDC_CLKSRC_REG);
  420. if (sdr_read32(MSDC_ECO_VER) >= 4) {
  421. val &= ~(0x3 << clk_src_bit[host->id]);
  422. val |= clksrc << clk_src_bit[host->id];
  423. } else {
  424. val &= ~0x3; val |= clksrc;
  425. }
  426. sdr_write32(MSDC_CLKSRC_REG, val);
  427. host->hclk = hclks[clksrc];
  428. host->hw->clk_src = clksrc;
  429. }
  430. #endif /* end of --- */
  431. static void msdc_set_mclk(struct msdc_host *host, int ddr, unsigned int hz)
  432. {
  433. //struct msdc_hw *hw = host->hw;
  434. void __iomem *base = host->base;
  435. u32 mode;
  436. u32 flags;
  437. u32 div;
  438. u32 sclk;
  439. u32 hclk = host->hclk;
  440. //u8 clksrc = hw->clk_src;
  441. if (!hz) { // set mmc system clock to 0 ?
  442. //ERR_MSG("set mclk to 0!!!");
  443. msdc_reset_hw(host);
  444. return;
  445. }
  446. msdc_irq_save(flags);
  447. if (ddr) {
  448. mode = 0x2; /* ddr mode and use divisor */
  449. if (hz >= (hclk >> 2)) {
  450. div = 1; /* mean div = 1/4 */
  451. sclk = hclk >> 2; /* sclk = clk / 4 */
  452. } else {
  453. div = (hclk + ((hz << 2) - 1)) / (hz << 2);
  454. sclk = (hclk >> 2) / div;
  455. }
  456. } else if (hz >= hclk) { /* bug fix */
  457. mode = 0x1; /* no divisor and divisor is ignored */
  458. div = 0;
  459. sclk = hclk;
  460. } else {
  461. mode = 0x0; /* use divisor */
  462. if (hz >= (hclk >> 1)) {
  463. div = 0; /* mean div = 1/2 */
  464. sclk = hclk >> 1; /* sclk = clk / 2 */
  465. } else {
  466. div = (hclk + ((hz << 2) - 1)) / (hz << 2);
  467. sclk = (hclk >> 2) / div;
  468. }
  469. }
  470. /* set clock mode and divisor */
  471. sdr_set_field(MSDC_CFG, MSDC_CFG_CKMOD, mode);
  472. sdr_set_field(MSDC_CFG, MSDC_CFG_CKDIV, div);
  473. /* wait clock stable */
  474. while (!(sdr_read32(MSDC_CFG) & MSDC_CFG_CKSTB))
  475. cpu_relax();
  476. host->sclk = sclk;
  477. host->mclk = hz;
  478. msdc_set_timeout(host, host->timeout_ns, host->timeout_clks); // need?
  479. INIT_MSG("================");
  480. INIT_MSG("!!! Set<%dKHz> Source<%dKHz> -> sclk<%dKHz>", hz / 1000, hclk / 1000, sclk / 1000);
  481. INIT_MSG("================");
  482. msdc_irq_restore(flags);
  483. }
  484. /* Fix me. when need to abort */
  485. static void msdc_abort_data(struct msdc_host *host)
  486. {
  487. void __iomem *base = host->base;
  488. struct mmc_command *stop = host->mrq->stop;
  489. ERR_MSG("Need to Abort.");
  490. msdc_reset_hw(host);
  491. msdc_clr_fifo();
  492. msdc_clr_int();
  493. // need to check FIFO count 0 ?
  494. if (stop) { /* try to stop, but may not success */
  495. ERR_MSG("stop when abort CMD<%d>", stop->opcode);
  496. (void)msdc_do_command(host, stop, 0, CMD_TIMEOUT);
  497. }
  498. //if (host->mclk >= 25000000) {
  499. // msdc_set_mclk(host, 0, host->mclk >> 1);
  500. //}
  501. }
  502. #if 0 /* --- by chhung */
  503. static void msdc_pin_config(struct msdc_host *host, int mode)
  504. {
  505. struct msdc_hw *hw = host->hw;
  506. void __iomem *base = host->base;
  507. int pull = (mode == MSDC_PIN_PULL_UP) ? GPIO_PULL_UP : GPIO_PULL_DOWN;
  508. /* Config WP pin */
  509. if (hw->flags & MSDC_WP_PIN_EN) {
  510. if (hw->config_gpio_pin) /* NULL */
  511. hw->config_gpio_pin(MSDC_WP_PIN, pull);
  512. }
  513. switch (mode) {
  514. case MSDC_PIN_PULL_UP:
  515. //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPU, 1); /* Check & FIXME */
  516. //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPD, 0); /* Check & FIXME */
  517. sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPU, 1);
  518. sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPD, 0);
  519. sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPU, 1);
  520. sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPD, 0);
  521. break;
  522. case MSDC_PIN_PULL_DOWN:
  523. //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPU, 0); /* Check & FIXME */
  524. //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPD, 1); /* Check & FIXME */
  525. sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPU, 0);
  526. sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPD, 1);
  527. sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPU, 0);
  528. sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPD, 1);
  529. break;
  530. case MSDC_PIN_PULL_NONE:
  531. default:
  532. //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPU, 0); /* Check & FIXME */
  533. //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPD, 0); /* Check & FIXME */
  534. sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPU, 0);
  535. sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPD, 0);
  536. sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPU, 0);
  537. sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPD, 0);
  538. break;
  539. }
  540. N_MSG(CFG, "Pins mode(%d), down(%d), up(%d)",
  541. mode, MSDC_PIN_PULL_DOWN, MSDC_PIN_PULL_UP);
  542. }
  543. void msdc_pin_reset(struct msdc_host *host, int mode)
  544. {
  545. struct msdc_hw *hw = (struct msdc_hw *)host->hw;
  546. void __iomem *base = host->base;
  547. int pull = (mode == MSDC_PIN_PULL_UP) ? GPIO_PULL_UP : GPIO_PULL_DOWN;
  548. /* Config reset pin */
  549. if (hw->flags & MSDC_RST_PIN_EN) {
  550. if (hw->config_gpio_pin) /* NULL */
  551. hw->config_gpio_pin(MSDC_RST_PIN, pull);
  552. if (mode == MSDC_PIN_PULL_UP)
  553. sdr_clr_bits(EMMC_IOCON, EMMC_IOCON_BOOTRST);
  554. else
  555. sdr_set_bits(EMMC_IOCON, EMMC_IOCON_BOOTRST);
  556. }
  557. }
  558. static void msdc_core_power(struct msdc_host *host, int on)
  559. {
  560. N_MSG(CFG, "Turn %s %s power (copower: %d -> %d)",
  561. on ? "on" : "off", "core", host->core_power, on);
  562. if (on && host->core_power == 0) {
  563. msdc_vcore_on(host);
  564. host->core_power = 1;
  565. msleep(1);
  566. } else if (!on && host->core_power == 1) {
  567. msdc_vcore_off(host);
  568. host->core_power = 0;
  569. msleep(1);
  570. }
  571. }
  572. static void msdc_host_power(struct msdc_host *host, int on)
  573. {
  574. N_MSG(CFG, "Turn %s %s power ", on ? "on" : "off", "host");
  575. if (on) {
  576. //msdc_core_power(host, 1); // need do card detection.
  577. msdc_pin_reset(host, MSDC_PIN_PULL_UP);
  578. } else {
  579. msdc_pin_reset(host, MSDC_PIN_PULL_DOWN);
  580. //msdc_core_power(host, 0);
  581. }
  582. }
  583. static void msdc_card_power(struct msdc_host *host, int on)
  584. {
  585. N_MSG(CFG, "Turn %s %s power ", on ? "on" : "off", "card");
  586. if (on) {
  587. msdc_pin_config(host, MSDC_PIN_PULL_UP);
  588. //msdc_vdd_on(host); // need todo card detection.
  589. msleep(1);
  590. } else {
  591. //msdc_vdd_off(host);
  592. msdc_pin_config(host, MSDC_PIN_PULL_DOWN);
  593. msleep(1);
  594. }
  595. }
  596. static void msdc_set_power_mode(struct msdc_host *host, u8 mode)
  597. {
  598. N_MSG(CFG, "Set power mode(%d)", mode);
  599. if (host->power_mode == MMC_POWER_OFF && mode != MMC_POWER_OFF) {
  600. msdc_host_power(host, 1);
  601. msdc_card_power(host, 1);
  602. } else if (host->power_mode != MMC_POWER_OFF && mode == MMC_POWER_OFF) {
  603. msdc_card_power(host, 0);
  604. msdc_host_power(host, 0);
  605. }
  606. host->power_mode = mode;
  607. }
  608. #endif /* end of --- */
  609. #ifdef CONFIG_PM
  610. /*
  611. register as callback function of WIFI(combo_sdio_register_pm) .
  612. can called by msdc_drv_suspend/resume too.
  613. */
  614. static void msdc_pm(pm_message_t state, void *data)
  615. {
  616. struct msdc_host *host = (struct msdc_host *)data;
  617. int evt = state.event;
  618. if (evt == PM_EVENT_USER_RESUME || evt == PM_EVENT_USER_SUSPEND) {
  619. INIT_MSG("USR_%s: suspend<%d> power<%d>",
  620. evt == PM_EVENT_USER_RESUME ? "EVENT_USER_RESUME" : "EVENT_USER_SUSPEND",
  621. host->suspend, host->power_mode);
  622. }
  623. if (evt == PM_EVENT_SUSPEND || evt == PM_EVENT_USER_SUSPEND) {
  624. if (host->suspend) /* already suspend */ /* default 0*/
  625. return;
  626. /* for memory card. already power off by mmc */
  627. if (evt == PM_EVENT_SUSPEND && host->power_mode == MMC_POWER_OFF)
  628. return;
  629. host->suspend = 1;
  630. host->pm_state = state; /* default PMSG_RESUME */
  631. } else if (evt == PM_EVENT_RESUME || evt == PM_EVENT_USER_RESUME) {
  632. if (!host->suspend) {
  633. //ERR_MSG("warning: already resume");
  634. return;
  635. }
  636. /* No PM resume when USR suspend */
  637. if (evt == PM_EVENT_RESUME && host->pm_state.event == PM_EVENT_USER_SUSPEND) {
  638. ERR_MSG("PM Resume when in USR Suspend"); /* won't happen. */
  639. return;
  640. }
  641. host->suspend = 0;
  642. host->pm_state = state;
  643. }
  644. }
  645. #endif
  646. /*--------------------------------------------------------------------------*/
  647. /* mmc_host_ops members */
  648. /*--------------------------------------------------------------------------*/
  649. static unsigned int msdc_command_start(struct msdc_host *host,
  650. struct mmc_command *cmd,
  651. int tune, /* not used */
  652. unsigned long timeout)
  653. {
  654. void __iomem *base = host->base;
  655. u32 opcode = cmd->opcode;
  656. u32 rawcmd;
  657. u32 wints = MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO |
  658. MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO |
  659. MSDC_INT_ACMD19_DONE;
  660. u32 resp;
  661. unsigned long tmo;
  662. /* Protocol layer does not provide response type, but our hardware needs
  663. * to know exact type, not just size!
  664. */
  665. if (opcode == MMC_SEND_OP_COND || opcode == SD_APP_OP_COND) {
  666. resp = RESP_R3;
  667. } else if (opcode == MMC_SET_RELATIVE_ADDR) {
  668. resp = (mmc_cmd_type(cmd) == MMC_CMD_BCR) ? RESP_R6 : RESP_R1;
  669. } else if (opcode == MMC_FAST_IO) {
  670. resp = RESP_R4;
  671. } else if (opcode == MMC_GO_IRQ_STATE) {
  672. resp = RESP_R5;
  673. } else if (opcode == MMC_SELECT_CARD) {
  674. resp = (cmd->arg != 0) ? RESP_R1B : RESP_NONE;
  675. } else if (opcode == SD_IO_RW_DIRECT || opcode == SD_IO_RW_EXTENDED) {
  676. resp = RESP_R1; /* SDIO workaround. */
  677. } else if (opcode == SD_SEND_IF_COND && (mmc_cmd_type(cmd) == MMC_CMD_BCR)) {
  678. resp = RESP_R1;
  679. } else {
  680. switch (mmc_resp_type(cmd)) {
  681. case MMC_RSP_R1:
  682. resp = RESP_R1;
  683. break;
  684. case MMC_RSP_R1B:
  685. resp = RESP_R1B;
  686. break;
  687. case MMC_RSP_R2:
  688. resp = RESP_R2;
  689. break;
  690. case MMC_RSP_R3:
  691. resp = RESP_R3;
  692. break;
  693. case MMC_RSP_NONE:
  694. default:
  695. resp = RESP_NONE;
  696. break;
  697. }
  698. }
  699. cmd->error = 0;
  700. /* rawcmd :
  701. * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 |
  702. * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode
  703. */
  704. rawcmd = opcode | msdc_rsp[resp] << 7 | host->blksz << 16;
  705. if (opcode == MMC_READ_MULTIPLE_BLOCK) {
  706. rawcmd |= (2 << 11);
  707. } else if (opcode == MMC_READ_SINGLE_BLOCK) {
  708. rawcmd |= (1 << 11);
  709. } else if (opcode == MMC_WRITE_MULTIPLE_BLOCK) {
  710. rawcmd |= ((2 << 11) | (1 << 13));
  711. } else if (opcode == MMC_WRITE_BLOCK) {
  712. rawcmd |= ((1 << 11) | (1 << 13));
  713. } else if (opcode == SD_IO_RW_EXTENDED) {
  714. if (cmd->data->flags & MMC_DATA_WRITE)
  715. rawcmd |= (1 << 13);
  716. if (cmd->data->blocks > 1)
  717. rawcmd |= (2 << 11);
  718. else
  719. rawcmd |= (1 << 11);
  720. } else if (opcode == SD_IO_RW_DIRECT && cmd->flags == (unsigned int)-1) {
  721. rawcmd |= (1 << 14);
  722. } else if ((opcode == SD_APP_SEND_SCR) ||
  723. (opcode == SD_APP_SEND_NUM_WR_BLKS) ||
  724. (opcode == SD_SWITCH && (mmc_cmd_type(cmd) == MMC_CMD_ADTC)) ||
  725. (opcode == SD_APP_SD_STATUS && (mmc_cmd_type(cmd) == MMC_CMD_ADTC)) ||
  726. (opcode == MMC_SEND_EXT_CSD && (mmc_cmd_type(cmd) == MMC_CMD_ADTC))) {
  727. rawcmd |= (1 << 11);
  728. } else if (opcode == MMC_STOP_TRANSMISSION) {
  729. rawcmd |= (1 << 14);
  730. rawcmd &= ~(0x0FFF << 16);
  731. }
  732. N_MSG(CMD, "CMD<%d><0x%.8x> Arg<0x%.8x>", opcode, rawcmd, cmd->arg);
  733. tmo = jiffies + timeout;
  734. if (opcode == MMC_SEND_STATUS) {
  735. for (;;) {
  736. if (!sdc_is_cmd_busy())
  737. break;
  738. if (time_after(jiffies, tmo)) {
  739. ERR_MSG("XXX cmd_busy timeout: before CMD<%d>", opcode);
  740. cmd->error = -ETIMEDOUT;
  741. msdc_reset_hw(host);
  742. goto end;
  743. }
  744. }
  745. } else {
  746. for (;;) {
  747. if (!sdc_is_busy())
  748. break;
  749. if (time_after(jiffies, tmo)) {
  750. ERR_MSG("XXX sdc_busy timeout: before CMD<%d>", opcode);
  751. cmd->error = -ETIMEDOUT;
  752. msdc_reset_hw(host);
  753. goto end;
  754. }
  755. }
  756. }
  757. //BUG_ON(in_interrupt());
  758. host->cmd = cmd;
  759. host->cmd_rsp = resp;
  760. init_completion(&host->cmd_done);
  761. sdr_set_bits(MSDC_INTEN, wints);
  762. sdc_send_cmd(rawcmd, cmd->arg);
  763. end:
  764. return cmd->error;
  765. }
  766. static unsigned int msdc_command_resp(struct msdc_host *host,
  767. struct mmc_command *cmd,
  768. int tune,
  769. unsigned long timeout)
  770. __must_hold(&host->lock)
  771. {
  772. void __iomem *base = host->base;
  773. u32 opcode = cmd->opcode;
  774. //u32 rawcmd;
  775. u32 resp;
  776. u32 wints = MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO |
  777. MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO |
  778. MSDC_INT_ACMD19_DONE;
  779. resp = host->cmd_rsp;
  780. BUG_ON(in_interrupt());
  781. //init_completion(&host->cmd_done);
  782. //sdr_set_bits(MSDC_INTEN, wints);
  783. spin_unlock(&host->lock);
  784. if (!wait_for_completion_timeout(&host->cmd_done, 10 * timeout)) {
  785. ERR_MSG("XXX CMD<%d> wait_for_completion timeout ARG<0x%.8x>", opcode, cmd->arg);
  786. cmd->error = -ETIMEDOUT;
  787. msdc_reset_hw(host);
  788. }
  789. spin_lock(&host->lock);
  790. sdr_clr_bits(MSDC_INTEN, wints);
  791. host->cmd = NULL;
  792. //end:
  793. #ifdef MT6575_SD_DEBUG
  794. switch (resp) {
  795. case RESP_NONE:
  796. N_MSG(RSP, "CMD_RSP(%d): %d RSP(%d)", opcode, cmd->error, resp);
  797. break;
  798. case RESP_R2:
  799. N_MSG(RSP, "CMD_RSP(%d): %d RSP(%d)= %.8x %.8x %.8x %.8x",
  800. opcode, cmd->error, resp, cmd->resp[0], cmd->resp[1],
  801. cmd->resp[2], cmd->resp[3]);
  802. break;
  803. default: /* Response types 1, 3, 4, 5, 6, 7(1b) */
  804. N_MSG(RSP, "CMD_RSP(%d): %d RSP(%d)= 0x%.8x",
  805. opcode, cmd->error, resp, cmd->resp[0]);
  806. if (cmd->error == 0) {
  807. switch (resp) {
  808. case RESP_R1:
  809. case RESP_R1B:
  810. msdc_dump_card_status(host, cmd->resp[0]);
  811. break;
  812. case RESP_R3:
  813. msdc_dump_ocr_reg(host, cmd->resp[0]);
  814. break;
  815. case RESP_R5:
  816. msdc_dump_io_resp(host, cmd->resp[0]);
  817. break;
  818. case RESP_R6:
  819. msdc_dump_rca_resp(host, cmd->resp[0]);
  820. break;
  821. }
  822. }
  823. break;
  824. }
  825. #endif
  826. /* do we need to save card's RCA when SD_SEND_RELATIVE_ADDR */
  827. if (!tune)
  828. return cmd->error;
  829. /* memory card CRC */
  830. if (host->hw->flags & MSDC_REMOVABLE && cmd->error == -EIO) {
  831. if (sdr_read32(SDC_CMD) & 0x1800) { /* check if has data phase */
  832. msdc_abort_data(host);
  833. } else {
  834. /* do basic: reset*/
  835. msdc_reset_hw(host);
  836. msdc_clr_fifo();
  837. msdc_clr_int();
  838. }
  839. cmd->error = msdc_tune_cmdrsp(host, cmd);
  840. }
  841. // check DAT0
  842. /* if (resp == RESP_R1B) {
  843. while ((sdr_read32(MSDC_PS) & 0x10000) != 0x10000);
  844. } */
  845. /* CMD12 Error Handle */
  846. return cmd->error;
  847. }
  848. static unsigned int msdc_do_command(struct msdc_host *host,
  849. struct mmc_command *cmd,
  850. int tune,
  851. unsigned long timeout)
  852. {
  853. if (msdc_command_start(host, cmd, tune, timeout))
  854. goto end;
  855. if (msdc_command_resp(host, cmd, tune, timeout))
  856. goto end;
  857. end:
  858. N_MSG(CMD, " return<%d> resp<0x%.8x>", cmd->error, cmd->resp[0]);
  859. return cmd->error;
  860. }
  861. #if 0 /* --- by chhung */
  862. // DMA resume / start / stop
  863. static void msdc_dma_resume(struct msdc_host *host)
  864. {
  865. void __iomem *base = host->base;
  866. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_RESUME, 1);
  867. N_MSG(DMA, "DMA resume");
  868. }
  869. #endif /* end of --- */
  870. static void msdc_dma_start(struct msdc_host *host)
  871. {
  872. void __iomem *base = host->base;
  873. u32 wints = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO | MSDC_INTEN_DATCRCERR;
  874. sdr_set_bits(MSDC_INTEN, wints);
  875. //dsb(); /* --- by chhung */
  876. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_START, 1);
  877. N_MSG(DMA, "DMA start");
  878. }
  879. static void msdc_dma_stop(struct msdc_host *host)
  880. {
  881. void __iomem *base = host->base;
  882. //u32 retries=500;
  883. u32 wints = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO | MSDC_INTEN_DATCRCERR;
  884. N_MSG(DMA, "DMA status: 0x%.8x", sdr_read32(MSDC_DMA_CFG));
  885. //while (sdr_read32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS);
  886. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP, 1);
  887. while (sdr_read32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS)
  888. ;
  889. //dsb(); /* --- by chhung */
  890. sdr_clr_bits(MSDC_INTEN, wints); /* Not just xfer_comp */
  891. N_MSG(DMA, "DMA stop");
  892. }
  893. /* calc checksum */
  894. static u8 msdc_dma_calcs(u8 *buf, u32 len)
  895. {
  896. u32 i, sum = 0;
  897. for (i = 0; i < len; i++)
  898. sum += buf[i];
  899. return 0xFF - (u8)sum;
  900. }
  901. /* gpd bd setup + dma registers */
  902. static void msdc_dma_config(struct msdc_host *host, struct msdc_dma *dma)
  903. {
  904. void __iomem *base = host->base;
  905. //u32 i, j, num, bdlen, arg, xfersz;
  906. u32 j, num;
  907. struct scatterlist *sg;
  908. struct gpd *gpd;
  909. struct bd *bd;
  910. switch (dma->mode) {
  911. case MSDC_MODE_DMA_BASIC:
  912. BUG_ON(host->xfer_size > 65535);
  913. BUG_ON(dma->sglen != 1);
  914. sdr_write32(MSDC_DMA_SA, PHYSADDR(sg_dma_address(sg)));
  915. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_LASTBUF, 1);
  916. //#if defined (CONFIG_RALINK_MT7620)
  917. if (ralink_soc == MT762X_SOC_MT7620A)
  918. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_XFERSZ, sg_dma_len(sg));
  919. //#elif defined (CONFIG_RALINK_MT7621) || defined (CONFIG_RALINK_MT7628)
  920. else
  921. sdr_write32((void __iomem *)(RALINK_MSDC_BASE + 0xa8), sg_dma_len(sg));
  922. //#endif
  923. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_BRUSTSZ,
  924. MSDC_BRUST_64B);
  925. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_MODE, 0);
  926. break;
  927. case MSDC_MODE_DMA_DESC:
  928. /* calculate the required number of gpd */
  929. num = (dma->sglen + MAX_BD_PER_GPD - 1) / MAX_BD_PER_GPD;
  930. BUG_ON(num != 1);
  931. gpd = dma->gpd;
  932. bd = dma->bd;
  933. /* modify gpd*/
  934. //gpd->intr = 0;
  935. gpd->hwo = 1; /* hw will clear it */
  936. gpd->bdp = 1;
  937. gpd->chksum = 0; /* need to clear first. */
  938. gpd->chksum = msdc_dma_calcs((u8 *)gpd, 16);
  939. /* modify bd*/
  940. for_each_sg(dma->sg, sg, dma->sglen, j) {
  941. bd[j].blkpad = 0;
  942. bd[j].dwpad = 0;
  943. bd[j].ptr = (void *)sg_dma_address(sg);
  944. bd[j].buflen = sg_dma_len(sg);
  945. if (j == dma->sglen - 1)
  946. bd[j].eol = 1; /* the last bd */
  947. else
  948. bd[j].eol = 0;
  949. bd[j].chksum = 0; /* checksume need to clear first */
  950. bd[j].chksum = msdc_dma_calcs((u8 *)(&bd[j]), 16);
  951. }
  952. sdr_set_field(MSDC_DMA_CFG, MSDC_DMA_CFG_DECSEN, 1);
  953. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_BRUSTSZ,
  954. MSDC_BRUST_64B);
  955. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_MODE, 1);
  956. sdr_write32(MSDC_DMA_SA, PHYSADDR((u32)dma->gpd_addr));
  957. break;
  958. default:
  959. break;
  960. }
  961. N_MSG(DMA, "DMA_CTRL = 0x%x", sdr_read32(MSDC_DMA_CTRL));
  962. N_MSG(DMA, "DMA_CFG = 0x%x", sdr_read32(MSDC_DMA_CFG));
  963. N_MSG(DMA, "DMA_SA = 0x%x", sdr_read32(MSDC_DMA_SA));
  964. }
  965. static void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma,
  966. struct scatterlist *sg, unsigned int sglen)
  967. {
  968. BUG_ON(sglen > MAX_BD_NUM); /* not support currently */
  969. dma->sg = sg;
  970. dma->sglen = sglen;
  971. dma->mode = MSDC_MODE_DMA_DESC;
  972. N_MSG(DMA, "DMA mode<%d> sglen<%d> xfersz<%d>", dma->mode, dma->sglen,
  973. host->xfer_size);
  974. msdc_dma_config(host, dma);
  975. }
  976. static int msdc_do_request(struct mmc_host *mmc, struct mmc_request *mrq)
  977. __must_hold(&host->lock)
  978. {
  979. struct msdc_host *host = mmc_priv(mmc);
  980. struct mmc_command *cmd;
  981. struct mmc_data *data;
  982. void __iomem *base = host->base;
  983. //u32 intsts = 0;
  984. int read = 1, send_type = 0;
  985. #define SND_DAT 0
  986. #define SND_CMD 1
  987. BUG_ON(mmc == NULL);
  988. BUG_ON(mrq == NULL);
  989. host->error = 0;
  990. cmd = mrq->cmd;
  991. data = mrq->cmd->data;
  992. #if 0 /* --- by chhung */
  993. //if(host->id ==1){
  994. N_MSG(OPS, "enable clock!");
  995. msdc_ungate_clock(host->id);
  996. //}
  997. #endif /* end of --- */
  998. if (!data) {
  999. send_type = SND_CMD;
  1000. if (msdc_do_command(host, cmd, 1, CMD_TIMEOUT) != 0)
  1001. goto done;
  1002. } else {
  1003. BUG_ON(data->blksz > HOST_MAX_BLKSZ);
  1004. send_type = SND_DAT;
  1005. data->error = 0;
  1006. read = data->flags & MMC_DATA_READ ? 1 : 0;
  1007. host->data = data;
  1008. host->xfer_size = data->blocks * data->blksz;
  1009. host->blksz = data->blksz;
  1010. if (read) {
  1011. if ((host->timeout_ns != data->timeout_ns) ||
  1012. (host->timeout_clks != data->timeout_clks)) {
  1013. msdc_set_timeout(host, data->timeout_ns, data->timeout_clks);
  1014. }
  1015. }
  1016. sdr_write32(SDC_BLK_NUM, data->blocks);
  1017. //msdc_clr_fifo(); /* no need */
  1018. msdc_dma_on(); /* enable DMA mode first!! */
  1019. init_completion(&host->xfer_done);
  1020. /* start the command first*/
  1021. if (msdc_command_start(host, cmd, 1, CMD_TIMEOUT) != 0)
  1022. goto done;
  1023. data->sg_count = dma_map_sg(mmc_dev(mmc), data->sg,
  1024. data->sg_len,
  1025. mmc_get_dma_dir(data));
  1026. msdc_dma_setup(host, &host->dma, data->sg,
  1027. data->sg_count);
  1028. /* then wait command done */
  1029. if (msdc_command_resp(host, cmd, 1, CMD_TIMEOUT) != 0)
  1030. goto done;
  1031. /* for read, the data coming too fast, then CRC error
  1032. start DMA no business with CRC. */
  1033. //init_completion(&host->xfer_done);
  1034. msdc_dma_start(host);
  1035. spin_unlock(&host->lock);
  1036. if (!wait_for_completion_timeout(&host->xfer_done, DAT_TIMEOUT)) {
  1037. ERR_MSG("XXX CMD<%d> wait xfer_done<%d> timeout!!", cmd->opcode, data->blocks * data->blksz);
  1038. ERR_MSG(" DMA_SA = 0x%x", sdr_read32(MSDC_DMA_SA));
  1039. ERR_MSG(" DMA_CA = 0x%x", sdr_read32(MSDC_DMA_CA));
  1040. ERR_MSG(" DMA_CTRL = 0x%x", sdr_read32(MSDC_DMA_CTRL));
  1041. ERR_MSG(" DMA_CFG = 0x%x", sdr_read32(MSDC_DMA_CFG));
  1042. data->error = -ETIMEDOUT;
  1043. msdc_reset_hw(host);
  1044. msdc_clr_fifo();
  1045. msdc_clr_int();
  1046. }
  1047. spin_lock(&host->lock);
  1048. msdc_dma_stop(host);
  1049. /* Last: stop transfer */
  1050. if (data->stop) {
  1051. if (msdc_do_command(host, data->stop, 0, CMD_TIMEOUT) != 0)
  1052. goto done;
  1053. }
  1054. }
  1055. done:
  1056. if (data != NULL) {
  1057. host->data = NULL;
  1058. dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len,
  1059. mmc_get_dma_dir(data));
  1060. host->blksz = 0;
  1061. #if 0 // don't stop twice!
  1062. if (host->hw->flags & MSDC_REMOVABLE && data->error) {
  1063. msdc_abort_data(host);
  1064. /* reset in IRQ, stop command has issued. -> No need */
  1065. }
  1066. #endif
  1067. N_MSG(OPS, "CMD<%d> data<%s %s> blksz<%d> block<%d> error<%d>", cmd->opcode, (dma ? "dma" : "pio"),
  1068. (read ? "read " : "write"), data->blksz, data->blocks, data->error);
  1069. }
  1070. #if 0 /* --- by chhung */
  1071. #if 1
  1072. //if(host->id==1) {
  1073. if (send_type == SND_CMD) {
  1074. if (cmd->opcode == MMC_SEND_STATUS) {
  1075. if ((cmd->resp[0] & CARD_READY_FOR_DATA) || (CARD_CURRENT_STATE(cmd->resp[0]) != 7)) {
  1076. N_MSG(OPS, "disable clock, CMD13 IDLE");
  1077. msdc_gate_clock(host->id);
  1078. }
  1079. } else {
  1080. N_MSG(OPS, "disable clock, CMD<%d>", cmd->opcode);
  1081. msdc_gate_clock(host->id);
  1082. }
  1083. } else {
  1084. if (read) {
  1085. N_MSG(OPS, "disable clock!!! Read CMD<%d>", cmd->opcode);
  1086. msdc_gate_clock(host->id);
  1087. }
  1088. }
  1089. //}
  1090. #else
  1091. msdc_gate_clock(host->id);
  1092. #endif
  1093. #endif /* end of --- */
  1094. if (mrq->cmd->error)
  1095. host->error = 0x001;
  1096. if (mrq->data && mrq->data->error)
  1097. host->error |= 0x010;
  1098. if (mrq->stop && mrq->stop->error)
  1099. host->error |= 0x100;
  1100. //if (host->error) ERR_MSG("host->error<%d>", host->error);
  1101. return host->error;
  1102. }
  1103. static int msdc_app_cmd(struct mmc_host *mmc, struct msdc_host *host)
  1104. {
  1105. struct mmc_command cmd;
  1106. struct mmc_request mrq;
  1107. u32 err;
  1108. memset(&cmd, 0, sizeof(struct mmc_command));
  1109. cmd.opcode = MMC_APP_CMD;
  1110. #if 0 /* bug: we meet mmc->card is null when ACMD6 */
  1111. cmd.arg = mmc->card->rca << 16;
  1112. #else
  1113. cmd.arg = host->app_cmd_arg;
  1114. #endif
  1115. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  1116. memset(&mrq, 0, sizeof(struct mmc_request));
  1117. mrq.cmd = &cmd; cmd.mrq = &mrq;
  1118. cmd.data = NULL;
  1119. err = msdc_do_command(host, &cmd, 0, CMD_TIMEOUT);
  1120. return err;
  1121. }
  1122. static int msdc_tune_cmdrsp(struct msdc_host *host, struct mmc_command *cmd)
  1123. {
  1124. int result = -1;
  1125. void __iomem *base = host->base;
  1126. u32 rsmpl, cur_rsmpl, orig_rsmpl;
  1127. u32 rrdly, cur_rrdly = 0xffffffff, orig_rrdly;
  1128. u32 skip = 1;
  1129. /* ==== don't support 3.0 now ====
  1130. 1: R_SMPL[1]
  1131. 2: PAD_CMD_RESP_RXDLY[26:22]
  1132. ==========================*/
  1133. // save the previous tune result
  1134. sdr_get_field(MSDC_IOCON, MSDC_IOCON_RSPL, &orig_rsmpl);
  1135. sdr_get_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_CMDRRDLY, &orig_rrdly);
  1136. rrdly = 0;
  1137. do {
  1138. for (rsmpl = 0; rsmpl < 2; rsmpl++) {
  1139. /* Lv1: R_SMPL[1] */
  1140. cur_rsmpl = (orig_rsmpl + rsmpl) % 2;
  1141. if (skip == 1) {
  1142. skip = 0;
  1143. continue;
  1144. }
  1145. sdr_set_field(MSDC_IOCON, MSDC_IOCON_RSPL, cur_rsmpl);
  1146. if (host->app_cmd) {
  1147. result = msdc_app_cmd(host->mmc, host);
  1148. if (result) {
  1149. ERR_MSG("TUNE_CMD app_cmd<%d> failed: RESP_RXDLY<%d>,R_SMPL<%d>",
  1150. host->mrq->cmd->opcode, cur_rrdly, cur_rsmpl);
  1151. continue;
  1152. }
  1153. }
  1154. result = msdc_do_command(host, cmd, 0, CMD_TIMEOUT); // not tune.
  1155. ERR_MSG("TUNE_CMD<%d> %s PAD_CMD_RESP_RXDLY[26:22]<%d> R_SMPL[1]<%d>", cmd->opcode,
  1156. (result == 0) ? "PASS" : "FAIL", cur_rrdly, cur_rsmpl);
  1157. if (result == 0)
  1158. return 0;
  1159. if (result != -EIO) {
  1160. ERR_MSG("TUNE_CMD<%d> Error<%d> not -EIO", cmd->opcode, result);
  1161. return result;
  1162. }
  1163. /* should be EIO */
  1164. if (sdr_read32(SDC_CMD) & 0x1800) { /* check if has data phase */
  1165. msdc_abort_data(host);
  1166. }
  1167. }
  1168. /* Lv2: PAD_CMD_RESP_RXDLY[26:22] */
  1169. cur_rrdly = (orig_rrdly + rrdly + 1) % 32;
  1170. sdr_set_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_CMDRRDLY, cur_rrdly);
  1171. } while (++rrdly < 32);
  1172. return result;
  1173. }
  1174. /* Support SD2.0 Only */
  1175. static int msdc_tune_bread(struct mmc_host *mmc, struct mmc_request *mrq)
  1176. {
  1177. struct msdc_host *host = mmc_priv(mmc);
  1178. void __iomem *base = host->base;
  1179. u32 ddr = 0;
  1180. u32 dcrc = 0;
  1181. u32 rxdly, cur_rxdly0, cur_rxdly1;
  1182. u32 dsmpl, cur_dsmpl, orig_dsmpl;
  1183. u32 cur_dat0, cur_dat1, cur_dat2, cur_dat3;
  1184. u32 cur_dat4, cur_dat5, cur_dat6, cur_dat7;
  1185. u32 orig_dat0, orig_dat1, orig_dat2, orig_dat3;
  1186. u32 orig_dat4, orig_dat5, orig_dat6, orig_dat7;
  1187. int result = -1;
  1188. u32 skip = 1;
  1189. sdr_get_field(MSDC_IOCON, MSDC_IOCON_DSPL, &orig_dsmpl);
  1190. /* Tune Method 2. */
  1191. sdr_set_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, 1);
  1192. rxdly = 0;
  1193. do {
  1194. for (dsmpl = 0; dsmpl < 2; dsmpl++) {
  1195. cur_dsmpl = (orig_dsmpl + dsmpl) % 2;
  1196. if (skip == 1) {
  1197. skip = 0;
  1198. continue;
  1199. }
  1200. sdr_set_field(MSDC_IOCON, MSDC_IOCON_DSPL, cur_dsmpl);
  1201. if (host->app_cmd) {
  1202. result = msdc_app_cmd(host->mmc, host);
  1203. if (result) {
  1204. ERR_MSG("TUNE_BREAD app_cmd<%d> failed", host->mrq->cmd->opcode);
  1205. continue;
  1206. }
  1207. }
  1208. result = msdc_do_request(mmc, mrq);
  1209. sdr_get_field(SDC_DCRC_STS,
  1210. SDC_DCRC_STS_POS | SDC_DCRC_STS_NEG,
  1211. &dcrc); /* RO */
  1212. if (!ddr)
  1213. dcrc &= ~SDC_DCRC_STS_NEG;
  1214. ERR_MSG("TUNE_BREAD<%s> dcrc<0x%x> DATRDDLY0/1<0x%x><0x%x> dsmpl<0x%x>",
  1215. (result == 0 && dcrc == 0) ? "PASS" : "FAIL", dcrc,
  1216. sdr_read32(MSDC_DAT_RDDLY0), sdr_read32(MSDC_DAT_RDDLY1), cur_dsmpl);
  1217. /* Fix me: result is 0, but dcrc is still exist */
  1218. if (result == 0 && dcrc == 0) {
  1219. goto done;
  1220. } else {
  1221. /* there is a case: command timeout, and data phase not processed */
  1222. if (mrq->data->error != 0 &&
  1223. mrq->data->error != -EIO) {
  1224. ERR_MSG("TUNE_READ: result<0x%x> cmd_error<%d> data_error<%d>",
  1225. result, mrq->cmd->error, mrq->data->error);
  1226. goto done;
  1227. }
  1228. }
  1229. }
  1230. cur_rxdly0 = sdr_read32(MSDC_DAT_RDDLY0);
  1231. cur_rxdly1 = sdr_read32(MSDC_DAT_RDDLY1);
  1232. /* E1 ECO. YD: Reverse */
  1233. if (sdr_read32(MSDC_ECO_VER) >= 4) {
  1234. orig_dat0 = (cur_rxdly0 >> 24) & 0x1F;
  1235. orig_dat1 = (cur_rxdly0 >> 16) & 0x1F;
  1236. orig_dat2 = (cur_rxdly0 >> 8) & 0x1F;
  1237. orig_dat3 = (cur_rxdly0 >> 0) & 0x1F;
  1238. orig_dat4 = (cur_rxdly1 >> 24) & 0x1F;
  1239. orig_dat5 = (cur_rxdly1 >> 16) & 0x1F;
  1240. orig_dat6 = (cur_rxdly1 >> 8) & 0x1F;
  1241. orig_dat7 = (cur_rxdly1 >> 0) & 0x1F;
  1242. } else {
  1243. orig_dat0 = (cur_rxdly0 >> 0) & 0x1F;
  1244. orig_dat1 = (cur_rxdly0 >> 8) & 0x1F;
  1245. orig_dat2 = (cur_rxdly0 >> 16) & 0x1F;
  1246. orig_dat3 = (cur_rxdly0 >> 24) & 0x1F;
  1247. orig_dat4 = (cur_rxdly1 >> 0) & 0x1F;
  1248. orig_dat5 = (cur_rxdly1 >> 8) & 0x1F;
  1249. orig_dat6 = (cur_rxdly1 >> 16) & 0x1F;
  1250. orig_dat7 = (cur_rxdly1 >> 24) & 0x1F;
  1251. }
  1252. if (ddr) {
  1253. cur_dat0 = (dcrc & (1 << 0) || dcrc & (1 << 8)) ? ((orig_dat0 + 1) % 32) : orig_dat0;
  1254. cur_dat1 = (dcrc & (1 << 1) || dcrc & (1 << 9)) ? ((orig_dat1 + 1) % 32) : orig_dat1;
  1255. cur_dat2 = (dcrc & (1 << 2) || dcrc & (1 << 10)) ? ((orig_dat2 + 1) % 32) : orig_dat2;
  1256. cur_dat3 = (dcrc & (1 << 3) || dcrc & (1 << 11)) ? ((orig_dat3 + 1) % 32) : orig_dat3;
  1257. } else {
  1258. cur_dat0 = (dcrc & (1 << 0)) ? ((orig_dat0 + 1) % 32) : orig_dat0;
  1259. cur_dat1 = (dcrc & (1 << 1)) ? ((orig_dat1 + 1) % 32) : orig_dat1;
  1260. cur_dat2 = (dcrc & (1 << 2)) ? ((orig_dat2 + 1) % 32) : orig_dat2;
  1261. cur_dat3 = (dcrc & (1 << 3)) ? ((orig_dat3 + 1) % 32) : orig_dat3;
  1262. }
  1263. cur_dat4 = (dcrc & (1 << 4)) ? ((orig_dat4 + 1) % 32) : orig_dat4;
  1264. cur_dat5 = (dcrc & (1 << 5)) ? ((orig_dat5 + 1) % 32) : orig_dat5;
  1265. cur_dat6 = (dcrc & (1 << 6)) ? ((orig_dat6 + 1) % 32) : orig_dat6;
  1266. cur_dat7 = (dcrc & (1 << 7)) ? ((orig_dat7 + 1) % 32) : orig_dat7;
  1267. cur_rxdly0 = (cur_dat0 << 24) | (cur_dat1 << 16) | (cur_dat2 << 8) | (cur_dat3 << 0);
  1268. cur_rxdly1 = (cur_dat4 << 24) | (cur_dat5 << 16) | (cur_dat6 << 8) | (cur_dat7 << 0);
  1269. sdr_write32(MSDC_DAT_RDDLY0, cur_rxdly0);
  1270. sdr_write32(MSDC_DAT_RDDLY1, cur_rxdly1);
  1271. } while (++rxdly < 32);
  1272. done:
  1273. return result;
  1274. }
  1275. static int msdc_tune_bwrite(struct mmc_host *mmc, struct mmc_request *mrq)
  1276. {
  1277. struct msdc_host *host = mmc_priv(mmc);
  1278. void __iomem *base = host->base;
  1279. u32 wrrdly, cur_wrrdly = 0xffffffff, orig_wrrdly;
  1280. u32 dsmpl, cur_dsmpl, orig_dsmpl;
  1281. u32 rxdly, cur_rxdly0;
  1282. u32 orig_dat0, orig_dat1, orig_dat2, orig_dat3;
  1283. u32 cur_dat0, cur_dat1, cur_dat2, cur_dat3;
  1284. int result = -1;
  1285. u32 skip = 1;
  1286. // MSDC_IOCON_DDR50CKD need to check. [Fix me]
  1287. sdr_get_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_DATWRDLY, &orig_wrrdly);
  1288. sdr_get_field(MSDC_IOCON, MSDC_IOCON_DSPL, &orig_dsmpl);
  1289. /* Tune Method 2. just DAT0 */
  1290. sdr_set_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, 1);
  1291. cur_rxdly0 = sdr_read32(MSDC_DAT_RDDLY0);
  1292. /* E1 ECO. YD: Reverse */
  1293. if (sdr_read32(MSDC_ECO_VER) >= 4) {
  1294. orig_dat0 = (cur_rxdly0 >> 24) & 0x1F;
  1295. orig_dat1 = (cur_rxdly0 >> 16) & 0x1F;
  1296. orig_dat2 = (cur_rxdly0 >> 8) & 0x1F;
  1297. orig_dat3 = (cur_rxdly0 >> 0) & 0x1F;
  1298. } else {
  1299. orig_dat0 = (cur_rxdly0 >> 0) & 0x1F;
  1300. orig_dat1 = (cur_rxdly0 >> 8) & 0x1F;
  1301. orig_dat2 = (cur_rxdly0 >> 16) & 0x1F;
  1302. orig_dat3 = (cur_rxdly0 >> 24) & 0x1F;
  1303. }
  1304. rxdly = 0;
  1305. do {
  1306. wrrdly = 0;
  1307. do {
  1308. for (dsmpl = 0; dsmpl < 2; dsmpl++) {
  1309. cur_dsmpl = (orig_dsmpl + dsmpl) % 2;
  1310. if (skip == 1) {
  1311. skip = 0;
  1312. continue;
  1313. }
  1314. sdr_set_field(MSDC_IOCON, MSDC_IOCON_DSPL, cur_dsmpl);
  1315. if (host->app_cmd) {
  1316. result = msdc_app_cmd(host->mmc, host);
  1317. if (result) {
  1318. ERR_MSG("TUNE_BWRITE app_cmd<%d> failed", host->mrq->cmd->opcode);
  1319. continue;
  1320. }
  1321. }
  1322. result = msdc_do_request(mmc, mrq);
  1323. ERR_MSG("TUNE_BWRITE<%s> DSPL<%d> DATWRDLY<%d> MSDC_DAT_RDDLY0<0x%x>",
  1324. result == 0 ? "PASS" : "FAIL",
  1325. cur_dsmpl, cur_wrrdly, cur_rxdly0);
  1326. if (result == 0) {
  1327. goto done;
  1328. } else {
  1329. /* there is a case: command timeout, and data phase not processed */
  1330. if (mrq->data->error != -EIO) {
  1331. ERR_MSG("TUNE_READ: result<0x%x> cmd_error<%d> data_error<%d>",
  1332. result, mrq->cmd->error, mrq->data->error);
  1333. goto done;
  1334. }
  1335. }
  1336. }
  1337. cur_wrrdly = (orig_wrrdly + wrrdly + 1) % 32;
  1338. sdr_set_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_DATWRDLY, cur_wrrdly);
  1339. } while (++wrrdly < 32);
  1340. cur_dat0 = (orig_dat0 + rxdly) % 32; /* only adjust bit-1 for crc */
  1341. cur_dat1 = orig_dat1;
  1342. cur_dat2 = orig_dat2;
  1343. cur_dat3 = orig_dat3;
  1344. cur_rxdly0 = (cur_dat0 << 24) | (cur_dat1 << 16) | (cur_dat2 << 8) | (cur_dat3 << 0);
  1345. sdr_write32(MSDC_DAT_RDDLY0, cur_rxdly0);
  1346. } while (++rxdly < 32);
  1347. done:
  1348. return result;
  1349. }
  1350. static int msdc_get_card_status(struct mmc_host *mmc, struct msdc_host *host, u32 *status)
  1351. {
  1352. struct mmc_command cmd;
  1353. struct mmc_request mrq;
  1354. u32 err;
  1355. memset(&cmd, 0, sizeof(struct mmc_command));
  1356. cmd.opcode = MMC_SEND_STATUS;
  1357. if (mmc->card) {
  1358. cmd.arg = mmc->card->rca << 16;
  1359. } else {
  1360. ERR_MSG("cmd13 mmc card is null");
  1361. cmd.arg = host->app_cmd_arg;
  1362. }
  1363. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  1364. memset(&mrq, 0, sizeof(struct mmc_request));
  1365. mrq.cmd = &cmd; cmd.mrq = &mrq;
  1366. cmd.data = NULL;
  1367. err = msdc_do_command(host, &cmd, 1, CMD_TIMEOUT);
  1368. if (status)
  1369. *status = cmd.resp[0];
  1370. return err;
  1371. }
  1372. static int msdc_check_busy(struct mmc_host *mmc, struct msdc_host *host)
  1373. {
  1374. u32 err = 0;
  1375. u32 status = 0;
  1376. do {
  1377. err = msdc_get_card_status(mmc, host, &status);
  1378. if (err)
  1379. return err;
  1380. /* need cmd12? */
  1381. ERR_MSG("cmd<13> resp<0x%x>", status);
  1382. } while (R1_CURRENT_STATE(status) == 7);
  1383. return err;
  1384. }
  1385. /* failed when msdc_do_request */
  1386. static int msdc_tune_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1387. {
  1388. struct msdc_host *host = mmc_priv(mmc);
  1389. struct mmc_command *cmd;
  1390. struct mmc_data *data;
  1391. //u32 base = host->base;
  1392. int ret = 0, read;
  1393. cmd = mrq->cmd;
  1394. data = mrq->cmd->data;
  1395. read = data->flags & MMC_DATA_READ ? 1 : 0;
  1396. if (read) {
  1397. if (data->error == -EIO)
  1398. ret = msdc_tune_bread(mmc, mrq);
  1399. } else {
  1400. ret = msdc_check_busy(mmc, host);
  1401. if (ret) {
  1402. ERR_MSG("XXX cmd13 wait program done failed");
  1403. return ret;
  1404. }
  1405. /* CRC and TO */
  1406. /* Fix me: don't care card status? */
  1407. ret = msdc_tune_bwrite(mmc, mrq);
  1408. }
  1409. return ret;
  1410. }
  1411. /* ops.request */
  1412. static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1413. {
  1414. struct msdc_host *host = mmc_priv(mmc);
  1415. //=== for sdio profile ===
  1416. #if 0 /* --- by chhung */
  1417. u32 old_H32, old_L32, new_H32, new_L32;
  1418. u32 ticks = 0, opcode = 0, sizes = 0, bRx = 0;
  1419. #endif /* end of --- */
  1420. WARN_ON(host->mrq);
  1421. /* start to process */
  1422. spin_lock(&host->lock);
  1423. #if 0 /* --- by chhung */
  1424. if (sdio_pro_enable) { //=== for sdio profile ===
  1425. if (mrq->cmd->opcode == 52 || mrq->cmd->opcode == 53)
  1426. GPT_GetCounter64(&old_L32, &old_H32);
  1427. }
  1428. #endif /* end of --- */
  1429. host->mrq = mrq;
  1430. if (msdc_do_request(mmc, mrq)) {
  1431. if (host->hw->flags & MSDC_REMOVABLE && ralink_soc == MT762X_SOC_MT7621AT && mrq->data && mrq->data->error)
  1432. msdc_tune_request(mmc, mrq);
  1433. }
  1434. /* ==== when request done, check if app_cmd ==== */
  1435. if (mrq->cmd->opcode == MMC_APP_CMD) {
  1436. host->app_cmd = 1;
  1437. host->app_cmd_arg = mrq->cmd->arg; /* save the RCA */
  1438. } else {
  1439. host->app_cmd = 0;
  1440. //host->app_cmd_arg = 0;
  1441. }
  1442. host->mrq = NULL;
  1443. #if 0 /* --- by chhung */
  1444. //=== for sdio profile ===
  1445. if (sdio_pro_enable) {
  1446. if (mrq->cmd->opcode == 52 || mrq->cmd->opcode == 53) {
  1447. GPT_GetCounter64(&new_L32, &new_H32);
  1448. ticks = msdc_time_calc(old_L32, old_H32, new_L32, new_H32);
  1449. opcode = mrq->cmd->opcode;
  1450. if (mrq->cmd->data) {
  1451. sizes = mrq->cmd->data->blocks * mrq->cmd->data->blksz;
  1452. bRx = mrq->cmd->data->flags & MMC_DATA_READ ? 1 : 0;
  1453. } else {
  1454. bRx = mrq->cmd->arg & 0x80000000 ? 1 : 0;
  1455. }
  1456. if (!mrq->cmd->error)
  1457. msdc_performance(opcode, sizes, bRx, ticks);
  1458. }
  1459. }
  1460. #endif /* end of --- */
  1461. spin_unlock(&host->lock);
  1462. mmc_request_done(mmc, mrq);
  1463. return;
  1464. }
  1465. /* called by ops.set_ios */
  1466. static void msdc_set_buswidth(struct msdc_host *host, u32 width)
  1467. {
  1468. void __iomem *base = host->base;
  1469. u32 val = sdr_read32(SDC_CFG);
  1470. val &= ~SDC_CFG_BUSWIDTH;
  1471. switch (width) {
  1472. default:
  1473. case MMC_BUS_WIDTH_1:
  1474. width = 1;
  1475. val |= (MSDC_BUS_1BITS << 16);
  1476. break;
  1477. case MMC_BUS_WIDTH_4:
  1478. val |= (MSDC_BUS_4BITS << 16);
  1479. break;
  1480. case MMC_BUS_WIDTH_8:
  1481. val |= (MSDC_BUS_8BITS << 16);
  1482. break;
  1483. }
  1484. sdr_write32(SDC_CFG, val);
  1485. N_MSG(CFG, "Bus Width = %d", width);
  1486. }
  1487. /* ops.set_ios */
  1488. static void msdc_ops_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1489. {
  1490. struct msdc_host *host = mmc_priv(mmc);
  1491. void __iomem *base = host->base;
  1492. u32 ddr = 0;
  1493. #ifdef MT6575_SD_DEBUG
  1494. static char *vdd[] = {
  1495. "1.50v", "1.55v", "1.60v", "1.65v", "1.70v", "1.80v", "1.90v",
  1496. "2.00v", "2.10v", "2.20v", "2.30v", "2.40v", "2.50v", "2.60v",
  1497. "2.70v", "2.80v", "2.90v", "3.00v", "3.10v", "3.20v", "3.30v",
  1498. "3.40v", "3.50v", "3.60v"
  1499. };
  1500. static char *power_mode[] = {
  1501. "OFF", "UP", "ON"
  1502. };
  1503. static char *bus_mode[] = {
  1504. "UNKNOWN", "OPENDRAIN", "PUSHPULL"
  1505. };
  1506. static char *timing[] = {
  1507. "LEGACY", "MMC_HS", "SD_HS"
  1508. };
  1509. printk("SET_IOS: CLK(%dkHz), BUS(%s), BW(%u), PWR(%s), VDD(%s), TIMING(%s)",
  1510. ios->clock / 1000, bus_mode[ios->bus_mode],
  1511. (ios->bus_width == MMC_BUS_WIDTH_4) ? 4 : 1,
  1512. power_mode[ios->power_mode], vdd[ios->vdd], timing[ios->timing]);
  1513. #endif
  1514. msdc_set_buswidth(host, ios->bus_width);
  1515. /* Power control ??? */
  1516. switch (ios->power_mode) {
  1517. case MMC_POWER_OFF:
  1518. case MMC_POWER_UP:
  1519. // msdc_set_power_mode(host, ios->power_mode); /* --- by chhung */
  1520. break;
  1521. case MMC_POWER_ON:
  1522. host->power_mode = MMC_POWER_ON;
  1523. break;
  1524. default:
  1525. break;
  1526. }
  1527. /* Clock control */
  1528. if (host->mclk != ios->clock) {
  1529. if (ios->clock > 25000000) {
  1530. //if (!(host->hw->flags & MSDC_REMOVABLE)) {
  1531. INIT_MSG("SD data latch edge<%d>", MSDC_SMPL_FALLING);
  1532. sdr_set_field(MSDC_IOCON, MSDC_IOCON_RSPL,
  1533. MSDC_SMPL_FALLING);
  1534. sdr_set_field(MSDC_IOCON, MSDC_IOCON_DSPL,
  1535. MSDC_SMPL_FALLING);
  1536. //} /* for tuning debug */
  1537. } else { /* default value */
  1538. sdr_write32(MSDC_IOCON, 0x00000000);
  1539. // sdr_write32(MSDC_DAT_RDDLY0, 0x00000000);
  1540. sdr_write32(MSDC_DAT_RDDLY0, 0x10101010); // for MT7620 E2 and afterward
  1541. sdr_write32(MSDC_DAT_RDDLY1, 0x00000000);
  1542. // sdr_write32(MSDC_PAD_TUNE, 0x00000000);
  1543. sdr_write32(MSDC_PAD_TUNE, 0x84101010); // for MT7620 E2 and afterward
  1544. }
  1545. msdc_set_mclk(host, ddr, ios->clock);
  1546. }
  1547. }
  1548. /* ops.get_ro */
  1549. static int msdc_ops_get_ro(struct mmc_host *mmc)
  1550. {
  1551. struct msdc_host *host = mmc_priv(mmc);
  1552. void __iomem *base = host->base;
  1553. unsigned long flags;
  1554. int ro = 0;
  1555. if (host->hw->flags & MSDC_WP_PIN_EN) { /* set for card */
  1556. spin_lock_irqsave(&host->lock, flags);
  1557. ro = (sdr_read32(MSDC_PS) >> 31);
  1558. spin_unlock_irqrestore(&host->lock, flags);
  1559. }
  1560. return ro;
  1561. }
  1562. /* ops.get_cd */
  1563. static int msdc_ops_get_cd(struct mmc_host *mmc)
  1564. {
  1565. struct msdc_host *host = mmc_priv(mmc);
  1566. void __iomem *base = host->base;
  1567. unsigned long flags;
  1568. int present = 1;
  1569. /* for sdio, MSDC_REMOVABLE not set, always return 1 */
  1570. if (!(host->hw->flags & MSDC_REMOVABLE)) {
  1571. /* For sdio, read H/W always get<1>, but may timeout some times */
  1572. #if 1
  1573. host->card_inserted = 1;
  1574. return 1;
  1575. #else
  1576. host->card_inserted = (host->pm_state.event == PM_EVENT_USER_RESUME) ? 1 : 0;
  1577. INIT_MSG("sdio ops_get_cd<%d>", host->card_inserted);
  1578. return host->card_inserted;
  1579. #endif
  1580. }
  1581. /* MSDC_CD_PIN_EN set for card */
  1582. if (host->hw->flags & MSDC_CD_PIN_EN) {
  1583. spin_lock_irqsave(&host->lock, flags);
  1584. #if 0
  1585. present = host->card_inserted; /* why not read from H/W: Fix me*/
  1586. #else
  1587. // CD
  1588. if (cd_active_low)
  1589. present = (sdr_read32(MSDC_PS) & MSDC_PS_CDSTS) ? 0 : 1;
  1590. else
  1591. present = (sdr_read32(MSDC_PS) & MSDC_PS_CDSTS) ? 1 : 0;
  1592. if (host->mmc->caps & MMC_CAP_NEEDS_POLL)
  1593. present = 1;
  1594. host->card_inserted = present;
  1595. #endif
  1596. spin_unlock_irqrestore(&host->lock, flags);
  1597. } else {
  1598. present = 0; /* TODO? Check DAT3 pins for card detection */
  1599. }
  1600. INIT_MSG("ops_get_cd return<%d>", present);
  1601. return present;
  1602. }
  1603. static struct mmc_host_ops mt_msdc_ops = {
  1604. .request = msdc_ops_request,
  1605. .set_ios = msdc_ops_set_ios,
  1606. .get_ro = msdc_ops_get_ro,
  1607. .get_cd = msdc_ops_get_cd,
  1608. };
  1609. /*--------------------------------------------------------------------------*/
  1610. /* interrupt handler */
  1611. /*--------------------------------------------------------------------------*/
  1612. static irqreturn_t msdc_irq(int irq, void *dev_id)
  1613. {
  1614. struct msdc_host *host = (struct msdc_host *)dev_id;
  1615. struct mmc_data *data = host->data;
  1616. struct mmc_command *cmd = host->cmd;
  1617. void __iomem *base = host->base;
  1618. u32 cmdsts = MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO | MSDC_INT_CMDRDY |
  1619. MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO | MSDC_INT_ACMDRDY |
  1620. MSDC_INT_ACMD19_DONE;
  1621. u32 datsts = MSDC_INT_DATCRCERR | MSDC_INT_DATTMO;
  1622. u32 intsts = sdr_read32(MSDC_INT);
  1623. u32 inten = sdr_read32(MSDC_INTEN); inten &= intsts;
  1624. sdr_write32(MSDC_INT, intsts); /* clear interrupts */
  1625. /* MSG will cause fatal error */
  1626. /* card change interrupt */
  1627. if (intsts & MSDC_INT_CDSC) {
  1628. if (host->mmc->caps & MMC_CAP_NEEDS_POLL)
  1629. return IRQ_HANDLED;
  1630. IRQ_MSG("MSDC_INT_CDSC irq<0x%.8x>", intsts);
  1631. schedule_delayed_work(&host->card_delaywork, HZ);
  1632. /* tuning when plug card ? */
  1633. }
  1634. /* sdio interrupt */
  1635. if (intsts & MSDC_INT_SDIOIRQ) {
  1636. IRQ_MSG("XXX MSDC_INT_SDIOIRQ"); /* seems not sdio irq */
  1637. //mmc_signal_sdio_irq(host->mmc);
  1638. }
  1639. /* transfer complete interrupt */
  1640. if (data != NULL) {
  1641. if (inten & MSDC_INT_XFER_COMPL) {
  1642. data->bytes_xfered = host->xfer_size;
  1643. complete(&host->xfer_done);
  1644. }
  1645. if (intsts & datsts) {
  1646. /* do basic reset, or stop command will sdc_busy */
  1647. msdc_reset_hw(host);
  1648. msdc_clr_fifo();
  1649. msdc_clr_int();
  1650. if (intsts & MSDC_INT_DATTMO) {
  1651. IRQ_MSG("XXX CMD<%d> MSDC_INT_DATTMO", host->mrq->cmd->opcode);
  1652. data->error = -ETIMEDOUT;
  1653. } else if (intsts & MSDC_INT_DATCRCERR) {
  1654. IRQ_MSG("XXX CMD<%d> MSDC_INT_DATCRCERR, SDC_DCRC_STS<0x%x>", host->mrq->cmd->opcode, sdr_read32(SDC_DCRC_STS));
  1655. data->error = -EIO;
  1656. }
  1657. //if(sdr_read32(MSDC_INTEN) & MSDC_INT_XFER_COMPL) {
  1658. complete(&host->xfer_done); /* Read CRC come fast, XFER_COMPL not enabled */
  1659. }
  1660. }
  1661. /* command interrupts */
  1662. if ((cmd != NULL) && (intsts & cmdsts)) {
  1663. if ((intsts & MSDC_INT_CMDRDY) || (intsts & MSDC_INT_ACMDRDY) ||
  1664. (intsts & MSDC_INT_ACMD19_DONE)) {
  1665. u32 *rsp = &cmd->resp[0];
  1666. switch (host->cmd_rsp) {
  1667. case RESP_NONE:
  1668. break;
  1669. case RESP_R2:
  1670. *rsp++ = sdr_read32(SDC_RESP3); *rsp++ = sdr_read32(SDC_RESP2);
  1671. *rsp++ = sdr_read32(SDC_RESP1); *rsp++ = sdr_read32(SDC_RESP0);
  1672. break;
  1673. default: /* Response types 1, 3, 4, 5, 6, 7(1b) */
  1674. if ((intsts & MSDC_INT_ACMDRDY) || (intsts & MSDC_INT_ACMD19_DONE))
  1675. *rsp = sdr_read32(SDC_ACMD_RESP);
  1676. else
  1677. *rsp = sdr_read32(SDC_RESP0);
  1678. break;
  1679. }
  1680. } else if ((intsts & MSDC_INT_RSPCRCERR) || (intsts & MSDC_INT_ACMDCRCERR)) {
  1681. if (intsts & MSDC_INT_ACMDCRCERR)
  1682. IRQ_MSG("XXX CMD<%d> MSDC_INT_ACMDCRCERR", cmd->opcode);
  1683. else
  1684. IRQ_MSG("XXX CMD<%d> MSDC_INT_RSPCRCERR", cmd->opcode);
  1685. cmd->error = -EIO;
  1686. } else if ((intsts & MSDC_INT_CMDTMO) || (intsts & MSDC_INT_ACMDTMO)) {
  1687. if (intsts & MSDC_INT_ACMDTMO)
  1688. IRQ_MSG("XXX CMD<%d> MSDC_INT_ACMDTMO", cmd->opcode);
  1689. else
  1690. IRQ_MSG("XXX CMD<%d> MSDC_INT_CMDTMO", cmd->opcode);
  1691. cmd->error = -ETIMEDOUT;
  1692. msdc_reset_hw(host);
  1693. msdc_clr_fifo();
  1694. msdc_clr_int();
  1695. }
  1696. complete(&host->cmd_done);
  1697. }
  1698. /* mmc irq interrupts */
  1699. if (intsts & MSDC_INT_MMCIRQ)
  1700. printk(KERN_INFO "msdc[%d] MMCIRQ: SDC_CSTS=0x%.8x\r\n", host->id, sdr_read32(SDC_CSTS));
  1701. #ifdef MT6575_SD_DEBUG
  1702. {
  1703. /* msdc_int_reg *int_reg = (msdc_int_reg*)&intsts;*/
  1704. N_MSG(INT, "IRQ_EVT(0x%x): MMCIRQ(%d) CDSC(%d), ACRDY(%d), ACTMO(%d), ACCRE(%d) AC19DN(%d)",
  1705. intsts,
  1706. int_reg->mmcirq,
  1707. int_reg->cdsc,
  1708. int_reg->atocmdrdy,
  1709. int_reg->atocmdtmo,
  1710. int_reg->atocmdcrc,
  1711. int_reg->atocmd19done);
  1712. N_MSG(INT, "IRQ_EVT(0x%x): SDIO(%d) CMDRDY(%d), CMDTMO(%d), RSPCRC(%d), CSTA(%d)",
  1713. intsts,
  1714. int_reg->sdioirq,
  1715. int_reg->cmdrdy,
  1716. int_reg->cmdtmo,
  1717. int_reg->rspcrc,
  1718. int_reg->csta);
  1719. N_MSG(INT, "IRQ_EVT(0x%x): XFCMP(%d) DXDONE(%d), DATTMO(%d), DATCRC(%d), DMAEMP(%d)",
  1720. intsts,
  1721. int_reg->xfercomp,
  1722. int_reg->dxferdone,
  1723. int_reg->dattmo,
  1724. int_reg->datcrc,
  1725. int_reg->dmaqempty);
  1726. }
  1727. #endif
  1728. return IRQ_HANDLED;
  1729. }
  1730. /*--------------------------------------------------------------------------*/
  1731. /* platform_driver members */
  1732. /*--------------------------------------------------------------------------*/
  1733. /* called by msdc_drv_probe/remove */
  1734. static void msdc_enable_cd_irq(struct msdc_host *host, int enable)
  1735. {
  1736. struct msdc_hw *hw = host->hw;
  1737. void __iomem *base = host->base;
  1738. /* for sdio, not set */
  1739. if ((hw->flags & MSDC_CD_PIN_EN) == 0) {
  1740. /* Pull down card detection pin since it is not avaiable */
  1741. /*
  1742. if (hw->config_gpio_pin)
  1743. hw->config_gpio_pin(MSDC_CD_PIN, GPIO_PULL_DOWN);
  1744. */
  1745. sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
  1746. sdr_clr_bits(MSDC_INTEN, MSDC_INTEN_CDSC);
  1747. sdr_clr_bits(SDC_CFG, SDC_CFG_INSWKUP);
  1748. return;
  1749. }
  1750. N_MSG(CFG, "CD IRQ Eanable(%d)", enable);
  1751. if (enable) {
  1752. /* card detection circuit relies on the core power so that the core power
  1753. * shouldn't be turned off. Here adds a reference count to keep
  1754. * the core power alive.
  1755. */
  1756. //msdc_vcore_on(host); //did in msdc_init_hw()
  1757. if (hw->config_gpio_pin) /* NULL */
  1758. hw->config_gpio_pin(MSDC_CD_PIN, GPIO_PULL_UP);
  1759. sdr_set_field(MSDC_PS, MSDC_PS_CDDEBOUNCE, DEFAULT_DEBOUNCE);
  1760. sdr_set_bits(MSDC_PS, MSDC_PS_CDEN);
  1761. sdr_set_bits(MSDC_INTEN, MSDC_INTEN_CDSC);
  1762. sdr_set_bits(SDC_CFG, SDC_CFG_INSWKUP); /* not in document! Fix me */
  1763. } else {
  1764. if (hw->config_gpio_pin) /* NULL */
  1765. hw->config_gpio_pin(MSDC_CD_PIN, GPIO_PULL_DOWN);
  1766. sdr_clr_bits(SDC_CFG, SDC_CFG_INSWKUP);
  1767. sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
  1768. sdr_clr_bits(MSDC_INTEN, MSDC_INTEN_CDSC);
  1769. /* Here decreases a reference count to core power since card
  1770. * detection circuit is shutdown.
  1771. */
  1772. //msdc_vcore_off(host);
  1773. }
  1774. }
  1775. /* called by msdc_drv_probe */
  1776. static void msdc_init_hw(struct msdc_host *host)
  1777. {
  1778. void __iomem *base = host->base;
  1779. /* Power on */
  1780. #if 0 /* --- by chhung */
  1781. msdc_vcore_on(host);
  1782. msdc_pin_reset(host, MSDC_PIN_PULL_UP);
  1783. msdc_select_clksrc(host, hw->clk_src);
  1784. enable_clock(PERI_MSDC0_PDN + host->id, "SD");
  1785. msdc_vdd_on(host);
  1786. #endif /* end of --- */
  1787. /* Configure to MMC/SD mode */
  1788. sdr_set_field(MSDC_CFG, MSDC_CFG_MODE, MSDC_SDMMC);
  1789. /* Reset */
  1790. msdc_reset_hw(host);
  1791. msdc_clr_fifo();
  1792. /* Disable card detection */
  1793. sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
  1794. /* Disable and clear all interrupts */
  1795. sdr_clr_bits(MSDC_INTEN, sdr_read32(MSDC_INTEN));
  1796. sdr_write32(MSDC_INT, sdr_read32(MSDC_INT));
  1797. #if 1
  1798. /* reset tuning parameter */
  1799. sdr_write32(MSDC_PAD_CTL0, 0x00090000);
  1800. sdr_write32(MSDC_PAD_CTL1, 0x000A0000);
  1801. sdr_write32(MSDC_PAD_CTL2, 0x000A0000);
  1802. // sdr_write32(MSDC_PAD_TUNE, 0x00000000);
  1803. sdr_write32(MSDC_PAD_TUNE, 0x84101010); // for MT7620 E2 and afterward
  1804. // sdr_write32(MSDC_DAT_RDDLY0, 0x00000000);
  1805. sdr_write32(MSDC_DAT_RDDLY0, 0x10101010); // for MT7620 E2 and afterward
  1806. sdr_write32(MSDC_DAT_RDDLY1, 0x00000000);
  1807. sdr_write32(MSDC_IOCON, 0x00000000);
  1808. #if 0 // use MT7620 default value: 0x403c004f
  1809. sdr_write32(MSDC_PATCH_BIT0, 0x003C000F); /* bit0 modified: Rx Data Clock Source: 1 -> 2.0*/
  1810. #endif
  1811. if (sdr_read32(MSDC_ECO_VER) >= 4) {
  1812. if (host->id == 1) {
  1813. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PATCH_BIT1_WRDAT_CRCS, 1);
  1814. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PATCH_BIT1_CMD_RSP, 1);
  1815. /* internal clock: latch read data */
  1816. sdr_set_bits(MSDC_PATCH_BIT0, MSDC_PATCH_BIT_CKGEN_CK);
  1817. }
  1818. }
  1819. #endif
  1820. /* for safety, should clear SDC_CFG.SDIO_INT_DET_EN & set SDC_CFG.SDIO in
  1821. pre-loader,uboot,kernel drivers. and SDC_CFG.SDIO_INT_DET_EN will be only
  1822. set when kernel driver wants to use SDIO bus interrupt */
  1823. /* Configure to enable SDIO mode. it's must otherwise sdio cmd5 failed */
  1824. sdr_set_bits(SDC_CFG, SDC_CFG_SDIO);
  1825. /* disable detect SDIO device interupt function */
  1826. sdr_clr_bits(SDC_CFG, SDC_CFG_SDIOIDE);
  1827. /* eneable SMT for glitch filter */
  1828. sdr_set_bits(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKSMT);
  1829. sdr_set_bits(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDSMT);
  1830. sdr_set_bits(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATSMT);
  1831. #if 1
  1832. /* set clk, cmd, dat pad driving */
  1833. sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVN, 4);
  1834. sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVP, 4);
  1835. sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVN, 4);
  1836. sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVP, 4);
  1837. sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVN, 4);
  1838. sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVP, 4);
  1839. #else
  1840. sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVN, 0);
  1841. sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVP, 0);
  1842. sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVN, 0);
  1843. sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVP, 0);
  1844. sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVN, 0);
  1845. sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVP, 0);
  1846. #endif
  1847. /* set sampling edge */
  1848. /* write crc timeout detection */
  1849. sdr_set_field(MSDC_PATCH_BIT0, 1 << 30, 1);
  1850. /* Configure to default data timeout */
  1851. sdr_set_field(SDC_CFG, SDC_CFG_DTOC, DEFAULT_DTOC);
  1852. msdc_set_buswidth(host, MMC_BUS_WIDTH_1);
  1853. N_MSG(FUC, "init hardware done!");
  1854. }
  1855. /* called by msdc_drv_remove */
  1856. static void msdc_deinit_hw(struct msdc_host *host)
  1857. {
  1858. void __iomem *base = host->base;
  1859. /* Disable and clear all interrupts */
  1860. sdr_clr_bits(MSDC_INTEN, sdr_read32(MSDC_INTEN));
  1861. sdr_write32(MSDC_INT, sdr_read32(MSDC_INT));
  1862. /* Disable card detection */
  1863. msdc_enable_cd_irq(host, 0);
  1864. // msdc_set_power_mode(host, MMC_POWER_OFF); /* make sure power down */ /* --- by chhung */
  1865. }
  1866. /* init gpd and bd list in msdc_drv_probe */
  1867. static void msdc_init_gpd_bd(struct msdc_host *host, struct msdc_dma *dma)
  1868. {
  1869. struct gpd *gpd = dma->gpd;
  1870. struct bd *bd = dma->bd;
  1871. int i;
  1872. /* we just support one gpd, but gpd->next must be set for desc
  1873. * DMA. That's why we alloc 2 gpd structurs.
  1874. */
  1875. memset(gpd, 0, sizeof(struct gpd) * 2);
  1876. gpd->bdp = 1; /* hwo, cs, bd pointer */
  1877. gpd->ptr = (void *)dma->bd_addr; /* physical address */
  1878. gpd->next = (void *)((u32)dma->gpd_addr + sizeof(struct gpd));
  1879. memset(bd, 0, sizeof(struct bd) * MAX_BD_NUM);
  1880. for (i = 0; i < (MAX_BD_NUM - 1); i++)
  1881. bd[i].next = (void *)(dma->bd_addr + sizeof(*bd) * (i + 1));
  1882. }
  1883. static int msdc_drv_probe(struct platform_device *pdev)
  1884. {
  1885. struct resource *res;
  1886. __iomem void *base;
  1887. struct mmc_host *mmc;
  1888. struct msdc_host *host;
  1889. struct msdc_hw *hw;
  1890. int ret;
  1891. //FIXME: this should be done by pinconf and not by the sd driver
  1892. if ((ralink_soc == MT762X_SOC_MT7688 ||
  1893. ralink_soc == MT762X_SOC_MT7628AN) &&
  1894. (!(rt_sysc_r32(0x60) & BIT(15))))
  1895. rt_sysc_m32(0xf << 17, 0xf << 17, 0x3c);
  1896. hw = &msdc0_hw;
  1897. if (of_property_read_bool(pdev->dev.of_node, "mtk,wp-en"))
  1898. msdc0_hw.flags |= MSDC_WP_PIN_EN;
  1899. /* Allocate MMC host for this device */
  1900. mmc = mmc_alloc_host(sizeof(struct msdc_host), &pdev->dev);
  1901. if (!mmc)
  1902. return -ENOMEM;
  1903. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1904. base = devm_ioremap_resource(&pdev->dev, res);
  1905. if (IS_ERR(base)) {
  1906. ret = PTR_ERR(base);
  1907. goto host_free;
  1908. }
  1909. /* Set host parameters to mmc */
  1910. mmc->ops = &mt_msdc_ops;
  1911. mmc->f_min = HOST_MIN_MCLK;
  1912. mmc->f_max = HOST_MAX_MCLK;
  1913. mmc->ocr_avail = MSDC_OCR_AVAIL;
  1914. mmc->caps = MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
  1915. //TODO: read this as bus-width from dt (via mmc_of_parse)
  1916. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1917. cd_active_low = !of_property_read_bool(pdev->dev.of_node, "mediatek,cd-high");
  1918. if (of_property_read_bool(pdev->dev.of_node, "mediatek,cd-poll"))
  1919. mmc->caps |= MMC_CAP_NEEDS_POLL;
  1920. /* MMC core transfer sizes tunable parameters */
  1921. mmc->max_segs = MAX_HW_SGMTS;
  1922. mmc->max_seg_size = MAX_SGMT_SZ;
  1923. mmc->max_blk_size = HOST_MAX_BLKSZ;
  1924. mmc->max_req_size = MAX_REQ_SZ;
  1925. mmc->max_blk_count = mmc->max_req_size;
  1926. host = mmc_priv(mmc);
  1927. host->hw = hw;
  1928. host->mmc = mmc;
  1929. host->id = pdev->id;
  1930. if (host->id < 0 || host->id >= 4)
  1931. host->id = 0;
  1932. host->error = 0;
  1933. host->irq = platform_get_irq(pdev, 0);
  1934. if (host->irq < 0) {
  1935. ret = -EINVAL;
  1936. goto host_free;
  1937. }
  1938. host->base = base;
  1939. host->mclk = 0; /* mclk: the request clock of mmc sub-system */
  1940. host->hclk = hclks[hw->clk_src]; /* hclk: clock of clock source to msdc controller */
  1941. host->sclk = 0; /* sclk: the really clock after divition */
  1942. host->pm_state = PMSG_RESUME;
  1943. host->suspend = 0;
  1944. host->core_clkon = 0;
  1945. host->card_clkon = 0;
  1946. host->core_power = 0;
  1947. host->power_mode = MMC_POWER_OFF;
  1948. // host->card_inserted = hw->flags & MSDC_REMOVABLE ? 0 : 1;
  1949. host->timeout_ns = 0;
  1950. host->timeout_clks = DEFAULT_DTOC * 65536;
  1951. host->mrq = NULL;
  1952. //init_MUTEX(&host->sem); /* we don't need to support multiple threads access */
  1953. dma_coerce_mask_and_coherent(mmc_dev(mmc), DMA_BIT_MASK(32));
  1954. /* using dma_alloc_coherent*/ /* todo: using 1, for all 4 slots */
  1955. host->dma.gpd = dma_alloc_coherent(&pdev->dev,
  1956. MAX_GPD_NUM * sizeof(struct gpd),
  1957. &host->dma.gpd_addr, GFP_KERNEL);
  1958. host->dma.bd = dma_alloc_coherent(&pdev->dev,
  1959. MAX_BD_NUM * sizeof(struct bd),
  1960. &host->dma.bd_addr, GFP_KERNEL);
  1961. if (!host->dma.gpd || !host->dma.bd) {
  1962. ret = -ENOMEM;
  1963. goto release_mem;
  1964. }
  1965. msdc_init_gpd_bd(host, &host->dma);
  1966. INIT_DELAYED_WORK(&host->card_delaywork, msdc_tasklet_card);
  1967. spin_lock_init(&host->lock);
  1968. msdc_init_hw(host);
  1969. /* TODO check weather flags 0 is correct, the mtk-sd driver uses
  1970. * IRQF_TRIGGER_LOW | IRQF_ONESHOT for flags
  1971. *
  1972. * for flags 0 the trigger polarity is determined by the
  1973. * device tree, but not the oneshot flag, but maybe it is also
  1974. * not needed because the soc could be oneshot safe.
  1975. */
  1976. ret = devm_request_irq(&pdev->dev, host->irq, msdc_irq, 0, pdev->name,
  1977. host);
  1978. if (ret)
  1979. goto release;
  1980. platform_set_drvdata(pdev, mmc);
  1981. ret = mmc_add_host(mmc);
  1982. if (ret)
  1983. goto release;
  1984. /* Config card detection pin and enable interrupts */
  1985. if (hw->flags & MSDC_CD_PIN_EN) { /* set for card */
  1986. msdc_enable_cd_irq(host, 1);
  1987. } else {
  1988. msdc_enable_cd_irq(host, 0);
  1989. }
  1990. return 0;
  1991. release:
  1992. platform_set_drvdata(pdev, NULL);
  1993. msdc_deinit_hw(host);
  1994. cancel_delayed_work_sync(&host->card_delaywork);
  1995. release_mem:
  1996. if (host->dma.gpd)
  1997. dma_free_coherent(&pdev->dev, MAX_GPD_NUM * sizeof(struct gpd),
  1998. host->dma.gpd, host->dma.gpd_addr);
  1999. if (host->dma.bd)
  2000. dma_free_coherent(&pdev->dev, MAX_BD_NUM * sizeof(struct bd),
  2001. host->dma.bd, host->dma.bd_addr);
  2002. host_free:
  2003. mmc_free_host(mmc);
  2004. return ret;
  2005. }
  2006. /* 4 device share one driver, using "drvdata" to show difference */
  2007. static int msdc_drv_remove(struct platform_device *pdev)
  2008. {
  2009. struct mmc_host *mmc;
  2010. struct msdc_host *host;
  2011. mmc = platform_get_drvdata(pdev);
  2012. BUG_ON(!mmc);
  2013. host = mmc_priv(mmc);
  2014. BUG_ON(!host);
  2015. ERR_MSG("removed !!!");
  2016. platform_set_drvdata(pdev, NULL);
  2017. mmc_remove_host(host->mmc);
  2018. msdc_deinit_hw(host);
  2019. cancel_delayed_work_sync(&host->card_delaywork);
  2020. dma_free_coherent(&pdev->dev, MAX_GPD_NUM * sizeof(struct gpd),
  2021. host->dma.gpd, host->dma.gpd_addr);
  2022. dma_free_coherent(&pdev->dev, MAX_BD_NUM * sizeof(struct bd),
  2023. host->dma.bd, host->dma.bd_addr);
  2024. mmc_free_host(host->mmc);
  2025. return 0;
  2026. }
  2027. /* Fix me: Power Flow */
  2028. #ifdef CONFIG_PM
  2029. static void msdc_drv_pm(struct platform_device *pdev, pm_message_t state)
  2030. {
  2031. struct mmc_host *mmc = platform_get_drvdata(pdev);
  2032. if (mmc) {
  2033. struct msdc_host *host = mmc_priv(mmc);
  2034. msdc_pm(state, (void *)host);
  2035. }
  2036. }
  2037. static int msdc_drv_suspend(struct platform_device *pdev, pm_message_t state)
  2038. {
  2039. if (state.event == PM_EVENT_SUSPEND)
  2040. msdc_drv_pm(pdev, state);
  2041. return 0;
  2042. }
  2043. static int msdc_drv_resume(struct platform_device *pdev)
  2044. {
  2045. struct pm_message state;
  2046. state.event = PM_EVENT_RESUME;
  2047. msdc_drv_pm(pdev, state);
  2048. return 0;
  2049. }
  2050. #endif
  2051. static const struct of_device_id mt7620_sdhci_match[] = {
  2052. { .compatible = "ralink,mt7620-sdhci" },
  2053. {},
  2054. };
  2055. MODULE_DEVICE_TABLE(of, mt7620_sdhci_match);
  2056. static struct platform_driver mt_msdc_driver = {
  2057. .probe = msdc_drv_probe,
  2058. .remove = msdc_drv_remove,
  2059. #ifdef CONFIG_PM
  2060. .suspend = msdc_drv_suspend,
  2061. .resume = msdc_drv_resume,
  2062. #endif
  2063. .driver = {
  2064. .name = DRV_NAME,
  2065. .of_match_table = mt7620_sdhci_match,
  2066. },
  2067. };
  2068. /*--------------------------------------------------------------------------*/
  2069. /* module init/exit */
  2070. /*--------------------------------------------------------------------------*/
  2071. static int __init mt_msdc_init(void)
  2072. {
  2073. int ret;
  2074. ret = platform_driver_register(&mt_msdc_driver);
  2075. if (ret) {
  2076. printk(KERN_ERR DRV_NAME ": Can't register driver");
  2077. return ret;
  2078. }
  2079. #if defined(MT6575_SD_DEBUG)
  2080. msdc_debug_proc_init();
  2081. #endif
  2082. return 0;
  2083. }
  2084. static void __exit mt_msdc_exit(void)
  2085. {
  2086. platform_driver_unregister(&mt_msdc_driver);
  2087. }
  2088. module_init(mt_msdc_init);
  2089. module_exit(mt_msdc_exit);
  2090. MODULE_LICENSE("GPL");
  2091. MODULE_DESCRIPTION("MediaTek MT6575 SD/MMC Card Driver");
  2092. MODULE_AUTHOR("Infinity Chen <[email protected]>");