dw_mmc.c 42 KB

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