sh_mmcif.c 41 KB

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