sh_mmcif.c 38 KB

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