sh_mmcif.c 40 KB

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