sh_mmcif.c 41 KB

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