sd.c 66 KB

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