dw_mmc.c 83 KB

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