sh_mmcif.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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. return;
  379. ecfgrx:
  380. dma_release_channel(host->chan_rx);
  381. host->chan_rx = NULL;
  382. erqrx:
  383. ecfgtx:
  384. dma_release_channel(host->chan_tx);
  385. host->chan_tx = NULL;
  386. }
  387. static void sh_mmcif_release_dma(struct sh_mmcif_host *host)
  388. {
  389. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  390. /* Descriptors are freed automatically */
  391. if (host->chan_tx) {
  392. struct dma_chan *chan = host->chan_tx;
  393. host->chan_tx = NULL;
  394. dma_release_channel(chan);
  395. }
  396. if (host->chan_rx) {
  397. struct dma_chan *chan = host->chan_rx;
  398. host->chan_rx = NULL;
  399. dma_release_channel(chan);
  400. }
  401. host->dma_active = false;
  402. }
  403. static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
  404. {
  405. struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
  406. bool sup_pclk = p ? p->sup_pclk : false;
  407. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  408. sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR);
  409. if (!clk)
  410. return;
  411. if (sup_pclk && clk == host->clk)
  412. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK);
  413. else
  414. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR &
  415. ((fls(DIV_ROUND_UP(host->clk,
  416. clk) - 1) - 1) << 16));
  417. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
  418. }
  419. static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
  420. {
  421. u32 tmp;
  422. tmp = 0x010f0000 & sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL);
  423. sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_ON);
  424. sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_OFF);
  425. sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp |
  426. SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 | SCCSTO_29);
  427. /* byte swap on */
  428. sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
  429. }
  430. static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
  431. {
  432. u32 state1, state2;
  433. int ret, timeout;
  434. host->sd_error = false;
  435. state1 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1);
  436. state2 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS2);
  437. dev_dbg(&host->pd->dev, "ERR HOST_STS1 = %08x\n", state1);
  438. dev_dbg(&host->pd->dev, "ERR HOST_STS2 = %08x\n", state2);
  439. if (state1 & STS1_CMDSEQ) {
  440. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK);
  441. sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK);
  442. for (timeout = 10000000; timeout; timeout--) {
  443. if (!(sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1)
  444. & STS1_CMDSEQ))
  445. break;
  446. mdelay(1);
  447. }
  448. if (!timeout) {
  449. dev_err(&host->pd->dev,
  450. "Forced end of command sequence timeout err\n");
  451. return -EIO;
  452. }
  453. sh_mmcif_sync_reset(host);
  454. dev_dbg(&host->pd->dev, "Forced end of command sequence\n");
  455. return -EIO;
  456. }
  457. if (state2 & STS2_CRC_ERR) {
  458. dev_err(&host->pd->dev, " CRC error: state %u, wait %u\n",
  459. host->state, host->wait_for);
  460. ret = -EIO;
  461. } else if (state2 & STS2_TIMEOUT_ERR) {
  462. dev_err(&host->pd->dev, " Timeout: state %u, wait %u\n",
  463. host->state, host->wait_for);
  464. ret = -ETIMEDOUT;
  465. } else {
  466. dev_dbg(&host->pd->dev, " End/Index error: state %u, wait %u\n",
  467. host->state, host->wait_for);
  468. ret = -EIO;
  469. }
  470. return ret;
  471. }
  472. static bool sh_mmcif_next_block(struct sh_mmcif_host *host, u32 *p)
  473. {
  474. struct mmc_data *data = host->mrq->data;
  475. host->sg_blkidx += host->blocksize;
  476. /* data->sg->length must be a multiple of host->blocksize? */
  477. BUG_ON(host->sg_blkidx > data->sg->length);
  478. if (host->sg_blkidx == data->sg->length) {
  479. host->sg_blkidx = 0;
  480. if (++host->sg_idx < data->sg_len)
  481. host->pio_ptr = sg_virt(++data->sg);
  482. } else {
  483. host->pio_ptr = p;
  484. }
  485. return host->sg_idx != data->sg_len;
  486. }
  487. static void sh_mmcif_single_read(struct sh_mmcif_host *host,
  488. struct mmc_request *mrq)
  489. {
  490. host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  491. BLOCK_SIZE_MASK) + 3;
  492. host->wait_for = MMCIF_WAIT_FOR_READ;
  493. /* buf read enable */
  494. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  495. }
  496. static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
  497. {
  498. struct mmc_data *data = host->mrq->data;
  499. u32 *p = sg_virt(data->sg);
  500. int i;
  501. if (host->sd_error) {
  502. data->error = sh_mmcif_error_manage(host);
  503. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  504. return false;
  505. }
  506. for (i = 0; i < host->blocksize / 4; i++)
  507. *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
  508. /* buffer read end */
  509. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
  510. host->wait_for = MMCIF_WAIT_FOR_READ_END;
  511. return true;
  512. }
  513. static void sh_mmcif_multi_read(struct sh_mmcif_host *host,
  514. struct mmc_request *mrq)
  515. {
  516. struct mmc_data *data = mrq->data;
  517. if (!data->sg_len || !data->sg->length)
  518. return;
  519. host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  520. BLOCK_SIZE_MASK;
  521. host->wait_for = MMCIF_WAIT_FOR_MREAD;
  522. host->sg_idx = 0;
  523. host->sg_blkidx = 0;
  524. host->pio_ptr = sg_virt(data->sg);
  525. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  526. }
  527. static bool sh_mmcif_mread_block(struct sh_mmcif_host *host)
  528. {
  529. struct mmc_data *data = host->mrq->data;
  530. u32 *p = host->pio_ptr;
  531. int i;
  532. if (host->sd_error) {
  533. data->error = sh_mmcif_error_manage(host);
  534. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  535. return false;
  536. }
  537. BUG_ON(!data->sg->length);
  538. for (i = 0; i < host->blocksize / 4; i++)
  539. *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
  540. if (!sh_mmcif_next_block(host, p))
  541. return false;
  542. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  543. return true;
  544. }
  545. static void sh_mmcif_single_write(struct sh_mmcif_host *host,
  546. struct mmc_request *mrq)
  547. {
  548. host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  549. BLOCK_SIZE_MASK) + 3;
  550. host->wait_for = MMCIF_WAIT_FOR_WRITE;
  551. /* buf write enable */
  552. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  553. }
  554. static bool sh_mmcif_write_block(struct sh_mmcif_host *host)
  555. {
  556. struct mmc_data *data = host->mrq->data;
  557. u32 *p = sg_virt(data->sg);
  558. int i;
  559. if (host->sd_error) {
  560. data->error = sh_mmcif_error_manage(host);
  561. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  562. return false;
  563. }
  564. for (i = 0; i < host->blocksize / 4; i++)
  565. sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
  566. /* buffer write end */
  567. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
  568. host->wait_for = MMCIF_WAIT_FOR_WRITE_END;
  569. return true;
  570. }
  571. static void sh_mmcif_multi_write(struct sh_mmcif_host *host,
  572. struct mmc_request *mrq)
  573. {
  574. struct mmc_data *data = mrq->data;
  575. if (!data->sg_len || !data->sg->length)
  576. return;
  577. host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
  578. BLOCK_SIZE_MASK;
  579. host->wait_for = MMCIF_WAIT_FOR_MWRITE;
  580. host->sg_idx = 0;
  581. host->sg_blkidx = 0;
  582. host->pio_ptr = sg_virt(data->sg);
  583. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  584. }
  585. static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host)
  586. {
  587. struct mmc_data *data = host->mrq->data;
  588. u32 *p = host->pio_ptr;
  589. int i;
  590. if (host->sd_error) {
  591. data->error = sh_mmcif_error_manage(host);
  592. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, data->error);
  593. return false;
  594. }
  595. BUG_ON(!data->sg->length);
  596. for (i = 0; i < host->blocksize / 4; i++)
  597. sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
  598. if (!sh_mmcif_next_block(host, p))
  599. return false;
  600. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  601. return true;
  602. }
  603. static void sh_mmcif_get_response(struct sh_mmcif_host *host,
  604. struct mmc_command *cmd)
  605. {
  606. if (cmd->flags & MMC_RSP_136) {
  607. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP3);
  608. cmd->resp[1] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP2);
  609. cmd->resp[2] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP1);
  610. cmd->resp[3] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
  611. } else
  612. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
  613. }
  614. static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
  615. struct mmc_command *cmd)
  616. {
  617. cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP_CMD12);
  618. }
  619. static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
  620. struct mmc_request *mrq)
  621. {
  622. struct mmc_data *data = mrq->data;
  623. struct mmc_command *cmd = mrq->cmd;
  624. u32 opc = cmd->opcode;
  625. u32 tmp = 0;
  626. /* Response Type check */
  627. switch (mmc_resp_type(cmd)) {
  628. case MMC_RSP_NONE:
  629. tmp |= CMD_SET_RTYP_NO;
  630. break;
  631. case MMC_RSP_R1:
  632. case MMC_RSP_R1B:
  633. case MMC_RSP_R3:
  634. tmp |= CMD_SET_RTYP_6B;
  635. break;
  636. case MMC_RSP_R2:
  637. tmp |= CMD_SET_RTYP_17B;
  638. break;
  639. default:
  640. dev_err(&host->pd->dev, "Unsupported response type.\n");
  641. break;
  642. }
  643. switch (opc) {
  644. /* RBSY */
  645. case MMC_SLEEP_AWAKE:
  646. case MMC_SWITCH:
  647. case MMC_STOP_TRANSMISSION:
  648. case MMC_SET_WRITE_PROT:
  649. case MMC_CLR_WRITE_PROT:
  650. case MMC_ERASE:
  651. tmp |= CMD_SET_RBSY;
  652. break;
  653. }
  654. /* WDAT / DATW */
  655. if (data) {
  656. tmp |= CMD_SET_WDAT;
  657. switch (host->bus_width) {
  658. case MMC_BUS_WIDTH_1:
  659. tmp |= CMD_SET_DATW_1;
  660. break;
  661. case MMC_BUS_WIDTH_4:
  662. tmp |= CMD_SET_DATW_4;
  663. break;
  664. case MMC_BUS_WIDTH_8:
  665. tmp |= CMD_SET_DATW_8;
  666. break;
  667. default:
  668. dev_err(&host->pd->dev, "Unsupported bus width.\n");
  669. break;
  670. }
  671. switch (host->timing) {
  672. case MMC_TIMING_UHS_DDR50:
  673. /*
  674. * MMC core will only set this timing, if the host
  675. * advertises the MMC_CAP_UHS_DDR50 capability. MMCIF
  676. * implementations with this capability, e.g. sh73a0,
  677. * will have to set it in their platform data.
  678. */
  679. tmp |= CMD_SET_DARS;
  680. break;
  681. }
  682. }
  683. /* DWEN */
  684. if (opc == MMC_WRITE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK)
  685. tmp |= CMD_SET_DWEN;
  686. /* CMLTE/CMD12EN */
  687. if (opc == MMC_READ_MULTIPLE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) {
  688. tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
  689. sh_mmcif_bitset(host, MMCIF_CE_BLOCK_SET,
  690. data->blocks << 16);
  691. }
  692. /* RIDXC[1:0] check bits */
  693. if (opc == MMC_SEND_OP_COND || opc == MMC_ALL_SEND_CID ||
  694. opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
  695. tmp |= CMD_SET_RIDXC_BITS;
  696. /* RCRC7C[1:0] check bits */
  697. if (opc == MMC_SEND_OP_COND)
  698. tmp |= CMD_SET_CRC7C_BITS;
  699. /* RCRC7C[1:0] internal CRC7 */
  700. if (opc == MMC_ALL_SEND_CID ||
  701. opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
  702. tmp |= CMD_SET_CRC7C_INTERNAL;
  703. return (opc << 24) | tmp;
  704. }
  705. static int sh_mmcif_data_trans(struct sh_mmcif_host *host,
  706. struct mmc_request *mrq, u32 opc)
  707. {
  708. switch (opc) {
  709. case MMC_READ_MULTIPLE_BLOCK:
  710. sh_mmcif_multi_read(host, mrq);
  711. return 0;
  712. case MMC_WRITE_MULTIPLE_BLOCK:
  713. sh_mmcif_multi_write(host, mrq);
  714. return 0;
  715. case MMC_WRITE_BLOCK:
  716. sh_mmcif_single_write(host, mrq);
  717. return 0;
  718. case MMC_READ_SINGLE_BLOCK:
  719. case MMC_SEND_EXT_CSD:
  720. sh_mmcif_single_read(host, mrq);
  721. return 0;
  722. default:
  723. dev_err(&host->pd->dev, "Unsupported CMD%d\n", opc);
  724. return -EINVAL;
  725. }
  726. }
  727. static void sh_mmcif_start_cmd(struct sh_mmcif_host *host,
  728. struct mmc_request *mrq)
  729. {
  730. struct mmc_command *cmd = mrq->cmd;
  731. u32 opc = cmd->opcode;
  732. u32 mask;
  733. switch (opc) {
  734. /* response busy check */
  735. case MMC_SLEEP_AWAKE:
  736. case MMC_SWITCH:
  737. case MMC_STOP_TRANSMISSION:
  738. case MMC_SET_WRITE_PROT:
  739. case MMC_CLR_WRITE_PROT:
  740. case MMC_ERASE:
  741. mask = MASK_START_CMD | MASK_MRBSYE;
  742. break;
  743. default:
  744. mask = MASK_START_CMD | MASK_MCRSPE;
  745. break;
  746. }
  747. if (mrq->data) {
  748. sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, 0);
  749. sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET,
  750. mrq->data->blksz);
  751. }
  752. opc = sh_mmcif_set_cmd(host, mrq);
  753. sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0);
  754. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, mask);
  755. /* set arg */
  756. sh_mmcif_writel(host->addr, MMCIF_CE_ARG, cmd->arg);
  757. /* set cmd */
  758. sh_mmcif_writel(host->addr, MMCIF_CE_CMD_SET, opc);
  759. host->wait_for = MMCIF_WAIT_FOR_CMD;
  760. schedule_delayed_work(&host->timeout_work, host->timeout);
  761. }
  762. static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host,
  763. struct mmc_request *mrq)
  764. {
  765. switch (mrq->cmd->opcode) {
  766. case MMC_READ_MULTIPLE_BLOCK:
  767. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
  768. break;
  769. case MMC_WRITE_MULTIPLE_BLOCK:
  770. sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
  771. break;
  772. default:
  773. dev_err(&host->pd->dev, "unsupported stop cmd\n");
  774. mrq->stop->error = sh_mmcif_error_manage(host);
  775. return;
  776. }
  777. host->wait_for = MMCIF_WAIT_FOR_STOP;
  778. }
  779. static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq)
  780. {
  781. struct sh_mmcif_host *host = mmc_priv(mmc);
  782. unsigned long flags;
  783. spin_lock_irqsave(&host->lock, flags);
  784. if (host->state != STATE_IDLE) {
  785. dev_dbg(&host->pd->dev, "%s() rejected, state %u\n", __func__, host->state);
  786. spin_unlock_irqrestore(&host->lock, flags);
  787. mrq->cmd->error = -EAGAIN;
  788. mmc_request_done(mmc, mrq);
  789. return;
  790. }
  791. host->state = STATE_REQUEST;
  792. spin_unlock_irqrestore(&host->lock, flags);
  793. switch (mrq->cmd->opcode) {
  794. /* MMCIF does not support SD/SDIO command */
  795. case MMC_SLEEP_AWAKE: /* = SD_IO_SEND_OP_COND (5) */
  796. case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
  797. if ((mrq->cmd->flags & MMC_CMD_MASK) != MMC_CMD_BCR)
  798. break;
  799. case MMC_APP_CMD:
  800. case SD_IO_RW_DIRECT:
  801. host->state = STATE_IDLE;
  802. mrq->cmd->error = -ETIMEDOUT;
  803. mmc_request_done(mmc, mrq);
  804. return;
  805. default:
  806. break;
  807. }
  808. host->mrq = mrq;
  809. sh_mmcif_start_cmd(host, mrq);
  810. }
  811. static int sh_mmcif_clk_update(struct sh_mmcif_host *host)
  812. {
  813. int ret = clk_enable(host->hclk);
  814. if (!ret) {
  815. host->clk = clk_get_rate(host->hclk);
  816. host->mmc->f_max = host->clk / 2;
  817. host->mmc->f_min = host->clk / 512;
  818. }
  819. return ret;
  820. }
  821. static void sh_mmcif_set_power(struct sh_mmcif_host *host, struct mmc_ios *ios)
  822. {
  823. struct sh_mmcif_plat_data *pd = host->pd->dev.platform_data;
  824. struct mmc_host *mmc = host->mmc;
  825. if (pd && pd->set_pwr)
  826. pd->set_pwr(host->pd, ios->power_mode != MMC_POWER_OFF);
  827. if (!IS_ERR(mmc->supply.vmmc))
  828. /* Errors ignored... */
  829. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
  830. ios->power_mode ? ios->vdd : 0);
  831. }
  832. static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  833. {
  834. struct sh_mmcif_host *host = mmc_priv(mmc);
  835. unsigned long flags;
  836. spin_lock_irqsave(&host->lock, flags);
  837. if (host->state != STATE_IDLE) {
  838. dev_dbg(&host->pd->dev, "%s() rejected, state %u\n", __func__, host->state);
  839. spin_unlock_irqrestore(&host->lock, flags);
  840. return;
  841. }
  842. host->state = STATE_IOS;
  843. spin_unlock_irqrestore(&host->lock, flags);
  844. if (ios->power_mode == MMC_POWER_UP) {
  845. if (!host->card_present) {
  846. /* See if we also get DMA */
  847. sh_mmcif_request_dma(host, host->pd->dev.platform_data);
  848. host->card_present = true;
  849. }
  850. sh_mmcif_set_power(host, ios);
  851. } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
  852. /* clock stop */
  853. sh_mmcif_clock_control(host, 0);
  854. if (ios->power_mode == MMC_POWER_OFF) {
  855. if (host->card_present) {
  856. sh_mmcif_release_dma(host);
  857. host->card_present = false;
  858. }
  859. }
  860. if (host->power) {
  861. pm_runtime_put_sync(&host->pd->dev);
  862. clk_disable(host->hclk);
  863. host->power = false;
  864. if (ios->power_mode == MMC_POWER_OFF)
  865. sh_mmcif_set_power(host, ios);
  866. }
  867. host->state = STATE_IDLE;
  868. return;
  869. }
  870. if (ios->clock) {
  871. if (!host->power) {
  872. sh_mmcif_clk_update(host);
  873. pm_runtime_get_sync(&host->pd->dev);
  874. host->power = true;
  875. sh_mmcif_sync_reset(host);
  876. }
  877. sh_mmcif_clock_control(host, ios->clock);
  878. }
  879. host->timing = ios->timing;
  880. host->bus_width = ios->bus_width;
  881. host->state = STATE_IDLE;
  882. }
  883. static int sh_mmcif_get_cd(struct mmc_host *mmc)
  884. {
  885. struct sh_mmcif_host *host = mmc_priv(mmc);
  886. struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
  887. int ret = mmc_gpio_get_cd(mmc);
  888. if (ret >= 0)
  889. return ret;
  890. if (!p || !p->get_cd)
  891. return -ENOSYS;
  892. else
  893. return p->get_cd(host->pd);
  894. }
  895. static struct mmc_host_ops sh_mmcif_ops = {
  896. .request = sh_mmcif_request,
  897. .set_ios = sh_mmcif_set_ios,
  898. .get_cd = sh_mmcif_get_cd,
  899. };
  900. static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host)
  901. {
  902. struct mmc_command *cmd = host->mrq->cmd;
  903. struct mmc_data *data = host->mrq->data;
  904. long time;
  905. if (host->sd_error) {
  906. switch (cmd->opcode) {
  907. case MMC_ALL_SEND_CID:
  908. case MMC_SELECT_CARD:
  909. case MMC_APP_CMD:
  910. cmd->error = -ETIMEDOUT;
  911. break;
  912. default:
  913. cmd->error = sh_mmcif_error_manage(host);
  914. break;
  915. }
  916. dev_dbg(&host->pd->dev, "CMD%d error %d\n",
  917. cmd->opcode, cmd->error);
  918. host->sd_error = false;
  919. return false;
  920. }
  921. if (!(cmd->flags & MMC_RSP_PRESENT)) {
  922. cmd->error = 0;
  923. return false;
  924. }
  925. sh_mmcif_get_response(host, cmd);
  926. if (!data)
  927. return false;
  928. /*
  929. * Completion can be signalled from DMA callback and error, so, have to
  930. * reset here, before setting .dma_active
  931. */
  932. init_completion(&host->dma_complete);
  933. if (data->flags & MMC_DATA_READ) {
  934. if (host->chan_rx)
  935. sh_mmcif_start_dma_rx(host);
  936. } else {
  937. if (host->chan_tx)
  938. sh_mmcif_start_dma_tx(host);
  939. }
  940. if (!host->dma_active) {
  941. data->error = sh_mmcif_data_trans(host, host->mrq, cmd->opcode);
  942. return !data->error;
  943. }
  944. /* Running in the IRQ thread, can sleep */
  945. time = wait_for_completion_interruptible_timeout(&host->dma_complete,
  946. host->timeout);
  947. if (data->flags & MMC_DATA_READ)
  948. dma_unmap_sg(host->chan_rx->device->dev,
  949. data->sg, data->sg_len,
  950. DMA_FROM_DEVICE);
  951. else
  952. dma_unmap_sg(host->chan_tx->device->dev,
  953. data->sg, data->sg_len,
  954. DMA_TO_DEVICE);
  955. if (host->sd_error) {
  956. dev_err(host->mmc->parent,
  957. "Error IRQ while waiting for DMA completion!\n");
  958. /* Woken up by an error IRQ: abort DMA */
  959. data->error = sh_mmcif_error_manage(host);
  960. } else if (!time) {
  961. dev_err(host->mmc->parent, "DMA timeout!\n");
  962. data->error = -ETIMEDOUT;
  963. } else if (time < 0) {
  964. dev_err(host->mmc->parent,
  965. "wait_for_completion_...() error %ld!\n", time);
  966. data->error = time;
  967. }
  968. sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC,
  969. BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
  970. host->dma_active = false;
  971. if (data->error) {
  972. data->bytes_xfered = 0;
  973. /* Abort DMA */
  974. if (data->flags & MMC_DATA_READ)
  975. dmaengine_terminate_all(host->chan_rx);
  976. else
  977. dmaengine_terminate_all(host->chan_tx);
  978. }
  979. return false;
  980. }
  981. static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)
  982. {
  983. struct sh_mmcif_host *host = dev_id;
  984. struct mmc_request *mrq;
  985. bool wait = false;
  986. cancel_delayed_work_sync(&host->timeout_work);
  987. mutex_lock(&host->thread_lock);
  988. mrq = host->mrq;
  989. if (!mrq) {
  990. dev_dbg(&host->pd->dev, "IRQ thread state %u, wait %u: NULL mrq!\n",
  991. host->state, host->wait_for);
  992. mutex_unlock(&host->thread_lock);
  993. return IRQ_HANDLED;
  994. }
  995. /*
  996. * All handlers return true, if processing continues, and false, if the
  997. * request has to be completed - successfully or not
  998. */
  999. switch (host->wait_for) {
  1000. case MMCIF_WAIT_FOR_REQUEST:
  1001. /* We're too late, the timeout has already kicked in */
  1002. mutex_unlock(&host->thread_lock);
  1003. return IRQ_HANDLED;
  1004. case MMCIF_WAIT_FOR_CMD:
  1005. /* Wait for data? */
  1006. wait = sh_mmcif_end_cmd(host);
  1007. break;
  1008. case MMCIF_WAIT_FOR_MREAD:
  1009. /* Wait for more data? */
  1010. wait = sh_mmcif_mread_block(host);
  1011. break;
  1012. case MMCIF_WAIT_FOR_READ:
  1013. /* Wait for data end? */
  1014. wait = sh_mmcif_read_block(host);
  1015. break;
  1016. case MMCIF_WAIT_FOR_MWRITE:
  1017. /* Wait data to write? */
  1018. wait = sh_mmcif_mwrite_block(host);
  1019. break;
  1020. case MMCIF_WAIT_FOR_WRITE:
  1021. /* Wait for data end? */
  1022. wait = sh_mmcif_write_block(host);
  1023. break;
  1024. case MMCIF_WAIT_FOR_STOP:
  1025. if (host->sd_error) {
  1026. mrq->stop->error = sh_mmcif_error_manage(host);
  1027. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, mrq->stop->error);
  1028. break;
  1029. }
  1030. sh_mmcif_get_cmd12response(host, mrq->stop);
  1031. mrq->stop->error = 0;
  1032. break;
  1033. case MMCIF_WAIT_FOR_READ_END:
  1034. case MMCIF_WAIT_FOR_WRITE_END:
  1035. if (host->sd_error) {
  1036. mrq->data->error = sh_mmcif_error_manage(host);
  1037. dev_dbg(&host->pd->dev, "%s(): %d\n", __func__, mrq->data->error);
  1038. }
  1039. break;
  1040. default:
  1041. BUG();
  1042. }
  1043. if (wait) {
  1044. schedule_delayed_work(&host->timeout_work, host->timeout);
  1045. /* Wait for more data */
  1046. mutex_unlock(&host->thread_lock);
  1047. return IRQ_HANDLED;
  1048. }
  1049. if (host->wait_for != MMCIF_WAIT_FOR_STOP) {
  1050. struct mmc_data *data = mrq->data;
  1051. if (!mrq->cmd->error && data && !data->error)
  1052. data->bytes_xfered =
  1053. data->blocks * data->blksz;
  1054. if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) {
  1055. sh_mmcif_stop_cmd(host, mrq);
  1056. if (!mrq->stop->error) {
  1057. schedule_delayed_work(&host->timeout_work, host->timeout);
  1058. mutex_unlock(&host->thread_lock);
  1059. return IRQ_HANDLED;
  1060. }
  1061. }
  1062. }
  1063. host->wait_for = MMCIF_WAIT_FOR_REQUEST;
  1064. host->state = STATE_IDLE;
  1065. host->mrq = NULL;
  1066. mmc_request_done(host->mmc, mrq);
  1067. mutex_unlock(&host->thread_lock);
  1068. return IRQ_HANDLED;
  1069. }
  1070. static irqreturn_t sh_mmcif_intr(int irq, void *dev_id)
  1071. {
  1072. struct sh_mmcif_host *host = dev_id;
  1073. u32 state;
  1074. int err = 0;
  1075. state = sh_mmcif_readl(host->addr, MMCIF_CE_INT);
  1076. if (state & INT_ERR_STS) {
  1077. /* error interrupts - process first */
  1078. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state);
  1079. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state);
  1080. err = 1;
  1081. } else if (state & INT_RBSYE) {
  1082. sh_mmcif_writel(host->addr, MMCIF_CE_INT,
  1083. ~(INT_RBSYE | INT_CRSPE));
  1084. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MRBSYE);
  1085. } else if (state & INT_CRSPE) {
  1086. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_CRSPE);
  1087. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCRSPE);
  1088. } else if (state & INT_BUFREN) {
  1089. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFREN);
  1090. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
  1091. } else if (state & INT_BUFWEN) {
  1092. sh_mmcif_writel(host->addr, MMCIF_CE_INT,
  1093. ~(INT_BUFWEN | INT_DTRANE | INT_CMD12DRE |
  1094. INT_CMD12RBE | INT_CMD12CRE));
  1095. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
  1096. } else if (state & INT_CMD12DRE) {
  1097. sh_mmcif_writel(host->addr, MMCIF_CE_INT,
  1098. ~(INT_CMD12DRE | INT_CMD12RBE |
  1099. INT_CMD12CRE | INT_BUFRE));
  1100. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
  1101. } else if (state & INT_BUFRE) {
  1102. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFRE);
  1103. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
  1104. } else if (state & INT_DTRANE) {
  1105. sh_mmcif_writel(host->addr, MMCIF_CE_INT,
  1106. ~(INT_CMD12DRE | INT_CMD12RBE |
  1107. INT_CMD12CRE | INT_DTRANE));
  1108. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
  1109. } else if (state & INT_CMD12RBE) {
  1110. sh_mmcif_writel(host->addr, MMCIF_CE_INT,
  1111. ~(INT_CMD12RBE | INT_CMD12CRE));
  1112. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
  1113. } else {
  1114. dev_dbg(&host->pd->dev, "Unsupported interrupt: 0x%x\n", state);
  1115. sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state);
  1116. sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state);
  1117. err = 1;
  1118. }
  1119. if (err) {
  1120. host->sd_error = true;
  1121. dev_dbg(&host->pd->dev, "int err state = %08x\n", state);
  1122. }
  1123. if (state & ~(INT_CMD12RBE | INT_CMD12CRE)) {
  1124. if (!host->dma_active)
  1125. return IRQ_WAKE_THREAD;
  1126. else if (host->sd_error)
  1127. mmcif_dma_complete(host);
  1128. } else {
  1129. dev_dbg(&host->pd->dev, "Unexpected IRQ 0x%x\n", state);
  1130. }
  1131. return IRQ_HANDLED;
  1132. }
  1133. static void mmcif_timeout_work(struct work_struct *work)
  1134. {
  1135. struct delayed_work *d = container_of(work, struct delayed_work, work);
  1136. struct sh_mmcif_host *host = container_of(d, struct sh_mmcif_host, timeout_work);
  1137. struct mmc_request *mrq = host->mrq;
  1138. unsigned long flags;
  1139. if (host->dying)
  1140. /* Don't run after mmc_remove_host() */
  1141. return;
  1142. dev_err(&host->pd->dev, "Timeout waiting for %u on CMD%u\n",
  1143. host->wait_for, mrq->cmd->opcode);
  1144. spin_lock_irqsave(&host->lock, flags);
  1145. if (host->state == STATE_IDLE) {
  1146. spin_unlock_irqrestore(&host->lock, flags);
  1147. return;
  1148. }
  1149. host->state = STATE_TIMEOUT;
  1150. spin_unlock_irqrestore(&host->lock, flags);
  1151. /*
  1152. * Handle races with cancel_delayed_work(), unless
  1153. * cancel_delayed_work_sync() is used
  1154. */
  1155. switch (host->wait_for) {
  1156. case MMCIF_WAIT_FOR_CMD:
  1157. mrq->cmd->error = sh_mmcif_error_manage(host);
  1158. break;
  1159. case MMCIF_WAIT_FOR_STOP:
  1160. mrq->stop->error = sh_mmcif_error_manage(host);
  1161. break;
  1162. case MMCIF_WAIT_FOR_MREAD:
  1163. case MMCIF_WAIT_FOR_MWRITE:
  1164. case MMCIF_WAIT_FOR_READ:
  1165. case MMCIF_WAIT_FOR_WRITE:
  1166. case MMCIF_WAIT_FOR_READ_END:
  1167. case MMCIF_WAIT_FOR_WRITE_END:
  1168. mrq->data->error = sh_mmcif_error_manage(host);
  1169. break;
  1170. default:
  1171. BUG();
  1172. }
  1173. host->state = STATE_IDLE;
  1174. host->wait_for = MMCIF_WAIT_FOR_REQUEST;
  1175. host->mrq = NULL;
  1176. mmc_request_done(host->mmc, mrq);
  1177. }
  1178. static void sh_mmcif_init_ocr(struct sh_mmcif_host *host)
  1179. {
  1180. struct sh_mmcif_plat_data *pd = host->pd->dev.platform_data;
  1181. struct mmc_host *mmc = host->mmc;
  1182. mmc_regulator_get_supply(mmc);
  1183. if (!pd)
  1184. return;
  1185. if (!mmc->ocr_avail)
  1186. mmc->ocr_avail = pd->ocr;
  1187. else if (pd->ocr)
  1188. dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
  1189. }
  1190. static int sh_mmcif_probe(struct platform_device *pdev)
  1191. {
  1192. int ret = 0, irq[2];
  1193. struct mmc_host *mmc;
  1194. struct sh_mmcif_host *host;
  1195. struct sh_mmcif_plat_data *pd = pdev->dev.platform_data;
  1196. struct resource *res;
  1197. void __iomem *reg;
  1198. const char *name;
  1199. irq[0] = platform_get_irq(pdev, 0);
  1200. irq[1] = platform_get_irq(pdev, 1);
  1201. if (irq[0] < 0) {
  1202. dev_err(&pdev->dev, "Get irq error\n");
  1203. return -ENXIO;
  1204. }
  1205. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1206. if (!res) {
  1207. dev_err(&pdev->dev, "platform_get_resource error.\n");
  1208. return -ENXIO;
  1209. }
  1210. reg = ioremap(res->start, resource_size(res));
  1211. if (!reg) {
  1212. dev_err(&pdev->dev, "ioremap error.\n");
  1213. return -ENOMEM;
  1214. }
  1215. mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), &pdev->dev);
  1216. if (!mmc) {
  1217. ret = -ENOMEM;
  1218. goto ealloch;
  1219. }
  1220. host = mmc_priv(mmc);
  1221. host->mmc = mmc;
  1222. host->addr = reg;
  1223. host->timeout = msecs_to_jiffies(1000);
  1224. host->pd = pdev;
  1225. spin_lock_init(&host->lock);
  1226. mmc->ops = &sh_mmcif_ops;
  1227. sh_mmcif_init_ocr(host);
  1228. mmc->caps = MMC_CAP_MMC_HIGHSPEED | MMC_CAP_WAIT_WHILE_BUSY;
  1229. if (pd && pd->caps)
  1230. mmc->caps |= pd->caps;
  1231. mmc->max_segs = 32;
  1232. mmc->max_blk_size = 512;
  1233. mmc->max_req_size = PAGE_CACHE_SIZE * mmc->max_segs;
  1234. mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size;
  1235. mmc->max_seg_size = mmc->max_req_size;
  1236. platform_set_drvdata(pdev, host);
  1237. pm_runtime_enable(&pdev->dev);
  1238. host->power = false;
  1239. host->hclk = clk_get(&pdev->dev, NULL);
  1240. if (IS_ERR(host->hclk)) {
  1241. ret = PTR_ERR(host->hclk);
  1242. dev_err(&pdev->dev, "cannot get clock: %d\n", ret);
  1243. goto eclkget;
  1244. }
  1245. ret = sh_mmcif_clk_update(host);
  1246. if (ret < 0)
  1247. goto eclkupdate;
  1248. ret = pm_runtime_resume(&pdev->dev);
  1249. if (ret < 0)
  1250. goto eresume;
  1251. INIT_DELAYED_WORK(&host->timeout_work, mmcif_timeout_work);
  1252. sh_mmcif_sync_reset(host);
  1253. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1254. name = irq[1] < 0 ? dev_name(&pdev->dev) : "sh_mmc:error";
  1255. ret = request_threaded_irq(irq[0], sh_mmcif_intr, sh_mmcif_irqt, 0, name, host);
  1256. if (ret) {
  1257. dev_err(&pdev->dev, "request_irq error (%s)\n", name);
  1258. goto ereqirq0;
  1259. }
  1260. if (irq[1] >= 0) {
  1261. ret = request_threaded_irq(irq[1], sh_mmcif_intr, sh_mmcif_irqt,
  1262. 0, "sh_mmc:int", host);
  1263. if (ret) {
  1264. dev_err(&pdev->dev, "request_irq error (sh_mmc:int)\n");
  1265. goto ereqirq1;
  1266. }
  1267. }
  1268. if (pd && pd->use_cd_gpio) {
  1269. ret = mmc_gpio_request_cd(mmc, pd->cd_gpio);
  1270. if (ret < 0)
  1271. goto erqcd;
  1272. }
  1273. mutex_init(&host->thread_lock);
  1274. clk_disable(host->hclk);
  1275. ret = mmc_add_host(mmc);
  1276. if (ret < 0)
  1277. goto emmcaddh;
  1278. dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
  1279. dev_info(&pdev->dev, "driver version %s\n", DRIVER_VERSION);
  1280. dev_dbg(&pdev->dev, "chip ver H'%04x\n",
  1281. sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0x0000ffff);
  1282. return ret;
  1283. emmcaddh:
  1284. erqcd:
  1285. if (irq[1] >= 0)
  1286. free_irq(irq[1], host);
  1287. ereqirq1:
  1288. free_irq(irq[0], host);
  1289. ereqirq0:
  1290. pm_runtime_suspend(&pdev->dev);
  1291. eresume:
  1292. clk_disable(host->hclk);
  1293. eclkupdate:
  1294. clk_put(host->hclk);
  1295. eclkget:
  1296. pm_runtime_disable(&pdev->dev);
  1297. mmc_free_host(mmc);
  1298. ealloch:
  1299. iounmap(reg);
  1300. return ret;
  1301. }
  1302. static int sh_mmcif_remove(struct platform_device *pdev)
  1303. {
  1304. struct sh_mmcif_host *host = platform_get_drvdata(pdev);
  1305. int irq[2];
  1306. host->dying = true;
  1307. clk_enable(host->hclk);
  1308. pm_runtime_get_sync(&pdev->dev);
  1309. dev_pm_qos_hide_latency_limit(&pdev->dev);
  1310. mmc_remove_host(host->mmc);
  1311. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1312. /*
  1313. * FIXME: cancel_delayed_work(_sync)() and free_irq() race with the
  1314. * mmc_remove_host() call above. But swapping order doesn't help either
  1315. * (a query on the linux-mmc mailing list didn't bring any replies).
  1316. */
  1317. cancel_delayed_work_sync(&host->timeout_work);
  1318. if (host->addr)
  1319. iounmap(host->addr);
  1320. irq[0] = platform_get_irq(pdev, 0);
  1321. irq[1] = platform_get_irq(pdev, 1);
  1322. free_irq(irq[0], host);
  1323. if (irq[1] >= 0)
  1324. free_irq(irq[1], host);
  1325. platform_set_drvdata(pdev, NULL);
  1326. clk_disable(host->hclk);
  1327. mmc_free_host(host->mmc);
  1328. pm_runtime_put_sync(&pdev->dev);
  1329. pm_runtime_disable(&pdev->dev);
  1330. return 0;
  1331. }
  1332. #ifdef CONFIG_PM
  1333. static int sh_mmcif_suspend(struct device *dev)
  1334. {
  1335. struct sh_mmcif_host *host = dev_get_drvdata(dev);
  1336. int ret = mmc_suspend_host(host->mmc);
  1337. if (!ret)
  1338. sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
  1339. return ret;
  1340. }
  1341. static int sh_mmcif_resume(struct device *dev)
  1342. {
  1343. struct sh_mmcif_host *host = dev_get_drvdata(dev);
  1344. return mmc_resume_host(host->mmc);
  1345. }
  1346. #else
  1347. #define sh_mmcif_suspend NULL
  1348. #define sh_mmcif_resume NULL
  1349. #endif /* CONFIG_PM */
  1350. static const struct of_device_id mmcif_of_match[] = {
  1351. { .compatible = "renesas,sh-mmcif" },
  1352. { }
  1353. };
  1354. MODULE_DEVICE_TABLE(of, mmcif_of_match);
  1355. static const struct dev_pm_ops sh_mmcif_dev_pm_ops = {
  1356. .suspend = sh_mmcif_suspend,
  1357. .resume = sh_mmcif_resume,
  1358. };
  1359. static struct platform_driver sh_mmcif_driver = {
  1360. .probe = sh_mmcif_probe,
  1361. .remove = sh_mmcif_remove,
  1362. .driver = {
  1363. .name = DRIVER_NAME,
  1364. .pm = &sh_mmcif_dev_pm_ops,
  1365. .owner = THIS_MODULE,
  1366. .of_match_table = mmcif_of_match,
  1367. },
  1368. };
  1369. module_platform_driver(sh_mmcif_driver);
  1370. MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver");
  1371. MODULE_LICENSE("GPL");
  1372. MODULE_ALIAS("platform:" DRIVER_NAME);
  1373. MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>");