xref: /openbmc/linux/drivers/mmc/host/mxs-mmc.c (revision 829c1bf4)
1e4243f13SShawn Guo /*
2e4243f13SShawn Guo  * Portions copyright (C) 2003 Russell King, PXA MMCI Driver
3e4243f13SShawn Guo  * Portions copyright (C) 2004-2005 Pierre Ossman, W83L51xD SD/MMC driver
4e4243f13SShawn Guo  *
5e4243f13SShawn Guo  * Copyright 2008 Embedded Alley Solutions, Inc.
6e4243f13SShawn Guo  * Copyright 2009-2011 Freescale Semiconductor, Inc.
7e4243f13SShawn Guo  *
8e4243f13SShawn Guo  * This program is free software; you can redistribute it and/or modify
9e4243f13SShawn Guo  * it under the terms of the GNU General Public License as published by
10e4243f13SShawn Guo  * the Free Software Foundation; either version 2 of the License, or
11e4243f13SShawn Guo  * (at your option) any later version.
12e4243f13SShawn Guo  *
13e4243f13SShawn Guo  * This program is distributed in the hope that it will be useful,
14e4243f13SShawn Guo  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15e4243f13SShawn Guo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16e4243f13SShawn Guo  * GNU General Public License for more details.
17e4243f13SShawn Guo  *
18e4243f13SShawn Guo  * You should have received a copy of the GNU General Public License along
19e4243f13SShawn Guo  * with this program; if not, write to the Free Software Foundation, Inc.,
20e4243f13SShawn Guo  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21e4243f13SShawn Guo  */
22e4243f13SShawn Guo 
23e4243f13SShawn Guo #include <linux/kernel.h>
24e4243f13SShawn Guo #include <linux/init.h>
25e4243f13SShawn Guo #include <linux/ioport.h>
266de4d817SShawn Guo #include <linux/of.h>
276de4d817SShawn Guo #include <linux/of_device.h>
286de4d817SShawn Guo #include <linux/of_gpio.h>
29e4243f13SShawn Guo #include <linux/platform_device.h>
30e4243f13SShawn Guo #include <linux/delay.h>
31e4243f13SShawn Guo #include <linux/interrupt.h>
32e4243f13SShawn Guo #include <linux/dma-mapping.h>
33e4243f13SShawn Guo #include <linux/dmaengine.h>
34e4243f13SShawn Guo #include <linux/highmem.h>
35e4243f13SShawn Guo #include <linux/clk.h>
36e4243f13SShawn Guo #include <linux/err.h>
37e4243f13SShawn Guo #include <linux/completion.h>
38e4243f13SShawn Guo #include <linux/mmc/host.h>
39e4243f13SShawn Guo #include <linux/mmc/mmc.h>
40e4243f13SShawn Guo #include <linux/mmc/sdio.h>
41e4243f13SShawn Guo #include <linux/gpio.h>
42e4243f13SShawn Guo #include <linux/regulator/consumer.h>
4388b47679SPaul Gortmaker #include <linux/module.h>
4439468604SHuang Shijie #include <linux/fsl/mxs-dma.h>
459c92cf24SShawn Guo #include <linux/pinctrl/consumer.h>
4670e60206SShawn Guo #include <linux/stmp_device.h>
4781f38ee8SShawn Guo #include <linux/mmc/mxs-mmc.h>
488be3d3b2SMarek Vasut #include <linux/spi/mxs-spi.h>
49e4243f13SShawn Guo 
50e4243f13SShawn Guo #define DRIVER_NAME	"mxs-mmc"
51e4243f13SShawn Guo 
52e4243f13SShawn Guo #define MXS_MMC_IRQ_BITS	(BM_SSP_CTRL1_SDIO_IRQ		| \
53e4243f13SShawn Guo 				 BM_SSP_CTRL1_RESP_ERR_IRQ	| \
54e4243f13SShawn Guo 				 BM_SSP_CTRL1_RESP_TIMEOUT_IRQ	| \
55e4243f13SShawn Guo 				 BM_SSP_CTRL1_DATA_TIMEOUT_IRQ	| \
56e4243f13SShawn Guo 				 BM_SSP_CTRL1_DATA_CRC_IRQ	| \
57e4243f13SShawn Guo 				 BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ	| \
58e4243f13SShawn Guo 				 BM_SSP_CTRL1_RECV_TIMEOUT_IRQ  | \
59e4243f13SShawn Guo 				 BM_SSP_CTRL1_FIFO_OVERRUN_IRQ)
60e4243f13SShawn Guo 
618be3d3b2SMarek Vasut /* card detect polling timeout */
628be3d3b2SMarek Vasut #define MXS_MMC_DETECT_TIMEOUT			(HZ/2)
63ef9b4d39SShawn Guo 
64e4243f13SShawn Guo struct mxs_mmc_host {
65829c1bf4SMarek Vasut 	struct mxs_ssp			ssp;
66829c1bf4SMarek Vasut 
67e4243f13SShawn Guo 	struct mmc_host			*mmc;
68e4243f13SShawn Guo 	struct mmc_request		*mrq;
69e4243f13SShawn Guo 	struct mmc_command		*cmd;
70e4243f13SShawn Guo 	struct mmc_data			*data;
71e4243f13SShawn Guo 
72b60188c8SShawn Guo 	int				dma_channel;
73e4243f13SShawn Guo 	struct dma_chan         	*dmach;
74e4243f13SShawn Guo 	struct mxs_dma_data		dma_data;
75e4243f13SShawn Guo 	unsigned int			dma_dir;
7605f5799cSVinod Koul 	enum dma_transfer_direction	slave_dirn;
77e4243f13SShawn Guo 	u32				ssp_pio_words[SSP_PIO_NUM];
78e4243f13SShawn Guo 
79e4243f13SShawn Guo 	unsigned char			bus_width;
80e4243f13SShawn Guo 	spinlock_t			lock;
81e4243f13SShawn Guo 	int				sdio_irq_en;
8231b0ff5eSShawn Guo 	int				wp_gpio;
83b6e76f10SMarek Vasut 	bool				wp_inverted;
84e4243f13SShawn Guo };
85e4243f13SShawn Guo 
86e4243f13SShawn Guo static int mxs_mmc_get_ro(struct mmc_host *mmc)
87e4243f13SShawn Guo {
88e4243f13SShawn Guo 	struct mxs_mmc_host *host = mmc_priv(mmc);
89b6e76f10SMarek Vasut 	int ret;
90e4243f13SShawn Guo 
9131b0ff5eSShawn Guo 	if (!gpio_is_valid(host->wp_gpio))
92e4243f13SShawn Guo 		return -EINVAL;
93e4243f13SShawn Guo 
94b6e76f10SMarek Vasut 	ret = gpio_get_value(host->wp_gpio);
95b6e76f10SMarek Vasut 
96b6e76f10SMarek Vasut 	if (host->wp_inverted)
97b6e76f10SMarek Vasut 		ret = !ret;
98b6e76f10SMarek Vasut 
99b6e76f10SMarek Vasut 	return ret;
100e4243f13SShawn Guo }
101e4243f13SShawn Guo 
102e4243f13SShawn Guo static int mxs_mmc_get_cd(struct mmc_host *mmc)
103e4243f13SShawn Guo {
104e4243f13SShawn Guo 	struct mxs_mmc_host *host = mmc_priv(mmc);
105829c1bf4SMarek Vasut 	struct mxs_ssp *ssp = &host->ssp;
106e4243f13SShawn Guo 
107829c1bf4SMarek Vasut 	return !(readl(ssp->base + HW_SSP_STATUS(ssp)) &
108e4243f13SShawn Guo 		 BM_SSP_STATUS_CARD_DETECT);
109e4243f13SShawn Guo }
110e4243f13SShawn Guo 
111e4243f13SShawn Guo static void mxs_mmc_reset(struct mxs_mmc_host *host)
112e4243f13SShawn Guo {
113829c1bf4SMarek Vasut 	struct mxs_ssp *ssp = &host->ssp;
114e4243f13SShawn Guo 	u32 ctrl0, ctrl1;
115e4243f13SShawn Guo 
116829c1bf4SMarek Vasut 	stmp_reset_block(ssp->base);
117e4243f13SShawn Guo 
118e4243f13SShawn Guo 	ctrl0 = BM_SSP_CTRL0_IGNORE_CRC;
119e4243f13SShawn Guo 	ctrl1 = BF_SSP(0x3, CTRL1_SSP_MODE) |
120e4243f13SShawn Guo 		BF_SSP(0x7, CTRL1_WORD_LENGTH) |
121e4243f13SShawn Guo 		BM_SSP_CTRL1_DMA_ENABLE |
122e4243f13SShawn Guo 		BM_SSP_CTRL1_POLARITY |
123e4243f13SShawn Guo 		BM_SSP_CTRL1_RECV_TIMEOUT_IRQ_EN |
124e4243f13SShawn Guo 		BM_SSP_CTRL1_DATA_CRC_IRQ_EN |
125e4243f13SShawn Guo 		BM_SSP_CTRL1_DATA_TIMEOUT_IRQ_EN |
126e4243f13SShawn Guo 		BM_SSP_CTRL1_RESP_TIMEOUT_IRQ_EN |
127e4243f13SShawn Guo 		BM_SSP_CTRL1_RESP_ERR_IRQ_EN;
128e4243f13SShawn Guo 
129e4243f13SShawn Guo 	writel(BF_SSP(0xffff, TIMING_TIMEOUT) |
130e4243f13SShawn Guo 	       BF_SSP(2, TIMING_CLOCK_DIVIDE) |
131e4243f13SShawn Guo 	       BF_SSP(0, TIMING_CLOCK_RATE),
132829c1bf4SMarek Vasut 	       ssp->base + HW_SSP_TIMING(ssp));
133e4243f13SShawn Guo 
134e4243f13SShawn Guo 	if (host->sdio_irq_en) {
135e4243f13SShawn Guo 		ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
136e4243f13SShawn Guo 		ctrl1 |= BM_SSP_CTRL1_SDIO_IRQ_EN;
137e4243f13SShawn Guo 	}
138e4243f13SShawn Guo 
139829c1bf4SMarek Vasut 	writel(ctrl0, ssp->base + HW_SSP_CTRL0);
140829c1bf4SMarek Vasut 	writel(ctrl1, ssp->base + HW_SSP_CTRL1(ssp));
141e4243f13SShawn Guo }
142e4243f13SShawn Guo 
143e4243f13SShawn Guo static void mxs_mmc_start_cmd(struct mxs_mmc_host *host,
144e4243f13SShawn Guo 			      struct mmc_command *cmd);
145e4243f13SShawn Guo 
146e4243f13SShawn Guo static void mxs_mmc_request_done(struct mxs_mmc_host *host)
147e4243f13SShawn Guo {
148e4243f13SShawn Guo 	struct mmc_command *cmd = host->cmd;
149e4243f13SShawn Guo 	struct mmc_data *data = host->data;
150e4243f13SShawn Guo 	struct mmc_request *mrq = host->mrq;
151829c1bf4SMarek Vasut 	struct mxs_ssp *ssp = &host->ssp;
152e4243f13SShawn Guo 
153e4243f13SShawn Guo 	if (mmc_resp_type(cmd) & MMC_RSP_PRESENT) {
154e4243f13SShawn Guo 		if (mmc_resp_type(cmd) & MMC_RSP_136) {
155829c1bf4SMarek Vasut 			cmd->resp[3] = readl(ssp->base + HW_SSP_SDRESP0(ssp));
156829c1bf4SMarek Vasut 			cmd->resp[2] = readl(ssp->base + HW_SSP_SDRESP1(ssp));
157829c1bf4SMarek Vasut 			cmd->resp[1] = readl(ssp->base + HW_SSP_SDRESP2(ssp));
158829c1bf4SMarek Vasut 			cmd->resp[0] = readl(ssp->base + HW_SSP_SDRESP3(ssp));
159e4243f13SShawn Guo 		} else {
160829c1bf4SMarek Vasut 			cmd->resp[0] = readl(ssp->base + HW_SSP_SDRESP0(ssp));
161e4243f13SShawn Guo 		}
162e4243f13SShawn Guo 	}
163e4243f13SShawn Guo 
164e4243f13SShawn Guo 	if (data) {
165e4243f13SShawn Guo 		dma_unmap_sg(mmc_dev(host->mmc), data->sg,
166e4243f13SShawn Guo 			     data->sg_len, host->dma_dir);
167e4243f13SShawn Guo 		/*
168e4243f13SShawn Guo 		 * If there was an error on any block, we mark all
169e4243f13SShawn Guo 		 * data blocks as being in error.
170e4243f13SShawn Guo 		 */
171e4243f13SShawn Guo 		if (!data->error)
172e4243f13SShawn Guo 			data->bytes_xfered = data->blocks * data->blksz;
173e4243f13SShawn Guo 		else
174e4243f13SShawn Guo 			data->bytes_xfered = 0;
175e4243f13SShawn Guo 
176e4243f13SShawn Guo 		host->data = NULL;
177e4243f13SShawn Guo 		if (mrq->stop) {
178e4243f13SShawn Guo 			mxs_mmc_start_cmd(host, mrq->stop);
179e4243f13SShawn Guo 			return;
180e4243f13SShawn Guo 		}
181e4243f13SShawn Guo 	}
182e4243f13SShawn Guo 
183e4243f13SShawn Guo 	host->mrq = NULL;
184e4243f13SShawn Guo 	mmc_request_done(host->mmc, mrq);
185e4243f13SShawn Guo }
186e4243f13SShawn Guo 
187e4243f13SShawn Guo static void mxs_mmc_dma_irq_callback(void *param)
188e4243f13SShawn Guo {
189e4243f13SShawn Guo 	struct mxs_mmc_host *host = param;
190e4243f13SShawn Guo 
191e4243f13SShawn Guo 	mxs_mmc_request_done(host);
192e4243f13SShawn Guo }
193e4243f13SShawn Guo 
194e4243f13SShawn Guo static irqreturn_t mxs_mmc_irq_handler(int irq, void *dev_id)
195e4243f13SShawn Guo {
196e4243f13SShawn Guo 	struct mxs_mmc_host *host = dev_id;
197e4243f13SShawn Guo 	struct mmc_command *cmd = host->cmd;
198e4243f13SShawn Guo 	struct mmc_data *data = host->data;
199829c1bf4SMarek Vasut 	struct mxs_ssp *ssp = &host->ssp;
200e4243f13SShawn Guo 	u32 stat;
201e4243f13SShawn Guo 
202e4243f13SShawn Guo 	spin_lock(&host->lock);
203e4243f13SShawn Guo 
204829c1bf4SMarek Vasut 	stat = readl(ssp->base + HW_SSP_CTRL1(ssp));
205e4243f13SShawn Guo 	writel(stat & MXS_MMC_IRQ_BITS,
206829c1bf4SMarek Vasut 	       ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_CLR);
207e4243f13SShawn Guo 
208e4243f13SShawn Guo 	if ((stat & BM_SSP_CTRL1_SDIO_IRQ) && (stat & BM_SSP_CTRL1_SDIO_IRQ_EN))
209e4243f13SShawn Guo 		mmc_signal_sdio_irq(host->mmc);
210e4243f13SShawn Guo 
211e4243f13SShawn Guo 	spin_unlock(&host->lock);
212e4243f13SShawn Guo 
213e4243f13SShawn Guo 	if (stat & BM_SSP_CTRL1_RESP_TIMEOUT_IRQ)
214e4243f13SShawn Guo 		cmd->error = -ETIMEDOUT;
215e4243f13SShawn Guo 	else if (stat & BM_SSP_CTRL1_RESP_ERR_IRQ)
216e4243f13SShawn Guo 		cmd->error = -EIO;
217e4243f13SShawn Guo 
218e4243f13SShawn Guo 	if (data) {
219e4243f13SShawn Guo 		if (stat & (BM_SSP_CTRL1_DATA_TIMEOUT_IRQ |
220e4243f13SShawn Guo 			    BM_SSP_CTRL1_RECV_TIMEOUT_IRQ))
221e4243f13SShawn Guo 			data->error = -ETIMEDOUT;
222e4243f13SShawn Guo 		else if (stat & BM_SSP_CTRL1_DATA_CRC_IRQ)
223e4243f13SShawn Guo 			data->error = -EILSEQ;
224e4243f13SShawn Guo 		else if (stat & (BM_SSP_CTRL1_FIFO_UNDERRUN_IRQ |
225e4243f13SShawn Guo 				 BM_SSP_CTRL1_FIFO_OVERRUN_IRQ))
226e4243f13SShawn Guo 			data->error = -EIO;
227e4243f13SShawn Guo 	}
228e4243f13SShawn Guo 
229e4243f13SShawn Guo 	return IRQ_HANDLED;
230e4243f13SShawn Guo }
231e4243f13SShawn Guo 
232e4243f13SShawn Guo static struct dma_async_tx_descriptor *mxs_mmc_prep_dma(
233921de864SHuang Shijie 	struct mxs_mmc_host *host, unsigned long flags)
234e4243f13SShawn Guo {
235e4243f13SShawn Guo 	struct dma_async_tx_descriptor *desc;
236e4243f13SShawn Guo 	struct mmc_data *data = host->data;
237e4243f13SShawn Guo 	struct scatterlist * sgl;
238e4243f13SShawn Guo 	unsigned int sg_len;
239e4243f13SShawn Guo 
240e4243f13SShawn Guo 	if (data) {
241e4243f13SShawn Guo 		/* data */
242e4243f13SShawn Guo 		dma_map_sg(mmc_dev(host->mmc), data->sg,
243e4243f13SShawn Guo 			   data->sg_len, host->dma_dir);
244e4243f13SShawn Guo 		sgl = data->sg;
245e4243f13SShawn Guo 		sg_len = data->sg_len;
246e4243f13SShawn Guo 	} else {
247e4243f13SShawn Guo 		/* pio */
248e4243f13SShawn Guo 		sgl = (struct scatterlist *) host->ssp_pio_words;
249e4243f13SShawn Guo 		sg_len = SSP_PIO_NUM;
250e4243f13SShawn Guo 	}
251e4243f13SShawn Guo 
25216052827SAlexandre Bounine 	desc = dmaengine_prep_slave_sg(host->dmach,
253921de864SHuang Shijie 				sgl, sg_len, host->slave_dirn, flags);
254e4243f13SShawn Guo 	if (desc) {
255e4243f13SShawn Guo 		desc->callback = mxs_mmc_dma_irq_callback;
256e4243f13SShawn Guo 		desc->callback_param = host;
257e4243f13SShawn Guo 	} else {
258e4243f13SShawn Guo 		if (data)
259e4243f13SShawn Guo 			dma_unmap_sg(mmc_dev(host->mmc), data->sg,
260e4243f13SShawn Guo 				     data->sg_len, host->dma_dir);
261e4243f13SShawn Guo 	}
262e4243f13SShawn Guo 
263e4243f13SShawn Guo 	return desc;
264e4243f13SShawn Guo }
265e4243f13SShawn Guo 
266e4243f13SShawn Guo static void mxs_mmc_bc(struct mxs_mmc_host *host)
267e4243f13SShawn Guo {
268e4243f13SShawn Guo 	struct mmc_command *cmd = host->cmd;
269e4243f13SShawn Guo 	struct dma_async_tx_descriptor *desc;
270e4243f13SShawn Guo 	u32 ctrl0, cmd0, cmd1;
271e4243f13SShawn Guo 
272e4243f13SShawn Guo 	ctrl0 = BM_SSP_CTRL0_ENABLE | BM_SSP_CTRL0_IGNORE_CRC;
273e4243f13SShawn Guo 	cmd0 = BF_SSP(cmd->opcode, CMD0_CMD) | BM_SSP_CMD0_APPEND_8CYC;
274e4243f13SShawn Guo 	cmd1 = cmd->arg;
275e4243f13SShawn Guo 
276e4243f13SShawn Guo 	if (host->sdio_irq_en) {
277e4243f13SShawn Guo 		ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
278e4243f13SShawn Guo 		cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
279e4243f13SShawn Guo 	}
280e4243f13SShawn Guo 
281e4243f13SShawn Guo 	host->ssp_pio_words[0] = ctrl0;
282e4243f13SShawn Guo 	host->ssp_pio_words[1] = cmd0;
283e4243f13SShawn Guo 	host->ssp_pio_words[2] = cmd1;
284e4243f13SShawn Guo 	host->dma_dir = DMA_NONE;
285a4e3e86dSShawn Guo 	host->slave_dirn = DMA_TRANS_NONE;
286921de864SHuang Shijie 	desc = mxs_mmc_prep_dma(host, DMA_CTRL_ACK);
287e4243f13SShawn Guo 	if (!desc)
288e4243f13SShawn Guo 		goto out;
289e4243f13SShawn Guo 
290e4243f13SShawn Guo 	dmaengine_submit(desc);
291d04525edSShawn Guo 	dma_async_issue_pending(host->dmach);
292e4243f13SShawn Guo 	return;
293e4243f13SShawn Guo 
294e4243f13SShawn Guo out:
295e4243f13SShawn Guo 	dev_warn(mmc_dev(host->mmc),
296e4243f13SShawn Guo 		 "%s: failed to prep dma\n", __func__);
297e4243f13SShawn Guo }
298e4243f13SShawn Guo 
299e4243f13SShawn Guo static void mxs_mmc_ac(struct mxs_mmc_host *host)
300e4243f13SShawn Guo {
301e4243f13SShawn Guo 	struct mmc_command *cmd = host->cmd;
302e4243f13SShawn Guo 	struct dma_async_tx_descriptor *desc;
303e4243f13SShawn Guo 	u32 ignore_crc, get_resp, long_resp;
304e4243f13SShawn Guo 	u32 ctrl0, cmd0, cmd1;
305e4243f13SShawn Guo 
306e4243f13SShawn Guo 	ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ?
307e4243f13SShawn Guo 			0 : BM_SSP_CTRL0_IGNORE_CRC;
308e4243f13SShawn Guo 	get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ?
309e4243f13SShawn Guo 			BM_SSP_CTRL0_GET_RESP : 0;
310e4243f13SShawn Guo 	long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ?
311e4243f13SShawn Guo 			BM_SSP_CTRL0_LONG_RESP : 0;
312e4243f13SShawn Guo 
313e4243f13SShawn Guo 	ctrl0 = BM_SSP_CTRL0_ENABLE | ignore_crc | get_resp | long_resp;
314e4243f13SShawn Guo 	cmd0 = BF_SSP(cmd->opcode, CMD0_CMD);
315e4243f13SShawn Guo 	cmd1 = cmd->arg;
316e4243f13SShawn Guo 
317e4243f13SShawn Guo 	if (host->sdio_irq_en) {
318e4243f13SShawn Guo 		ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
319e4243f13SShawn Guo 		cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
320e4243f13SShawn Guo 	}
321e4243f13SShawn Guo 
322e4243f13SShawn Guo 	host->ssp_pio_words[0] = ctrl0;
323e4243f13SShawn Guo 	host->ssp_pio_words[1] = cmd0;
324e4243f13SShawn Guo 	host->ssp_pio_words[2] = cmd1;
325e4243f13SShawn Guo 	host->dma_dir = DMA_NONE;
326a4e3e86dSShawn Guo 	host->slave_dirn = DMA_TRANS_NONE;
327921de864SHuang Shijie 	desc = mxs_mmc_prep_dma(host, DMA_CTRL_ACK);
328e4243f13SShawn Guo 	if (!desc)
329e4243f13SShawn Guo 		goto out;
330e4243f13SShawn Guo 
331e4243f13SShawn Guo 	dmaengine_submit(desc);
332d04525edSShawn Guo 	dma_async_issue_pending(host->dmach);
333e4243f13SShawn Guo 	return;
334e4243f13SShawn Guo 
335e4243f13SShawn Guo out:
336e4243f13SShawn Guo 	dev_warn(mmc_dev(host->mmc),
337e4243f13SShawn Guo 		 "%s: failed to prep dma\n", __func__);
338e4243f13SShawn Guo }
339e4243f13SShawn Guo 
340e4243f13SShawn Guo static unsigned short mxs_ns_to_ssp_ticks(unsigned clock_rate, unsigned ns)
341e4243f13SShawn Guo {
342e4243f13SShawn Guo 	const unsigned int ssp_timeout_mul = 4096;
343e4243f13SShawn Guo 	/*
344e4243f13SShawn Guo 	 * Calculate ticks in ms since ns are large numbers
345e4243f13SShawn Guo 	 * and might overflow
346e4243f13SShawn Guo 	 */
347e4243f13SShawn Guo 	const unsigned int clock_per_ms = clock_rate / 1000;
348e4243f13SShawn Guo 	const unsigned int ms = ns / 1000;
349e4243f13SShawn Guo 	const unsigned int ticks = ms * clock_per_ms;
350e4243f13SShawn Guo 	const unsigned int ssp_ticks = ticks / ssp_timeout_mul;
351e4243f13SShawn Guo 
352e4243f13SShawn Guo 	WARN_ON(ssp_ticks == 0);
353e4243f13SShawn Guo 	return ssp_ticks;
354e4243f13SShawn Guo }
355e4243f13SShawn Guo 
356e4243f13SShawn Guo static void mxs_mmc_adtc(struct mxs_mmc_host *host)
357e4243f13SShawn Guo {
358e4243f13SShawn Guo 	struct mmc_command *cmd = host->cmd;
359e4243f13SShawn Guo 	struct mmc_data *data = cmd->data;
360e4243f13SShawn Guo 	struct dma_async_tx_descriptor *desc;
361e4243f13SShawn Guo 	struct scatterlist *sgl = data->sg, *sg;
362e4243f13SShawn Guo 	unsigned int sg_len = data->sg_len;
363e4243f13SShawn Guo 	int i;
364e4243f13SShawn Guo 
365e4243f13SShawn Guo 	unsigned short dma_data_dir, timeout;
36605f5799cSVinod Koul 	enum dma_transfer_direction slave_dirn;
367e4243f13SShawn Guo 	unsigned int data_size = 0, log2_blksz;
368e4243f13SShawn Guo 	unsigned int blocks = data->blocks;
369e4243f13SShawn Guo 
370829c1bf4SMarek Vasut 	struct mxs_ssp *ssp = &host->ssp;
371829c1bf4SMarek Vasut 
372e4243f13SShawn Guo 	u32 ignore_crc, get_resp, long_resp, read;
373e4243f13SShawn Guo 	u32 ctrl0, cmd0, cmd1, val;
374e4243f13SShawn Guo 
375e4243f13SShawn Guo 	ignore_crc = (mmc_resp_type(cmd) & MMC_RSP_CRC) ?
376e4243f13SShawn Guo 			0 : BM_SSP_CTRL0_IGNORE_CRC;
377e4243f13SShawn Guo 	get_resp = (mmc_resp_type(cmd) & MMC_RSP_PRESENT) ?
378e4243f13SShawn Guo 			BM_SSP_CTRL0_GET_RESP : 0;
379e4243f13SShawn Guo 	long_resp = (mmc_resp_type(cmd) & MMC_RSP_136) ?
380e4243f13SShawn Guo 			BM_SSP_CTRL0_LONG_RESP : 0;
381e4243f13SShawn Guo 
382e4243f13SShawn Guo 	if (data->flags & MMC_DATA_WRITE) {
383e4243f13SShawn Guo 		dma_data_dir = DMA_TO_DEVICE;
38405f5799cSVinod Koul 		slave_dirn = DMA_MEM_TO_DEV;
385e4243f13SShawn Guo 		read = 0;
386e4243f13SShawn Guo 	} else {
387e4243f13SShawn Guo 		dma_data_dir = DMA_FROM_DEVICE;
38805f5799cSVinod Koul 		slave_dirn = DMA_DEV_TO_MEM;
389e4243f13SShawn Guo 		read = BM_SSP_CTRL0_READ;
390e4243f13SShawn Guo 	}
391e4243f13SShawn Guo 
392e4243f13SShawn Guo 	ctrl0 = BF_SSP(host->bus_width, CTRL0_BUS_WIDTH) |
393e4243f13SShawn Guo 		ignore_crc | get_resp | long_resp |
394e4243f13SShawn Guo 		BM_SSP_CTRL0_DATA_XFER | read |
395e4243f13SShawn Guo 		BM_SSP_CTRL0_WAIT_FOR_IRQ |
396e4243f13SShawn Guo 		BM_SSP_CTRL0_ENABLE;
397e4243f13SShawn Guo 
398e4243f13SShawn Guo 	cmd0 = BF_SSP(cmd->opcode, CMD0_CMD);
399e4243f13SShawn Guo 
400e4243f13SShawn Guo 	/* get logarithm to base 2 of block size for setting register */
401e4243f13SShawn Guo 	log2_blksz = ilog2(data->blksz);
402e4243f13SShawn Guo 
403e4243f13SShawn Guo 	/*
404e4243f13SShawn Guo 	 * take special care of the case that data size from data->sg
405e4243f13SShawn Guo 	 * is not equal to blocks x blksz
406e4243f13SShawn Guo 	 */
407e4243f13SShawn Guo 	for_each_sg(sgl, sg, sg_len, i)
408e4243f13SShawn Guo 		data_size += sg->length;
409e4243f13SShawn Guo 
410e4243f13SShawn Guo 	if (data_size != data->blocks * data->blksz)
411e4243f13SShawn Guo 		blocks = 1;
412e4243f13SShawn Guo 
413e4243f13SShawn Guo 	/* xfer count, block size and count need to be set differently */
414829c1bf4SMarek Vasut 	if (ssp_is_old(ssp)) {
415e4243f13SShawn Guo 		ctrl0 |= BF_SSP(data_size, CTRL0_XFER_COUNT);
416e4243f13SShawn Guo 		cmd0 |= BF_SSP(log2_blksz, CMD0_BLOCK_SIZE) |
417e4243f13SShawn Guo 			BF_SSP(blocks - 1, CMD0_BLOCK_COUNT);
418e4243f13SShawn Guo 	} else {
419829c1bf4SMarek Vasut 		writel(data_size, ssp->base + HW_SSP_XFER_SIZE);
420e4243f13SShawn Guo 		writel(BF_SSP(log2_blksz, BLOCK_SIZE_BLOCK_SIZE) |
421e4243f13SShawn Guo 		       BF_SSP(blocks - 1, BLOCK_SIZE_BLOCK_COUNT),
422829c1bf4SMarek Vasut 		       ssp->base + HW_SSP_BLOCK_SIZE);
423e4243f13SShawn Guo 	}
424e4243f13SShawn Guo 
425e4243f13SShawn Guo 	if ((cmd->opcode == MMC_STOP_TRANSMISSION) ||
426e4243f13SShawn Guo 	    (cmd->opcode == SD_IO_RW_EXTENDED))
427e4243f13SShawn Guo 		cmd0 |= BM_SSP_CMD0_APPEND_8CYC;
428e4243f13SShawn Guo 
429e4243f13SShawn Guo 	cmd1 = cmd->arg;
430e4243f13SShawn Guo 
431e4243f13SShawn Guo 	if (host->sdio_irq_en) {
432e4243f13SShawn Guo 		ctrl0 |= BM_SSP_CTRL0_SDIO_IRQ_CHECK;
433e4243f13SShawn Guo 		cmd0 |= BM_SSP_CMD0_CONT_CLKING_EN | BM_SSP_CMD0_SLOW_CLKING_EN;
434e4243f13SShawn Guo 	}
435e4243f13SShawn Guo 
436e4243f13SShawn Guo 	/* set the timeout count */
437829c1bf4SMarek Vasut 	timeout = mxs_ns_to_ssp_ticks(ssp->clk_rate, data->timeout_ns);
438829c1bf4SMarek Vasut 	val = readl(ssp->base + HW_SSP_TIMING(ssp));
439e4243f13SShawn Guo 	val &= ~(BM_SSP_TIMING_TIMEOUT);
440e4243f13SShawn Guo 	val |= BF_SSP(timeout, TIMING_TIMEOUT);
441829c1bf4SMarek Vasut 	writel(val, ssp->base + HW_SSP_TIMING(ssp));
442e4243f13SShawn Guo 
443e4243f13SShawn Guo 	/* pio */
444e4243f13SShawn Guo 	host->ssp_pio_words[0] = ctrl0;
445e4243f13SShawn Guo 	host->ssp_pio_words[1] = cmd0;
446e4243f13SShawn Guo 	host->ssp_pio_words[2] = cmd1;
447e4243f13SShawn Guo 	host->dma_dir = DMA_NONE;
448a4e3e86dSShawn Guo 	host->slave_dirn = DMA_TRANS_NONE;
449e4243f13SShawn Guo 	desc = mxs_mmc_prep_dma(host, 0);
450e4243f13SShawn Guo 	if (!desc)
451e4243f13SShawn Guo 		goto out;
452e4243f13SShawn Guo 
453e4243f13SShawn Guo 	/* append data sg */
454e4243f13SShawn Guo 	WARN_ON(host->data != NULL);
455e4243f13SShawn Guo 	host->data = data;
456e4243f13SShawn Guo 	host->dma_dir = dma_data_dir;
45705f5799cSVinod Koul 	host->slave_dirn = slave_dirn;
458921de864SHuang Shijie 	desc = mxs_mmc_prep_dma(host, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
459e4243f13SShawn Guo 	if (!desc)
460e4243f13SShawn Guo 		goto out;
461e4243f13SShawn Guo 
462e4243f13SShawn Guo 	dmaengine_submit(desc);
463d04525edSShawn Guo 	dma_async_issue_pending(host->dmach);
464e4243f13SShawn Guo 	return;
465e4243f13SShawn Guo out:
466e4243f13SShawn Guo 	dev_warn(mmc_dev(host->mmc),
467e4243f13SShawn Guo 		 "%s: failed to prep dma\n", __func__);
468e4243f13SShawn Guo }
469e4243f13SShawn Guo 
470e4243f13SShawn Guo static void mxs_mmc_start_cmd(struct mxs_mmc_host *host,
471e4243f13SShawn Guo 			      struct mmc_command *cmd)
472e4243f13SShawn Guo {
473e4243f13SShawn Guo 	host->cmd = cmd;
474e4243f13SShawn Guo 
475e4243f13SShawn Guo 	switch (mmc_cmd_type(cmd)) {
476e4243f13SShawn Guo 	case MMC_CMD_BC:
477e4243f13SShawn Guo 		mxs_mmc_bc(host);
478e4243f13SShawn Guo 		break;
479e4243f13SShawn Guo 	case MMC_CMD_BCR:
480e4243f13SShawn Guo 		mxs_mmc_ac(host);
481e4243f13SShawn Guo 		break;
482e4243f13SShawn Guo 	case MMC_CMD_AC:
483e4243f13SShawn Guo 		mxs_mmc_ac(host);
484e4243f13SShawn Guo 		break;
485e4243f13SShawn Guo 	case MMC_CMD_ADTC:
486e4243f13SShawn Guo 		mxs_mmc_adtc(host);
487e4243f13SShawn Guo 		break;
488e4243f13SShawn Guo 	default:
489e4243f13SShawn Guo 		dev_warn(mmc_dev(host->mmc),
490e4243f13SShawn Guo 			 "%s: unknown MMC command\n", __func__);
491e4243f13SShawn Guo 		break;
492e4243f13SShawn Guo 	}
493e4243f13SShawn Guo }
494e4243f13SShawn Guo 
495e4243f13SShawn Guo static void mxs_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
496e4243f13SShawn Guo {
497e4243f13SShawn Guo 	struct mxs_mmc_host *host = mmc_priv(mmc);
498e4243f13SShawn Guo 
499e4243f13SShawn Guo 	WARN_ON(host->mrq != NULL);
500e4243f13SShawn Guo 	host->mrq = mrq;
501e4243f13SShawn Guo 	mxs_mmc_start_cmd(host, mrq->cmd);
502e4243f13SShawn Guo }
503e4243f13SShawn Guo 
504e4243f13SShawn Guo static void mxs_mmc_set_clk_rate(struct mxs_mmc_host *host, unsigned int rate)
505e4243f13SShawn Guo {
506829c1bf4SMarek Vasut 	struct mxs_ssp *ssp = &host->ssp;
507d982dcdcSKoen Beel 	unsigned int ssp_clk, ssp_sck;
508d982dcdcSKoen Beel 	u32 clock_divide, clock_rate;
509e4243f13SShawn Guo 	u32 val;
510e4243f13SShawn Guo 
511829c1bf4SMarek Vasut 	ssp_clk = clk_get_rate(ssp->clk);
512e4243f13SShawn Guo 
513d982dcdcSKoen Beel 	for (clock_divide = 2; clock_divide <= 254; clock_divide += 2) {
514d982dcdcSKoen Beel 		clock_rate = DIV_ROUND_UP(ssp_clk, rate * clock_divide);
515d982dcdcSKoen Beel 		clock_rate = (clock_rate > 0) ? clock_rate - 1 : 0;
516d982dcdcSKoen Beel 		if (clock_rate <= 255)
517e4243f13SShawn Guo 			break;
518e4243f13SShawn Guo 	}
519e4243f13SShawn Guo 
520d982dcdcSKoen Beel 	if (clock_divide > 254) {
521e4243f13SShawn Guo 		dev_err(mmc_dev(host->mmc),
522e4243f13SShawn Guo 			"%s: cannot set clock to %d\n", __func__, rate);
523e4243f13SShawn Guo 		return;
524e4243f13SShawn Guo 	}
525e4243f13SShawn Guo 
526d982dcdcSKoen Beel 	ssp_sck = ssp_clk / clock_divide / (1 + clock_rate);
527e4243f13SShawn Guo 
528829c1bf4SMarek Vasut 	val = readl(ssp->base + HW_SSP_TIMING(ssp));
529e4243f13SShawn Guo 	val &= ~(BM_SSP_TIMING_CLOCK_DIVIDE | BM_SSP_TIMING_CLOCK_RATE);
530d982dcdcSKoen Beel 	val |= BF_SSP(clock_divide, TIMING_CLOCK_DIVIDE);
531d982dcdcSKoen Beel 	val |= BF_SSP(clock_rate, TIMING_CLOCK_RATE);
532829c1bf4SMarek Vasut 	writel(val, ssp->base + HW_SSP_TIMING(ssp));
533e4243f13SShawn Guo 
534829c1bf4SMarek Vasut 	ssp->clk_rate = ssp_sck;
535e4243f13SShawn Guo 
536e4243f13SShawn Guo 	dev_dbg(mmc_dev(host->mmc),
537d982dcdcSKoen Beel 		"%s: clock_divide %d, clock_rate %d, ssp_clk %d, rate_actual %d, rate_requested %d\n",
538d982dcdcSKoen Beel 		__func__, clock_divide, clock_rate, ssp_clk, ssp_sck, rate);
539e4243f13SShawn Guo }
540e4243f13SShawn Guo 
541e4243f13SShawn Guo static void mxs_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
542e4243f13SShawn Guo {
543e4243f13SShawn Guo 	struct mxs_mmc_host *host = mmc_priv(mmc);
544e4243f13SShawn Guo 
545e4243f13SShawn Guo 	if (ios->bus_width == MMC_BUS_WIDTH_8)
546e4243f13SShawn Guo 		host->bus_width = 2;
547e4243f13SShawn Guo 	else if (ios->bus_width == MMC_BUS_WIDTH_4)
548e4243f13SShawn Guo 		host->bus_width = 1;
549e4243f13SShawn Guo 	else
550e4243f13SShawn Guo 		host->bus_width = 0;
551e4243f13SShawn Guo 
552e4243f13SShawn Guo 	if (ios->clock)
553e4243f13SShawn Guo 		mxs_mmc_set_clk_rate(host, ios->clock);
554e4243f13SShawn Guo }
555e4243f13SShawn Guo 
556e4243f13SShawn Guo static void mxs_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
557e4243f13SShawn Guo {
558e4243f13SShawn Guo 	struct mxs_mmc_host *host = mmc_priv(mmc);
559829c1bf4SMarek Vasut 	struct mxs_ssp *ssp = &host->ssp;
560e4243f13SShawn Guo 	unsigned long flags;
561e4243f13SShawn Guo 
562e4243f13SShawn Guo 	spin_lock_irqsave(&host->lock, flags);
563e4243f13SShawn Guo 
564e4243f13SShawn Guo 	host->sdio_irq_en = enable;
565e4243f13SShawn Guo 
566e4243f13SShawn Guo 	if (enable) {
567e4243f13SShawn Guo 		writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK,
568829c1bf4SMarek Vasut 		       ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
569e4243f13SShawn Guo 		writel(BM_SSP_CTRL1_SDIO_IRQ_EN,
570829c1bf4SMarek Vasut 		       ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_SET);
571e4243f13SShawn Guo 
572829c1bf4SMarek Vasut 		if (readl(ssp->base + HW_SSP_STATUS(ssp)) &
573e0bf141dSShawn Guo 				BM_SSP_STATUS_SDIO_IRQ)
574e4243f13SShawn Guo 			mmc_signal_sdio_irq(host->mmc);
575e4243f13SShawn Guo 
576e4243f13SShawn Guo 	} else {
577e4243f13SShawn Guo 		writel(BM_SSP_CTRL0_SDIO_IRQ_CHECK,
578829c1bf4SMarek Vasut 		       ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
579e4243f13SShawn Guo 		writel(BM_SSP_CTRL1_SDIO_IRQ_EN,
580829c1bf4SMarek Vasut 		       ssp->base + HW_SSP_CTRL1(ssp) + STMP_OFFSET_REG_CLR);
581e4243f13SShawn Guo 	}
582e4243f13SShawn Guo 
583e4243f13SShawn Guo 	spin_unlock_irqrestore(&host->lock, flags);
584e4243f13SShawn Guo }
585e4243f13SShawn Guo 
586e4243f13SShawn Guo static const struct mmc_host_ops mxs_mmc_ops = {
587e4243f13SShawn Guo 	.request = mxs_mmc_request,
588e4243f13SShawn Guo 	.get_ro = mxs_mmc_get_ro,
589e4243f13SShawn Guo 	.get_cd = mxs_mmc_get_cd,
590e4243f13SShawn Guo 	.set_ios = mxs_mmc_set_ios,
591e4243f13SShawn Guo 	.enable_sdio_irq = mxs_mmc_enable_sdio_irq,
592e4243f13SShawn Guo };
593e4243f13SShawn Guo 
594e4243f13SShawn Guo static bool mxs_mmc_dma_filter(struct dma_chan *chan, void *param)
595e4243f13SShawn Guo {
596e4243f13SShawn Guo 	struct mxs_mmc_host *host = param;
597e4243f13SShawn Guo 
598e4243f13SShawn Guo 	if (!mxs_dma_is_apbh(chan))
599e4243f13SShawn Guo 		return false;
600e4243f13SShawn Guo 
601b60188c8SShawn Guo 	if (chan->chan_id != host->dma_channel)
602e4243f13SShawn Guo 		return false;
603e4243f13SShawn Guo 
604e4243f13SShawn Guo 	chan->private = &host->dma_data;
605e4243f13SShawn Guo 
606e4243f13SShawn Guo 	return true;
607e4243f13SShawn Guo }
608e4243f13SShawn Guo 
609600a991fSMarek Vasut static struct platform_device_id mxs_ssp_ids[] = {
610ef9b4d39SShawn Guo 	{
611ef9b4d39SShawn Guo 		.name = "imx23-mmc",
612600a991fSMarek Vasut 		.driver_data = IMX23_SSP,
613ef9b4d39SShawn Guo 	}, {
614ef9b4d39SShawn Guo 		.name = "imx28-mmc",
615600a991fSMarek Vasut 		.driver_data = IMX28_SSP,
616ef9b4d39SShawn Guo 	}, {
617ef9b4d39SShawn Guo 		/* sentinel */
618ef9b4d39SShawn Guo 	}
619ef9b4d39SShawn Guo };
620600a991fSMarek Vasut MODULE_DEVICE_TABLE(platform, mxs_ssp_ids);
621ef9b4d39SShawn Guo 
6226de4d817SShawn Guo static const struct of_device_id mxs_mmc_dt_ids[] = {
623600a991fSMarek Vasut 	{ .compatible = "fsl,imx23-mmc", .data = (void *) IMX23_SSP, },
624600a991fSMarek Vasut 	{ .compatible = "fsl,imx28-mmc", .data = (void *) IMX28_SSP, },
6256de4d817SShawn Guo 	{ /* sentinel */ }
6266de4d817SShawn Guo };
6276de4d817SShawn Guo MODULE_DEVICE_TABLE(of, mxs_mmc_dt_ids);
6286de4d817SShawn Guo 
629e4243f13SShawn Guo static int mxs_mmc_probe(struct platform_device *pdev)
630e4243f13SShawn Guo {
6316de4d817SShawn Guo 	const struct of_device_id *of_id =
6326de4d817SShawn Guo 			of_match_device(mxs_mmc_dt_ids, &pdev->dev);
6336de4d817SShawn Guo 	struct device_node *np = pdev->dev.of_node;
634e4243f13SShawn Guo 	struct mxs_mmc_host *host;
635e4243f13SShawn Guo 	struct mmc_host *mmc;
636df06bfc7SShawn Guo 	struct resource *iores, *dmares;
637e4243f13SShawn Guo 	struct mxs_mmc_platform_data *pdata;
6389c92cf24SShawn Guo 	struct pinctrl *pinctrl;
639e4243f13SShawn Guo 	int ret = 0, irq_err, irq_dma;
640e4243f13SShawn Guo 	dma_cap_mask_t mask;
6414dc5a79fSShawn Guo 	struct regulator *reg_vmmc;
642b6e76f10SMarek Vasut 	enum of_gpio_flags flags;
643829c1bf4SMarek Vasut 	struct mxs_ssp *ssp;
644e4243f13SShawn Guo 
645e4243f13SShawn Guo 	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
646e4243f13SShawn Guo 	dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
647e4243f13SShawn Guo 	irq_err = platform_get_irq(pdev, 0);
648e4243f13SShawn Guo 	irq_dma = platform_get_irq(pdev, 1);
6496de4d817SShawn Guo 	if (!iores || irq_err < 0 || irq_dma < 0)
650e4243f13SShawn Guo 		return -EINVAL;
651e4243f13SShawn Guo 
652e4243f13SShawn Guo 	mmc = mmc_alloc_host(sizeof(struct mxs_mmc_host), &pdev->dev);
653df06bfc7SShawn Guo 	if (!mmc)
654df06bfc7SShawn Guo 		return -ENOMEM;
655e4243f13SShawn Guo 
656e4243f13SShawn Guo 	host = mmc_priv(mmc);
657829c1bf4SMarek Vasut 	ssp = &host->ssp;
658829c1bf4SMarek Vasut 	ssp->dev = &pdev->dev;
659829c1bf4SMarek Vasut 	ssp->base = devm_request_and_ioremap(&pdev->dev, iores);
660829c1bf4SMarek Vasut 	if (!ssp->base) {
661df06bfc7SShawn Guo 		ret = -EADDRNOTAVAIL;
662e4243f13SShawn Guo 		goto out_mmc_free;
663e4243f13SShawn Guo 	}
664e4243f13SShawn Guo 
6656de4d817SShawn Guo 	if (np) {
666829c1bf4SMarek Vasut 		ssp->devid = (enum mxs_ssp_id) of_id->data;
6676de4d817SShawn Guo 		/*
6686de4d817SShawn Guo 		 * TODO: This is a temporary solution and should be changed
6696de4d817SShawn Guo 		 * to use generic DMA binding later when the helpers get in.
6706de4d817SShawn Guo 		 */
6716de4d817SShawn Guo 		ret = of_property_read_u32(np, "fsl,ssp-dma-channel",
6726de4d817SShawn Guo 					   &host->dma_channel);
6736de4d817SShawn Guo 		if (ret) {
6746de4d817SShawn Guo 			dev_err(mmc_dev(host->mmc),
6756de4d817SShawn Guo 				"failed to get dma channel\n");
6766de4d817SShawn Guo 			goto out_mmc_free;
6776de4d817SShawn Guo 		}
6786de4d817SShawn Guo 	} else {
679829c1bf4SMarek Vasut 		ssp->devid = pdev->id_entry->driver_data;
680b60188c8SShawn Guo 		host->dma_channel = dmares->start;
6816de4d817SShawn Guo 	}
6826de4d817SShawn Guo 
6836de4d817SShawn Guo 	host->mmc = mmc;
684e4243f13SShawn Guo 	host->sdio_irq_en = 0;
685e4243f13SShawn Guo 
6864dc5a79fSShawn Guo 	reg_vmmc = devm_regulator_get(&pdev->dev, "vmmc");
6874dc5a79fSShawn Guo 	if (!IS_ERR(reg_vmmc)) {
6884dc5a79fSShawn Guo 		ret = regulator_enable(reg_vmmc);
6894dc5a79fSShawn Guo 		if (ret) {
6904dc5a79fSShawn Guo 			dev_err(&pdev->dev,
6914dc5a79fSShawn Guo 				"Failed to enable vmmc regulator: %d\n", ret);
6924dc5a79fSShawn Guo 			goto out_mmc_free;
6934dc5a79fSShawn Guo 		}
6944dc5a79fSShawn Guo 	}
6954dc5a79fSShawn Guo 
6969c92cf24SShawn Guo 	pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
6979c92cf24SShawn Guo 	if (IS_ERR(pinctrl)) {
6989c92cf24SShawn Guo 		ret = PTR_ERR(pinctrl);
6996de4d817SShawn Guo 		goto out_mmc_free;
7009c92cf24SShawn Guo 	}
7019c92cf24SShawn Guo 
702829c1bf4SMarek Vasut 	ssp->clk = clk_get(&pdev->dev, NULL);
703829c1bf4SMarek Vasut 	if (IS_ERR(ssp->clk)) {
704829c1bf4SMarek Vasut 		ret = PTR_ERR(ssp->clk);
705df06bfc7SShawn Guo 		goto out_mmc_free;
706e4243f13SShawn Guo 	}
707829c1bf4SMarek Vasut 	clk_prepare_enable(ssp->clk);
708e4243f13SShawn Guo 
709e4243f13SShawn Guo 	mxs_mmc_reset(host);
710e4243f13SShawn Guo 
711e4243f13SShawn Guo 	dma_cap_zero(mask);
712e4243f13SShawn Guo 	dma_cap_set(DMA_SLAVE, mask);
713e4243f13SShawn Guo 	host->dma_data.chan_irq = irq_dma;
714e4243f13SShawn Guo 	host->dmach = dma_request_channel(mask, mxs_mmc_dma_filter, host);
715e4243f13SShawn Guo 	if (!host->dmach) {
716e4243f13SShawn Guo 		dev_err(mmc_dev(host->mmc),
717e4243f13SShawn Guo 			"%s: failed to request dma\n", __func__);
718e4243f13SShawn Guo 		goto out_clk_put;
719e4243f13SShawn Guo 	}
720e4243f13SShawn Guo 
721e4243f13SShawn Guo 	/* set mmc core parameters */
722e4243f13SShawn Guo 	mmc->ops = &mxs_mmc_ops;
723e4243f13SShawn Guo 	mmc->caps = MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
724e4243f13SShawn Guo 		    MMC_CAP_SDIO_IRQ | MMC_CAP_NEEDS_POLL;
725e4243f13SShawn Guo 
726e4243f13SShawn Guo 	pdata =	mmc_dev(host->mmc)->platform_data;
7276de4d817SShawn Guo 	if (!pdata) {
7286de4d817SShawn Guo 		u32 bus_width = 0;
7296de4d817SShawn Guo 		of_property_read_u32(np, "bus-width", &bus_width);
7306de4d817SShawn Guo 		if (bus_width == 4)
7316de4d817SShawn Guo 			mmc->caps |= MMC_CAP_4_BIT_DATA;
7326de4d817SShawn Guo 		else if (bus_width == 8)
7336de4d817SShawn Guo 			mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA;
734b6e76f10SMarek Vasut 		host->wp_gpio = of_get_named_gpio_flags(np, "wp-gpios", 0,
735b6e76f10SMarek Vasut 							&flags);
736b6e76f10SMarek Vasut 		if (flags & OF_GPIO_ACTIVE_LOW)
737b6e76f10SMarek Vasut 			host->wp_inverted = 1;
7386de4d817SShawn Guo 	} else {
739e4243f13SShawn Guo 		if (pdata->flags & SLOTF_8_BIT_CAPABLE)
740e4243f13SShawn Guo 			mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA;
741e4243f13SShawn Guo 		if (pdata->flags & SLOTF_4_BIT_CAPABLE)
742e4243f13SShawn Guo 			mmc->caps |= MMC_CAP_4_BIT_DATA;
74331b0ff5eSShawn Guo 		host->wp_gpio = pdata->wp_gpio;
744e4243f13SShawn Guo 	}
745e4243f13SShawn Guo 
746e4243f13SShawn Guo 	mmc->f_min = 400000;
747e4243f13SShawn Guo 	mmc->f_max = 288000000;
748e4243f13SShawn Guo 	mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
749e4243f13SShawn Guo 
750e4243f13SShawn Guo 	mmc->max_segs = 52;
751e4243f13SShawn Guo 	mmc->max_blk_size = 1 << 0xf;
752829c1bf4SMarek Vasut 	mmc->max_blk_count = (ssp_is_old(ssp)) ? 0xff : 0xffffff;
753829c1bf4SMarek Vasut 	mmc->max_req_size = (ssp_is_old(ssp)) ? 0xffff : 0xffffffff;
754e4243f13SShawn Guo 	mmc->max_seg_size = dma_get_max_seg_size(host->dmach->device->dev);
755e4243f13SShawn Guo 
756e4243f13SShawn Guo 	platform_set_drvdata(pdev, mmc);
757e4243f13SShawn Guo 
758df06bfc7SShawn Guo 	ret = devm_request_irq(&pdev->dev, irq_err, mxs_mmc_irq_handler, 0,
759df06bfc7SShawn Guo 			       DRIVER_NAME, host);
760e4243f13SShawn Guo 	if (ret)
761e4243f13SShawn Guo 		goto out_free_dma;
762e4243f13SShawn Guo 
763e4243f13SShawn Guo 	spin_lock_init(&host->lock);
764e4243f13SShawn Guo 
765e4243f13SShawn Guo 	ret = mmc_add_host(mmc);
766e4243f13SShawn Guo 	if (ret)
767df06bfc7SShawn Guo 		goto out_free_dma;
768e4243f13SShawn Guo 
769e4243f13SShawn Guo 	dev_info(mmc_dev(host->mmc), "initialized\n");
770e4243f13SShawn Guo 
771e4243f13SShawn Guo 	return 0;
772e4243f13SShawn Guo 
773e4243f13SShawn Guo out_free_dma:
774e4243f13SShawn Guo 	if (host->dmach)
775e4243f13SShawn Guo 		dma_release_channel(host->dmach);
776e4243f13SShawn Guo out_clk_put:
777829c1bf4SMarek Vasut 	clk_disable_unprepare(ssp->clk);
778829c1bf4SMarek Vasut 	clk_put(ssp->clk);
779e4243f13SShawn Guo out_mmc_free:
780e4243f13SShawn Guo 	mmc_free_host(mmc);
781e4243f13SShawn Guo 	return ret;
782e4243f13SShawn Guo }
783e4243f13SShawn Guo 
784e4243f13SShawn Guo static int mxs_mmc_remove(struct platform_device *pdev)
785e4243f13SShawn Guo {
786e4243f13SShawn Guo 	struct mmc_host *mmc = platform_get_drvdata(pdev);
787e4243f13SShawn Guo 	struct mxs_mmc_host *host = mmc_priv(mmc);
788829c1bf4SMarek Vasut 	struct mxs_ssp *ssp = &host->ssp;
789e4243f13SShawn Guo 
790e4243f13SShawn Guo 	mmc_remove_host(mmc);
791e4243f13SShawn Guo 
792e4243f13SShawn Guo 	platform_set_drvdata(pdev, NULL);
793e4243f13SShawn Guo 
794e4243f13SShawn Guo 	if (host->dmach)
795e4243f13SShawn Guo 		dma_release_channel(host->dmach);
796e4243f13SShawn Guo 
797829c1bf4SMarek Vasut 	clk_disable_unprepare(ssp->clk);
798829c1bf4SMarek Vasut 	clk_put(ssp->clk);
799e4243f13SShawn Guo 
800e4243f13SShawn Guo 	mmc_free_host(mmc);
801e4243f13SShawn Guo 
802e4243f13SShawn Guo 	return 0;
803e4243f13SShawn Guo }
804e4243f13SShawn Guo 
805e4243f13SShawn Guo #ifdef CONFIG_PM
806e4243f13SShawn Guo static int mxs_mmc_suspend(struct device *dev)
807e4243f13SShawn Guo {
808e4243f13SShawn Guo 	struct mmc_host *mmc = dev_get_drvdata(dev);
809e4243f13SShawn Guo 	struct mxs_mmc_host *host = mmc_priv(mmc);
810829c1bf4SMarek Vasut 	struct mxs_ssp *ssp = &host->ssp;
811e4243f13SShawn Guo 	int ret = 0;
812e4243f13SShawn Guo 
813e4243f13SShawn Guo 	ret = mmc_suspend_host(mmc);
814e4243f13SShawn Guo 
815829c1bf4SMarek Vasut 	clk_disable_unprepare(ssp->clk);
816e4243f13SShawn Guo 
817e4243f13SShawn Guo 	return ret;
818e4243f13SShawn Guo }
819e4243f13SShawn Guo 
820e4243f13SShawn Guo static int mxs_mmc_resume(struct device *dev)
821e4243f13SShawn Guo {
822e4243f13SShawn Guo 	struct mmc_host *mmc = dev_get_drvdata(dev);
823e4243f13SShawn Guo 	struct mxs_mmc_host *host = mmc_priv(mmc);
824829c1bf4SMarek Vasut 	struct mxs_ssp *ssp = &host->ssp;
825e4243f13SShawn Guo 	int ret = 0;
826e4243f13SShawn Guo 
827829c1bf4SMarek Vasut 	clk_prepare_enable(ssp->clk);
828e4243f13SShawn Guo 
829e4243f13SShawn Guo 	ret = mmc_resume_host(mmc);
830e4243f13SShawn Guo 
831e4243f13SShawn Guo 	return ret;
832e4243f13SShawn Guo }
833e4243f13SShawn Guo 
834e4243f13SShawn Guo static const struct dev_pm_ops mxs_mmc_pm_ops = {
835e4243f13SShawn Guo 	.suspend	= mxs_mmc_suspend,
836e4243f13SShawn Guo 	.resume		= mxs_mmc_resume,
837e4243f13SShawn Guo };
838e4243f13SShawn Guo #endif
839e4243f13SShawn Guo 
840e4243f13SShawn Guo static struct platform_driver mxs_mmc_driver = {
841e4243f13SShawn Guo 	.probe		= mxs_mmc_probe,
842e4243f13SShawn Guo 	.remove		= mxs_mmc_remove,
843600a991fSMarek Vasut 	.id_table	= mxs_ssp_ids,
844e4243f13SShawn Guo 	.driver		= {
845e4243f13SShawn Guo 		.name	= DRIVER_NAME,
846e4243f13SShawn Guo 		.owner	= THIS_MODULE,
847e4243f13SShawn Guo #ifdef CONFIG_PM
848e4243f13SShawn Guo 		.pm	= &mxs_mmc_pm_ops,
849e4243f13SShawn Guo #endif
850a3e545e9SMarek Vasut 		.of_match_table = mxs_mmc_dt_ids,
851e4243f13SShawn Guo 	},
852e4243f13SShawn Guo };
853e4243f13SShawn Guo 
854d1f81a64SAxel Lin module_platform_driver(mxs_mmc_driver);
855e4243f13SShawn Guo 
856e4243f13SShawn Guo MODULE_DESCRIPTION("FREESCALE MXS MMC peripheral");
857e4243f13SShawn Guo MODULE_AUTHOR("Freescale Semiconductor");
858e4243f13SShawn Guo MODULE_LICENSE("GPL");
859