sh_mmcif.c 40 KB

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