dw_mmc.c 84 KB

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