sh_mmcif.c 38 KB

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