mxs-mmc.c 20 KB

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