dw_mmc.c 54 KB

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