mxs-mmc.c 20 KB

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