dw_mmc.c 68 KB

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