dw_mmc.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288
  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->quirks & DW_MCI_QUIRK_NO_WRITE_PROTECT)
  674. read_only = 0;
  675. else if (brd->get_ro)
  676. read_only = brd->get_ro(slot->id);
  677. else
  678. read_only =
  679. mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
  680. dev_dbg(&mmc->class_dev, "card is %s\n",
  681. read_only ? "read-only" : "read-write");
  682. return read_only;
  683. }
  684. static int dw_mci_get_cd(struct mmc_host *mmc)
  685. {
  686. int present;
  687. struct dw_mci_slot *slot = mmc_priv(mmc);
  688. struct dw_mci_board *brd = slot->host->pdata;
  689. /* Use platform get_cd function, else try onboard card detect */
  690. if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
  691. present = 1;
  692. else if (brd->get_cd)
  693. present = !brd->get_cd(slot->id);
  694. else
  695. present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
  696. == 0 ? 1 : 0;
  697. if (present)
  698. dev_dbg(&mmc->class_dev, "card is present\n");
  699. else
  700. dev_dbg(&mmc->class_dev, "card is not present\n");
  701. return present;
  702. }
  703. /*
  704. * Disable lower power mode.
  705. *
  706. * Low power mode will stop the card clock when idle. According to the
  707. * description of the CLKENA register we should disable low power mode
  708. * for SDIO cards if we need SDIO interrupts to work.
  709. *
  710. * This function is fast if low power mode is already disabled.
  711. */
  712. static void dw_mci_disable_low_power(struct dw_mci_slot *slot)
  713. {
  714. struct dw_mci *host = slot->host;
  715. u32 clk_en_a;
  716. const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
  717. clk_en_a = mci_readl(host, CLKENA);
  718. if (clk_en_a & clken_low_pwr) {
  719. mci_writel(host, CLKENA, clk_en_a & ~clken_low_pwr);
  720. mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
  721. SDMMC_CMD_PRV_DAT_WAIT, 0);
  722. }
  723. }
  724. static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
  725. {
  726. struct dw_mci_slot *slot = mmc_priv(mmc);
  727. struct dw_mci *host = slot->host;
  728. u32 int_mask;
  729. /* Enable/disable Slot Specific SDIO interrupt */
  730. int_mask = mci_readl(host, INTMASK);
  731. if (enb) {
  732. /*
  733. * Turn off low power mode if it was enabled. This is a bit of
  734. * a heavy operation and we disable / enable IRQs a lot, so
  735. * we'll leave low power mode disabled and it will get
  736. * re-enabled again in dw_mci_setup_bus().
  737. */
  738. dw_mci_disable_low_power(slot);
  739. mci_writel(host, INTMASK,
  740. (int_mask | SDMMC_INT_SDIO(slot->id)));
  741. } else {
  742. mci_writel(host, INTMASK,
  743. (int_mask & ~SDMMC_INT_SDIO(slot->id)));
  744. }
  745. }
  746. static const struct mmc_host_ops dw_mci_ops = {
  747. .request = dw_mci_request,
  748. .pre_req = dw_mci_pre_req,
  749. .post_req = dw_mci_post_req,
  750. .set_ios = dw_mci_set_ios,
  751. .get_ro = dw_mci_get_ro,
  752. .get_cd = dw_mci_get_cd,
  753. .enable_sdio_irq = dw_mci_enable_sdio_irq,
  754. };
  755. static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
  756. __releases(&host->lock)
  757. __acquires(&host->lock)
  758. {
  759. struct dw_mci_slot *slot;
  760. struct mmc_host *prev_mmc = host->cur_slot->mmc;
  761. WARN_ON(host->cmd || host->data);
  762. host->cur_slot->mrq = NULL;
  763. host->mrq = NULL;
  764. if (!list_empty(&host->queue)) {
  765. slot = list_entry(host->queue.next,
  766. struct dw_mci_slot, queue_node);
  767. list_del(&slot->queue_node);
  768. dev_vdbg(host->dev, "list not empty: %s is next\n",
  769. mmc_hostname(slot->mmc));
  770. host->state = STATE_SENDING_CMD;
  771. dw_mci_start_request(host, slot);
  772. } else {
  773. dev_vdbg(host->dev, "list empty\n");
  774. host->state = STATE_IDLE;
  775. }
  776. spin_unlock(&host->lock);
  777. mmc_request_done(prev_mmc, mrq);
  778. spin_lock(&host->lock);
  779. }
  780. static void dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
  781. {
  782. u32 status = host->cmd_status;
  783. host->cmd_status = 0;
  784. /* Read the response from the card (up to 16 bytes) */
  785. if (cmd->flags & MMC_RSP_PRESENT) {
  786. if (cmd->flags & MMC_RSP_136) {
  787. cmd->resp[3] = mci_readl(host, RESP0);
  788. cmd->resp[2] = mci_readl(host, RESP1);
  789. cmd->resp[1] = mci_readl(host, RESP2);
  790. cmd->resp[0] = mci_readl(host, RESP3);
  791. } else {
  792. cmd->resp[0] = mci_readl(host, RESP0);
  793. cmd->resp[1] = 0;
  794. cmd->resp[2] = 0;
  795. cmd->resp[3] = 0;
  796. }
  797. }
  798. if (status & SDMMC_INT_RTO)
  799. cmd->error = -ETIMEDOUT;
  800. else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
  801. cmd->error = -EILSEQ;
  802. else if (status & SDMMC_INT_RESP_ERR)
  803. cmd->error = -EIO;
  804. else
  805. cmd->error = 0;
  806. if (cmd->error) {
  807. /* newer ip versions need a delay between retries */
  808. if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
  809. mdelay(20);
  810. if (cmd->data) {
  811. dw_mci_stop_dma(host);
  812. host->data = NULL;
  813. }
  814. }
  815. }
  816. static void dw_mci_tasklet_func(unsigned long priv)
  817. {
  818. struct dw_mci *host = (struct dw_mci *)priv;
  819. struct mmc_data *data;
  820. struct mmc_command *cmd;
  821. enum dw_mci_state state;
  822. enum dw_mci_state prev_state;
  823. u32 status, ctrl;
  824. spin_lock(&host->lock);
  825. state = host->state;
  826. data = host->data;
  827. do {
  828. prev_state = state;
  829. switch (state) {
  830. case STATE_IDLE:
  831. break;
  832. case STATE_SENDING_CMD:
  833. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  834. &host->pending_events))
  835. break;
  836. cmd = host->cmd;
  837. host->cmd = NULL;
  838. set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
  839. dw_mci_command_complete(host, cmd);
  840. if (cmd == host->mrq->sbc && !cmd->error) {
  841. prev_state = state = STATE_SENDING_CMD;
  842. __dw_mci_start_request(host, host->cur_slot,
  843. host->mrq->cmd);
  844. goto unlock;
  845. }
  846. if (!host->mrq->data || cmd->error) {
  847. dw_mci_request_end(host, host->mrq);
  848. goto unlock;
  849. }
  850. prev_state = state = STATE_SENDING_DATA;
  851. /* fall through */
  852. case STATE_SENDING_DATA:
  853. if (test_and_clear_bit(EVENT_DATA_ERROR,
  854. &host->pending_events)) {
  855. dw_mci_stop_dma(host);
  856. if (data->stop)
  857. send_stop_cmd(host, data);
  858. state = STATE_DATA_ERROR;
  859. break;
  860. }
  861. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  862. &host->pending_events))
  863. break;
  864. set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
  865. prev_state = state = STATE_DATA_BUSY;
  866. /* fall through */
  867. case STATE_DATA_BUSY:
  868. if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
  869. &host->pending_events))
  870. break;
  871. host->data = NULL;
  872. set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
  873. status = host->data_status;
  874. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  875. if (status & SDMMC_INT_DTO) {
  876. data->error = -ETIMEDOUT;
  877. } else if (status & SDMMC_INT_DCRC) {
  878. data->error = -EILSEQ;
  879. } else if (status & SDMMC_INT_EBE &&
  880. host->dir_status ==
  881. DW_MCI_SEND_STATUS) {
  882. /*
  883. * No data CRC status was returned.
  884. * The number of bytes transferred will
  885. * be exaggerated in PIO mode.
  886. */
  887. data->bytes_xfered = 0;
  888. data->error = -ETIMEDOUT;
  889. } else {
  890. dev_err(host->dev,
  891. "data FIFO error "
  892. "(status=%08x)\n",
  893. status);
  894. data->error = -EIO;
  895. }
  896. /*
  897. * After an error, there may be data lingering
  898. * in the FIFO, so reset it - doing so
  899. * generates a block interrupt, hence setting
  900. * the scatter-gather pointer to NULL.
  901. */
  902. sg_miter_stop(&host->sg_miter);
  903. host->sg = NULL;
  904. ctrl = mci_readl(host, CTRL);
  905. ctrl |= SDMMC_CTRL_FIFO_RESET;
  906. mci_writel(host, CTRL, ctrl);
  907. } else {
  908. data->bytes_xfered = data->blocks * data->blksz;
  909. data->error = 0;
  910. }
  911. if (!data->stop) {
  912. dw_mci_request_end(host, host->mrq);
  913. goto unlock;
  914. }
  915. if (host->mrq->sbc && !data->error) {
  916. data->stop->error = 0;
  917. dw_mci_request_end(host, host->mrq);
  918. goto unlock;
  919. }
  920. prev_state = state = STATE_SENDING_STOP;
  921. if (!data->error)
  922. send_stop_cmd(host, data);
  923. /* fall through */
  924. case STATE_SENDING_STOP:
  925. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  926. &host->pending_events))
  927. break;
  928. host->cmd = NULL;
  929. dw_mci_command_complete(host, host->mrq->stop);
  930. dw_mci_request_end(host, host->mrq);
  931. goto unlock;
  932. case STATE_DATA_ERROR:
  933. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  934. &host->pending_events))
  935. break;
  936. state = STATE_DATA_BUSY;
  937. break;
  938. }
  939. } while (state != prev_state);
  940. host->state = state;
  941. unlock:
  942. spin_unlock(&host->lock);
  943. }
  944. /* push final bytes to part_buf, only use during push */
  945. static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
  946. {
  947. memcpy((void *)&host->part_buf, buf, cnt);
  948. host->part_buf_count = cnt;
  949. }
  950. /* append bytes to part_buf, only use during push */
  951. static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
  952. {
  953. cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
  954. memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
  955. host->part_buf_count += cnt;
  956. return cnt;
  957. }
  958. /* pull first bytes from part_buf, only use during pull */
  959. static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
  960. {
  961. cnt = min(cnt, (int)host->part_buf_count);
  962. if (cnt) {
  963. memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
  964. cnt);
  965. host->part_buf_count -= cnt;
  966. host->part_buf_start += cnt;
  967. }
  968. return cnt;
  969. }
  970. /* pull final bytes from the part_buf, assuming it's just been filled */
  971. static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
  972. {
  973. memcpy(buf, &host->part_buf, cnt);
  974. host->part_buf_start = cnt;
  975. host->part_buf_count = (1 << host->data_shift) - cnt;
  976. }
  977. static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
  978. {
  979. /* try and push anything in the part_buf */
  980. if (unlikely(host->part_buf_count)) {
  981. int len = dw_mci_push_part_bytes(host, buf, cnt);
  982. buf += len;
  983. cnt -= len;
  984. if (!sg_next(host->sg) || host->part_buf_count == 2) {
  985. mci_writew(host, DATA(host->data_offset),
  986. host->part_buf16);
  987. host->part_buf_count = 0;
  988. }
  989. }
  990. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  991. if (unlikely((unsigned long)buf & 0x1)) {
  992. while (cnt >= 2) {
  993. u16 aligned_buf[64];
  994. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  995. int items = len >> 1;
  996. int i;
  997. /* memcpy from input buffer into aligned buffer */
  998. memcpy(aligned_buf, buf, len);
  999. buf += len;
  1000. cnt -= len;
  1001. /* push data from aligned buffer into fifo */
  1002. for (i = 0; i < items; ++i)
  1003. mci_writew(host, DATA(host->data_offset),
  1004. aligned_buf[i]);
  1005. }
  1006. } else
  1007. #endif
  1008. {
  1009. u16 *pdata = buf;
  1010. for (; cnt >= 2; cnt -= 2)
  1011. mci_writew(host, DATA(host->data_offset), *pdata++);
  1012. buf = pdata;
  1013. }
  1014. /* put anything remaining in the part_buf */
  1015. if (cnt) {
  1016. dw_mci_set_part_bytes(host, buf, cnt);
  1017. if (!sg_next(host->sg))
  1018. mci_writew(host, DATA(host->data_offset),
  1019. host->part_buf16);
  1020. }
  1021. }
  1022. static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
  1023. {
  1024. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1025. if (unlikely((unsigned long)buf & 0x1)) {
  1026. while (cnt >= 2) {
  1027. /* pull data from fifo into aligned buffer */
  1028. u16 aligned_buf[64];
  1029. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1030. int items = len >> 1;
  1031. int i;
  1032. for (i = 0; i < items; ++i)
  1033. aligned_buf[i] = mci_readw(host,
  1034. DATA(host->data_offset));
  1035. /* memcpy from aligned buffer into output buffer */
  1036. memcpy(buf, aligned_buf, len);
  1037. buf += len;
  1038. cnt -= len;
  1039. }
  1040. } else
  1041. #endif
  1042. {
  1043. u16 *pdata = buf;
  1044. for (; cnt >= 2; cnt -= 2)
  1045. *pdata++ = mci_readw(host, DATA(host->data_offset));
  1046. buf = pdata;
  1047. }
  1048. if (cnt) {
  1049. host->part_buf16 = mci_readw(host, DATA(host->data_offset));
  1050. dw_mci_pull_final_bytes(host, buf, cnt);
  1051. }
  1052. }
  1053. static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
  1054. {
  1055. /* try and push anything in the part_buf */
  1056. if (unlikely(host->part_buf_count)) {
  1057. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1058. buf += len;
  1059. cnt -= len;
  1060. if (!sg_next(host->sg) || host->part_buf_count == 4) {
  1061. mci_writel(host, DATA(host->data_offset),
  1062. host->part_buf32);
  1063. host->part_buf_count = 0;
  1064. }
  1065. }
  1066. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1067. if (unlikely((unsigned long)buf & 0x3)) {
  1068. while (cnt >= 4) {
  1069. u32 aligned_buf[32];
  1070. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  1071. int items = len >> 2;
  1072. int i;
  1073. /* memcpy from input buffer into aligned buffer */
  1074. memcpy(aligned_buf, buf, len);
  1075. buf += len;
  1076. cnt -= len;
  1077. /* push data from aligned buffer into fifo */
  1078. for (i = 0; i < items; ++i)
  1079. mci_writel(host, DATA(host->data_offset),
  1080. aligned_buf[i]);
  1081. }
  1082. } else
  1083. #endif
  1084. {
  1085. u32 *pdata = buf;
  1086. for (; cnt >= 4; cnt -= 4)
  1087. mci_writel(host, DATA(host->data_offset), *pdata++);
  1088. buf = pdata;
  1089. }
  1090. /* put anything remaining in the part_buf */
  1091. if (cnt) {
  1092. dw_mci_set_part_bytes(host, buf, cnt);
  1093. if (!sg_next(host->sg))
  1094. mci_writel(host, DATA(host->data_offset),
  1095. host->part_buf32);
  1096. }
  1097. }
  1098. static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
  1099. {
  1100. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1101. if (unlikely((unsigned long)buf & 0x3)) {
  1102. while (cnt >= 4) {
  1103. /* pull data from fifo into aligned buffer */
  1104. u32 aligned_buf[32];
  1105. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  1106. int items = len >> 2;
  1107. int i;
  1108. for (i = 0; i < items; ++i)
  1109. aligned_buf[i] = mci_readl(host,
  1110. DATA(host->data_offset));
  1111. /* memcpy from aligned buffer into output buffer */
  1112. memcpy(buf, aligned_buf, len);
  1113. buf += len;
  1114. cnt -= len;
  1115. }
  1116. } else
  1117. #endif
  1118. {
  1119. u32 *pdata = buf;
  1120. for (; cnt >= 4; cnt -= 4)
  1121. *pdata++ = mci_readl(host, DATA(host->data_offset));
  1122. buf = pdata;
  1123. }
  1124. if (cnt) {
  1125. host->part_buf32 = mci_readl(host, DATA(host->data_offset));
  1126. dw_mci_pull_final_bytes(host, buf, cnt);
  1127. }
  1128. }
  1129. static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
  1130. {
  1131. /* try and push anything in the part_buf */
  1132. if (unlikely(host->part_buf_count)) {
  1133. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1134. buf += len;
  1135. cnt -= len;
  1136. if (!sg_next(host->sg) || host->part_buf_count == 8) {
  1137. mci_writew(host, DATA(host->data_offset),
  1138. host->part_buf);
  1139. host->part_buf_count = 0;
  1140. }
  1141. }
  1142. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1143. if (unlikely((unsigned long)buf & 0x7)) {
  1144. while (cnt >= 8) {
  1145. u64 aligned_buf[16];
  1146. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1147. int items = len >> 3;
  1148. int i;
  1149. /* memcpy from input buffer into aligned buffer */
  1150. memcpy(aligned_buf, buf, len);
  1151. buf += len;
  1152. cnt -= len;
  1153. /* push data from aligned buffer into fifo */
  1154. for (i = 0; i < items; ++i)
  1155. mci_writeq(host, DATA(host->data_offset),
  1156. aligned_buf[i]);
  1157. }
  1158. } else
  1159. #endif
  1160. {
  1161. u64 *pdata = buf;
  1162. for (; cnt >= 8; cnt -= 8)
  1163. mci_writeq(host, DATA(host->data_offset), *pdata++);
  1164. buf = pdata;
  1165. }
  1166. /* put anything remaining in the part_buf */
  1167. if (cnt) {
  1168. dw_mci_set_part_bytes(host, buf, cnt);
  1169. if (!sg_next(host->sg))
  1170. mci_writeq(host, DATA(host->data_offset),
  1171. host->part_buf);
  1172. }
  1173. }
  1174. static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
  1175. {
  1176. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1177. if (unlikely((unsigned long)buf & 0x7)) {
  1178. while (cnt >= 8) {
  1179. /* pull data from fifo into aligned buffer */
  1180. u64 aligned_buf[16];
  1181. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1182. int items = len >> 3;
  1183. int i;
  1184. for (i = 0; i < items; ++i)
  1185. aligned_buf[i] = mci_readq(host,
  1186. DATA(host->data_offset));
  1187. /* memcpy from aligned buffer into output buffer */
  1188. memcpy(buf, aligned_buf, len);
  1189. buf += len;
  1190. cnt -= len;
  1191. }
  1192. } else
  1193. #endif
  1194. {
  1195. u64 *pdata = buf;
  1196. for (; cnt >= 8; cnt -= 8)
  1197. *pdata++ = mci_readq(host, DATA(host->data_offset));
  1198. buf = pdata;
  1199. }
  1200. if (cnt) {
  1201. host->part_buf = mci_readq(host, DATA(host->data_offset));
  1202. dw_mci_pull_final_bytes(host, buf, cnt);
  1203. }
  1204. }
  1205. static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
  1206. {
  1207. int len;
  1208. /* get remaining partial bytes */
  1209. len = dw_mci_pull_part_bytes(host, buf, cnt);
  1210. if (unlikely(len == cnt))
  1211. return;
  1212. buf += len;
  1213. cnt -= len;
  1214. /* get the rest of the data */
  1215. host->pull_data(host, buf, cnt);
  1216. }
  1217. static void dw_mci_read_data_pio(struct dw_mci *host)
  1218. {
  1219. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  1220. void *buf;
  1221. unsigned int offset;
  1222. struct mmc_data *data = host->data;
  1223. int shift = host->data_shift;
  1224. u32 status;
  1225. unsigned int nbytes = 0, len;
  1226. unsigned int remain, fcnt;
  1227. do {
  1228. if (!sg_miter_next(sg_miter))
  1229. goto done;
  1230. host->sg = sg_miter->__sg;
  1231. buf = sg_miter->addr;
  1232. remain = sg_miter->length;
  1233. offset = 0;
  1234. do {
  1235. fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
  1236. << shift) + host->part_buf_count;
  1237. len = min(remain, fcnt);
  1238. if (!len)
  1239. break;
  1240. dw_mci_pull_data(host, (void *)(buf + offset), len);
  1241. offset += len;
  1242. nbytes += len;
  1243. remain -= len;
  1244. } while (remain);
  1245. sg_miter->consumed = offset;
  1246. status = mci_readl(host, MINTSTS);
  1247. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  1248. } while (status & SDMMC_INT_RXDR); /*if the RXDR is ready read again*/
  1249. data->bytes_xfered += nbytes;
  1250. if (!remain) {
  1251. if (!sg_miter_next(sg_miter))
  1252. goto done;
  1253. sg_miter->consumed = 0;
  1254. }
  1255. sg_miter_stop(sg_miter);
  1256. return;
  1257. done:
  1258. data->bytes_xfered += nbytes;
  1259. sg_miter_stop(sg_miter);
  1260. host->sg = NULL;
  1261. smp_wmb();
  1262. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  1263. }
  1264. static void dw_mci_write_data_pio(struct dw_mci *host)
  1265. {
  1266. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  1267. void *buf;
  1268. unsigned int offset;
  1269. struct mmc_data *data = host->data;
  1270. int shift = host->data_shift;
  1271. u32 status;
  1272. unsigned int nbytes = 0, len;
  1273. unsigned int fifo_depth = host->fifo_depth;
  1274. unsigned int remain, fcnt;
  1275. do {
  1276. if (!sg_miter_next(sg_miter))
  1277. goto done;
  1278. host->sg = sg_miter->__sg;
  1279. buf = sg_miter->addr;
  1280. remain = sg_miter->length;
  1281. offset = 0;
  1282. do {
  1283. fcnt = ((fifo_depth -
  1284. SDMMC_GET_FCNT(mci_readl(host, STATUS)))
  1285. << shift) - host->part_buf_count;
  1286. len = min(remain, fcnt);
  1287. if (!len)
  1288. break;
  1289. host->push_data(host, (void *)(buf + offset), len);
  1290. offset += len;
  1291. nbytes += len;
  1292. remain -= len;
  1293. } while (remain);
  1294. sg_miter->consumed = offset;
  1295. status = mci_readl(host, MINTSTS);
  1296. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1297. } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
  1298. data->bytes_xfered += nbytes;
  1299. if (!remain) {
  1300. if (!sg_miter_next(sg_miter))
  1301. goto done;
  1302. sg_miter->consumed = 0;
  1303. }
  1304. sg_miter_stop(sg_miter);
  1305. return;
  1306. done:
  1307. data->bytes_xfered += nbytes;
  1308. sg_miter_stop(sg_miter);
  1309. host->sg = NULL;
  1310. smp_wmb();
  1311. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  1312. }
  1313. static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
  1314. {
  1315. if (!host->cmd_status)
  1316. host->cmd_status = status;
  1317. smp_wmb();
  1318. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1319. tasklet_schedule(&host->tasklet);
  1320. }
  1321. static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
  1322. {
  1323. struct dw_mci *host = dev_id;
  1324. u32 pending;
  1325. unsigned int pass_count = 0;
  1326. int i;
  1327. do {
  1328. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  1329. /*
  1330. * DTO fix - version 2.10a and below, and only if internal DMA
  1331. * is configured.
  1332. */
  1333. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
  1334. if (!pending &&
  1335. ((mci_readl(host, STATUS) >> 17) & 0x1fff))
  1336. pending |= SDMMC_INT_DATA_OVER;
  1337. }
  1338. if (!pending)
  1339. break;
  1340. if (pending & DW_MCI_CMD_ERROR_FLAGS) {
  1341. mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
  1342. host->cmd_status = pending;
  1343. smp_wmb();
  1344. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1345. }
  1346. if (pending & DW_MCI_DATA_ERROR_FLAGS) {
  1347. /* if there is an error report DATA_ERROR */
  1348. mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
  1349. host->data_status = pending;
  1350. smp_wmb();
  1351. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  1352. tasklet_schedule(&host->tasklet);
  1353. }
  1354. if (pending & SDMMC_INT_DATA_OVER) {
  1355. mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
  1356. if (!host->data_status)
  1357. host->data_status = pending;
  1358. smp_wmb();
  1359. if (host->dir_status == DW_MCI_RECV_STATUS) {
  1360. if (host->sg != NULL)
  1361. dw_mci_read_data_pio(host);
  1362. }
  1363. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  1364. tasklet_schedule(&host->tasklet);
  1365. }
  1366. if (pending & SDMMC_INT_RXDR) {
  1367. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  1368. if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
  1369. dw_mci_read_data_pio(host);
  1370. }
  1371. if (pending & SDMMC_INT_TXDR) {
  1372. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1373. if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
  1374. dw_mci_write_data_pio(host);
  1375. }
  1376. if (pending & SDMMC_INT_CMD_DONE) {
  1377. mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
  1378. dw_mci_cmd_interrupt(host, pending);
  1379. }
  1380. if (pending & SDMMC_INT_CD) {
  1381. mci_writel(host, RINTSTS, SDMMC_INT_CD);
  1382. queue_work(host->card_workqueue, &host->card_work);
  1383. }
  1384. /* Handle SDIO Interrupts */
  1385. for (i = 0; i < host->num_slots; i++) {
  1386. struct dw_mci_slot *slot = host->slot[i];
  1387. if (pending & SDMMC_INT_SDIO(i)) {
  1388. mci_writel(host, RINTSTS, SDMMC_INT_SDIO(i));
  1389. mmc_signal_sdio_irq(slot->mmc);
  1390. }
  1391. }
  1392. } while (pass_count++ < 5);
  1393. #ifdef CONFIG_MMC_DW_IDMAC
  1394. /* Handle DMA interrupts */
  1395. pending = mci_readl(host, IDSTS);
  1396. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  1397. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI);
  1398. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
  1399. host->dma_ops->complete(host);
  1400. }
  1401. #endif
  1402. return IRQ_HANDLED;
  1403. }
  1404. static void dw_mci_work_routine_card(struct work_struct *work)
  1405. {
  1406. struct dw_mci *host = container_of(work, struct dw_mci, card_work);
  1407. int i;
  1408. for (i = 0; i < host->num_slots; i++) {
  1409. struct dw_mci_slot *slot = host->slot[i];
  1410. struct mmc_host *mmc = slot->mmc;
  1411. struct mmc_request *mrq;
  1412. int present;
  1413. u32 ctrl;
  1414. present = dw_mci_get_cd(mmc);
  1415. while (present != slot->last_detect_state) {
  1416. dev_dbg(&slot->mmc->class_dev, "card %s\n",
  1417. present ? "inserted" : "removed");
  1418. /* Power up slot (before spin_lock, may sleep) */
  1419. if (present != 0 && host->pdata->setpower)
  1420. host->pdata->setpower(slot->id, mmc->ocr_avail);
  1421. spin_lock_bh(&host->lock);
  1422. /* Card change detected */
  1423. slot->last_detect_state = present;
  1424. /* Mark card as present if applicable */
  1425. if (present != 0)
  1426. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1427. /* Clean up queue if present */
  1428. mrq = slot->mrq;
  1429. if (mrq) {
  1430. if (mrq == host->mrq) {
  1431. host->data = NULL;
  1432. host->cmd = NULL;
  1433. switch (host->state) {
  1434. case STATE_IDLE:
  1435. break;
  1436. case STATE_SENDING_CMD:
  1437. mrq->cmd->error = -ENOMEDIUM;
  1438. if (!mrq->data)
  1439. break;
  1440. /* fall through */
  1441. case STATE_SENDING_DATA:
  1442. mrq->data->error = -ENOMEDIUM;
  1443. dw_mci_stop_dma(host);
  1444. break;
  1445. case STATE_DATA_BUSY:
  1446. case STATE_DATA_ERROR:
  1447. if (mrq->data->error == -EINPROGRESS)
  1448. mrq->data->error = -ENOMEDIUM;
  1449. if (!mrq->stop)
  1450. break;
  1451. /* fall through */
  1452. case STATE_SENDING_STOP:
  1453. mrq->stop->error = -ENOMEDIUM;
  1454. break;
  1455. }
  1456. dw_mci_request_end(host, mrq);
  1457. } else {
  1458. list_del(&slot->queue_node);
  1459. mrq->cmd->error = -ENOMEDIUM;
  1460. if (mrq->data)
  1461. mrq->data->error = -ENOMEDIUM;
  1462. if (mrq->stop)
  1463. mrq->stop->error = -ENOMEDIUM;
  1464. spin_unlock(&host->lock);
  1465. mmc_request_done(slot->mmc, mrq);
  1466. spin_lock(&host->lock);
  1467. }
  1468. }
  1469. /* Power down slot */
  1470. if (present == 0) {
  1471. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1472. /*
  1473. * Clear down the FIFO - doing so generates a
  1474. * block interrupt, hence setting the
  1475. * scatter-gather pointer to NULL.
  1476. */
  1477. sg_miter_stop(&host->sg_miter);
  1478. host->sg = NULL;
  1479. ctrl = mci_readl(host, CTRL);
  1480. ctrl |= SDMMC_CTRL_FIFO_RESET;
  1481. mci_writel(host, CTRL, ctrl);
  1482. #ifdef CONFIG_MMC_DW_IDMAC
  1483. ctrl = mci_readl(host, BMOD);
  1484. /* Software reset of DMA */
  1485. ctrl |= SDMMC_IDMAC_SWRESET;
  1486. mci_writel(host, BMOD, ctrl);
  1487. #endif
  1488. }
  1489. spin_unlock_bh(&host->lock);
  1490. /* Power down slot (after spin_unlock, may sleep) */
  1491. if (present == 0 && host->pdata->setpower)
  1492. host->pdata->setpower(slot->id, 0);
  1493. present = dw_mci_get_cd(mmc);
  1494. }
  1495. mmc_detect_change(slot->mmc,
  1496. msecs_to_jiffies(host->pdata->detect_delay_ms));
  1497. }
  1498. }
  1499. static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
  1500. {
  1501. struct mmc_host *mmc;
  1502. struct dw_mci_slot *slot;
  1503. mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
  1504. if (!mmc)
  1505. return -ENOMEM;
  1506. slot = mmc_priv(mmc);
  1507. slot->id = id;
  1508. slot->mmc = mmc;
  1509. slot->host = host;
  1510. mmc->ops = &dw_mci_ops;
  1511. mmc->f_min = DIV_ROUND_UP(host->bus_hz, 510);
  1512. mmc->f_max = host->bus_hz;
  1513. if (host->pdata->get_ocr)
  1514. mmc->ocr_avail = host->pdata->get_ocr(id);
  1515. else
  1516. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1517. /*
  1518. * Start with slot power disabled, it will be enabled when a card
  1519. * is detected.
  1520. */
  1521. if (host->pdata->setpower)
  1522. host->pdata->setpower(id, 0);
  1523. if (host->pdata->caps)
  1524. mmc->caps = host->pdata->caps;
  1525. if (host->pdata->caps2)
  1526. mmc->caps2 = host->pdata->caps2;
  1527. if (host->pdata->get_bus_wd)
  1528. if (host->pdata->get_bus_wd(slot->id) >= 4)
  1529. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1530. if (host->pdata->quirks & DW_MCI_QUIRK_HIGHSPEED)
  1531. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  1532. if (mmc->caps2 & MMC_CAP2_POWEROFF_NOTIFY)
  1533. mmc->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
  1534. else
  1535. mmc->power_notify_type = MMC_HOST_PW_NOTIFY_NONE;
  1536. if (host->pdata->blk_settings) {
  1537. mmc->max_segs = host->pdata->blk_settings->max_segs;
  1538. mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
  1539. mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
  1540. mmc->max_req_size = host->pdata->blk_settings->max_req_size;
  1541. mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
  1542. } else {
  1543. /* Useful defaults if platform data is unset. */
  1544. #ifdef CONFIG_MMC_DW_IDMAC
  1545. mmc->max_segs = host->ring_size;
  1546. mmc->max_blk_size = 65536;
  1547. mmc->max_blk_count = host->ring_size;
  1548. mmc->max_seg_size = 0x1000;
  1549. mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count;
  1550. #else
  1551. mmc->max_segs = 64;
  1552. mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
  1553. mmc->max_blk_count = 512;
  1554. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1555. mmc->max_seg_size = mmc->max_req_size;
  1556. #endif /* CONFIG_MMC_DW_IDMAC */
  1557. }
  1558. host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
  1559. if (IS_ERR(host->vmmc)) {
  1560. pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
  1561. host->vmmc = NULL;
  1562. } else
  1563. regulator_enable(host->vmmc);
  1564. if (dw_mci_get_cd(mmc))
  1565. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1566. else
  1567. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1568. host->slot[id] = slot;
  1569. mmc_add_host(mmc);
  1570. #if defined(CONFIG_DEBUG_FS)
  1571. dw_mci_init_debugfs(slot);
  1572. #endif
  1573. /* Card initially undetected */
  1574. slot->last_detect_state = 0;
  1575. /*
  1576. * Card may have been plugged in prior to boot so we
  1577. * need to run the detect tasklet
  1578. */
  1579. queue_work(host->card_workqueue, &host->card_work);
  1580. return 0;
  1581. }
  1582. static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
  1583. {
  1584. /* Shutdown detect IRQ */
  1585. if (slot->host->pdata->exit)
  1586. slot->host->pdata->exit(id);
  1587. /* Debugfs stuff is cleaned up by mmc core */
  1588. mmc_remove_host(slot->mmc);
  1589. slot->host->slot[id] = NULL;
  1590. mmc_free_host(slot->mmc);
  1591. }
  1592. static void dw_mci_init_dma(struct dw_mci *host)
  1593. {
  1594. /* Alloc memory for sg translation */
  1595. host->sg_cpu = dma_alloc_coherent(host->dev, PAGE_SIZE,
  1596. &host->sg_dma, GFP_KERNEL);
  1597. if (!host->sg_cpu) {
  1598. dev_err(host->dev, "%s: could not alloc DMA memory\n",
  1599. __func__);
  1600. goto no_dma;
  1601. }
  1602. /* Determine which DMA interface to use */
  1603. #ifdef CONFIG_MMC_DW_IDMAC
  1604. host->dma_ops = &dw_mci_idmac_ops;
  1605. #endif
  1606. if (!host->dma_ops)
  1607. goto no_dma;
  1608. if (host->dma_ops->init && host->dma_ops->start &&
  1609. host->dma_ops->stop && host->dma_ops->cleanup) {
  1610. if (host->dma_ops->init(host)) {
  1611. dev_err(host->dev, "%s: Unable to initialize "
  1612. "DMA Controller.\n", __func__);
  1613. goto no_dma;
  1614. }
  1615. } else {
  1616. dev_err(host->dev, "DMA initialization not found.\n");
  1617. goto no_dma;
  1618. }
  1619. host->use_dma = 1;
  1620. return;
  1621. no_dma:
  1622. dev_info(host->dev, "Using PIO mode.\n");
  1623. host->use_dma = 0;
  1624. return;
  1625. }
  1626. static bool mci_wait_reset(struct device *dev, struct dw_mci *host)
  1627. {
  1628. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  1629. unsigned int ctrl;
  1630. mci_writel(host, CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
  1631. SDMMC_CTRL_DMA_RESET));
  1632. /* wait till resets clear */
  1633. do {
  1634. ctrl = mci_readl(host, CTRL);
  1635. if (!(ctrl & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
  1636. SDMMC_CTRL_DMA_RESET)))
  1637. return true;
  1638. } while (time_before(jiffies, timeout));
  1639. dev_err(dev, "Timeout resetting block (ctrl %#x)\n", ctrl);
  1640. return false;
  1641. }
  1642. int dw_mci_probe(struct dw_mci *host)
  1643. {
  1644. int width, i, ret = 0;
  1645. u32 fifo_size;
  1646. int init_slots = 0;
  1647. if (!host->pdata || !host->pdata->init) {
  1648. dev_err(host->dev,
  1649. "Platform data must supply init function\n");
  1650. return -ENODEV;
  1651. }
  1652. if (!host->pdata->select_slot && host->pdata->num_slots > 1) {
  1653. dev_err(host->dev,
  1654. "Platform data must supply select_slot function\n");
  1655. return -ENODEV;
  1656. }
  1657. host->biu_clk = clk_get(host->dev, "biu");
  1658. if (IS_ERR(host->biu_clk)) {
  1659. dev_dbg(host->dev, "biu clock not available\n");
  1660. } else {
  1661. ret = clk_prepare_enable(host->biu_clk);
  1662. if (ret) {
  1663. dev_err(host->dev, "failed to enable biu clock\n");
  1664. clk_put(host->biu_clk);
  1665. return ret;
  1666. }
  1667. }
  1668. host->ciu_clk = clk_get(host->dev, "ciu");
  1669. if (IS_ERR(host->ciu_clk)) {
  1670. dev_dbg(host->dev, "ciu clock not available\n");
  1671. } else {
  1672. ret = clk_prepare_enable(host->ciu_clk);
  1673. if (ret) {
  1674. dev_err(host->dev, "failed to enable ciu clock\n");
  1675. clk_put(host->ciu_clk);
  1676. goto err_clk_biu;
  1677. }
  1678. }
  1679. if (IS_ERR(host->ciu_clk))
  1680. host->bus_hz = host->pdata->bus_hz;
  1681. else
  1682. host->bus_hz = clk_get_rate(host->ciu_clk);
  1683. if (!host->bus_hz) {
  1684. dev_err(host->dev,
  1685. "Platform data must supply bus speed\n");
  1686. ret = -ENODEV;
  1687. goto err_clk_ciu;
  1688. }
  1689. host->quirks = host->pdata->quirks;
  1690. spin_lock_init(&host->lock);
  1691. INIT_LIST_HEAD(&host->queue);
  1692. /*
  1693. * Get the host data width - this assumes that HCON has been set with
  1694. * the correct values.
  1695. */
  1696. i = (mci_readl(host, HCON) >> 7) & 0x7;
  1697. if (!i) {
  1698. host->push_data = dw_mci_push_data16;
  1699. host->pull_data = dw_mci_pull_data16;
  1700. width = 16;
  1701. host->data_shift = 1;
  1702. } else if (i == 2) {
  1703. host->push_data = dw_mci_push_data64;
  1704. host->pull_data = dw_mci_pull_data64;
  1705. width = 64;
  1706. host->data_shift = 3;
  1707. } else {
  1708. /* Check for a reserved value, and warn if it is */
  1709. WARN((i != 1),
  1710. "HCON reports a reserved host data width!\n"
  1711. "Defaulting to 32-bit access.\n");
  1712. host->push_data = dw_mci_push_data32;
  1713. host->pull_data = dw_mci_pull_data32;
  1714. width = 32;
  1715. host->data_shift = 2;
  1716. }
  1717. /* Reset all blocks */
  1718. if (!mci_wait_reset(host->dev, host))
  1719. return -ENODEV;
  1720. host->dma_ops = host->pdata->dma_ops;
  1721. dw_mci_init_dma(host);
  1722. /* Clear the interrupts for the host controller */
  1723. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1724. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  1725. /* Put in max timeout */
  1726. mci_writel(host, TMOUT, 0xFFFFFFFF);
  1727. /*
  1728. * FIFO threshold settings RxMark = fifo_size / 2 - 1,
  1729. * Tx Mark = fifo_size / 2 DMA Size = 8
  1730. */
  1731. if (!host->pdata->fifo_depth) {
  1732. /*
  1733. * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
  1734. * have been overwritten by the bootloader, just like we're
  1735. * about to do, so if you know the value for your hardware, you
  1736. * should put it in the platform data.
  1737. */
  1738. fifo_size = mci_readl(host, FIFOTH);
  1739. fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
  1740. } else {
  1741. fifo_size = host->pdata->fifo_depth;
  1742. }
  1743. host->fifo_depth = fifo_size;
  1744. host->fifoth_val = ((0x2 << 28) | ((fifo_size/2 - 1) << 16) |
  1745. ((fifo_size/2) << 0));
  1746. mci_writel(host, FIFOTH, host->fifoth_val);
  1747. /* disable clock to CIU */
  1748. mci_writel(host, CLKENA, 0);
  1749. mci_writel(host, CLKSRC, 0);
  1750. tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
  1751. host->card_workqueue = alloc_workqueue("dw-mci-card",
  1752. WQ_MEM_RECLAIM | WQ_NON_REENTRANT, 1);
  1753. if (!host->card_workqueue)
  1754. goto err_dmaunmap;
  1755. INIT_WORK(&host->card_work, dw_mci_work_routine_card);
  1756. ret = request_irq(host->irq, dw_mci_interrupt, host->irq_flags, "dw-mci", host);
  1757. if (ret)
  1758. goto err_workqueue;
  1759. if (host->pdata->num_slots)
  1760. host->num_slots = host->pdata->num_slots;
  1761. else
  1762. host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
  1763. /* We need at least one slot to succeed */
  1764. for (i = 0; i < host->num_slots; i++) {
  1765. ret = dw_mci_init_slot(host, i);
  1766. if (ret)
  1767. dev_dbg(host->dev, "slot %d init failed\n", i);
  1768. else
  1769. init_slots++;
  1770. }
  1771. if (init_slots) {
  1772. dev_info(host->dev, "%d slots initialized\n", init_slots);
  1773. } else {
  1774. dev_dbg(host->dev, "attempted to initialize %d slots, "
  1775. "but failed on all\n", host->num_slots);
  1776. goto err_init_slot;
  1777. }
  1778. /*
  1779. * In 2.40a spec, Data offset is changed.
  1780. * Need to check the version-id and set data-offset for DATA register.
  1781. */
  1782. host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
  1783. dev_info(host->dev, "Version ID is %04x\n", host->verid);
  1784. if (host->verid < DW_MMC_240A)
  1785. host->data_offset = DATA_OFFSET;
  1786. else
  1787. host->data_offset = DATA_240A_OFFSET;
  1788. /*
  1789. * Enable interrupts for command done, data over, data empty, card det,
  1790. * receive ready and error such as transmit, receive timeout, crc error
  1791. */
  1792. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1793. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  1794. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  1795. DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
  1796. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
  1797. dev_info(host->dev, "DW MMC controller at irq %d, "
  1798. "%d bit host data width, "
  1799. "%u deep fifo\n",
  1800. host->irq, width, fifo_size);
  1801. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
  1802. dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
  1803. return 0;
  1804. err_init_slot:
  1805. free_irq(host->irq, host);
  1806. err_workqueue:
  1807. destroy_workqueue(host->card_workqueue);
  1808. err_dmaunmap:
  1809. if (host->use_dma && host->dma_ops->exit)
  1810. host->dma_ops->exit(host);
  1811. dma_free_coherent(host->dev, PAGE_SIZE,
  1812. host->sg_cpu, host->sg_dma);
  1813. if (host->vmmc) {
  1814. regulator_disable(host->vmmc);
  1815. regulator_put(host->vmmc);
  1816. }
  1817. err_clk_ciu:
  1818. if (!IS_ERR(host->ciu_clk)) {
  1819. clk_disable_unprepare(host->ciu_clk);
  1820. clk_put(host->ciu_clk);
  1821. }
  1822. err_clk_biu:
  1823. if (!IS_ERR(host->biu_clk)) {
  1824. clk_disable_unprepare(host->biu_clk);
  1825. clk_put(host->biu_clk);
  1826. }
  1827. return ret;
  1828. }
  1829. EXPORT_SYMBOL(dw_mci_probe);
  1830. void dw_mci_remove(struct dw_mci *host)
  1831. {
  1832. int i;
  1833. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1834. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  1835. for (i = 0; i < host->num_slots; i++) {
  1836. dev_dbg(host->dev, "remove slot %d\n", i);
  1837. if (host->slot[i])
  1838. dw_mci_cleanup_slot(host->slot[i], i);
  1839. }
  1840. /* disable clock to CIU */
  1841. mci_writel(host, CLKENA, 0);
  1842. mci_writel(host, CLKSRC, 0);
  1843. free_irq(host->irq, host);
  1844. destroy_workqueue(host->card_workqueue);
  1845. dma_free_coherent(host->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  1846. if (host->use_dma && host->dma_ops->exit)
  1847. host->dma_ops->exit(host);
  1848. if (host->vmmc) {
  1849. regulator_disable(host->vmmc);
  1850. regulator_put(host->vmmc);
  1851. }
  1852. if (!IS_ERR(host->ciu_clk))
  1853. clk_disable_unprepare(host->ciu_clk);
  1854. if (!IS_ERR(host->biu_clk))
  1855. clk_disable_unprepare(host->biu_clk);
  1856. clk_put(host->ciu_clk);
  1857. clk_put(host->biu_clk);
  1858. }
  1859. EXPORT_SYMBOL(dw_mci_remove);
  1860. #ifdef CONFIG_PM_SLEEP
  1861. /*
  1862. * TODO: we should probably disable the clock to the card in the suspend path.
  1863. */
  1864. int dw_mci_suspend(struct dw_mci *host)
  1865. {
  1866. int i, ret = 0;
  1867. for (i = 0; i < host->num_slots; i++) {
  1868. struct dw_mci_slot *slot = host->slot[i];
  1869. if (!slot)
  1870. continue;
  1871. ret = mmc_suspend_host(slot->mmc);
  1872. if (ret < 0) {
  1873. while (--i >= 0) {
  1874. slot = host->slot[i];
  1875. if (slot)
  1876. mmc_resume_host(host->slot[i]->mmc);
  1877. }
  1878. return ret;
  1879. }
  1880. }
  1881. if (host->vmmc)
  1882. regulator_disable(host->vmmc);
  1883. return 0;
  1884. }
  1885. EXPORT_SYMBOL(dw_mci_suspend);
  1886. int dw_mci_resume(struct dw_mci *host)
  1887. {
  1888. int i, ret;
  1889. if (host->vmmc)
  1890. regulator_enable(host->vmmc);
  1891. if (!mci_wait_reset(host->dev, host)) {
  1892. ret = -ENODEV;
  1893. return ret;
  1894. }
  1895. if (host->use_dma && host->dma_ops->init)
  1896. host->dma_ops->init(host);
  1897. /* Restore the old value at FIFOTH register */
  1898. mci_writel(host, FIFOTH, host->fifoth_val);
  1899. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1900. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  1901. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  1902. DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
  1903. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
  1904. for (i = 0; i < host->num_slots; i++) {
  1905. struct dw_mci_slot *slot = host->slot[i];
  1906. if (!slot)
  1907. continue;
  1908. ret = mmc_resume_host(host->slot[i]->mmc);
  1909. if (ret < 0)
  1910. return ret;
  1911. }
  1912. return 0;
  1913. }
  1914. EXPORT_SYMBOL(dw_mci_resume);
  1915. #endif /* CONFIG_PM_SLEEP */
  1916. static int __init dw_mci_init(void)
  1917. {
  1918. printk(KERN_INFO "Synopsys Designware Multimedia Card Interface Driver");
  1919. return 0;
  1920. }
  1921. static void __exit dw_mci_exit(void)
  1922. {
  1923. }
  1924. module_init(dw_mci_init);
  1925. module_exit(dw_mci_exit);
  1926. MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
  1927. MODULE_AUTHOR("NXP Semiconductor VietNam");
  1928. MODULE_AUTHOR("Imagination Technologies Ltd");
  1929. MODULE_LICENSE("GPL v2");