dw_mmc.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388
  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/iopoll.h>
  22. #include <linux/ioport.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/slab.h>
  28. #include <linux/stat.h>
  29. #include <linux/delay.h>
  30. #include <linux/irq.h>
  31. #include <linux/mmc/card.h>
  32. #include <linux/mmc/host.h>
  33. #include <linux/mmc/mmc.h>
  34. #include <linux/mmc/sd.h>
  35. #include <linux/mmc/sdio.h>
  36. #include <linux/bitops.h>
  37. #include <linux/regulator/consumer.h>
  38. #include <linux/of.h>
  39. #include <linux/of_gpio.h>
  40. #include <linux/mmc/slot-gpio.h>
  41. #include "dw_mmc.h"
  42. /* Common flag combinations */
  43. #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
  44. SDMMC_INT_HTO | SDMMC_INT_SBE | \
  45. SDMMC_INT_EBE | SDMMC_INT_HLE)
  46. #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
  47. SDMMC_INT_RESP_ERR | SDMMC_INT_HLE)
  48. #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
  49. DW_MCI_CMD_ERROR_FLAGS)
  50. #define DW_MCI_SEND_STATUS 1
  51. #define DW_MCI_RECV_STATUS 2
  52. #define DW_MCI_DMA_THRESHOLD 16
  53. #define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */
  54. #define DW_MCI_FREQ_MIN 100000 /* unit: HZ */
  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. #define DESC_RING_BUF_SZ PAGE_SIZE
  60. struct idmac_desc_64addr {
  61. u32 des0; /* Control Descriptor */
  62. #define IDMAC_OWN_CLR64(x) \
  63. !((x) & cpu_to_le32(IDMAC_DES0_OWN))
  64. u32 des1; /* Reserved */
  65. u32 des2; /*Buffer sizes */
  66. #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
  67. ((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \
  68. ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
  69. u32 des3; /* Reserved */
  70. u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/
  71. u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/
  72. u32 des6; /* Lower 32-bits of Next Descriptor Address */
  73. u32 des7; /* Upper 32-bits of Next Descriptor Address */
  74. };
  75. struct idmac_desc {
  76. __le32 des0; /* Control Descriptor */
  77. #define IDMAC_DES0_DIC BIT(1)
  78. #define IDMAC_DES0_LD BIT(2)
  79. #define IDMAC_DES0_FD BIT(3)
  80. #define IDMAC_DES0_CH BIT(4)
  81. #define IDMAC_DES0_ER BIT(5)
  82. #define IDMAC_DES0_CES BIT(30)
  83. #define IDMAC_DES0_OWN BIT(31)
  84. __le32 des1; /* Buffer sizes */
  85. #define IDMAC_SET_BUFFER1_SIZE(d, s) \
  86. ((d)->des1 = ((d)->des1 & cpu_to_le32(0x03ffe000)) | (cpu_to_le32((s) & 0x1fff)))
  87. __le32 des2; /* buffer 1 physical address */
  88. __le32 des3; /* buffer 2 physical address */
  89. };
  90. /* Each descriptor can transfer up to 4KB of data in chained mode */
  91. #define DW_MCI_DESC_DATA_LENGTH 0x1000
  92. #if defined(CONFIG_DEBUG_FS)
  93. static int dw_mci_req_show(struct seq_file *s, void *v)
  94. {
  95. struct dw_mci_slot *slot = s->private;
  96. struct mmc_request *mrq;
  97. struct mmc_command *cmd;
  98. struct mmc_command *stop;
  99. struct mmc_data *data;
  100. /* Make sure we get a consistent snapshot */
  101. spin_lock_bh(&slot->host->lock);
  102. mrq = slot->mrq;
  103. if (mrq) {
  104. cmd = mrq->cmd;
  105. data = mrq->data;
  106. stop = mrq->stop;
  107. if (cmd)
  108. seq_printf(s,
  109. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  110. cmd->opcode, cmd->arg, cmd->flags,
  111. cmd->resp[0], cmd->resp[1], cmd->resp[2],
  112. cmd->resp[2], cmd->error);
  113. if (data)
  114. seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
  115. data->bytes_xfered, data->blocks,
  116. data->blksz, data->flags, data->error);
  117. if (stop)
  118. seq_printf(s,
  119. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  120. stop->opcode, stop->arg, stop->flags,
  121. stop->resp[0], stop->resp[1], stop->resp[2],
  122. stop->resp[2], stop->error);
  123. }
  124. spin_unlock_bh(&slot->host->lock);
  125. return 0;
  126. }
  127. static int dw_mci_req_open(struct inode *inode, struct file *file)
  128. {
  129. return single_open(file, dw_mci_req_show, inode->i_private);
  130. }
  131. static const struct file_operations dw_mci_req_fops = {
  132. .owner = THIS_MODULE,
  133. .open = dw_mci_req_open,
  134. .read = seq_read,
  135. .llseek = seq_lseek,
  136. .release = single_release,
  137. };
  138. static int dw_mci_regs_show(struct seq_file *s, void *v)
  139. {
  140. struct dw_mci *host = s->private;
  141. seq_printf(s, "STATUS:\t0x%08x\n", mci_readl(host, STATUS));
  142. seq_printf(s, "RINTSTS:\t0x%08x\n", mci_readl(host, RINTSTS));
  143. seq_printf(s, "CMD:\t0x%08x\n", mci_readl(host, CMD));
  144. seq_printf(s, "CTRL:\t0x%08x\n", mci_readl(host, CTRL));
  145. seq_printf(s, "INTMASK:\t0x%08x\n", mci_readl(host, INTMASK));
  146. seq_printf(s, "CLKENA:\t0x%08x\n", mci_readl(host, CLKENA));
  147. return 0;
  148. }
  149. static int dw_mci_regs_open(struct inode *inode, struct file *file)
  150. {
  151. return single_open(file, dw_mci_regs_show, inode->i_private);
  152. }
  153. static const struct file_operations dw_mci_regs_fops = {
  154. .owner = THIS_MODULE,
  155. .open = dw_mci_regs_open,
  156. .read = seq_read,
  157. .llseek = seq_lseek,
  158. .release = single_release,
  159. };
  160. static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
  161. {
  162. struct mmc_host *mmc = slot->mmc;
  163. struct dw_mci *host = slot->host;
  164. struct dentry *root;
  165. struct dentry *node;
  166. root = mmc->debugfs_root;
  167. if (!root)
  168. return;
  169. node = debugfs_create_file("regs", S_IRUSR, root, host,
  170. &dw_mci_regs_fops);
  171. if (!node)
  172. goto err;
  173. node = debugfs_create_file("req", S_IRUSR, root, slot,
  174. &dw_mci_req_fops);
  175. if (!node)
  176. goto err;
  177. node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
  178. if (!node)
  179. goto err;
  180. node = debugfs_create_x32("pending_events", S_IRUSR, root,
  181. (u32 *)&host->pending_events);
  182. if (!node)
  183. goto err;
  184. node = debugfs_create_x32("completed_events", S_IRUSR, root,
  185. (u32 *)&host->completed_events);
  186. if (!node)
  187. goto err;
  188. return;
  189. err:
  190. dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
  191. }
  192. #endif /* defined(CONFIG_DEBUG_FS) */
  193. static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
  194. {
  195. u32 ctrl;
  196. ctrl = mci_readl(host, CTRL);
  197. ctrl |= reset;
  198. mci_writel(host, CTRL, ctrl);
  199. /* wait till resets clear */
  200. if (readl_poll_timeout_atomic(host->regs + SDMMC_CTRL, ctrl,
  201. !(ctrl & reset),
  202. 1, 500 * USEC_PER_MSEC)) {
  203. dev_err(host->dev,
  204. "Timeout resetting block (ctrl reset %#x)\n",
  205. ctrl & reset);
  206. return false;
  207. }
  208. return true;
  209. }
  210. static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags)
  211. {
  212. u32 status;
  213. /*
  214. * Databook says that before issuing a new data transfer command
  215. * we need to check to see if the card is busy. Data transfer commands
  216. * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
  217. *
  218. * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
  219. * expected.
  220. */
  221. if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
  222. !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) {
  223. if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
  224. status,
  225. !(status & SDMMC_STATUS_BUSY),
  226. 10, 500 * USEC_PER_MSEC))
  227. dev_err(host->dev, "Busy; trying anyway\n");
  228. }
  229. }
  230. static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
  231. {
  232. struct dw_mci *host = slot->host;
  233. unsigned int cmd_status = 0;
  234. mci_writel(host, CMDARG, arg);
  235. wmb(); /* drain writebuffer */
  236. dw_mci_wait_while_busy(host, cmd);
  237. mci_writel(host, CMD, SDMMC_CMD_START | cmd);
  238. if (readl_poll_timeout_atomic(host->regs + SDMMC_CMD, cmd_status,
  239. !(cmd_status & SDMMC_CMD_START),
  240. 1, 500 * USEC_PER_MSEC))
  241. dev_err(&slot->mmc->class_dev,
  242. "Timeout sending command (cmd %#x arg %#x status %#x)\n",
  243. cmd, arg, cmd_status);
  244. }
  245. static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
  246. {
  247. struct dw_mci_slot *slot = mmc_priv(mmc);
  248. struct dw_mci *host = slot->host;
  249. u32 cmdr;
  250. cmd->error = -EINPROGRESS;
  251. cmdr = cmd->opcode;
  252. if (cmd->opcode == MMC_STOP_TRANSMISSION ||
  253. cmd->opcode == MMC_GO_IDLE_STATE ||
  254. cmd->opcode == MMC_GO_INACTIVE_STATE ||
  255. (cmd->opcode == SD_IO_RW_DIRECT &&
  256. ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
  257. cmdr |= SDMMC_CMD_STOP;
  258. else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
  259. cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
  260. if (cmd->opcode == SD_SWITCH_VOLTAGE) {
  261. u32 clk_en_a;
  262. /* Special bit makes CMD11 not die */
  263. cmdr |= SDMMC_CMD_VOLT_SWITCH;
  264. /* Change state to continue to handle CMD11 weirdness */
  265. WARN_ON(slot->host->state != STATE_SENDING_CMD);
  266. slot->host->state = STATE_SENDING_CMD11;
  267. /*
  268. * We need to disable low power mode (automatic clock stop)
  269. * while doing voltage switch so we don't confuse the card,
  270. * since stopping the clock is a specific part of the UHS
  271. * voltage change dance.
  272. *
  273. * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
  274. * unconditionally turned back on in dw_mci_setup_bus() if it's
  275. * ever called with a non-zero clock. That shouldn't happen
  276. * until the voltage change is all done.
  277. */
  278. clk_en_a = mci_readl(host, CLKENA);
  279. clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
  280. mci_writel(host, CLKENA, clk_en_a);
  281. mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
  282. SDMMC_CMD_PRV_DAT_WAIT, 0);
  283. }
  284. if (cmd->flags & MMC_RSP_PRESENT) {
  285. /* We expect a response, so set this bit */
  286. cmdr |= SDMMC_CMD_RESP_EXP;
  287. if (cmd->flags & MMC_RSP_136)
  288. cmdr |= SDMMC_CMD_RESP_LONG;
  289. }
  290. if (cmd->flags & MMC_RSP_CRC)
  291. cmdr |= SDMMC_CMD_RESP_CRC;
  292. if (cmd->data) {
  293. cmdr |= SDMMC_CMD_DAT_EXP;
  294. if (cmd->data->flags & MMC_DATA_WRITE)
  295. cmdr |= SDMMC_CMD_DAT_WR;
  296. }
  297. if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &slot->flags))
  298. cmdr |= SDMMC_CMD_USE_HOLD_REG;
  299. return cmdr;
  300. }
  301. static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
  302. {
  303. struct mmc_command *stop;
  304. u32 cmdr;
  305. if (!cmd->data)
  306. return 0;
  307. stop = &host->stop_abort;
  308. cmdr = cmd->opcode;
  309. memset(stop, 0, sizeof(struct mmc_command));
  310. if (cmdr == MMC_READ_SINGLE_BLOCK ||
  311. cmdr == MMC_READ_MULTIPLE_BLOCK ||
  312. cmdr == MMC_WRITE_BLOCK ||
  313. cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
  314. cmdr == MMC_SEND_TUNING_BLOCK ||
  315. cmdr == MMC_SEND_TUNING_BLOCK_HS200) {
  316. stop->opcode = MMC_STOP_TRANSMISSION;
  317. stop->arg = 0;
  318. stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
  319. } else if (cmdr == SD_IO_RW_EXTENDED) {
  320. stop->opcode = SD_IO_RW_DIRECT;
  321. stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
  322. ((cmd->arg >> 28) & 0x7);
  323. stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
  324. } else {
  325. return 0;
  326. }
  327. cmdr = stop->opcode | SDMMC_CMD_STOP |
  328. SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
  329. if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &host->cur_slot->flags))
  330. cmdr |= SDMMC_CMD_USE_HOLD_REG;
  331. return cmdr;
  332. }
  333. static void dw_mci_start_command(struct dw_mci *host,
  334. struct mmc_command *cmd, u32 cmd_flags)
  335. {
  336. host->cmd = cmd;
  337. dev_vdbg(host->dev,
  338. "start command: ARGR=0x%08x CMDR=0x%08x\n",
  339. cmd->arg, cmd_flags);
  340. mci_writel(host, CMDARG, cmd->arg);
  341. wmb(); /* drain writebuffer */
  342. dw_mci_wait_while_busy(host, cmd_flags);
  343. mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
  344. }
  345. static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
  346. {
  347. struct mmc_command *stop = &host->stop_abort;
  348. dw_mci_start_command(host, stop, host->stop_cmdr);
  349. }
  350. /* DMA interface functions */
  351. static void dw_mci_stop_dma(struct dw_mci *host)
  352. {
  353. if (host->using_dma) {
  354. host->dma_ops->stop(host);
  355. host->dma_ops->cleanup(host);
  356. }
  357. /* Data transfer was stopped by the interrupt handler */
  358. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  359. }
  360. static void dw_mci_dma_cleanup(struct dw_mci *host)
  361. {
  362. struct mmc_data *data = host->data;
  363. if (data && data->host_cookie == COOKIE_MAPPED) {
  364. dma_unmap_sg(host->dev,
  365. data->sg,
  366. data->sg_len,
  367. mmc_get_dma_dir(data));
  368. data->host_cookie = COOKIE_UNMAPPED;
  369. }
  370. }
  371. static void dw_mci_idmac_reset(struct dw_mci *host)
  372. {
  373. u32 bmod = mci_readl(host, BMOD);
  374. /* Software reset of DMA */
  375. bmod |= SDMMC_IDMAC_SWRESET;
  376. mci_writel(host, BMOD, bmod);
  377. }
  378. static void dw_mci_idmac_stop_dma(struct dw_mci *host)
  379. {
  380. u32 temp;
  381. /* Disable and reset the IDMAC interface */
  382. temp = mci_readl(host, CTRL);
  383. temp &= ~SDMMC_CTRL_USE_IDMAC;
  384. temp |= SDMMC_CTRL_DMA_RESET;
  385. mci_writel(host, CTRL, temp);
  386. /* Stop the IDMAC running */
  387. temp = mci_readl(host, BMOD);
  388. temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
  389. temp |= SDMMC_IDMAC_SWRESET;
  390. mci_writel(host, BMOD, temp);
  391. }
  392. static void dw_mci_dmac_complete_dma(void *arg)
  393. {
  394. struct dw_mci *host = arg;
  395. struct mmc_data *data = host->data;
  396. dev_vdbg(host->dev, "DMA complete\n");
  397. if ((host->use_dma == TRANS_MODE_EDMAC) &&
  398. data && (data->flags & MMC_DATA_READ))
  399. /* Invalidate cache after read */
  400. dma_sync_sg_for_cpu(mmc_dev(host->cur_slot->mmc),
  401. data->sg,
  402. data->sg_len,
  403. DMA_FROM_DEVICE);
  404. host->dma_ops->cleanup(host);
  405. /*
  406. * If the card was removed, data will be NULL. No point in trying to
  407. * send the stop command or waiting for NBUSY in this case.
  408. */
  409. if (data) {
  410. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  411. tasklet_schedule(&host->tasklet);
  412. }
  413. }
  414. static int dw_mci_idmac_init(struct dw_mci *host)
  415. {
  416. int i;
  417. if (host->dma_64bit_address == 1) {
  418. struct idmac_desc_64addr *p;
  419. /* Number of descriptors in the ring buffer */
  420. host->ring_size =
  421. DESC_RING_BUF_SZ / sizeof(struct idmac_desc_64addr);
  422. /* Forward link the descriptor list */
  423. for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
  424. i++, p++) {
  425. p->des6 = (host->sg_dma +
  426. (sizeof(struct idmac_desc_64addr) *
  427. (i + 1))) & 0xffffffff;
  428. p->des7 = (u64)(host->sg_dma +
  429. (sizeof(struct idmac_desc_64addr) *
  430. (i + 1))) >> 32;
  431. /* Initialize reserved and buffer size fields to "0" */
  432. p->des1 = 0;
  433. p->des2 = 0;
  434. p->des3 = 0;
  435. }
  436. /* Set the last descriptor as the end-of-ring descriptor */
  437. p->des6 = host->sg_dma & 0xffffffff;
  438. p->des7 = (u64)host->sg_dma >> 32;
  439. p->des0 = IDMAC_DES0_ER;
  440. } else {
  441. struct idmac_desc *p;
  442. /* Number of descriptors in the ring buffer */
  443. host->ring_size =
  444. DESC_RING_BUF_SZ / sizeof(struct idmac_desc);
  445. /* Forward link the descriptor list */
  446. for (i = 0, p = host->sg_cpu;
  447. i < host->ring_size - 1;
  448. i++, p++) {
  449. p->des3 = cpu_to_le32(host->sg_dma +
  450. (sizeof(struct idmac_desc) * (i + 1)));
  451. p->des1 = 0;
  452. }
  453. /* Set the last descriptor as the end-of-ring descriptor */
  454. p->des3 = cpu_to_le32(host->sg_dma);
  455. p->des0 = cpu_to_le32(IDMAC_DES0_ER);
  456. }
  457. dw_mci_idmac_reset(host);
  458. if (host->dma_64bit_address == 1) {
  459. /* Mask out interrupts - get Tx & Rx complete only */
  460. mci_writel(host, IDSTS64, IDMAC_INT_CLR);
  461. mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
  462. SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
  463. /* Set the descriptor base address */
  464. mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
  465. mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
  466. } else {
  467. /* Mask out interrupts - get Tx & Rx complete only */
  468. mci_writel(host, IDSTS, IDMAC_INT_CLR);
  469. mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
  470. SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
  471. /* Set the descriptor base address */
  472. mci_writel(host, DBADDR, host->sg_dma);
  473. }
  474. return 0;
  475. }
  476. static inline int dw_mci_prepare_desc64(struct dw_mci *host,
  477. struct mmc_data *data,
  478. unsigned int sg_len)
  479. {
  480. unsigned int desc_len;
  481. struct idmac_desc_64addr *desc_first, *desc_last, *desc;
  482. u32 val;
  483. int i;
  484. desc_first = desc_last = desc = host->sg_cpu;
  485. for (i = 0; i < sg_len; i++) {
  486. unsigned int length = sg_dma_len(&data->sg[i]);
  487. u64 mem_addr = sg_dma_address(&data->sg[i]);
  488. for ( ; length ; desc++) {
  489. desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
  490. length : DW_MCI_DESC_DATA_LENGTH;
  491. length -= desc_len;
  492. /*
  493. * Wait for the former clear OWN bit operation
  494. * of IDMAC to make sure that this descriptor
  495. * isn't still owned by IDMAC as IDMAC's write
  496. * ops and CPU's read ops are asynchronous.
  497. */
  498. if (readl_poll_timeout_atomic(&desc->des0, val,
  499. !(val & IDMAC_DES0_OWN),
  500. 10, 100 * USEC_PER_MSEC))
  501. goto err_own_bit;
  502. /*
  503. * Set the OWN bit and disable interrupts
  504. * for this descriptor
  505. */
  506. desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
  507. IDMAC_DES0_CH;
  508. /* Buffer length */
  509. IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len);
  510. /* Physical address to DMA to/from */
  511. desc->des4 = mem_addr & 0xffffffff;
  512. desc->des5 = mem_addr >> 32;
  513. /* Update physical address for the next desc */
  514. mem_addr += desc_len;
  515. /* Save pointer to the last descriptor */
  516. desc_last = desc;
  517. }
  518. }
  519. /* Set first descriptor */
  520. desc_first->des0 |= IDMAC_DES0_FD;
  521. /* Set last descriptor */
  522. desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
  523. desc_last->des0 |= IDMAC_DES0_LD;
  524. return 0;
  525. err_own_bit:
  526. /* restore the descriptor chain as it's polluted */
  527. dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
  528. memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
  529. dw_mci_idmac_init(host);
  530. return -EINVAL;
  531. }
  532. static inline int dw_mci_prepare_desc32(struct dw_mci *host,
  533. struct mmc_data *data,
  534. unsigned int sg_len)
  535. {
  536. unsigned int desc_len;
  537. struct idmac_desc *desc_first, *desc_last, *desc;
  538. u32 val;
  539. int i;
  540. desc_first = desc_last = desc = host->sg_cpu;
  541. for (i = 0; i < sg_len; i++) {
  542. unsigned int length = sg_dma_len(&data->sg[i]);
  543. u32 mem_addr = sg_dma_address(&data->sg[i]);
  544. for ( ; length ; desc++) {
  545. desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
  546. length : DW_MCI_DESC_DATA_LENGTH;
  547. length -= desc_len;
  548. /*
  549. * Wait for the former clear OWN bit operation
  550. * of IDMAC to make sure that this descriptor
  551. * isn't still owned by IDMAC as IDMAC's write
  552. * ops and CPU's read ops are asynchronous.
  553. */
  554. if (readl_poll_timeout_atomic(&desc->des0, val,
  555. IDMAC_OWN_CLR64(val),
  556. 10,
  557. 100 * USEC_PER_MSEC))
  558. goto err_own_bit;
  559. /*
  560. * Set the OWN bit and disable interrupts
  561. * for this descriptor
  562. */
  563. desc->des0 = cpu_to_le32(IDMAC_DES0_OWN |
  564. IDMAC_DES0_DIC |
  565. IDMAC_DES0_CH);
  566. /* Buffer length */
  567. IDMAC_SET_BUFFER1_SIZE(desc, desc_len);
  568. /* Physical address to DMA to/from */
  569. desc->des2 = cpu_to_le32(mem_addr);
  570. /* Update physical address for the next desc */
  571. mem_addr += desc_len;
  572. /* Save pointer to the last descriptor */
  573. desc_last = desc;
  574. }
  575. }
  576. /* Set first descriptor */
  577. desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD);
  578. /* Set last descriptor */
  579. desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH |
  580. IDMAC_DES0_DIC));
  581. desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD);
  582. return 0;
  583. err_own_bit:
  584. /* restore the descriptor chain as it's polluted */
  585. dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
  586. memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
  587. dw_mci_idmac_init(host);
  588. return -EINVAL;
  589. }
  590. static int dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
  591. {
  592. u32 temp;
  593. int ret;
  594. if (host->dma_64bit_address == 1)
  595. ret = dw_mci_prepare_desc64(host, host->data, sg_len);
  596. else
  597. ret = dw_mci_prepare_desc32(host, host->data, sg_len);
  598. if (ret)
  599. goto out;
  600. /* drain writebuffer */
  601. wmb();
  602. /* Make sure to reset DMA in case we did PIO before this */
  603. dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
  604. dw_mci_idmac_reset(host);
  605. /* Select IDMAC interface */
  606. temp = mci_readl(host, CTRL);
  607. temp |= SDMMC_CTRL_USE_IDMAC;
  608. mci_writel(host, CTRL, temp);
  609. /* drain writebuffer */
  610. wmb();
  611. /* Enable the IDMAC */
  612. temp = mci_readl(host, BMOD);
  613. temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
  614. mci_writel(host, BMOD, temp);
  615. /* Start it running */
  616. mci_writel(host, PLDMND, 1);
  617. out:
  618. return ret;
  619. }
  620. static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
  621. .init = dw_mci_idmac_init,
  622. .start = dw_mci_idmac_start_dma,
  623. .stop = dw_mci_idmac_stop_dma,
  624. .complete = dw_mci_dmac_complete_dma,
  625. .cleanup = dw_mci_dma_cleanup,
  626. };
  627. static void dw_mci_edmac_stop_dma(struct dw_mci *host)
  628. {
  629. dmaengine_terminate_async(host->dms->ch);
  630. }
  631. static int dw_mci_edmac_start_dma(struct dw_mci *host,
  632. unsigned int sg_len)
  633. {
  634. struct dma_slave_config cfg;
  635. struct dma_async_tx_descriptor *desc = NULL;
  636. struct scatterlist *sgl = host->data->sg;
  637. const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
  638. u32 sg_elems = host->data->sg_len;
  639. u32 fifoth_val;
  640. u32 fifo_offset = host->fifo_reg - host->regs;
  641. int ret = 0;
  642. /* Set external dma config: burst size, burst width */
  643. cfg.dst_addr = host->phy_regs + fifo_offset;
  644. cfg.src_addr = cfg.dst_addr;
  645. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  646. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  647. /* Match burst msize with external dma config */
  648. fifoth_val = mci_readl(host, FIFOTH);
  649. cfg.dst_maxburst = mszs[(fifoth_val >> 28) & 0x7];
  650. cfg.src_maxburst = cfg.dst_maxburst;
  651. if (host->data->flags & MMC_DATA_WRITE)
  652. cfg.direction = DMA_MEM_TO_DEV;
  653. else
  654. cfg.direction = DMA_DEV_TO_MEM;
  655. ret = dmaengine_slave_config(host->dms->ch, &cfg);
  656. if (ret) {
  657. dev_err(host->dev, "Failed to config edmac.\n");
  658. return -EBUSY;
  659. }
  660. desc = dmaengine_prep_slave_sg(host->dms->ch, sgl,
  661. sg_len, cfg.direction,
  662. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  663. if (!desc) {
  664. dev_err(host->dev, "Can't prepare slave sg.\n");
  665. return -EBUSY;
  666. }
  667. /* Set dw_mci_dmac_complete_dma as callback */
  668. desc->callback = dw_mci_dmac_complete_dma;
  669. desc->callback_param = (void *)host;
  670. dmaengine_submit(desc);
  671. /* Flush cache before write */
  672. if (host->data->flags & MMC_DATA_WRITE)
  673. dma_sync_sg_for_device(mmc_dev(host->cur_slot->mmc), sgl,
  674. sg_elems, DMA_TO_DEVICE);
  675. dma_async_issue_pending(host->dms->ch);
  676. return 0;
  677. }
  678. static int dw_mci_edmac_init(struct dw_mci *host)
  679. {
  680. /* Request external dma channel */
  681. host->dms = kzalloc(sizeof(struct dw_mci_dma_slave), GFP_KERNEL);
  682. if (!host->dms)
  683. return -ENOMEM;
  684. host->dms->ch = dma_request_slave_channel(host->dev, "rx-tx");
  685. if (!host->dms->ch) {
  686. dev_err(host->dev, "Failed to get external DMA channel.\n");
  687. kfree(host->dms);
  688. host->dms = NULL;
  689. return -ENXIO;
  690. }
  691. return 0;
  692. }
  693. static void dw_mci_edmac_exit(struct dw_mci *host)
  694. {
  695. if (host->dms) {
  696. if (host->dms->ch) {
  697. dma_release_channel(host->dms->ch);
  698. host->dms->ch = NULL;
  699. }
  700. kfree(host->dms);
  701. host->dms = NULL;
  702. }
  703. }
  704. static const struct dw_mci_dma_ops dw_mci_edmac_ops = {
  705. .init = dw_mci_edmac_init,
  706. .exit = dw_mci_edmac_exit,
  707. .start = dw_mci_edmac_start_dma,
  708. .stop = dw_mci_edmac_stop_dma,
  709. .complete = dw_mci_dmac_complete_dma,
  710. .cleanup = dw_mci_dma_cleanup,
  711. };
  712. static int dw_mci_pre_dma_transfer(struct dw_mci *host,
  713. struct mmc_data *data,
  714. int cookie)
  715. {
  716. struct scatterlist *sg;
  717. unsigned int i, sg_len;
  718. if (data->host_cookie == COOKIE_PRE_MAPPED)
  719. return data->sg_len;
  720. /*
  721. * We don't do DMA on "complex" transfers, i.e. with
  722. * non-word-aligned buffers or lengths. Also, we don't bother
  723. * with all the DMA setup overhead for short transfers.
  724. */
  725. if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
  726. return -EINVAL;
  727. if (data->blksz & 3)
  728. return -EINVAL;
  729. for_each_sg(data->sg, sg, data->sg_len, i) {
  730. if (sg->offset & 3 || sg->length & 3)
  731. return -EINVAL;
  732. }
  733. sg_len = dma_map_sg(host->dev,
  734. data->sg,
  735. data->sg_len,
  736. mmc_get_dma_dir(data));
  737. if (sg_len == 0)
  738. return -EINVAL;
  739. data->host_cookie = cookie;
  740. return sg_len;
  741. }
  742. static void dw_mci_pre_req(struct mmc_host *mmc,
  743. struct mmc_request *mrq)
  744. {
  745. struct dw_mci_slot *slot = mmc_priv(mmc);
  746. struct mmc_data *data = mrq->data;
  747. if (!slot->host->use_dma || !data)
  748. return;
  749. /* This data might be unmapped at this time */
  750. data->host_cookie = COOKIE_UNMAPPED;
  751. if (dw_mci_pre_dma_transfer(slot->host, mrq->data,
  752. COOKIE_PRE_MAPPED) < 0)
  753. data->host_cookie = COOKIE_UNMAPPED;
  754. }
  755. static void dw_mci_post_req(struct mmc_host *mmc,
  756. struct mmc_request *mrq,
  757. int err)
  758. {
  759. struct dw_mci_slot *slot = mmc_priv(mmc);
  760. struct mmc_data *data = mrq->data;
  761. if (!slot->host->use_dma || !data)
  762. return;
  763. if (data->host_cookie != COOKIE_UNMAPPED)
  764. dma_unmap_sg(slot->host->dev,
  765. data->sg,
  766. data->sg_len,
  767. mmc_get_dma_dir(data));
  768. data->host_cookie = COOKIE_UNMAPPED;
  769. }
  770. static int dw_mci_get_cd(struct mmc_host *mmc)
  771. {
  772. int present;
  773. struct dw_mci_slot *slot = mmc_priv(mmc);
  774. struct dw_mci *host = slot->host;
  775. int gpio_cd = mmc_gpio_get_cd(mmc);
  776. /* Use platform get_cd function, else try onboard card detect */
  777. if (((mmc->caps & MMC_CAP_NEEDS_POLL)
  778. || !mmc_card_is_removable(mmc))) {
  779. present = 1;
  780. if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
  781. if (mmc->caps & MMC_CAP_NEEDS_POLL) {
  782. dev_info(&mmc->class_dev,
  783. "card is polling.\n");
  784. } else {
  785. dev_info(&mmc->class_dev,
  786. "card is non-removable.\n");
  787. }
  788. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  789. }
  790. return present;
  791. } else if (gpio_cd >= 0)
  792. present = gpio_cd;
  793. else
  794. present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
  795. == 0 ? 1 : 0;
  796. spin_lock_bh(&host->lock);
  797. if (present && !test_and_set_bit(DW_MMC_CARD_PRESENT, &slot->flags))
  798. dev_dbg(&mmc->class_dev, "card is present\n");
  799. else if (!present &&
  800. !test_and_clear_bit(DW_MMC_CARD_PRESENT, &slot->flags))
  801. dev_dbg(&mmc->class_dev, "card is not present\n");
  802. spin_unlock_bh(&host->lock);
  803. return present;
  804. }
  805. static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
  806. {
  807. unsigned int blksz = data->blksz;
  808. const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
  809. u32 fifo_width = 1 << host->data_shift;
  810. u32 blksz_depth = blksz / fifo_width, fifoth_val;
  811. u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
  812. int idx = ARRAY_SIZE(mszs) - 1;
  813. /* pio should ship this scenario */
  814. if (!host->use_dma)
  815. return;
  816. tx_wmark = (host->fifo_depth) / 2;
  817. tx_wmark_invers = host->fifo_depth - tx_wmark;
  818. /*
  819. * MSIZE is '1',
  820. * if blksz is not a multiple of the FIFO width
  821. */
  822. if (blksz % fifo_width)
  823. goto done;
  824. do {
  825. if (!((blksz_depth % mszs[idx]) ||
  826. (tx_wmark_invers % mszs[idx]))) {
  827. msize = idx;
  828. rx_wmark = mszs[idx] - 1;
  829. break;
  830. }
  831. } while (--idx > 0);
  832. /*
  833. * If idx is '0', it won't be tried
  834. * Thus, initial values are uesed
  835. */
  836. done:
  837. fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
  838. mci_writel(host, FIFOTH, fifoth_val);
  839. }
  840. static void dw_mci_ctrl_thld(struct dw_mci *host, struct mmc_data *data)
  841. {
  842. unsigned int blksz = data->blksz;
  843. u32 blksz_depth, fifo_depth;
  844. u16 thld_size;
  845. u8 enable;
  846. /*
  847. * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
  848. * in the FIFO region, so we really shouldn't access it).
  849. */
  850. if (host->verid < DW_MMC_240A ||
  851. (host->verid < DW_MMC_280A && data->flags & MMC_DATA_WRITE))
  852. return;
  853. /*
  854. * Card write Threshold is introduced since 2.80a
  855. * It's used when HS400 mode is enabled.
  856. */
  857. if (data->flags & MMC_DATA_WRITE &&
  858. !(host->timing != MMC_TIMING_MMC_HS400))
  859. return;
  860. if (data->flags & MMC_DATA_WRITE)
  861. enable = SDMMC_CARD_WR_THR_EN;
  862. else
  863. enable = SDMMC_CARD_RD_THR_EN;
  864. if (host->timing != MMC_TIMING_MMC_HS200 &&
  865. host->timing != MMC_TIMING_UHS_SDR104)
  866. goto disable;
  867. blksz_depth = blksz / (1 << host->data_shift);
  868. fifo_depth = host->fifo_depth;
  869. if (blksz_depth > fifo_depth)
  870. goto disable;
  871. /*
  872. * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
  873. * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
  874. * Currently just choose blksz.
  875. */
  876. thld_size = blksz;
  877. mci_writel(host, CDTHRCTL, SDMMC_SET_THLD(thld_size, enable));
  878. return;
  879. disable:
  880. mci_writel(host, CDTHRCTL, 0);
  881. }
  882. static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
  883. {
  884. unsigned long irqflags;
  885. int sg_len;
  886. u32 temp;
  887. host->using_dma = 0;
  888. /* If we don't have a channel, we can't do DMA */
  889. if (!host->use_dma)
  890. return -ENODEV;
  891. sg_len = dw_mci_pre_dma_transfer(host, data, COOKIE_MAPPED);
  892. if (sg_len < 0) {
  893. host->dma_ops->stop(host);
  894. return sg_len;
  895. }
  896. host->using_dma = 1;
  897. if (host->use_dma == TRANS_MODE_IDMAC)
  898. dev_vdbg(host->dev,
  899. "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
  900. (unsigned long)host->sg_cpu,
  901. (unsigned long)host->sg_dma,
  902. sg_len);
  903. /*
  904. * Decide the MSIZE and RX/TX Watermark.
  905. * If current block size is same with previous size,
  906. * no need to update fifoth.
  907. */
  908. if (host->prev_blksz != data->blksz)
  909. dw_mci_adjust_fifoth(host, data);
  910. /* Enable the DMA interface */
  911. temp = mci_readl(host, CTRL);
  912. temp |= SDMMC_CTRL_DMA_ENABLE;
  913. mci_writel(host, CTRL, temp);
  914. /* Disable RX/TX IRQs, let DMA handle it */
  915. spin_lock_irqsave(&host->irq_lock, irqflags);
  916. temp = mci_readl(host, INTMASK);
  917. temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
  918. mci_writel(host, INTMASK, temp);
  919. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  920. if (host->dma_ops->start(host, sg_len)) {
  921. host->dma_ops->stop(host);
  922. /* We can't do DMA, try PIO for this one */
  923. dev_dbg(host->dev,
  924. "%s: fall back to PIO mode for current transfer\n",
  925. __func__);
  926. return -ENODEV;
  927. }
  928. return 0;
  929. }
  930. static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
  931. {
  932. unsigned long irqflags;
  933. int flags = SG_MITER_ATOMIC;
  934. u32 temp;
  935. data->error = -EINPROGRESS;
  936. WARN_ON(host->data);
  937. host->sg = NULL;
  938. host->data = data;
  939. if (data->flags & MMC_DATA_READ)
  940. host->dir_status = DW_MCI_RECV_STATUS;
  941. else
  942. host->dir_status = DW_MCI_SEND_STATUS;
  943. dw_mci_ctrl_thld(host, data);
  944. if (dw_mci_submit_data_dma(host, data)) {
  945. if (host->data->flags & MMC_DATA_READ)
  946. flags |= SG_MITER_TO_SG;
  947. else
  948. flags |= SG_MITER_FROM_SG;
  949. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  950. host->sg = data->sg;
  951. host->part_buf_start = 0;
  952. host->part_buf_count = 0;
  953. mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
  954. spin_lock_irqsave(&host->irq_lock, irqflags);
  955. temp = mci_readl(host, INTMASK);
  956. temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
  957. mci_writel(host, INTMASK, temp);
  958. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  959. temp = mci_readl(host, CTRL);
  960. temp &= ~SDMMC_CTRL_DMA_ENABLE;
  961. mci_writel(host, CTRL, temp);
  962. /*
  963. * Use the initial fifoth_val for PIO mode. If wm_algined
  964. * is set, we set watermark same as data size.
  965. * If next issued data may be transfered by DMA mode,
  966. * prev_blksz should be invalidated.
  967. */
  968. if (host->wm_aligned)
  969. dw_mci_adjust_fifoth(host, data);
  970. else
  971. mci_writel(host, FIFOTH, host->fifoth_val);
  972. host->prev_blksz = 0;
  973. } else {
  974. /*
  975. * Keep the current block size.
  976. * It will be used to decide whether to update
  977. * fifoth register next time.
  978. */
  979. host->prev_blksz = data->blksz;
  980. }
  981. }
  982. static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
  983. {
  984. struct dw_mci *host = slot->host;
  985. unsigned int clock = slot->clock;
  986. u32 div;
  987. u32 clk_en_a;
  988. u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
  989. /* We must continue to set bit 28 in CMD until the change is complete */
  990. if (host->state == STATE_WAITING_CMD11_DONE)
  991. sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
  992. if (!clock) {
  993. mci_writel(host, CLKENA, 0);
  994. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  995. } else if (clock != host->current_speed || force_clkinit) {
  996. div = host->bus_hz / clock;
  997. if (host->bus_hz % clock && host->bus_hz > clock)
  998. /*
  999. * move the + 1 after the divide to prevent
  1000. * over-clocking the card.
  1001. */
  1002. div += 1;
  1003. div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
  1004. if ((clock != slot->__clk_old &&
  1005. !test_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags)) ||
  1006. force_clkinit) {
  1007. /* Silent the verbose log if calling from PM context */
  1008. if (!force_clkinit)
  1009. dev_info(&slot->mmc->class_dev,
  1010. "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
  1011. slot->id, host->bus_hz, clock,
  1012. div ? ((host->bus_hz / div) >> 1) :
  1013. host->bus_hz, div);
  1014. /*
  1015. * If card is polling, display the message only
  1016. * one time at boot time.
  1017. */
  1018. if (slot->mmc->caps & MMC_CAP_NEEDS_POLL &&
  1019. slot->mmc->f_min == clock)
  1020. set_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags);
  1021. }
  1022. /* disable clock */
  1023. mci_writel(host, CLKENA, 0);
  1024. mci_writel(host, CLKSRC, 0);
  1025. /* inform CIU */
  1026. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  1027. /* set clock to desired speed */
  1028. mci_writel(host, CLKDIV, div);
  1029. /* inform CIU */
  1030. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  1031. /* enable clock; only low power if no SDIO */
  1032. clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
  1033. if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
  1034. clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
  1035. mci_writel(host, CLKENA, clk_en_a);
  1036. /* inform CIU */
  1037. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  1038. /* keep the last clock value that was requested from core */
  1039. slot->__clk_old = clock;
  1040. }
  1041. host->current_speed = clock;
  1042. /* Set the current slot bus width */
  1043. mci_writel(host, CTYPE, (slot->ctype << slot->id));
  1044. }
  1045. static void __dw_mci_start_request(struct dw_mci *host,
  1046. struct dw_mci_slot *slot,
  1047. struct mmc_command *cmd)
  1048. {
  1049. struct mmc_request *mrq;
  1050. struct mmc_data *data;
  1051. u32 cmdflags;
  1052. mrq = slot->mrq;
  1053. host->cur_slot = slot;
  1054. host->mrq = mrq;
  1055. host->pending_events = 0;
  1056. host->completed_events = 0;
  1057. host->cmd_status = 0;
  1058. host->data_status = 0;
  1059. host->dir_status = 0;
  1060. data = cmd->data;
  1061. if (data) {
  1062. mci_writel(host, TMOUT, 0xFFFFFFFF);
  1063. mci_writel(host, BYTCNT, data->blksz*data->blocks);
  1064. mci_writel(host, BLKSIZ, data->blksz);
  1065. }
  1066. cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
  1067. /* this is the first command, send the initialization clock */
  1068. if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
  1069. cmdflags |= SDMMC_CMD_INIT;
  1070. if (data) {
  1071. dw_mci_submit_data(host, data);
  1072. wmb(); /* drain writebuffer */
  1073. }
  1074. dw_mci_start_command(host, cmd, cmdflags);
  1075. if (cmd->opcode == SD_SWITCH_VOLTAGE) {
  1076. unsigned long irqflags;
  1077. /*
  1078. * Databook says to fail after 2ms w/ no response, but evidence
  1079. * shows that sometimes the cmd11 interrupt takes over 130ms.
  1080. * We'll set to 500ms, plus an extra jiffy just in case jiffies
  1081. * is just about to roll over.
  1082. *
  1083. * We do this whole thing under spinlock and only if the
  1084. * command hasn't already completed (indicating the the irq
  1085. * already ran so we don't want the timeout).
  1086. */
  1087. spin_lock_irqsave(&host->irq_lock, irqflags);
  1088. if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
  1089. mod_timer(&host->cmd11_timer,
  1090. jiffies + msecs_to_jiffies(500) + 1);
  1091. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  1092. }
  1093. host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
  1094. }
  1095. static void dw_mci_start_request(struct dw_mci *host,
  1096. struct dw_mci_slot *slot)
  1097. {
  1098. struct mmc_request *mrq = slot->mrq;
  1099. struct mmc_command *cmd;
  1100. cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
  1101. __dw_mci_start_request(host, slot, cmd);
  1102. }
  1103. /* must be called with host->lock held */
  1104. static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
  1105. struct mmc_request *mrq)
  1106. {
  1107. dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
  1108. host->state);
  1109. slot->mrq = mrq;
  1110. if (host->state == STATE_WAITING_CMD11_DONE) {
  1111. dev_warn(&slot->mmc->class_dev,
  1112. "Voltage change didn't complete\n");
  1113. /*
  1114. * this case isn't expected to happen, so we can
  1115. * either crash here or just try to continue on
  1116. * in the closest possible state
  1117. */
  1118. host->state = STATE_IDLE;
  1119. }
  1120. if (host->state == STATE_IDLE) {
  1121. host->state = STATE_SENDING_CMD;
  1122. dw_mci_start_request(host, slot);
  1123. } else {
  1124. list_add_tail(&slot->queue_node, &host->queue);
  1125. }
  1126. }
  1127. static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1128. {
  1129. struct dw_mci_slot *slot = mmc_priv(mmc);
  1130. struct dw_mci *host = slot->host;
  1131. WARN_ON(slot->mrq);
  1132. /*
  1133. * The check for card presence and queueing of the request must be
  1134. * atomic, otherwise the card could be removed in between and the
  1135. * request wouldn't fail until another card was inserted.
  1136. */
  1137. if (!dw_mci_get_cd(mmc)) {
  1138. mrq->cmd->error = -ENOMEDIUM;
  1139. mmc_request_done(mmc, mrq);
  1140. return;
  1141. }
  1142. spin_lock_bh(&host->lock);
  1143. dw_mci_queue_request(host, slot, mrq);
  1144. spin_unlock_bh(&host->lock);
  1145. }
  1146. static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1147. {
  1148. struct dw_mci_slot *slot = mmc_priv(mmc);
  1149. const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
  1150. u32 regs;
  1151. int ret;
  1152. switch (ios->bus_width) {
  1153. case MMC_BUS_WIDTH_4:
  1154. slot->ctype = SDMMC_CTYPE_4BIT;
  1155. break;
  1156. case MMC_BUS_WIDTH_8:
  1157. slot->ctype = SDMMC_CTYPE_8BIT;
  1158. break;
  1159. default:
  1160. /* set default 1 bit mode */
  1161. slot->ctype = SDMMC_CTYPE_1BIT;
  1162. }
  1163. regs = mci_readl(slot->host, UHS_REG);
  1164. /* DDR mode set */
  1165. if (ios->timing == MMC_TIMING_MMC_DDR52 ||
  1166. ios->timing == MMC_TIMING_UHS_DDR50 ||
  1167. ios->timing == MMC_TIMING_MMC_HS400)
  1168. regs |= ((0x1 << slot->id) << 16);
  1169. else
  1170. regs &= ~((0x1 << slot->id) << 16);
  1171. mci_writel(slot->host, UHS_REG, regs);
  1172. slot->host->timing = ios->timing;
  1173. /*
  1174. * Use mirror of ios->clock to prevent race with mmc
  1175. * core ios update when finding the minimum.
  1176. */
  1177. slot->clock = ios->clock;
  1178. if (drv_data && drv_data->set_ios)
  1179. drv_data->set_ios(slot->host, ios);
  1180. switch (ios->power_mode) {
  1181. case MMC_POWER_UP:
  1182. if (!IS_ERR(mmc->supply.vmmc)) {
  1183. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
  1184. ios->vdd);
  1185. if (ret) {
  1186. dev_err(slot->host->dev,
  1187. "failed to enable vmmc regulator\n");
  1188. /*return, if failed turn on vmmc*/
  1189. return;
  1190. }
  1191. }
  1192. set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
  1193. regs = mci_readl(slot->host, PWREN);
  1194. regs |= (1 << slot->id);
  1195. mci_writel(slot->host, PWREN, regs);
  1196. break;
  1197. case MMC_POWER_ON:
  1198. if (!slot->host->vqmmc_enabled) {
  1199. if (!IS_ERR(mmc->supply.vqmmc)) {
  1200. ret = regulator_enable(mmc->supply.vqmmc);
  1201. if (ret < 0)
  1202. dev_err(slot->host->dev,
  1203. "failed to enable vqmmc\n");
  1204. else
  1205. slot->host->vqmmc_enabled = true;
  1206. } else {
  1207. /* Keep track so we don't reset again */
  1208. slot->host->vqmmc_enabled = true;
  1209. }
  1210. /* Reset our state machine after powering on */
  1211. dw_mci_ctrl_reset(slot->host,
  1212. SDMMC_CTRL_ALL_RESET_FLAGS);
  1213. }
  1214. /* Adjust clock / bus width after power is up */
  1215. dw_mci_setup_bus(slot, false);
  1216. break;
  1217. case MMC_POWER_OFF:
  1218. /* Turn clock off before power goes down */
  1219. dw_mci_setup_bus(slot, false);
  1220. if (!IS_ERR(mmc->supply.vmmc))
  1221. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  1222. if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
  1223. regulator_disable(mmc->supply.vqmmc);
  1224. slot->host->vqmmc_enabled = false;
  1225. regs = mci_readl(slot->host, PWREN);
  1226. regs &= ~(1 << slot->id);
  1227. mci_writel(slot->host, PWREN, regs);
  1228. break;
  1229. default:
  1230. break;
  1231. }
  1232. if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
  1233. slot->host->state = STATE_IDLE;
  1234. }
  1235. static int dw_mci_card_busy(struct mmc_host *mmc)
  1236. {
  1237. struct dw_mci_slot *slot = mmc_priv(mmc);
  1238. u32 status;
  1239. /*
  1240. * Check the busy bit which is low when DAT[3:0]
  1241. * (the data lines) are 0000
  1242. */
  1243. status = mci_readl(slot->host, STATUS);
  1244. return !!(status & SDMMC_STATUS_BUSY);
  1245. }
  1246. static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
  1247. {
  1248. struct dw_mci_slot *slot = mmc_priv(mmc);
  1249. struct dw_mci *host = slot->host;
  1250. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1251. u32 uhs;
  1252. u32 v18 = SDMMC_UHS_18V << slot->id;
  1253. int ret;
  1254. if (drv_data && drv_data->switch_voltage)
  1255. return drv_data->switch_voltage(mmc, ios);
  1256. /*
  1257. * Program the voltage. Note that some instances of dw_mmc may use
  1258. * the UHS_REG for this. For other instances (like exynos) the UHS_REG
  1259. * does no harm but you need to set the regulator directly. Try both.
  1260. */
  1261. uhs = mci_readl(host, UHS_REG);
  1262. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
  1263. uhs &= ~v18;
  1264. else
  1265. uhs |= v18;
  1266. if (!IS_ERR(mmc->supply.vqmmc)) {
  1267. ret = mmc_regulator_set_vqmmc(mmc, ios);
  1268. if (ret) {
  1269. dev_dbg(&mmc->class_dev,
  1270. "Regulator set error %d - %s V\n",
  1271. ret, uhs & v18 ? "1.8" : "3.3");
  1272. return ret;
  1273. }
  1274. }
  1275. mci_writel(host, UHS_REG, uhs);
  1276. return 0;
  1277. }
  1278. static int dw_mci_get_ro(struct mmc_host *mmc)
  1279. {
  1280. int read_only;
  1281. struct dw_mci_slot *slot = mmc_priv(mmc);
  1282. int gpio_ro = mmc_gpio_get_ro(mmc);
  1283. /* Use platform get_ro function, else try on board write protect */
  1284. if (gpio_ro >= 0)
  1285. read_only = gpio_ro;
  1286. else
  1287. read_only =
  1288. mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
  1289. dev_dbg(&mmc->class_dev, "card is %s\n",
  1290. read_only ? "read-only" : "read-write");
  1291. return read_only;
  1292. }
  1293. static void dw_mci_hw_reset(struct mmc_host *mmc)
  1294. {
  1295. struct dw_mci_slot *slot = mmc_priv(mmc);
  1296. struct dw_mci *host = slot->host;
  1297. int reset;
  1298. if (host->use_dma == TRANS_MODE_IDMAC)
  1299. dw_mci_idmac_reset(host);
  1300. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET |
  1301. SDMMC_CTRL_FIFO_RESET))
  1302. return;
  1303. /*
  1304. * According to eMMC spec, card reset procedure:
  1305. * tRstW >= 1us: RST_n pulse width
  1306. * tRSCA >= 200us: RST_n to Command time
  1307. * tRSTH >= 1us: RST_n high period
  1308. */
  1309. reset = mci_readl(host, RST_N);
  1310. reset &= ~(SDMMC_RST_HWACTIVE << slot->id);
  1311. mci_writel(host, RST_N, reset);
  1312. usleep_range(1, 2);
  1313. reset |= SDMMC_RST_HWACTIVE << slot->id;
  1314. mci_writel(host, RST_N, reset);
  1315. usleep_range(200, 300);
  1316. }
  1317. static void dw_mci_init_card(struct mmc_host *mmc, struct mmc_card *card)
  1318. {
  1319. struct dw_mci_slot *slot = mmc_priv(mmc);
  1320. struct dw_mci *host = slot->host;
  1321. /*
  1322. * Low power mode will stop the card clock when idle. According to the
  1323. * description of the CLKENA register we should disable low power mode
  1324. * for SDIO cards if we need SDIO interrupts to work.
  1325. */
  1326. if (mmc->caps & MMC_CAP_SDIO_IRQ) {
  1327. const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
  1328. u32 clk_en_a_old;
  1329. u32 clk_en_a;
  1330. clk_en_a_old = mci_readl(host, CLKENA);
  1331. if (card->type == MMC_TYPE_SDIO ||
  1332. card->type == MMC_TYPE_SD_COMBO) {
  1333. if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags)) {
  1334. pm_runtime_get_noresume(mmc->parent);
  1335. set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
  1336. }
  1337. clk_en_a = clk_en_a_old & ~clken_low_pwr;
  1338. } else {
  1339. if (test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags)) {
  1340. pm_runtime_put_noidle(mmc->parent);
  1341. clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
  1342. }
  1343. clk_en_a = clk_en_a_old | clken_low_pwr;
  1344. }
  1345. if (clk_en_a != clk_en_a_old) {
  1346. mci_writel(host, CLKENA, clk_en_a);
  1347. mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
  1348. SDMMC_CMD_PRV_DAT_WAIT, 0);
  1349. }
  1350. }
  1351. }
  1352. static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
  1353. {
  1354. struct dw_mci_slot *slot = mmc_priv(mmc);
  1355. struct dw_mci *host = slot->host;
  1356. unsigned long irqflags;
  1357. u32 int_mask;
  1358. spin_lock_irqsave(&host->irq_lock, irqflags);
  1359. /* Enable/disable Slot Specific SDIO interrupt */
  1360. int_mask = mci_readl(host, INTMASK);
  1361. if (enb)
  1362. int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
  1363. else
  1364. int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
  1365. mci_writel(host, INTMASK, int_mask);
  1366. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  1367. }
  1368. static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
  1369. {
  1370. struct dw_mci_slot *slot = mmc_priv(mmc);
  1371. struct dw_mci *host = slot->host;
  1372. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1373. int err = -EINVAL;
  1374. if (drv_data && drv_data->execute_tuning)
  1375. err = drv_data->execute_tuning(slot, opcode);
  1376. return err;
  1377. }
  1378. static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc,
  1379. struct mmc_ios *ios)
  1380. {
  1381. struct dw_mci_slot *slot = mmc_priv(mmc);
  1382. struct dw_mci *host = slot->host;
  1383. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1384. if (drv_data && drv_data->prepare_hs400_tuning)
  1385. return drv_data->prepare_hs400_tuning(host, ios);
  1386. return 0;
  1387. }
  1388. static bool dw_mci_reset(struct dw_mci *host)
  1389. {
  1390. u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
  1391. bool ret = false;
  1392. u32 status = 0;
  1393. /*
  1394. * Resetting generates a block interrupt, hence setting
  1395. * the scatter-gather pointer to NULL.
  1396. */
  1397. if (host->sg) {
  1398. sg_miter_stop(&host->sg_miter);
  1399. host->sg = NULL;
  1400. }
  1401. if (host->use_dma)
  1402. flags |= SDMMC_CTRL_DMA_RESET;
  1403. if (dw_mci_ctrl_reset(host, flags)) {
  1404. /*
  1405. * In all cases we clear the RAWINTS
  1406. * register to clear any interrupts.
  1407. */
  1408. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1409. if (!host->use_dma) {
  1410. ret = true;
  1411. goto ciu_out;
  1412. }
  1413. /* Wait for dma_req to be cleared */
  1414. if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
  1415. status,
  1416. !(status & SDMMC_STATUS_DMA_REQ),
  1417. 1, 500 * USEC_PER_MSEC)) {
  1418. dev_err(host->dev,
  1419. "%s: Timeout waiting for dma_req to be cleared\n",
  1420. __func__);
  1421. goto ciu_out;
  1422. }
  1423. /* when using DMA next we reset the fifo again */
  1424. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
  1425. goto ciu_out;
  1426. } else {
  1427. /* if the controller reset bit did clear, then set clock regs */
  1428. if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
  1429. dev_err(host->dev,
  1430. "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
  1431. __func__);
  1432. goto ciu_out;
  1433. }
  1434. }
  1435. if (host->use_dma == TRANS_MODE_IDMAC)
  1436. /* It is also recommended that we reset and reprogram idmac */
  1437. dw_mci_idmac_reset(host);
  1438. ret = true;
  1439. ciu_out:
  1440. /* After a CTRL reset we need to have CIU set clock registers */
  1441. mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0);
  1442. return ret;
  1443. }
  1444. static const struct mmc_host_ops dw_mci_ops = {
  1445. .request = dw_mci_request,
  1446. .pre_req = dw_mci_pre_req,
  1447. .post_req = dw_mci_post_req,
  1448. .set_ios = dw_mci_set_ios,
  1449. .get_ro = dw_mci_get_ro,
  1450. .get_cd = dw_mci_get_cd,
  1451. .hw_reset = dw_mci_hw_reset,
  1452. .enable_sdio_irq = dw_mci_enable_sdio_irq,
  1453. .execute_tuning = dw_mci_execute_tuning,
  1454. .card_busy = dw_mci_card_busy,
  1455. .start_signal_voltage_switch = dw_mci_switch_voltage,
  1456. .init_card = dw_mci_init_card,
  1457. .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning,
  1458. };
  1459. static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
  1460. __releases(&host->lock)
  1461. __acquires(&host->lock)
  1462. {
  1463. struct dw_mci_slot *slot;
  1464. struct mmc_host *prev_mmc = host->cur_slot->mmc;
  1465. WARN_ON(host->cmd || host->data);
  1466. host->cur_slot->mrq = NULL;
  1467. host->mrq = NULL;
  1468. if (!list_empty(&host->queue)) {
  1469. slot = list_entry(host->queue.next,
  1470. struct dw_mci_slot, queue_node);
  1471. list_del(&slot->queue_node);
  1472. dev_vdbg(host->dev, "list not empty: %s is next\n",
  1473. mmc_hostname(slot->mmc));
  1474. host->state = STATE_SENDING_CMD;
  1475. dw_mci_start_request(host, slot);
  1476. } else {
  1477. dev_vdbg(host->dev, "list empty\n");
  1478. if (host->state == STATE_SENDING_CMD11)
  1479. host->state = STATE_WAITING_CMD11_DONE;
  1480. else
  1481. host->state = STATE_IDLE;
  1482. }
  1483. spin_unlock(&host->lock);
  1484. mmc_request_done(prev_mmc, mrq);
  1485. spin_lock(&host->lock);
  1486. }
  1487. static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
  1488. {
  1489. u32 status = host->cmd_status;
  1490. host->cmd_status = 0;
  1491. /* Read the response from the card (up to 16 bytes) */
  1492. if (cmd->flags & MMC_RSP_PRESENT) {
  1493. if (cmd->flags & MMC_RSP_136) {
  1494. cmd->resp[3] = mci_readl(host, RESP0);
  1495. cmd->resp[2] = mci_readl(host, RESP1);
  1496. cmd->resp[1] = mci_readl(host, RESP2);
  1497. cmd->resp[0] = mci_readl(host, RESP3);
  1498. } else {
  1499. cmd->resp[0] = mci_readl(host, RESP0);
  1500. cmd->resp[1] = 0;
  1501. cmd->resp[2] = 0;
  1502. cmd->resp[3] = 0;
  1503. }
  1504. }
  1505. if (status & SDMMC_INT_RTO)
  1506. cmd->error = -ETIMEDOUT;
  1507. else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
  1508. cmd->error = -EILSEQ;
  1509. else if (status & SDMMC_INT_RESP_ERR)
  1510. cmd->error = -EIO;
  1511. else
  1512. cmd->error = 0;
  1513. return cmd->error;
  1514. }
  1515. static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
  1516. {
  1517. u32 status = host->data_status;
  1518. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  1519. if (status & SDMMC_INT_DRTO) {
  1520. data->error = -ETIMEDOUT;
  1521. } else if (status & SDMMC_INT_DCRC) {
  1522. data->error = -EILSEQ;
  1523. } else if (status & SDMMC_INT_EBE) {
  1524. if (host->dir_status ==
  1525. DW_MCI_SEND_STATUS) {
  1526. /*
  1527. * No data CRC status was returned.
  1528. * The number of bytes transferred
  1529. * will be exaggerated in PIO mode.
  1530. */
  1531. data->bytes_xfered = 0;
  1532. data->error = -ETIMEDOUT;
  1533. } else if (host->dir_status ==
  1534. DW_MCI_RECV_STATUS) {
  1535. data->error = -EILSEQ;
  1536. }
  1537. } else {
  1538. /* SDMMC_INT_SBE is included */
  1539. data->error = -EILSEQ;
  1540. }
  1541. dev_dbg(host->dev, "data error, status 0x%08x\n", status);
  1542. /*
  1543. * After an error, there may be data lingering
  1544. * in the FIFO
  1545. */
  1546. dw_mci_reset(host);
  1547. } else {
  1548. data->bytes_xfered = data->blocks * data->blksz;
  1549. data->error = 0;
  1550. }
  1551. return data->error;
  1552. }
  1553. static void dw_mci_set_drto(struct dw_mci *host)
  1554. {
  1555. unsigned int drto_clks;
  1556. unsigned int drto_ms;
  1557. drto_clks = mci_readl(host, TMOUT) >> 8;
  1558. drto_ms = DIV_ROUND_UP(drto_clks, host->bus_hz / 1000);
  1559. /* add a bit spare time */
  1560. drto_ms += 10;
  1561. mod_timer(&host->dto_timer, jiffies + msecs_to_jiffies(drto_ms));
  1562. }
  1563. static void dw_mci_tasklet_func(unsigned long priv)
  1564. {
  1565. struct dw_mci *host = (struct dw_mci *)priv;
  1566. struct mmc_data *data;
  1567. struct mmc_command *cmd;
  1568. struct mmc_request *mrq;
  1569. enum dw_mci_state state;
  1570. enum dw_mci_state prev_state;
  1571. unsigned int err;
  1572. spin_lock(&host->lock);
  1573. state = host->state;
  1574. data = host->data;
  1575. mrq = host->mrq;
  1576. do {
  1577. prev_state = state;
  1578. switch (state) {
  1579. case STATE_IDLE:
  1580. case STATE_WAITING_CMD11_DONE:
  1581. break;
  1582. case STATE_SENDING_CMD11:
  1583. case STATE_SENDING_CMD:
  1584. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  1585. &host->pending_events))
  1586. break;
  1587. cmd = host->cmd;
  1588. host->cmd = NULL;
  1589. set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
  1590. err = dw_mci_command_complete(host, cmd);
  1591. if (cmd == mrq->sbc && !err) {
  1592. prev_state = state = STATE_SENDING_CMD;
  1593. __dw_mci_start_request(host, host->cur_slot,
  1594. mrq->cmd);
  1595. goto unlock;
  1596. }
  1597. if (cmd->data && err) {
  1598. /*
  1599. * During UHS tuning sequence, sending the stop
  1600. * command after the response CRC error would
  1601. * throw the system into a confused state
  1602. * causing all future tuning phases to report
  1603. * failure.
  1604. *
  1605. * In such case controller will move into a data
  1606. * transfer state after a response error or
  1607. * response CRC error. Let's let that finish
  1608. * before trying to send a stop, so we'll go to
  1609. * STATE_SENDING_DATA.
  1610. *
  1611. * Although letting the data transfer take place
  1612. * will waste a bit of time (we already know
  1613. * the command was bad), it can't cause any
  1614. * errors since it's possible it would have
  1615. * taken place anyway if this tasklet got
  1616. * delayed. Allowing the transfer to take place
  1617. * avoids races and keeps things simple.
  1618. */
  1619. if ((err != -ETIMEDOUT) &&
  1620. (cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
  1621. state = STATE_SENDING_DATA;
  1622. continue;
  1623. }
  1624. dw_mci_stop_dma(host);
  1625. send_stop_abort(host, data);
  1626. state = STATE_SENDING_STOP;
  1627. break;
  1628. }
  1629. if (!cmd->data || err) {
  1630. dw_mci_request_end(host, mrq);
  1631. goto unlock;
  1632. }
  1633. prev_state = state = STATE_SENDING_DATA;
  1634. /* fall through */
  1635. case STATE_SENDING_DATA:
  1636. /*
  1637. * We could get a data error and never a transfer
  1638. * complete so we'd better check for it here.
  1639. *
  1640. * Note that we don't really care if we also got a
  1641. * transfer complete; stopping the DMA and sending an
  1642. * abort won't hurt.
  1643. */
  1644. if (test_and_clear_bit(EVENT_DATA_ERROR,
  1645. &host->pending_events)) {
  1646. dw_mci_stop_dma(host);
  1647. if (!(host->data_status & (SDMMC_INT_DRTO |
  1648. SDMMC_INT_EBE)))
  1649. send_stop_abort(host, data);
  1650. state = STATE_DATA_ERROR;
  1651. break;
  1652. }
  1653. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  1654. &host->pending_events)) {
  1655. /*
  1656. * If all data-related interrupts don't come
  1657. * within the given time in reading data state.
  1658. */
  1659. if (host->dir_status == DW_MCI_RECV_STATUS)
  1660. dw_mci_set_drto(host);
  1661. break;
  1662. }
  1663. set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
  1664. /*
  1665. * Handle an EVENT_DATA_ERROR that might have shown up
  1666. * before the transfer completed. This might not have
  1667. * been caught by the check above because the interrupt
  1668. * could have gone off between the previous check and
  1669. * the check for transfer complete.
  1670. *
  1671. * Technically this ought not be needed assuming we
  1672. * get a DATA_COMPLETE eventually (we'll notice the
  1673. * error and end the request), but it shouldn't hurt.
  1674. *
  1675. * This has the advantage of sending the stop command.
  1676. */
  1677. if (test_and_clear_bit(EVENT_DATA_ERROR,
  1678. &host->pending_events)) {
  1679. dw_mci_stop_dma(host);
  1680. if (!(host->data_status & (SDMMC_INT_DRTO |
  1681. SDMMC_INT_EBE)))
  1682. send_stop_abort(host, data);
  1683. state = STATE_DATA_ERROR;
  1684. break;
  1685. }
  1686. prev_state = state = STATE_DATA_BUSY;
  1687. /* fall through */
  1688. case STATE_DATA_BUSY:
  1689. if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
  1690. &host->pending_events)) {
  1691. /*
  1692. * If data error interrupt comes but data over
  1693. * interrupt doesn't come within the given time.
  1694. * in reading data state.
  1695. */
  1696. if (host->dir_status == DW_MCI_RECV_STATUS)
  1697. dw_mci_set_drto(host);
  1698. break;
  1699. }
  1700. host->data = NULL;
  1701. set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
  1702. err = dw_mci_data_complete(host, data);
  1703. if (!err) {
  1704. if (!data->stop || mrq->sbc) {
  1705. if (mrq->sbc && data->stop)
  1706. data->stop->error = 0;
  1707. dw_mci_request_end(host, mrq);
  1708. goto unlock;
  1709. }
  1710. /* stop command for open-ended transfer*/
  1711. if (data->stop)
  1712. send_stop_abort(host, data);
  1713. } else {
  1714. /*
  1715. * If we don't have a command complete now we'll
  1716. * never get one since we just reset everything;
  1717. * better end the request.
  1718. *
  1719. * If we do have a command complete we'll fall
  1720. * through to the SENDING_STOP command and
  1721. * everything will be peachy keen.
  1722. */
  1723. if (!test_bit(EVENT_CMD_COMPLETE,
  1724. &host->pending_events)) {
  1725. host->cmd = NULL;
  1726. dw_mci_request_end(host, mrq);
  1727. goto unlock;
  1728. }
  1729. }
  1730. /*
  1731. * If err has non-zero,
  1732. * stop-abort command has been already issued.
  1733. */
  1734. prev_state = state = STATE_SENDING_STOP;
  1735. /* fall through */
  1736. case STATE_SENDING_STOP:
  1737. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  1738. &host->pending_events))
  1739. break;
  1740. /* CMD error in data command */
  1741. if (mrq->cmd->error && mrq->data)
  1742. dw_mci_reset(host);
  1743. host->cmd = NULL;
  1744. host->data = NULL;
  1745. if (!mrq->sbc && mrq->stop)
  1746. dw_mci_command_complete(host, mrq->stop);
  1747. else
  1748. host->cmd_status = 0;
  1749. dw_mci_request_end(host, mrq);
  1750. goto unlock;
  1751. case STATE_DATA_ERROR:
  1752. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  1753. &host->pending_events))
  1754. break;
  1755. state = STATE_DATA_BUSY;
  1756. break;
  1757. }
  1758. } while (state != prev_state);
  1759. host->state = state;
  1760. unlock:
  1761. spin_unlock(&host->lock);
  1762. }
  1763. /* push final bytes to part_buf, only use during push */
  1764. static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1765. {
  1766. memcpy((void *)&host->part_buf, buf, cnt);
  1767. host->part_buf_count = cnt;
  1768. }
  1769. /* append bytes to part_buf, only use during push */
  1770. static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1771. {
  1772. cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
  1773. memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
  1774. host->part_buf_count += cnt;
  1775. return cnt;
  1776. }
  1777. /* pull first bytes from part_buf, only use during pull */
  1778. static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1779. {
  1780. cnt = min_t(int, cnt, host->part_buf_count);
  1781. if (cnt) {
  1782. memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
  1783. cnt);
  1784. host->part_buf_count -= cnt;
  1785. host->part_buf_start += cnt;
  1786. }
  1787. return cnt;
  1788. }
  1789. /* pull final bytes from the part_buf, assuming it's just been filled */
  1790. static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
  1791. {
  1792. memcpy(buf, &host->part_buf, cnt);
  1793. host->part_buf_start = cnt;
  1794. host->part_buf_count = (1 << host->data_shift) - cnt;
  1795. }
  1796. static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
  1797. {
  1798. struct mmc_data *data = host->data;
  1799. int init_cnt = cnt;
  1800. /* try and push anything in the part_buf */
  1801. if (unlikely(host->part_buf_count)) {
  1802. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1803. buf += len;
  1804. cnt -= len;
  1805. if (host->part_buf_count == 2) {
  1806. mci_fifo_writew(host->fifo_reg, host->part_buf16);
  1807. host->part_buf_count = 0;
  1808. }
  1809. }
  1810. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1811. if (unlikely((unsigned long)buf & 0x1)) {
  1812. while (cnt >= 2) {
  1813. u16 aligned_buf[64];
  1814. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1815. int items = len >> 1;
  1816. int i;
  1817. /* memcpy from input buffer into aligned buffer */
  1818. memcpy(aligned_buf, buf, len);
  1819. buf += len;
  1820. cnt -= len;
  1821. /* push data from aligned buffer into fifo */
  1822. for (i = 0; i < items; ++i)
  1823. mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
  1824. }
  1825. } else
  1826. #endif
  1827. {
  1828. u16 *pdata = buf;
  1829. for (; cnt >= 2; cnt -= 2)
  1830. mci_fifo_writew(host->fifo_reg, *pdata++);
  1831. buf = pdata;
  1832. }
  1833. /* put anything remaining in the part_buf */
  1834. if (cnt) {
  1835. dw_mci_set_part_bytes(host, buf, cnt);
  1836. /* Push data if we have reached the expected data length */
  1837. if ((data->bytes_xfered + init_cnt) ==
  1838. (data->blksz * data->blocks))
  1839. mci_fifo_writew(host->fifo_reg, host->part_buf16);
  1840. }
  1841. }
  1842. static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
  1843. {
  1844. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1845. if (unlikely((unsigned long)buf & 0x1)) {
  1846. while (cnt >= 2) {
  1847. /* pull data from fifo into aligned buffer */
  1848. u16 aligned_buf[64];
  1849. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1850. int items = len >> 1;
  1851. int i;
  1852. for (i = 0; i < items; ++i)
  1853. aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
  1854. /* memcpy from aligned buffer into output buffer */
  1855. memcpy(buf, aligned_buf, len);
  1856. buf += len;
  1857. cnt -= len;
  1858. }
  1859. } else
  1860. #endif
  1861. {
  1862. u16 *pdata = buf;
  1863. for (; cnt >= 2; cnt -= 2)
  1864. *pdata++ = mci_fifo_readw(host->fifo_reg);
  1865. buf = pdata;
  1866. }
  1867. if (cnt) {
  1868. host->part_buf16 = mci_fifo_readw(host->fifo_reg);
  1869. dw_mci_pull_final_bytes(host, buf, cnt);
  1870. }
  1871. }
  1872. static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
  1873. {
  1874. struct mmc_data *data = host->data;
  1875. int init_cnt = cnt;
  1876. /* try and push anything in the part_buf */
  1877. if (unlikely(host->part_buf_count)) {
  1878. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1879. buf += len;
  1880. cnt -= len;
  1881. if (host->part_buf_count == 4) {
  1882. mci_fifo_writel(host->fifo_reg, host->part_buf32);
  1883. host->part_buf_count = 0;
  1884. }
  1885. }
  1886. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1887. if (unlikely((unsigned long)buf & 0x3)) {
  1888. while (cnt >= 4) {
  1889. u32 aligned_buf[32];
  1890. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  1891. int items = len >> 2;
  1892. int i;
  1893. /* memcpy from input buffer into aligned buffer */
  1894. memcpy(aligned_buf, buf, len);
  1895. buf += len;
  1896. cnt -= len;
  1897. /* push data from aligned buffer into fifo */
  1898. for (i = 0; i < items; ++i)
  1899. mci_fifo_writel(host->fifo_reg, aligned_buf[i]);
  1900. }
  1901. } else
  1902. #endif
  1903. {
  1904. u32 *pdata = buf;
  1905. for (; cnt >= 4; cnt -= 4)
  1906. mci_fifo_writel(host->fifo_reg, *pdata++);
  1907. buf = pdata;
  1908. }
  1909. /* put anything remaining in the part_buf */
  1910. if (cnt) {
  1911. dw_mci_set_part_bytes(host, buf, cnt);
  1912. /* Push data if we have reached the expected data length */
  1913. if ((data->bytes_xfered + init_cnt) ==
  1914. (data->blksz * data->blocks))
  1915. mci_fifo_writel(host->fifo_reg, host->part_buf32);
  1916. }
  1917. }
  1918. static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
  1919. {
  1920. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1921. if (unlikely((unsigned long)buf & 0x3)) {
  1922. while (cnt >= 4) {
  1923. /* pull data from fifo into aligned buffer */
  1924. u32 aligned_buf[32];
  1925. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  1926. int items = len >> 2;
  1927. int i;
  1928. for (i = 0; i < items; ++i)
  1929. aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
  1930. /* memcpy from aligned buffer into output buffer */
  1931. memcpy(buf, aligned_buf, len);
  1932. buf += len;
  1933. cnt -= len;
  1934. }
  1935. } else
  1936. #endif
  1937. {
  1938. u32 *pdata = buf;
  1939. for (; cnt >= 4; cnt -= 4)
  1940. *pdata++ = mci_fifo_readl(host->fifo_reg);
  1941. buf = pdata;
  1942. }
  1943. if (cnt) {
  1944. host->part_buf32 = mci_fifo_readl(host->fifo_reg);
  1945. dw_mci_pull_final_bytes(host, buf, cnt);
  1946. }
  1947. }
  1948. static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
  1949. {
  1950. struct mmc_data *data = host->data;
  1951. int init_cnt = cnt;
  1952. /* try and push anything in the part_buf */
  1953. if (unlikely(host->part_buf_count)) {
  1954. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1955. buf += len;
  1956. cnt -= len;
  1957. if (host->part_buf_count == 8) {
  1958. mci_fifo_writeq(host->fifo_reg, host->part_buf);
  1959. host->part_buf_count = 0;
  1960. }
  1961. }
  1962. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1963. if (unlikely((unsigned long)buf & 0x7)) {
  1964. while (cnt >= 8) {
  1965. u64 aligned_buf[16];
  1966. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1967. int items = len >> 3;
  1968. int i;
  1969. /* memcpy from input buffer into aligned buffer */
  1970. memcpy(aligned_buf, buf, len);
  1971. buf += len;
  1972. cnt -= len;
  1973. /* push data from aligned buffer into fifo */
  1974. for (i = 0; i < items; ++i)
  1975. mci_fifo_writeq(host->fifo_reg, aligned_buf[i]);
  1976. }
  1977. } else
  1978. #endif
  1979. {
  1980. u64 *pdata = buf;
  1981. for (; cnt >= 8; cnt -= 8)
  1982. mci_fifo_writeq(host->fifo_reg, *pdata++);
  1983. buf = pdata;
  1984. }
  1985. /* put anything remaining in the part_buf */
  1986. if (cnt) {
  1987. dw_mci_set_part_bytes(host, buf, cnt);
  1988. /* Push data if we have reached the expected data length */
  1989. if ((data->bytes_xfered + init_cnt) ==
  1990. (data->blksz * data->blocks))
  1991. mci_fifo_writeq(host->fifo_reg, host->part_buf);
  1992. }
  1993. }
  1994. static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
  1995. {
  1996. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1997. if (unlikely((unsigned long)buf & 0x7)) {
  1998. while (cnt >= 8) {
  1999. /* pull data from fifo into aligned buffer */
  2000. u64 aligned_buf[16];
  2001. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  2002. int items = len >> 3;
  2003. int i;
  2004. for (i = 0; i < items; ++i)
  2005. aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
  2006. /* memcpy from aligned buffer into output buffer */
  2007. memcpy(buf, aligned_buf, len);
  2008. buf += len;
  2009. cnt -= len;
  2010. }
  2011. } else
  2012. #endif
  2013. {
  2014. u64 *pdata = buf;
  2015. for (; cnt >= 8; cnt -= 8)
  2016. *pdata++ = mci_fifo_readq(host->fifo_reg);
  2017. buf = pdata;
  2018. }
  2019. if (cnt) {
  2020. host->part_buf = mci_fifo_readq(host->fifo_reg);
  2021. dw_mci_pull_final_bytes(host, buf, cnt);
  2022. }
  2023. }
  2024. static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
  2025. {
  2026. int len;
  2027. /* get remaining partial bytes */
  2028. len = dw_mci_pull_part_bytes(host, buf, cnt);
  2029. if (unlikely(len == cnt))
  2030. return;
  2031. buf += len;
  2032. cnt -= len;
  2033. /* get the rest of the data */
  2034. host->pull_data(host, buf, cnt);
  2035. }
  2036. static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
  2037. {
  2038. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  2039. void *buf;
  2040. unsigned int offset;
  2041. struct mmc_data *data = host->data;
  2042. int shift = host->data_shift;
  2043. u32 status;
  2044. unsigned int len;
  2045. unsigned int remain, fcnt;
  2046. do {
  2047. if (!sg_miter_next(sg_miter))
  2048. goto done;
  2049. host->sg = sg_miter->piter.sg;
  2050. buf = sg_miter->addr;
  2051. remain = sg_miter->length;
  2052. offset = 0;
  2053. do {
  2054. fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
  2055. << shift) + host->part_buf_count;
  2056. len = min(remain, fcnt);
  2057. if (!len)
  2058. break;
  2059. dw_mci_pull_data(host, (void *)(buf + offset), len);
  2060. data->bytes_xfered += len;
  2061. offset += len;
  2062. remain -= len;
  2063. } while (remain);
  2064. sg_miter->consumed = offset;
  2065. status = mci_readl(host, MINTSTS);
  2066. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  2067. /* if the RXDR is ready read again */
  2068. } while ((status & SDMMC_INT_RXDR) ||
  2069. (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
  2070. if (!remain) {
  2071. if (!sg_miter_next(sg_miter))
  2072. goto done;
  2073. sg_miter->consumed = 0;
  2074. }
  2075. sg_miter_stop(sg_miter);
  2076. return;
  2077. done:
  2078. sg_miter_stop(sg_miter);
  2079. host->sg = NULL;
  2080. smp_wmb(); /* drain writebuffer */
  2081. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  2082. }
  2083. static void dw_mci_write_data_pio(struct dw_mci *host)
  2084. {
  2085. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  2086. void *buf;
  2087. unsigned int offset;
  2088. struct mmc_data *data = host->data;
  2089. int shift = host->data_shift;
  2090. u32 status;
  2091. unsigned int len;
  2092. unsigned int fifo_depth = host->fifo_depth;
  2093. unsigned int remain, fcnt;
  2094. do {
  2095. if (!sg_miter_next(sg_miter))
  2096. goto done;
  2097. host->sg = sg_miter->piter.sg;
  2098. buf = sg_miter->addr;
  2099. remain = sg_miter->length;
  2100. offset = 0;
  2101. do {
  2102. fcnt = ((fifo_depth -
  2103. SDMMC_GET_FCNT(mci_readl(host, STATUS)))
  2104. << shift) - host->part_buf_count;
  2105. len = min(remain, fcnt);
  2106. if (!len)
  2107. break;
  2108. host->push_data(host, (void *)(buf + offset), len);
  2109. data->bytes_xfered += len;
  2110. offset += len;
  2111. remain -= len;
  2112. } while (remain);
  2113. sg_miter->consumed = offset;
  2114. status = mci_readl(host, MINTSTS);
  2115. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  2116. } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
  2117. if (!remain) {
  2118. if (!sg_miter_next(sg_miter))
  2119. goto done;
  2120. sg_miter->consumed = 0;
  2121. }
  2122. sg_miter_stop(sg_miter);
  2123. return;
  2124. done:
  2125. sg_miter_stop(sg_miter);
  2126. host->sg = NULL;
  2127. smp_wmb(); /* drain writebuffer */
  2128. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  2129. }
  2130. static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
  2131. {
  2132. if (!host->cmd_status)
  2133. host->cmd_status = status;
  2134. smp_wmb(); /* drain writebuffer */
  2135. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  2136. tasklet_schedule(&host->tasklet);
  2137. }
  2138. static void dw_mci_handle_cd(struct dw_mci *host)
  2139. {
  2140. int i;
  2141. for (i = 0; i < host->num_slots; i++) {
  2142. struct dw_mci_slot *slot = host->slot[i];
  2143. if (!slot)
  2144. continue;
  2145. if (slot->mmc->ops->card_event)
  2146. slot->mmc->ops->card_event(slot->mmc);
  2147. mmc_detect_change(slot->mmc,
  2148. msecs_to_jiffies(host->pdata->detect_delay_ms));
  2149. }
  2150. }
  2151. static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
  2152. {
  2153. struct dw_mci *host = dev_id;
  2154. u32 pending;
  2155. int i;
  2156. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  2157. if (pending) {
  2158. /* Check volt switch first, since it can look like an error */
  2159. if ((host->state == STATE_SENDING_CMD11) &&
  2160. (pending & SDMMC_INT_VOLT_SWITCH)) {
  2161. unsigned long irqflags;
  2162. mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
  2163. pending &= ~SDMMC_INT_VOLT_SWITCH;
  2164. /*
  2165. * Hold the lock; we know cmd11_timer can't be kicked
  2166. * off after the lock is released, so safe to delete.
  2167. */
  2168. spin_lock_irqsave(&host->irq_lock, irqflags);
  2169. dw_mci_cmd_interrupt(host, pending);
  2170. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  2171. del_timer(&host->cmd11_timer);
  2172. }
  2173. if (pending & DW_MCI_CMD_ERROR_FLAGS) {
  2174. mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
  2175. host->cmd_status = pending;
  2176. smp_wmb(); /* drain writebuffer */
  2177. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  2178. }
  2179. if (pending & DW_MCI_DATA_ERROR_FLAGS) {
  2180. /* if there is an error report DATA_ERROR */
  2181. mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
  2182. host->data_status = pending;
  2183. smp_wmb(); /* drain writebuffer */
  2184. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  2185. tasklet_schedule(&host->tasklet);
  2186. }
  2187. if (pending & SDMMC_INT_DATA_OVER) {
  2188. del_timer(&host->dto_timer);
  2189. mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
  2190. if (!host->data_status)
  2191. host->data_status = pending;
  2192. smp_wmb(); /* drain writebuffer */
  2193. if (host->dir_status == DW_MCI_RECV_STATUS) {
  2194. if (host->sg != NULL)
  2195. dw_mci_read_data_pio(host, true);
  2196. }
  2197. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  2198. tasklet_schedule(&host->tasklet);
  2199. }
  2200. if (pending & SDMMC_INT_RXDR) {
  2201. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  2202. if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
  2203. dw_mci_read_data_pio(host, false);
  2204. }
  2205. if (pending & SDMMC_INT_TXDR) {
  2206. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  2207. if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
  2208. dw_mci_write_data_pio(host);
  2209. }
  2210. if (pending & SDMMC_INT_CMD_DONE) {
  2211. mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
  2212. dw_mci_cmd_interrupt(host, pending);
  2213. }
  2214. if (pending & SDMMC_INT_CD) {
  2215. mci_writel(host, RINTSTS, SDMMC_INT_CD);
  2216. dw_mci_handle_cd(host);
  2217. }
  2218. /* Handle SDIO Interrupts */
  2219. for (i = 0; i < host->num_slots; i++) {
  2220. struct dw_mci_slot *slot = host->slot[i];
  2221. if (!slot)
  2222. continue;
  2223. if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
  2224. mci_writel(host, RINTSTS,
  2225. SDMMC_INT_SDIO(slot->sdio_id));
  2226. mmc_signal_sdio_irq(slot->mmc);
  2227. }
  2228. }
  2229. }
  2230. if (host->use_dma != TRANS_MODE_IDMAC)
  2231. return IRQ_HANDLED;
  2232. /* Handle IDMA interrupts */
  2233. if (host->dma_64bit_address == 1) {
  2234. pending = mci_readl(host, IDSTS64);
  2235. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  2236. mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
  2237. SDMMC_IDMAC_INT_RI);
  2238. mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
  2239. if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
  2240. host->dma_ops->complete((void *)host);
  2241. }
  2242. } else {
  2243. pending = mci_readl(host, IDSTS);
  2244. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  2245. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
  2246. SDMMC_IDMAC_INT_RI);
  2247. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
  2248. if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
  2249. host->dma_ops->complete((void *)host);
  2250. }
  2251. }
  2252. return IRQ_HANDLED;
  2253. }
  2254. static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
  2255. {
  2256. struct mmc_host *mmc;
  2257. struct dw_mci_slot *slot;
  2258. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2259. int ctrl_id, ret;
  2260. u32 freq[2];
  2261. mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
  2262. if (!mmc)
  2263. return -ENOMEM;
  2264. slot = mmc_priv(mmc);
  2265. slot->id = id;
  2266. slot->sdio_id = host->sdio_id0 + id;
  2267. slot->mmc = mmc;
  2268. slot->host = host;
  2269. host->slot[id] = slot;
  2270. mmc->ops = &dw_mci_ops;
  2271. if (of_property_read_u32_array(host->dev->of_node,
  2272. "clock-freq-min-max", freq, 2)) {
  2273. mmc->f_min = DW_MCI_FREQ_MIN;
  2274. mmc->f_max = DW_MCI_FREQ_MAX;
  2275. } else {
  2276. dev_info(host->dev,
  2277. "'clock-freq-min-max' property was deprecated.\n");
  2278. mmc->f_min = freq[0];
  2279. mmc->f_max = freq[1];
  2280. }
  2281. /*if there are external regulators, get them*/
  2282. ret = mmc_regulator_get_supply(mmc);
  2283. if (ret == -EPROBE_DEFER)
  2284. goto err_host_allocated;
  2285. if (!mmc->ocr_avail)
  2286. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  2287. if (host->pdata->caps)
  2288. mmc->caps = host->pdata->caps;
  2289. /*
  2290. * Support MMC_CAP_ERASE by default.
  2291. * It needs to use trim/discard/erase commands.
  2292. */
  2293. mmc->caps |= MMC_CAP_ERASE;
  2294. if (host->pdata->pm_caps)
  2295. mmc->pm_caps = host->pdata->pm_caps;
  2296. if (host->dev->of_node) {
  2297. ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
  2298. if (ctrl_id < 0)
  2299. ctrl_id = 0;
  2300. } else {
  2301. ctrl_id = to_platform_device(host->dev)->id;
  2302. }
  2303. if (drv_data && drv_data->caps)
  2304. mmc->caps |= drv_data->caps[ctrl_id];
  2305. if (host->pdata->caps2)
  2306. mmc->caps2 = host->pdata->caps2;
  2307. ret = mmc_of_parse(mmc);
  2308. if (ret)
  2309. goto err_host_allocated;
  2310. /* Useful defaults if platform data is unset. */
  2311. if (host->use_dma == TRANS_MODE_IDMAC) {
  2312. mmc->max_segs = host->ring_size;
  2313. mmc->max_blk_size = 65535;
  2314. mmc->max_seg_size = 0x1000;
  2315. mmc->max_req_size = mmc->max_seg_size * host->ring_size;
  2316. mmc->max_blk_count = mmc->max_req_size / 512;
  2317. } else if (host->use_dma == TRANS_MODE_EDMAC) {
  2318. mmc->max_segs = 64;
  2319. mmc->max_blk_size = 65535;
  2320. mmc->max_blk_count = 65535;
  2321. mmc->max_req_size =
  2322. mmc->max_blk_size * mmc->max_blk_count;
  2323. mmc->max_seg_size = mmc->max_req_size;
  2324. } else {
  2325. /* TRANS_MODE_PIO */
  2326. mmc->max_segs = 64;
  2327. mmc->max_blk_size = 65535; /* BLKSIZ is 16 bits */
  2328. mmc->max_blk_count = 512;
  2329. mmc->max_req_size = mmc->max_blk_size *
  2330. mmc->max_blk_count;
  2331. mmc->max_seg_size = mmc->max_req_size;
  2332. }
  2333. dw_mci_get_cd(mmc);
  2334. ret = mmc_add_host(mmc);
  2335. if (ret)
  2336. goto err_host_allocated;
  2337. #if defined(CONFIG_DEBUG_FS)
  2338. dw_mci_init_debugfs(slot);
  2339. #endif
  2340. return 0;
  2341. err_host_allocated:
  2342. mmc_free_host(mmc);
  2343. return ret;
  2344. }
  2345. static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
  2346. {
  2347. /* Debugfs stuff is cleaned up by mmc core */
  2348. mmc_remove_host(slot->mmc);
  2349. slot->host->slot[id] = NULL;
  2350. mmc_free_host(slot->mmc);
  2351. }
  2352. static void dw_mci_init_dma(struct dw_mci *host)
  2353. {
  2354. int addr_config;
  2355. struct device *dev = host->dev;
  2356. struct device_node *np = dev->of_node;
  2357. /*
  2358. * Check tansfer mode from HCON[17:16]
  2359. * Clear the ambiguous description of dw_mmc databook:
  2360. * 2b'00: No DMA Interface -> Actually means using Internal DMA block
  2361. * 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block
  2362. * 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block
  2363. * 2b'11: Non DW DMA Interface -> pio only
  2364. * Compared to DesignWare DMA Interface, Generic DMA Interface has a
  2365. * simpler request/acknowledge handshake mechanism and both of them
  2366. * are regarded as external dma master for dw_mmc.
  2367. */
  2368. host->use_dma = SDMMC_GET_TRANS_MODE(mci_readl(host, HCON));
  2369. if (host->use_dma == DMA_INTERFACE_IDMA) {
  2370. host->use_dma = TRANS_MODE_IDMAC;
  2371. } else if (host->use_dma == DMA_INTERFACE_DWDMA ||
  2372. host->use_dma == DMA_INTERFACE_GDMA) {
  2373. host->use_dma = TRANS_MODE_EDMAC;
  2374. } else {
  2375. goto no_dma;
  2376. }
  2377. /* Determine which DMA interface to use */
  2378. if (host->use_dma == TRANS_MODE_IDMAC) {
  2379. /*
  2380. * Check ADDR_CONFIG bit in HCON to find
  2381. * IDMAC address bus width
  2382. */
  2383. addr_config = SDMMC_GET_ADDR_CONFIG(mci_readl(host, HCON));
  2384. if (addr_config == 1) {
  2385. /* host supports IDMAC in 64-bit address mode */
  2386. host->dma_64bit_address = 1;
  2387. dev_info(host->dev,
  2388. "IDMAC supports 64-bit address mode.\n");
  2389. if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
  2390. dma_set_coherent_mask(host->dev,
  2391. DMA_BIT_MASK(64));
  2392. } else {
  2393. /* host supports IDMAC in 32-bit address mode */
  2394. host->dma_64bit_address = 0;
  2395. dev_info(host->dev,
  2396. "IDMAC supports 32-bit address mode.\n");
  2397. }
  2398. /* Alloc memory for sg translation */
  2399. host->sg_cpu = dmam_alloc_coherent(host->dev,
  2400. DESC_RING_BUF_SZ,
  2401. &host->sg_dma, GFP_KERNEL);
  2402. if (!host->sg_cpu) {
  2403. dev_err(host->dev,
  2404. "%s: could not alloc DMA memory\n",
  2405. __func__);
  2406. goto no_dma;
  2407. }
  2408. host->dma_ops = &dw_mci_idmac_ops;
  2409. dev_info(host->dev, "Using internal DMA controller.\n");
  2410. } else {
  2411. /* TRANS_MODE_EDMAC: check dma bindings again */
  2412. if ((of_property_count_strings(np, "dma-names") < 0) ||
  2413. (!of_find_property(np, "dmas", NULL))) {
  2414. goto no_dma;
  2415. }
  2416. host->dma_ops = &dw_mci_edmac_ops;
  2417. dev_info(host->dev, "Using external DMA controller.\n");
  2418. }
  2419. if (host->dma_ops->init && host->dma_ops->start &&
  2420. host->dma_ops->stop && host->dma_ops->cleanup) {
  2421. if (host->dma_ops->init(host)) {
  2422. dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n",
  2423. __func__);
  2424. goto no_dma;
  2425. }
  2426. } else {
  2427. dev_err(host->dev, "DMA initialization not found.\n");
  2428. goto no_dma;
  2429. }
  2430. return;
  2431. no_dma:
  2432. dev_info(host->dev, "Using PIO mode.\n");
  2433. host->use_dma = TRANS_MODE_PIO;
  2434. }
  2435. static void dw_mci_cmd11_timer(unsigned long arg)
  2436. {
  2437. struct dw_mci *host = (struct dw_mci *)arg;
  2438. if (host->state != STATE_SENDING_CMD11) {
  2439. dev_warn(host->dev, "Unexpected CMD11 timeout\n");
  2440. return;
  2441. }
  2442. host->cmd_status = SDMMC_INT_RTO;
  2443. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  2444. tasklet_schedule(&host->tasklet);
  2445. }
  2446. static void dw_mci_dto_timer(unsigned long arg)
  2447. {
  2448. struct dw_mci *host = (struct dw_mci *)arg;
  2449. switch (host->state) {
  2450. case STATE_SENDING_DATA:
  2451. case STATE_DATA_BUSY:
  2452. /*
  2453. * If DTO interrupt does NOT come in sending data state,
  2454. * we should notify the driver to terminate current transfer
  2455. * and report a data timeout to the core.
  2456. */
  2457. host->data_status = SDMMC_INT_DRTO;
  2458. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  2459. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  2460. tasklet_schedule(&host->tasklet);
  2461. break;
  2462. default:
  2463. break;
  2464. }
  2465. }
  2466. #ifdef CONFIG_OF
  2467. static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
  2468. {
  2469. struct dw_mci_board *pdata;
  2470. struct device *dev = host->dev;
  2471. struct device_node *np = dev->of_node;
  2472. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2473. int ret;
  2474. u32 clock_frequency;
  2475. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  2476. if (!pdata)
  2477. return ERR_PTR(-ENOMEM);
  2478. /* find reset controller when exist */
  2479. pdata->rstc = devm_reset_control_get_optional(dev, "reset");
  2480. if (IS_ERR(pdata->rstc)) {
  2481. if (PTR_ERR(pdata->rstc) == -EPROBE_DEFER)
  2482. return ERR_PTR(-EPROBE_DEFER);
  2483. }
  2484. /* find out number of slots supported */
  2485. of_property_read_u32(np, "num-slots", &pdata->num_slots);
  2486. if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
  2487. dev_info(dev,
  2488. "fifo-depth property not found, using value of FIFOTH register as default\n");
  2489. of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
  2490. of_property_read_u32(np, "data-addr", &host->data_addr_override);
  2491. if (of_get_property(np, "fifo-watermark-aligned", NULL))
  2492. host->wm_aligned = true;
  2493. if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
  2494. pdata->bus_hz = clock_frequency;
  2495. if (drv_data && drv_data->parse_dt) {
  2496. ret = drv_data->parse_dt(host);
  2497. if (ret)
  2498. return ERR_PTR(ret);
  2499. }
  2500. return pdata;
  2501. }
  2502. #else /* CONFIG_OF */
  2503. static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
  2504. {
  2505. return ERR_PTR(-EINVAL);
  2506. }
  2507. #endif /* CONFIG_OF */
  2508. static void dw_mci_enable_cd(struct dw_mci *host)
  2509. {
  2510. unsigned long irqflags;
  2511. u32 temp;
  2512. int i;
  2513. struct dw_mci_slot *slot;
  2514. /*
  2515. * No need for CD if all slots have a non-error GPIO
  2516. * as well as broken card detection is found.
  2517. */
  2518. for (i = 0; i < host->num_slots; i++) {
  2519. slot = host->slot[i];
  2520. if (slot->mmc->caps & MMC_CAP_NEEDS_POLL)
  2521. return;
  2522. if (mmc_gpio_get_cd(slot->mmc) < 0)
  2523. break;
  2524. }
  2525. if (i == host->num_slots)
  2526. return;
  2527. spin_lock_irqsave(&host->irq_lock, irqflags);
  2528. temp = mci_readl(host, INTMASK);
  2529. temp |= SDMMC_INT_CD;
  2530. mci_writel(host, INTMASK, temp);
  2531. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  2532. }
  2533. int dw_mci_probe(struct dw_mci *host)
  2534. {
  2535. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2536. int width, i, ret = 0;
  2537. u32 fifo_size;
  2538. int init_slots = 0;
  2539. if (!host->pdata) {
  2540. host->pdata = dw_mci_parse_dt(host);
  2541. if (PTR_ERR(host->pdata) == -EPROBE_DEFER) {
  2542. return -EPROBE_DEFER;
  2543. } else if (IS_ERR(host->pdata)) {
  2544. dev_err(host->dev, "platform data not available\n");
  2545. return -EINVAL;
  2546. }
  2547. }
  2548. host->biu_clk = devm_clk_get(host->dev, "biu");
  2549. if (IS_ERR(host->biu_clk)) {
  2550. dev_dbg(host->dev, "biu clock not available\n");
  2551. } else {
  2552. ret = clk_prepare_enable(host->biu_clk);
  2553. if (ret) {
  2554. dev_err(host->dev, "failed to enable biu clock\n");
  2555. return ret;
  2556. }
  2557. }
  2558. host->ciu_clk = devm_clk_get(host->dev, "ciu");
  2559. if (IS_ERR(host->ciu_clk)) {
  2560. dev_dbg(host->dev, "ciu clock not available\n");
  2561. host->bus_hz = host->pdata->bus_hz;
  2562. } else {
  2563. ret = clk_prepare_enable(host->ciu_clk);
  2564. if (ret) {
  2565. dev_err(host->dev, "failed to enable ciu clock\n");
  2566. goto err_clk_biu;
  2567. }
  2568. if (host->pdata->bus_hz) {
  2569. ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
  2570. if (ret)
  2571. dev_warn(host->dev,
  2572. "Unable to set bus rate to %uHz\n",
  2573. host->pdata->bus_hz);
  2574. }
  2575. host->bus_hz = clk_get_rate(host->ciu_clk);
  2576. }
  2577. if (!host->bus_hz) {
  2578. dev_err(host->dev,
  2579. "Platform data must supply bus speed\n");
  2580. ret = -ENODEV;
  2581. goto err_clk_ciu;
  2582. }
  2583. if (drv_data && drv_data->init) {
  2584. ret = drv_data->init(host);
  2585. if (ret) {
  2586. dev_err(host->dev,
  2587. "implementation specific init failed\n");
  2588. goto err_clk_ciu;
  2589. }
  2590. }
  2591. if (!IS_ERR(host->pdata->rstc)) {
  2592. reset_control_assert(host->pdata->rstc);
  2593. usleep_range(10, 50);
  2594. reset_control_deassert(host->pdata->rstc);
  2595. }
  2596. setup_timer(&host->cmd11_timer,
  2597. dw_mci_cmd11_timer, (unsigned long)host);
  2598. setup_timer(&host->dto_timer,
  2599. dw_mci_dto_timer, (unsigned long)host);
  2600. spin_lock_init(&host->lock);
  2601. spin_lock_init(&host->irq_lock);
  2602. INIT_LIST_HEAD(&host->queue);
  2603. /*
  2604. * Get the host data width - this assumes that HCON has been set with
  2605. * the correct values.
  2606. */
  2607. i = SDMMC_GET_HDATA_WIDTH(mci_readl(host, HCON));
  2608. if (!i) {
  2609. host->push_data = dw_mci_push_data16;
  2610. host->pull_data = dw_mci_pull_data16;
  2611. width = 16;
  2612. host->data_shift = 1;
  2613. } else if (i == 2) {
  2614. host->push_data = dw_mci_push_data64;
  2615. host->pull_data = dw_mci_pull_data64;
  2616. width = 64;
  2617. host->data_shift = 3;
  2618. } else {
  2619. /* Check for a reserved value, and warn if it is */
  2620. WARN((i != 1),
  2621. "HCON reports a reserved host data width!\n"
  2622. "Defaulting to 32-bit access.\n");
  2623. host->push_data = dw_mci_push_data32;
  2624. host->pull_data = dw_mci_pull_data32;
  2625. width = 32;
  2626. host->data_shift = 2;
  2627. }
  2628. /* Reset all blocks */
  2629. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
  2630. ret = -ENODEV;
  2631. goto err_clk_ciu;
  2632. }
  2633. host->dma_ops = host->pdata->dma_ops;
  2634. dw_mci_init_dma(host);
  2635. /* Clear the interrupts for the host controller */
  2636. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2637. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  2638. /* Put in max timeout */
  2639. mci_writel(host, TMOUT, 0xFFFFFFFF);
  2640. /*
  2641. * FIFO threshold settings RxMark = fifo_size / 2 - 1,
  2642. * Tx Mark = fifo_size / 2 DMA Size = 8
  2643. */
  2644. if (!host->pdata->fifo_depth) {
  2645. /*
  2646. * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
  2647. * have been overwritten by the bootloader, just like we're
  2648. * about to do, so if you know the value for your hardware, you
  2649. * should put it in the platform data.
  2650. */
  2651. fifo_size = mci_readl(host, FIFOTH);
  2652. fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
  2653. } else {
  2654. fifo_size = host->pdata->fifo_depth;
  2655. }
  2656. host->fifo_depth = fifo_size;
  2657. host->fifoth_val =
  2658. SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
  2659. mci_writel(host, FIFOTH, host->fifoth_val);
  2660. /* disable clock to CIU */
  2661. mci_writel(host, CLKENA, 0);
  2662. mci_writel(host, CLKSRC, 0);
  2663. /*
  2664. * In 2.40a spec, Data offset is changed.
  2665. * Need to check the version-id and set data-offset for DATA register.
  2666. */
  2667. host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
  2668. dev_info(host->dev, "Version ID is %04x\n", host->verid);
  2669. if (host->data_addr_override)
  2670. host->fifo_reg = host->regs + host->data_addr_override;
  2671. else if (host->verid < DW_MMC_240A)
  2672. host->fifo_reg = host->regs + DATA_OFFSET;
  2673. else
  2674. host->fifo_reg = host->regs + DATA_240A_OFFSET;
  2675. tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
  2676. ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
  2677. host->irq_flags, "dw-mci", host);
  2678. if (ret)
  2679. goto err_dmaunmap;
  2680. if (host->pdata->num_slots)
  2681. host->num_slots = host->pdata->num_slots;
  2682. else
  2683. host->num_slots = 1;
  2684. if (host->num_slots < 1 ||
  2685. host->num_slots > SDMMC_GET_SLOT_NUM(mci_readl(host, HCON))) {
  2686. dev_err(host->dev,
  2687. "Platform data must supply correct num_slots.\n");
  2688. ret = -ENODEV;
  2689. goto err_clk_ciu;
  2690. }
  2691. /*
  2692. * Enable interrupts for command done, data over, data empty,
  2693. * receive ready and error such as transmit, receive timeout, crc error
  2694. */
  2695. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  2696. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  2697. DW_MCI_ERROR_FLAGS);
  2698. /* Enable mci interrupt */
  2699. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
  2700. dev_info(host->dev,
  2701. "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
  2702. host->irq, width, fifo_size);
  2703. /* We need at least one slot to succeed */
  2704. for (i = 0; i < host->num_slots; i++) {
  2705. ret = dw_mci_init_slot(host, i);
  2706. if (ret)
  2707. dev_dbg(host->dev, "slot %d init failed\n", i);
  2708. else
  2709. init_slots++;
  2710. }
  2711. if (init_slots) {
  2712. dev_info(host->dev, "%d slots initialized\n", init_slots);
  2713. } else {
  2714. dev_dbg(host->dev,
  2715. "attempted to initialize %d slots, but failed on all\n",
  2716. host->num_slots);
  2717. goto err_dmaunmap;
  2718. }
  2719. /* Now that slots are all setup, we can enable card detect */
  2720. dw_mci_enable_cd(host);
  2721. return 0;
  2722. err_dmaunmap:
  2723. if (host->use_dma && host->dma_ops->exit)
  2724. host->dma_ops->exit(host);
  2725. if (!IS_ERR(host->pdata->rstc))
  2726. reset_control_assert(host->pdata->rstc);
  2727. err_clk_ciu:
  2728. clk_disable_unprepare(host->ciu_clk);
  2729. err_clk_biu:
  2730. clk_disable_unprepare(host->biu_clk);
  2731. return ret;
  2732. }
  2733. EXPORT_SYMBOL(dw_mci_probe);
  2734. void dw_mci_remove(struct dw_mci *host)
  2735. {
  2736. int i;
  2737. for (i = 0; i < host->num_slots; i++) {
  2738. dev_dbg(host->dev, "remove slot %d\n", i);
  2739. if (host->slot[i])
  2740. dw_mci_cleanup_slot(host->slot[i], i);
  2741. }
  2742. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2743. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  2744. /* disable clock to CIU */
  2745. mci_writel(host, CLKENA, 0);
  2746. mci_writel(host, CLKSRC, 0);
  2747. if (host->use_dma && host->dma_ops->exit)
  2748. host->dma_ops->exit(host);
  2749. if (!IS_ERR(host->pdata->rstc))
  2750. reset_control_assert(host->pdata->rstc);
  2751. clk_disable_unprepare(host->ciu_clk);
  2752. clk_disable_unprepare(host->biu_clk);
  2753. }
  2754. EXPORT_SYMBOL(dw_mci_remove);
  2755. #ifdef CONFIG_PM
  2756. int dw_mci_runtime_suspend(struct device *dev)
  2757. {
  2758. struct dw_mci *host = dev_get_drvdata(dev);
  2759. if (host->use_dma && host->dma_ops->exit)
  2760. host->dma_ops->exit(host);
  2761. clk_disable_unprepare(host->ciu_clk);
  2762. if (host->cur_slot &&
  2763. (mmc_can_gpio_cd(host->cur_slot->mmc) ||
  2764. !mmc_card_is_removable(host->cur_slot->mmc)))
  2765. clk_disable_unprepare(host->biu_clk);
  2766. return 0;
  2767. }
  2768. EXPORT_SYMBOL(dw_mci_runtime_suspend);
  2769. int dw_mci_runtime_resume(struct device *dev)
  2770. {
  2771. int i, ret = 0;
  2772. struct dw_mci *host = dev_get_drvdata(dev);
  2773. if (host->cur_slot &&
  2774. (mmc_can_gpio_cd(host->cur_slot->mmc) ||
  2775. !mmc_card_is_removable(host->cur_slot->mmc))) {
  2776. ret = clk_prepare_enable(host->biu_clk);
  2777. if (ret)
  2778. return ret;
  2779. }
  2780. ret = clk_prepare_enable(host->ciu_clk);
  2781. if (ret)
  2782. goto err;
  2783. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
  2784. clk_disable_unprepare(host->ciu_clk);
  2785. ret = -ENODEV;
  2786. goto err;
  2787. }
  2788. if (host->use_dma && host->dma_ops->init)
  2789. host->dma_ops->init(host);
  2790. /*
  2791. * Restore the initial value at FIFOTH register
  2792. * And Invalidate the prev_blksz with zero
  2793. */
  2794. mci_writel(host, FIFOTH, host->fifoth_val);
  2795. host->prev_blksz = 0;
  2796. /* Put in max timeout */
  2797. mci_writel(host, TMOUT, 0xFFFFFFFF);
  2798. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2799. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  2800. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  2801. DW_MCI_ERROR_FLAGS);
  2802. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
  2803. for (i = 0; i < host->num_slots; i++) {
  2804. struct dw_mci_slot *slot = host->slot[i];
  2805. if (!slot)
  2806. continue;
  2807. if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER)
  2808. dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
  2809. /* Force setup bus to guarantee available clock output */
  2810. dw_mci_setup_bus(slot, true);
  2811. }
  2812. /* Now that slots are all setup, we can enable card detect */
  2813. dw_mci_enable_cd(host);
  2814. return 0;
  2815. err:
  2816. if (host->cur_slot &&
  2817. (mmc_can_gpio_cd(host->cur_slot->mmc) ||
  2818. !mmc_card_is_removable(host->cur_slot->mmc)))
  2819. clk_disable_unprepare(host->biu_clk);
  2820. return ret;
  2821. }
  2822. EXPORT_SYMBOL(dw_mci_runtime_resume);
  2823. #endif /* CONFIG_PM */
  2824. static int __init dw_mci_init(void)
  2825. {
  2826. pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
  2827. return 0;
  2828. }
  2829. static void __exit dw_mci_exit(void)
  2830. {
  2831. }
  2832. module_init(dw_mci_init);
  2833. module_exit(dw_mci_exit);
  2834. MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
  2835. MODULE_AUTHOR("NXP Semiconductor VietNam");
  2836. MODULE_AUTHOR("Imagination Technologies Ltd");
  2837. MODULE_LICENSE("GPL v2");