sh_mmcif.c 40 KB

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