sh_mmcif.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. /*
  2. * MMCIF eMMC driver.
  3. *
  4. * Copyright (C) 2010 Renesas Solutions Corp.
  5. * Yusuke Goda <yusuke.goda.sx@renesas.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License.
  10. *
  11. *
  12. * TODO
  13. * 1. DMA
  14. * 2. Power management
  15. * 3. Handle MMC errors better
  16. *
  17. */
  18. /*
  19. * The MMCIF driver is now processing MMC requests asynchronously, according
  20. * to the Linux MMC API requirement.
  21. *
  22. * The MMCIF driver processes MMC requests in up to 3 stages: command, optional
  23. * data, and optional stop. To achieve asynchronous processing each of these
  24. * stages is split into two halves: a top and a bottom half. The top half
  25. * initialises the hardware, installs a timeout handler to handle completion
  26. * timeouts, and returns. In case of the command stage this immediately returns
  27. * control to the caller, leaving all further processing to run asynchronously.
  28. * All further request processing is performed by the bottom halves.
  29. *
  30. * The bottom half further consists of a "hard" IRQ handler, an IRQ handler
  31. * thread, a DMA completion callback, if DMA is used, a timeout work, and
  32. * request- and stage-specific handler methods.
  33. *
  34. * Each bottom half run begins with either a hardware interrupt, a DMA callback
  35. * invocation, or a timeout work run. In case of an error or a successful
  36. * processing completion, the MMC core is informed and the request processing is
  37. * finished. In case processing has to continue, i.e., if data has to be read
  38. * from or written to the card, or if a stop command has to be sent, the next
  39. * top half is called, which performs the necessary hardware handling and
  40. * reschedules the timeout work. This returns the driver state machine into the
  41. * bottom half waiting state.
  42. */
  43. #include <linux/bitops.h>
  44. #include <linux/clk.h>
  45. #include <linux/completion.h>
  46. #include <linux/delay.h>
  47. #include <linux/dma-mapping.h>
  48. #include <linux/dmaengine.h>
  49. #include <linux/mmc/card.h>
  50. #include <linux/mmc/core.h>
  51. #include <linux/mmc/host.h>
  52. #include <linux/mmc/mmc.h>
  53. #include <linux/mmc/sdio.h>
  54. #include <linux/mmc/sh_mmcif.h>
  55. #include <linux/mmc/slot-gpio.h>
  56. #include <linux/mod_devicetable.h>
  57. #include <linux/mutex.h>
  58. #include <linux/pagemap.h>
  59. #include <linux/platform_device.h>
  60. #include <linux/pm_qos.h>
  61. #include <linux/pm_runtime.h>
  62. #include <linux/sh_dma.h>
  63. #include <linux/spinlock.h>
  64. #include <linux/module.h>
  65. #define DRIVER_NAME "sh_mmcif"
  66. #define DRIVER_VERSION "2010-04-28"
  67. /* CE_CMD_SET */
  68. #define CMD_MASK 0x3f000000
  69. #define CMD_SET_RTYP_NO ((0 << 23) | (0 << 22))
  70. #define CMD_SET_RTYP_6B ((0 << 23) | (1 << 22)) /* R1/R1b/R3/R4/R5 */
  71. #define CMD_SET_RTYP_17B ((1 << 23) | (0 << 22)) /* R2 */
  72. #define CMD_SET_RBSY (1 << 21) /* R1b */
  73. #define CMD_SET_CCSEN (1 << 20)
  74. #define CMD_SET_WDAT (1 << 19) /* 1: on data, 0: no data */
  75. #define CMD_SET_DWEN (1 << 18) /* 1: write, 0: read */
  76. #define CMD_SET_CMLTE (1 << 17) /* 1: multi block trans, 0: single */
  77. #define CMD_SET_CMD12EN (1 << 16) /* 1: CMD12 auto issue */
  78. #define CMD_SET_RIDXC_INDEX ((0 << 15) | (0 << 14)) /* index check */
  79. #define CMD_SET_RIDXC_BITS ((0 << 15) | (1 << 14)) /* check bits check */
  80. #define CMD_SET_RIDXC_NO ((1 << 15) | (0 << 14)) /* no check */
  81. #define CMD_SET_CRC7C ((0 << 13) | (0 << 12)) /* CRC7 check*/
  82. #define CMD_SET_CRC7C_BITS ((0 << 13) | (1 << 12)) /* check bits check*/
  83. #define CMD_SET_CRC7C_INTERNAL ((1 << 13) | (0 << 12)) /* internal CRC7 check*/
  84. #define CMD_SET_CRC16C (1 << 10) /* 0: CRC16 check*/
  85. #define CMD_SET_CRCSTE (1 << 8) /* 1: not receive CRC status */
  86. #define CMD_SET_TBIT (1 << 7) /* 1: tran mission bit "Low" */
  87. #define CMD_SET_OPDM (1 << 6) /* 1: open/drain */
  88. #define CMD_SET_CCSH (1 << 5)
  89. #define CMD_SET_DARS (1 << 2) /* Dual Data Rate */
  90. #define CMD_SET_DATW_1 ((0 << 1) | (0 << 0)) /* 1bit */
  91. #define CMD_SET_DATW_4 ((0 << 1) | (1 << 0)) /* 4bit */
  92. #define CMD_SET_DATW_8 ((1 << 1) | (0 << 0)) /* 8bit */
  93. /* CE_CMD_CTRL */
  94. #define CMD_CTRL_BREAK (1 << 0)
  95. /* CE_BLOCK_SET */
  96. #define BLOCK_SIZE_MASK 0x0000ffff
  97. /* CE_INT */
  98. #define INT_CCSDE (1 << 29)
  99. #define INT_CMD12DRE (1 << 26)
  100. #define INT_CMD12RBE (1 << 25)
  101. #define INT_CMD12CRE (1 << 24)
  102. #define INT_DTRANE (1 << 23)
  103. #define INT_BUFRE (1 << 22)
  104. #define INT_BUFWEN (1 << 21)
  105. #define INT_BUFREN (1 << 20)
  106. #define INT_CCSRCV (1 << 19)
  107. #define INT_RBSYE (1 << 17)
  108. #define INT_CRSPE (1 << 16)
  109. #define INT_CMDVIO (1 << 15)
  110. #define INT_BUFVIO (1 << 14)
  111. #define INT_WDATERR (1 << 11)
  112. #define INT_RDATERR (1 << 10)
  113. #define INT_RIDXERR (1 << 9)
  114. #define INT_RSPERR (1 << 8)
  115. #define INT_CCSTO (1 << 5)
  116. #define INT_CRCSTO (1 << 4)
  117. #define INT_WDATTO (1 << 3)
  118. #define INT_RDATTO (1 << 2)
  119. #define INT_RBSYTO (1 << 1)
  120. #define INT_RSPTO (1 << 0)
  121. #define INT_ERR_STS (INT_CMDVIO | INT_BUFVIO | INT_WDATERR | \
  122. INT_RDATERR | INT_RIDXERR | INT_RSPERR | \
  123. INT_CCSTO | INT_CRCSTO | INT_WDATTO | \
  124. INT_RDATTO | INT_RBSYTO | INT_RSPTO)
  125. #define INT_ALL (INT_RBSYE | INT_CRSPE | INT_BUFREN | \
  126. INT_BUFWEN | INT_CMD12DRE | INT_BUFRE | \
  127. INT_DTRANE | INT_CMD12RBE | INT_CMD12CRE)
  128. #define INT_CCS (INT_CCSTO | INT_CCSRCV | INT_CCSDE)
  129. /* CE_INT_MASK */
  130. #define MASK_ALL 0x00000000
  131. #define MASK_MCCSDE (1 << 29)
  132. #define MASK_MCMD12DRE (1 << 26)
  133. #define MASK_MCMD12RBE (1 << 25)
  134. #define MASK_MCMD12CRE (1 << 24)
  135. #define MASK_MDTRANE (1 << 23)
  136. #define MASK_MBUFRE (1 << 22)
  137. #define MASK_MBUFWEN (1 << 21)
  138. #define MASK_MBUFREN (1 << 20)
  139. #define MASK_MCCSRCV (1 << 19)
  140. #define MASK_MRBSYE (1 << 17)
  141. #define MASK_MCRSPE (1 << 16)
  142. #define MASK_MCMDVIO (1 << 15)
  143. #define MASK_MBUFVIO (1 << 14)
  144. #define MASK_MWDATERR (1 << 11)
  145. #define MASK_MRDATERR (1 << 10)
  146. #define MASK_MRIDXERR (1 << 9)
  147. #define MASK_MRSPERR (1 << 8)
  148. #define MASK_MCCSTO (1 << 5)
  149. #define MASK_MCRCSTO (1 << 4)
  150. #define MASK_MWDATTO (1 << 3)
  151. #define MASK_MRDATTO (1 << 2)
  152. #define MASK_MRBSYTO (1 << 1)
  153. #define MASK_MRSPTO (1 << 0)
  154. #define MASK_START_CMD (MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR | \
  155. MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR | \
  156. MASK_MCRCSTO | MASK_MWDATTO | \
  157. MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO)
  158. #define MASK_CLEAN (INT_ERR_STS | MASK_MRBSYE | MASK_MCRSPE | \
  159. MASK_MBUFREN | MASK_MBUFWEN | \
  160. MASK_MCMD12DRE | MASK_MBUFRE | MASK_MDTRANE | \
  161. MASK_MCMD12RBE | MASK_MCMD12CRE)
  162. /* CE_HOST_STS1 */
  163. #define STS1_CMDSEQ (1 << 31)
  164. /* CE_HOST_STS2 */
  165. #define STS2_CRCSTE (1 << 31)
  166. #define STS2_CRC16E (1 << 30)
  167. #define STS2_AC12CRCE (1 << 29)
  168. #define STS2_RSPCRC7E (1 << 28)
  169. #define STS2_CRCSTEBE (1 << 27)
  170. #define STS2_RDATEBE (1 << 26)
  171. #define STS2_AC12REBE (1 << 25)
  172. #define STS2_RSPEBE (1 << 24)
  173. #define STS2_AC12IDXE (1 << 23)
  174. #define STS2_RSPIDXE (1 << 22)
  175. #define STS2_CCSTO (1 << 15)
  176. #define STS2_RDATTO (1 << 14)
  177. #define STS2_DATBSYTO (1 << 13)
  178. #define STS2_CRCSTTO (1 << 12)
  179. #define STS2_AC12BSYTO (1 << 11)
  180. #define STS2_RSPBSYTO (1 << 10)
  181. #define STS2_AC12RSPTO (1 << 9)
  182. #define STS2_RSPTO (1 << 8)
  183. #define STS2_CRC_ERR (STS2_CRCSTE | STS2_CRC16E | \
  184. STS2_AC12CRCE | STS2_RSPCRC7E | STS2_CRCSTEBE)
  185. #define STS2_TIMEOUT_ERR (STS2_CCSTO | STS2_RDATTO | \
  186. STS2_DATBSYTO | STS2_CRCSTTO | \
  187. STS2_AC12BSYTO | STS2_RSPBSYTO | \
  188. STS2_AC12RSPTO | STS2_RSPTO)
  189. #define CLKDEV_EMMC_DATA 52000000 /* 52MHz */
  190. #define CLKDEV_MMC_DATA 20000000 /* 20MHz */
  191. #define CLKDEV_INIT 400000 /* 400 KHz */
  192. enum mmcif_state {
  193. STATE_IDLE,
  194. STATE_REQUEST,
  195. STATE_IOS,
  196. STATE_TIMEOUT,
  197. };
  198. enum mmcif_wait_for {
  199. MMCIF_WAIT_FOR_REQUEST,
  200. MMCIF_WAIT_FOR_CMD,
  201. MMCIF_WAIT_FOR_MREAD,
  202. MMCIF_WAIT_FOR_MWRITE,
  203. MMCIF_WAIT_FOR_READ,
  204. MMCIF_WAIT_FOR_WRITE,
  205. MMCIF_WAIT_FOR_READ_END,
  206. MMCIF_WAIT_FOR_WRITE_END,
  207. MMCIF_WAIT_FOR_STOP,
  208. };
  209. struct sh_mmcif_host {
  210. struct mmc_host *mmc;
  211. struct mmc_request *mrq;
  212. struct platform_device *pd;
  213. struct clk *hclk;
  214. unsigned int clk;
  215. int bus_width;
  216. unsigned char timing;
  217. bool sd_error;
  218. bool dying;
  219. long timeout;
  220. void __iomem *addr;
  221. u32 *pio_ptr;
  222. spinlock_t lock; /* protect sh_mmcif_host::state */
  223. enum mmcif_state state;
  224. enum mmcif_wait_for wait_for;
  225. struct delayed_work timeout_work;
  226. size_t blocksize;
  227. int sg_idx;
  228. int sg_blkidx;
  229. bool power;
  230. bool card_present;
  231. bool ccs_enable; /* Command Completion Signal support */
  232. bool clk_ctrl2_enable;
  233. struct mutex thread_lock;
  234. /* DMA support */
  235. struct dma_chan *chan_rx;
  236. struct dma_chan *chan_tx;
  237. struct completion dma_complete;
  238. bool dma_active;
  239. };
  240. static inline void sh_mmcif_bitset(struct sh_mmcif_host *host,
  241. unsigned int reg, u32 val)
  242. {
  243. writel(val | readl(host->addr + reg), host->addr + reg);
  244. }
  245. static inline void sh_mmcif_bitclr(struct sh_mmcif_host *host,
  246. unsigned int reg, u32 val)
  247. {
  248. writel(~val & readl(host->addr + reg), host->addr + reg);
  249. }
  250. static void mmcif_dma_complete(void *arg)
  251. {
  252. struct sh_mmcif_host *host = arg;
  253. struct mmc_request *mrq = host->mrq;
  254. dev_dbg(&host->pd->dev, "Command completed\n");
  255. if (WARN(!mrq || !mrq->data, "%s: NULL data in DMA completion!\n",
  256. dev_name(&host->pd->dev)))
  257. return;
  258. complete(&host->dma_complete);
  259. }
  260. static void sh_mmcif_start_dma_rx(struct sh_mmcif_host *host)
  261. {
  262. struct mmc_data *data = host->mrq->data;
  263. struct scatterlist *sg = data->sg;
  264. struct dma_async_tx_descriptor *desc = NULL;
  265. struct dma_chan *chan = host->chan_rx;
  266. dma_cookie_t cookie = -EINVAL;
  267. int ret;
  268. ret = dma_map_sg(chan->device->dev, sg, data->sg_len,
  269. DMA_FROM_DEVICE);
  270. if (ret > 0) {
  271. host->dma_active = true;
  272. desc = dmaengine_prep_slave_sg(chan, sg, ret,
  273. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  274. }
  275. if (desc) {
  276. desc->callback = mmcif_dma_complete;
  277. desc->callback_param = host;
  278. cookie = dmaengine_submit(desc);
  279. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN);
  280. dma_async_issue_pending(chan);
  281. }
  282. dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d\n",
  283. __func__, data->sg_len, ret, cookie);
  284. if (!desc) {
  285. /* DMA failed, fall back to PIO */
  286. if (ret >= 0)
  287. ret = -EIO;
  288. host->chan_rx = NULL;
  289. host->dma_active = false;
  290. dma_release_channel(chan);
  291. /* Free the Tx channel too */
  292. chan = host->chan_tx;
  293. if (chan) {
  294. host->chan_tx = NULL;
  295. dma_release_channel(chan);
  296. }
  297. dev_warn(&host->pd->dev,
  298. "DMA failed: %d, falling back to PIO\n", ret);
  299. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  300. }
  301. dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
  302. desc, cookie, data->sg_len);
  303. }
  304. static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host)
  305. {
  306. struct mmc_data *data = host->mrq->data;
  307. struct scatterlist *sg = data->sg;
  308. struct dma_async_tx_descriptor *desc = NULL;
  309. struct dma_chan *chan = host->chan_tx;
  310. dma_cookie_t cookie = -EINVAL;
  311. int ret;
  312. ret = dma_map_sg(chan->device->dev, sg, data->sg_len,
  313. DMA_TO_DEVICE);
  314. if (ret > 0) {
  315. host->dma_active = true;
  316. desc = dmaengine_prep_slave_sg(chan, sg, ret,
  317. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  318. }
  319. if (desc) {
  320. desc->callback = mmcif_dma_complete;
  321. desc->callback_param = host;
  322. cookie = dmaengine_submit(desc);
  323. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAWEN);
  324. dma_async_issue_pending(chan);
  325. }
  326. dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d\n",
  327. __func__, data->sg_len, ret, cookie);
  328. if (!desc) {
  329. /* DMA failed, fall back to PIO */
  330. if (ret >= 0)
  331. ret = -EIO;
  332. host->chan_tx = NULL;
  333. host->dma_active = false;
  334. dma_release_channel(chan);
  335. /* Free the Rx channel too */
  336. chan = host->chan_rx;
  337. if (chan) {
  338. host->chan_rx = NULL;
  339. dma_release_channel(chan);
  340. }
  341. dev_warn(&host->pd->dev,
  342. "DMA failed: %d, falling back to PIO\n", ret);
  343. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  344. }
  345. dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d\n", __func__,
  346. desc, cookie);
  347. }
  348. static void sh_mmcif_request_dma(struct sh_mmcif_host *host,
  349. struct sh_mmcif_plat_data *pdata)
  350. {
  351. struct resource *res = platform_get_resource(host->pd, IORESOURCE_MEM, 0);
  352. struct dma_slave_config cfg;
  353. dma_cap_mask_t mask;
  354. int ret;
  355. host->dma_active = false;
  356. if (pdata) {
  357. if (pdata->slave_id_tx <= 0 || pdata->slave_id_rx <= 0)
  358. return;
  359. } else if (!host->pd->dev.of_node) {
  360. return;
  361. }
  362. /* We can only either use DMA for both Tx and Rx or not use it at all */
  363. dma_cap_zero(mask);
  364. dma_cap_set(DMA_SLAVE, mask);
  365. host->chan_tx = dma_request_slave_channel_compat(mask, shdma_chan_filter,
  366. pdata ? (void *)pdata->slave_id_tx : NULL,
  367. &host->pd->dev, "tx");
  368. dev_dbg(&host->pd->dev, "%s: TX: got channel %p\n", __func__,
  369. host->chan_tx);
  370. if (!host->chan_tx)
  371. return;
  372. /* In the OF case the driver will get the slave ID from the DT */
  373. if (pdata)
  374. cfg.slave_id = pdata->slave_id_tx;
  375. cfg.direction = DMA_MEM_TO_DEV;
  376. cfg.dst_addr = res->start + MMCIF_CE_DATA;
  377. cfg.src_addr = 0;
  378. ret = dmaengine_slave_config(host->chan_tx, &cfg);
  379. if (ret < 0)
  380. goto ecfgtx;
  381. host->chan_rx = dma_request_slave_channel_compat(mask, shdma_chan_filter,
  382. pdata ? (void *)pdata->slave_id_rx : NULL,
  383. &host->pd->dev, "rx");
  384. dev_dbg(&host->pd->dev, "%s: RX: got channel %p\n", __func__,
  385. host->chan_rx);
  386. if (!host->chan_rx)
  387. goto erqrx;
  388. if (pdata)
  389. cfg.slave_id = pdata->slave_id_rx;
  390. cfg.direction = DMA_DEV_TO_MEM;
  391. cfg.dst_addr = 0;
  392. cfg.src_addr = res->start + MMCIF_CE_DATA;
  393. ret = dmaengine_slave_config(host->chan_rx, &cfg);
  394. if (ret < 0)
  395. goto ecfgrx;
  396. return;
  397. ecfgrx:
  398. dma_release_channel(host->chan_rx);
  399. host->chan_rx = NULL;
  400. erqrx:
  401. ecfgtx:
  402. dma_release_channel(host->chan_tx);
  403. host->chan_tx = NULL;
  404. }
  405. static void sh_mmcif_release_dma(struct sh_mmcif_host *host)
  406. {
  407. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  408. /* Descriptors are freed automatically */
  409. if (host->chan_tx) {
  410. struct dma_chan *chan = host->chan_tx;
  411. host->chan_tx = NULL;
  412. dma_release_channel(chan);
  413. }
  414. if (host->chan_rx) {
  415. struct dma_chan *chan = host->chan_rx;
  416. host->chan_rx = NULL;
  417. dma_release_channel(chan);
  418. }
  419. host->dma_active = false;
  420. }
  421. static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
  422. {
  423. struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
  424. bool sup_pclk = p ? p->sup_pclk : false;
  425. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  426. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR);
  427. if (!clk)
  428. return;
  429. if (sup_pclk && clk == host->clk)
  430. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK);
  431. else
  432. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR &
  433. ((fls(DIV_ROUND_UP(host->clk,
  434. clk) - 1) - 1) << 16));
  435. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  436. }
  437. static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
  438. {
  439. u32 tmp;
  440. tmp = 0x010f0000 & sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL);
  441. sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_ON);
  442. sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_OFF);
  443. if (host->ccs_enable)
  444. tmp |= SCCSTO_29;
  445. if (host->clk_ctrl2_enable)
  446. sh_mmcif_writel(host->addr, MMCIF_CE_CLK_CTRL2, 0x0F0F0000);
  447. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp |
  448. SRSPTO_256 | SRBSYTO_29 | SRWDTO_29);
  449. /* byte swap on */
  450. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
  451. }
  452. static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
  453. {
  454. u32 state1, state2;
  455. int ret, timeout;
  456. host->sd_error = false;
  457. state1 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1);
  458. state2 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS2);
  459. dev_dbg(&host->pd->dev, "ERR HOST_STS1 = %08x\n", state1);
  460. dev_dbg(&host->pd->dev, "ERR HOST_STS2 = %08x\n", state2);
  461. if (state1 & STS1_CMDSEQ) {
  462. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK);
  463. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK);
  464. for (timeout = 10000000; timeout; timeout--) {
  465. if (!(sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1)
  466. & STS1_CMDSEQ))
  467. break;
  468. mdelay(1);
  469. }
  470. if (!timeout) {
  471. dev_err(&host->pd->dev,
  472. "Forced end of command sequence timeout err\n");
  473. return -EIO;
  474. }
  475. sh_mmcif_sync_reset(host);
  476. dev_dbg(&host->pd->dev, "Forced end of command sequence\n");
  477. return -EIO;
  478. }
  479. if (state2 & STS2_CRC_ERR) {
  480. dev_err(&host->pd->dev, " CRC error: state %u, wait %u\n",
  481. host->state, host->wait_for);
  482. ret = -EIO;
  483. } else if (state2 & STS2_TIMEOUT_ERR) {
  484. dev_err(&host->pd->dev, " Timeout: state %u, wait %u\n",
  485. host->state, host->wait_for);
  486. ret = -ETIMEDOUT;
  487. } else {
  488. dev_dbg(&host->pd->dev, " End/Index error: state %u, wait %u\n",
  489. host->state, host->wait_for);
  490. ret = -EIO;
  491. }
  492. return ret;
  493. }
  494. static bool sh_mmcif_next_block(struct sh_mmcif_host *host, u32 *p)
  495. {
  496. struct mmc_data *data = host->mrq->data;
  497. host->sg_blkidx += host->blocksize;
  498. /* data->sg->length must be a multiple of host->blocksize? */
  499. BUG_ON(host->sg_blkidx > data->sg->length);
  500. if (host->sg_blkidx == data->sg->length) {
  501. host->sg_blkidx = 0;
  502. if (++host->sg_idx < data->sg_len)
  503. host->pio_ptr = sg_virt(++data->sg);
  504. } else {
  505. host->pio_ptr = p;
  506. }
  507. return host->sg_idx != data->sg_len;
  508. }
  509. static void sh_mmcif_single_read(struct sh_mmcif_host *host,
  510. struct mmc_request *mrq)
  511. {
  512. host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  513. BLOCK_SIZE_MASK) + 3;
  514. host->wait_for = MMCIF_WAIT_FOR_READ;
  515. /* buf read enable */
  516. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  517. }
  518. static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
  519. {
  520. struct mmc_data *data = host->mrq->data;
  521. u32 *p = sg_virt(data->sg);
  522. int i;
  523. if (host->sd_error) {
  524. data->error = sh_mmcif_error_manage(host);
  525. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  526. return false;
  527. }
  528. for (i = 0; i < host->blocksize / 4; i++)
  529. *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
  530. /* buffer read end */
  531. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
  532. host->wait_for = MMCIF_WAIT_FOR_READ_END;
  533. return true;
  534. }
  535. static void sh_mmcif_multi_read(struct sh_mmcif_host *host,
  536. struct mmc_request *mrq)
  537. {
  538. struct mmc_data *data = mrq->data;
  539. if (!data->sg_len || !data->sg->length)
  540. return;
  541. host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  542. BLOCK_SIZE_MASK;
  543. host->wait_for = MMCIF_WAIT_FOR_MREAD;
  544. host->sg_idx = 0;
  545. host->sg_blkidx = 0;
  546. host->pio_ptr = sg_virt(data->sg);
  547. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  548. }
  549. static bool sh_mmcif_mread_block(struct sh_mmcif_host *host)
  550. {
  551. struct mmc_data *data = host->mrq->data;
  552. u32 *p = host->pio_ptr;
  553. int i;
  554. if (host->sd_error) {
  555. data->error = sh_mmcif_error_manage(host);
  556. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  557. return false;
  558. }
  559. BUG_ON(!data->sg->length);
  560. for (i = 0; i < host->blocksize / 4; i++)
  561. *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
  562. if (!sh_mmcif_next_block(host, p))
  563. return false;
  564. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  565. return true;
  566. }
  567. static void sh_mmcif_single_write(struct sh_mmcif_host *host,
  568. struct mmc_request *mrq)
  569. {
  570. host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  571. BLOCK_SIZE_MASK) + 3;
  572. host->wait_for = MMCIF_WAIT_FOR_WRITE;
  573. /* buf write enable */
  574. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  575. }
  576. static bool sh_mmcif_write_block(struct sh_mmcif_host *host)
  577. {
  578. struct mmc_data *data = host->mrq->data;
  579. u32 *p = sg_virt(data->sg);
  580. int i;
  581. if (host->sd_error) {
  582. data->error = sh_mmcif_error_manage(host);
  583. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  584. return false;
  585. }
  586. for (i = 0; i < host->blocksize / 4; i++)
  587. sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
  588. /* buffer write end */
  589. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
  590. host->wait_for = MMCIF_WAIT_FOR_WRITE_END;
  591. return true;
  592. }
  593. static void sh_mmcif_multi_write(struct sh_mmcif_host *host,
  594. struct mmc_request *mrq)
  595. {
  596. struct mmc_data *data = mrq->data;
  597. if (!data->sg_len || !data->sg->length)
  598. return;
  599. host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  600. BLOCK_SIZE_MASK;
  601. host->wait_for = MMCIF_WAIT_FOR_MWRITE;
  602. host->sg_idx = 0;
  603. host->sg_blkidx = 0;
  604. host->pio_ptr = sg_virt(data->sg);
  605. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  606. }
  607. static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host)
  608. {
  609. struct mmc_data *data = host->mrq->data;
  610. u32 *p = host->pio_ptr;
  611. int i;
  612. if (host->sd_error) {
  613. data->error = sh_mmcif_error_manage(host);
  614. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  615. return false;
  616. }
  617. BUG_ON(!data->sg->length);
  618. for (i = 0; i < host->blocksize / 4; i++)
  619. sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
  620. if (!sh_mmcif_next_block(host, p))
  621. return false;
  622. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  623. return true;
  624. }
  625. static void sh_mmcif_get_response(struct sh_mmcif_host *host,
  626. struct mmc_command *cmd)
  627. {
  628. if (cmd->flags & MMC_RSP_136) {
  629. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP3);
  630. cmd->resp[1] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP2);
  631. cmd->resp[2] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP1);
  632. cmd->resp[3] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
  633. } else
  634. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
  635. }
  636. static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
  637. struct mmc_command *cmd)
  638. {
  639. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP_CMD12);
  640. }
  641. static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
  642. struct mmc_request *mrq)
  643. {
  644. struct mmc_data *data = mrq->data;
  645. struct mmc_command *cmd = mrq->cmd;
  646. u32 opc = cmd->opcode;
  647. u32 tmp = 0;
  648. /* Response Type check */
  649. switch (mmc_resp_type(cmd)) {
  650. case MMC_RSP_NONE:
  651. tmp |= CMD_SET_RTYP_NO;
  652. break;
  653. case MMC_RSP_R1:
  654. case MMC_RSP_R1B:
  655. case MMC_RSP_R3:
  656. tmp |= CMD_SET_RTYP_6B;
  657. break;
  658. case MMC_RSP_R2:
  659. tmp |= CMD_SET_RTYP_17B;
  660. break;
  661. default:
  662. dev_err(&host->pd->dev, "Unsupported response type.\n");
  663. break;
  664. }
  665. switch (opc) {
  666. /* RBSY */
  667. case MMC_SLEEP_AWAKE:
  668. case MMC_SWITCH:
  669. case MMC_STOP_TRANSMISSION:
  670. case MMC_SET_WRITE_PROT:
  671. case MMC_CLR_WRITE_PROT:
  672. case MMC_ERASE:
  673. tmp |= CMD_SET_RBSY;
  674. break;
  675. }
  676. /* WDAT / DATW */
  677. if (data) {
  678. tmp |= CMD_SET_WDAT;
  679. switch (host->bus_width) {
  680. case MMC_BUS_WIDTH_1:
  681. tmp |= CMD_SET_DATW_1;
  682. break;
  683. case MMC_BUS_WIDTH_4:
  684. tmp |= CMD_SET_DATW_4;
  685. break;
  686. case MMC_BUS_WIDTH_8:
  687. tmp |= CMD_SET_DATW_8;
  688. break;
  689. default:
  690. dev_err(&host->pd->dev, "Unsupported bus width.\n");
  691. break;
  692. }
  693. switch (host->timing) {
  694. case MMC_TIMING_UHS_DDR50:
  695. /*
  696. * MMC core will only set this timing, if the host
  697. * advertises the MMC_CAP_UHS_DDR50 capability. MMCIF
  698. * implementations with this capability, e.g. sh73a0,
  699. * will have to set it in their platform data.
  700. */
  701. tmp |= CMD_SET_DARS;
  702. break;
  703. }
  704. }
  705. /* DWEN */
  706. if (opc == MMC_WRITE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK)
  707. tmp |= CMD_SET_DWEN;
  708. /* CMLTE/CMD12EN */
  709. if (opc == MMC_READ_MULTIPLE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) {
  710. tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
  711. sh_mmcif_bitset(host, MMCIF_CE_BLOCK_SET,
  712. data->blocks << 16);
  713. }
  714. /* RIDXC[1:0] check bits */
  715. if (opc == MMC_SEND_OP_COND || opc == MMC_ALL_SEND_CID ||
  716. opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
  717. tmp |= CMD_SET_RIDXC_BITS;
  718. /* RCRC7C[1:0] check bits */
  719. if (opc == MMC_SEND_OP_COND)
  720. tmp |= CMD_SET_CRC7C_BITS;
  721. /* RCRC7C[1:0] internal CRC7 */
  722. if (opc == MMC_ALL_SEND_CID ||
  723. opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
  724. tmp |= CMD_SET_CRC7C_INTERNAL;
  725. return (opc << 24) | tmp;
  726. }
  727. static int sh_mmcif_data_trans(struct sh_mmcif_host *host,
  728. struct mmc_request *mrq, u32 opc)
  729. {
  730. switch (opc) {
  731. case MMC_READ_MULTIPLE_BLOCK:
  732. sh_mmcif_multi_read(host, mrq);
  733. return 0;
  734. case MMC_WRITE_MULTIPLE_BLOCK:
  735. sh_mmcif_multi_write(host, mrq);
  736. return 0;
  737. case MMC_WRITE_BLOCK:
  738. sh_mmcif_single_write(host, mrq);
  739. return 0;
  740. case MMC_READ_SINGLE_BLOCK:
  741. case MMC_SEND_EXT_CSD:
  742. sh_mmcif_single_read(host, mrq);
  743. return 0;
  744. default:
  745. dev_err(&host->pd->dev, "Unsupported CMD%d\n", opc);
  746. return -EINVAL;
  747. }
  748. }
  749. static void sh_mmcif_start_cmd(struct sh_mmcif_host *host,
  750. struct mmc_request *mrq)
  751. {
  752. struct mmc_command *cmd = mrq->cmd;
  753. u32 opc = cmd->opcode;
  754. u32 mask;
  755. switch (opc) {
  756. /* response busy check */
  757. case MMC_SLEEP_AWAKE:
  758. case MMC_SWITCH:
  759. case MMC_STOP_TRANSMISSION:
  760. case MMC_SET_WRITE_PROT:
  761. case MMC_CLR_WRITE_PROT:
  762. case MMC_ERASE:
  763. mask = MASK_START_CMD | MASK_MRBSYE;
  764. break;
  765. default:
  766. mask = MASK_START_CMD | MASK_MCRSPE;
  767. break;
  768. }
  769. if (host->ccs_enable)
  770. mask |= MASK_MCCSTO;
  771. if (mrq->data) {
  772. sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, 0);
  773. sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET,
  774. mrq->data->blksz);
  775. }
  776. opc = sh_mmcif_set_cmd(host, mrq);
  777. if (host->ccs_enable)
  778. sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0);
  779. else
  780. sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0 | INT_CCS);
  781. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, mask);
  782. /* set arg */
  783. sh_mmcif_writel(host->addr, MMCIF_CE_ARG, cmd->arg);
  784. /* set cmd */
  785. sh_mmcif_writel(host->addr, MMCIF_CE_CMD_SET, opc);
  786. host->wait_for = MMCIF_WAIT_FOR_CMD;
  787. schedule_delayed_work(&host->timeout_work, host->timeout);
  788. }
  789. static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host,
  790. struct mmc_request *mrq)
  791. {
  792. switch (mrq->cmd->opcode) {
  793. case MMC_READ_MULTIPLE_BLOCK:
  794. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
  795. break;
  796. case MMC_WRITE_MULTIPLE_BLOCK:
  797. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
  798. break;
  799. default:
  800. dev_err(&host->pd->dev, "unsupported stop cmd\n");
  801. mrq->stop->error = sh_mmcif_error_manage(host);
  802. return;
  803. }
  804. host->wait_for = MMCIF_WAIT_FOR_STOP;
  805. }
  806. static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq)
  807. {
  808. struct sh_mmcif_host *host = mmc_priv(mmc);
  809. unsigned long flags;
  810. spin_lock_irqsave(&host->lock, flags);
  811. if (host->state != STATE_IDLE) {
  812. dev_dbg(&host->pd->dev, "%s() rejected, state %u\n", __func__, host->state);
  813. spin_unlock_irqrestore(&host->lock, flags);
  814. mrq->cmd->error = -EAGAIN;
  815. mmc_request_done(mmc, mrq);
  816. return;
  817. }
  818. host->state = STATE_REQUEST;
  819. spin_unlock_irqrestore(&host->lock, flags);
  820. switch (mrq->cmd->opcode) {
  821. /* MMCIF does not support SD/SDIO command */
  822. case MMC_SLEEP_AWAKE: /* = SD_IO_SEND_OP_COND (5) */
  823. case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
  824. if ((mrq->cmd->flags & MMC_CMD_MASK) != MMC_CMD_BCR)
  825. break;
  826. case MMC_APP_CMD:
  827. case SD_IO_RW_DIRECT:
  828. host->state = STATE_IDLE;
  829. mrq->cmd->error = -ETIMEDOUT;
  830. mmc_request_done(mmc, mrq);
  831. return;
  832. default:
  833. break;
  834. }
  835. host->mrq = mrq;
  836. sh_mmcif_start_cmd(host, mrq);
  837. }
  838. static int sh_mmcif_clk_update(struct sh_mmcif_host *host)
  839. {
  840. int ret = clk_enable(host->hclk);
  841. if (!ret) {
  842. host->clk = clk_get_rate(host->hclk);
  843. host->mmc->f_max = host->clk / 2;
  844. host->mmc->f_min = host->clk / 512;
  845. }
  846. return ret;
  847. }
  848. static void sh_mmcif_set_power(struct sh_mmcif_host *host, struct mmc_ios *ios)
  849. {
  850. struct mmc_host *mmc = host->mmc;
  851. if (!IS_ERR(mmc->supply.vmmc))
  852. /* Errors ignored... */
  853. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
  854. ios->power_mode ? ios->vdd : 0);
  855. }
  856. static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  857. {
  858. struct sh_mmcif_host *host = mmc_priv(mmc);
  859. unsigned long flags;
  860. spin_lock_irqsave(&host->lock, flags);
  861. if (host->state != STATE_IDLE) {
  862. dev_dbg(&host->pd->dev, "%s() rejected, state %u\n", __func__, host->state);
  863. spin_unlock_irqrestore(&host->lock, flags);
  864. return;
  865. }
  866. host->state = STATE_IOS;
  867. spin_unlock_irqrestore(&host->lock, flags);
  868. if (ios->power_mode == MMC_POWER_UP) {
  869. if (!host->card_present) {
  870. /* See if we also get DMA */
  871. sh_mmcif_request_dma(host, host->pd->dev.platform_data);
  872. host->card_present = true;
  873. }
  874. sh_mmcif_set_power(host, ios);
  875. } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
  876. /* clock stop */
  877. sh_mmcif_clock_control(host, 0);
  878. if (ios->power_mode == MMC_POWER_OFF) {
  879. if (host->card_present) {
  880. sh_mmcif_release_dma(host);
  881. host->card_present = false;
  882. }
  883. }
  884. if (host->power) {
  885. pm_runtime_put_sync(&host->pd->dev);
  886. clk_disable(host->hclk);
  887. host->power = false;
  888. if (ios->power_mode == MMC_POWER_OFF)
  889. sh_mmcif_set_power(host, ios);
  890. }
  891. host->state = STATE_IDLE;
  892. return;
  893. }
  894. if (ios->clock) {
  895. if (!host->power) {
  896. sh_mmcif_clk_update(host);
  897. pm_runtime_get_sync(&host->pd->dev);
  898. host->power = true;
  899. sh_mmcif_sync_reset(host);
  900. }
  901. sh_mmcif_clock_control(host, ios->clock);
  902. }
  903. host->timing = ios->timing;
  904. host->bus_width = ios->bus_width;
  905. host->state = STATE_IDLE;
  906. }
  907. static int sh_mmcif_get_cd(struct mmc_host *mmc)
  908. {
  909. struct sh_mmcif_host *host = mmc_priv(mmc);
  910. struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
  911. int ret = mmc_gpio_get_cd(mmc);
  912. if (ret >= 0)
  913. return ret;
  914. if (!p || !p->get_cd)
  915. return -ENOSYS;
  916. else
  917. return p->get_cd(host->pd);
  918. }
  919. static struct mmc_host_ops sh_mmcif_ops = {
  920. .request = sh_mmcif_request,
  921. .set_ios = sh_mmcif_set_ios,
  922. .get_cd = sh_mmcif_get_cd,
  923. };
  924. static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host)
  925. {
  926. struct mmc_command *cmd = host->mrq->cmd;
  927. struct mmc_data *data = host->mrq->data;
  928. long time;
  929. if (host->sd_error) {
  930. switch (cmd->opcode) {
  931. case MMC_ALL_SEND_CID:
  932. case MMC_SELECT_CARD:
  933. case MMC_APP_CMD:
  934. cmd->error = -ETIMEDOUT;
  935. break;
  936. default:
  937. cmd->error = sh_mmcif_error_manage(host);
  938. break;
  939. }
  940. dev_dbg(&host->pd->dev, "CMD%d error %d\n",
  941. cmd->opcode, cmd->error);
  942. host->sd_error = false;
  943. return false;
  944. }
  945. if (!(cmd->flags & MMC_RSP_PRESENT)) {
  946. cmd->error = 0;
  947. return false;
  948. }
  949. sh_mmcif_get_response(host, cmd);
  950. if (!data)
  951. return false;
  952. /*
  953. * Completion can be signalled from DMA callback and error, so, have to
  954. * reset here, before setting .dma_active
  955. */
  956. init_completion(&host->dma_complete);
  957. if (data->flags & MMC_DATA_READ) {
  958. if (host->chan_rx)
  959. sh_mmcif_start_dma_rx(host);
  960. } else {
  961. if (host->chan_tx)
  962. sh_mmcif_start_dma_tx(host);
  963. }
  964. if (!host->dma_active) {
  965. data->error = sh_mmcif_data_trans(host, host->mrq, cmd->opcode);
  966. return !data->error;
  967. }
  968. /* Running in the IRQ thread, can sleep */
  969. time = wait_for_completion_interruptible_timeout(&host->dma_complete,
  970. host->timeout);
  971. if (data->flags & MMC_DATA_READ)
  972. dma_unmap_sg(host->chan_rx->device->dev,
  973. data->sg, data->sg_len,
  974. DMA_FROM_DEVICE);
  975. else
  976. dma_unmap_sg(host->chan_tx->device->dev,
  977. data->sg, data->sg_len,
  978. DMA_TO_DEVICE);
  979. if (host->sd_error) {
  980. dev_err(host->mmc->parent,
  981. "Error IRQ while waiting for DMA completion!\n");
  982. /* Woken up by an error IRQ: abort DMA */
  983. data->error = sh_mmcif_error_manage(host);
  984. } else if (!time) {
  985. dev_err(host->mmc->parent, "DMA timeout!\n");
  986. data->error = -ETIMEDOUT;
  987. } else if (time < 0) {
  988. dev_err(host->mmc->parent,
  989. "wait_for_completion_...() error %ld!\n", time);
  990. data->error = time;
  991. }
  992. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC,
  993. BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  994. host->dma_active = false;
  995. if (data->error) {
  996. data->bytes_xfered = 0;
  997. /* Abort DMA */
  998. if (data->flags & MMC_DATA_READ)
  999. dmaengine_terminate_all(host->chan_rx);
  1000. else
  1001. dmaengine_terminate_all(host->chan_tx);
  1002. }
  1003. return false;
  1004. }
  1005. static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)
  1006. {
  1007. struct sh_mmcif_host *host = dev_id;
  1008. struct mmc_request *mrq;
  1009. bool wait = false;
  1010. cancel_delayed_work_sync(&host->timeout_work);
  1011. mutex_lock(&host->thread_lock);
  1012. mrq = host->mrq;
  1013. if (!mrq) {
  1014. dev_dbg(&host->pd->dev, "IRQ thread state %u, wait %u: NULL mrq!\n",
  1015. host->state, host->wait_for);
  1016. mutex_unlock(&host->thread_lock);
  1017. return IRQ_HANDLED;
  1018. }
  1019. /*
  1020. * All handlers return true, if processing continues, and false, if the
  1021. * request has to be completed - successfully or not
  1022. */
  1023. switch (host->wait_for) {
  1024. case MMCIF_WAIT_FOR_REQUEST:
  1025. /* We're too late, the timeout has already kicked in */
  1026. mutex_unlock(&host->thread_lock);
  1027. return IRQ_HANDLED;
  1028. case MMCIF_WAIT_FOR_CMD:
  1029. /* Wait for data? */
  1030. wait = sh_mmcif_end_cmd(host);
  1031. break;
  1032. case MMCIF_WAIT_FOR_MREAD:
  1033. /* Wait for more data? */
  1034. wait = sh_mmcif_mread_block(host);
  1035. break;
  1036. case MMCIF_WAIT_FOR_READ:
  1037. /* Wait for data end? */
  1038. wait = sh_mmcif_read_block(host);
  1039. break;
  1040. case MMCIF_WAIT_FOR_MWRITE:
  1041. /* Wait data to write? */
  1042. wait = sh_mmcif_mwrite_block(host);
  1043. break;
  1044. case MMCIF_WAIT_FOR_WRITE:
  1045. /* Wait for data end? */
  1046. wait = sh_mmcif_write_block(host);
  1047. break;
  1048. case MMCIF_WAIT_FOR_STOP:
  1049. if (host->sd_error) {
  1050. mrq->stop->error = sh_mmcif_error_manage(host);
  1051. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, mrq->stop->error);
  1052. break;
  1053. }
  1054. sh_mmcif_get_cmd12response(host, mrq->stop);
  1055. mrq->stop->error = 0;
  1056. break;
  1057. case MMCIF_WAIT_FOR_READ_END:
  1058. case MMCIF_WAIT_FOR_WRITE_END:
  1059. if (host->sd_error) {
  1060. mrq->data->error = sh_mmcif_error_manage(host);
  1061. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, mrq->data->error);
  1062. }
  1063. break;
  1064. default:
  1065. BUG();
  1066. }
  1067. if (wait) {
  1068. schedule_delayed_work(&host->timeout_work, host->timeout);
  1069. /* Wait for more data */
  1070. mutex_unlock(&host->thread_lock);
  1071. return IRQ_HANDLED;
  1072. }
  1073. if (host->wait_for != MMCIF_WAIT_FOR_STOP) {
  1074. struct mmc_data *data = mrq->data;
  1075. if (!mrq->cmd->error && data && !data->error)
  1076. data->bytes_xfered =
  1077. data->blocks * data->blksz;
  1078. if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) {
  1079. sh_mmcif_stop_cmd(host, mrq);
  1080. if (!mrq->stop->error) {
  1081. schedule_delayed_work(&host->timeout_work, host->timeout);
  1082. mutex_unlock(&host->thread_lock);
  1083. return IRQ_HANDLED;
  1084. }
  1085. }
  1086. }
  1087. host->wait_for = MMCIF_WAIT_FOR_REQUEST;
  1088. host->state = STATE_IDLE;
  1089. host->mrq = NULL;
  1090. mmc_request_done(host->mmc, mrq);
  1091. mutex_unlock(&host->thread_lock);
  1092. return IRQ_HANDLED;
  1093. }
  1094. static irqreturn_t sh_mmcif_intr(int irq, void *dev_id)
  1095. {
  1096. struct sh_mmcif_host *host = dev_id;
  1097. u32 state, mask;
  1098. state = sh_mmcif_readl(host->addr, MMCIF_CE_INT);
  1099. mask = sh_mmcif_readl(host->addr, MMCIF_CE_INT_MASK);
  1100. if (host->ccs_enable)
  1101. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~(state & mask));
  1102. else
  1103. sh_mmcif_writel(host->addr, MMCIF_CE_INT, INT_CCS | ~(state & mask));
  1104. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state & MASK_CLEAN);
  1105. if (state & ~MASK_CLEAN)
  1106. dev_dbg(&host->pd->dev, "IRQ state = 0x%08x incompletely cleared\n",
  1107. state);
  1108. if (state & INT_ERR_STS || state & ~INT_ALL) {
  1109. host->sd_error = true;
  1110. dev_dbg(&host->pd->dev, "int err state = 0x%08x\n", state);
  1111. }
  1112. if (state & ~(INT_CMD12RBE | INT_CMD12CRE)) {
  1113. if (!host->mrq)
  1114. dev_dbg(&host->pd->dev, "NULL IRQ state = 0x%08x\n", state);
  1115. if (!host->dma_active)
  1116. return IRQ_WAKE_THREAD;
  1117. else if (host->sd_error)
  1118. mmcif_dma_complete(host);
  1119. } else {
  1120. dev_dbg(&host->pd->dev, "Unexpected IRQ 0x%x\n", state);
  1121. }
  1122. return IRQ_HANDLED;
  1123. }
  1124. static void mmcif_timeout_work(struct work_struct *work)
  1125. {
  1126. struct delayed_work *d = container_of(work, struct delayed_work, work);
  1127. struct sh_mmcif_host *host = container_of(d, struct sh_mmcif_host, timeout_work);
  1128. struct mmc_request *mrq = host->mrq;
  1129. unsigned long flags;
  1130. if (host->dying)
  1131. /* Don't run after mmc_remove_host() */
  1132. return;
  1133. dev_err(&host->pd->dev, "Timeout waiting for %u on CMD%u\n",
  1134. host->wait_for, mrq->cmd->opcode);
  1135. spin_lock_irqsave(&host->lock, flags);
  1136. if (host->state == STATE_IDLE) {
  1137. spin_unlock_irqrestore(&host->lock, flags);
  1138. return;
  1139. }
  1140. host->state = STATE_TIMEOUT;
  1141. spin_unlock_irqrestore(&host->lock, flags);
  1142. /*
  1143. * Handle races with cancel_delayed_work(), unless
  1144. * cancel_delayed_work_sync() is used
  1145. */
  1146. switch (host->wait_for) {
  1147. case MMCIF_WAIT_FOR_CMD:
  1148. mrq->cmd->error = sh_mmcif_error_manage(host);
  1149. break;
  1150. case MMCIF_WAIT_FOR_STOP:
  1151. mrq->stop->error = sh_mmcif_error_manage(host);
  1152. break;
  1153. case MMCIF_WAIT_FOR_MREAD:
  1154. case MMCIF_WAIT_FOR_MWRITE:
  1155. case MMCIF_WAIT_FOR_READ:
  1156. case MMCIF_WAIT_FOR_WRITE:
  1157. case MMCIF_WAIT_FOR_READ_END:
  1158. case MMCIF_WAIT_FOR_WRITE_END:
  1159. mrq->data->error = sh_mmcif_error_manage(host);
  1160. break;
  1161. default:
  1162. BUG();
  1163. }
  1164. host->state = STATE_IDLE;
  1165. host->wait_for = MMCIF_WAIT_FOR_REQUEST;
  1166. host->mrq = NULL;
  1167. mmc_request_done(host->mmc, mrq);
  1168. }
  1169. static void sh_mmcif_init_ocr(struct sh_mmcif_host *host)
  1170. {
  1171. struct sh_mmcif_plat_data *pd = host->pd->dev.platform_data;
  1172. struct mmc_host *mmc = host->mmc;
  1173. mmc_regulator_get_supply(mmc);
  1174. if (!pd)
  1175. return;
  1176. if (!mmc->ocr_avail)
  1177. mmc->ocr_avail = pd->ocr;
  1178. else if (pd->ocr)
  1179. dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
  1180. }
  1181. static int sh_mmcif_probe(struct platform_device *pdev)
  1182. {
  1183. int ret = 0, irq[2];
  1184. struct mmc_host *mmc;
  1185. struct sh_mmcif_host *host;
  1186. struct sh_mmcif_plat_data *pd = pdev->dev.platform_data;
  1187. struct resource *res;
  1188. void __iomem *reg;
  1189. const char *name;
  1190. irq[0] = platform_get_irq(pdev, 0);
  1191. irq[1] = platform_get_irq(pdev, 1);
  1192. if (irq[0] < 0) {
  1193. dev_err(&pdev->dev, "Get irq error\n");
  1194. return -ENXIO;
  1195. }
  1196. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1197. if (!res) {
  1198. dev_err(&pdev->dev, "platform_get_resource error.\n");
  1199. return -ENXIO;
  1200. }
  1201. reg = ioremap(res->start, resource_size(res));
  1202. if (!reg) {
  1203. dev_err(&pdev->dev, "ioremap error.\n");
  1204. return -ENOMEM;
  1205. }
  1206. mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), &pdev->dev);
  1207. if (!mmc) {
  1208. ret = -ENOMEM;
  1209. goto ealloch;
  1210. }
  1211. ret = mmc_of_parse(mmc);
  1212. if (ret < 0)
  1213. goto eofparse;
  1214. host = mmc_priv(mmc);
  1215. host->mmc = mmc;
  1216. host->addr = reg;
  1217. host->timeout = msecs_to_jiffies(1000);
  1218. host->ccs_enable = !pd || !pd->ccs_unsupported;
  1219. host->clk_ctrl2_enable = pd && pd->clk_ctrl2_present;
  1220. host->pd = pdev;
  1221. spin_lock_init(&host->lock);
  1222. mmc->ops = &sh_mmcif_ops;
  1223. sh_mmcif_init_ocr(host);
  1224. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_WAIT_WHILE_BUSY;
  1225. if (pd && pd->caps)
  1226. mmc->caps |= pd->caps;
  1227. mmc->max_segs = 32;
  1228. mmc->max_blk_size = 512;
  1229. mmc->max_req_size = PAGE_CACHE_SIZE * mmc->max_segs;
  1230. mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size;
  1231. mmc->max_seg_size = mmc->max_req_size;
  1232. platform_set_drvdata(pdev, host);
  1233. pm_runtime_enable(&pdev->dev);
  1234. host->power = false;
  1235. host->hclk = clk_get(&pdev->dev, NULL);
  1236. if (IS_ERR(host->hclk)) {
  1237. ret = PTR_ERR(host->hclk);
  1238. dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
  1239. goto eclkget;
  1240. }
  1241. ret = sh_mmcif_clk_update(host);
  1242. if (ret < 0)
  1243. goto eclkupdate;
  1244. ret = pm_runtime_resume(&pdev->dev);
  1245. if (ret < 0)
  1246. goto eresume;
  1247. INIT_DELAYED_WORK(&host->timeout_work, mmcif_timeout_work);
  1248. sh_mmcif_sync_reset(host);
  1249. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1250. name = irq[1] < 0 ? dev_name(&pdev->dev) : "sh_mmc:error";
  1251. ret = request_threaded_irq(irq[0], sh_mmcif_intr, sh_mmcif_irqt, 0, name, host);
  1252. if (ret) {
  1253. dev_err(&pdev->dev, "request_irq error (%s)\n", name);
  1254. goto ereqirq0;
  1255. }
  1256. if (irq[1] >= 0) {
  1257. ret = request_threaded_irq(irq[1], sh_mmcif_intr, sh_mmcif_irqt,
  1258. 0, "sh_mmc:int", host);
  1259. if (ret) {
  1260. dev_err(&pdev->dev, "request_irq error (sh_mmc:int)\n");
  1261. goto ereqirq1;
  1262. }
  1263. }
  1264. if (pd && pd->use_cd_gpio) {
  1265. ret = mmc_gpio_request_cd(mmc, pd->cd_gpio, 0);
  1266. if (ret < 0)
  1267. goto erqcd;
  1268. }
  1269. mutex_init(&host->thread_lock);
  1270. clk_disable(host->hclk);
  1271. ret = mmc_add_host(mmc);
  1272. if (ret < 0)
  1273. goto emmcaddh;
  1274. dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
  1275. dev_info(&pdev->dev, "driver version %s\n", DRIVER_VERSION);
  1276. dev_dbg(&pdev->dev, "chip ver H'%04x\n",
  1277. sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0x0000ffff);
  1278. return ret;
  1279. emmcaddh:
  1280. erqcd:
  1281. if (irq[1] >= 0)
  1282. free_irq(irq[1], host);
  1283. ereqirq1:
  1284. free_irq(irq[0], host);
  1285. ereqirq0:
  1286. pm_runtime_suspend(&pdev->dev);
  1287. eresume:
  1288. clk_disable(host->hclk);
  1289. eclkupdate:
  1290. clk_put(host->hclk);
  1291. eclkget:
  1292. pm_runtime_disable(&pdev->dev);
  1293. eofparse:
  1294. mmc_free_host(mmc);
  1295. ealloch:
  1296. iounmap(reg);
  1297. return ret;
  1298. }
  1299. static int sh_mmcif_remove(struct platform_device *pdev)
  1300. {
  1301. struct sh_mmcif_host *host = platform_get_drvdata(pdev);
  1302. int irq[2];
  1303. host->dying = true;
  1304. clk_enable(host->hclk);
  1305. pm_runtime_get_sync(&pdev->dev);
  1306. dev_pm_qos_hide_latency_limit(&pdev->dev);
  1307. mmc_remove_host(host->mmc);
  1308. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1309. /*
  1310. * FIXME: cancel_delayed_work(_sync)() and free_irq() race with the
  1311. * mmc_remove_host() call above. But swapping order doesn't help either
  1312. * (a query on the linux-mmc mailing list didn't bring any replies).
  1313. */
  1314. cancel_delayed_work_sync(&host->timeout_work);
  1315. if (host->addr)
  1316. iounmap(host->addr);
  1317. irq[0] = platform_get_irq(pdev, 0);
  1318. irq[1] = platform_get_irq(pdev, 1);
  1319. free_irq(irq[0], host);
  1320. if (irq[1] >= 0)
  1321. free_irq(irq[1], host);
  1322. clk_disable(host->hclk);
  1323. mmc_free_host(host->mmc);
  1324. pm_runtime_put_sync(&pdev->dev);
  1325. pm_runtime_disable(&pdev->dev);
  1326. return 0;
  1327. }
  1328. #ifdef CONFIG_PM
  1329. static int sh_mmcif_suspend(struct device *dev)
  1330. {
  1331. struct sh_mmcif_host *host = dev_get_drvdata(dev);
  1332. int ret = mmc_suspend_host(host->mmc);
  1333. if (!ret)
  1334. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1335. return ret;
  1336. }
  1337. static int sh_mmcif_resume(struct device *dev)
  1338. {
  1339. struct sh_mmcif_host *host = dev_get_drvdata(dev);
  1340. return mmc_resume_host(host->mmc);
  1341. }
  1342. #else
  1343. #define sh_mmcif_suspend NULL
  1344. #define sh_mmcif_resume NULL
  1345. #endif /* CONFIG_PM */
  1346. static const struct of_device_id mmcif_of_match[] = {
  1347. { .compatible = "renesas,sh-mmcif" },
  1348. { }
  1349. };
  1350. MODULE_DEVICE_TABLE(of, mmcif_of_match);
  1351. static const struct dev_pm_ops sh_mmcif_dev_pm_ops = {
  1352. .suspend = sh_mmcif_suspend,
  1353. .resume = sh_mmcif_resume,
  1354. };
  1355. static struct platform_driver sh_mmcif_driver = {
  1356. .probe = sh_mmcif_probe,
  1357. .remove = sh_mmcif_remove,
  1358. .driver = {
  1359. .name = DRIVER_NAME,
  1360. .pm = &sh_mmcif_dev_pm_ops,
  1361. .owner = THIS_MODULE,
  1362. .of_match_table = mmcif_of_match,
  1363. },
  1364. };
  1365. module_platform_driver(sh_mmcif_driver);
  1366. MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver");
  1367. MODULE_LICENSE("GPL");
  1368. MODULE_ALIAS("platform:" DRIVER_NAME);
  1369. MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>");