sd.c 68 KB

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