mxs-mmc.c 21 KB

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