dw_mmc.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  1. /*
  2. * Synopsys DesignWare Multimedia Card Interface driver
  3. * (Based on NXP driver for lpc 31xx)
  4. *
  5. * Copyright (C) 2009 NXP Semiconductors
  6. * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/blkdev.h>
  14. #include <linux/clk.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/device.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/ioport.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/slab.h>
  27. #include <linux/stat.h>
  28. #include <linux/delay.h>
  29. #include <linux/irq.h>
  30. #include <linux/mmc/host.h>
  31. #include <linux/mmc/mmc.h>
  32. #include <linux/mmc/dw_mmc.h>
  33. #include <linux/bitops.h>
  34. #include <linux/regulator/consumer.h>
  35. #include <linux/workqueue.h>
  36. #include "dw_mmc.h"
  37. /* Common flag combinations */
  38. #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DTO | SDMMC_INT_DCRC | \
  39. SDMMC_INT_HTO | SDMMC_INT_SBE | \
  40. SDMMC_INT_EBE)
  41. #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
  42. SDMMC_INT_RESP_ERR)
  43. #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
  44. DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
  45. #define DW_MCI_SEND_STATUS 1
  46. #define DW_MCI_RECV_STATUS 2
  47. #define DW_MCI_DMA_THRESHOLD 16
  48. #ifdef CONFIG_MMC_DW_IDMAC
  49. struct idmac_desc {
  50. u32 des0; /* Control Descriptor */
  51. #define IDMAC_DES0_DIC BIT(1)
  52. #define IDMAC_DES0_LD BIT(2)
  53. #define IDMAC_DES0_FD BIT(3)
  54. #define IDMAC_DES0_CH BIT(4)
  55. #define IDMAC_DES0_ER BIT(5)
  56. #define IDMAC_DES0_CES BIT(30)
  57. #define IDMAC_DES0_OWN BIT(31)
  58. u32 des1; /* Buffer sizes */
  59. #define IDMAC_SET_BUFFER1_SIZE(d, s) \
  60. ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
  61. u32 des2; /* buffer 1 physical address */
  62. u32 des3; /* buffer 2 physical address */
  63. };
  64. #endif /* CONFIG_MMC_DW_IDMAC */
  65. /**
  66. * struct dw_mci_slot - MMC slot state
  67. * @mmc: The mmc_host representing this slot.
  68. * @host: The MMC controller this slot is using.
  69. * @ctype: Card type for this slot.
  70. * @mrq: mmc_request currently being processed or waiting to be
  71. * processed, or NULL when the slot is idle.
  72. * @queue_node: List node for placing this node in the @queue list of
  73. * &struct dw_mci.
  74. * @clock: Clock rate configured by set_ios(). Protected by host->lock.
  75. * @flags: Random state bits associated with the slot.
  76. * @id: Number of this slot.
  77. * @last_detect_state: Most recently observed card detect state.
  78. */
  79. struct dw_mci_slot {
  80. struct mmc_host *mmc;
  81. struct dw_mci *host;
  82. u32 ctype;
  83. struct mmc_request *mrq;
  84. struct list_head queue_node;
  85. unsigned int clock;
  86. unsigned long flags;
  87. #define DW_MMC_CARD_PRESENT 0
  88. #define DW_MMC_CARD_NEED_INIT 1
  89. int id;
  90. int last_detect_state;
  91. };
  92. static struct workqueue_struct *dw_mci_card_workqueue;
  93. #if defined(CONFIG_DEBUG_FS)
  94. static int dw_mci_req_show(struct seq_file *s, void *v)
  95. {
  96. struct dw_mci_slot *slot = s->private;
  97. struct mmc_request *mrq;
  98. struct mmc_command *cmd;
  99. struct mmc_command *stop;
  100. struct mmc_data *data;
  101. /* Make sure we get a consistent snapshot */
  102. spin_lock_bh(&slot->host->lock);
  103. mrq = slot->mrq;
  104. if (mrq) {
  105. cmd = mrq->cmd;
  106. data = mrq->data;
  107. stop = mrq->stop;
  108. if (cmd)
  109. seq_printf(s,
  110. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  111. cmd->opcode, cmd->arg, cmd->flags,
  112. cmd->resp[0], cmd->resp[1], cmd->resp[2],
  113. cmd->resp[2], cmd->error);
  114. if (data)
  115. seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
  116. data->bytes_xfered, data->blocks,
  117. data->blksz, data->flags, data->error);
  118. if (stop)
  119. seq_printf(s,
  120. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  121. stop->opcode, stop->arg, stop->flags,
  122. stop->resp[0], stop->resp[1], stop->resp[2],
  123. stop->resp[2], stop->error);
  124. }
  125. spin_unlock_bh(&slot->host->lock);
  126. return 0;
  127. }
  128. static int dw_mci_req_open(struct inode *inode, struct file *file)
  129. {
  130. return single_open(file, dw_mci_req_show, inode->i_private);
  131. }
  132. static const struct file_operations dw_mci_req_fops = {
  133. .owner = THIS_MODULE,
  134. .open = dw_mci_req_open,
  135. .read = seq_read,
  136. .llseek = seq_lseek,
  137. .release = single_release,
  138. };
  139. static int dw_mci_regs_show(struct seq_file *s, void *v)
  140. {
  141. seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
  142. seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
  143. seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
  144. seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
  145. seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
  146. seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
  147. return 0;
  148. }
  149. static int dw_mci_regs_open(struct inode *inode, struct file *file)
  150. {
  151. return single_open(file, dw_mci_regs_show, inode->i_private);
  152. }
  153. static const struct file_operations dw_mci_regs_fops = {
  154. .owner = THIS_MODULE,
  155. .open = dw_mci_regs_open,
  156. .read = seq_read,
  157. .llseek = seq_lseek,
  158. .release = single_release,
  159. };
  160. static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
  161. {
  162. struct mmc_host *mmc = slot->mmc;
  163. struct dw_mci *host = slot->host;
  164. struct dentry *root;
  165. struct dentry *node;
  166. root = mmc->debugfs_root;
  167. if (!root)
  168. return;
  169. node = debugfs_create_file("regs", S_IRUSR, root, host,
  170. &dw_mci_regs_fops);
  171. if (!node)
  172. goto err;
  173. node = debugfs_create_file("req", S_IRUSR, root, slot,
  174. &dw_mci_req_fops);
  175. if (!node)
  176. goto err;
  177. node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
  178. if (!node)
  179. goto err;
  180. node = debugfs_create_x32("pending_events", S_IRUSR, root,
  181. (u32 *)&host->pending_events);
  182. if (!node)
  183. goto err;
  184. node = debugfs_create_x32("completed_events", S_IRUSR, root,
  185. (u32 *)&host->completed_events);
  186. if (!node)
  187. goto err;
  188. return;
  189. err:
  190. dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
  191. }
  192. #endif /* defined(CONFIG_DEBUG_FS) */
  193. static void dw_mci_set_timeout(struct dw_mci *host)
  194. {
  195. /* timeout (maximum) */
  196. mci_writel(host, TMOUT, 0xffffffff);
  197. }
  198. static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
  199. {
  200. struct mmc_data *data;
  201. u32 cmdr;
  202. cmd->error = -EINPROGRESS;
  203. cmdr = cmd->opcode;
  204. if (cmdr == MMC_STOP_TRANSMISSION)
  205. cmdr |= SDMMC_CMD_STOP;
  206. else
  207. cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
  208. if (cmd->flags & MMC_RSP_PRESENT) {
  209. /* We expect a response, so set this bit */
  210. cmdr |= SDMMC_CMD_RESP_EXP;
  211. if (cmd->flags & MMC_RSP_136)
  212. cmdr |= SDMMC_CMD_RESP_LONG;
  213. }
  214. if (cmd->flags & MMC_RSP_CRC)
  215. cmdr |= SDMMC_CMD_RESP_CRC;
  216. data = cmd->data;
  217. if (data) {
  218. cmdr |= SDMMC_CMD_DAT_EXP;
  219. if (data->flags & MMC_DATA_STREAM)
  220. cmdr |= SDMMC_CMD_STRM_MODE;
  221. if (data->flags & MMC_DATA_WRITE)
  222. cmdr |= SDMMC_CMD_DAT_WR;
  223. }
  224. return cmdr;
  225. }
  226. static void dw_mci_start_command(struct dw_mci *host,
  227. struct mmc_command *cmd, u32 cmd_flags)
  228. {
  229. host->cmd = cmd;
  230. dev_vdbg(&host->pdev->dev,
  231. "start command: ARGR=0x%08x CMDR=0x%08x\n",
  232. cmd->arg, cmd_flags);
  233. mci_writel(host, CMDARG, cmd->arg);
  234. wmb();
  235. mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
  236. }
  237. static void send_stop_cmd(struct dw_mci *host, struct mmc_data *data)
  238. {
  239. dw_mci_start_command(host, data->stop, host->stop_cmdr);
  240. }
  241. /* DMA interface functions */
  242. static void dw_mci_stop_dma(struct dw_mci *host)
  243. {
  244. if (host->using_dma) {
  245. host->dma_ops->stop(host);
  246. host->dma_ops->cleanup(host);
  247. } else {
  248. /* Data transfer was stopped by the interrupt handler */
  249. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  250. }
  251. }
  252. #ifdef CONFIG_MMC_DW_IDMAC
  253. static void dw_mci_dma_cleanup(struct dw_mci *host)
  254. {
  255. struct mmc_data *data = host->data;
  256. if (data)
  257. dma_unmap_sg(&host->pdev->dev, data->sg, data->sg_len,
  258. ((data->flags & MMC_DATA_WRITE)
  259. ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
  260. }
  261. static void dw_mci_idmac_stop_dma(struct dw_mci *host)
  262. {
  263. u32 temp;
  264. /* Disable and reset the IDMAC interface */
  265. temp = mci_readl(host, CTRL);
  266. temp &= ~SDMMC_CTRL_USE_IDMAC;
  267. temp |= SDMMC_CTRL_DMA_RESET;
  268. mci_writel(host, CTRL, temp);
  269. /* Stop the IDMAC running */
  270. temp = mci_readl(host, BMOD);
  271. temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
  272. mci_writel(host, BMOD, temp);
  273. }
  274. static void dw_mci_idmac_complete_dma(struct dw_mci *host)
  275. {
  276. struct mmc_data *data = host->data;
  277. dev_vdbg(&host->pdev->dev, "DMA complete\n");
  278. host->dma_ops->cleanup(host);
  279. /*
  280. * If the card was removed, data will be NULL. No point in trying to
  281. * send the stop command or waiting for NBUSY in this case.
  282. */
  283. if (data) {
  284. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  285. tasklet_schedule(&host->tasklet);
  286. }
  287. }
  288. static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
  289. unsigned int sg_len)
  290. {
  291. int i;
  292. struct idmac_desc *desc = host->sg_cpu;
  293. for (i = 0; i < sg_len; i++, desc++) {
  294. unsigned int length = sg_dma_len(&data->sg[i]);
  295. u32 mem_addr = sg_dma_address(&data->sg[i]);
  296. /* Set the OWN bit and disable interrupts for this descriptor */
  297. desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | IDMAC_DES0_CH;
  298. /* Buffer length */
  299. IDMAC_SET_BUFFER1_SIZE(desc, length);
  300. /* Physical address to DMA to/from */
  301. desc->des2 = mem_addr;
  302. }
  303. /* Set first descriptor */
  304. desc = host->sg_cpu;
  305. desc->des0 |= IDMAC_DES0_FD;
  306. /* Set last descriptor */
  307. desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc);
  308. desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
  309. desc->des0 |= IDMAC_DES0_LD;
  310. wmb();
  311. }
  312. static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
  313. {
  314. u32 temp;
  315. dw_mci_translate_sglist(host, host->data, sg_len);
  316. /* Select IDMAC interface */
  317. temp = mci_readl(host, CTRL);
  318. temp |= SDMMC_CTRL_USE_IDMAC;
  319. mci_writel(host, CTRL, temp);
  320. wmb();
  321. /* Enable the IDMAC */
  322. temp = mci_readl(host, BMOD);
  323. temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
  324. mci_writel(host, BMOD, temp);
  325. /* Start it running */
  326. mci_writel(host, PLDMND, 1);
  327. }
  328. static int dw_mci_idmac_init(struct dw_mci *host)
  329. {
  330. struct idmac_desc *p;
  331. int i;
  332. /* Number of descriptors in the ring buffer */
  333. host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
  334. /* Forward link the descriptor list */
  335. for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++)
  336. p->des3 = host->sg_dma + (sizeof(struct idmac_desc) * (i + 1));
  337. /* Set the last descriptor as the end-of-ring descriptor */
  338. p->des3 = host->sg_dma;
  339. p->des0 = IDMAC_DES0_ER;
  340. /* Mask out interrupts - get Tx & Rx complete only */
  341. mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI | SDMMC_IDMAC_INT_RI |
  342. SDMMC_IDMAC_INT_TI);
  343. /* Set the descriptor base address */
  344. mci_writel(host, DBADDR, host->sg_dma);
  345. return 0;
  346. }
  347. static struct dw_mci_dma_ops dw_mci_idmac_ops = {
  348. .init = dw_mci_idmac_init,
  349. .start = dw_mci_idmac_start_dma,
  350. .stop = dw_mci_idmac_stop_dma,
  351. .complete = dw_mci_idmac_complete_dma,
  352. .cleanup = dw_mci_dma_cleanup,
  353. };
  354. #endif /* CONFIG_MMC_DW_IDMAC */
  355. static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
  356. {
  357. struct scatterlist *sg;
  358. unsigned int i, direction, sg_len;
  359. u32 temp;
  360. host->using_dma = 0;
  361. /* If we don't have a channel, we can't do DMA */
  362. if (!host->use_dma)
  363. return -ENODEV;
  364. /*
  365. * We don't do DMA on "complex" transfers, i.e. with
  366. * non-word-aligned buffers or lengths. Also, we don't bother
  367. * with all the DMA setup overhead for short transfers.
  368. */
  369. if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
  370. return -EINVAL;
  371. if (data->blksz & 3)
  372. return -EINVAL;
  373. for_each_sg(data->sg, sg, data->sg_len, i) {
  374. if (sg->offset & 3 || sg->length & 3)
  375. return -EINVAL;
  376. }
  377. host->using_dma = 1;
  378. if (data->flags & MMC_DATA_READ)
  379. direction = DMA_FROM_DEVICE;
  380. else
  381. direction = DMA_TO_DEVICE;
  382. sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len,
  383. direction);
  384. dev_vdbg(&host->pdev->dev,
  385. "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
  386. (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
  387. sg_len);
  388. /* Enable the DMA interface */
  389. temp = mci_readl(host, CTRL);
  390. temp |= SDMMC_CTRL_DMA_ENABLE;
  391. mci_writel(host, CTRL, temp);
  392. /* Disable RX/TX IRQs, let DMA handle it */
  393. temp = mci_readl(host, INTMASK);
  394. temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
  395. mci_writel(host, INTMASK, temp);
  396. host->dma_ops->start(host, sg_len);
  397. return 0;
  398. }
  399. static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
  400. {
  401. u32 temp;
  402. data->error = -EINPROGRESS;
  403. WARN_ON(host->data);
  404. host->sg = NULL;
  405. host->data = data;
  406. if (data->flags & MMC_DATA_READ)
  407. host->dir_status = DW_MCI_RECV_STATUS;
  408. else
  409. host->dir_status = DW_MCI_SEND_STATUS;
  410. if (dw_mci_submit_data_dma(host, data)) {
  411. host->sg = data->sg;
  412. host->pio_offset = 0;
  413. host->part_buf_start = 0;
  414. host->part_buf_count = 0;
  415. mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
  416. temp = mci_readl(host, INTMASK);
  417. temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
  418. mci_writel(host, INTMASK, temp);
  419. temp = mci_readl(host, CTRL);
  420. temp &= ~SDMMC_CTRL_DMA_ENABLE;
  421. mci_writel(host, CTRL, temp);
  422. }
  423. }
  424. static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
  425. {
  426. struct dw_mci *host = slot->host;
  427. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  428. unsigned int cmd_status = 0;
  429. mci_writel(host, CMDARG, arg);
  430. wmb();
  431. mci_writel(host, CMD, SDMMC_CMD_START | cmd);
  432. while (time_before(jiffies, timeout)) {
  433. cmd_status = mci_readl(host, CMD);
  434. if (!(cmd_status & SDMMC_CMD_START))
  435. return;
  436. }
  437. dev_err(&slot->mmc->class_dev,
  438. "Timeout sending command (cmd %#x arg %#x status %#x)\n",
  439. cmd, arg, cmd_status);
  440. }
  441. static void dw_mci_setup_bus(struct dw_mci_slot *slot)
  442. {
  443. struct dw_mci *host = slot->host;
  444. u32 div;
  445. if (slot->clock != host->current_speed) {
  446. if (host->bus_hz % slot->clock)
  447. /*
  448. * move the + 1 after the divide to prevent
  449. * over-clocking the card.
  450. */
  451. div = ((host->bus_hz / slot->clock) >> 1) + 1;
  452. else
  453. div = (host->bus_hz / slot->clock) >> 1;
  454. dev_info(&slot->mmc->class_dev,
  455. "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ"
  456. " div = %d)\n", slot->id, host->bus_hz, slot->clock,
  457. div ? ((host->bus_hz / div) >> 1) : host->bus_hz, div);
  458. /* disable clock */
  459. mci_writel(host, CLKENA, 0);
  460. mci_writel(host, CLKSRC, 0);
  461. /* inform CIU */
  462. mci_send_cmd(slot,
  463. SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
  464. /* set clock to desired speed */
  465. mci_writel(host, CLKDIV, div);
  466. /* inform CIU */
  467. mci_send_cmd(slot,
  468. SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
  469. /* enable clock */
  470. mci_writel(host, CLKENA, SDMMC_CLKEN_ENABLE |
  471. SDMMC_CLKEN_LOW_PWR);
  472. /* inform CIU */
  473. mci_send_cmd(slot,
  474. SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
  475. host->current_speed = slot->clock;
  476. }
  477. /* Set the current slot bus width */
  478. mci_writel(host, CTYPE, (slot->ctype << slot->id));
  479. }
  480. static void dw_mci_start_request(struct dw_mci *host,
  481. struct dw_mci_slot *slot)
  482. {
  483. struct mmc_request *mrq;
  484. struct mmc_command *cmd;
  485. struct mmc_data *data;
  486. u32 cmdflags;
  487. mrq = slot->mrq;
  488. if (host->pdata->select_slot)
  489. host->pdata->select_slot(slot->id);
  490. /* Slot specific timing and width adjustment */
  491. dw_mci_setup_bus(slot);
  492. host->cur_slot = slot;
  493. host->mrq = mrq;
  494. host->pending_events = 0;
  495. host->completed_events = 0;
  496. host->data_status = 0;
  497. data = mrq->data;
  498. if (data) {
  499. dw_mci_set_timeout(host);
  500. mci_writel(host, BYTCNT, data->blksz*data->blocks);
  501. mci_writel(host, BLKSIZ, data->blksz);
  502. }
  503. cmd = mrq->cmd;
  504. cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
  505. /* this is the first command, send the initialization clock */
  506. if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
  507. cmdflags |= SDMMC_CMD_INIT;
  508. if (data) {
  509. dw_mci_submit_data(host, data);
  510. wmb();
  511. }
  512. dw_mci_start_command(host, cmd, cmdflags);
  513. if (mrq->stop)
  514. host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
  515. }
  516. /* must be called with host->lock held */
  517. static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
  518. struct mmc_request *mrq)
  519. {
  520. dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
  521. host->state);
  522. slot->mrq = mrq;
  523. if (host->state == STATE_IDLE) {
  524. host->state = STATE_SENDING_CMD;
  525. dw_mci_start_request(host, slot);
  526. } else {
  527. list_add_tail(&slot->queue_node, &host->queue);
  528. }
  529. }
  530. static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  531. {
  532. struct dw_mci_slot *slot = mmc_priv(mmc);
  533. struct dw_mci *host = slot->host;
  534. WARN_ON(slot->mrq);
  535. /*
  536. * The check for card presence and queueing of the request must be
  537. * atomic, otherwise the card could be removed in between and the
  538. * request wouldn't fail until another card was inserted.
  539. */
  540. spin_lock_bh(&host->lock);
  541. if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
  542. spin_unlock_bh(&host->lock);
  543. mrq->cmd->error = -ENOMEDIUM;
  544. mmc_request_done(mmc, mrq);
  545. return;
  546. }
  547. dw_mci_queue_request(host, slot, mrq);
  548. spin_unlock_bh(&host->lock);
  549. }
  550. static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  551. {
  552. struct dw_mci_slot *slot = mmc_priv(mmc);
  553. u32 regs;
  554. /* set default 1 bit mode */
  555. slot->ctype = SDMMC_CTYPE_1BIT;
  556. switch (ios->bus_width) {
  557. case MMC_BUS_WIDTH_1:
  558. slot->ctype = SDMMC_CTYPE_1BIT;
  559. break;
  560. case MMC_BUS_WIDTH_4:
  561. slot->ctype = SDMMC_CTYPE_4BIT;
  562. break;
  563. case MMC_BUS_WIDTH_8:
  564. slot->ctype = SDMMC_CTYPE_8BIT;
  565. break;
  566. }
  567. /* DDR mode set */
  568. if (ios->timing == MMC_TIMING_UHS_DDR50) {
  569. regs = mci_readl(slot->host, UHS_REG);
  570. regs |= (0x1 << slot->id) << 16;
  571. mci_writel(slot->host, UHS_REG, regs);
  572. }
  573. if (ios->clock) {
  574. /*
  575. * Use mirror of ios->clock to prevent race with mmc
  576. * core ios update when finding the minimum.
  577. */
  578. slot->clock = ios->clock;
  579. }
  580. switch (ios->power_mode) {
  581. case MMC_POWER_UP:
  582. set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
  583. break;
  584. default:
  585. break;
  586. }
  587. }
  588. static int dw_mci_get_ro(struct mmc_host *mmc)
  589. {
  590. int read_only;
  591. struct dw_mci_slot *slot = mmc_priv(mmc);
  592. struct dw_mci_board *brd = slot->host->pdata;
  593. /* Use platform get_ro function, else try on board write protect */
  594. if (brd->get_ro)
  595. read_only = brd->get_ro(slot->id);
  596. else
  597. read_only =
  598. mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
  599. dev_dbg(&mmc->class_dev, "card is %s\n",
  600. read_only ? "read-only" : "read-write");
  601. return read_only;
  602. }
  603. static int dw_mci_get_cd(struct mmc_host *mmc)
  604. {
  605. int present;
  606. struct dw_mci_slot *slot = mmc_priv(mmc);
  607. struct dw_mci_board *brd = slot->host->pdata;
  608. /* Use platform get_cd function, else try onboard card detect */
  609. if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
  610. present = 1;
  611. else if (brd->get_cd)
  612. present = !brd->get_cd(slot->id);
  613. else
  614. present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
  615. == 0 ? 1 : 0;
  616. if (present)
  617. dev_dbg(&mmc->class_dev, "card is present\n");
  618. else
  619. dev_dbg(&mmc->class_dev, "card is not present\n");
  620. return present;
  621. }
  622. static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
  623. {
  624. struct dw_mci_slot *slot = mmc_priv(mmc);
  625. struct dw_mci *host = slot->host;
  626. u32 int_mask;
  627. /* Enable/disable Slot Specific SDIO interrupt */
  628. int_mask = mci_readl(host, INTMASK);
  629. if (enb) {
  630. mci_writel(host, INTMASK,
  631. (int_mask | (1 << SDMMC_INT_SDIO(slot->id))));
  632. } else {
  633. mci_writel(host, INTMASK,
  634. (int_mask & ~(1 << SDMMC_INT_SDIO(slot->id))));
  635. }
  636. }
  637. static const struct mmc_host_ops dw_mci_ops = {
  638. .request = dw_mci_request,
  639. .set_ios = dw_mci_set_ios,
  640. .get_ro = dw_mci_get_ro,
  641. .get_cd = dw_mci_get_cd,
  642. .enable_sdio_irq = dw_mci_enable_sdio_irq,
  643. };
  644. static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
  645. __releases(&host->lock)
  646. __acquires(&host->lock)
  647. {
  648. struct dw_mci_slot *slot;
  649. struct mmc_host *prev_mmc = host->cur_slot->mmc;
  650. WARN_ON(host->cmd || host->data);
  651. host->cur_slot->mrq = NULL;
  652. host->mrq = NULL;
  653. if (!list_empty(&host->queue)) {
  654. slot = list_entry(host->queue.next,
  655. struct dw_mci_slot, queue_node);
  656. list_del(&slot->queue_node);
  657. dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
  658. mmc_hostname(slot->mmc));
  659. host->state = STATE_SENDING_CMD;
  660. dw_mci_start_request(host, slot);
  661. } else {
  662. dev_vdbg(&host->pdev->dev, "list empty\n");
  663. host->state = STATE_IDLE;
  664. }
  665. spin_unlock(&host->lock);
  666. mmc_request_done(prev_mmc, mrq);
  667. spin_lock(&host->lock);
  668. }
  669. static void dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
  670. {
  671. u32 status = host->cmd_status;
  672. host->cmd_status = 0;
  673. /* Read the response from the card (up to 16 bytes) */
  674. if (cmd->flags & MMC_RSP_PRESENT) {
  675. if (cmd->flags & MMC_RSP_136) {
  676. cmd->resp[3] = mci_readl(host, RESP0);
  677. cmd->resp[2] = mci_readl(host, RESP1);
  678. cmd->resp[1] = mci_readl(host, RESP2);
  679. cmd->resp[0] = mci_readl(host, RESP3);
  680. } else {
  681. cmd->resp[0] = mci_readl(host, RESP0);
  682. cmd->resp[1] = 0;
  683. cmd->resp[2] = 0;
  684. cmd->resp[3] = 0;
  685. }
  686. }
  687. if (status & SDMMC_INT_RTO)
  688. cmd->error = -ETIMEDOUT;
  689. else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
  690. cmd->error = -EILSEQ;
  691. else if (status & SDMMC_INT_RESP_ERR)
  692. cmd->error = -EIO;
  693. else
  694. cmd->error = 0;
  695. if (cmd->error) {
  696. /* newer ip versions need a delay between retries */
  697. if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
  698. mdelay(20);
  699. if (cmd->data) {
  700. host->data = NULL;
  701. dw_mci_stop_dma(host);
  702. }
  703. }
  704. }
  705. static void dw_mci_tasklet_func(unsigned long priv)
  706. {
  707. struct dw_mci *host = (struct dw_mci *)priv;
  708. struct mmc_data *data;
  709. struct mmc_command *cmd;
  710. enum dw_mci_state state;
  711. enum dw_mci_state prev_state;
  712. u32 status, ctrl;
  713. spin_lock(&host->lock);
  714. state = host->state;
  715. data = host->data;
  716. do {
  717. prev_state = state;
  718. switch (state) {
  719. case STATE_IDLE:
  720. break;
  721. case STATE_SENDING_CMD:
  722. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  723. &host->pending_events))
  724. break;
  725. cmd = host->cmd;
  726. host->cmd = NULL;
  727. set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
  728. dw_mci_command_complete(host, host->mrq->cmd);
  729. if (!host->mrq->data || cmd->error) {
  730. dw_mci_request_end(host, host->mrq);
  731. goto unlock;
  732. }
  733. prev_state = state = STATE_SENDING_DATA;
  734. /* fall through */
  735. case STATE_SENDING_DATA:
  736. if (test_and_clear_bit(EVENT_DATA_ERROR,
  737. &host->pending_events)) {
  738. dw_mci_stop_dma(host);
  739. if (data->stop)
  740. send_stop_cmd(host, data);
  741. state = STATE_DATA_ERROR;
  742. break;
  743. }
  744. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  745. &host->pending_events))
  746. break;
  747. set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
  748. prev_state = state = STATE_DATA_BUSY;
  749. /* fall through */
  750. case STATE_DATA_BUSY:
  751. if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
  752. &host->pending_events))
  753. break;
  754. host->data = NULL;
  755. set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
  756. status = host->data_status;
  757. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  758. if (status & SDMMC_INT_DTO) {
  759. data->error = -ETIMEDOUT;
  760. } else if (status & SDMMC_INT_DCRC) {
  761. data->error = -EILSEQ;
  762. } else if (status & SDMMC_INT_EBE &&
  763. host->dir_status ==
  764. DW_MCI_SEND_STATUS) {
  765. /*
  766. * No data CRC status was returned.
  767. * The number of bytes transferred will
  768. * be exaggerated in PIO mode.
  769. */
  770. data->bytes_xfered = 0;
  771. data->error = -ETIMEDOUT;
  772. } else {
  773. dev_err(&host->pdev->dev,
  774. "data FIFO error "
  775. "(status=%08x)\n",
  776. status);
  777. data->error = -EIO;
  778. }
  779. /*
  780. * After an error, there may be data lingering
  781. * in the FIFO, so reset it - doing so
  782. * generates a block interrupt, hence setting
  783. * the scatter-gather pointer to NULL.
  784. */
  785. host->sg = NULL;
  786. ctrl = mci_readl(host, CTRL);
  787. ctrl |= SDMMC_CTRL_FIFO_RESET;
  788. mci_writel(host, CTRL, ctrl);
  789. } else {
  790. data->bytes_xfered = data->blocks * data->blksz;
  791. data->error = 0;
  792. }
  793. if (!data->stop) {
  794. dw_mci_request_end(host, host->mrq);
  795. goto unlock;
  796. }
  797. prev_state = state = STATE_SENDING_STOP;
  798. if (!data->error)
  799. send_stop_cmd(host, data);
  800. /* fall through */
  801. case STATE_SENDING_STOP:
  802. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  803. &host->pending_events))
  804. break;
  805. host->cmd = NULL;
  806. dw_mci_command_complete(host, host->mrq->stop);
  807. dw_mci_request_end(host, host->mrq);
  808. goto unlock;
  809. case STATE_DATA_ERROR:
  810. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  811. &host->pending_events))
  812. break;
  813. state = STATE_DATA_BUSY;
  814. break;
  815. }
  816. } while (state != prev_state);
  817. host->state = state;
  818. unlock:
  819. spin_unlock(&host->lock);
  820. }
  821. /* push final bytes to part_buf, only use during push */
  822. static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
  823. {
  824. memcpy((void *)&host->part_buf, buf, cnt);
  825. host->part_buf_count = cnt;
  826. }
  827. /* append bytes to part_buf, only use during push */
  828. static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
  829. {
  830. cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
  831. memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
  832. host->part_buf_count += cnt;
  833. return cnt;
  834. }
  835. /* pull first bytes from part_buf, only use during pull */
  836. static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
  837. {
  838. cnt = min(cnt, (int)host->part_buf_count);
  839. if (cnt) {
  840. memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
  841. cnt);
  842. host->part_buf_count -= cnt;
  843. host->part_buf_start += cnt;
  844. }
  845. return cnt;
  846. }
  847. /* pull final bytes from the part_buf, assuming it's just been filled */
  848. static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
  849. {
  850. memcpy(buf, &host->part_buf, cnt);
  851. host->part_buf_start = cnt;
  852. host->part_buf_count = (1 << host->data_shift) - cnt;
  853. }
  854. static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
  855. {
  856. /* try and push anything in the part_buf */
  857. if (unlikely(host->part_buf_count)) {
  858. int len = dw_mci_push_part_bytes(host, buf, cnt);
  859. buf += len;
  860. cnt -= len;
  861. if (!sg_next(host->sg) || host->part_buf_count == 2) {
  862. mci_writew(host, DATA(host->data_offset),
  863. host->part_buf16);
  864. host->part_buf_count = 0;
  865. }
  866. }
  867. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  868. if (unlikely((unsigned long)buf & 0x1)) {
  869. while (cnt >= 2) {
  870. u16 aligned_buf[64];
  871. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  872. int items = len >> 1;
  873. int i;
  874. /* memcpy from input buffer into aligned buffer */
  875. memcpy(aligned_buf, buf, len);
  876. buf += len;
  877. cnt -= len;
  878. /* push data from aligned buffer into fifo */
  879. for (i = 0; i < items; ++i)
  880. mci_writew(host, DATA(host->data_offset),
  881. aligned_buf[i]);
  882. }
  883. } else
  884. #endif
  885. {
  886. u16 *pdata = buf;
  887. for (; cnt >= 2; cnt -= 2)
  888. mci_writew(host, DATA(host->data_offset), *pdata++);
  889. buf = pdata;
  890. }
  891. /* put anything remaining in the part_buf */
  892. if (cnt) {
  893. dw_mci_set_part_bytes(host, buf, cnt);
  894. if (!sg_next(host->sg))
  895. mci_writew(host, DATA(host->data_offset),
  896. host->part_buf16);
  897. }
  898. }
  899. static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
  900. {
  901. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  902. if (unlikely((unsigned long)buf & 0x1)) {
  903. while (cnt >= 2) {
  904. /* pull data from fifo into aligned buffer */
  905. u16 aligned_buf[64];
  906. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  907. int items = len >> 1;
  908. int i;
  909. for (i = 0; i < items; ++i)
  910. aligned_buf[i] = mci_readw(host,
  911. DATA(host->data_offset));
  912. /* memcpy from aligned buffer into output buffer */
  913. memcpy(buf, aligned_buf, len);
  914. buf += len;
  915. cnt -= len;
  916. }
  917. } else
  918. #endif
  919. {
  920. u16 *pdata = buf;
  921. for (; cnt >= 2; cnt -= 2)
  922. *pdata++ = mci_readw(host, DATA(host->data_offset));
  923. buf = pdata;
  924. }
  925. if (cnt) {
  926. host->part_buf16 = mci_readw(host, DATA(host->data_offset));
  927. dw_mci_pull_final_bytes(host, buf, cnt);
  928. }
  929. }
  930. static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
  931. {
  932. /* try and push anything in the part_buf */
  933. if (unlikely(host->part_buf_count)) {
  934. int len = dw_mci_push_part_bytes(host, buf, cnt);
  935. buf += len;
  936. cnt -= len;
  937. if (!sg_next(host->sg) || host->part_buf_count == 4) {
  938. mci_writel(host, DATA(host->data_offset),
  939. host->part_buf32);
  940. host->part_buf_count = 0;
  941. }
  942. }
  943. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  944. if (unlikely((unsigned long)buf & 0x3)) {
  945. while (cnt >= 4) {
  946. u32 aligned_buf[32];
  947. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  948. int items = len >> 2;
  949. int i;
  950. /* memcpy from input buffer into aligned buffer */
  951. memcpy(aligned_buf, buf, len);
  952. buf += len;
  953. cnt -= len;
  954. /* push data from aligned buffer into fifo */
  955. for (i = 0; i < items; ++i)
  956. mci_writel(host, DATA(host->data_offset),
  957. aligned_buf[i]);
  958. }
  959. } else
  960. #endif
  961. {
  962. u32 *pdata = buf;
  963. for (; cnt >= 4; cnt -= 4)
  964. mci_writel(host, DATA(host->data_offset), *pdata++);
  965. buf = pdata;
  966. }
  967. /* put anything remaining in the part_buf */
  968. if (cnt) {
  969. dw_mci_set_part_bytes(host, buf, cnt);
  970. if (!sg_next(host->sg))
  971. mci_writel(host, DATA(host->data_offset),
  972. host->part_buf32);
  973. }
  974. }
  975. static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
  976. {
  977. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  978. if (unlikely((unsigned long)buf & 0x3)) {
  979. while (cnt >= 4) {
  980. /* pull data from fifo into aligned buffer */
  981. u32 aligned_buf[32];
  982. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  983. int items = len >> 2;
  984. int i;
  985. for (i = 0; i < items; ++i)
  986. aligned_buf[i] = mci_readl(host,
  987. DATA(host->data_offset));
  988. /* memcpy from aligned buffer into output buffer */
  989. memcpy(buf, aligned_buf, len);
  990. buf += len;
  991. cnt -= len;
  992. }
  993. } else
  994. #endif
  995. {
  996. u32 *pdata = buf;
  997. for (; cnt >= 4; cnt -= 4)
  998. *pdata++ = mci_readl(host, DATA(host->data_offset));
  999. buf = pdata;
  1000. }
  1001. if (cnt) {
  1002. host->part_buf32 = mci_readl(host, DATA(host->data_offset));
  1003. dw_mci_pull_final_bytes(host, buf, cnt);
  1004. }
  1005. }
  1006. static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
  1007. {
  1008. /* try and push anything in the part_buf */
  1009. if (unlikely(host->part_buf_count)) {
  1010. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1011. buf += len;
  1012. cnt -= len;
  1013. if (!sg_next(host->sg) || host->part_buf_count == 8) {
  1014. mci_writew(host, DATA(host->data_offset),
  1015. host->part_buf);
  1016. host->part_buf_count = 0;
  1017. }
  1018. }
  1019. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1020. if (unlikely((unsigned long)buf & 0x7)) {
  1021. while (cnt >= 8) {
  1022. u64 aligned_buf[16];
  1023. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1024. int items = len >> 3;
  1025. int i;
  1026. /* memcpy from input buffer into aligned buffer */
  1027. memcpy(aligned_buf, buf, len);
  1028. buf += len;
  1029. cnt -= len;
  1030. /* push data from aligned buffer into fifo */
  1031. for (i = 0; i < items; ++i)
  1032. mci_writeq(host, DATA(host->data_offset),
  1033. aligned_buf[i]);
  1034. }
  1035. } else
  1036. #endif
  1037. {
  1038. u64 *pdata = buf;
  1039. for (; cnt >= 8; cnt -= 8)
  1040. mci_writeq(host, DATA(host->data_offset), *pdata++);
  1041. buf = pdata;
  1042. }
  1043. /* put anything remaining in the part_buf */
  1044. if (cnt) {
  1045. dw_mci_set_part_bytes(host, buf, cnt);
  1046. if (!sg_next(host->sg))
  1047. mci_writeq(host, DATA(host->data_offset),
  1048. host->part_buf);
  1049. }
  1050. }
  1051. static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
  1052. {
  1053. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1054. if (unlikely((unsigned long)buf & 0x7)) {
  1055. while (cnt >= 8) {
  1056. /* pull data from fifo into aligned buffer */
  1057. u64 aligned_buf[16];
  1058. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1059. int items = len >> 3;
  1060. int i;
  1061. for (i = 0; i < items; ++i)
  1062. aligned_buf[i] = mci_readq(host,
  1063. DATA(host->data_offset));
  1064. /* memcpy from aligned buffer into output buffer */
  1065. memcpy(buf, aligned_buf, len);
  1066. buf += len;
  1067. cnt -= len;
  1068. }
  1069. } else
  1070. #endif
  1071. {
  1072. u64 *pdata = buf;
  1073. for (; cnt >= 8; cnt -= 8)
  1074. *pdata++ = mci_readq(host, DATA(host->data_offset));
  1075. buf = pdata;
  1076. }
  1077. if (cnt) {
  1078. host->part_buf = mci_readq(host, DATA(host->data_offset));
  1079. dw_mci_pull_final_bytes(host, buf, cnt);
  1080. }
  1081. }
  1082. static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
  1083. {
  1084. int len;
  1085. /* get remaining partial bytes */
  1086. len = dw_mci_pull_part_bytes(host, buf, cnt);
  1087. if (unlikely(len == cnt))
  1088. return;
  1089. buf += len;
  1090. cnt -= len;
  1091. /* get the rest of the data */
  1092. host->pull_data(host, buf, cnt);
  1093. }
  1094. static void dw_mci_read_data_pio(struct dw_mci *host)
  1095. {
  1096. struct scatterlist *sg = host->sg;
  1097. void *buf = sg_virt(sg);
  1098. unsigned int offset = host->pio_offset;
  1099. struct mmc_data *data = host->data;
  1100. int shift = host->data_shift;
  1101. u32 status;
  1102. unsigned int nbytes = 0, len;
  1103. do {
  1104. len = host->part_buf_count +
  1105. (SDMMC_GET_FCNT(mci_readl(host, STATUS)) << shift);
  1106. if (offset + len <= sg->length) {
  1107. dw_mci_pull_data(host, (void *)(buf + offset), len);
  1108. offset += len;
  1109. nbytes += len;
  1110. if (offset == sg->length) {
  1111. flush_dcache_page(sg_page(sg));
  1112. host->sg = sg = sg_next(sg);
  1113. if (!sg)
  1114. goto done;
  1115. offset = 0;
  1116. buf = sg_virt(sg);
  1117. }
  1118. } else {
  1119. unsigned int remaining = sg->length - offset;
  1120. dw_mci_pull_data(host, (void *)(buf + offset),
  1121. remaining);
  1122. nbytes += remaining;
  1123. flush_dcache_page(sg_page(sg));
  1124. host->sg = sg = sg_next(sg);
  1125. if (!sg)
  1126. goto done;
  1127. offset = len - remaining;
  1128. buf = sg_virt(sg);
  1129. dw_mci_pull_data(host, buf, offset);
  1130. nbytes += offset;
  1131. }
  1132. status = mci_readl(host, MINTSTS);
  1133. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  1134. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  1135. host->data_status = status;
  1136. data->bytes_xfered += nbytes;
  1137. smp_wmb();
  1138. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  1139. tasklet_schedule(&host->tasklet);
  1140. return;
  1141. }
  1142. } while (status & SDMMC_INT_RXDR); /*if the RXDR is ready read again*/
  1143. host->pio_offset = offset;
  1144. data->bytes_xfered += nbytes;
  1145. return;
  1146. done:
  1147. data->bytes_xfered += nbytes;
  1148. smp_wmb();
  1149. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  1150. }
  1151. static void dw_mci_write_data_pio(struct dw_mci *host)
  1152. {
  1153. struct scatterlist *sg = host->sg;
  1154. void *buf = sg_virt(sg);
  1155. unsigned int offset = host->pio_offset;
  1156. struct mmc_data *data = host->data;
  1157. int shift = host->data_shift;
  1158. u32 status;
  1159. unsigned int nbytes = 0, len;
  1160. do {
  1161. len = ((host->fifo_depth -
  1162. SDMMC_GET_FCNT(mci_readl(host, STATUS))) << shift)
  1163. - host->part_buf_count;
  1164. if (offset + len <= sg->length) {
  1165. host->push_data(host, (void *)(buf + offset), len);
  1166. offset += len;
  1167. nbytes += len;
  1168. if (offset == sg->length) {
  1169. host->sg = sg = sg_next(sg);
  1170. if (!sg)
  1171. goto done;
  1172. offset = 0;
  1173. buf = sg_virt(sg);
  1174. }
  1175. } else {
  1176. unsigned int remaining = sg->length - offset;
  1177. host->push_data(host, (void *)(buf + offset),
  1178. remaining);
  1179. nbytes += remaining;
  1180. host->sg = sg = sg_next(sg);
  1181. if (!sg)
  1182. goto done;
  1183. offset = len - remaining;
  1184. buf = sg_virt(sg);
  1185. host->push_data(host, (void *)buf, offset);
  1186. nbytes += offset;
  1187. }
  1188. status = mci_readl(host, MINTSTS);
  1189. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1190. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  1191. host->data_status = status;
  1192. data->bytes_xfered += nbytes;
  1193. smp_wmb();
  1194. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  1195. tasklet_schedule(&host->tasklet);
  1196. return;
  1197. }
  1198. } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
  1199. host->pio_offset = offset;
  1200. data->bytes_xfered += nbytes;
  1201. return;
  1202. done:
  1203. data->bytes_xfered += nbytes;
  1204. smp_wmb();
  1205. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  1206. }
  1207. static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
  1208. {
  1209. if (!host->cmd_status)
  1210. host->cmd_status = status;
  1211. smp_wmb();
  1212. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1213. tasklet_schedule(&host->tasklet);
  1214. }
  1215. static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
  1216. {
  1217. struct dw_mci *host = dev_id;
  1218. u32 status, pending;
  1219. unsigned int pass_count = 0;
  1220. int i;
  1221. do {
  1222. status = mci_readl(host, RINTSTS);
  1223. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  1224. /*
  1225. * DTO fix - version 2.10a and below, and only if internal DMA
  1226. * is configured.
  1227. */
  1228. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
  1229. if (!pending &&
  1230. ((mci_readl(host, STATUS) >> 17) & 0x1fff))
  1231. pending |= SDMMC_INT_DATA_OVER;
  1232. }
  1233. if (!pending)
  1234. break;
  1235. if (pending & DW_MCI_CMD_ERROR_FLAGS) {
  1236. mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
  1237. host->cmd_status = status;
  1238. smp_wmb();
  1239. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1240. }
  1241. if (pending & DW_MCI_DATA_ERROR_FLAGS) {
  1242. /* if there is an error report DATA_ERROR */
  1243. mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
  1244. host->data_status = status;
  1245. smp_wmb();
  1246. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  1247. if (!(pending & (SDMMC_INT_DTO | SDMMC_INT_DCRC |
  1248. SDMMC_INT_SBE | SDMMC_INT_EBE)))
  1249. tasklet_schedule(&host->tasklet);
  1250. }
  1251. if (pending & SDMMC_INT_DATA_OVER) {
  1252. mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
  1253. if (!host->data_status)
  1254. host->data_status = status;
  1255. smp_wmb();
  1256. if (host->dir_status == DW_MCI_RECV_STATUS) {
  1257. if (host->sg != NULL)
  1258. dw_mci_read_data_pio(host);
  1259. }
  1260. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  1261. tasklet_schedule(&host->tasklet);
  1262. }
  1263. if (pending & SDMMC_INT_RXDR) {
  1264. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  1265. if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
  1266. dw_mci_read_data_pio(host);
  1267. }
  1268. if (pending & SDMMC_INT_TXDR) {
  1269. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1270. if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
  1271. dw_mci_write_data_pio(host);
  1272. }
  1273. if (pending & SDMMC_INT_CMD_DONE) {
  1274. mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
  1275. dw_mci_cmd_interrupt(host, status);
  1276. }
  1277. if (pending & SDMMC_INT_CD) {
  1278. mci_writel(host, RINTSTS, SDMMC_INT_CD);
  1279. queue_work(dw_mci_card_workqueue, &host->card_work);
  1280. }
  1281. /* Handle SDIO Interrupts */
  1282. for (i = 0; i < host->num_slots; i++) {
  1283. struct dw_mci_slot *slot = host->slot[i];
  1284. if (pending & SDMMC_INT_SDIO(i)) {
  1285. mci_writel(host, RINTSTS, SDMMC_INT_SDIO(i));
  1286. mmc_signal_sdio_irq(slot->mmc);
  1287. }
  1288. }
  1289. } while (pass_count++ < 5);
  1290. #ifdef CONFIG_MMC_DW_IDMAC
  1291. /* Handle DMA interrupts */
  1292. pending = mci_readl(host, IDSTS);
  1293. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  1294. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI);
  1295. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
  1296. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  1297. host->dma_ops->complete(host);
  1298. }
  1299. #endif
  1300. return IRQ_HANDLED;
  1301. }
  1302. static void dw_mci_work_routine_card(struct work_struct *work)
  1303. {
  1304. struct dw_mci *host = container_of(work, struct dw_mci, card_work);
  1305. int i;
  1306. for (i = 0; i < host->num_slots; i++) {
  1307. struct dw_mci_slot *slot = host->slot[i];
  1308. struct mmc_host *mmc = slot->mmc;
  1309. struct mmc_request *mrq;
  1310. int present;
  1311. u32 ctrl;
  1312. present = dw_mci_get_cd(mmc);
  1313. while (present != slot->last_detect_state) {
  1314. dev_dbg(&slot->mmc->class_dev, "card %s\n",
  1315. present ? "inserted" : "removed");
  1316. /* Power up slot (before spin_lock, may sleep) */
  1317. if (present != 0 && host->pdata->setpower)
  1318. host->pdata->setpower(slot->id, mmc->ocr_avail);
  1319. spin_lock_bh(&host->lock);
  1320. /* Card change detected */
  1321. slot->last_detect_state = present;
  1322. /* Mark card as present if applicable */
  1323. if (present != 0)
  1324. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1325. /* Clean up queue if present */
  1326. mrq = slot->mrq;
  1327. if (mrq) {
  1328. if (mrq == host->mrq) {
  1329. host->data = NULL;
  1330. host->cmd = NULL;
  1331. switch (host->state) {
  1332. case STATE_IDLE:
  1333. break;
  1334. case STATE_SENDING_CMD:
  1335. mrq->cmd->error = -ENOMEDIUM;
  1336. if (!mrq->data)
  1337. break;
  1338. /* fall through */
  1339. case STATE_SENDING_DATA:
  1340. mrq->data->error = -ENOMEDIUM;
  1341. dw_mci_stop_dma(host);
  1342. break;
  1343. case STATE_DATA_BUSY:
  1344. case STATE_DATA_ERROR:
  1345. if (mrq->data->error == -EINPROGRESS)
  1346. mrq->data->error = -ENOMEDIUM;
  1347. if (!mrq->stop)
  1348. break;
  1349. /* fall through */
  1350. case STATE_SENDING_STOP:
  1351. mrq->stop->error = -ENOMEDIUM;
  1352. break;
  1353. }
  1354. dw_mci_request_end(host, mrq);
  1355. } else {
  1356. list_del(&slot->queue_node);
  1357. mrq->cmd->error = -ENOMEDIUM;
  1358. if (mrq->data)
  1359. mrq->data->error = -ENOMEDIUM;
  1360. if (mrq->stop)
  1361. mrq->stop->error = -ENOMEDIUM;
  1362. spin_unlock(&host->lock);
  1363. mmc_request_done(slot->mmc, mrq);
  1364. spin_lock(&host->lock);
  1365. }
  1366. }
  1367. /* Power down slot */
  1368. if (present == 0) {
  1369. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1370. /*
  1371. * Clear down the FIFO - doing so generates a
  1372. * block interrupt, hence setting the
  1373. * scatter-gather pointer to NULL.
  1374. */
  1375. host->sg = NULL;
  1376. ctrl = mci_readl(host, CTRL);
  1377. ctrl |= SDMMC_CTRL_FIFO_RESET;
  1378. mci_writel(host, CTRL, ctrl);
  1379. #ifdef CONFIG_MMC_DW_IDMAC
  1380. ctrl = mci_readl(host, BMOD);
  1381. ctrl |= 0x01; /* Software reset of DMA */
  1382. mci_writel(host, BMOD, ctrl);
  1383. #endif
  1384. }
  1385. spin_unlock_bh(&host->lock);
  1386. /* Power down slot (after spin_unlock, may sleep) */
  1387. if (present == 0 && host->pdata->setpower)
  1388. host->pdata->setpower(slot->id, 0);
  1389. present = dw_mci_get_cd(mmc);
  1390. }
  1391. mmc_detect_change(slot->mmc,
  1392. msecs_to_jiffies(host->pdata->detect_delay_ms));
  1393. }
  1394. }
  1395. static int __init dw_mci_init_slot(struct dw_mci *host, unsigned int id)
  1396. {
  1397. struct mmc_host *mmc;
  1398. struct dw_mci_slot *slot;
  1399. mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), &host->pdev->dev);
  1400. if (!mmc)
  1401. return -ENOMEM;
  1402. slot = mmc_priv(mmc);
  1403. slot->id = id;
  1404. slot->mmc = mmc;
  1405. slot->host = host;
  1406. mmc->ops = &dw_mci_ops;
  1407. mmc->f_min = DIV_ROUND_UP(host->bus_hz, 510);
  1408. mmc->f_max = host->bus_hz;
  1409. if (host->pdata->get_ocr)
  1410. mmc->ocr_avail = host->pdata->get_ocr(id);
  1411. else
  1412. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1413. /*
  1414. * Start with slot power disabled, it will be enabled when a card
  1415. * is detected.
  1416. */
  1417. if (host->pdata->setpower)
  1418. host->pdata->setpower(id, 0);
  1419. if (host->pdata->caps)
  1420. mmc->caps = host->pdata->caps;
  1421. else
  1422. mmc->caps = 0;
  1423. if (host->pdata->caps2)
  1424. mmc->caps2 = host->pdata->caps2;
  1425. else
  1426. mmc->caps2 = 0;
  1427. if (host->pdata->get_bus_wd)
  1428. if (host->pdata->get_bus_wd(slot->id) >= 4)
  1429. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1430. if (host->pdata->quirks & DW_MCI_QUIRK_HIGHSPEED)
  1431. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  1432. #ifdef CONFIG_MMC_DW_IDMAC
  1433. mmc->max_segs = host->ring_size;
  1434. mmc->max_blk_size = 65536;
  1435. mmc->max_blk_count = host->ring_size;
  1436. mmc->max_seg_size = 0x1000;
  1437. mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count;
  1438. #else
  1439. if (host->pdata->blk_settings) {
  1440. mmc->max_segs = host->pdata->blk_settings->max_segs;
  1441. mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
  1442. mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
  1443. mmc->max_req_size = host->pdata->blk_settings->max_req_size;
  1444. mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
  1445. } else {
  1446. /* Useful defaults if platform data is unset. */
  1447. mmc->max_segs = 64;
  1448. mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
  1449. mmc->max_blk_count = 512;
  1450. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1451. mmc->max_seg_size = mmc->max_req_size;
  1452. }
  1453. #endif /* CONFIG_MMC_DW_IDMAC */
  1454. host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
  1455. if (IS_ERR(host->vmmc)) {
  1456. pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
  1457. host->vmmc = NULL;
  1458. } else
  1459. regulator_enable(host->vmmc);
  1460. if (dw_mci_get_cd(mmc))
  1461. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1462. else
  1463. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1464. host->slot[id] = slot;
  1465. mmc_add_host(mmc);
  1466. #if defined(CONFIG_DEBUG_FS)
  1467. dw_mci_init_debugfs(slot);
  1468. #endif
  1469. /* Card initially undetected */
  1470. slot->last_detect_state = 0;
  1471. /*
  1472. * Card may have been plugged in prior to boot so we
  1473. * need to run the detect tasklet
  1474. */
  1475. queue_work(dw_mci_card_workqueue, &host->card_work);
  1476. return 0;
  1477. }
  1478. static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
  1479. {
  1480. /* Shutdown detect IRQ */
  1481. if (slot->host->pdata->exit)
  1482. slot->host->pdata->exit(id);
  1483. /* Debugfs stuff is cleaned up by mmc core */
  1484. mmc_remove_host(slot->mmc);
  1485. slot->host->slot[id] = NULL;
  1486. mmc_free_host(slot->mmc);
  1487. }
  1488. static void dw_mci_init_dma(struct dw_mci *host)
  1489. {
  1490. /* Alloc memory for sg translation */
  1491. host->sg_cpu = dma_alloc_coherent(&host->pdev->dev, PAGE_SIZE,
  1492. &host->sg_dma, GFP_KERNEL);
  1493. if (!host->sg_cpu) {
  1494. dev_err(&host->pdev->dev, "%s: could not alloc DMA memory\n",
  1495. __func__);
  1496. goto no_dma;
  1497. }
  1498. /* Determine which DMA interface to use */
  1499. #ifdef CONFIG_MMC_DW_IDMAC
  1500. host->dma_ops = &dw_mci_idmac_ops;
  1501. dev_info(&host->pdev->dev, "Using internal DMA controller.\n");
  1502. #endif
  1503. if (!host->dma_ops)
  1504. goto no_dma;
  1505. if (host->dma_ops->init) {
  1506. if (host->dma_ops->init(host)) {
  1507. dev_err(&host->pdev->dev, "%s: Unable to initialize "
  1508. "DMA Controller.\n", __func__);
  1509. goto no_dma;
  1510. }
  1511. } else {
  1512. dev_err(&host->pdev->dev, "DMA initialization not found.\n");
  1513. goto no_dma;
  1514. }
  1515. host->use_dma = 1;
  1516. return;
  1517. no_dma:
  1518. dev_info(&host->pdev->dev, "Using PIO mode.\n");
  1519. host->use_dma = 0;
  1520. return;
  1521. }
  1522. static bool mci_wait_reset(struct device *dev, struct dw_mci *host)
  1523. {
  1524. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  1525. unsigned int ctrl;
  1526. mci_writel(host, CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
  1527. SDMMC_CTRL_DMA_RESET));
  1528. /* wait till resets clear */
  1529. do {
  1530. ctrl = mci_readl(host, CTRL);
  1531. if (!(ctrl & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
  1532. SDMMC_CTRL_DMA_RESET)))
  1533. return true;
  1534. } while (time_before(jiffies, timeout));
  1535. dev_err(dev, "Timeout resetting block (ctrl %#x)\n", ctrl);
  1536. return false;
  1537. }
  1538. static int dw_mci_probe(struct platform_device *pdev)
  1539. {
  1540. struct dw_mci *host;
  1541. struct resource *regs;
  1542. struct dw_mci_board *pdata;
  1543. int irq, ret, i, width;
  1544. u32 fifo_size;
  1545. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1546. if (!regs)
  1547. return -ENXIO;
  1548. irq = platform_get_irq(pdev, 0);
  1549. if (irq < 0)
  1550. return irq;
  1551. host = kzalloc(sizeof(struct dw_mci), GFP_KERNEL);
  1552. if (!host)
  1553. return -ENOMEM;
  1554. host->pdev = pdev;
  1555. host->pdata = pdata = pdev->dev.platform_data;
  1556. if (!pdata || !pdata->init) {
  1557. dev_err(&pdev->dev,
  1558. "Platform data must supply init function\n");
  1559. ret = -ENODEV;
  1560. goto err_freehost;
  1561. }
  1562. if (!pdata->select_slot && pdata->num_slots > 1) {
  1563. dev_err(&pdev->dev,
  1564. "Platform data must supply select_slot function\n");
  1565. ret = -ENODEV;
  1566. goto err_freehost;
  1567. }
  1568. if (!pdata->bus_hz) {
  1569. dev_err(&pdev->dev,
  1570. "Platform data must supply bus speed\n");
  1571. ret = -ENODEV;
  1572. goto err_freehost;
  1573. }
  1574. host->bus_hz = pdata->bus_hz;
  1575. host->quirks = pdata->quirks;
  1576. spin_lock_init(&host->lock);
  1577. INIT_LIST_HEAD(&host->queue);
  1578. ret = -ENOMEM;
  1579. host->regs = ioremap(regs->start, resource_size(regs));
  1580. if (!host->regs)
  1581. goto err_freehost;
  1582. host->dma_ops = pdata->dma_ops;
  1583. dw_mci_init_dma(host);
  1584. /*
  1585. * Get the host data width - this assumes that HCON has been set with
  1586. * the correct values.
  1587. */
  1588. i = (mci_readl(host, HCON) >> 7) & 0x7;
  1589. if (!i) {
  1590. host->push_data = dw_mci_push_data16;
  1591. host->pull_data = dw_mci_pull_data16;
  1592. width = 16;
  1593. host->data_shift = 1;
  1594. } else if (i == 2) {
  1595. host->push_data = dw_mci_push_data64;
  1596. host->pull_data = dw_mci_pull_data64;
  1597. width = 64;
  1598. host->data_shift = 3;
  1599. } else {
  1600. /* Check for a reserved value, and warn if it is */
  1601. WARN((i != 1),
  1602. "HCON reports a reserved host data width!\n"
  1603. "Defaulting to 32-bit access.\n");
  1604. host->push_data = dw_mci_push_data32;
  1605. host->pull_data = dw_mci_pull_data32;
  1606. width = 32;
  1607. host->data_shift = 2;
  1608. }
  1609. /* Reset all blocks */
  1610. if (!mci_wait_reset(&pdev->dev, host)) {
  1611. ret = -ENODEV;
  1612. goto err_dmaunmap;
  1613. }
  1614. /* Clear the interrupts for the host controller */
  1615. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1616. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  1617. /* Put in max timeout */
  1618. mci_writel(host, TMOUT, 0xFFFFFFFF);
  1619. /*
  1620. * FIFO threshold settings RxMark = fifo_size / 2 - 1,
  1621. * Tx Mark = fifo_size / 2 DMA Size = 8
  1622. */
  1623. if (!host->pdata->fifo_depth) {
  1624. /*
  1625. * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
  1626. * have been overwritten by the bootloader, just like we're
  1627. * about to do, so if you know the value for your hardware, you
  1628. * should put it in the platform data.
  1629. */
  1630. fifo_size = mci_readl(host, FIFOTH);
  1631. fifo_size = 1 + ((fifo_size >> 16) & 0x7ff);
  1632. } else {
  1633. fifo_size = host->pdata->fifo_depth;
  1634. }
  1635. host->fifo_depth = fifo_size;
  1636. host->fifoth_val = ((0x2 << 28) | ((fifo_size/2 - 1) << 16) |
  1637. ((fifo_size/2) << 0));
  1638. mci_writel(host, FIFOTH, host->fifoth_val);
  1639. /* disable clock to CIU */
  1640. mci_writel(host, CLKENA, 0);
  1641. mci_writel(host, CLKSRC, 0);
  1642. tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
  1643. dw_mci_card_workqueue = alloc_workqueue("dw-mci-card",
  1644. WQ_MEM_RECLAIM | WQ_NON_REENTRANT, 1);
  1645. if (!dw_mci_card_workqueue)
  1646. goto err_dmaunmap;
  1647. INIT_WORK(&host->card_work, dw_mci_work_routine_card);
  1648. ret = request_irq(irq, dw_mci_interrupt, 0, "dw-mci", host);
  1649. if (ret)
  1650. goto err_workqueue;
  1651. platform_set_drvdata(pdev, host);
  1652. if (host->pdata->num_slots)
  1653. host->num_slots = host->pdata->num_slots;
  1654. else
  1655. host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
  1656. /* We need at least one slot to succeed */
  1657. for (i = 0; i < host->num_slots; i++) {
  1658. ret = dw_mci_init_slot(host, i);
  1659. if (ret) {
  1660. ret = -ENODEV;
  1661. goto err_init_slot;
  1662. }
  1663. }
  1664. /*
  1665. * In 2.40a spec, Data offset is changed.
  1666. * Need to check the version-id and set data-offset for DATA register.
  1667. */
  1668. host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
  1669. dev_info(&pdev->dev, "Version ID is %04x\n", host->verid);
  1670. if (host->verid < DW_MMC_240A)
  1671. host->data_offset = DATA_OFFSET;
  1672. else
  1673. host->data_offset = DATA_240A_OFFSET;
  1674. /*
  1675. * Enable interrupts for command done, data over, data empty, card det,
  1676. * receive ready and error such as transmit, receive timeout, crc error
  1677. */
  1678. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1679. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  1680. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  1681. DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
  1682. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
  1683. dev_info(&pdev->dev, "DW MMC controller at irq %d, "
  1684. "%d bit host data width, "
  1685. "%u deep fifo\n",
  1686. irq, width, fifo_size);
  1687. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
  1688. dev_info(&pdev->dev, "Internal DMAC interrupt fix enabled.\n");
  1689. return 0;
  1690. err_init_slot:
  1691. /* De-init any initialized slots */
  1692. while (i > 0) {
  1693. if (host->slot[i])
  1694. dw_mci_cleanup_slot(host->slot[i], i);
  1695. i--;
  1696. }
  1697. free_irq(irq, host);
  1698. err_workqueue:
  1699. destroy_workqueue(dw_mci_card_workqueue);
  1700. err_dmaunmap:
  1701. if (host->use_dma && host->dma_ops->exit)
  1702. host->dma_ops->exit(host);
  1703. dma_free_coherent(&host->pdev->dev, PAGE_SIZE,
  1704. host->sg_cpu, host->sg_dma);
  1705. iounmap(host->regs);
  1706. if (host->vmmc) {
  1707. regulator_disable(host->vmmc);
  1708. regulator_put(host->vmmc);
  1709. }
  1710. err_freehost:
  1711. kfree(host);
  1712. return ret;
  1713. }
  1714. static int __exit dw_mci_remove(struct platform_device *pdev)
  1715. {
  1716. struct dw_mci *host = platform_get_drvdata(pdev);
  1717. int i;
  1718. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1719. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  1720. platform_set_drvdata(pdev, NULL);
  1721. for (i = 0; i < host->num_slots; i++) {
  1722. dev_dbg(&pdev->dev, "remove slot %d\n", i);
  1723. if (host->slot[i])
  1724. dw_mci_cleanup_slot(host->slot[i], i);
  1725. }
  1726. /* disable clock to CIU */
  1727. mci_writel(host, CLKENA, 0);
  1728. mci_writel(host, CLKSRC, 0);
  1729. free_irq(platform_get_irq(pdev, 0), host);
  1730. destroy_workqueue(dw_mci_card_workqueue);
  1731. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  1732. if (host->use_dma && host->dma_ops->exit)
  1733. host->dma_ops->exit(host);
  1734. if (host->vmmc) {
  1735. regulator_disable(host->vmmc);
  1736. regulator_put(host->vmmc);
  1737. }
  1738. iounmap(host->regs);
  1739. kfree(host);
  1740. return 0;
  1741. }
  1742. #ifdef CONFIG_PM_SLEEP
  1743. /*
  1744. * TODO: we should probably disable the clock to the card in the suspend path.
  1745. */
  1746. static int dw_mci_suspend(struct device *dev)
  1747. {
  1748. int i, ret;
  1749. struct dw_mci *host = dev_get_drvdata(dev);
  1750. for (i = 0; i < host->num_slots; i++) {
  1751. struct dw_mci_slot *slot = host->slot[i];
  1752. if (!slot)
  1753. continue;
  1754. ret = mmc_suspend_host(slot->mmc);
  1755. if (ret < 0) {
  1756. while (--i >= 0) {
  1757. slot = host->slot[i];
  1758. if (slot)
  1759. mmc_resume_host(host->slot[i]->mmc);
  1760. }
  1761. return ret;
  1762. }
  1763. }
  1764. if (host->vmmc)
  1765. regulator_disable(host->vmmc);
  1766. return 0;
  1767. }
  1768. static int dw_mci_resume(struct device *dev)
  1769. {
  1770. int i, ret;
  1771. struct dw_mci *host = dev_get_drvdata(dev);
  1772. if (host->vmmc)
  1773. regulator_enable(host->vmmc);
  1774. if (host->dma_ops->init)
  1775. host->dma_ops->init(host);
  1776. if (!mci_wait_reset(dev, host)) {
  1777. ret = -ENODEV;
  1778. return ret;
  1779. }
  1780. /* Restore the old value at FIFOTH register */
  1781. mci_writel(host, FIFOTH, host->fifoth_val);
  1782. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1783. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  1784. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  1785. DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
  1786. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
  1787. for (i = 0; i < host->num_slots; i++) {
  1788. struct dw_mci_slot *slot = host->slot[i];
  1789. if (!slot)
  1790. continue;
  1791. ret = mmc_resume_host(host->slot[i]->mmc);
  1792. if (ret < 0)
  1793. return ret;
  1794. }
  1795. return 0;
  1796. }
  1797. #else
  1798. #define dw_mci_suspend NULL
  1799. #define dw_mci_resume NULL
  1800. #endif /* CONFIG_PM_SLEEP */
  1801. static SIMPLE_DEV_PM_OPS(dw_mci_pmops, dw_mci_suspend, dw_mci_resume);
  1802. static struct platform_driver dw_mci_driver = {
  1803. .remove = __exit_p(dw_mci_remove),
  1804. .driver = {
  1805. .name = "dw_mmc",
  1806. .pm = &dw_mci_pmops,
  1807. },
  1808. };
  1809. static int __init dw_mci_init(void)
  1810. {
  1811. return platform_driver_probe(&dw_mci_driver, dw_mci_probe);
  1812. }
  1813. static void __exit dw_mci_exit(void)
  1814. {
  1815. platform_driver_unregister(&dw_mci_driver);
  1816. }
  1817. module_init(dw_mci_init);
  1818. module_exit(dw_mci_exit);
  1819. MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
  1820. MODULE_AUTHOR("NXP Semiconductor VietNam");
  1821. MODULE_AUTHOR("Imagination Technologies Ltd");
  1822. MODULE_LICENSE("GPL v2");