sh_mmcif.c 39 KB

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