mxcmmc.c 31 KB

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