dw_mmc.c 59 KB

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