• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
4  *  Copyright (C) 2013, Imagination Technologies
5  *
6  *  JZ4740 SD/MMC controller driver
7  */
8 
9 #include <linux/bitops.h>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/dmaengine.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/err.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/irq.h>
18 #include <linux/mmc/host.h>
19 #include <linux/mmc/slot-gpio.h>
20 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/platform_device.h>
24 #include <linux/scatterlist.h>
25 
26 #include <asm/cacheflush.h>
27 
28 #define JZ_REG_MMC_STRPCL	0x00
29 #define JZ_REG_MMC_STATUS	0x04
30 #define JZ_REG_MMC_CLKRT	0x08
31 #define JZ_REG_MMC_CMDAT	0x0C
32 #define JZ_REG_MMC_RESTO	0x10
33 #define JZ_REG_MMC_RDTO		0x14
34 #define JZ_REG_MMC_BLKLEN	0x18
35 #define JZ_REG_MMC_NOB		0x1C
36 #define JZ_REG_MMC_SNOB		0x20
37 #define JZ_REG_MMC_IMASK	0x24
38 #define JZ_REG_MMC_IREG		0x28
39 #define JZ_REG_MMC_CMD		0x2C
40 #define JZ_REG_MMC_ARG		0x30
41 #define JZ_REG_MMC_RESP_FIFO	0x34
42 #define JZ_REG_MMC_RXFIFO	0x38
43 #define JZ_REG_MMC_TXFIFO	0x3C
44 #define JZ_REG_MMC_LPM		0x40
45 #define JZ_REG_MMC_DMAC		0x44
46 
47 #define JZ_MMC_STRPCL_EXIT_MULTIPLE BIT(7)
48 #define JZ_MMC_STRPCL_EXIT_TRANSFER BIT(6)
49 #define JZ_MMC_STRPCL_START_READWAIT BIT(5)
50 #define JZ_MMC_STRPCL_STOP_READWAIT BIT(4)
51 #define JZ_MMC_STRPCL_RESET BIT(3)
52 #define JZ_MMC_STRPCL_START_OP BIT(2)
53 #define JZ_MMC_STRPCL_CLOCK_CONTROL (BIT(1) | BIT(0))
54 #define JZ_MMC_STRPCL_CLOCK_STOP BIT(0)
55 #define JZ_MMC_STRPCL_CLOCK_START BIT(1)
56 
57 
58 #define JZ_MMC_STATUS_IS_RESETTING BIT(15)
59 #define JZ_MMC_STATUS_SDIO_INT_ACTIVE BIT(14)
60 #define JZ_MMC_STATUS_PRG_DONE BIT(13)
61 #define JZ_MMC_STATUS_DATA_TRAN_DONE BIT(12)
62 #define JZ_MMC_STATUS_END_CMD_RES BIT(11)
63 #define JZ_MMC_STATUS_DATA_FIFO_AFULL BIT(10)
64 #define JZ_MMC_STATUS_IS_READWAIT BIT(9)
65 #define JZ_MMC_STATUS_CLK_EN BIT(8)
66 #define JZ_MMC_STATUS_DATA_FIFO_FULL BIT(7)
67 #define JZ_MMC_STATUS_DATA_FIFO_EMPTY BIT(6)
68 #define JZ_MMC_STATUS_CRC_RES_ERR BIT(5)
69 #define JZ_MMC_STATUS_CRC_READ_ERROR BIT(4)
70 #define JZ_MMC_STATUS_TIMEOUT_WRITE BIT(3)
71 #define JZ_MMC_STATUS_CRC_WRITE_ERROR BIT(2)
72 #define JZ_MMC_STATUS_TIMEOUT_RES BIT(1)
73 #define JZ_MMC_STATUS_TIMEOUT_READ BIT(0)
74 
75 #define JZ_MMC_STATUS_READ_ERROR_MASK (BIT(4) | BIT(0))
76 #define JZ_MMC_STATUS_WRITE_ERROR_MASK (BIT(3) | BIT(2))
77 
78 
79 #define JZ_MMC_CMDAT_IO_ABORT BIT(11)
80 #define JZ_MMC_CMDAT_BUS_WIDTH_4BIT BIT(10)
81 #define JZ_MMC_CMDAT_BUS_WIDTH_8BIT (BIT(10) | BIT(9))
82 #define	JZ_MMC_CMDAT_BUS_WIDTH_MASK (BIT(10) | BIT(9))
83 #define JZ_MMC_CMDAT_DMA_EN BIT(8)
84 #define JZ_MMC_CMDAT_INIT BIT(7)
85 #define JZ_MMC_CMDAT_BUSY BIT(6)
86 #define JZ_MMC_CMDAT_STREAM BIT(5)
87 #define JZ_MMC_CMDAT_WRITE BIT(4)
88 #define JZ_MMC_CMDAT_DATA_EN BIT(3)
89 #define JZ_MMC_CMDAT_RESPONSE_FORMAT (BIT(2) | BIT(1) | BIT(0))
90 #define JZ_MMC_CMDAT_RSP_R1 1
91 #define JZ_MMC_CMDAT_RSP_R2 2
92 #define JZ_MMC_CMDAT_RSP_R3 3
93 
94 #define JZ_MMC_IRQ_SDIO BIT(7)
95 #define JZ_MMC_IRQ_TXFIFO_WR_REQ BIT(6)
96 #define JZ_MMC_IRQ_RXFIFO_RD_REQ BIT(5)
97 #define JZ_MMC_IRQ_END_CMD_RES BIT(2)
98 #define JZ_MMC_IRQ_PRG_DONE BIT(1)
99 #define JZ_MMC_IRQ_DATA_TRAN_DONE BIT(0)
100 
101 #define JZ_MMC_DMAC_DMA_SEL BIT(1)
102 #define JZ_MMC_DMAC_DMA_EN BIT(0)
103 
104 #define	JZ_MMC_LPM_DRV_RISING BIT(31)
105 #define	JZ_MMC_LPM_DRV_RISING_QTR_PHASE_DLY BIT(31)
106 #define	JZ_MMC_LPM_DRV_RISING_1NS_DLY BIT(30)
107 #define	JZ_MMC_LPM_SMP_RISING_QTR_OR_HALF_PHASE_DLY BIT(29)
108 #define	JZ_MMC_LPM_LOW_POWER_MODE_EN BIT(0)
109 
110 #define JZ_MMC_CLK_RATE 24000000
111 #define JZ_MMC_REQ_TIMEOUT_MS 5000
112 
113 enum jz4740_mmc_version {
114 	JZ_MMC_JZ4740,
115 	JZ_MMC_JZ4725B,
116 	JZ_MMC_JZ4760,
117 	JZ_MMC_JZ4780,
118 	JZ_MMC_X1000,
119 };
120 
121 enum jz4740_mmc_state {
122 	JZ4740_MMC_STATE_READ_RESPONSE,
123 	JZ4740_MMC_STATE_TRANSFER_DATA,
124 	JZ4740_MMC_STATE_SEND_STOP,
125 	JZ4740_MMC_STATE_DONE,
126 };
127 
128 /*
129  * The MMC core allows to prepare a mmc_request while another mmc_request
130  * is in-flight. This is used via the pre_req/post_req hooks.
131  * This driver uses the pre_req/post_req hooks to map/unmap the mmc_request.
132  * Following what other drivers do (sdhci, dw_mmc) we use the following cookie
133  * flags to keep track of the mmc_request mapping state.
134  *
135  * COOKIE_UNMAPPED: the request is not mapped.
136  * COOKIE_PREMAPPED: the request was mapped in pre_req,
137  * and should be unmapped in post_req.
138  * COOKIE_MAPPED: the request was mapped in the irq handler,
139  * and should be unmapped before mmc_request_done is called..
140  */
141 enum jz4780_cookie {
142 	COOKIE_UNMAPPED = 0,
143 	COOKIE_PREMAPPED,
144 	COOKIE_MAPPED,
145 };
146 
147 struct jz4740_mmc_host {
148 	struct mmc_host *mmc;
149 	struct platform_device *pdev;
150 	struct clk *clk;
151 
152 	enum jz4740_mmc_version version;
153 
154 	int irq;
155 
156 	void __iomem *base;
157 	struct resource *mem_res;
158 	struct mmc_request *req;
159 	struct mmc_command *cmd;
160 
161 	unsigned long waiting;
162 
163 	uint32_t cmdat;
164 
165 	uint32_t irq_mask;
166 
167 	spinlock_t lock;
168 
169 	struct timer_list timeout_timer;
170 	struct sg_mapping_iter miter;
171 	enum jz4740_mmc_state state;
172 
173 	/* DMA support */
174 	struct dma_chan *dma_rx;
175 	struct dma_chan *dma_tx;
176 	bool use_dma;
177 
178 /* The DMA trigger level is 8 words, that is to say, the DMA read
179  * trigger is when data words in MSC_RXFIFO is >= 8 and the DMA write
180  * trigger is when data words in MSC_TXFIFO is < 8.
181  */
182 #define JZ4740_MMC_FIFO_HALF_SIZE 8
183 };
184 
jz4740_mmc_write_irq_mask(struct jz4740_mmc_host * host,uint32_t val)185 static void jz4740_mmc_write_irq_mask(struct jz4740_mmc_host *host,
186 				      uint32_t val)
187 {
188 	if (host->version >= JZ_MMC_JZ4725B)
189 		return writel(val, host->base + JZ_REG_MMC_IMASK);
190 	else
191 		return writew(val, host->base + JZ_REG_MMC_IMASK);
192 }
193 
jz4740_mmc_write_irq_reg(struct jz4740_mmc_host * host,uint32_t val)194 static void jz4740_mmc_write_irq_reg(struct jz4740_mmc_host *host,
195 				     uint32_t val)
196 {
197 	if (host->version >= JZ_MMC_JZ4780)
198 		writel(val, host->base + JZ_REG_MMC_IREG);
199 	else
200 		writew(val, host->base + JZ_REG_MMC_IREG);
201 }
202 
jz4740_mmc_read_irq_reg(struct jz4740_mmc_host * host)203 static uint32_t jz4740_mmc_read_irq_reg(struct jz4740_mmc_host *host)
204 {
205 	if (host->version >= JZ_MMC_JZ4780)
206 		return readl(host->base + JZ_REG_MMC_IREG);
207 	else
208 		return readw(host->base + JZ_REG_MMC_IREG);
209 }
210 
211 /*----------------------------------------------------------------------------*/
212 /* DMA infrastructure */
213 
jz4740_mmc_release_dma_channels(struct jz4740_mmc_host * host)214 static void jz4740_mmc_release_dma_channels(struct jz4740_mmc_host *host)
215 {
216 	if (!host->use_dma)
217 		return;
218 
219 	dma_release_channel(host->dma_tx);
220 	dma_release_channel(host->dma_rx);
221 }
222 
jz4740_mmc_acquire_dma_channels(struct jz4740_mmc_host * host)223 static int jz4740_mmc_acquire_dma_channels(struct jz4740_mmc_host *host)
224 {
225 	host->dma_tx = dma_request_chan(mmc_dev(host->mmc), "tx");
226 	if (IS_ERR(host->dma_tx)) {
227 		dev_err(mmc_dev(host->mmc), "Failed to get dma_tx channel\n");
228 		return PTR_ERR(host->dma_tx);
229 	}
230 
231 	host->dma_rx = dma_request_chan(mmc_dev(host->mmc), "rx");
232 	if (IS_ERR(host->dma_rx)) {
233 		dev_err(mmc_dev(host->mmc), "Failed to get dma_rx channel\n");
234 		dma_release_channel(host->dma_tx);
235 		return PTR_ERR(host->dma_rx);
236 	}
237 
238 	/*
239 	 * Limit the maximum segment size in any SG entry according to
240 	 * the parameters of the DMA engine device.
241 	 */
242 	if (host->dma_tx) {
243 		struct device *dev = host->dma_tx->device->dev;
244 		unsigned int max_seg_size = dma_get_max_seg_size(dev);
245 
246 		if (max_seg_size < host->mmc->max_seg_size)
247 			host->mmc->max_seg_size = max_seg_size;
248 	}
249 
250 	if (host->dma_rx) {
251 		struct device *dev = host->dma_rx->device->dev;
252 		unsigned int max_seg_size = dma_get_max_seg_size(dev);
253 
254 		if (max_seg_size < host->mmc->max_seg_size)
255 			host->mmc->max_seg_size = max_seg_size;
256 	}
257 
258 	return 0;
259 }
260 
jz4740_mmc_get_dma_chan(struct jz4740_mmc_host * host,struct mmc_data * data)261 static inline struct dma_chan *jz4740_mmc_get_dma_chan(struct jz4740_mmc_host *host,
262 						       struct mmc_data *data)
263 {
264 	return (data->flags & MMC_DATA_READ) ? host->dma_rx : host->dma_tx;
265 }
266 
jz4740_mmc_dma_unmap(struct jz4740_mmc_host * host,struct mmc_data * data)267 static void jz4740_mmc_dma_unmap(struct jz4740_mmc_host *host,
268 				 struct mmc_data *data)
269 {
270 	struct dma_chan *chan = jz4740_mmc_get_dma_chan(host, data);
271 	enum dma_data_direction dir = mmc_get_dma_dir(data);
272 
273 	dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, dir);
274 	data->host_cookie = COOKIE_UNMAPPED;
275 }
276 
277 /* Prepares DMA data for current or next transfer.
278  * A request can be in-flight when this is called.
279  */
jz4740_mmc_prepare_dma_data(struct jz4740_mmc_host * host,struct mmc_data * data,int cookie)280 static int jz4740_mmc_prepare_dma_data(struct jz4740_mmc_host *host,
281 				       struct mmc_data *data,
282 				       int cookie)
283 {
284 	struct dma_chan *chan = jz4740_mmc_get_dma_chan(host, data);
285 	enum dma_data_direction dir = mmc_get_dma_dir(data);
286 	int sg_count;
287 
288 	if (data->host_cookie == COOKIE_PREMAPPED)
289 		return data->sg_count;
290 
291 	sg_count = dma_map_sg(chan->device->dev,
292 			data->sg,
293 			data->sg_len,
294 			dir);
295 
296 	if (sg_count <= 0) {
297 		dev_err(mmc_dev(host->mmc),
298 			"Failed to map scatterlist for DMA operation\n");
299 		return -EINVAL;
300 	}
301 
302 	data->sg_count = sg_count;
303 	data->host_cookie = cookie;
304 
305 	return data->sg_count;
306 }
307 
jz4740_mmc_start_dma_transfer(struct jz4740_mmc_host * host,struct mmc_data * data)308 static int jz4740_mmc_start_dma_transfer(struct jz4740_mmc_host *host,
309 					 struct mmc_data *data)
310 {
311 	struct dma_chan *chan = jz4740_mmc_get_dma_chan(host, data);
312 	struct dma_async_tx_descriptor *desc;
313 	struct dma_slave_config conf = {
314 		.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
315 		.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
316 		.src_maxburst = JZ4740_MMC_FIFO_HALF_SIZE,
317 		.dst_maxburst = JZ4740_MMC_FIFO_HALF_SIZE,
318 	};
319 	int sg_count;
320 
321 	if (data->flags & MMC_DATA_WRITE) {
322 		conf.direction = DMA_MEM_TO_DEV;
323 		conf.dst_addr = host->mem_res->start + JZ_REG_MMC_TXFIFO;
324 	} else {
325 		conf.direction = DMA_DEV_TO_MEM;
326 		conf.src_addr = host->mem_res->start + JZ_REG_MMC_RXFIFO;
327 	}
328 
329 	sg_count = jz4740_mmc_prepare_dma_data(host, data, COOKIE_MAPPED);
330 	if (sg_count < 0)
331 		return sg_count;
332 
333 	dmaengine_slave_config(chan, &conf);
334 	desc = dmaengine_prep_slave_sg(chan, data->sg, sg_count,
335 			conf.direction,
336 			DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
337 	if (!desc) {
338 		dev_err(mmc_dev(host->mmc),
339 			"Failed to allocate DMA %s descriptor",
340 			 conf.direction == DMA_MEM_TO_DEV ? "TX" : "RX");
341 		goto dma_unmap;
342 	}
343 
344 	dmaengine_submit(desc);
345 	dma_async_issue_pending(chan);
346 
347 	return 0;
348 
349 dma_unmap:
350 	if (data->host_cookie == COOKIE_MAPPED)
351 		jz4740_mmc_dma_unmap(host, data);
352 	return -ENOMEM;
353 }
354 
jz4740_mmc_pre_request(struct mmc_host * mmc,struct mmc_request * mrq)355 static void jz4740_mmc_pre_request(struct mmc_host *mmc,
356 				   struct mmc_request *mrq)
357 {
358 	struct jz4740_mmc_host *host = mmc_priv(mmc);
359 	struct mmc_data *data = mrq->data;
360 
361 	if (!host->use_dma)
362 		return;
363 
364 	data->host_cookie = COOKIE_UNMAPPED;
365 	if (jz4740_mmc_prepare_dma_data(host, data, COOKIE_PREMAPPED) < 0)
366 		data->host_cookie = COOKIE_UNMAPPED;
367 }
368 
jz4740_mmc_post_request(struct mmc_host * mmc,struct mmc_request * mrq,int err)369 static void jz4740_mmc_post_request(struct mmc_host *mmc,
370 				    struct mmc_request *mrq,
371 				    int err)
372 {
373 	struct jz4740_mmc_host *host = mmc_priv(mmc);
374 	struct mmc_data *data = mrq->data;
375 
376 	if (data && data->host_cookie != COOKIE_UNMAPPED)
377 		jz4740_mmc_dma_unmap(host, data);
378 
379 	if (err) {
380 		struct dma_chan *chan = jz4740_mmc_get_dma_chan(host, data);
381 
382 		dmaengine_terminate_all(chan);
383 	}
384 }
385 
386 /*----------------------------------------------------------------------------*/
387 
jz4740_mmc_set_irq_enabled(struct jz4740_mmc_host * host,unsigned int irq,bool enabled)388 static void jz4740_mmc_set_irq_enabled(struct jz4740_mmc_host *host,
389 	unsigned int irq, bool enabled)
390 {
391 	unsigned long flags;
392 
393 	spin_lock_irqsave(&host->lock, flags);
394 	if (enabled)
395 		host->irq_mask &= ~irq;
396 	else
397 		host->irq_mask |= irq;
398 
399 	jz4740_mmc_write_irq_mask(host, host->irq_mask);
400 	spin_unlock_irqrestore(&host->lock, flags);
401 }
402 
jz4740_mmc_clock_enable(struct jz4740_mmc_host * host,bool start_transfer)403 static void jz4740_mmc_clock_enable(struct jz4740_mmc_host *host,
404 	bool start_transfer)
405 {
406 	uint16_t val = JZ_MMC_STRPCL_CLOCK_START;
407 
408 	if (start_transfer)
409 		val |= JZ_MMC_STRPCL_START_OP;
410 
411 	writew(val, host->base + JZ_REG_MMC_STRPCL);
412 }
413 
jz4740_mmc_clock_disable(struct jz4740_mmc_host * host)414 static void jz4740_mmc_clock_disable(struct jz4740_mmc_host *host)
415 {
416 	uint32_t status;
417 	unsigned int timeout = 1000;
418 
419 	writew(JZ_MMC_STRPCL_CLOCK_STOP, host->base + JZ_REG_MMC_STRPCL);
420 	do {
421 		status = readl(host->base + JZ_REG_MMC_STATUS);
422 	} while (status & JZ_MMC_STATUS_CLK_EN && --timeout);
423 }
424 
jz4740_mmc_reset(struct jz4740_mmc_host * host)425 static void jz4740_mmc_reset(struct jz4740_mmc_host *host)
426 {
427 	uint32_t status;
428 	unsigned int timeout = 1000;
429 
430 	writew(JZ_MMC_STRPCL_RESET, host->base + JZ_REG_MMC_STRPCL);
431 	udelay(10);
432 	do {
433 		status = readl(host->base + JZ_REG_MMC_STATUS);
434 	} while (status & JZ_MMC_STATUS_IS_RESETTING && --timeout);
435 }
436 
jz4740_mmc_request_done(struct jz4740_mmc_host * host)437 static void jz4740_mmc_request_done(struct jz4740_mmc_host *host)
438 {
439 	struct mmc_request *req;
440 	struct mmc_data *data;
441 
442 	req = host->req;
443 	data = req->data;
444 	host->req = NULL;
445 
446 	if (data && data->host_cookie == COOKIE_MAPPED)
447 		jz4740_mmc_dma_unmap(host, data);
448 	mmc_request_done(host->mmc, req);
449 }
450 
jz4740_mmc_poll_irq(struct jz4740_mmc_host * host,unsigned int irq)451 static unsigned int jz4740_mmc_poll_irq(struct jz4740_mmc_host *host,
452 	unsigned int irq)
453 {
454 	unsigned int timeout = 0x800;
455 	uint32_t status;
456 
457 	do {
458 		status = jz4740_mmc_read_irq_reg(host);
459 	} while (!(status & irq) && --timeout);
460 
461 	if (timeout == 0) {
462 		set_bit(0, &host->waiting);
463 		mod_timer(&host->timeout_timer,
464 			  jiffies + msecs_to_jiffies(JZ_MMC_REQ_TIMEOUT_MS));
465 		jz4740_mmc_set_irq_enabled(host, irq, true);
466 		return true;
467 	}
468 
469 	return false;
470 }
471 
jz4740_mmc_transfer_check_state(struct jz4740_mmc_host * host,struct mmc_data * data)472 static void jz4740_mmc_transfer_check_state(struct jz4740_mmc_host *host,
473 	struct mmc_data *data)
474 {
475 	int status;
476 
477 	status = readl(host->base + JZ_REG_MMC_STATUS);
478 	if (status & JZ_MMC_STATUS_WRITE_ERROR_MASK) {
479 		if (status & (JZ_MMC_STATUS_TIMEOUT_WRITE)) {
480 			host->req->cmd->error = -ETIMEDOUT;
481 			data->error = -ETIMEDOUT;
482 		} else {
483 			host->req->cmd->error = -EIO;
484 			data->error = -EIO;
485 		}
486 	} else if (status & JZ_MMC_STATUS_READ_ERROR_MASK) {
487 		if (status & (JZ_MMC_STATUS_TIMEOUT_READ)) {
488 			host->req->cmd->error = -ETIMEDOUT;
489 			data->error = -ETIMEDOUT;
490 		} else {
491 			host->req->cmd->error = -EIO;
492 			data->error = -EIO;
493 		}
494 	}
495 }
496 
jz4740_mmc_write_data(struct jz4740_mmc_host * host,struct mmc_data * data)497 static bool jz4740_mmc_write_data(struct jz4740_mmc_host *host,
498 	struct mmc_data *data)
499 {
500 	struct sg_mapping_iter *miter = &host->miter;
501 	void __iomem *fifo_addr = host->base + JZ_REG_MMC_TXFIFO;
502 	uint32_t *buf;
503 	bool timeout;
504 	size_t i, j;
505 
506 	while (sg_miter_next(miter)) {
507 		buf = miter->addr;
508 		i = miter->length / 4;
509 		j = i / 8;
510 		i = i & 0x7;
511 		while (j) {
512 			timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_TXFIFO_WR_REQ);
513 			if (unlikely(timeout))
514 				goto poll_timeout;
515 
516 			writel(buf[0], fifo_addr);
517 			writel(buf[1], fifo_addr);
518 			writel(buf[2], fifo_addr);
519 			writel(buf[3], fifo_addr);
520 			writel(buf[4], fifo_addr);
521 			writel(buf[5], fifo_addr);
522 			writel(buf[6], fifo_addr);
523 			writel(buf[7], fifo_addr);
524 			buf += 8;
525 			--j;
526 		}
527 		if (unlikely(i)) {
528 			timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_TXFIFO_WR_REQ);
529 			if (unlikely(timeout))
530 				goto poll_timeout;
531 
532 			while (i) {
533 				writel(*buf, fifo_addr);
534 				++buf;
535 				--i;
536 			}
537 		}
538 		data->bytes_xfered += miter->length;
539 	}
540 	sg_miter_stop(miter);
541 
542 	return false;
543 
544 poll_timeout:
545 	miter->consumed = (void *)buf - miter->addr;
546 	data->bytes_xfered += miter->consumed;
547 	sg_miter_stop(miter);
548 
549 	return true;
550 }
551 
jz4740_mmc_read_data(struct jz4740_mmc_host * host,struct mmc_data * data)552 static bool jz4740_mmc_read_data(struct jz4740_mmc_host *host,
553 				struct mmc_data *data)
554 {
555 	struct sg_mapping_iter *miter = &host->miter;
556 	void __iomem *fifo_addr = host->base + JZ_REG_MMC_RXFIFO;
557 	uint32_t *buf;
558 	uint32_t d;
559 	uint32_t status;
560 	size_t i, j;
561 	unsigned int timeout;
562 
563 	while (sg_miter_next(miter)) {
564 		buf = miter->addr;
565 		i = miter->length;
566 		j = i / 32;
567 		i = i & 0x1f;
568 		while (j) {
569 			timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_RXFIFO_RD_REQ);
570 			if (unlikely(timeout))
571 				goto poll_timeout;
572 
573 			buf[0] = readl(fifo_addr);
574 			buf[1] = readl(fifo_addr);
575 			buf[2] = readl(fifo_addr);
576 			buf[3] = readl(fifo_addr);
577 			buf[4] = readl(fifo_addr);
578 			buf[5] = readl(fifo_addr);
579 			buf[6] = readl(fifo_addr);
580 			buf[7] = readl(fifo_addr);
581 
582 			buf += 8;
583 			--j;
584 		}
585 
586 		if (unlikely(i)) {
587 			timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_RXFIFO_RD_REQ);
588 			if (unlikely(timeout))
589 				goto poll_timeout;
590 
591 			while (i >= 4) {
592 				*buf++ = readl(fifo_addr);
593 				i -= 4;
594 			}
595 			if (unlikely(i > 0)) {
596 				d = readl(fifo_addr);
597 				memcpy(buf, &d, i);
598 			}
599 		}
600 		data->bytes_xfered += miter->length;
601 	}
602 	sg_miter_stop(miter);
603 
604 	/* For whatever reason there is sometime one word more in the fifo then
605 	 * requested */
606 	timeout = 1000;
607 	status = readl(host->base + JZ_REG_MMC_STATUS);
608 	while (!(status & JZ_MMC_STATUS_DATA_FIFO_EMPTY) && --timeout) {
609 		d = readl(fifo_addr);
610 		status = readl(host->base + JZ_REG_MMC_STATUS);
611 	}
612 
613 	return false;
614 
615 poll_timeout:
616 	miter->consumed = (void *)buf - miter->addr;
617 	data->bytes_xfered += miter->consumed;
618 	sg_miter_stop(miter);
619 
620 	return true;
621 }
622 
jz4740_mmc_timeout(struct timer_list * t)623 static void jz4740_mmc_timeout(struct timer_list *t)
624 {
625 	struct jz4740_mmc_host *host = from_timer(host, t, timeout_timer);
626 
627 	if (!test_and_clear_bit(0, &host->waiting))
628 		return;
629 
630 	jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_END_CMD_RES, false);
631 
632 	host->req->cmd->error = -ETIMEDOUT;
633 	jz4740_mmc_request_done(host);
634 }
635 
jz4740_mmc_read_response(struct jz4740_mmc_host * host,struct mmc_command * cmd)636 static void jz4740_mmc_read_response(struct jz4740_mmc_host *host,
637 	struct mmc_command *cmd)
638 {
639 	int i;
640 	uint16_t tmp;
641 	void __iomem *fifo_addr = host->base + JZ_REG_MMC_RESP_FIFO;
642 
643 	if (cmd->flags & MMC_RSP_136) {
644 		tmp = readw(fifo_addr);
645 		for (i = 0; i < 4; ++i) {
646 			cmd->resp[i] = tmp << 24;
647 			tmp = readw(fifo_addr);
648 			cmd->resp[i] |= tmp << 8;
649 			tmp = readw(fifo_addr);
650 			cmd->resp[i] |= tmp >> 8;
651 		}
652 	} else {
653 		cmd->resp[0] = readw(fifo_addr) << 24;
654 		cmd->resp[0] |= readw(fifo_addr) << 8;
655 		cmd->resp[0] |= readw(fifo_addr) & 0xff;
656 	}
657 }
658 
jz4740_mmc_send_command(struct jz4740_mmc_host * host,struct mmc_command * cmd)659 static void jz4740_mmc_send_command(struct jz4740_mmc_host *host,
660 	struct mmc_command *cmd)
661 {
662 	uint32_t cmdat = host->cmdat;
663 
664 	host->cmdat &= ~JZ_MMC_CMDAT_INIT;
665 	jz4740_mmc_clock_disable(host);
666 
667 	host->cmd = cmd;
668 
669 	if (cmd->flags & MMC_RSP_BUSY)
670 		cmdat |= JZ_MMC_CMDAT_BUSY;
671 
672 	switch (mmc_resp_type(cmd)) {
673 	case MMC_RSP_R1B:
674 	case MMC_RSP_R1:
675 		cmdat |= JZ_MMC_CMDAT_RSP_R1;
676 		break;
677 	case MMC_RSP_R2:
678 		cmdat |= JZ_MMC_CMDAT_RSP_R2;
679 		break;
680 	case MMC_RSP_R3:
681 		cmdat |= JZ_MMC_CMDAT_RSP_R3;
682 		break;
683 	default:
684 		break;
685 	}
686 
687 	if (cmd->data) {
688 		cmdat |= JZ_MMC_CMDAT_DATA_EN;
689 		if (cmd->data->flags & MMC_DATA_WRITE)
690 			cmdat |= JZ_MMC_CMDAT_WRITE;
691 		if (host->use_dma) {
692 			/*
693 			 * The JZ4780's MMC controller has integrated DMA ability
694 			 * in addition to being able to use the external DMA
695 			 * controller. It moves DMA control bits to a separate
696 			 * register. The DMA_SEL bit chooses the external
697 			 * controller over the integrated one. Earlier SoCs
698 			 * can only use the external controller, and have a
699 			 * single DMA enable bit in CMDAT.
700 			 */
701 			if (host->version >= JZ_MMC_JZ4780) {
702 				writel(JZ_MMC_DMAC_DMA_EN | JZ_MMC_DMAC_DMA_SEL,
703 				       host->base + JZ_REG_MMC_DMAC);
704 			} else {
705 				cmdat |= JZ_MMC_CMDAT_DMA_EN;
706 			}
707 		} else if (host->version >= JZ_MMC_JZ4780) {
708 			writel(0, host->base + JZ_REG_MMC_DMAC);
709 		}
710 
711 		writew(cmd->data->blksz, host->base + JZ_REG_MMC_BLKLEN);
712 		writew(cmd->data->blocks, host->base + JZ_REG_MMC_NOB);
713 	}
714 
715 	writeb(cmd->opcode, host->base + JZ_REG_MMC_CMD);
716 	writel(cmd->arg, host->base + JZ_REG_MMC_ARG);
717 	writel(cmdat, host->base + JZ_REG_MMC_CMDAT);
718 
719 	jz4740_mmc_clock_enable(host, 1);
720 }
721 
jz_mmc_prepare_data_transfer(struct jz4740_mmc_host * host)722 static void jz_mmc_prepare_data_transfer(struct jz4740_mmc_host *host)
723 {
724 	struct mmc_command *cmd = host->req->cmd;
725 	struct mmc_data *data = cmd->data;
726 	int direction;
727 
728 	if (data->flags & MMC_DATA_READ)
729 		direction = SG_MITER_TO_SG;
730 	else
731 		direction = SG_MITER_FROM_SG;
732 
733 	sg_miter_start(&host->miter, data->sg, data->sg_len, direction);
734 }
735 
736 
jz_mmc_irq_worker(int irq,void * devid)737 static irqreturn_t jz_mmc_irq_worker(int irq, void *devid)
738 {
739 	struct jz4740_mmc_host *host = (struct jz4740_mmc_host *)devid;
740 	struct mmc_command *cmd = host->req->cmd;
741 	struct mmc_request *req = host->req;
742 	struct mmc_data *data = cmd->data;
743 	bool timeout = false;
744 
745 	if (cmd->error)
746 		host->state = JZ4740_MMC_STATE_DONE;
747 
748 	switch (host->state) {
749 	case JZ4740_MMC_STATE_READ_RESPONSE:
750 		if (cmd->flags & MMC_RSP_PRESENT)
751 			jz4740_mmc_read_response(host, cmd);
752 
753 		if (!data)
754 			break;
755 
756 		jz_mmc_prepare_data_transfer(host);
757 		fallthrough;
758 
759 	case JZ4740_MMC_STATE_TRANSFER_DATA:
760 		if (host->use_dma) {
761 			/* Use DMA if enabled.
762 			 * Data transfer direction is defined later by
763 			 * relying on data flags in
764 			 * jz4740_mmc_prepare_dma_data() and
765 			 * jz4740_mmc_start_dma_transfer().
766 			 */
767 			timeout = jz4740_mmc_start_dma_transfer(host, data);
768 			data->bytes_xfered = data->blocks * data->blksz;
769 		} else if (data->flags & MMC_DATA_READ)
770 			/* Use PIO if DMA is not enabled.
771 			 * Data transfer direction was defined before
772 			 * by relying on data flags in
773 			 * jz_mmc_prepare_data_transfer().
774 			 */
775 			timeout = jz4740_mmc_read_data(host, data);
776 		else
777 			timeout = jz4740_mmc_write_data(host, data);
778 
779 		if (unlikely(timeout)) {
780 			host->state = JZ4740_MMC_STATE_TRANSFER_DATA;
781 			break;
782 		}
783 
784 		jz4740_mmc_transfer_check_state(host, data);
785 
786 		timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_DATA_TRAN_DONE);
787 		if (unlikely(timeout)) {
788 			host->state = JZ4740_MMC_STATE_SEND_STOP;
789 			break;
790 		}
791 		jz4740_mmc_write_irq_reg(host, JZ_MMC_IRQ_DATA_TRAN_DONE);
792 		fallthrough;
793 
794 	case JZ4740_MMC_STATE_SEND_STOP:
795 		if (!req->stop)
796 			break;
797 
798 		jz4740_mmc_send_command(host, req->stop);
799 
800 		if (mmc_resp_type(req->stop) & MMC_RSP_BUSY) {
801 			timeout = jz4740_mmc_poll_irq(host,
802 						      JZ_MMC_IRQ_PRG_DONE);
803 			if (timeout) {
804 				host->state = JZ4740_MMC_STATE_DONE;
805 				break;
806 			}
807 		}
808 		fallthrough;
809 
810 	case JZ4740_MMC_STATE_DONE:
811 		break;
812 	}
813 
814 	if (!timeout)
815 		jz4740_mmc_request_done(host);
816 
817 	return IRQ_HANDLED;
818 }
819 
jz_mmc_irq(int irq,void * devid)820 static irqreturn_t jz_mmc_irq(int irq, void *devid)
821 {
822 	struct jz4740_mmc_host *host = devid;
823 	struct mmc_command *cmd = host->cmd;
824 	uint32_t irq_reg, status, tmp;
825 
826 	status = readl(host->base + JZ_REG_MMC_STATUS);
827 	irq_reg = jz4740_mmc_read_irq_reg(host);
828 
829 	tmp = irq_reg;
830 	irq_reg &= ~host->irq_mask;
831 
832 	tmp &= ~(JZ_MMC_IRQ_TXFIFO_WR_REQ | JZ_MMC_IRQ_RXFIFO_RD_REQ |
833 		JZ_MMC_IRQ_PRG_DONE | JZ_MMC_IRQ_DATA_TRAN_DONE);
834 
835 	if (tmp != irq_reg)
836 		jz4740_mmc_write_irq_reg(host, tmp & ~irq_reg);
837 
838 	if (irq_reg & JZ_MMC_IRQ_SDIO) {
839 		jz4740_mmc_write_irq_reg(host, JZ_MMC_IRQ_SDIO);
840 		mmc_signal_sdio_irq(host->mmc);
841 		irq_reg &= ~JZ_MMC_IRQ_SDIO;
842 	}
843 
844 	if (host->req && cmd && irq_reg) {
845 		if (test_and_clear_bit(0, &host->waiting)) {
846 			del_timer(&host->timeout_timer);
847 
848 			if (status & JZ_MMC_STATUS_TIMEOUT_RES) {
849 				cmd->error = -ETIMEDOUT;
850 			} else if (status & JZ_MMC_STATUS_CRC_RES_ERR) {
851 				cmd->error = -EIO;
852 			} else if (status & (JZ_MMC_STATUS_CRC_READ_ERROR |
853 				    JZ_MMC_STATUS_CRC_WRITE_ERROR)) {
854 				if (cmd->data)
855 					cmd->data->error = -EIO;
856 				cmd->error = -EIO;
857 			}
858 
859 			jz4740_mmc_set_irq_enabled(host, irq_reg, false);
860 			jz4740_mmc_write_irq_reg(host, irq_reg);
861 
862 			return IRQ_WAKE_THREAD;
863 		}
864 	}
865 
866 	return IRQ_HANDLED;
867 }
868 
jz4740_mmc_set_clock_rate(struct jz4740_mmc_host * host,int rate)869 static int jz4740_mmc_set_clock_rate(struct jz4740_mmc_host *host, int rate)
870 {
871 	int div = 0;
872 	int real_rate;
873 
874 	jz4740_mmc_clock_disable(host);
875 	clk_set_rate(host->clk, host->mmc->f_max);
876 
877 	real_rate = clk_get_rate(host->clk);
878 
879 	while (real_rate > rate && div < 7) {
880 		++div;
881 		real_rate >>= 1;
882 	}
883 
884 	writew(div, host->base + JZ_REG_MMC_CLKRT);
885 
886 	if (real_rate > 25000000) {
887 		if (host->version >= JZ_MMC_JZ4780) {
888 			writel(JZ_MMC_LPM_DRV_RISING_QTR_PHASE_DLY |
889 				   JZ_MMC_LPM_SMP_RISING_QTR_OR_HALF_PHASE_DLY |
890 				   JZ_MMC_LPM_LOW_POWER_MODE_EN,
891 				   host->base + JZ_REG_MMC_LPM);
892 		} else if (host->version >= JZ_MMC_JZ4760) {
893 			writel(JZ_MMC_LPM_DRV_RISING |
894 				   JZ_MMC_LPM_LOW_POWER_MODE_EN,
895 				   host->base + JZ_REG_MMC_LPM);
896 		} else if (host->version >= JZ_MMC_JZ4725B)
897 			writel(JZ_MMC_LPM_LOW_POWER_MODE_EN,
898 				   host->base + JZ_REG_MMC_LPM);
899 	}
900 
901 	return real_rate;
902 }
903 
jz4740_mmc_request(struct mmc_host * mmc,struct mmc_request * req)904 static void jz4740_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
905 {
906 	struct jz4740_mmc_host *host = mmc_priv(mmc);
907 
908 	host->req = req;
909 
910 	jz4740_mmc_write_irq_reg(host, ~0);
911 	jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_END_CMD_RES, true);
912 
913 	host->state = JZ4740_MMC_STATE_READ_RESPONSE;
914 	set_bit(0, &host->waiting);
915 	mod_timer(&host->timeout_timer,
916 		  jiffies + msecs_to_jiffies(JZ_MMC_REQ_TIMEOUT_MS));
917 	jz4740_mmc_send_command(host, req->cmd);
918 }
919 
jz4740_mmc_set_ios(struct mmc_host * mmc,struct mmc_ios * ios)920 static void jz4740_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
921 {
922 	struct jz4740_mmc_host *host = mmc_priv(mmc);
923 	if (ios->clock)
924 		jz4740_mmc_set_clock_rate(host, ios->clock);
925 
926 	switch (ios->power_mode) {
927 	case MMC_POWER_UP:
928 		jz4740_mmc_reset(host);
929 		if (!IS_ERR(mmc->supply.vmmc))
930 			mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
931 		host->cmdat |= JZ_MMC_CMDAT_INIT;
932 		clk_prepare_enable(host->clk);
933 		break;
934 	case MMC_POWER_ON:
935 		break;
936 	default:
937 		if (!IS_ERR(mmc->supply.vmmc))
938 			mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
939 		clk_disable_unprepare(host->clk);
940 		break;
941 	}
942 
943 	switch (ios->bus_width) {
944 	case MMC_BUS_WIDTH_1:
945 		host->cmdat &= ~JZ_MMC_CMDAT_BUS_WIDTH_MASK;
946 		break;
947 	case MMC_BUS_WIDTH_4:
948 		host->cmdat &= ~JZ_MMC_CMDAT_BUS_WIDTH_MASK;
949 		host->cmdat |= JZ_MMC_CMDAT_BUS_WIDTH_4BIT;
950 		break;
951 	case MMC_BUS_WIDTH_8:
952 		host->cmdat &= ~JZ_MMC_CMDAT_BUS_WIDTH_MASK;
953 		host->cmdat |= JZ_MMC_CMDAT_BUS_WIDTH_8BIT;
954 		break;
955 	default:
956 		break;
957 	}
958 }
959 
jz4740_mmc_enable_sdio_irq(struct mmc_host * mmc,int enable)960 static void jz4740_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
961 {
962 	struct jz4740_mmc_host *host = mmc_priv(mmc);
963 	jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_SDIO, enable);
964 }
965 
966 static const struct mmc_host_ops jz4740_mmc_ops = {
967 	.request	= jz4740_mmc_request,
968 	.pre_req	= jz4740_mmc_pre_request,
969 	.post_req	= jz4740_mmc_post_request,
970 	.set_ios	= jz4740_mmc_set_ios,
971 	.get_ro		= mmc_gpio_get_ro,
972 	.get_cd		= mmc_gpio_get_cd,
973 	.enable_sdio_irq = jz4740_mmc_enable_sdio_irq,
974 };
975 
976 static const struct of_device_id jz4740_mmc_of_match[] = {
977 	{ .compatible = "ingenic,jz4740-mmc", .data = (void *) JZ_MMC_JZ4740 },
978 	{ .compatible = "ingenic,jz4725b-mmc", .data = (void *)JZ_MMC_JZ4725B },
979 	{ .compatible = "ingenic,jz4760-mmc", .data = (void *) JZ_MMC_JZ4760 },
980 	{ .compatible = "ingenic,jz4775-mmc", .data = (void *) JZ_MMC_JZ4780 },
981 	{ .compatible = "ingenic,jz4780-mmc", .data = (void *) JZ_MMC_JZ4780 },
982 	{ .compatible = "ingenic,x1000-mmc", .data = (void *) JZ_MMC_X1000 },
983 	{},
984 };
985 MODULE_DEVICE_TABLE(of, jz4740_mmc_of_match);
986 
jz4740_mmc_probe(struct platform_device * pdev)987 static int jz4740_mmc_probe(struct platform_device* pdev)
988 {
989 	int ret;
990 	struct mmc_host *mmc;
991 	struct jz4740_mmc_host *host;
992 	const struct of_device_id *match;
993 
994 	mmc = mmc_alloc_host(sizeof(struct jz4740_mmc_host), &pdev->dev);
995 	if (!mmc) {
996 		dev_err(&pdev->dev, "Failed to alloc mmc host structure\n");
997 		return -ENOMEM;
998 	}
999 
1000 	host = mmc_priv(mmc);
1001 
1002 	match = of_match_device(jz4740_mmc_of_match, &pdev->dev);
1003 	if (match) {
1004 		host->version = (enum jz4740_mmc_version)match->data;
1005 	} else {
1006 		/* JZ4740 should be the only one using legacy probe */
1007 		host->version = JZ_MMC_JZ4740;
1008 	}
1009 
1010 	ret = mmc_of_parse(mmc);
1011 	if (ret) {
1012 		dev_err_probe(&pdev->dev, ret, "could not parse device properties\n");
1013 		goto err_free_host;
1014 	}
1015 
1016 	mmc_regulator_get_supply(mmc);
1017 
1018 	host->irq = platform_get_irq(pdev, 0);
1019 	if (host->irq < 0) {
1020 		ret = host->irq;
1021 		goto err_free_host;
1022 	}
1023 
1024 	host->clk = devm_clk_get(&pdev->dev, "mmc");
1025 	if (IS_ERR(host->clk)) {
1026 		ret = PTR_ERR(host->clk);
1027 		dev_err(&pdev->dev, "Failed to get mmc clock\n");
1028 		goto err_free_host;
1029 	}
1030 
1031 	host->mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1032 	host->base = devm_ioremap_resource(&pdev->dev, host->mem_res);
1033 	if (IS_ERR(host->base)) {
1034 		ret = PTR_ERR(host->base);
1035 		goto err_free_host;
1036 	}
1037 
1038 	mmc->ops = &jz4740_mmc_ops;
1039 	if (!mmc->f_max)
1040 		mmc->f_max = JZ_MMC_CLK_RATE;
1041 
1042 	/*
1043 	 * There seems to be a problem with this driver on the JZ4760 and
1044 	 * JZ4760B SoCs. There, when using the maximum rate supported (50 MHz),
1045 	 * the communication fails with many SD cards.
1046 	 * Until this bug is sorted out, limit the maximum rate to 24 MHz.
1047 	 */
1048 	if (host->version == JZ_MMC_JZ4760 && mmc->f_max > JZ_MMC_CLK_RATE)
1049 		mmc->f_max = JZ_MMC_CLK_RATE;
1050 
1051 	mmc->f_min = mmc->f_max / 128;
1052 	mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1053 
1054 	/*
1055 	 * We use a fixed timeout of 5s, hence inform the core about it. A
1056 	 * future improvement should instead respect the cmd->busy_timeout.
1057 	 */
1058 	mmc->max_busy_timeout = JZ_MMC_REQ_TIMEOUT_MS;
1059 
1060 	mmc->max_blk_size = (1 << 10) - 1;
1061 	mmc->max_blk_count = (1 << 15) - 1;
1062 	mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1063 
1064 	mmc->max_segs = 128;
1065 	mmc->max_seg_size = mmc->max_req_size;
1066 
1067 	host->mmc = mmc;
1068 	host->pdev = pdev;
1069 	spin_lock_init(&host->lock);
1070 	host->irq_mask = ~0;
1071 
1072 	jz4740_mmc_reset(host);
1073 
1074 	ret = request_threaded_irq(host->irq, jz_mmc_irq, jz_mmc_irq_worker, 0,
1075 			dev_name(&pdev->dev), host);
1076 	if (ret) {
1077 		dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
1078 		goto err_free_host;
1079 	}
1080 
1081 	jz4740_mmc_clock_disable(host);
1082 	timer_setup(&host->timeout_timer, jz4740_mmc_timeout, 0);
1083 
1084 	ret = jz4740_mmc_acquire_dma_channels(host);
1085 	if (ret == -EPROBE_DEFER)
1086 		goto err_free_irq;
1087 	host->use_dma = !ret;
1088 
1089 	platform_set_drvdata(pdev, host);
1090 	ret = mmc_add_host(mmc);
1091 
1092 	if (ret) {
1093 		dev_err(&pdev->dev, "Failed to add mmc host: %d\n", ret);
1094 		goto err_release_dma;
1095 	}
1096 	dev_info(&pdev->dev, "Ingenic SD/MMC card driver registered\n");
1097 
1098 	dev_info(&pdev->dev, "Using %s, %d-bit mode\n",
1099 		 host->use_dma ? "DMA" : "PIO",
1100 		 (mmc->caps & MMC_CAP_8_BIT_DATA) ? 8 :
1101 		 ((mmc->caps & MMC_CAP_4_BIT_DATA) ? 4 : 1));
1102 
1103 	return 0;
1104 
1105 err_release_dma:
1106 	if (host->use_dma)
1107 		jz4740_mmc_release_dma_channels(host);
1108 err_free_irq:
1109 	free_irq(host->irq, host);
1110 err_free_host:
1111 	mmc_free_host(mmc);
1112 
1113 	return ret;
1114 }
1115 
jz4740_mmc_remove(struct platform_device * pdev)1116 static int jz4740_mmc_remove(struct platform_device *pdev)
1117 {
1118 	struct jz4740_mmc_host *host = platform_get_drvdata(pdev);
1119 
1120 	del_timer_sync(&host->timeout_timer);
1121 	jz4740_mmc_set_irq_enabled(host, 0xff, false);
1122 	jz4740_mmc_reset(host);
1123 
1124 	mmc_remove_host(host->mmc);
1125 
1126 	free_irq(host->irq, host);
1127 
1128 	if (host->use_dma)
1129 		jz4740_mmc_release_dma_channels(host);
1130 
1131 	mmc_free_host(host->mmc);
1132 
1133 	return 0;
1134 }
1135 
jz4740_mmc_suspend(struct device * dev)1136 static int __maybe_unused jz4740_mmc_suspend(struct device *dev)
1137 {
1138 	return pinctrl_pm_select_sleep_state(dev);
1139 }
1140 
jz4740_mmc_resume(struct device * dev)1141 static int __maybe_unused jz4740_mmc_resume(struct device *dev)
1142 {
1143 	return pinctrl_select_default_state(dev);
1144 }
1145 
1146 static SIMPLE_DEV_PM_OPS(jz4740_mmc_pm_ops, jz4740_mmc_suspend,
1147 	jz4740_mmc_resume);
1148 
1149 static struct platform_driver jz4740_mmc_driver = {
1150 	.probe = jz4740_mmc_probe,
1151 	.remove = jz4740_mmc_remove,
1152 	.driver = {
1153 		.name = "jz4740-mmc",
1154 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1155 		.of_match_table = of_match_ptr(jz4740_mmc_of_match),
1156 		.pm = pm_ptr(&jz4740_mmc_pm_ops),
1157 	},
1158 };
1159 
1160 module_platform_driver(jz4740_mmc_driver);
1161 
1162 MODULE_DESCRIPTION("JZ4740 SD/MMC controller driver");
1163 MODULE_LICENSE("GPL");
1164 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1165