dw_mmc.c 54 KB

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