mxcmmc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/mmc/host/mxcmmc.c - Freescale i.MX MMCI driver
  4. *
  5. * This is a driver for the SDHC controller found in Freescale MX2/MX3
  6. * SoCs. It is basically the same hardware as found on MX1 (imxmmc.c).
  7. * Unlike the hardware found on MX1, this hardware just works and does
  8. * not need all the quirks found in imxmmc.c, hence the separate driver.
  9. *
  10. * Copyright (C) 2008 Sascha Hauer, Pengutronix <[email protected]>
  11. * Copyright (C) 2006 Pavel Pisa, PiKRON <[email protected]>
  12. *
  13. * derived from pxamci.c by Russell King
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/ioport.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/highmem.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/mmc/host.h>
  25. #include <linux/mmc/card.h>
  26. #include <linux/delay.h>
  27. #include <linux/clk.h>
  28. #include <linux/io.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/types.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/of_dma.h>
  35. #include <linux/mmc/slot-gpio.h>
  36. #include <asm/dma.h>
  37. #include <asm/irq.h>
  38. #include <linux/platform_data/mmc-mxcmmc.h>
  39. #include <linux/dma/imx-dma.h>
  40. #define DRIVER_NAME "mxc-mmc"
  41. #define MXCMCI_TIMEOUT_MS 10000
  42. #define MMC_REG_STR_STP_CLK 0x00
  43. #define MMC_REG_STATUS 0x04
  44. #define MMC_REG_CLK_RATE 0x08
  45. #define MMC_REG_CMD_DAT_CONT 0x0C
  46. #define MMC_REG_RES_TO 0x10
  47. #define MMC_REG_READ_TO 0x14
  48. #define MMC_REG_BLK_LEN 0x18
  49. #define MMC_REG_NOB 0x1C
  50. #define MMC_REG_REV_NO 0x20
  51. #define MMC_REG_INT_CNTR 0x24
  52. #define MMC_REG_CMD 0x28
  53. #define MMC_REG_ARG 0x2C
  54. #define MMC_REG_RES_FIFO 0x34
  55. #define MMC_REG_BUFFER_ACCESS 0x38
  56. #define STR_STP_CLK_RESET (1 << 3)
  57. #define STR_STP_CLK_START_CLK (1 << 1)
  58. #define STR_STP_CLK_STOP_CLK (1 << 0)
  59. #define STATUS_CARD_INSERTION (1 << 31)
  60. #define STATUS_CARD_REMOVAL (1 << 30)
  61. #define STATUS_YBUF_EMPTY (1 << 29)
  62. #define STATUS_XBUF_EMPTY (1 << 28)
  63. #define STATUS_YBUF_FULL (1 << 27)
  64. #define STATUS_XBUF_FULL (1 << 26)
  65. #define STATUS_BUF_UND_RUN (1 << 25)
  66. #define STATUS_BUF_OVFL (1 << 24)
  67. #define STATUS_SDIO_INT_ACTIVE (1 << 14)
  68. #define STATUS_END_CMD_RESP (1 << 13)
  69. #define STATUS_WRITE_OP_DONE (1 << 12)
  70. #define STATUS_DATA_TRANS_DONE (1 << 11)
  71. #define STATUS_READ_OP_DONE (1 << 11)
  72. #define STATUS_WR_CRC_ERROR_CODE_MASK (3 << 10)
  73. #define STATUS_CARD_BUS_CLK_RUN (1 << 8)
  74. #define STATUS_BUF_READ_RDY (1 << 7)
  75. #define STATUS_BUF_WRITE_RDY (1 << 6)
  76. #define STATUS_RESP_CRC_ERR (1 << 5)
  77. #define STATUS_CRC_READ_ERR (1 << 3)
  78. #define STATUS_CRC_WRITE_ERR (1 << 2)
  79. #define STATUS_TIME_OUT_RESP (1 << 1)
  80. #define STATUS_TIME_OUT_READ (1 << 0)
  81. #define STATUS_ERR_MASK 0x2f
  82. #define CMD_DAT_CONT_CMD_RESP_LONG_OFF (1 << 12)
  83. #define CMD_DAT_CONT_STOP_READWAIT (1 << 11)
  84. #define CMD_DAT_CONT_START_READWAIT (1 << 10)
  85. #define CMD_DAT_CONT_BUS_WIDTH_4 (2 << 8)
  86. #define CMD_DAT_CONT_INIT (1 << 7)
  87. #define CMD_DAT_CONT_WRITE (1 << 4)
  88. #define CMD_DAT_CONT_DATA_ENABLE (1 << 3)
  89. #define CMD_DAT_CONT_RESPONSE_48BIT_CRC (1 << 0)
  90. #define CMD_DAT_CONT_RESPONSE_136BIT (2 << 0)
  91. #define CMD_DAT_CONT_RESPONSE_48BIT (3 << 0)
  92. #define INT_SDIO_INT_WKP_EN (1 << 18)
  93. #define INT_CARD_INSERTION_WKP_EN (1 << 17)
  94. #define INT_CARD_REMOVAL_WKP_EN (1 << 16)
  95. #define INT_CARD_INSERTION_EN (1 << 15)
  96. #define INT_CARD_REMOVAL_EN (1 << 14)
  97. #define INT_SDIO_IRQ_EN (1 << 13)
  98. #define INT_DAT0_EN (1 << 12)
  99. #define INT_BUF_READ_EN (1 << 4)
  100. #define INT_BUF_WRITE_EN (1 << 3)
  101. #define INT_END_CMD_RES_EN (1 << 2)
  102. #define INT_WRITE_OP_DONE_EN (1 << 1)
  103. #define INT_READ_OP_EN (1 << 0)
  104. enum mxcmci_type {
  105. IMX21_MMC,
  106. IMX31_MMC,
  107. MPC512X_MMC,
  108. };
  109. struct mxcmci_host {
  110. struct mmc_host *mmc;
  111. void __iomem *base;
  112. dma_addr_t phys_base;
  113. int detect_irq;
  114. struct dma_chan *dma;
  115. struct dma_async_tx_descriptor *desc;
  116. int do_dma;
  117. int default_irq_mask;
  118. int use_sdio;
  119. unsigned int power_mode;
  120. struct imxmmc_platform_data *pdata;
  121. struct mmc_request *req;
  122. struct mmc_command *cmd;
  123. struct mmc_data *data;
  124. unsigned int datasize;
  125. unsigned int dma_dir;
  126. u16 rev_no;
  127. unsigned int cmdat;
  128. struct clk *clk_ipg;
  129. struct clk *clk_per;
  130. int clock;
  131. struct work_struct datawork;
  132. spinlock_t lock;
  133. int burstlen;
  134. int dmareq;
  135. struct dma_slave_config dma_slave_config;
  136. struct imx_dma_data dma_data;
  137. struct timer_list watchdog;
  138. enum mxcmci_type devtype;
  139. };
  140. static const struct of_device_id mxcmci_of_match[] = {
  141. {
  142. .compatible = "fsl,imx21-mmc",
  143. .data = (void *) IMX21_MMC,
  144. }, {
  145. .compatible = "fsl,imx31-mmc",
  146. .data = (void *) IMX31_MMC,
  147. }, {
  148. .compatible = "fsl,mpc5121-sdhc",
  149. .data = (void *) MPC512X_MMC,
  150. }, {
  151. /* sentinel */
  152. }
  153. };
  154. MODULE_DEVICE_TABLE(of, mxcmci_of_match);
  155. static inline int is_imx31_mmc(struct mxcmci_host *host)
  156. {
  157. return host->devtype == IMX31_MMC;
  158. }
  159. static inline int is_mpc512x_mmc(struct mxcmci_host *host)
  160. {
  161. return host->devtype == MPC512X_MMC;
  162. }
  163. static inline u32 mxcmci_readl(struct mxcmci_host *host, int reg)
  164. {
  165. if (IS_ENABLED(CONFIG_PPC_MPC512x))
  166. return ioread32be(host->base + reg);
  167. else
  168. return readl(host->base + reg);
  169. }
  170. static inline void mxcmci_writel(struct mxcmci_host *host, u32 val, int reg)
  171. {
  172. if (IS_ENABLED(CONFIG_PPC_MPC512x))
  173. iowrite32be(val, host->base + reg);
  174. else
  175. writel(val, host->base + reg);
  176. }
  177. static inline u16 mxcmci_readw(struct mxcmci_host *host, int reg)
  178. {
  179. if (IS_ENABLED(CONFIG_PPC_MPC512x))
  180. return ioread32be(host->base + reg);
  181. else
  182. return readw(host->base + reg);
  183. }
  184. static inline void mxcmci_writew(struct mxcmci_host *host, u16 val, int reg)
  185. {
  186. if (IS_ENABLED(CONFIG_PPC_MPC512x))
  187. iowrite32be(val, host->base + reg);
  188. else
  189. writew(val, host->base + reg);
  190. }
  191. static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios);
  192. static void mxcmci_set_power(struct mxcmci_host *host, unsigned int vdd)
  193. {
  194. if (!IS_ERR(host->mmc->supply.vmmc)) {
  195. if (host->power_mode == MMC_POWER_UP)
  196. mmc_regulator_set_ocr(host->mmc,
  197. host->mmc->supply.vmmc, vdd);
  198. else if (host->power_mode == MMC_POWER_OFF)
  199. mmc_regulator_set_ocr(host->mmc,
  200. host->mmc->supply.vmmc, 0);
  201. }
  202. if (host->pdata && host->pdata->setpower)
  203. host->pdata->setpower(mmc_dev(host->mmc), vdd);
  204. }
  205. static inline int mxcmci_use_dma(struct mxcmci_host *host)
  206. {
  207. return host->do_dma;
  208. }
  209. static void mxcmci_softreset(struct mxcmci_host *host)
  210. {
  211. int i;
  212. dev_dbg(mmc_dev(host->mmc), "mxcmci_softreset\n");
  213. /* reset sequence */
  214. mxcmci_writew(host, STR_STP_CLK_RESET, MMC_REG_STR_STP_CLK);
  215. mxcmci_writew(host, STR_STP_CLK_RESET | STR_STP_CLK_START_CLK,
  216. MMC_REG_STR_STP_CLK);
  217. for (i = 0; i < 8; i++)
  218. mxcmci_writew(host, STR_STP_CLK_START_CLK, MMC_REG_STR_STP_CLK);
  219. mxcmci_writew(host, 0xff, MMC_REG_RES_TO);
  220. }
  221. #if IS_ENABLED(CONFIG_PPC_MPC512x)
  222. static inline void buffer_swap32(u32 *buf, int len)
  223. {
  224. int i;
  225. for (i = 0; i < ((len + 3) / 4); i++) {
  226. *buf = swab32(*buf);
  227. buf++;
  228. }
  229. }
  230. static void mxcmci_swap_buffers(struct mmc_data *data)
  231. {
  232. struct scatterlist *sg;
  233. int i;
  234. for_each_sg(data->sg, sg, data->sg_len, i)
  235. buffer_swap32(sg_virt(sg), sg->length);
  236. }
  237. #else
  238. static inline void mxcmci_swap_buffers(struct mmc_data *data) {}
  239. #endif
  240. static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
  241. {
  242. unsigned int nob = data->blocks;
  243. unsigned int blksz = data->blksz;
  244. unsigned int datasize = nob * blksz;
  245. struct scatterlist *sg;
  246. enum dma_transfer_direction slave_dirn;
  247. int i, nents;
  248. host->data = data;
  249. data->bytes_xfered = 0;
  250. mxcmci_writew(host, nob, MMC_REG_NOB);
  251. mxcmci_writew(host, blksz, MMC_REG_BLK_LEN);
  252. host->datasize = datasize;
  253. if (!mxcmci_use_dma(host))
  254. return 0;
  255. for_each_sg(data->sg, sg, data->sg_len, i) {
  256. if (sg->offset & 3 || sg->length & 3 || sg->length < 512) {
  257. host->do_dma = 0;
  258. return 0;
  259. }
  260. }
  261. if (data->flags & MMC_DATA_READ) {
  262. host->dma_dir = DMA_FROM_DEVICE;
  263. slave_dirn = DMA_DEV_TO_MEM;
  264. } else {
  265. host->dma_dir = DMA_TO_DEVICE;
  266. slave_dirn = DMA_MEM_TO_DEV;
  267. mxcmci_swap_buffers(data);
  268. }
  269. nents = dma_map_sg(host->dma->device->dev, data->sg,
  270. data->sg_len, host->dma_dir);
  271. if (nents != data->sg_len)
  272. return -EINVAL;
  273. host->desc = dmaengine_prep_slave_sg(host->dma,
  274. data->sg, data->sg_len, slave_dirn,
  275. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  276. if (!host->desc) {
  277. dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len,
  278. host->dma_dir);
  279. host->do_dma = 0;
  280. return 0; /* Fall back to PIO */
  281. }
  282. wmb();
  283. dmaengine_submit(host->desc);
  284. dma_async_issue_pending(host->dma);
  285. mod_timer(&host->watchdog, jiffies + msecs_to_jiffies(MXCMCI_TIMEOUT_MS));
  286. return 0;
  287. }
  288. static void mxcmci_cmd_done(struct mxcmci_host *host, unsigned int stat);
  289. static void mxcmci_data_done(struct mxcmci_host *host, unsigned int stat);
  290. static void mxcmci_dma_callback(void *data)
  291. {
  292. struct mxcmci_host *host = data;
  293. u32 stat;
  294. del_timer(&host->watchdog);
  295. stat = mxcmci_readl(host, MMC_REG_STATUS);
  296. dev_dbg(mmc_dev(host->mmc), "%s: 0x%08x\n", __func__, stat);
  297. mxcmci_data_done(host, stat);
  298. }
  299. static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_command *cmd,
  300. unsigned int cmdat)
  301. {
  302. u32 int_cntr = host->default_irq_mask;
  303. unsigned long flags;
  304. WARN_ON(host->cmd != NULL);
  305. host->cmd = cmd;
  306. switch (mmc_resp_type(cmd)) {
  307. case MMC_RSP_R1: /* short CRC, OPCODE */
  308. case MMC_RSP_R1B:/* short CRC, OPCODE, BUSY */
  309. cmdat |= CMD_DAT_CONT_RESPONSE_48BIT_CRC;
  310. break;
  311. case MMC_RSP_R2: /* long 136 bit + CRC */
  312. cmdat |= CMD_DAT_CONT_RESPONSE_136BIT;
  313. break;
  314. case MMC_RSP_R3: /* short */
  315. cmdat |= CMD_DAT_CONT_RESPONSE_48BIT;
  316. break;
  317. case MMC_RSP_NONE:
  318. break;
  319. default:
  320. dev_err(mmc_dev(host->mmc), "unhandled response type 0x%x\n",
  321. mmc_resp_type(cmd));
  322. cmd->error = -EINVAL;
  323. return -EINVAL;
  324. }
  325. int_cntr = INT_END_CMD_RES_EN;
  326. if (mxcmci_use_dma(host)) {
  327. if (host->dma_dir == DMA_FROM_DEVICE) {
  328. host->desc->callback = mxcmci_dma_callback;
  329. host->desc->callback_param = host;
  330. } else {
  331. int_cntr |= INT_WRITE_OP_DONE_EN;
  332. }
  333. }
  334. spin_lock_irqsave(&host->lock, flags);
  335. if (host->use_sdio)
  336. int_cntr |= INT_SDIO_IRQ_EN;
  337. mxcmci_writel(host, int_cntr, MMC_REG_INT_CNTR);
  338. spin_unlock_irqrestore(&host->lock, flags);
  339. mxcmci_writew(host, cmd->opcode, MMC_REG_CMD);
  340. mxcmci_writel(host, cmd->arg, MMC_REG_ARG);
  341. mxcmci_writew(host, cmdat, MMC_REG_CMD_DAT_CONT);
  342. return 0;
  343. }
  344. static void mxcmci_finish_request(struct mxcmci_host *host,
  345. struct mmc_request *req)
  346. {
  347. u32 int_cntr = host->default_irq_mask;
  348. unsigned long flags;
  349. spin_lock_irqsave(&host->lock, flags);
  350. if (host->use_sdio)
  351. int_cntr |= INT_SDIO_IRQ_EN;
  352. mxcmci_writel(host, int_cntr, MMC_REG_INT_CNTR);
  353. spin_unlock_irqrestore(&host->lock, flags);
  354. host->req = NULL;
  355. host->cmd = NULL;
  356. host->data = NULL;
  357. mmc_request_done(host->mmc, req);
  358. }
  359. static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat)
  360. {
  361. struct mmc_data *data = host->data;
  362. int data_error;
  363. if (mxcmci_use_dma(host)) {
  364. dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len,
  365. host->dma_dir);
  366. mxcmci_swap_buffers(data);
  367. }
  368. if (stat & STATUS_ERR_MASK) {
  369. dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n",
  370. stat);
  371. if (stat & STATUS_CRC_READ_ERR) {
  372. dev_err(mmc_dev(host->mmc), "%s: -EILSEQ\n", __func__);
  373. data->error = -EILSEQ;
  374. } else if (stat & STATUS_CRC_WRITE_ERR) {
  375. u32 err_code = (stat >> 9) & 0x3;
  376. if (err_code == 2) { /* No CRC response */
  377. dev_err(mmc_dev(host->mmc),
  378. "%s: No CRC -ETIMEDOUT\n", __func__);
  379. data->error = -ETIMEDOUT;
  380. } else {
  381. dev_err(mmc_dev(host->mmc),
  382. "%s: -EILSEQ\n", __func__);
  383. data->error = -EILSEQ;
  384. }
  385. } else if (stat & STATUS_TIME_OUT_READ) {
  386. dev_err(mmc_dev(host->mmc),
  387. "%s: read -ETIMEDOUT\n", __func__);
  388. data->error = -ETIMEDOUT;
  389. } else {
  390. dev_err(mmc_dev(host->mmc), "%s: -EIO\n", __func__);
  391. data->error = -EIO;
  392. }
  393. } else {
  394. data->bytes_xfered = host->datasize;
  395. }
  396. data_error = data->error;
  397. host->data = NULL;
  398. return data_error;
  399. }
  400. static void mxcmci_read_response(struct mxcmci_host *host, unsigned int stat)
  401. {
  402. struct mmc_command *cmd = host->cmd;
  403. int i;
  404. u32 a, b, c;
  405. if (!cmd)
  406. return;
  407. if (stat & STATUS_TIME_OUT_RESP) {
  408. dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n");
  409. cmd->error = -ETIMEDOUT;
  410. } else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
  411. dev_dbg(mmc_dev(host->mmc), "cmd crc error\n");
  412. cmd->error = -EILSEQ;
  413. }
  414. if (cmd->flags & MMC_RSP_PRESENT) {
  415. if (cmd->flags & MMC_RSP_136) {
  416. for (i = 0; i < 4; i++) {
  417. a = mxcmci_readw(host, MMC_REG_RES_FIFO);
  418. b = mxcmci_readw(host, MMC_REG_RES_FIFO);
  419. cmd->resp[i] = a << 16 | b;
  420. }
  421. } else {
  422. a = mxcmci_readw(host, MMC_REG_RES_FIFO);
  423. b = mxcmci_readw(host, MMC_REG_RES_FIFO);
  424. c = mxcmci_readw(host, MMC_REG_RES_FIFO);
  425. cmd->resp[0] = a << 24 | b << 8 | c >> 8;
  426. }
  427. }
  428. }
  429. static int mxcmci_poll_status(struct mxcmci_host *host, u32 mask)
  430. {
  431. u32 stat;
  432. unsigned long timeout = jiffies + HZ;
  433. do {
  434. stat = mxcmci_readl(host, MMC_REG_STATUS);
  435. if (stat & STATUS_ERR_MASK)
  436. return stat;
  437. if (time_after(jiffies, timeout)) {
  438. mxcmci_softreset(host);
  439. mxcmci_set_clk_rate(host, host->clock);
  440. return STATUS_TIME_OUT_READ;
  441. }
  442. if (stat & mask)
  443. return 0;
  444. cpu_relax();
  445. } while (1);
  446. }
  447. static int mxcmci_pull(struct mxcmci_host *host, void *_buf, int bytes)
  448. {
  449. unsigned int stat;
  450. u32 *buf = _buf;
  451. while (bytes > 3) {
  452. stat = mxcmci_poll_status(host,
  453. STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
  454. if (stat)
  455. return stat;
  456. *buf++ = cpu_to_le32(mxcmci_readl(host, MMC_REG_BUFFER_ACCESS));
  457. bytes -= 4;
  458. }
  459. if (bytes) {
  460. u8 *b = (u8 *)buf;
  461. u32 tmp;
  462. stat = mxcmci_poll_status(host,
  463. STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
  464. if (stat)
  465. return stat;
  466. tmp = cpu_to_le32(mxcmci_readl(host, MMC_REG_BUFFER_ACCESS));
  467. memcpy(b, &tmp, bytes);
  468. }
  469. return 0;
  470. }
  471. static int mxcmci_push(struct mxcmci_host *host, void *_buf, int bytes)
  472. {
  473. unsigned int stat;
  474. u32 *buf = _buf;
  475. while (bytes > 3) {
  476. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  477. if (stat)
  478. return stat;
  479. mxcmci_writel(host, cpu_to_le32(*buf++), MMC_REG_BUFFER_ACCESS);
  480. bytes -= 4;
  481. }
  482. if (bytes) {
  483. u8 *b = (u8 *)buf;
  484. u32 tmp;
  485. stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  486. if (stat)
  487. return stat;
  488. memcpy(&tmp, b, bytes);
  489. mxcmci_writel(host, cpu_to_le32(tmp), MMC_REG_BUFFER_ACCESS);
  490. }
  491. return mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
  492. }
  493. static int mxcmci_transfer_data(struct mxcmci_host *host)
  494. {
  495. struct mmc_data *data = host->req->data;
  496. struct scatterlist *sg;
  497. int stat, i;
  498. host->data = data;
  499. host->datasize = 0;
  500. if (data->flags & MMC_DATA_READ) {
  501. for_each_sg(data->sg, sg, data->sg_len, i) {
  502. stat = mxcmci_pull(host, sg_virt(sg), sg->length);
  503. if (stat)
  504. return stat;
  505. host->datasize += sg->length;
  506. }
  507. } else {
  508. for_each_sg(data->sg, sg, data->sg_len, i) {
  509. stat = mxcmci_push(host, sg_virt(sg), sg->length);
  510. if (stat)
  511. return stat;
  512. host->datasize += sg->length;
  513. }
  514. stat = mxcmci_poll_status(host, STATUS_WRITE_OP_DONE);
  515. if (stat)
  516. return stat;
  517. }
  518. return 0;
  519. }
  520. static void mxcmci_datawork(struct work_struct *work)
  521. {
  522. struct mxcmci_host *host = container_of(work, struct mxcmci_host,
  523. datawork);
  524. int datastat = mxcmci_transfer_data(host);
  525. mxcmci_writel(host, STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE,
  526. MMC_REG_STATUS);
  527. mxcmci_finish_data(host, datastat);
  528. if (host->req->stop) {
  529. if (mxcmci_start_cmd(host, host->req->stop, 0)) {
  530. mxcmci_finish_request(host, host->req);
  531. return;
  532. }
  533. } else {
  534. mxcmci_finish_request(host, host->req);
  535. }
  536. }
  537. static void mxcmci_data_done(struct mxcmci_host *host, unsigned int stat)
  538. {
  539. struct mmc_request *req;
  540. int data_error;
  541. unsigned long flags;
  542. spin_lock_irqsave(&host->lock, flags);
  543. if (!host->data) {
  544. spin_unlock_irqrestore(&host->lock, flags);
  545. return;
  546. }
  547. if (!host->req) {
  548. spin_unlock_irqrestore(&host->lock, flags);
  549. return;
  550. }
  551. req = host->req;
  552. if (!req->stop)
  553. host->req = NULL; /* we will handle finish req below */
  554. data_error = mxcmci_finish_data(host, stat);
  555. spin_unlock_irqrestore(&host->lock, flags);
  556. if (data_error)
  557. return;
  558. mxcmci_read_response(host, stat);
  559. host->cmd = NULL;
  560. if (req->stop) {
  561. if (mxcmci_start_cmd(host, req->stop, 0)) {
  562. mxcmci_finish_request(host, req);
  563. return;
  564. }
  565. } else {
  566. mxcmci_finish_request(host, req);
  567. }
  568. }
  569. static void mxcmci_cmd_done(struct mxcmci_host *host, unsigned int stat)
  570. {
  571. mxcmci_read_response(host, stat);
  572. host->cmd = NULL;
  573. if (!host->data && host->req) {
  574. mxcmci_finish_request(host, host->req);
  575. return;
  576. }
  577. /* For the DMA case the DMA engine handles the data transfer
  578. * automatically. For non DMA we have to do it ourselves.
  579. * Don't do it in interrupt context though.
  580. */
  581. if (!mxcmci_use_dma(host) && host->data)
  582. schedule_work(&host->datawork);
  583. }
  584. static irqreturn_t mxcmci_irq(int irq, void *devid)
  585. {
  586. struct mxcmci_host *host = devid;
  587. bool sdio_irq;
  588. u32 stat;
  589. stat = mxcmci_readl(host, MMC_REG_STATUS);
  590. mxcmci_writel(host,
  591. stat & ~(STATUS_SDIO_INT_ACTIVE | STATUS_DATA_TRANS_DONE |
  592. STATUS_WRITE_OP_DONE),
  593. MMC_REG_STATUS);
  594. dev_dbg(mmc_dev(host->mmc), "%s: 0x%08x\n", __func__, stat);
  595. spin_lock(&host->lock);
  596. sdio_irq = (stat & STATUS_SDIO_INT_ACTIVE) && host->use_sdio;
  597. spin_unlock(&host->lock);
  598. if (mxcmci_use_dma(host) && (stat & (STATUS_WRITE_OP_DONE)))
  599. mxcmci_writel(host, STATUS_WRITE_OP_DONE, MMC_REG_STATUS);
  600. if (sdio_irq) {
  601. mxcmci_writel(host, STATUS_SDIO_INT_ACTIVE, MMC_REG_STATUS);
  602. mmc_signal_sdio_irq(host->mmc);
  603. }
  604. if (stat & STATUS_END_CMD_RESP)
  605. mxcmci_cmd_done(host, stat);
  606. if (mxcmci_use_dma(host) && (stat & STATUS_WRITE_OP_DONE)) {
  607. del_timer(&host->watchdog);
  608. mxcmci_data_done(host, stat);
  609. }
  610. if (host->default_irq_mask &&
  611. (stat & (STATUS_CARD_INSERTION | STATUS_CARD_REMOVAL)))
  612. mmc_detect_change(host->mmc, msecs_to_jiffies(200));
  613. return IRQ_HANDLED;
  614. }
  615. static void mxcmci_request(struct mmc_host *mmc, struct mmc_request *req)
  616. {
  617. struct mxcmci_host *host = mmc_priv(mmc);
  618. unsigned int cmdat = host->cmdat;
  619. int error;
  620. WARN_ON(host->req != NULL);
  621. host->req = req;
  622. host->cmdat &= ~CMD_DAT_CONT_INIT;
  623. if (host->dma)
  624. host->do_dma = 1;
  625. if (req->data) {
  626. error = mxcmci_setup_data(host, req->data);
  627. if (error) {
  628. req->cmd->error = error;
  629. goto out;
  630. }
  631. cmdat |= CMD_DAT_CONT_DATA_ENABLE;
  632. if (req->data->flags & MMC_DATA_WRITE)
  633. cmdat |= CMD_DAT_CONT_WRITE;
  634. }
  635. error = mxcmci_start_cmd(host, req->cmd, cmdat);
  636. out:
  637. if (error)
  638. mxcmci_finish_request(host, req);
  639. }
  640. static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios)
  641. {
  642. unsigned int divider;
  643. int prescaler = 0;
  644. unsigned int clk_in = clk_get_rate(host->clk_per);
  645. while (prescaler <= 0x800) {
  646. for (divider = 1; divider <= 0xF; divider++) {
  647. int x;
  648. x = (clk_in / (divider + 1));
  649. if (prescaler)
  650. x /= (prescaler * 2);
  651. if (x <= clk_ios)
  652. break;
  653. }
  654. if (divider < 0x10)
  655. break;
  656. if (prescaler == 0)
  657. prescaler = 1;
  658. else
  659. prescaler <<= 1;
  660. }
  661. mxcmci_writew(host, (prescaler << 4) | divider, MMC_REG_CLK_RATE);
  662. dev_dbg(mmc_dev(host->mmc), "scaler: %d divider: %d in: %d out: %d\n",
  663. prescaler, divider, clk_in, clk_ios);
  664. }
  665. static int mxcmci_setup_dma(struct mmc_host *mmc)
  666. {
  667. struct mxcmci_host *host = mmc_priv(mmc);
  668. struct dma_slave_config *config = &host->dma_slave_config;
  669. config->dst_addr = host->phys_base + MMC_REG_BUFFER_ACCESS;
  670. config->src_addr = host->phys_base + MMC_REG_BUFFER_ACCESS;
  671. config->dst_addr_width = 4;
  672. config->src_addr_width = 4;
  673. config->dst_maxburst = host->burstlen;
  674. config->src_maxburst = host->burstlen;
  675. config->device_fc = false;
  676. return dmaengine_slave_config(host->dma, config);
  677. }
  678. static void mxcmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  679. {
  680. struct mxcmci_host *host = mmc_priv(mmc);
  681. int burstlen, ret;
  682. /*
  683. * use burstlen of 64 (16 words) in 4 bit mode (--> reg value 0)
  684. * use burstlen of 16 (4 words) in 1 bit mode (--> reg value 16)
  685. */
  686. if (ios->bus_width == MMC_BUS_WIDTH_4)
  687. burstlen = 16;
  688. else
  689. burstlen = 4;
  690. if (mxcmci_use_dma(host) && burstlen != host->burstlen) {
  691. host->burstlen = burstlen;
  692. ret = mxcmci_setup_dma(mmc);
  693. if (ret) {
  694. dev_err(mmc_dev(host->mmc),
  695. "failed to config DMA channel. Falling back to PIO\n");
  696. dma_release_channel(host->dma);
  697. host->do_dma = 0;
  698. host->dma = NULL;
  699. }
  700. }
  701. if (ios->bus_width == MMC_BUS_WIDTH_4)
  702. host->cmdat |= CMD_DAT_CONT_BUS_WIDTH_4;
  703. else
  704. host->cmdat &= ~CMD_DAT_CONT_BUS_WIDTH_4;
  705. if (host->power_mode != ios->power_mode) {
  706. host->power_mode = ios->power_mode;
  707. mxcmci_set_power(host, ios->vdd);
  708. if (ios->power_mode == MMC_POWER_ON)
  709. host->cmdat |= CMD_DAT_CONT_INIT;
  710. }
  711. if (ios->clock) {
  712. mxcmci_set_clk_rate(host, ios->clock);
  713. mxcmci_writew(host, STR_STP_CLK_START_CLK, MMC_REG_STR_STP_CLK);
  714. } else {
  715. mxcmci_writew(host, STR_STP_CLK_STOP_CLK, MMC_REG_STR_STP_CLK);
  716. }
  717. host->clock = ios->clock;
  718. }
  719. static irqreturn_t mxcmci_detect_irq(int irq, void *data)
  720. {
  721. struct mmc_host *mmc = data;
  722. dev_dbg(mmc_dev(mmc), "%s\n", __func__);
  723. mmc_detect_change(mmc, msecs_to_jiffies(250));
  724. return IRQ_HANDLED;
  725. }
  726. static int mxcmci_get_ro(struct mmc_host *mmc)
  727. {
  728. struct mxcmci_host *host = mmc_priv(mmc);
  729. if (host->pdata && host->pdata->get_ro)
  730. return !!host->pdata->get_ro(mmc_dev(mmc));
  731. /*
  732. * If board doesn't support read only detection (no mmc_gpio
  733. * context or gpio is invalid), then let the mmc core decide
  734. * what to do.
  735. */
  736. return mmc_gpio_get_ro(mmc);
  737. }
  738. static void mxcmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  739. {
  740. struct mxcmci_host *host = mmc_priv(mmc);
  741. unsigned long flags;
  742. u32 int_cntr;
  743. spin_lock_irqsave(&host->lock, flags);
  744. host->use_sdio = enable;
  745. int_cntr = mxcmci_readl(host, MMC_REG_INT_CNTR);
  746. if (enable)
  747. int_cntr |= INT_SDIO_IRQ_EN;
  748. else
  749. int_cntr &= ~INT_SDIO_IRQ_EN;
  750. mxcmci_writel(host, int_cntr, MMC_REG_INT_CNTR);
  751. spin_unlock_irqrestore(&host->lock, flags);
  752. }
  753. static void mxcmci_init_card(struct mmc_host *host, struct mmc_card *card)
  754. {
  755. struct mxcmci_host *mxcmci = mmc_priv(host);
  756. /*
  757. * MX3 SoCs have a silicon bug which corrupts CRC calculation of
  758. * multi-block transfers when connected SDIO peripheral doesn't
  759. * drive the BUSY line as required by the specs.
  760. * One way to prevent this is to only allow 1-bit transfers.
  761. */
  762. if (is_imx31_mmc(mxcmci) && mmc_card_sdio(card))
  763. host->caps &= ~MMC_CAP_4_BIT_DATA;
  764. else
  765. host->caps |= MMC_CAP_4_BIT_DATA;
  766. }
  767. static bool filter(struct dma_chan *chan, void *param)
  768. {
  769. struct mxcmci_host *host = param;
  770. if (!imx_dma_is_general_purpose(chan))
  771. return false;
  772. chan->private = &host->dma_data;
  773. return true;
  774. }
  775. static void mxcmci_watchdog(struct timer_list *t)
  776. {
  777. struct mxcmci_host *host = from_timer(host, t, watchdog);
  778. struct mmc_request *req = host->req;
  779. unsigned int stat = mxcmci_readl(host, MMC_REG_STATUS);
  780. if (host->dma_dir == DMA_FROM_DEVICE) {
  781. dmaengine_terminate_all(host->dma);
  782. dev_err(mmc_dev(host->mmc),
  783. "%s: read time out (status = 0x%08x)\n",
  784. __func__, stat);
  785. } else {
  786. dev_err(mmc_dev(host->mmc),
  787. "%s: write time out (status = 0x%08x)\n",
  788. __func__, stat);
  789. mxcmci_softreset(host);
  790. }
  791. /* Mark transfer as erroneus and inform the upper layers */
  792. if (host->data)
  793. host->data->error = -ETIMEDOUT;
  794. host->req = NULL;
  795. host->cmd = NULL;
  796. host->data = NULL;
  797. mmc_request_done(host->mmc, req);
  798. }
  799. static const struct mmc_host_ops mxcmci_ops = {
  800. .request = mxcmci_request,
  801. .set_ios = mxcmci_set_ios,
  802. .get_ro = mxcmci_get_ro,
  803. .enable_sdio_irq = mxcmci_enable_sdio_irq,
  804. .init_card = mxcmci_init_card,
  805. };
  806. static int mxcmci_probe(struct platform_device *pdev)
  807. {
  808. struct mmc_host *mmc;
  809. struct mxcmci_host *host;
  810. struct resource *res;
  811. int ret = 0, irq;
  812. bool dat3_card_detect = false;
  813. dma_cap_mask_t mask;
  814. struct imxmmc_platform_data *pdata = pdev->dev.platform_data;
  815. pr_info("i.MX/MPC512x SDHC driver\n");
  816. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  817. irq = platform_get_irq(pdev, 0);
  818. if (irq < 0)
  819. return irq;
  820. mmc = mmc_alloc_host(sizeof(*host), &pdev->dev);
  821. if (!mmc)
  822. return -ENOMEM;
  823. host = mmc_priv(mmc);
  824. host->base = devm_ioremap_resource(&pdev->dev, res);
  825. if (IS_ERR(host->base)) {
  826. ret = PTR_ERR(host->base);
  827. goto out_free;
  828. }
  829. host->phys_base = res->start;
  830. ret = mmc_of_parse(mmc);
  831. if (ret)
  832. goto out_free;
  833. mmc->ops = &mxcmci_ops;
  834. /* For devicetree parsing, the bus width is read from devicetree */
  835. if (pdata)
  836. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
  837. else
  838. mmc->caps |= MMC_CAP_SDIO_IRQ;
  839. /* MMC core transfer sizes tunable parameters */
  840. mmc->max_blk_size = 2048;
  841. mmc->max_blk_count = 65535;
  842. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  843. mmc->max_seg_size = mmc->max_req_size;
  844. host->devtype = (uintptr_t)of_device_get_match_data(&pdev->dev);
  845. /* adjust max_segs after devtype detection */
  846. if (!is_mpc512x_mmc(host))
  847. mmc->max_segs = 64;
  848. host->mmc = mmc;
  849. host->pdata = pdata;
  850. spin_lock_init(&host->lock);
  851. if (pdata)
  852. dat3_card_detect = pdata->dat3_card_detect;
  853. else if (mmc_card_is_removable(mmc)
  854. && !of_property_read_bool(pdev->dev.of_node, "cd-gpios"))
  855. dat3_card_detect = true;
  856. ret = mmc_regulator_get_supply(mmc);
  857. if (ret)
  858. goto out_free;
  859. if (!mmc->ocr_avail) {
  860. if (pdata && pdata->ocr_avail)
  861. mmc->ocr_avail = pdata->ocr_avail;
  862. else
  863. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  864. }
  865. if (dat3_card_detect)
  866. host->default_irq_mask =
  867. INT_CARD_INSERTION_EN | INT_CARD_REMOVAL_EN;
  868. else
  869. host->default_irq_mask = 0;
  870. host->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  871. if (IS_ERR(host->clk_ipg)) {
  872. ret = PTR_ERR(host->clk_ipg);
  873. goto out_free;
  874. }
  875. host->clk_per = devm_clk_get(&pdev->dev, "per");
  876. if (IS_ERR(host->clk_per)) {
  877. ret = PTR_ERR(host->clk_per);
  878. goto out_free;
  879. }
  880. ret = clk_prepare_enable(host->clk_per);
  881. if (ret)
  882. goto out_free;
  883. ret = clk_prepare_enable(host->clk_ipg);
  884. if (ret)
  885. goto out_clk_per_put;
  886. mxcmci_softreset(host);
  887. host->rev_no = mxcmci_readw(host, MMC_REG_REV_NO);
  888. if (host->rev_no != 0x400) {
  889. ret = -ENODEV;
  890. dev_err(mmc_dev(host->mmc), "wrong rev.no. 0x%08x. aborting.\n",
  891. host->rev_no);
  892. goto out_clk_put;
  893. }
  894. mmc->f_min = clk_get_rate(host->clk_per) >> 16;
  895. mmc->f_max = clk_get_rate(host->clk_per) >> 1;
  896. /* recommended in data sheet */
  897. mxcmci_writew(host, 0x2db4, MMC_REG_READ_TO);
  898. mxcmci_writel(host, host->default_irq_mask, MMC_REG_INT_CNTR);
  899. if (!host->pdata) {
  900. host->dma = dma_request_chan(&pdev->dev, "rx-tx");
  901. if (IS_ERR(host->dma)) {
  902. if (PTR_ERR(host->dma) == -EPROBE_DEFER) {
  903. ret = -EPROBE_DEFER;
  904. goto out_clk_put;
  905. }
  906. /* Ignore errors to fall back to PIO mode */
  907. host->dma = NULL;
  908. }
  909. } else {
  910. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  911. if (res) {
  912. host->dmareq = res->start;
  913. host->dma_data.peripheral_type = IMX_DMATYPE_SDHC;
  914. host->dma_data.priority = DMA_PRIO_LOW;
  915. host->dma_data.dma_request = host->dmareq;
  916. dma_cap_zero(mask);
  917. dma_cap_set(DMA_SLAVE, mask);
  918. host->dma = dma_request_channel(mask, filter, host);
  919. }
  920. }
  921. if (host->dma)
  922. mmc->max_seg_size = dma_get_max_seg_size(
  923. host->dma->device->dev);
  924. else
  925. dev_info(mmc_dev(host->mmc), "dma not available. Using PIO\n");
  926. INIT_WORK(&host->datawork, mxcmci_datawork);
  927. ret = devm_request_irq(&pdev->dev, irq, mxcmci_irq, 0,
  928. dev_name(&pdev->dev), host);
  929. if (ret)
  930. goto out_free_dma;
  931. platform_set_drvdata(pdev, mmc);
  932. if (host->pdata && host->pdata->init) {
  933. ret = host->pdata->init(&pdev->dev, mxcmci_detect_irq,
  934. host->mmc);
  935. if (ret)
  936. goto out_free_dma;
  937. }
  938. timer_setup(&host->watchdog, mxcmci_watchdog, 0);
  939. ret = mmc_add_host(mmc);
  940. if (ret)
  941. goto out_free_dma;
  942. return 0;
  943. out_free_dma:
  944. if (host->dma)
  945. dma_release_channel(host->dma);
  946. out_clk_put:
  947. clk_disable_unprepare(host->clk_ipg);
  948. out_clk_per_put:
  949. clk_disable_unprepare(host->clk_per);
  950. out_free:
  951. mmc_free_host(mmc);
  952. return ret;
  953. }
  954. static int mxcmci_remove(struct platform_device *pdev)
  955. {
  956. struct mmc_host *mmc = platform_get_drvdata(pdev);
  957. struct mxcmci_host *host = mmc_priv(mmc);
  958. mmc_remove_host(mmc);
  959. if (host->pdata && host->pdata->exit)
  960. host->pdata->exit(&pdev->dev, mmc);
  961. if (host->dma)
  962. dma_release_channel(host->dma);
  963. clk_disable_unprepare(host->clk_per);
  964. clk_disable_unprepare(host->clk_ipg);
  965. mmc_free_host(mmc);
  966. return 0;
  967. }
  968. static int mxcmci_suspend(struct device *dev)
  969. {
  970. struct mmc_host *mmc = dev_get_drvdata(dev);
  971. struct mxcmci_host *host = mmc_priv(mmc);
  972. clk_disable_unprepare(host->clk_per);
  973. clk_disable_unprepare(host->clk_ipg);
  974. return 0;
  975. }
  976. static int mxcmci_resume(struct device *dev)
  977. {
  978. struct mmc_host *mmc = dev_get_drvdata(dev);
  979. struct mxcmci_host *host = mmc_priv(mmc);
  980. int ret;
  981. ret = clk_prepare_enable(host->clk_per);
  982. if (ret)
  983. return ret;
  984. ret = clk_prepare_enable(host->clk_ipg);
  985. if (ret)
  986. clk_disable_unprepare(host->clk_per);
  987. return ret;
  988. }
  989. static DEFINE_SIMPLE_DEV_PM_OPS(mxcmci_pm_ops, mxcmci_suspend, mxcmci_resume);
  990. static struct platform_driver mxcmci_driver = {
  991. .probe = mxcmci_probe,
  992. .remove = mxcmci_remove,
  993. .driver = {
  994. .name = DRIVER_NAME,
  995. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  996. .pm = pm_sleep_ptr(&mxcmci_pm_ops),
  997. .of_match_table = mxcmci_of_match,
  998. }
  999. };
  1000. module_platform_driver(mxcmci_driver);
  1001. MODULE_DESCRIPTION("i.MX Multimedia Card Interface Driver");
  1002. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  1003. MODULE_LICENSE("GPL");
  1004. MODULE_ALIAS("platform:mxc-mmc");