dw_mmc.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848
  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/sd.h>
  32. #include <linux/mmc/sdio.h>
  33. #include <linux/mmc/dw_mmc.h>
  34. #include <linux/bitops.h>
  35. #include <linux/regulator/consumer.h>
  36. #include <linux/workqueue.h>
  37. #include <linux/of.h>
  38. #include <linux/of_gpio.h>
  39. #include <linux/mmc/slot-gpio.h>
  40. #include "dw_mmc.h"
  41. /* Common flag combinations */
  42. #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
  43. SDMMC_INT_HTO | SDMMC_INT_SBE | \
  44. SDMMC_INT_EBE)
  45. #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
  46. SDMMC_INT_RESP_ERR)
  47. #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
  48. DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
  49. #define DW_MCI_SEND_STATUS 1
  50. #define DW_MCI_RECV_STATUS 2
  51. #define DW_MCI_DMA_THRESHOLD 16
  52. #define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */
  53. #define DW_MCI_FREQ_MIN 400000 /* unit: HZ */
  54. #ifdef CONFIG_MMC_DW_IDMAC
  55. #define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
  56. SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
  57. SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
  58. SDMMC_IDMAC_INT_TI)
  59. struct idmac_desc {
  60. u32 des0; /* Control Descriptor */
  61. #define IDMAC_DES0_DIC BIT(1)
  62. #define IDMAC_DES0_LD BIT(2)
  63. #define IDMAC_DES0_FD BIT(3)
  64. #define IDMAC_DES0_CH BIT(4)
  65. #define IDMAC_DES0_ER BIT(5)
  66. #define IDMAC_DES0_CES BIT(30)
  67. #define IDMAC_DES0_OWN BIT(31)
  68. u32 des1; /* Buffer sizes */
  69. #define IDMAC_SET_BUFFER1_SIZE(d, s) \
  70. ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
  71. u32 des2; /* buffer 1 physical address */
  72. u32 des3; /* buffer 2 physical address */
  73. };
  74. #endif /* CONFIG_MMC_DW_IDMAC */
  75. static bool dw_mci_reset(struct dw_mci *host);
  76. #if defined(CONFIG_DEBUG_FS)
  77. static int dw_mci_req_show(struct seq_file *s, void *v)
  78. {
  79. struct dw_mci_slot *slot = s->private;
  80. struct mmc_request *mrq;
  81. struct mmc_command *cmd;
  82. struct mmc_command *stop;
  83. struct mmc_data *data;
  84. /* Make sure we get a consistent snapshot */
  85. spin_lock_bh(&slot->host->lock);
  86. mrq = slot->mrq;
  87. if (mrq) {
  88. cmd = mrq->cmd;
  89. data = mrq->data;
  90. stop = mrq->stop;
  91. if (cmd)
  92. seq_printf(s,
  93. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  94. cmd->opcode, cmd->arg, cmd->flags,
  95. cmd->resp[0], cmd->resp[1], cmd->resp[2],
  96. cmd->resp[2], cmd->error);
  97. if (data)
  98. seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
  99. data->bytes_xfered, data->blocks,
  100. data->blksz, data->flags, data->error);
  101. if (stop)
  102. seq_printf(s,
  103. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  104. stop->opcode, stop->arg, stop->flags,
  105. stop->resp[0], stop->resp[1], stop->resp[2],
  106. stop->resp[2], stop->error);
  107. }
  108. spin_unlock_bh(&slot->host->lock);
  109. return 0;
  110. }
  111. static int dw_mci_req_open(struct inode *inode, struct file *file)
  112. {
  113. return single_open(file, dw_mci_req_show, inode->i_private);
  114. }
  115. static const struct file_operations dw_mci_req_fops = {
  116. .owner = THIS_MODULE,
  117. .open = dw_mci_req_open,
  118. .read = seq_read,
  119. .llseek = seq_lseek,
  120. .release = single_release,
  121. };
  122. static int dw_mci_regs_show(struct seq_file *s, void *v)
  123. {
  124. seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
  125. seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
  126. seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
  127. seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
  128. seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
  129. seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
  130. return 0;
  131. }
  132. static int dw_mci_regs_open(struct inode *inode, struct file *file)
  133. {
  134. return single_open(file, dw_mci_regs_show, inode->i_private);
  135. }
  136. static const struct file_operations dw_mci_regs_fops = {
  137. .owner = THIS_MODULE,
  138. .open = dw_mci_regs_open,
  139. .read = seq_read,
  140. .llseek = seq_lseek,
  141. .release = single_release,
  142. };
  143. static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
  144. {
  145. struct mmc_host *mmc = slot->mmc;
  146. struct dw_mci *host = slot->host;
  147. struct dentry *root;
  148. struct dentry *node;
  149. root = mmc->debugfs_root;
  150. if (!root)
  151. return;
  152. node = debugfs_create_file("regs", S_IRUSR, root, host,
  153. &dw_mci_regs_fops);
  154. if (!node)
  155. goto err;
  156. node = debugfs_create_file("req", S_IRUSR, root, slot,
  157. &dw_mci_req_fops);
  158. if (!node)
  159. goto err;
  160. node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
  161. if (!node)
  162. goto err;
  163. node = debugfs_create_x32("pending_events", S_IRUSR, root,
  164. (u32 *)&host->pending_events);
  165. if (!node)
  166. goto err;
  167. node = debugfs_create_x32("completed_events", S_IRUSR, root,
  168. (u32 *)&host->completed_events);
  169. if (!node)
  170. goto err;
  171. return;
  172. err:
  173. dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
  174. }
  175. #endif /* defined(CONFIG_DEBUG_FS) */
  176. static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg);
  177. static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
  178. {
  179. struct mmc_data *data;
  180. struct dw_mci_slot *slot = mmc_priv(mmc);
  181. struct dw_mci *host = slot->host;
  182. const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
  183. u32 cmdr;
  184. cmd->error = -EINPROGRESS;
  185. cmdr = cmd->opcode;
  186. if (cmd->opcode == MMC_STOP_TRANSMISSION ||
  187. cmd->opcode == MMC_GO_IDLE_STATE ||
  188. cmd->opcode == MMC_GO_INACTIVE_STATE ||
  189. (cmd->opcode == SD_IO_RW_DIRECT &&
  190. ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
  191. cmdr |= SDMMC_CMD_STOP;
  192. else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
  193. cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
  194. if (cmd->opcode == SD_SWITCH_VOLTAGE) {
  195. u32 clk_en_a;
  196. /* Special bit makes CMD11 not die */
  197. cmdr |= SDMMC_CMD_VOLT_SWITCH;
  198. /* Change state to continue to handle CMD11 weirdness */
  199. WARN_ON(slot->host->state != STATE_SENDING_CMD);
  200. slot->host->state = STATE_SENDING_CMD11;
  201. /*
  202. * We need to disable low power mode (automatic clock stop)
  203. * while doing voltage switch so we don't confuse the card,
  204. * since stopping the clock is a specific part of the UHS
  205. * voltage change dance.
  206. *
  207. * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
  208. * unconditionally turned back on in dw_mci_setup_bus() if it's
  209. * ever called with a non-zero clock. That shouldn't happen
  210. * until the voltage change is all done.
  211. */
  212. clk_en_a = mci_readl(host, CLKENA);
  213. clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
  214. mci_writel(host, CLKENA, clk_en_a);
  215. mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
  216. SDMMC_CMD_PRV_DAT_WAIT, 0);
  217. }
  218. if (cmd->flags & MMC_RSP_PRESENT) {
  219. /* We expect a response, so set this bit */
  220. cmdr |= SDMMC_CMD_RESP_EXP;
  221. if (cmd->flags & MMC_RSP_136)
  222. cmdr |= SDMMC_CMD_RESP_LONG;
  223. }
  224. if (cmd->flags & MMC_RSP_CRC)
  225. cmdr |= SDMMC_CMD_RESP_CRC;
  226. data = cmd->data;
  227. if (data) {
  228. cmdr |= SDMMC_CMD_DAT_EXP;
  229. if (data->flags & MMC_DATA_STREAM)
  230. cmdr |= SDMMC_CMD_STRM_MODE;
  231. if (data->flags & MMC_DATA_WRITE)
  232. cmdr |= SDMMC_CMD_DAT_WR;
  233. }
  234. if (drv_data && drv_data->prepare_command)
  235. drv_data->prepare_command(slot->host, &cmdr);
  236. return cmdr;
  237. }
  238. static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
  239. {
  240. struct mmc_command *stop;
  241. u32 cmdr;
  242. if (!cmd->data)
  243. return 0;
  244. stop = &host->stop_abort;
  245. cmdr = cmd->opcode;
  246. memset(stop, 0, sizeof(struct mmc_command));
  247. if (cmdr == MMC_READ_SINGLE_BLOCK ||
  248. cmdr == MMC_READ_MULTIPLE_BLOCK ||
  249. cmdr == MMC_WRITE_BLOCK ||
  250. cmdr == MMC_WRITE_MULTIPLE_BLOCK) {
  251. stop->opcode = MMC_STOP_TRANSMISSION;
  252. stop->arg = 0;
  253. stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
  254. } else if (cmdr == SD_IO_RW_EXTENDED) {
  255. stop->opcode = SD_IO_RW_DIRECT;
  256. stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
  257. ((cmd->arg >> 28) & 0x7);
  258. stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
  259. } else {
  260. return 0;
  261. }
  262. cmdr = stop->opcode | SDMMC_CMD_STOP |
  263. SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
  264. return cmdr;
  265. }
  266. static void dw_mci_start_command(struct dw_mci *host,
  267. struct mmc_command *cmd, u32 cmd_flags)
  268. {
  269. host->cmd = cmd;
  270. dev_vdbg(host->dev,
  271. "start command: ARGR=0x%08x CMDR=0x%08x\n",
  272. cmd->arg, cmd_flags);
  273. mci_writel(host, CMDARG, cmd->arg);
  274. wmb();
  275. mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
  276. }
  277. static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
  278. {
  279. struct mmc_command *stop = data->stop ? data->stop : &host->stop_abort;
  280. dw_mci_start_command(host, stop, host->stop_cmdr);
  281. }
  282. /* DMA interface functions */
  283. static void dw_mci_stop_dma(struct dw_mci *host)
  284. {
  285. if (host->using_dma) {
  286. host->dma_ops->stop(host);
  287. host->dma_ops->cleanup(host);
  288. }
  289. /* Data transfer was stopped by the interrupt handler */
  290. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  291. }
  292. static int dw_mci_get_dma_dir(struct mmc_data *data)
  293. {
  294. if (data->flags & MMC_DATA_WRITE)
  295. return DMA_TO_DEVICE;
  296. else
  297. return DMA_FROM_DEVICE;
  298. }
  299. #ifdef CONFIG_MMC_DW_IDMAC
  300. static void dw_mci_dma_cleanup(struct dw_mci *host)
  301. {
  302. struct mmc_data *data = host->data;
  303. if (data)
  304. if (!data->host_cookie)
  305. dma_unmap_sg(host->dev,
  306. data->sg,
  307. data->sg_len,
  308. dw_mci_get_dma_dir(data));
  309. }
  310. static void dw_mci_idmac_reset(struct dw_mci *host)
  311. {
  312. u32 bmod = mci_readl(host, BMOD);
  313. /* Software reset of DMA */
  314. bmod |= SDMMC_IDMAC_SWRESET;
  315. mci_writel(host, BMOD, bmod);
  316. }
  317. static void dw_mci_idmac_stop_dma(struct dw_mci *host)
  318. {
  319. u32 temp;
  320. /* Disable and reset the IDMAC interface */
  321. temp = mci_readl(host, CTRL);
  322. temp &= ~SDMMC_CTRL_USE_IDMAC;
  323. temp |= SDMMC_CTRL_DMA_RESET;
  324. mci_writel(host, CTRL, temp);
  325. /* Stop the IDMAC running */
  326. temp = mci_readl(host, BMOD);
  327. temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
  328. temp |= SDMMC_IDMAC_SWRESET;
  329. mci_writel(host, BMOD, temp);
  330. }
  331. static void dw_mci_idmac_complete_dma(struct dw_mci *host)
  332. {
  333. struct mmc_data *data = host->data;
  334. dev_vdbg(host->dev, "DMA complete\n");
  335. host->dma_ops->cleanup(host);
  336. /*
  337. * If the card was removed, data will be NULL. No point in trying to
  338. * send the stop command or waiting for NBUSY in this case.
  339. */
  340. if (data) {
  341. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  342. tasklet_schedule(&host->tasklet);
  343. }
  344. }
  345. static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
  346. unsigned int sg_len)
  347. {
  348. int i;
  349. struct idmac_desc *desc = host->sg_cpu;
  350. for (i = 0; i < sg_len; i++, desc++) {
  351. unsigned int length = sg_dma_len(&data->sg[i]);
  352. u32 mem_addr = sg_dma_address(&data->sg[i]);
  353. /* Set the OWN bit and disable interrupts for this descriptor */
  354. desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | IDMAC_DES0_CH;
  355. /* Buffer length */
  356. IDMAC_SET_BUFFER1_SIZE(desc, length);
  357. /* Physical address to DMA to/from */
  358. desc->des2 = mem_addr;
  359. }
  360. /* Set first descriptor */
  361. desc = host->sg_cpu;
  362. desc->des0 |= IDMAC_DES0_FD;
  363. /* Set last descriptor */
  364. desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc);
  365. desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
  366. desc->des0 |= IDMAC_DES0_LD;
  367. wmb();
  368. }
  369. static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
  370. {
  371. u32 temp;
  372. dw_mci_translate_sglist(host, host->data, sg_len);
  373. /* Select IDMAC interface */
  374. temp = mci_readl(host, CTRL);
  375. temp |= SDMMC_CTRL_USE_IDMAC;
  376. mci_writel(host, CTRL, temp);
  377. wmb();
  378. /* Enable the IDMAC */
  379. temp = mci_readl(host, BMOD);
  380. temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
  381. mci_writel(host, BMOD, temp);
  382. /* Start it running */
  383. mci_writel(host, PLDMND, 1);
  384. }
  385. static int dw_mci_idmac_init(struct dw_mci *host)
  386. {
  387. struct idmac_desc *p;
  388. int i;
  389. /* Number of descriptors in the ring buffer */
  390. host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
  391. /* Forward link the descriptor list */
  392. for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++)
  393. p->des3 = host->sg_dma + (sizeof(struct idmac_desc) * (i + 1));
  394. /* Set the last descriptor as the end-of-ring descriptor */
  395. p->des3 = host->sg_dma;
  396. p->des0 = IDMAC_DES0_ER;
  397. dw_mci_idmac_reset(host);
  398. /* Mask out interrupts - get Tx & Rx complete only */
  399. mci_writel(host, IDSTS, IDMAC_INT_CLR);
  400. mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI | SDMMC_IDMAC_INT_RI |
  401. SDMMC_IDMAC_INT_TI);
  402. /* Set the descriptor base address */
  403. mci_writel(host, DBADDR, host->sg_dma);
  404. return 0;
  405. }
  406. static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
  407. .init = dw_mci_idmac_init,
  408. .start = dw_mci_idmac_start_dma,
  409. .stop = dw_mci_idmac_stop_dma,
  410. .complete = dw_mci_idmac_complete_dma,
  411. .cleanup = dw_mci_dma_cleanup,
  412. };
  413. #endif /* CONFIG_MMC_DW_IDMAC */
  414. static int dw_mci_pre_dma_transfer(struct dw_mci *host,
  415. struct mmc_data *data,
  416. bool next)
  417. {
  418. struct scatterlist *sg;
  419. unsigned int i, sg_len;
  420. if (!next && data->host_cookie)
  421. return data->host_cookie;
  422. /*
  423. * We don't do DMA on "complex" transfers, i.e. with
  424. * non-word-aligned buffers or lengths. Also, we don't bother
  425. * with all the DMA setup overhead for short transfers.
  426. */
  427. if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
  428. return -EINVAL;
  429. if (data->blksz & 3)
  430. return -EINVAL;
  431. for_each_sg(data->sg, sg, data->sg_len, i) {
  432. if (sg->offset & 3 || sg->length & 3)
  433. return -EINVAL;
  434. }
  435. sg_len = dma_map_sg(host->dev,
  436. data->sg,
  437. data->sg_len,
  438. dw_mci_get_dma_dir(data));
  439. if (sg_len == 0)
  440. return -EINVAL;
  441. if (next)
  442. data->host_cookie = sg_len;
  443. return sg_len;
  444. }
  445. static void dw_mci_pre_req(struct mmc_host *mmc,
  446. struct mmc_request *mrq,
  447. bool is_first_req)
  448. {
  449. struct dw_mci_slot *slot = mmc_priv(mmc);
  450. struct mmc_data *data = mrq->data;
  451. if (!slot->host->use_dma || !data)
  452. return;
  453. if (data->host_cookie) {
  454. data->host_cookie = 0;
  455. return;
  456. }
  457. if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0)
  458. data->host_cookie = 0;
  459. }
  460. static void dw_mci_post_req(struct mmc_host *mmc,
  461. struct mmc_request *mrq,
  462. int err)
  463. {
  464. struct dw_mci_slot *slot = mmc_priv(mmc);
  465. struct mmc_data *data = mrq->data;
  466. if (!slot->host->use_dma || !data)
  467. return;
  468. if (data->host_cookie)
  469. dma_unmap_sg(slot->host->dev,
  470. data->sg,
  471. data->sg_len,
  472. dw_mci_get_dma_dir(data));
  473. data->host_cookie = 0;
  474. }
  475. static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
  476. {
  477. #ifdef CONFIG_MMC_DW_IDMAC
  478. unsigned int blksz = data->blksz;
  479. const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
  480. u32 fifo_width = 1 << host->data_shift;
  481. u32 blksz_depth = blksz / fifo_width, fifoth_val;
  482. u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
  483. int idx = (sizeof(mszs) / sizeof(mszs[0])) - 1;
  484. tx_wmark = (host->fifo_depth) / 2;
  485. tx_wmark_invers = host->fifo_depth - tx_wmark;
  486. /*
  487. * MSIZE is '1',
  488. * if blksz is not a multiple of the FIFO width
  489. */
  490. if (blksz % fifo_width) {
  491. msize = 0;
  492. rx_wmark = 1;
  493. goto done;
  494. }
  495. do {
  496. if (!((blksz_depth % mszs[idx]) ||
  497. (tx_wmark_invers % mszs[idx]))) {
  498. msize = idx;
  499. rx_wmark = mszs[idx] - 1;
  500. break;
  501. }
  502. } while (--idx > 0);
  503. /*
  504. * If idx is '0', it won't be tried
  505. * Thus, initial values are uesed
  506. */
  507. done:
  508. fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
  509. mci_writel(host, FIFOTH, fifoth_val);
  510. #endif
  511. }
  512. static void dw_mci_ctrl_rd_thld(struct dw_mci *host, struct mmc_data *data)
  513. {
  514. unsigned int blksz = data->blksz;
  515. u32 blksz_depth, fifo_depth;
  516. u16 thld_size;
  517. WARN_ON(!(data->flags & MMC_DATA_READ));
  518. if (host->timing != MMC_TIMING_MMC_HS200 &&
  519. host->timing != MMC_TIMING_UHS_SDR104)
  520. goto disable;
  521. blksz_depth = blksz / (1 << host->data_shift);
  522. fifo_depth = host->fifo_depth;
  523. if (blksz_depth > fifo_depth)
  524. goto disable;
  525. /*
  526. * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
  527. * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
  528. * Currently just choose blksz.
  529. */
  530. thld_size = blksz;
  531. mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(thld_size, 1));
  532. return;
  533. disable:
  534. mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(0, 0));
  535. }
  536. static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
  537. {
  538. int sg_len;
  539. u32 temp;
  540. host->using_dma = 0;
  541. /* If we don't have a channel, we can't do DMA */
  542. if (!host->use_dma)
  543. return -ENODEV;
  544. sg_len = dw_mci_pre_dma_transfer(host, data, 0);
  545. if (sg_len < 0) {
  546. host->dma_ops->stop(host);
  547. return sg_len;
  548. }
  549. host->using_dma = 1;
  550. dev_vdbg(host->dev,
  551. "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
  552. (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
  553. sg_len);
  554. /*
  555. * Decide the MSIZE and RX/TX Watermark.
  556. * If current block size is same with previous size,
  557. * no need to update fifoth.
  558. */
  559. if (host->prev_blksz != data->blksz)
  560. dw_mci_adjust_fifoth(host, data);
  561. /* Enable the DMA interface */
  562. temp = mci_readl(host, CTRL);
  563. temp |= SDMMC_CTRL_DMA_ENABLE;
  564. mci_writel(host, CTRL, temp);
  565. /* Disable RX/TX IRQs, let DMA handle it */
  566. temp = mci_readl(host, INTMASK);
  567. temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
  568. mci_writel(host, INTMASK, temp);
  569. host->dma_ops->start(host, sg_len);
  570. return 0;
  571. }
  572. static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
  573. {
  574. u32 temp;
  575. data->error = -EINPROGRESS;
  576. WARN_ON(host->data);
  577. host->sg = NULL;
  578. host->data = data;
  579. if (data->flags & MMC_DATA_READ) {
  580. host->dir_status = DW_MCI_RECV_STATUS;
  581. dw_mci_ctrl_rd_thld(host, data);
  582. } else {
  583. host->dir_status = DW_MCI_SEND_STATUS;
  584. }
  585. if (dw_mci_submit_data_dma(host, data)) {
  586. int flags = SG_MITER_ATOMIC;
  587. if (host->data->flags & MMC_DATA_READ)
  588. flags |= SG_MITER_TO_SG;
  589. else
  590. flags |= SG_MITER_FROM_SG;
  591. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  592. host->sg = data->sg;
  593. host->part_buf_start = 0;
  594. host->part_buf_count = 0;
  595. mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
  596. temp = mci_readl(host, INTMASK);
  597. temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
  598. mci_writel(host, INTMASK, temp);
  599. temp = mci_readl(host, CTRL);
  600. temp &= ~SDMMC_CTRL_DMA_ENABLE;
  601. mci_writel(host, CTRL, temp);
  602. /*
  603. * Use the initial fifoth_val for PIO mode.
  604. * If next issued data may be transfered by DMA mode,
  605. * prev_blksz should be invalidated.
  606. */
  607. mci_writel(host, FIFOTH, host->fifoth_val);
  608. host->prev_blksz = 0;
  609. } else {
  610. /*
  611. * Keep the current block size.
  612. * It will be used to decide whether to update
  613. * fifoth register next time.
  614. */
  615. host->prev_blksz = data->blksz;
  616. }
  617. }
  618. static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
  619. {
  620. struct dw_mci *host = slot->host;
  621. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  622. unsigned int cmd_status = 0;
  623. mci_writel(host, CMDARG, arg);
  624. wmb();
  625. mci_writel(host, CMD, SDMMC_CMD_START | cmd);
  626. while (time_before(jiffies, timeout)) {
  627. cmd_status = mci_readl(host, CMD);
  628. if (!(cmd_status & SDMMC_CMD_START))
  629. return;
  630. }
  631. dev_err(&slot->mmc->class_dev,
  632. "Timeout sending command (cmd %#x arg %#x status %#x)\n",
  633. cmd, arg, cmd_status);
  634. }
  635. static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
  636. {
  637. struct dw_mci *host = slot->host;
  638. unsigned int clock = slot->clock;
  639. u32 div;
  640. u32 clk_en_a;
  641. u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
  642. /* We must continue to set bit 28 in CMD until the change is complete */
  643. if (host->state == STATE_WAITING_CMD11_DONE)
  644. sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
  645. if (!clock) {
  646. mci_writel(host, CLKENA, 0);
  647. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  648. } else if (clock != host->current_speed || force_clkinit) {
  649. div = host->bus_hz / clock;
  650. if (host->bus_hz % clock && host->bus_hz > clock)
  651. /*
  652. * move the + 1 after the divide to prevent
  653. * over-clocking the card.
  654. */
  655. div += 1;
  656. div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
  657. if ((clock << div) != slot->__clk_old || force_clkinit)
  658. dev_info(&slot->mmc->class_dev,
  659. "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
  660. slot->id, host->bus_hz, clock,
  661. div ? ((host->bus_hz / div) >> 1) :
  662. host->bus_hz, div);
  663. /* disable clock */
  664. mci_writel(host, CLKENA, 0);
  665. mci_writel(host, CLKSRC, 0);
  666. /* inform CIU */
  667. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  668. /* set clock to desired speed */
  669. mci_writel(host, CLKDIV, div);
  670. /* inform CIU */
  671. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  672. /* enable clock; only low power if no SDIO */
  673. clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
  674. if (!(mci_readl(host, INTMASK) & SDMMC_INT_SDIO(slot->id)))
  675. clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
  676. mci_writel(host, CLKENA, clk_en_a);
  677. /* inform CIU */
  678. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  679. /* keep the clock with reflecting clock dividor */
  680. slot->__clk_old = clock << div;
  681. }
  682. host->current_speed = clock;
  683. /* Set the current slot bus width */
  684. mci_writel(host, CTYPE, (slot->ctype << slot->id));
  685. }
  686. static void __dw_mci_start_request(struct dw_mci *host,
  687. struct dw_mci_slot *slot,
  688. struct mmc_command *cmd)
  689. {
  690. struct mmc_request *mrq;
  691. struct mmc_data *data;
  692. u32 cmdflags;
  693. mrq = slot->mrq;
  694. host->cur_slot = slot;
  695. host->mrq = mrq;
  696. host->pending_events = 0;
  697. host->completed_events = 0;
  698. host->cmd_status = 0;
  699. host->data_status = 0;
  700. host->dir_status = 0;
  701. data = cmd->data;
  702. if (data) {
  703. mci_writel(host, TMOUT, 0xFFFFFFFF);
  704. mci_writel(host, BYTCNT, data->blksz*data->blocks);
  705. mci_writel(host, BLKSIZ, data->blksz);
  706. }
  707. cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
  708. /* this is the first command, send the initialization clock */
  709. if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
  710. cmdflags |= SDMMC_CMD_INIT;
  711. if (data) {
  712. dw_mci_submit_data(host, data);
  713. wmb();
  714. }
  715. dw_mci_start_command(host, cmd, cmdflags);
  716. if (mrq->stop)
  717. host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
  718. else
  719. host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
  720. }
  721. static void dw_mci_start_request(struct dw_mci *host,
  722. struct dw_mci_slot *slot)
  723. {
  724. struct mmc_request *mrq = slot->mrq;
  725. struct mmc_command *cmd;
  726. cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
  727. __dw_mci_start_request(host, slot, cmd);
  728. }
  729. /* must be called with host->lock held */
  730. static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
  731. struct mmc_request *mrq)
  732. {
  733. dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
  734. host->state);
  735. slot->mrq = mrq;
  736. if (host->state == STATE_WAITING_CMD11_DONE) {
  737. dev_warn(&slot->mmc->class_dev,
  738. "Voltage change didn't complete\n");
  739. /*
  740. * this case isn't expected to happen, so we can
  741. * either crash here or just try to continue on
  742. * in the closest possible state
  743. */
  744. host->state = STATE_IDLE;
  745. }
  746. if (host->state == STATE_IDLE) {
  747. host->state = STATE_SENDING_CMD;
  748. dw_mci_start_request(host, slot);
  749. } else {
  750. list_add_tail(&slot->queue_node, &host->queue);
  751. }
  752. }
  753. static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  754. {
  755. struct dw_mci_slot *slot = mmc_priv(mmc);
  756. struct dw_mci *host = slot->host;
  757. WARN_ON(slot->mrq);
  758. /*
  759. * The check for card presence and queueing of the request must be
  760. * atomic, otherwise the card could be removed in between and the
  761. * request wouldn't fail until another card was inserted.
  762. */
  763. spin_lock_bh(&host->lock);
  764. if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
  765. spin_unlock_bh(&host->lock);
  766. mrq->cmd->error = -ENOMEDIUM;
  767. mmc_request_done(mmc, mrq);
  768. return;
  769. }
  770. dw_mci_queue_request(host, slot, mrq);
  771. spin_unlock_bh(&host->lock);
  772. }
  773. static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  774. {
  775. struct dw_mci_slot *slot = mmc_priv(mmc);
  776. const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
  777. u32 regs;
  778. int ret;
  779. switch (ios->bus_width) {
  780. case MMC_BUS_WIDTH_4:
  781. slot->ctype = SDMMC_CTYPE_4BIT;
  782. break;
  783. case MMC_BUS_WIDTH_8:
  784. slot->ctype = SDMMC_CTYPE_8BIT;
  785. break;
  786. default:
  787. /* set default 1 bit mode */
  788. slot->ctype = SDMMC_CTYPE_1BIT;
  789. }
  790. regs = mci_readl(slot->host, UHS_REG);
  791. /* DDR mode set */
  792. if (ios->timing == MMC_TIMING_MMC_DDR52)
  793. regs |= ((0x1 << slot->id) << 16);
  794. else
  795. regs &= ~((0x1 << slot->id) << 16);
  796. mci_writel(slot->host, UHS_REG, regs);
  797. slot->host->timing = ios->timing;
  798. /*
  799. * Use mirror of ios->clock to prevent race with mmc
  800. * core ios update when finding the minimum.
  801. */
  802. slot->clock = ios->clock;
  803. if (drv_data && drv_data->set_ios)
  804. drv_data->set_ios(slot->host, ios);
  805. /* Slot specific timing and width adjustment */
  806. dw_mci_setup_bus(slot, false);
  807. if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
  808. slot->host->state = STATE_IDLE;
  809. switch (ios->power_mode) {
  810. case MMC_POWER_UP:
  811. if (!IS_ERR(mmc->supply.vmmc)) {
  812. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
  813. ios->vdd);
  814. if (ret) {
  815. dev_err(slot->host->dev,
  816. "failed to enable vmmc regulator\n");
  817. /*return, if failed turn on vmmc*/
  818. return;
  819. }
  820. }
  821. if (!IS_ERR(mmc->supply.vqmmc) && !slot->host->vqmmc_enabled) {
  822. ret = regulator_enable(mmc->supply.vqmmc);
  823. if (ret < 0)
  824. dev_err(slot->host->dev,
  825. "failed to enable vqmmc regulator\n");
  826. else
  827. slot->host->vqmmc_enabled = true;
  828. }
  829. set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
  830. regs = mci_readl(slot->host, PWREN);
  831. regs |= (1 << slot->id);
  832. mci_writel(slot->host, PWREN, regs);
  833. break;
  834. case MMC_POWER_OFF:
  835. if (!IS_ERR(mmc->supply.vmmc))
  836. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  837. if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled) {
  838. regulator_disable(mmc->supply.vqmmc);
  839. slot->host->vqmmc_enabled = false;
  840. }
  841. regs = mci_readl(slot->host, PWREN);
  842. regs &= ~(1 << slot->id);
  843. mci_writel(slot->host, PWREN, regs);
  844. break;
  845. default:
  846. break;
  847. }
  848. }
  849. static int dw_mci_card_busy(struct mmc_host *mmc)
  850. {
  851. struct dw_mci_slot *slot = mmc_priv(mmc);
  852. u32 status;
  853. /*
  854. * Check the busy bit which is low when DAT[3:0]
  855. * (the data lines) are 0000
  856. */
  857. status = mci_readl(slot->host, STATUS);
  858. return !!(status & SDMMC_STATUS_BUSY);
  859. }
  860. static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
  861. {
  862. struct dw_mci_slot *slot = mmc_priv(mmc);
  863. struct dw_mci *host = slot->host;
  864. u32 uhs;
  865. u32 v18 = SDMMC_UHS_18V << slot->id;
  866. int min_uv, max_uv;
  867. int ret;
  868. /*
  869. * Program the voltage. Note that some instances of dw_mmc may use
  870. * the UHS_REG for this. For other instances (like exynos) the UHS_REG
  871. * does no harm but you need to set the regulator directly. Try both.
  872. */
  873. uhs = mci_readl(host, UHS_REG);
  874. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  875. min_uv = 2700000;
  876. max_uv = 3600000;
  877. uhs &= ~v18;
  878. } else {
  879. min_uv = 1700000;
  880. max_uv = 1950000;
  881. uhs |= v18;
  882. }
  883. if (!IS_ERR(mmc->supply.vqmmc)) {
  884. ret = regulator_set_voltage(mmc->supply.vqmmc, min_uv, max_uv);
  885. if (ret) {
  886. dev_err(&mmc->class_dev,
  887. "Regulator set error %d: %d - %d\n",
  888. ret, min_uv, max_uv);
  889. return ret;
  890. }
  891. }
  892. mci_writel(host, UHS_REG, uhs);
  893. return 0;
  894. }
  895. static int dw_mci_get_ro(struct mmc_host *mmc)
  896. {
  897. int read_only;
  898. struct dw_mci_slot *slot = mmc_priv(mmc);
  899. int gpio_ro = mmc_gpio_get_ro(mmc);
  900. /* Use platform get_ro function, else try on board write protect */
  901. if ((slot->quirks & DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT) ||
  902. (slot->host->quirks & DW_MCI_QUIRK_NO_WRITE_PROTECT))
  903. read_only = 0;
  904. else if (!IS_ERR_VALUE(gpio_ro))
  905. read_only = gpio_ro;
  906. else
  907. read_only =
  908. mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
  909. dev_dbg(&mmc->class_dev, "card is %s\n",
  910. read_only ? "read-only" : "read-write");
  911. return read_only;
  912. }
  913. static int dw_mci_get_cd(struct mmc_host *mmc)
  914. {
  915. int present;
  916. struct dw_mci_slot *slot = mmc_priv(mmc);
  917. struct dw_mci_board *brd = slot->host->pdata;
  918. struct dw_mci *host = slot->host;
  919. int gpio_cd = mmc_gpio_get_cd(mmc);
  920. /* Use platform get_cd function, else try onboard card detect */
  921. if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
  922. present = 1;
  923. else if (!IS_ERR_VALUE(gpio_cd))
  924. present = gpio_cd;
  925. else
  926. present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
  927. == 0 ? 1 : 0;
  928. spin_lock_bh(&host->lock);
  929. if (present) {
  930. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  931. dev_dbg(&mmc->class_dev, "card is present\n");
  932. } else {
  933. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  934. dev_dbg(&mmc->class_dev, "card is not present\n");
  935. }
  936. spin_unlock_bh(&host->lock);
  937. return present;
  938. }
  939. /*
  940. * Disable lower power mode.
  941. *
  942. * Low power mode will stop the card clock when idle. According to the
  943. * description of the CLKENA register we should disable low power mode
  944. * for SDIO cards if we need SDIO interrupts to work.
  945. *
  946. * This function is fast if low power mode is already disabled.
  947. */
  948. static void dw_mci_disable_low_power(struct dw_mci_slot *slot)
  949. {
  950. struct dw_mci *host = slot->host;
  951. u32 clk_en_a;
  952. const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
  953. clk_en_a = mci_readl(host, CLKENA);
  954. if (clk_en_a & clken_low_pwr) {
  955. mci_writel(host, CLKENA, clk_en_a & ~clken_low_pwr);
  956. mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
  957. SDMMC_CMD_PRV_DAT_WAIT, 0);
  958. }
  959. }
  960. static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
  961. {
  962. struct dw_mci_slot *slot = mmc_priv(mmc);
  963. struct dw_mci *host = slot->host;
  964. u32 int_mask;
  965. /* Enable/disable Slot Specific SDIO interrupt */
  966. int_mask = mci_readl(host, INTMASK);
  967. if (enb) {
  968. /*
  969. * Turn off low power mode if it was enabled. This is a bit of
  970. * a heavy operation and we disable / enable IRQs a lot, so
  971. * we'll leave low power mode disabled and it will get
  972. * re-enabled again in dw_mci_setup_bus().
  973. */
  974. dw_mci_disable_low_power(slot);
  975. mci_writel(host, INTMASK,
  976. (int_mask | SDMMC_INT_SDIO(slot->id)));
  977. } else {
  978. mci_writel(host, INTMASK,
  979. (int_mask & ~SDMMC_INT_SDIO(slot->id)));
  980. }
  981. }
  982. static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
  983. {
  984. struct dw_mci_slot *slot = mmc_priv(mmc);
  985. struct dw_mci *host = slot->host;
  986. const struct dw_mci_drv_data *drv_data = host->drv_data;
  987. struct dw_mci_tuning_data tuning_data;
  988. int err = -ENOSYS;
  989. if (opcode == MMC_SEND_TUNING_BLOCK_HS200) {
  990. if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
  991. tuning_data.blk_pattern = tuning_blk_pattern_8bit;
  992. tuning_data.blksz = sizeof(tuning_blk_pattern_8bit);
  993. } else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
  994. tuning_data.blk_pattern = tuning_blk_pattern_4bit;
  995. tuning_data.blksz = sizeof(tuning_blk_pattern_4bit);
  996. } else {
  997. return -EINVAL;
  998. }
  999. } else if (opcode == MMC_SEND_TUNING_BLOCK) {
  1000. tuning_data.blk_pattern = tuning_blk_pattern_4bit;
  1001. tuning_data.blksz = sizeof(tuning_blk_pattern_4bit);
  1002. } else {
  1003. dev_err(host->dev,
  1004. "Undefined command(%d) for tuning\n", opcode);
  1005. return -EINVAL;
  1006. }
  1007. if (drv_data && drv_data->execute_tuning)
  1008. err = drv_data->execute_tuning(slot, opcode, &tuning_data);
  1009. return err;
  1010. }
  1011. static const struct mmc_host_ops dw_mci_ops = {
  1012. .request = dw_mci_request,
  1013. .pre_req = dw_mci_pre_req,
  1014. .post_req = dw_mci_post_req,
  1015. .set_ios = dw_mci_set_ios,
  1016. .get_ro = dw_mci_get_ro,
  1017. .get_cd = dw_mci_get_cd,
  1018. .enable_sdio_irq = dw_mci_enable_sdio_irq,
  1019. .execute_tuning = dw_mci_execute_tuning,
  1020. .card_busy = dw_mci_card_busy,
  1021. .start_signal_voltage_switch = dw_mci_switch_voltage,
  1022. };
  1023. static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
  1024. __releases(&host->lock)
  1025. __acquires(&host->lock)
  1026. {
  1027. struct dw_mci_slot *slot;
  1028. struct mmc_host *prev_mmc = host->cur_slot->mmc;
  1029. WARN_ON(host->cmd || host->data);
  1030. host->cur_slot->mrq = NULL;
  1031. host->mrq = NULL;
  1032. if (!list_empty(&host->queue)) {
  1033. slot = list_entry(host->queue.next,
  1034. struct dw_mci_slot, queue_node);
  1035. list_del(&slot->queue_node);
  1036. dev_vdbg(host->dev, "list not empty: %s is next\n",
  1037. mmc_hostname(slot->mmc));
  1038. host->state = STATE_SENDING_CMD;
  1039. dw_mci_start_request(host, slot);
  1040. } else {
  1041. dev_vdbg(host->dev, "list empty\n");
  1042. if (host->state == STATE_SENDING_CMD11)
  1043. host->state = STATE_WAITING_CMD11_DONE;
  1044. else
  1045. host->state = STATE_IDLE;
  1046. }
  1047. spin_unlock(&host->lock);
  1048. mmc_request_done(prev_mmc, mrq);
  1049. spin_lock(&host->lock);
  1050. }
  1051. static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
  1052. {
  1053. u32 status = host->cmd_status;
  1054. host->cmd_status = 0;
  1055. /* Read the response from the card (up to 16 bytes) */
  1056. if (cmd->flags & MMC_RSP_PRESENT) {
  1057. if (cmd->flags & MMC_RSP_136) {
  1058. cmd->resp[3] = mci_readl(host, RESP0);
  1059. cmd->resp[2] = mci_readl(host, RESP1);
  1060. cmd->resp[1] = mci_readl(host, RESP2);
  1061. cmd->resp[0] = mci_readl(host, RESP3);
  1062. } else {
  1063. cmd->resp[0] = mci_readl(host, RESP0);
  1064. cmd->resp[1] = 0;
  1065. cmd->resp[2] = 0;
  1066. cmd->resp[3] = 0;
  1067. }
  1068. }
  1069. if (status & SDMMC_INT_RTO)
  1070. cmd->error = -ETIMEDOUT;
  1071. else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
  1072. cmd->error = -EILSEQ;
  1073. else if (status & SDMMC_INT_RESP_ERR)
  1074. cmd->error = -EIO;
  1075. else
  1076. cmd->error = 0;
  1077. if (cmd->error) {
  1078. /* newer ip versions need a delay between retries */
  1079. if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
  1080. mdelay(20);
  1081. }
  1082. return cmd->error;
  1083. }
  1084. static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
  1085. {
  1086. u32 status = host->data_status;
  1087. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  1088. if (status & SDMMC_INT_DRTO) {
  1089. data->error = -ETIMEDOUT;
  1090. } else if (status & SDMMC_INT_DCRC) {
  1091. data->error = -EILSEQ;
  1092. } else if (status & SDMMC_INT_EBE) {
  1093. if (host->dir_status ==
  1094. DW_MCI_SEND_STATUS) {
  1095. /*
  1096. * No data CRC status was returned.
  1097. * The number of bytes transferred
  1098. * will be exaggerated in PIO mode.
  1099. */
  1100. data->bytes_xfered = 0;
  1101. data->error = -ETIMEDOUT;
  1102. } else if (host->dir_status ==
  1103. DW_MCI_RECV_STATUS) {
  1104. data->error = -EIO;
  1105. }
  1106. } else {
  1107. /* SDMMC_INT_SBE is included */
  1108. data->error = -EIO;
  1109. }
  1110. dev_dbg(host->dev, "data error, status 0x%08x\n", status);
  1111. /*
  1112. * After an error, there may be data lingering
  1113. * in the FIFO
  1114. */
  1115. dw_mci_reset(host);
  1116. } else {
  1117. data->bytes_xfered = data->blocks * data->blksz;
  1118. data->error = 0;
  1119. }
  1120. return data->error;
  1121. }
  1122. static void dw_mci_tasklet_func(unsigned long priv)
  1123. {
  1124. struct dw_mci *host = (struct dw_mci *)priv;
  1125. struct mmc_data *data;
  1126. struct mmc_command *cmd;
  1127. struct mmc_request *mrq;
  1128. enum dw_mci_state state;
  1129. enum dw_mci_state prev_state;
  1130. unsigned int err;
  1131. spin_lock(&host->lock);
  1132. state = host->state;
  1133. data = host->data;
  1134. mrq = host->mrq;
  1135. do {
  1136. prev_state = state;
  1137. switch (state) {
  1138. case STATE_IDLE:
  1139. case STATE_WAITING_CMD11_DONE:
  1140. break;
  1141. case STATE_SENDING_CMD11:
  1142. case STATE_SENDING_CMD:
  1143. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  1144. &host->pending_events))
  1145. break;
  1146. cmd = host->cmd;
  1147. host->cmd = NULL;
  1148. set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
  1149. err = dw_mci_command_complete(host, cmd);
  1150. if (cmd == mrq->sbc && !err) {
  1151. prev_state = state = STATE_SENDING_CMD;
  1152. __dw_mci_start_request(host, host->cur_slot,
  1153. mrq->cmd);
  1154. goto unlock;
  1155. }
  1156. if (cmd->data && err) {
  1157. dw_mci_stop_dma(host);
  1158. send_stop_abort(host, data);
  1159. state = STATE_SENDING_STOP;
  1160. break;
  1161. }
  1162. if (!cmd->data || err) {
  1163. dw_mci_request_end(host, mrq);
  1164. goto unlock;
  1165. }
  1166. prev_state = state = STATE_SENDING_DATA;
  1167. /* fall through */
  1168. case STATE_SENDING_DATA:
  1169. /*
  1170. * We could get a data error and never a transfer
  1171. * complete so we'd better check for it here.
  1172. *
  1173. * Note that we don't really care if we also got a
  1174. * transfer complete; stopping the DMA and sending an
  1175. * abort won't hurt.
  1176. */
  1177. if (test_and_clear_bit(EVENT_DATA_ERROR,
  1178. &host->pending_events)) {
  1179. dw_mci_stop_dma(host);
  1180. send_stop_abort(host, data);
  1181. state = STATE_DATA_ERROR;
  1182. break;
  1183. }
  1184. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  1185. &host->pending_events))
  1186. break;
  1187. set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
  1188. /*
  1189. * Handle an EVENT_DATA_ERROR that might have shown up
  1190. * before the transfer completed. This might not have
  1191. * been caught by the check above because the interrupt
  1192. * could have gone off between the previous check and
  1193. * the check for transfer complete.
  1194. *
  1195. * Technically this ought not be needed assuming we
  1196. * get a DATA_COMPLETE eventually (we'll notice the
  1197. * error and end the request), but it shouldn't hurt.
  1198. *
  1199. * This has the advantage of sending the stop command.
  1200. */
  1201. if (test_and_clear_bit(EVENT_DATA_ERROR,
  1202. &host->pending_events)) {
  1203. dw_mci_stop_dma(host);
  1204. send_stop_abort(host, data);
  1205. state = STATE_DATA_ERROR;
  1206. break;
  1207. }
  1208. prev_state = state = STATE_DATA_BUSY;
  1209. /* fall through */
  1210. case STATE_DATA_BUSY:
  1211. if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
  1212. &host->pending_events))
  1213. break;
  1214. host->data = NULL;
  1215. set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
  1216. err = dw_mci_data_complete(host, data);
  1217. if (!err) {
  1218. if (!data->stop || mrq->sbc) {
  1219. if (mrq->sbc && data->stop)
  1220. data->stop->error = 0;
  1221. dw_mci_request_end(host, mrq);
  1222. goto unlock;
  1223. }
  1224. /* stop command for open-ended transfer*/
  1225. if (data->stop)
  1226. send_stop_abort(host, data);
  1227. } else {
  1228. /*
  1229. * If we don't have a command complete now we'll
  1230. * never get one since we just reset everything;
  1231. * better end the request.
  1232. *
  1233. * If we do have a command complete we'll fall
  1234. * through to the SENDING_STOP command and
  1235. * everything will be peachy keen.
  1236. */
  1237. if (!test_bit(EVENT_CMD_COMPLETE,
  1238. &host->pending_events)) {
  1239. host->cmd = NULL;
  1240. dw_mci_request_end(host, mrq);
  1241. goto unlock;
  1242. }
  1243. }
  1244. /*
  1245. * If err has non-zero,
  1246. * stop-abort command has been already issued.
  1247. */
  1248. prev_state = state = STATE_SENDING_STOP;
  1249. /* fall through */
  1250. case STATE_SENDING_STOP:
  1251. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  1252. &host->pending_events))
  1253. break;
  1254. /* CMD error in data command */
  1255. if (mrq->cmd->error && mrq->data)
  1256. dw_mci_reset(host);
  1257. host->cmd = NULL;
  1258. host->data = NULL;
  1259. if (mrq->stop)
  1260. dw_mci_command_complete(host, mrq->stop);
  1261. else
  1262. host->cmd_status = 0;
  1263. dw_mci_request_end(host, mrq);
  1264. goto unlock;
  1265. case STATE_DATA_ERROR:
  1266. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  1267. &host->pending_events))
  1268. break;
  1269. state = STATE_DATA_BUSY;
  1270. break;
  1271. }
  1272. } while (state != prev_state);
  1273. host->state = state;
  1274. unlock:
  1275. spin_unlock(&host->lock);
  1276. }
  1277. /* push final bytes to part_buf, only use during push */
  1278. static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1279. {
  1280. memcpy((void *)&host->part_buf, buf, cnt);
  1281. host->part_buf_count = cnt;
  1282. }
  1283. /* append bytes to part_buf, only use during push */
  1284. static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1285. {
  1286. cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
  1287. memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
  1288. host->part_buf_count += cnt;
  1289. return cnt;
  1290. }
  1291. /* pull first bytes from part_buf, only use during pull */
  1292. static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1293. {
  1294. cnt = min(cnt, (int)host->part_buf_count);
  1295. if (cnt) {
  1296. memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
  1297. cnt);
  1298. host->part_buf_count -= cnt;
  1299. host->part_buf_start += cnt;
  1300. }
  1301. return cnt;
  1302. }
  1303. /* pull final bytes from the part_buf, assuming it's just been filled */
  1304. static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
  1305. {
  1306. memcpy(buf, &host->part_buf, cnt);
  1307. host->part_buf_start = cnt;
  1308. host->part_buf_count = (1 << host->data_shift) - cnt;
  1309. }
  1310. static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
  1311. {
  1312. struct mmc_data *data = host->data;
  1313. int init_cnt = cnt;
  1314. /* try and push anything in the part_buf */
  1315. if (unlikely(host->part_buf_count)) {
  1316. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1317. buf += len;
  1318. cnt -= len;
  1319. if (host->part_buf_count == 2) {
  1320. mci_writew(host, DATA(host->data_offset),
  1321. host->part_buf16);
  1322. host->part_buf_count = 0;
  1323. }
  1324. }
  1325. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1326. if (unlikely((unsigned long)buf & 0x1)) {
  1327. while (cnt >= 2) {
  1328. u16 aligned_buf[64];
  1329. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1330. int items = len >> 1;
  1331. int i;
  1332. /* memcpy from input buffer into aligned buffer */
  1333. memcpy(aligned_buf, buf, len);
  1334. buf += len;
  1335. cnt -= len;
  1336. /* push data from aligned buffer into fifo */
  1337. for (i = 0; i < items; ++i)
  1338. mci_writew(host, DATA(host->data_offset),
  1339. aligned_buf[i]);
  1340. }
  1341. } else
  1342. #endif
  1343. {
  1344. u16 *pdata = buf;
  1345. for (; cnt >= 2; cnt -= 2)
  1346. mci_writew(host, DATA(host->data_offset), *pdata++);
  1347. buf = pdata;
  1348. }
  1349. /* put anything remaining in the part_buf */
  1350. if (cnt) {
  1351. dw_mci_set_part_bytes(host, buf, cnt);
  1352. /* Push data if we have reached the expected data length */
  1353. if ((data->bytes_xfered + init_cnt) ==
  1354. (data->blksz * data->blocks))
  1355. mci_writew(host, DATA(host->data_offset),
  1356. host->part_buf16);
  1357. }
  1358. }
  1359. static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
  1360. {
  1361. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1362. if (unlikely((unsigned long)buf & 0x1)) {
  1363. while (cnt >= 2) {
  1364. /* pull data from fifo into aligned buffer */
  1365. u16 aligned_buf[64];
  1366. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1367. int items = len >> 1;
  1368. int i;
  1369. for (i = 0; i < items; ++i)
  1370. aligned_buf[i] = mci_readw(host,
  1371. DATA(host->data_offset));
  1372. /* memcpy from aligned buffer into output buffer */
  1373. memcpy(buf, aligned_buf, len);
  1374. buf += len;
  1375. cnt -= len;
  1376. }
  1377. } else
  1378. #endif
  1379. {
  1380. u16 *pdata = buf;
  1381. for (; cnt >= 2; cnt -= 2)
  1382. *pdata++ = mci_readw(host, DATA(host->data_offset));
  1383. buf = pdata;
  1384. }
  1385. if (cnt) {
  1386. host->part_buf16 = mci_readw(host, DATA(host->data_offset));
  1387. dw_mci_pull_final_bytes(host, buf, cnt);
  1388. }
  1389. }
  1390. static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
  1391. {
  1392. struct mmc_data *data = host->data;
  1393. int init_cnt = cnt;
  1394. /* try and push anything in the part_buf */
  1395. if (unlikely(host->part_buf_count)) {
  1396. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1397. buf += len;
  1398. cnt -= len;
  1399. if (host->part_buf_count == 4) {
  1400. mci_writel(host, DATA(host->data_offset),
  1401. host->part_buf32);
  1402. host->part_buf_count = 0;
  1403. }
  1404. }
  1405. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1406. if (unlikely((unsigned long)buf & 0x3)) {
  1407. while (cnt >= 4) {
  1408. u32 aligned_buf[32];
  1409. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  1410. int items = len >> 2;
  1411. int i;
  1412. /* memcpy from input buffer into aligned buffer */
  1413. memcpy(aligned_buf, buf, len);
  1414. buf += len;
  1415. cnt -= len;
  1416. /* push data from aligned buffer into fifo */
  1417. for (i = 0; i < items; ++i)
  1418. mci_writel(host, DATA(host->data_offset),
  1419. aligned_buf[i]);
  1420. }
  1421. } else
  1422. #endif
  1423. {
  1424. u32 *pdata = buf;
  1425. for (; cnt >= 4; cnt -= 4)
  1426. mci_writel(host, DATA(host->data_offset), *pdata++);
  1427. buf = pdata;
  1428. }
  1429. /* put anything remaining in the part_buf */
  1430. if (cnt) {
  1431. dw_mci_set_part_bytes(host, buf, cnt);
  1432. /* Push data if we have reached the expected data length */
  1433. if ((data->bytes_xfered + init_cnt) ==
  1434. (data->blksz * data->blocks))
  1435. mci_writel(host, DATA(host->data_offset),
  1436. host->part_buf32);
  1437. }
  1438. }
  1439. static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
  1440. {
  1441. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1442. if (unlikely((unsigned long)buf & 0x3)) {
  1443. while (cnt >= 4) {
  1444. /* pull data from fifo into aligned buffer */
  1445. u32 aligned_buf[32];
  1446. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  1447. int items = len >> 2;
  1448. int i;
  1449. for (i = 0; i < items; ++i)
  1450. aligned_buf[i] = mci_readl(host,
  1451. DATA(host->data_offset));
  1452. /* memcpy from aligned buffer into output buffer */
  1453. memcpy(buf, aligned_buf, len);
  1454. buf += len;
  1455. cnt -= len;
  1456. }
  1457. } else
  1458. #endif
  1459. {
  1460. u32 *pdata = buf;
  1461. for (; cnt >= 4; cnt -= 4)
  1462. *pdata++ = mci_readl(host, DATA(host->data_offset));
  1463. buf = pdata;
  1464. }
  1465. if (cnt) {
  1466. host->part_buf32 = mci_readl(host, DATA(host->data_offset));
  1467. dw_mci_pull_final_bytes(host, buf, cnt);
  1468. }
  1469. }
  1470. static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
  1471. {
  1472. struct mmc_data *data = host->data;
  1473. int init_cnt = cnt;
  1474. /* try and push anything in the part_buf */
  1475. if (unlikely(host->part_buf_count)) {
  1476. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1477. buf += len;
  1478. cnt -= len;
  1479. if (host->part_buf_count == 8) {
  1480. mci_writeq(host, DATA(host->data_offset),
  1481. host->part_buf);
  1482. host->part_buf_count = 0;
  1483. }
  1484. }
  1485. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1486. if (unlikely((unsigned long)buf & 0x7)) {
  1487. while (cnt >= 8) {
  1488. u64 aligned_buf[16];
  1489. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1490. int items = len >> 3;
  1491. int i;
  1492. /* memcpy from input buffer into aligned buffer */
  1493. memcpy(aligned_buf, buf, len);
  1494. buf += len;
  1495. cnt -= len;
  1496. /* push data from aligned buffer into fifo */
  1497. for (i = 0; i < items; ++i)
  1498. mci_writeq(host, DATA(host->data_offset),
  1499. aligned_buf[i]);
  1500. }
  1501. } else
  1502. #endif
  1503. {
  1504. u64 *pdata = buf;
  1505. for (; cnt >= 8; cnt -= 8)
  1506. mci_writeq(host, DATA(host->data_offset), *pdata++);
  1507. buf = pdata;
  1508. }
  1509. /* put anything remaining in the part_buf */
  1510. if (cnt) {
  1511. dw_mci_set_part_bytes(host, buf, cnt);
  1512. /* Push data if we have reached the expected data length */
  1513. if ((data->bytes_xfered + init_cnt) ==
  1514. (data->blksz * data->blocks))
  1515. mci_writeq(host, DATA(host->data_offset),
  1516. host->part_buf);
  1517. }
  1518. }
  1519. static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
  1520. {
  1521. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1522. if (unlikely((unsigned long)buf & 0x7)) {
  1523. while (cnt >= 8) {
  1524. /* pull data from fifo into aligned buffer */
  1525. u64 aligned_buf[16];
  1526. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1527. int items = len >> 3;
  1528. int i;
  1529. for (i = 0; i < items; ++i)
  1530. aligned_buf[i] = mci_readq(host,
  1531. DATA(host->data_offset));
  1532. /* memcpy from aligned buffer into output buffer */
  1533. memcpy(buf, aligned_buf, len);
  1534. buf += len;
  1535. cnt -= len;
  1536. }
  1537. } else
  1538. #endif
  1539. {
  1540. u64 *pdata = buf;
  1541. for (; cnt >= 8; cnt -= 8)
  1542. *pdata++ = mci_readq(host, DATA(host->data_offset));
  1543. buf = pdata;
  1544. }
  1545. if (cnt) {
  1546. host->part_buf = mci_readq(host, DATA(host->data_offset));
  1547. dw_mci_pull_final_bytes(host, buf, cnt);
  1548. }
  1549. }
  1550. static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
  1551. {
  1552. int len;
  1553. /* get remaining partial bytes */
  1554. len = dw_mci_pull_part_bytes(host, buf, cnt);
  1555. if (unlikely(len == cnt))
  1556. return;
  1557. buf += len;
  1558. cnt -= len;
  1559. /* get the rest of the data */
  1560. host->pull_data(host, buf, cnt);
  1561. }
  1562. static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
  1563. {
  1564. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  1565. void *buf;
  1566. unsigned int offset;
  1567. struct mmc_data *data = host->data;
  1568. int shift = host->data_shift;
  1569. u32 status;
  1570. unsigned int len;
  1571. unsigned int remain, fcnt;
  1572. do {
  1573. if (!sg_miter_next(sg_miter))
  1574. goto done;
  1575. host->sg = sg_miter->piter.sg;
  1576. buf = sg_miter->addr;
  1577. remain = sg_miter->length;
  1578. offset = 0;
  1579. do {
  1580. fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
  1581. << shift) + host->part_buf_count;
  1582. len = min(remain, fcnt);
  1583. if (!len)
  1584. break;
  1585. dw_mci_pull_data(host, (void *)(buf + offset), len);
  1586. data->bytes_xfered += len;
  1587. offset += len;
  1588. remain -= len;
  1589. } while (remain);
  1590. sg_miter->consumed = offset;
  1591. status = mci_readl(host, MINTSTS);
  1592. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  1593. /* if the RXDR is ready read again */
  1594. } while ((status & SDMMC_INT_RXDR) ||
  1595. (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
  1596. if (!remain) {
  1597. if (!sg_miter_next(sg_miter))
  1598. goto done;
  1599. sg_miter->consumed = 0;
  1600. }
  1601. sg_miter_stop(sg_miter);
  1602. return;
  1603. done:
  1604. sg_miter_stop(sg_miter);
  1605. host->sg = NULL;
  1606. smp_wmb();
  1607. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  1608. }
  1609. static void dw_mci_write_data_pio(struct dw_mci *host)
  1610. {
  1611. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  1612. void *buf;
  1613. unsigned int offset;
  1614. struct mmc_data *data = host->data;
  1615. int shift = host->data_shift;
  1616. u32 status;
  1617. unsigned int len;
  1618. unsigned int fifo_depth = host->fifo_depth;
  1619. unsigned int remain, fcnt;
  1620. do {
  1621. if (!sg_miter_next(sg_miter))
  1622. goto done;
  1623. host->sg = sg_miter->piter.sg;
  1624. buf = sg_miter->addr;
  1625. remain = sg_miter->length;
  1626. offset = 0;
  1627. do {
  1628. fcnt = ((fifo_depth -
  1629. SDMMC_GET_FCNT(mci_readl(host, STATUS)))
  1630. << shift) - host->part_buf_count;
  1631. len = min(remain, fcnt);
  1632. if (!len)
  1633. break;
  1634. host->push_data(host, (void *)(buf + offset), len);
  1635. data->bytes_xfered += len;
  1636. offset += len;
  1637. remain -= len;
  1638. } while (remain);
  1639. sg_miter->consumed = offset;
  1640. status = mci_readl(host, MINTSTS);
  1641. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1642. } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
  1643. if (!remain) {
  1644. if (!sg_miter_next(sg_miter))
  1645. goto done;
  1646. sg_miter->consumed = 0;
  1647. }
  1648. sg_miter_stop(sg_miter);
  1649. return;
  1650. done:
  1651. sg_miter_stop(sg_miter);
  1652. host->sg = NULL;
  1653. smp_wmb();
  1654. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  1655. }
  1656. static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
  1657. {
  1658. if (!host->cmd_status)
  1659. host->cmd_status = status;
  1660. smp_wmb();
  1661. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1662. tasklet_schedule(&host->tasklet);
  1663. }
  1664. static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
  1665. {
  1666. struct dw_mci *host = dev_id;
  1667. u32 pending;
  1668. int i;
  1669. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  1670. /*
  1671. * DTO fix - version 2.10a and below, and only if internal DMA
  1672. * is configured.
  1673. */
  1674. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
  1675. if (!pending &&
  1676. ((mci_readl(host, STATUS) >> 17) & 0x1fff))
  1677. pending |= SDMMC_INT_DATA_OVER;
  1678. }
  1679. if (pending) {
  1680. /* Check volt switch first, since it can look like an error */
  1681. if ((host->state == STATE_SENDING_CMD11) &&
  1682. (pending & SDMMC_INT_VOLT_SWITCH)) {
  1683. mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
  1684. pending &= ~SDMMC_INT_VOLT_SWITCH;
  1685. dw_mci_cmd_interrupt(host, pending);
  1686. }
  1687. if (pending & DW_MCI_CMD_ERROR_FLAGS) {
  1688. mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
  1689. host->cmd_status = pending;
  1690. smp_wmb();
  1691. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1692. }
  1693. if (pending & DW_MCI_DATA_ERROR_FLAGS) {
  1694. /* if there is an error report DATA_ERROR */
  1695. mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
  1696. host->data_status = pending;
  1697. smp_wmb();
  1698. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  1699. tasklet_schedule(&host->tasklet);
  1700. }
  1701. if (pending & SDMMC_INT_DATA_OVER) {
  1702. mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
  1703. if (!host->data_status)
  1704. host->data_status = pending;
  1705. smp_wmb();
  1706. if (host->dir_status == DW_MCI_RECV_STATUS) {
  1707. if (host->sg != NULL)
  1708. dw_mci_read_data_pio(host, true);
  1709. }
  1710. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  1711. tasklet_schedule(&host->tasklet);
  1712. }
  1713. if (pending & SDMMC_INT_RXDR) {
  1714. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  1715. if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
  1716. dw_mci_read_data_pio(host, false);
  1717. }
  1718. if (pending & SDMMC_INT_TXDR) {
  1719. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1720. if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
  1721. dw_mci_write_data_pio(host);
  1722. }
  1723. if (pending & SDMMC_INT_CMD_DONE) {
  1724. mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
  1725. dw_mci_cmd_interrupt(host, pending);
  1726. }
  1727. if (pending & SDMMC_INT_CD) {
  1728. mci_writel(host, RINTSTS, SDMMC_INT_CD);
  1729. queue_work(host->card_workqueue, &host->card_work);
  1730. }
  1731. /* Handle SDIO Interrupts */
  1732. for (i = 0; i < host->num_slots; i++) {
  1733. struct dw_mci_slot *slot = host->slot[i];
  1734. if (pending & SDMMC_INT_SDIO(i)) {
  1735. mci_writel(host, RINTSTS, SDMMC_INT_SDIO(i));
  1736. mmc_signal_sdio_irq(slot->mmc);
  1737. }
  1738. }
  1739. }
  1740. #ifdef CONFIG_MMC_DW_IDMAC
  1741. /* Handle DMA interrupts */
  1742. pending = mci_readl(host, IDSTS);
  1743. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  1744. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI);
  1745. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
  1746. host->dma_ops->complete(host);
  1747. }
  1748. #endif
  1749. return IRQ_HANDLED;
  1750. }
  1751. static void dw_mci_work_routine_card(struct work_struct *work)
  1752. {
  1753. struct dw_mci *host = container_of(work, struct dw_mci, card_work);
  1754. int i;
  1755. for (i = 0; i < host->num_slots; i++) {
  1756. struct dw_mci_slot *slot = host->slot[i];
  1757. struct mmc_host *mmc = slot->mmc;
  1758. struct mmc_request *mrq;
  1759. int present;
  1760. present = dw_mci_get_cd(mmc);
  1761. while (present != slot->last_detect_state) {
  1762. dev_dbg(&slot->mmc->class_dev, "card %s\n",
  1763. present ? "inserted" : "removed");
  1764. spin_lock_bh(&host->lock);
  1765. /* Card change detected */
  1766. slot->last_detect_state = present;
  1767. /* Clean up queue if present */
  1768. mrq = slot->mrq;
  1769. if (mrq) {
  1770. if (mrq == host->mrq) {
  1771. host->data = NULL;
  1772. host->cmd = NULL;
  1773. switch (host->state) {
  1774. case STATE_IDLE:
  1775. case STATE_WAITING_CMD11_DONE:
  1776. break;
  1777. case STATE_SENDING_CMD11:
  1778. case STATE_SENDING_CMD:
  1779. mrq->cmd->error = -ENOMEDIUM;
  1780. if (!mrq->data)
  1781. break;
  1782. /* fall through */
  1783. case STATE_SENDING_DATA:
  1784. mrq->data->error = -ENOMEDIUM;
  1785. dw_mci_stop_dma(host);
  1786. break;
  1787. case STATE_DATA_BUSY:
  1788. case STATE_DATA_ERROR:
  1789. if (mrq->data->error == -EINPROGRESS)
  1790. mrq->data->error = -ENOMEDIUM;
  1791. /* fall through */
  1792. case STATE_SENDING_STOP:
  1793. if (mrq->stop)
  1794. mrq->stop->error = -ENOMEDIUM;
  1795. break;
  1796. }
  1797. dw_mci_request_end(host, mrq);
  1798. } else {
  1799. list_del(&slot->queue_node);
  1800. mrq->cmd->error = -ENOMEDIUM;
  1801. if (mrq->data)
  1802. mrq->data->error = -ENOMEDIUM;
  1803. if (mrq->stop)
  1804. mrq->stop->error = -ENOMEDIUM;
  1805. spin_unlock(&host->lock);
  1806. mmc_request_done(slot->mmc, mrq);
  1807. spin_lock(&host->lock);
  1808. }
  1809. }
  1810. /* Power down slot */
  1811. if (present == 0)
  1812. dw_mci_reset(host);
  1813. spin_unlock_bh(&host->lock);
  1814. present = dw_mci_get_cd(mmc);
  1815. }
  1816. mmc_detect_change(slot->mmc,
  1817. msecs_to_jiffies(host->pdata->detect_delay_ms));
  1818. }
  1819. }
  1820. #ifdef CONFIG_OF
  1821. /* given a slot id, find out the device node representing that slot */
  1822. static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
  1823. {
  1824. struct device_node *np;
  1825. const __be32 *addr;
  1826. int len;
  1827. if (!dev || !dev->of_node)
  1828. return NULL;
  1829. for_each_child_of_node(dev->of_node, np) {
  1830. addr = of_get_property(np, "reg", &len);
  1831. if (!addr || (len < sizeof(int)))
  1832. continue;
  1833. if (be32_to_cpup(addr) == slot)
  1834. return np;
  1835. }
  1836. return NULL;
  1837. }
  1838. static struct dw_mci_of_slot_quirks {
  1839. char *quirk;
  1840. int id;
  1841. } of_slot_quirks[] = {
  1842. {
  1843. .quirk = "disable-wp",
  1844. .id = DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT,
  1845. },
  1846. };
  1847. static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
  1848. {
  1849. struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
  1850. int quirks = 0;
  1851. int idx;
  1852. /* get quirks */
  1853. for (idx = 0; idx < ARRAY_SIZE(of_slot_quirks); idx++)
  1854. if (of_get_property(np, of_slot_quirks[idx].quirk, NULL)) {
  1855. dev_warn(dev, "Slot quirk %s is deprecated\n",
  1856. of_slot_quirks[idx].quirk);
  1857. quirks |= of_slot_quirks[idx].id;
  1858. }
  1859. return quirks;
  1860. }
  1861. #else /* CONFIG_OF */
  1862. static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
  1863. {
  1864. return 0;
  1865. }
  1866. #endif /* CONFIG_OF */
  1867. static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
  1868. {
  1869. struct mmc_host *mmc;
  1870. struct dw_mci_slot *slot;
  1871. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1872. int ctrl_id, ret;
  1873. u32 freq[2];
  1874. mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
  1875. if (!mmc)
  1876. return -ENOMEM;
  1877. slot = mmc_priv(mmc);
  1878. slot->id = id;
  1879. slot->mmc = mmc;
  1880. slot->host = host;
  1881. host->slot[id] = slot;
  1882. slot->quirks = dw_mci_of_get_slot_quirks(host->dev, slot->id);
  1883. mmc->ops = &dw_mci_ops;
  1884. if (of_property_read_u32_array(host->dev->of_node,
  1885. "clock-freq-min-max", freq, 2)) {
  1886. mmc->f_min = DW_MCI_FREQ_MIN;
  1887. mmc->f_max = DW_MCI_FREQ_MAX;
  1888. } else {
  1889. mmc->f_min = freq[0];
  1890. mmc->f_max = freq[1];
  1891. }
  1892. /*if there are external regulators, get them*/
  1893. ret = mmc_regulator_get_supply(mmc);
  1894. if (ret == -EPROBE_DEFER)
  1895. goto err_host_allocated;
  1896. if (!mmc->ocr_avail)
  1897. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1898. if (host->pdata->caps)
  1899. mmc->caps = host->pdata->caps;
  1900. if (host->pdata->pm_caps)
  1901. mmc->pm_caps = host->pdata->pm_caps;
  1902. if (host->dev->of_node) {
  1903. ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
  1904. if (ctrl_id < 0)
  1905. ctrl_id = 0;
  1906. } else {
  1907. ctrl_id = to_platform_device(host->dev)->id;
  1908. }
  1909. if (drv_data && drv_data->caps)
  1910. mmc->caps |= drv_data->caps[ctrl_id];
  1911. if (host->pdata->caps2)
  1912. mmc->caps2 = host->pdata->caps2;
  1913. ret = mmc_of_parse(mmc);
  1914. if (ret)
  1915. goto err_host_allocated;
  1916. if (host->pdata->blk_settings) {
  1917. mmc->max_segs = host->pdata->blk_settings->max_segs;
  1918. mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
  1919. mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
  1920. mmc->max_req_size = host->pdata->blk_settings->max_req_size;
  1921. mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
  1922. } else {
  1923. /* Useful defaults if platform data is unset. */
  1924. #ifdef CONFIG_MMC_DW_IDMAC
  1925. mmc->max_segs = host->ring_size;
  1926. mmc->max_blk_size = 65536;
  1927. mmc->max_blk_count = host->ring_size;
  1928. mmc->max_seg_size = 0x1000;
  1929. mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count;
  1930. #else
  1931. mmc->max_segs = 64;
  1932. mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
  1933. mmc->max_blk_count = 512;
  1934. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1935. mmc->max_seg_size = mmc->max_req_size;
  1936. #endif /* CONFIG_MMC_DW_IDMAC */
  1937. }
  1938. if (dw_mci_get_cd(mmc))
  1939. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1940. else
  1941. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1942. ret = mmc_add_host(mmc);
  1943. if (ret)
  1944. goto err_host_allocated;
  1945. #if defined(CONFIG_DEBUG_FS)
  1946. dw_mci_init_debugfs(slot);
  1947. #endif
  1948. /* Card initially undetected */
  1949. slot->last_detect_state = 0;
  1950. return 0;
  1951. err_host_allocated:
  1952. mmc_free_host(mmc);
  1953. return ret;
  1954. }
  1955. static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
  1956. {
  1957. /* Debugfs stuff is cleaned up by mmc core */
  1958. mmc_remove_host(slot->mmc);
  1959. slot->host->slot[id] = NULL;
  1960. mmc_free_host(slot->mmc);
  1961. }
  1962. static void dw_mci_init_dma(struct dw_mci *host)
  1963. {
  1964. /* Alloc memory for sg translation */
  1965. host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE,
  1966. &host->sg_dma, GFP_KERNEL);
  1967. if (!host->sg_cpu) {
  1968. dev_err(host->dev, "%s: could not alloc DMA memory\n",
  1969. __func__);
  1970. goto no_dma;
  1971. }
  1972. /* Determine which DMA interface to use */
  1973. #ifdef CONFIG_MMC_DW_IDMAC
  1974. host->dma_ops = &dw_mci_idmac_ops;
  1975. dev_info(host->dev, "Using internal DMA controller.\n");
  1976. #endif
  1977. if (!host->dma_ops)
  1978. goto no_dma;
  1979. if (host->dma_ops->init && host->dma_ops->start &&
  1980. host->dma_ops->stop && host->dma_ops->cleanup) {
  1981. if (host->dma_ops->init(host)) {
  1982. dev_err(host->dev, "%s: Unable to initialize "
  1983. "DMA Controller.\n", __func__);
  1984. goto no_dma;
  1985. }
  1986. } else {
  1987. dev_err(host->dev, "DMA initialization not found.\n");
  1988. goto no_dma;
  1989. }
  1990. host->use_dma = 1;
  1991. return;
  1992. no_dma:
  1993. dev_info(host->dev, "Using PIO mode.\n");
  1994. host->use_dma = 0;
  1995. return;
  1996. }
  1997. static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
  1998. {
  1999. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  2000. u32 ctrl;
  2001. ctrl = mci_readl(host, CTRL);
  2002. ctrl |= reset;
  2003. mci_writel(host, CTRL, ctrl);
  2004. /* wait till resets clear */
  2005. do {
  2006. ctrl = mci_readl(host, CTRL);
  2007. if (!(ctrl & reset))
  2008. return true;
  2009. } while (time_before(jiffies, timeout));
  2010. dev_err(host->dev,
  2011. "Timeout resetting block (ctrl reset %#x)\n",
  2012. ctrl & reset);
  2013. return false;
  2014. }
  2015. static bool dw_mci_reset(struct dw_mci *host)
  2016. {
  2017. u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
  2018. bool ret = false;
  2019. /*
  2020. * Reseting generates a block interrupt, hence setting
  2021. * the scatter-gather pointer to NULL.
  2022. */
  2023. if (host->sg) {
  2024. sg_miter_stop(&host->sg_miter);
  2025. host->sg = NULL;
  2026. }
  2027. if (host->use_dma)
  2028. flags |= SDMMC_CTRL_DMA_RESET;
  2029. if (dw_mci_ctrl_reset(host, flags)) {
  2030. /*
  2031. * In all cases we clear the RAWINTS register to clear any
  2032. * interrupts.
  2033. */
  2034. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2035. /* if using dma we wait for dma_req to clear */
  2036. if (host->use_dma) {
  2037. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  2038. u32 status;
  2039. do {
  2040. status = mci_readl(host, STATUS);
  2041. if (!(status & SDMMC_STATUS_DMA_REQ))
  2042. break;
  2043. cpu_relax();
  2044. } while (time_before(jiffies, timeout));
  2045. if (status & SDMMC_STATUS_DMA_REQ) {
  2046. dev_err(host->dev,
  2047. "%s: Timeout waiting for dma_req to "
  2048. "clear during reset\n", __func__);
  2049. goto ciu_out;
  2050. }
  2051. /* when using DMA next we reset the fifo again */
  2052. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
  2053. goto ciu_out;
  2054. }
  2055. } else {
  2056. /* if the controller reset bit did clear, then set clock regs */
  2057. if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
  2058. dev_err(host->dev, "%s: fifo/dma reset bits didn't "
  2059. "clear but ciu was reset, doing clock update\n",
  2060. __func__);
  2061. goto ciu_out;
  2062. }
  2063. }
  2064. #if IS_ENABLED(CONFIG_MMC_DW_IDMAC)
  2065. /* It is also recommended that we reset and reprogram idmac */
  2066. dw_mci_idmac_reset(host);
  2067. #endif
  2068. ret = true;
  2069. ciu_out:
  2070. /* After a CTRL reset we need to have CIU set clock registers */
  2071. mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0);
  2072. return ret;
  2073. }
  2074. #ifdef CONFIG_OF
  2075. static struct dw_mci_of_quirks {
  2076. char *quirk;
  2077. int id;
  2078. } of_quirks[] = {
  2079. {
  2080. .quirk = "broken-cd",
  2081. .id = DW_MCI_QUIRK_BROKEN_CARD_DETECTION,
  2082. }, {
  2083. .quirk = "disable-wp",
  2084. .id = DW_MCI_QUIRK_NO_WRITE_PROTECT,
  2085. },
  2086. };
  2087. static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
  2088. {
  2089. struct dw_mci_board *pdata;
  2090. struct device *dev = host->dev;
  2091. struct device_node *np = dev->of_node;
  2092. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2093. int idx, ret;
  2094. u32 clock_frequency;
  2095. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  2096. if (!pdata) {
  2097. dev_err(dev, "could not allocate memory for pdata\n");
  2098. return ERR_PTR(-ENOMEM);
  2099. }
  2100. /* find out number of slots supported */
  2101. if (of_property_read_u32(dev->of_node, "num-slots",
  2102. &pdata->num_slots)) {
  2103. dev_info(dev, "num-slots property not found, "
  2104. "assuming 1 slot is available\n");
  2105. pdata->num_slots = 1;
  2106. }
  2107. /* get quirks */
  2108. for (idx = 0; idx < ARRAY_SIZE(of_quirks); idx++)
  2109. if (of_get_property(np, of_quirks[idx].quirk, NULL))
  2110. pdata->quirks |= of_quirks[idx].id;
  2111. if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
  2112. dev_info(dev, "fifo-depth property not found, using "
  2113. "value of FIFOTH register as default\n");
  2114. of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
  2115. if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
  2116. pdata->bus_hz = clock_frequency;
  2117. if (drv_data && drv_data->parse_dt) {
  2118. ret = drv_data->parse_dt(host);
  2119. if (ret)
  2120. return ERR_PTR(ret);
  2121. }
  2122. if (of_find_property(np, "supports-highspeed", NULL))
  2123. pdata->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  2124. return pdata;
  2125. }
  2126. #else /* CONFIG_OF */
  2127. static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
  2128. {
  2129. return ERR_PTR(-EINVAL);
  2130. }
  2131. #endif /* CONFIG_OF */
  2132. int dw_mci_probe(struct dw_mci *host)
  2133. {
  2134. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2135. int width, i, ret = 0;
  2136. u32 fifo_size;
  2137. int init_slots = 0;
  2138. if (!host->pdata) {
  2139. host->pdata = dw_mci_parse_dt(host);
  2140. if (IS_ERR(host->pdata)) {
  2141. dev_err(host->dev, "platform data not available\n");
  2142. return -EINVAL;
  2143. }
  2144. }
  2145. if (host->pdata->num_slots > 1) {
  2146. dev_err(host->dev,
  2147. "Platform data must supply num_slots.\n");
  2148. return -ENODEV;
  2149. }
  2150. host->biu_clk = devm_clk_get(host->dev, "biu");
  2151. if (IS_ERR(host->biu_clk)) {
  2152. dev_dbg(host->dev, "biu clock not available\n");
  2153. } else {
  2154. ret = clk_prepare_enable(host->biu_clk);
  2155. if (ret) {
  2156. dev_err(host->dev, "failed to enable biu clock\n");
  2157. return ret;
  2158. }
  2159. }
  2160. host->ciu_clk = devm_clk_get(host->dev, "ciu");
  2161. if (IS_ERR(host->ciu_clk)) {
  2162. dev_dbg(host->dev, "ciu clock not available\n");
  2163. host->bus_hz = host->pdata->bus_hz;
  2164. } else {
  2165. ret = clk_prepare_enable(host->ciu_clk);
  2166. if (ret) {
  2167. dev_err(host->dev, "failed to enable ciu clock\n");
  2168. goto err_clk_biu;
  2169. }
  2170. if (host->pdata->bus_hz) {
  2171. ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
  2172. if (ret)
  2173. dev_warn(host->dev,
  2174. "Unable to set bus rate to %uHz\n",
  2175. host->pdata->bus_hz);
  2176. }
  2177. host->bus_hz = clk_get_rate(host->ciu_clk);
  2178. }
  2179. if (!host->bus_hz) {
  2180. dev_err(host->dev,
  2181. "Platform data must supply bus speed\n");
  2182. ret = -ENODEV;
  2183. goto err_clk_ciu;
  2184. }
  2185. if (drv_data && drv_data->init) {
  2186. ret = drv_data->init(host);
  2187. if (ret) {
  2188. dev_err(host->dev,
  2189. "implementation specific init failed\n");
  2190. goto err_clk_ciu;
  2191. }
  2192. }
  2193. if (drv_data && drv_data->setup_clock) {
  2194. ret = drv_data->setup_clock(host);
  2195. if (ret) {
  2196. dev_err(host->dev,
  2197. "implementation specific clock setup failed\n");
  2198. goto err_clk_ciu;
  2199. }
  2200. }
  2201. host->quirks = host->pdata->quirks;
  2202. spin_lock_init(&host->lock);
  2203. INIT_LIST_HEAD(&host->queue);
  2204. /*
  2205. * Get the host data width - this assumes that HCON has been set with
  2206. * the correct values.
  2207. */
  2208. i = (mci_readl(host, HCON) >> 7) & 0x7;
  2209. if (!i) {
  2210. host->push_data = dw_mci_push_data16;
  2211. host->pull_data = dw_mci_pull_data16;
  2212. width = 16;
  2213. host->data_shift = 1;
  2214. } else if (i == 2) {
  2215. host->push_data = dw_mci_push_data64;
  2216. host->pull_data = dw_mci_pull_data64;
  2217. width = 64;
  2218. host->data_shift = 3;
  2219. } else {
  2220. /* Check for a reserved value, and warn if it is */
  2221. WARN((i != 1),
  2222. "HCON reports a reserved host data width!\n"
  2223. "Defaulting to 32-bit access.\n");
  2224. host->push_data = dw_mci_push_data32;
  2225. host->pull_data = dw_mci_pull_data32;
  2226. width = 32;
  2227. host->data_shift = 2;
  2228. }
  2229. /* Reset all blocks */
  2230. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS))
  2231. return -ENODEV;
  2232. host->dma_ops = host->pdata->dma_ops;
  2233. dw_mci_init_dma(host);
  2234. /* Clear the interrupts for the host controller */
  2235. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2236. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  2237. /* Put in max timeout */
  2238. mci_writel(host, TMOUT, 0xFFFFFFFF);
  2239. /*
  2240. * FIFO threshold settings RxMark = fifo_size / 2 - 1,
  2241. * Tx Mark = fifo_size / 2 DMA Size = 8
  2242. */
  2243. if (!host->pdata->fifo_depth) {
  2244. /*
  2245. * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
  2246. * have been overwritten by the bootloader, just like we're
  2247. * about to do, so if you know the value for your hardware, you
  2248. * should put it in the platform data.
  2249. */
  2250. fifo_size = mci_readl(host, FIFOTH);
  2251. fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
  2252. } else {
  2253. fifo_size = host->pdata->fifo_depth;
  2254. }
  2255. host->fifo_depth = fifo_size;
  2256. host->fifoth_val =
  2257. SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
  2258. mci_writel(host, FIFOTH, host->fifoth_val);
  2259. /* disable clock to CIU */
  2260. mci_writel(host, CLKENA, 0);
  2261. mci_writel(host, CLKSRC, 0);
  2262. /*
  2263. * In 2.40a spec, Data offset is changed.
  2264. * Need to check the version-id and set data-offset for DATA register.
  2265. */
  2266. host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
  2267. dev_info(host->dev, "Version ID is %04x\n", host->verid);
  2268. if (host->verid < DW_MMC_240A)
  2269. host->data_offset = DATA_OFFSET;
  2270. else
  2271. host->data_offset = DATA_240A_OFFSET;
  2272. tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
  2273. host->card_workqueue = alloc_workqueue("dw-mci-card",
  2274. WQ_MEM_RECLAIM, 1);
  2275. if (!host->card_workqueue) {
  2276. ret = -ENOMEM;
  2277. goto err_dmaunmap;
  2278. }
  2279. INIT_WORK(&host->card_work, dw_mci_work_routine_card);
  2280. ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
  2281. host->irq_flags, "dw-mci", host);
  2282. if (ret)
  2283. goto err_workqueue;
  2284. if (host->pdata->num_slots)
  2285. host->num_slots = host->pdata->num_slots;
  2286. else
  2287. host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
  2288. /*
  2289. * Enable interrupts for command done, data over, data empty, card det,
  2290. * receive ready and error such as transmit, receive timeout, crc error
  2291. */
  2292. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2293. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  2294. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  2295. DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
  2296. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
  2297. dev_info(host->dev, "DW MMC controller at irq %d, "
  2298. "%d bit host data width, "
  2299. "%u deep fifo\n",
  2300. host->irq, width, fifo_size);
  2301. /* We need at least one slot to succeed */
  2302. for (i = 0; i < host->num_slots; i++) {
  2303. ret = dw_mci_init_slot(host, i);
  2304. if (ret)
  2305. dev_dbg(host->dev, "slot %d init failed\n", i);
  2306. else
  2307. init_slots++;
  2308. }
  2309. if (init_slots) {
  2310. dev_info(host->dev, "%d slots initialized\n", init_slots);
  2311. } else {
  2312. dev_dbg(host->dev, "attempted to initialize %d slots, "
  2313. "but failed on all\n", host->num_slots);
  2314. goto err_workqueue;
  2315. }
  2316. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
  2317. dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
  2318. return 0;
  2319. err_workqueue:
  2320. destroy_workqueue(host->card_workqueue);
  2321. err_dmaunmap:
  2322. if (host->use_dma && host->dma_ops->exit)
  2323. host->dma_ops->exit(host);
  2324. err_clk_ciu:
  2325. if (!IS_ERR(host->ciu_clk))
  2326. clk_disable_unprepare(host->ciu_clk);
  2327. err_clk_biu:
  2328. if (!IS_ERR(host->biu_clk))
  2329. clk_disable_unprepare(host->biu_clk);
  2330. return ret;
  2331. }
  2332. EXPORT_SYMBOL(dw_mci_probe);
  2333. void dw_mci_remove(struct dw_mci *host)
  2334. {
  2335. int i;
  2336. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2337. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  2338. for (i = 0; i < host->num_slots; i++) {
  2339. dev_dbg(host->dev, "remove slot %d\n", i);
  2340. if (host->slot[i])
  2341. dw_mci_cleanup_slot(host->slot[i], i);
  2342. }
  2343. /* disable clock to CIU */
  2344. mci_writel(host, CLKENA, 0);
  2345. mci_writel(host, CLKSRC, 0);
  2346. destroy_workqueue(host->card_workqueue);
  2347. if (host->use_dma && host->dma_ops->exit)
  2348. host->dma_ops->exit(host);
  2349. if (!IS_ERR(host->ciu_clk))
  2350. clk_disable_unprepare(host->ciu_clk);
  2351. if (!IS_ERR(host->biu_clk))
  2352. clk_disable_unprepare(host->biu_clk);
  2353. }
  2354. EXPORT_SYMBOL(dw_mci_remove);
  2355. #ifdef CONFIG_PM_SLEEP
  2356. /*
  2357. * TODO: we should probably disable the clock to the card in the suspend path.
  2358. */
  2359. int dw_mci_suspend(struct dw_mci *host)
  2360. {
  2361. return 0;
  2362. }
  2363. EXPORT_SYMBOL(dw_mci_suspend);
  2364. int dw_mci_resume(struct dw_mci *host)
  2365. {
  2366. int i, ret;
  2367. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
  2368. ret = -ENODEV;
  2369. return ret;
  2370. }
  2371. if (host->use_dma && host->dma_ops->init)
  2372. host->dma_ops->init(host);
  2373. /*
  2374. * Restore the initial value at FIFOTH register
  2375. * And Invalidate the prev_blksz with zero
  2376. */
  2377. mci_writel(host, FIFOTH, host->fifoth_val);
  2378. host->prev_blksz = 0;
  2379. /* Put in max timeout */
  2380. mci_writel(host, TMOUT, 0xFFFFFFFF);
  2381. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2382. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  2383. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  2384. DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
  2385. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
  2386. for (i = 0; i < host->num_slots; i++) {
  2387. struct dw_mci_slot *slot = host->slot[i];
  2388. if (!slot)
  2389. continue;
  2390. if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) {
  2391. dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
  2392. dw_mci_setup_bus(slot, true);
  2393. }
  2394. }
  2395. return 0;
  2396. }
  2397. EXPORT_SYMBOL(dw_mci_resume);
  2398. #endif /* CONFIG_PM_SLEEP */
  2399. static int __init dw_mci_init(void)
  2400. {
  2401. pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
  2402. return 0;
  2403. }
  2404. static void __exit dw_mci_exit(void)
  2405. {
  2406. }
  2407. module_init(dw_mci_init);
  2408. module_exit(dw_mci_exit);
  2409. MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
  2410. MODULE_AUTHOR("NXP Semiconductor VietNam");
  2411. MODULE_AUTHOR("Imagination Technologies Ltd");
  2412. MODULE_LICENSE("GPL v2");