mxs-mmc.c 19 KB

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