sh_mmcif.c 39 KB

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