dw_mmc.c 51 KB

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