dw_mmc.c 54 KB

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