mxs-mmc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*
  2. * Portions copyright (C) 2003 Russell King, PXA MMCI Driver
  3. * Portions copyright (C) 2004-2005 Pierre Ossman, W83L51xD SD/MMC driver
  4. *
  5. * Copyright 2008 Embedded Alley Solutions, Inc.
  6. * Copyright 2009-2011 Freescale Semiconductor, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/ioport.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/delay.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/dmaengine.h>
  33. #include <linux/highmem.h>
  34. #include <linux/clk.h>
  35. #include <linux/err.h>
  36. #include <linux/completion.h>
  37. #include <linux/mmc/host.h>
  38. #include <linux/mmc/mmc.h>
  39. #include <linux/mmc/sdio.h>
  40. #include <linux/gpio.h>
  41. #include <linux/regulator/consumer.h>
  42. #include <linux/module.h>
  43. #include <linux/stmp_device.h>
  44. #include <linux/spi/mxs-spi.h>
  45. #define DRIVER_NAME "mxs-mmc"
  46. #define MXS_MMC_IRQ_BITS (BM_SSP_CTRL1_SDIO_IRQ | \
  47. BM_SSP_CTRL1_RESP_ERR_IRQ | \
  48. BM_SSP_CTRL1_RESP_TIMEOUT_IRQ | \
  49. BM_SSP_CTRL1_DATA_TIMEOUT_IRQ | \
  50. BM_SSP_CTRL1_DATA_CRC_IRQ | \
  51. BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ | \
  52. BM_SSP_CTRL1_RECV_TIMEOUT_IRQ | \
  53. BM_SSP_CTRL1_FIFO_OVERRUN_IRQ)
  54. /* card detect polling timeout */
  55. #define MXS_MMC_DETECT_TIMEOUT (HZ/2)
  56. struct mxs_mmc_host {
  57. struct mxs_ssp ssp;
  58. struct mmc_host *mmc;
  59. struct mmc_request *mrq;
  60. struct mmc_command *cmd;
  61. struct mmc_data *data;
  62. unsigned char bus_width;
  63. spinlock_t lock;
  64. int sdio_irq_en;
  65. int wp_gpio;
  66. bool wp_inverted;
  67. bool cd_inverted;
  68. bool broken_cd;
  69. bool non_removable;
  70. };
  71. static int mxs_mmc_get_ro(struct mmc_host *mmc)
  72. {
  73. struct mxs_mmc_host *host = mmc_priv(mmc);
  74. int ret;
  75. if (!gpio_is_valid(host->wp_gpio))
  76. return -EINVAL;
  77. ret = gpio_get_value(host->wp_gpio);
  78. if (host->wp_inverted)
  79. ret = !ret;
  80. return ret;
  81. }
  82. static int mxs_mmc_get_cd(struct mmc_host *mmc)
  83. {
  84. struct mxs_mmc_host *host = mmc_priv(mmc);
  85. struct mxs_ssp *ssp = &host->ssp;
  86. return host->non_removable || host->broken_cd ||
  87. !(readl(ssp->base + HW_SSP_STATUS(ssp)) &
  88. BM_SSP_STATUS_CARD_DETECT) ^ host->cd_inverted;
  89. }
  90. static int mxs_mmc_reset(struct mxs_mmc_host *host)
  91. {
  92. struct mxs_ssp *ssp = &host->ssp;
  93. u32 ctrl0, ctrl1;
  94. int ret;
  95. ret = stmp_reset_block(ssp->base);
  96. if (ret)
  97. return ret;
  98. ctrl0 = BM_SSP_CTRL0_IGNORE_CRC;
  99. ctrl1 = BF_SSP(0x3, CTRL1_SSP_MODE) |
  100. BF_SSP(0x7, CTRL1_WORD_LENGTH) |
  101. BM_SSP_CTRL1_DMA_ENABLE |
  102. BM_SSP_CTRL1_POLARITY |
  103. BM_SSP_CTRL1_RECV_TIMEOUT_IRQ_EN |
  104. BM_SSP_CTRL1_DATA_CRC_IRQ_EN |
  105. BM_SSP_CTRL1_DATA_TIMEOUT_IRQ_EN |
  106. BM_SSP_CTRL1_RESP_TIMEOUT_IRQ_EN |
  107. BM_SSP_CTRL1_RESP_ERR_IRQ_EN;
  108. writel(BF_SSP(0xffff, TIMING_TIMEOUT) |
  109. BF_SSP(2, TIMING_CLOCK_DIVIDE) |
  110. BF_SSP(0, TIMING_CLOCK_RATE),
  111. ssp->base + HW_SSP_TIMING(ssp));
  112. if (host->sdio_irq_en) {
  113. ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
  114. ctrl1 |= BM_SSP_CTRL1_SDIO_IRQ_EN;
  115. }
  116. writel(ctrl0, ssp->base + HW_SSP_CTRL0);
  117. writel(ctrl1, ssp->base + HW_SSP_CTRL1(ssp));
  118. return 0;
  119. }
  120. static void mxs_mmc_start_cmd(struct mxs_mmc_host *host,
  121. struct mmc_command *cmd);
  122. static void mxs_mmc_request_done(struct mxs_mmc_host *host)
  123. {
  124. struct mmc_command *cmd = host->cmd;
  125. struct mmc_data *data = host->data;
  126. struct mmc_request *mrq = host->mrq;
  127. struct mxs_ssp *ssp = &host->ssp;
  128. if (mmc_resp_type(cmd) & MMC_RSP_PRESENT) {
  129. if (mmc_resp_type(cmd) & MMC_RSP_136) {
  130. cmd->resp[3] = readl(ssp->base + HW_SSP_SDRESP0(ssp));
  131. cmd->resp[2] = readl(ssp->base + HW_SSP_SDRESP1(ssp));
  132. cmd->resp[1] = readl(ssp->base + HW_SSP_SDRESP2(ssp));
  133. cmd->resp[0] = readl(ssp->base + HW_SSP_SDRESP3(ssp));
  134. } else {
  135. cmd->resp[0] = readl(ssp->base + HW_SSP_SDRESP0(ssp));
  136. }
  137. }
  138. if (data) {
  139. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  140. data->sg_len, ssp->dma_dir);
  141. /*
  142. * If there was an error on any block, we mark all
  143. * data blocks as being in error.
  144. */
  145. if (!data->error)
  146. data->bytes_xfered = data->blocks * data->blksz;
  147. else
  148. data->bytes_xfered = 0;
  149. host->data = NULL;
  150. if (mrq->stop) {
  151. mxs_mmc_start_cmd(host, mrq->stop);
  152. return;
  153. }
  154. }
  155. host->mrq = NULL;
  156. mmc_request_done(host->mmc, mrq);
  157. }
  158. static void mxs_mmc_dma_irq_callback(void *param)
  159. {
  160. struct mxs_mmc_host *host = param;
  161. mxs_mmc_request_done(host);
  162. }
  163. static irqreturn_t mxs_mmc_irq_handler(int irq, void *dev_id)
  164. {
  165. struct mxs_mmc_host *host = dev_id;
  166. struct mmc_command *cmd = host->cmd;
  167. struct mmc_data *data = host->data;
  168. struct mxs_ssp *ssp = &host->ssp;
  169. u32 stat;
  170. spin_lock(&host->lock);
  171. stat = readl(ssp->base + HW_SSP_CTRL1(ssp));
  172. writel(stat & MXS_MMC_IRQ_BITS,
  173. ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_CLR);
  174. spin_unlock(&host->lock);
  175. if ((stat & BM_SSP_CTRL1_SDIO_IRQ) && (stat & BM_SSP_CTRL1_SDIO_IRQ_EN))
  176. mmc_signal_sdio_irq(host->mmc);
  177. if (stat & BM_SSP_CTRL1_RESP_TIMEOUT_IRQ)
  178. cmd->error = -ETIMEDOUT;
  179. else if (stat & BM_SSP_CTRL1_RESP_ERR_IRQ)
  180. cmd->error = -EIO;
  181. if (data) {
  182. if (stat & (BM_SSP_CTRL1_DATA_TIMEOUT_IRQ |
  183. BM_SSP_CTRL1_RECV_TIMEOUT_IRQ))
  184. data->error = -ETIMEDOUT;
  185. else if (stat & BM_SSP_CTRL1_DATA_CRC_IRQ)
  186. data->error = -EILSEQ;
  187. else if (stat & (BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ |
  188. BM_SSP_CTRL1_FIFO_OVERRUN_IRQ))
  189. data->error = -EIO;
  190. }
  191. return IRQ_HANDLED;
  192. }
  193. static struct dma_async_tx_descriptor *mxs_mmc_prep_dma(
  194. struct mxs_mmc_host *host, unsigned long flags)
  195. {
  196. struct mxs_ssp *ssp = &host->ssp;
  197. struct dma_async_tx_descriptor *desc;
  198. struct mmc_data *data = host->data;
  199. struct scatterlist * sgl;
  200. unsigned int sg_len;
  201. if (data) {
  202. /* data */
  203. dma_map_sg(mmc_dev(host->mmc), data->sg,
  204. data->sg_len, ssp->dma_dir);
  205. sgl = data->sg;
  206. sg_len = data->sg_len;
  207. } else {
  208. /* pio */
  209. sgl = (struct scatterlist *) ssp->ssp_pio_words;
  210. sg_len = SSP_PIO_NUM;
  211. }
  212. desc = dmaengine_prep_slave_sg(ssp->dmach,
  213. sgl, sg_len, ssp->slave_dirn, flags);
  214. if (desc) {
  215. desc->callback = mxs_mmc_dma_irq_callback;
  216. desc->callback_param = host;
  217. } else {
  218. if (data)
  219. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  220. data->sg_len, ssp->dma_dir);
  221. }
  222. return desc;
  223. }
  224. static void mxs_mmc_bc(struct mxs_mmc_host *host)
  225. {
  226. struct mxs_ssp *ssp = &host->ssp;
  227. struct mmc_command *cmd = host->cmd;
  228. struct dma_async_tx_descriptor *desc;
  229. u32 ctrl0, cmd0, cmd1;
  230. ctrl0 = BM_SSP_CTRL0_ENABLE | BM_SSP_CTRL0_IGNORE_CRC;
  231. cmd0 = BF_SSP(cmd->opcode, CMD0_CMD) | BM_SSP_CMD0_APPEND_8CYC;
  232. cmd1 = cmd->arg;
  233. if (host->sdio_irq_en) {
  234. ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
  235. cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
  236. }
  237. ssp->ssp_pio_words[0] = ctrl0;
  238. ssp->ssp_pio_words[1] = cmd0;
  239. ssp->ssp_pio_words[2] = cmd1;
  240. ssp->dma_dir = DMA_NONE;
  241. ssp->slave_dirn = DMA_TRANS_NONE;
  242. desc = mxs_mmc_prep_dma(host, DMA_CTRL_ACK);
  243. if (!desc)
  244. goto out;
  245. dmaengine_submit(desc);
  246. dma_async_issue_pending(ssp->dmach);
  247. return;
  248. out:
  249. dev_warn(mmc_dev(host->mmc),
  250. "%s: failed to prep dma\n", __func__);
  251. }
  252. static void mxs_mmc_ac(struct mxs_mmc_host *host)
  253. {
  254. struct mxs_ssp *ssp = &host->ssp;
  255. struct mmc_command *cmd = host->cmd;
  256. struct dma_async_tx_descriptor *desc;
  257. u32 ignore_crc, get_resp, long_resp;
  258. u32 ctrl0, cmd0, cmd1;
  259. ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ?
  260. 0 : BM_SSP_CTRL0_IGNORE_CRC;
  261. get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ?
  262. BM_SSP_CTRL0_GET_RESP : 0;
  263. long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ?
  264. BM_SSP_CTRL0_LONG_RESP : 0;
  265. ctrl0 = BM_SSP_CTRL0_ENABLE | ignore_crc | get_resp | long_resp;
  266. cmd0 = BF_SSP(cmd->opcode, CMD0_CMD);
  267. cmd1 = cmd->arg;
  268. if (host->sdio_irq_en) {
  269. ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
  270. cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
  271. }
  272. ssp->ssp_pio_words[0] = ctrl0;
  273. ssp->ssp_pio_words[1] = cmd0;
  274. ssp->ssp_pio_words[2] = cmd1;
  275. ssp->dma_dir = DMA_NONE;
  276. ssp->slave_dirn = DMA_TRANS_NONE;
  277. desc = mxs_mmc_prep_dma(host, DMA_CTRL_ACK);
  278. if (!desc)
  279. goto out;
  280. dmaengine_submit(desc);
  281. dma_async_issue_pending(ssp->dmach);
  282. return;
  283. out:
  284. dev_warn(mmc_dev(host->mmc),
  285. "%s: failed to prep dma\n", __func__);
  286. }
  287. static unsigned short mxs_ns_to_ssp_ticks(unsigned clock_rate, unsigned ns)
  288. {
  289. const unsigned int ssp_timeout_mul = 4096;
  290. /*
  291. * Calculate ticks in ms since ns are large numbers
  292. * and might overflow
  293. */
  294. const unsigned int clock_per_ms = clock_rate / 1000;
  295. const unsigned int ms = ns / 1000;
  296. const unsigned int ticks = ms * clock_per_ms;
  297. const unsigned int ssp_ticks = ticks / ssp_timeout_mul;
  298. WARN_ON(ssp_ticks == 0);
  299. return ssp_ticks;
  300. }
  301. static void mxs_mmc_adtc(struct mxs_mmc_host *host)
  302. {
  303. struct mmc_command *cmd = host->cmd;
  304. struct mmc_data *data = cmd->data;
  305. struct dma_async_tx_descriptor *desc;
  306. struct scatterlist *sgl = data->sg, *sg;
  307. unsigned int sg_len = data->sg_len;
  308. unsigned int i;
  309. unsigned short dma_data_dir, timeout;
  310. enum dma_transfer_direction slave_dirn;
  311. unsigned int data_size = 0, log2_blksz;
  312. unsigned int blocks = data->blocks;
  313. struct mxs_ssp *ssp = &host->ssp;
  314. u32 ignore_crc, get_resp, long_resp, read;
  315. u32 ctrl0, cmd0, cmd1, val;
  316. ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ?
  317. 0 : BM_SSP_CTRL0_IGNORE_CRC;
  318. get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ?
  319. BM_SSP_CTRL0_GET_RESP : 0;
  320. long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ?
  321. BM_SSP_CTRL0_LONG_RESP : 0;
  322. if (data->flags & MMC_DATA_WRITE) {
  323. dma_data_dir = DMA_TO_DEVICE;
  324. slave_dirn = DMA_MEM_TO_DEV;
  325. read = 0;
  326. } else {
  327. dma_data_dir = DMA_FROM_DEVICE;
  328. slave_dirn = DMA_DEV_TO_MEM;
  329. read = BM_SSP_CTRL0_READ;
  330. }
  331. ctrl0 = BF_SSP(host->bus_width, CTRL0_BUS_WIDTH) |
  332. ignore_crc | get_resp | long_resp |
  333. BM_SSP_CTRL0_DATA_XFER | read |
  334. BM_SSP_CTRL0_WAIT_FOR_IRQ |
  335. BM_SSP_CTRL0_ENABLE;
  336. cmd0 = BF_SSP(cmd->opcode, CMD0_CMD);
  337. /* get logarithm to base 2 of block size for setting register */
  338. log2_blksz = ilog2(data->blksz);
  339. /*
  340. * take special care of the case that data size from data->sg
  341. * is not equal to blocks x blksz
  342. */
  343. for_each_sg(sgl, sg, sg_len, i)
  344. data_size += sg->length;
  345. if (data_size != data->blocks * data->blksz)
  346. blocks = 1;
  347. /* xfer count, block size and count need to be set differently */
  348. if (ssp_is_old(ssp)) {
  349. ctrl0 |= BF_SSP(data_size, CTRL0_XFER_COUNT);
  350. cmd0 |= BF_SSP(log2_blksz, CMD0_BLOCK_SIZE) |
  351. BF_SSP(blocks - 1, CMD0_BLOCK_COUNT);
  352. } else {
  353. writel(data_size, ssp->base + HW_SSP_XFER_SIZE);
  354. writel(BF_SSP(log2_blksz, BLOCK_SIZE_BLOCK_SIZE) |
  355. BF_SSP(blocks - 1, BLOCK_SIZE_BLOCK_COUNT),
  356. ssp->base + HW_SSP_BLOCK_SIZE);
  357. }
  358. if ((cmd->opcode == MMC_STOP_TRANSMISSION) ||
  359. (cmd->opcode == SD_IO_RW_EXTENDED))
  360. cmd0 |= BM_SSP_CMD0_APPEND_8CYC;
  361. cmd1 = cmd->arg;
  362. if (host->sdio_irq_en) {
  363. ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
  364. cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
  365. }
  366. /* set the timeout count */
  367. timeout = mxs_ns_to_ssp_ticks(ssp->clk_rate, data->timeout_ns);
  368. val = readl(ssp->base + HW_SSP_TIMING(ssp));
  369. val &= ~(BM_SSP_TIMING_TIMEOUT);
  370. val |= BF_SSP(timeout, TIMING_TIMEOUT);
  371. writel(val, ssp->base + HW_SSP_TIMING(ssp));
  372. /* pio */
  373. ssp->ssp_pio_words[0] = ctrl0;
  374. ssp->ssp_pio_words[1] = cmd0;
  375. ssp->ssp_pio_words[2] = cmd1;
  376. ssp->dma_dir = DMA_NONE;
  377. ssp->slave_dirn = DMA_TRANS_NONE;
  378. desc = mxs_mmc_prep_dma(host, 0);
  379. if (!desc)
  380. goto out;
  381. /* append data sg */
  382. WARN_ON(host->data != NULL);
  383. host->data = data;
  384. ssp->dma_dir = dma_data_dir;
  385. ssp->slave_dirn = slave_dirn;
  386. desc = mxs_mmc_prep_dma(host, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  387. if (!desc)
  388. goto out;
  389. dmaengine_submit(desc);
  390. dma_async_issue_pending(ssp->dmach);
  391. return;
  392. out:
  393. dev_warn(mmc_dev(host->mmc),
  394. "%s: failed to prep dma\n", __func__);
  395. }
  396. static void mxs_mmc_start_cmd(struct mxs_mmc_host *host,
  397. struct mmc_command *cmd)
  398. {
  399. host->cmd = cmd;
  400. switch (mmc_cmd_type(cmd)) {
  401. case MMC_CMD_BC:
  402. mxs_mmc_bc(host);
  403. break;
  404. case MMC_CMD_BCR:
  405. mxs_mmc_ac(host);
  406. break;
  407. case MMC_CMD_AC:
  408. mxs_mmc_ac(host);
  409. break;
  410. case MMC_CMD_ADTC:
  411. mxs_mmc_adtc(host);
  412. break;
  413. default:
  414. dev_warn(mmc_dev(host->mmc),
  415. "%s: unknown MMC command\n", __func__);
  416. break;
  417. }
  418. }
  419. static void mxs_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  420. {
  421. struct mxs_mmc_host *host = mmc_priv(mmc);
  422. WARN_ON(host->mrq != NULL);
  423. host->mrq = mrq;
  424. mxs_mmc_start_cmd(host, mrq->cmd);
  425. }
  426. static void mxs_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  427. {
  428. struct mxs_mmc_host *host = mmc_priv(mmc);
  429. if (ios->bus_width == MMC_BUS_WIDTH_8)
  430. host->bus_width = 2;
  431. else if (ios->bus_width == MMC_BUS_WIDTH_4)
  432. host->bus_width = 1;
  433. else
  434. host->bus_width = 0;
  435. if (ios->clock)
  436. mxs_ssp_set_clk_rate(&host->ssp, ios->clock);
  437. }
  438. static void mxs_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  439. {
  440. struct mxs_mmc_host *host = mmc_priv(mmc);
  441. struct mxs_ssp *ssp = &host->ssp;
  442. unsigned long flags;
  443. spin_lock_irqsave(&host->lock, flags);
  444. host->sdio_irq_en = enable;
  445. if (enable) {
  446. writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK,
  447. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  448. writel(BM_SSP_CTRL1_SDIO_IRQ_EN,
  449. ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_SET);
  450. } else {
  451. writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK,
  452. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
  453. writel(BM_SSP_CTRL1_SDIO_IRQ_EN,
  454. ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_CLR);
  455. }
  456. spin_unlock_irqrestore(&host->lock, flags);
  457. if (enable && readl(ssp->base + HW_SSP_STATUS(ssp)) &
  458. BM_SSP_STATUS_SDIO_IRQ)
  459. mmc_signal_sdio_irq(host->mmc);
  460. }
  461. static const struct mmc_host_ops mxs_mmc_ops = {
  462. .request = mxs_mmc_request,
  463. .get_ro = mxs_mmc_get_ro,
  464. .get_cd = mxs_mmc_get_cd,
  465. .set_ios = mxs_mmc_set_ios,
  466. .enable_sdio_irq = mxs_mmc_enable_sdio_irq,
  467. };
  468. static struct platform_device_id mxs_ssp_ids[] = {
  469. {
  470. .name = "imx23-mmc",
  471. .driver_data = IMX23_SSP,
  472. }, {
  473. .name = "imx28-mmc",
  474. .driver_data = IMX28_SSP,
  475. }, {
  476. /* sentinel */
  477. }
  478. };
  479. MODULE_DEVICE_TABLE(platform, mxs_ssp_ids);
  480. static const struct of_device_id mxs_mmc_dt_ids[] = {
  481. { .compatible = "fsl,imx23-mmc", .data = (void *) IMX23_SSP, },
  482. { .compatible = "fsl,imx28-mmc", .data = (void *) IMX28_SSP, },
  483. { /* sentinel */ }
  484. };
  485. MODULE_DEVICE_TABLE(of, mxs_mmc_dt_ids);
  486. static int mxs_mmc_probe(struct platform_device *pdev)
  487. {
  488. const struct of_device_id *of_id =
  489. of_match_device(mxs_mmc_dt_ids, &pdev->dev);
  490. struct device_node *np = pdev->dev.of_node;
  491. struct mxs_mmc_host *host;
  492. struct mmc_host *mmc;
  493. struct resource *iores;
  494. int ret = 0, irq_err;
  495. struct regulator *reg_vmmc;
  496. enum of_gpio_flags flags;
  497. struct mxs_ssp *ssp;
  498. u32 bus_width = 0;
  499. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  500. irq_err = platform_get_irq(pdev, 0);
  501. if (!iores || irq_err < 0)
  502. return -EINVAL;
  503. mmc = mmc_alloc_host(sizeof(struct mxs_mmc_host), &pdev->dev);
  504. if (!mmc)
  505. return -ENOMEM;
  506. host = mmc_priv(mmc);
  507. ssp = &host->ssp;
  508. ssp->dev = &pdev->dev;
  509. ssp->base = devm_ioremap_resource(&pdev->dev, iores);
  510. if (IS_ERR(ssp->base)) {
  511. ret = PTR_ERR(ssp->base);
  512. goto out_mmc_free;
  513. }
  514. ssp->devid = (enum mxs_ssp_id) of_id->data;
  515. host->mmc = mmc;
  516. host->sdio_irq_en = 0;
  517. reg_vmmc = devm_regulator_get(&pdev->dev, "vmmc");
  518. if (!IS_ERR(reg_vmmc)) {
  519. ret = regulator_enable(reg_vmmc);
  520. if (ret) {
  521. dev_err(&pdev->dev,
  522. "Failed to enable vmmc regulator: %d\n", ret);
  523. goto out_mmc_free;
  524. }
  525. }
  526. ssp->clk = devm_clk_get(&pdev->dev, NULL);
  527. if (IS_ERR(ssp->clk)) {
  528. ret = PTR_ERR(ssp->clk);
  529. goto out_mmc_free;
  530. }
  531. clk_prepare_enable(ssp->clk);
  532. ret = mxs_mmc_reset(host);
  533. if (ret) {
  534. dev_err(&pdev->dev, "Failed to reset mmc: %d\n", ret);
  535. goto out_clk_disable;
  536. }
  537. ssp->dmach = dma_request_slave_channel(&pdev->dev, "rx-tx");
  538. if (!ssp->dmach) {
  539. dev_err(mmc_dev(host->mmc),
  540. "%s: failed to request dma\n", __func__);
  541. ret = -ENODEV;
  542. goto out_clk_disable;
  543. }
  544. /* set mmc core parameters */
  545. mmc->ops = &mxs_mmc_ops;
  546. mmc->caps = MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
  547. MMC_CAP_SDIO_IRQ | MMC_CAP_NEEDS_POLL;
  548. of_property_read_u32(np, "bus-width", &bus_width);
  549. if (bus_width == 4)
  550. mmc->caps |= MMC_CAP_4_BIT_DATA;
  551. else if (bus_width == 8)
  552. mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA;
  553. host->broken_cd = of_property_read_bool(np, "broken-cd");
  554. host->non_removable = of_property_read_bool(np, "non-removable");
  555. if (host->non_removable)
  556. mmc->caps |= MMC_CAP_NONREMOVABLE;
  557. host->wp_gpio = of_get_named_gpio_flags(np, "wp-gpios", 0, &flags);
  558. if (flags & OF_GPIO_ACTIVE_LOW)
  559. host->wp_inverted = 1;
  560. host->cd_inverted = of_property_read_bool(np, "cd-inverted");
  561. mmc->f_min = 400000;
  562. mmc->f_max = 288000000;
  563. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  564. mmc->max_segs = 52;
  565. mmc->max_blk_size = 1 << 0xf;
  566. mmc->max_blk_count = (ssp_is_old(ssp)) ? 0xff : 0xffffff;
  567. mmc->max_req_size = (ssp_is_old(ssp)) ? 0xffff : 0xffffffff;
  568. mmc->max_seg_size = dma_get_max_seg_size(ssp->dmach->device->dev);
  569. platform_set_drvdata(pdev, mmc);
  570. ret = devm_request_irq(&pdev->dev, irq_err, mxs_mmc_irq_handler, 0,
  571. DRIVER_NAME, host);
  572. if (ret)
  573. goto out_free_dma;
  574. spin_lock_init(&host->lock);
  575. ret = mmc_add_host(mmc);
  576. if (ret)
  577. goto out_free_dma;
  578. dev_info(mmc_dev(host->mmc), "initialized\n");
  579. return 0;
  580. out_free_dma:
  581. if (ssp->dmach)
  582. dma_release_channel(ssp->dmach);
  583. out_clk_disable:
  584. clk_disable_unprepare(ssp->clk);
  585. out_mmc_free:
  586. mmc_free_host(mmc);
  587. return ret;
  588. }
  589. static int mxs_mmc_remove(struct platform_device *pdev)
  590. {
  591. struct mmc_host *mmc = platform_get_drvdata(pdev);
  592. struct mxs_mmc_host *host = mmc_priv(mmc);
  593. struct mxs_ssp *ssp = &host->ssp;
  594. mmc_remove_host(mmc);
  595. if (ssp->dmach)
  596. dma_release_channel(ssp->dmach);
  597. clk_disable_unprepare(ssp->clk);
  598. mmc_free_host(mmc);
  599. return 0;
  600. }
  601. #ifdef CONFIG_PM
  602. static int mxs_mmc_suspend(struct device *dev)
  603. {
  604. struct mmc_host *mmc = dev_get_drvdata(dev);
  605. struct mxs_mmc_host *host = mmc_priv(mmc);
  606. struct mxs_ssp *ssp = &host->ssp;
  607. int ret = 0;
  608. ret = mmc_suspend_host(mmc);
  609. clk_disable_unprepare(ssp->clk);
  610. return ret;
  611. }
  612. static int mxs_mmc_resume(struct device *dev)
  613. {
  614. struct mmc_host *mmc = dev_get_drvdata(dev);
  615. struct mxs_mmc_host *host = mmc_priv(mmc);
  616. struct mxs_ssp *ssp = &host->ssp;
  617. int ret = 0;
  618. clk_prepare_enable(ssp->clk);
  619. ret = mmc_resume_host(mmc);
  620. return ret;
  621. }
  622. static const struct dev_pm_ops mxs_mmc_pm_ops = {
  623. .suspend = mxs_mmc_suspend,
  624. .resume = mxs_mmc_resume,
  625. };
  626. #endif
  627. static struct platform_driver mxs_mmc_driver = {
  628. .probe = mxs_mmc_probe,
  629. .remove = mxs_mmc_remove,
  630. .id_table = mxs_ssp_ids,
  631. .driver = {
  632. .name = DRIVER_NAME,
  633. .owner = THIS_MODULE,
  634. #ifdef CONFIG_PM
  635. .pm = &mxs_mmc_pm_ops,
  636. #endif
  637. .of_match_table = mxs_mmc_dt_ids,
  638. },
  639. };
  640. module_platform_driver(mxs_mmc_driver);
  641. MODULE_DESCRIPTION("FREESCALE MXS MMC peripheral");
  642. MODULE_AUTHOR("Freescale Semiconductor");
  643. MODULE_LICENSE("GPL");
  644. MODULE_ALIAS("platform:" DRIVER_NAME);