sh_mmcif.c 38 KB

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