dw_mmc.c 68 KB

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