• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for the MMC / SD / SDIO IP found in:
4  *
5  * TC6393XB, TC6391XB, TC6387XB, T7L66XB, ASIC3, SH-Mobile SoCs
6  *
7  * Copyright (C) 2015-19 Renesas Electronics Corporation
8  * Copyright (C) 2016-19 Sang Engineering, Wolfram Sang
9  * Copyright (C) 2017 Horms Solutions, Simon Horman
10  * Copyright (C) 2011 Guennadi Liakhovetski
11  * Copyright (C) 2007 Ian Molton
12  * Copyright (C) 2004 Ian Molton
13  *
14  * This driver draws mainly on scattered spec sheets, Reverse engineering
15  * of the toshiba e800  SD driver and some parts of the 2.4 ASIC3 driver (4 bit
16  * support). (Further 4 bit support from a later datasheet).
17  *
18  * TODO:
19  *   Investigate using a workqueue for PIO transfers
20  *   Eliminate FIXMEs
21  *   Better Power management
22  *   Handle MMC errors better
23  *   double buffer support
24  *
25  */
26 
27 #include <linux/delay.h>
28 #include <linux/device.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/highmem.h>
31 #include <linux/interrupt.h>
32 #include <linux/io.h>
33 #include <linux/irq.h>
34 #include <linux/mfd/tmio.h>
35 #include <linux/mmc/card.h>
36 #include <linux/mmc/host.h>
37 #include <linux/mmc/mmc.h>
38 #include <linux/mmc/slot-gpio.h>
39 #include <linux/module.h>
40 #include <linux/pagemap.h>
41 #include <linux/platform_device.h>
42 #include <linux/pm_qos.h>
43 #include <linux/pm_runtime.h>
44 #include <linux/regulator/consumer.h>
45 #include <linux/mmc/sdio.h>
46 #include <linux/scatterlist.h>
47 #include <linux/sizes.h>
48 #include <linux/spinlock.h>
49 #include <linux/workqueue.h>
50 
51 #include "tmio_mmc.h"
52 
tmio_mmc_start_dma(struct tmio_mmc_host * host,struct mmc_data * data)53 static inline void tmio_mmc_start_dma(struct tmio_mmc_host *host,
54 				      struct mmc_data *data)
55 {
56 	if (host->dma_ops)
57 		host->dma_ops->start(host, data);
58 }
59 
tmio_mmc_enable_dma(struct tmio_mmc_host * host,bool enable)60 static inline void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
61 {
62 	if (host->dma_ops)
63 		host->dma_ops->enable(host, enable);
64 }
65 
tmio_mmc_request_dma(struct tmio_mmc_host * host,struct tmio_mmc_data * pdata)66 static inline void tmio_mmc_request_dma(struct tmio_mmc_host *host,
67 					struct tmio_mmc_data *pdata)
68 {
69 	if (host->dma_ops) {
70 		host->dma_ops->request(host, pdata);
71 	} else {
72 		host->chan_tx = NULL;
73 		host->chan_rx = NULL;
74 	}
75 }
76 
tmio_mmc_release_dma(struct tmio_mmc_host * host)77 static inline void tmio_mmc_release_dma(struct tmio_mmc_host *host)
78 {
79 	if (host->dma_ops)
80 		host->dma_ops->release(host);
81 }
82 
tmio_mmc_abort_dma(struct tmio_mmc_host * host)83 static inline void tmio_mmc_abort_dma(struct tmio_mmc_host *host)
84 {
85 	if (host->dma_ops)
86 		host->dma_ops->abort(host);
87 }
88 
tmio_mmc_dataend_dma(struct tmio_mmc_host * host)89 static inline void tmio_mmc_dataend_dma(struct tmio_mmc_host *host)
90 {
91 	if (host->dma_ops)
92 		host->dma_ops->dataend(host);
93 }
94 
tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host * host,u32 i)95 void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
96 {
97 	host->sdcard_irq_mask &= ~(i & TMIO_MASK_IRQ);
98 	sd_ctrl_write32_as_16_and_16(host, CTL_IRQ_MASK, host->sdcard_irq_mask);
99 }
100 EXPORT_SYMBOL_GPL(tmio_mmc_enable_mmc_irqs);
101 
tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host * host,u32 i)102 void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
103 {
104 	host->sdcard_irq_mask |= (i & TMIO_MASK_IRQ);
105 	sd_ctrl_write32_as_16_and_16(host, CTL_IRQ_MASK, host->sdcard_irq_mask);
106 }
107 EXPORT_SYMBOL_GPL(tmio_mmc_disable_mmc_irqs);
108 
tmio_mmc_ack_mmc_irqs(struct tmio_mmc_host * host,u32 i)109 static void tmio_mmc_ack_mmc_irqs(struct tmio_mmc_host *host, u32 i)
110 {
111 	sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, ~i);
112 }
113 
tmio_mmc_init_sg(struct tmio_mmc_host * host,struct mmc_data * data)114 static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
115 {
116 	host->sg_len = data->sg_len;
117 	host->sg_ptr = data->sg;
118 	host->sg_orig = data->sg;
119 	host->sg_off = 0;
120 }
121 
tmio_mmc_next_sg(struct tmio_mmc_host * host)122 static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
123 {
124 	host->sg_ptr = sg_next(host->sg_ptr);
125 	host->sg_off = 0;
126 	return --host->sg_len;
127 }
128 
129 #define CMDREQ_TIMEOUT	5000
130 
tmio_mmc_enable_sdio_irq(struct mmc_host * mmc,int enable)131 static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
132 {
133 	struct tmio_mmc_host *host = mmc_priv(mmc);
134 
135 	if (enable && !host->sdio_irq_enabled) {
136 		u16 sdio_status;
137 
138 		/* Keep device active while SDIO irq is enabled */
139 		pm_runtime_get_sync(mmc_dev(mmc));
140 
141 		host->sdio_irq_enabled = true;
142 		host->sdio_irq_mask = TMIO_SDIO_MASK_ALL & ~TMIO_SDIO_STAT_IOIRQ;
143 
144 		/* Clear obsolete interrupts before enabling */
145 		sdio_status = sd_ctrl_read16(host, CTL_SDIO_STATUS) & ~TMIO_SDIO_MASK_ALL;
146 		if (host->pdata->flags & TMIO_MMC_SDIO_STATUS_SETBITS)
147 			sdio_status |= TMIO_SDIO_SETBITS_MASK;
148 		sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status);
149 
150 		sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
151 	} else if (!enable && host->sdio_irq_enabled) {
152 		host->sdio_irq_mask = TMIO_SDIO_MASK_ALL;
153 		sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
154 
155 		host->sdio_irq_enabled = false;
156 		pm_runtime_mark_last_busy(mmc_dev(mmc));
157 		pm_runtime_put_autosuspend(mmc_dev(mmc));
158 	}
159 }
160 
tmio_mmc_reset(struct tmio_mmc_host * host)161 static void tmio_mmc_reset(struct tmio_mmc_host *host)
162 {
163 	/* FIXME - should we set stop clock reg here */
164 	sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
165 	usleep_range(10000, 11000);
166 	sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
167 	usleep_range(10000, 11000);
168 
169 	if (host->pdata->flags & TMIO_MMC_SDIO_IRQ) {
170 		sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
171 		sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
172 	}
173 }
174 
tmio_mmc_hw_reset(struct mmc_host * mmc)175 static void tmio_mmc_hw_reset(struct mmc_host *mmc)
176 {
177 	struct tmio_mmc_host *host = mmc_priv(mmc);
178 
179 	host->reset(host);
180 
181 	tmio_mmc_abort_dma(host);
182 
183 	if (host->hw_reset)
184 		host->hw_reset(host);
185 }
186 
tmio_mmc_reset_work(struct work_struct * work)187 static void tmio_mmc_reset_work(struct work_struct *work)
188 {
189 	struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
190 						  delayed_reset_work.work);
191 	struct mmc_request *mrq;
192 	unsigned long flags;
193 
194 	spin_lock_irqsave(&host->lock, flags);
195 	mrq = host->mrq;
196 
197 	/*
198 	 * is request already finished? Since we use a non-blocking
199 	 * cancel_delayed_work(), it can happen, that a .set_ios() call preempts
200 	 * us, so, have to check for IS_ERR(host->mrq)
201 	 */
202 	if (IS_ERR_OR_NULL(mrq) ||
203 	    time_is_after_jiffies(host->last_req_ts +
204 				  msecs_to_jiffies(CMDREQ_TIMEOUT))) {
205 		spin_unlock_irqrestore(&host->lock, flags);
206 		return;
207 	}
208 
209 	dev_warn(&host->pdev->dev,
210 		 "timeout waiting for hardware interrupt (CMD%u)\n",
211 		 mrq->cmd->opcode);
212 
213 	if (host->data)
214 		host->data->error = -ETIMEDOUT;
215 	else if (host->cmd)
216 		host->cmd->error = -ETIMEDOUT;
217 	else
218 		mrq->cmd->error = -ETIMEDOUT;
219 
220 	/* No new calls yet, but disallow concurrent tmio_mmc_done_work() */
221 	host->mrq = ERR_PTR(-EBUSY);
222 	host->cmd = NULL;
223 	host->data = NULL;
224 
225 	spin_unlock_irqrestore(&host->lock, flags);
226 
227 	tmio_mmc_hw_reset(host->mmc);
228 
229 	/* Ready for new calls */
230 	host->mrq = NULL;
231 
232 	mmc_request_done(host->mmc, mrq);
233 }
234 
235 /* These are the bitmasks the tmio chip requires to implement the MMC response
236  * types. Note that R1 and R6 are the same in this scheme. */
237 #define APP_CMD        0x0040
238 #define RESP_NONE      0x0300
239 #define RESP_R1        0x0400
240 #define RESP_R1B       0x0500
241 #define RESP_R2        0x0600
242 #define RESP_R3        0x0700
243 #define DATA_PRESENT   0x0800
244 #define TRANSFER_READ  0x1000
245 #define TRANSFER_MULTI 0x2000
246 #define SECURITY_CMD   0x4000
247 #define NO_CMD12_ISSUE 0x4000 /* TMIO_MMC_HAVE_CMD12_CTRL */
248 
tmio_mmc_start_command(struct tmio_mmc_host * host,struct mmc_command * cmd)249 static int tmio_mmc_start_command(struct tmio_mmc_host *host,
250 				  struct mmc_command *cmd)
251 {
252 	struct mmc_data *data = host->data;
253 	int c = cmd->opcode;
254 
255 	switch (mmc_resp_type(cmd)) {
256 	case MMC_RSP_NONE: c |= RESP_NONE; break;
257 	case MMC_RSP_R1:
258 	case MMC_RSP_R1_NO_CRC:
259 			   c |= RESP_R1;   break;
260 	case MMC_RSP_R1B:  c |= RESP_R1B;  break;
261 	case MMC_RSP_R2:   c |= RESP_R2;   break;
262 	case MMC_RSP_R3:   c |= RESP_R3;   break;
263 	default:
264 		pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
265 		return -EINVAL;
266 	}
267 
268 	host->cmd = cmd;
269 
270 /* FIXME - this seems to be ok commented out but the spec suggest this bit
271  *         should be set when issuing app commands.
272  *	if(cmd->flags & MMC_FLAG_ACMD)
273  *		c |= APP_CMD;
274  */
275 	if (data) {
276 		c |= DATA_PRESENT;
277 		if (data->blocks > 1) {
278 			sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, TMIO_STOP_SEC);
279 			c |= TRANSFER_MULTI;
280 
281 			/*
282 			 * Disable auto CMD12 at IO_RW_EXTENDED and
283 			 * SET_BLOCK_COUNT when doing multiple block transfer
284 			 */
285 			if ((host->pdata->flags & TMIO_MMC_HAVE_CMD12_CTRL) &&
286 			    (cmd->opcode == SD_IO_RW_EXTENDED || host->mrq->sbc))
287 				c |= NO_CMD12_ISSUE;
288 		}
289 		if (data->flags & MMC_DATA_READ)
290 			c |= TRANSFER_READ;
291 	}
292 
293 	tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_CMD);
294 
295 	/* Fire off the command */
296 	sd_ctrl_write32_as_16_and_16(host, CTL_ARG_REG, cmd->arg);
297 	sd_ctrl_write16(host, CTL_SD_CMD, c);
298 
299 	return 0;
300 }
301 
tmio_mmc_transfer_data(struct tmio_mmc_host * host,unsigned short * buf,unsigned int count)302 static void tmio_mmc_transfer_data(struct tmio_mmc_host *host,
303 				   unsigned short *buf,
304 				   unsigned int count)
305 {
306 	int is_read = host->data->flags & MMC_DATA_READ;
307 	u8  *buf8;
308 
309 	/*
310 	 * Transfer the data
311 	 */
312 	if (host->pdata->flags & TMIO_MMC_32BIT_DATA_PORT) {
313 		u32 data = 0;
314 		u32 *buf32 = (u32 *)buf;
315 
316 		if (is_read)
317 			sd_ctrl_read32_rep(host, CTL_SD_DATA_PORT, buf32,
318 					   count >> 2);
319 		else
320 			sd_ctrl_write32_rep(host, CTL_SD_DATA_PORT, buf32,
321 					    count >> 2);
322 
323 		/* if count was multiple of 4 */
324 		if (!(count & 0x3))
325 			return;
326 
327 		buf32 += count >> 2;
328 		count %= 4;
329 
330 		if (is_read) {
331 			sd_ctrl_read32_rep(host, CTL_SD_DATA_PORT, &data, 1);
332 			memcpy(buf32, &data, count);
333 		} else {
334 			memcpy(&data, buf32, count);
335 			sd_ctrl_write32_rep(host, CTL_SD_DATA_PORT, &data, 1);
336 		}
337 
338 		return;
339 	}
340 
341 	if (is_read)
342 		sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
343 	else
344 		sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
345 
346 	/* if count was even number */
347 	if (!(count & 0x1))
348 		return;
349 
350 	/* if count was odd number */
351 	buf8 = (u8 *)(buf + (count >> 1));
352 
353 	/*
354 	 * FIXME
355 	 *
356 	 * driver and this function are assuming that
357 	 * it is used as little endian
358 	 */
359 	if (is_read)
360 		*buf8 = sd_ctrl_read16(host, CTL_SD_DATA_PORT) & 0xff;
361 	else
362 		sd_ctrl_write16(host, CTL_SD_DATA_PORT, *buf8);
363 }
364 
365 /*
366  * This chip always returns (at least?) as much data as you ask for.
367  * I'm unsure what happens if you ask for less than a block. This should be
368  * looked into to ensure that a funny length read doesn't hose the controller.
369  */
tmio_mmc_pio_irq(struct tmio_mmc_host * host)370 static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
371 {
372 	struct mmc_data *data = host->data;
373 	void *sg_virt;
374 	unsigned short *buf;
375 	unsigned int count;
376 	unsigned long flags;
377 
378 	if (host->dma_on) {
379 		pr_err("PIO IRQ in DMA mode!\n");
380 		return;
381 	} else if (!data) {
382 		pr_debug("Spurious PIO IRQ\n");
383 		return;
384 	}
385 
386 	sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
387 	buf = (unsigned short *)(sg_virt + host->sg_off);
388 
389 	count = host->sg_ptr->length - host->sg_off;
390 	if (count > data->blksz)
391 		count = data->blksz;
392 
393 	pr_debug("count: %08x offset: %08x flags %08x\n",
394 		 count, host->sg_off, data->flags);
395 
396 	/* Transfer the data */
397 	tmio_mmc_transfer_data(host, buf, count);
398 
399 	host->sg_off += count;
400 
401 	tmio_mmc_kunmap_atomic(host->sg_ptr, &flags, sg_virt);
402 
403 	if (host->sg_off == host->sg_ptr->length)
404 		tmio_mmc_next_sg(host);
405 }
406 
tmio_mmc_check_bounce_buffer(struct tmio_mmc_host * host)407 static void tmio_mmc_check_bounce_buffer(struct tmio_mmc_host *host)
408 {
409 	if (host->sg_ptr == &host->bounce_sg) {
410 		unsigned long flags;
411 		void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
412 
413 		memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
414 		tmio_mmc_kunmap_atomic(host->sg_orig, &flags, sg_vaddr);
415 	}
416 }
417 
418 /* needs to be called with host->lock held */
tmio_mmc_do_data_irq(struct tmio_mmc_host * host)419 void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
420 {
421 	struct mmc_data *data = host->data;
422 	struct mmc_command *stop;
423 
424 	host->data = NULL;
425 
426 	if (!data) {
427 		dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
428 		return;
429 	}
430 	stop = data->stop;
431 
432 	/* FIXME - return correct transfer count on errors */
433 	if (!data->error)
434 		data->bytes_xfered = data->blocks * data->blksz;
435 	else
436 		data->bytes_xfered = 0;
437 
438 	pr_debug("Completed data request\n");
439 
440 	/*
441 	 * FIXME: other drivers allow an optional stop command of any given type
442 	 *        which we dont do, as the chip can auto generate them.
443 	 *        Perhaps we can be smarter about when to use auto CMD12 and
444 	 *        only issue the auto request when we know this is the desired
445 	 *        stop command, allowing fallback to the stop command the
446 	 *        upper layers expect. For now, we do what works.
447 	 */
448 
449 	if (data->flags & MMC_DATA_READ) {
450 		if (host->dma_on)
451 			tmio_mmc_check_bounce_buffer(host);
452 		dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
453 			host->mrq);
454 	} else {
455 		dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
456 			host->mrq);
457 	}
458 
459 	if (stop && !host->mrq->sbc) {
460 		if (stop->opcode != MMC_STOP_TRANSMISSION || stop->arg)
461 			dev_err(&host->pdev->dev, "unsupported stop: CMD%u,0x%x. We did CMD12,0\n",
462 				stop->opcode, stop->arg);
463 
464 		/* fill in response from auto CMD12 */
465 		stop->resp[0] = sd_ctrl_read16_and_16_as_32(host, CTL_RESPONSE);
466 
467 		sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0);
468 	}
469 
470 	schedule_work(&host->done);
471 }
472 EXPORT_SYMBOL_GPL(tmio_mmc_do_data_irq);
473 
tmio_mmc_data_irq(struct tmio_mmc_host * host,unsigned int stat)474 static void tmio_mmc_data_irq(struct tmio_mmc_host *host, unsigned int stat)
475 {
476 	struct mmc_data *data;
477 
478 	spin_lock(&host->lock);
479 	data = host->data;
480 
481 	if (!data)
482 		goto out;
483 
484 	if (stat & TMIO_STAT_CRCFAIL || stat & TMIO_STAT_STOPBIT_ERR ||
485 	    stat & TMIO_STAT_TXUNDERRUN)
486 		data->error = -EILSEQ;
487 	if (host->dma_on && (data->flags & MMC_DATA_WRITE)) {
488 		u32 status = sd_ctrl_read16_and_16_as_32(host, CTL_STATUS);
489 		bool done = false;
490 
491 		/*
492 		 * Has all data been written out yet? Testing on SuperH showed,
493 		 * that in most cases the first interrupt comes already with the
494 		 * BUSY status bit clear, but on some operations, like mount or
495 		 * in the beginning of a write / sync / umount, there is one
496 		 * DATAEND interrupt with the BUSY bit set, in this cases
497 		 * waiting for one more interrupt fixes the problem.
498 		 */
499 		if (host->pdata->flags & TMIO_MMC_HAS_IDLE_WAIT) {
500 			if (status & TMIO_STAT_SCLKDIVEN)
501 				done = true;
502 		} else {
503 			if (!(status & TMIO_STAT_CMD_BUSY))
504 				done = true;
505 		}
506 
507 		if (done) {
508 			tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
509 			tmio_mmc_dataend_dma(host);
510 		}
511 	} else if (host->dma_on && (data->flags & MMC_DATA_READ)) {
512 		tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
513 		tmio_mmc_dataend_dma(host);
514 	} else {
515 		tmio_mmc_do_data_irq(host);
516 		tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_READOP | TMIO_MASK_WRITEOP);
517 	}
518 out:
519 	spin_unlock(&host->lock);
520 }
521 
tmio_mmc_cmd_irq(struct tmio_mmc_host * host,unsigned int stat)522 static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host, unsigned int stat)
523 {
524 	struct mmc_command *cmd = host->cmd;
525 	int i, addr;
526 
527 	spin_lock(&host->lock);
528 
529 	if (!host->cmd) {
530 		pr_debug("Spurious CMD irq\n");
531 		goto out;
532 	}
533 
534 	/* This controller is sicker than the PXA one. Not only do we need to
535 	 * drop the top 8 bits of the first response word, we also need to
536 	 * modify the order of the response for short response command types.
537 	 */
538 
539 	for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
540 		cmd->resp[i] = sd_ctrl_read16_and_16_as_32(host, addr);
541 
542 	if (cmd->flags &  MMC_RSP_136) {
543 		cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
544 		cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
545 		cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
546 		cmd->resp[3] <<= 8;
547 	} else if (cmd->flags & MMC_RSP_R3) {
548 		cmd->resp[0] = cmd->resp[3];
549 	}
550 
551 	if (stat & TMIO_STAT_CMDTIMEOUT)
552 		cmd->error = -ETIMEDOUT;
553 	else if ((stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC) ||
554 		 stat & TMIO_STAT_STOPBIT_ERR ||
555 		 stat & TMIO_STAT_CMD_IDX_ERR)
556 		cmd->error = -EILSEQ;
557 
558 	/* If there is data to handle we enable data IRQs here, and
559 	 * we will ultimatley finish the request in the data_end handler.
560 	 * If theres no data or we encountered an error, finish now.
561 	 */
562 	if (host->data && (!cmd->error || cmd->error == -EILSEQ)) {
563 		if (host->data->flags & MMC_DATA_READ) {
564 			if (!host->dma_on) {
565 				tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_READOP);
566 			} else {
567 				tmio_mmc_disable_mmc_irqs(host,
568 							  TMIO_MASK_READOP);
569 				tasklet_schedule(&host->dma_issue);
570 			}
571 		} else {
572 			if (!host->dma_on) {
573 				tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
574 			} else {
575 				tmio_mmc_disable_mmc_irqs(host,
576 							  TMIO_MASK_WRITEOP);
577 				tasklet_schedule(&host->dma_issue);
578 			}
579 		}
580 	} else {
581 		schedule_work(&host->done);
582 	}
583 
584 out:
585 	spin_unlock(&host->lock);
586 }
587 
__tmio_mmc_card_detect_irq(struct tmio_mmc_host * host,int ireg,int status)588 static bool __tmio_mmc_card_detect_irq(struct tmio_mmc_host *host,
589 				       int ireg, int status)
590 {
591 	struct mmc_host *mmc = host->mmc;
592 
593 	/* Card insert / remove attempts */
594 	if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
595 		tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
596 			TMIO_STAT_CARD_REMOVE);
597 		if ((((ireg & TMIO_STAT_CARD_REMOVE) && mmc->card) ||
598 		     ((ireg & TMIO_STAT_CARD_INSERT) && !mmc->card)) &&
599 		    !work_pending(&mmc->detect.work))
600 			mmc_detect_change(host->mmc, msecs_to_jiffies(100));
601 		return true;
602 	}
603 
604 	return false;
605 }
606 
__tmio_mmc_sdcard_irq(struct tmio_mmc_host * host,int ireg,int status)607 static bool __tmio_mmc_sdcard_irq(struct tmio_mmc_host *host, int ireg,
608 				  int status)
609 {
610 	/* Command completion */
611 	if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
612 		tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CMDRESPEND |
613 				      TMIO_STAT_CMDTIMEOUT);
614 		tmio_mmc_cmd_irq(host, status);
615 		return true;
616 	}
617 
618 	/* Data transfer */
619 	if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
620 		tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
621 		tmio_mmc_pio_irq(host);
622 		return true;
623 	}
624 
625 	/* Data transfer completion */
626 	if (ireg & TMIO_STAT_DATAEND) {
627 		tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_DATAEND);
628 		tmio_mmc_data_irq(host, status);
629 		return true;
630 	}
631 
632 	return false;
633 }
634 
__tmio_mmc_sdio_irq(struct tmio_mmc_host * host)635 static bool __tmio_mmc_sdio_irq(struct tmio_mmc_host *host)
636 {
637 	struct mmc_host *mmc = host->mmc;
638 	struct tmio_mmc_data *pdata = host->pdata;
639 	unsigned int ireg, status;
640 	unsigned int sdio_status;
641 
642 	if (!(pdata->flags & TMIO_MMC_SDIO_IRQ))
643 		return false;
644 
645 	status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
646 	ireg = status & TMIO_SDIO_MASK_ALL & ~host->sdio_irq_mask;
647 
648 	sdio_status = status & ~TMIO_SDIO_MASK_ALL;
649 	if (pdata->flags & TMIO_MMC_SDIO_STATUS_SETBITS)
650 		sdio_status |= TMIO_SDIO_SETBITS_MASK;
651 
652 	sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status);
653 
654 	if (mmc->caps & MMC_CAP_SDIO_IRQ && ireg & TMIO_SDIO_STAT_IOIRQ)
655 		mmc_signal_sdio_irq(mmc);
656 
657 	return ireg;
658 }
659 
tmio_mmc_irq(int irq,void * devid)660 irqreturn_t tmio_mmc_irq(int irq, void *devid)
661 {
662 	struct tmio_mmc_host *host = devid;
663 	unsigned int ireg, status;
664 
665 	status = sd_ctrl_read16_and_16_as_32(host, CTL_STATUS);
666 	ireg = status & TMIO_MASK_IRQ & ~host->sdcard_irq_mask;
667 
668 	/* Clear the status except the interrupt status */
669 	sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, TMIO_MASK_IRQ);
670 
671 	if (__tmio_mmc_card_detect_irq(host, ireg, status))
672 		return IRQ_HANDLED;
673 	if (__tmio_mmc_sdcard_irq(host, ireg, status))
674 		return IRQ_HANDLED;
675 
676 	if (__tmio_mmc_sdio_irq(host))
677 		return IRQ_HANDLED;
678 
679 	return IRQ_NONE;
680 }
681 EXPORT_SYMBOL_GPL(tmio_mmc_irq);
682 
tmio_mmc_start_data(struct tmio_mmc_host * host,struct mmc_data * data)683 static int tmio_mmc_start_data(struct tmio_mmc_host *host,
684 			       struct mmc_data *data)
685 {
686 	struct tmio_mmc_data *pdata = host->pdata;
687 
688 	pr_debug("setup data transfer: blocksize %08x  nr_blocks %d\n",
689 		 data->blksz, data->blocks);
690 
691 	/* Some hardware cannot perform 2 byte requests in 4/8 bit mode */
692 	if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4 ||
693 	    host->mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
694 		int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
695 
696 		if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
697 			pr_err("%s: %d byte block unsupported in 4/8 bit mode\n",
698 			       mmc_hostname(host->mmc), data->blksz);
699 			return -EINVAL;
700 		}
701 	}
702 
703 	tmio_mmc_init_sg(host, data);
704 	host->data = data;
705 	host->dma_on = false;
706 
707 	/* Set transfer length / blocksize */
708 	sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
709 	if (host->mmc->max_blk_count >= SZ_64K)
710 		sd_ctrl_write32(host, CTL_XFER_BLK_COUNT, data->blocks);
711 	else
712 		sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
713 
714 	tmio_mmc_start_dma(host, data);
715 
716 	return 0;
717 }
718 
tmio_mmc_execute_tuning(struct mmc_host * mmc,u32 opcode)719 static int tmio_mmc_execute_tuning(struct mmc_host *mmc, u32 opcode)
720 {
721 	struct tmio_mmc_host *host = mmc_priv(mmc);
722 	int i, ret = 0;
723 
724 	if (!host->init_tuning || !host->select_tuning)
725 		/* Tuning is not supported */
726 		goto out;
727 
728 	host->tap_num = host->init_tuning(host);
729 	if (!host->tap_num)
730 		/* Tuning is not supported */
731 		goto out;
732 
733 	if (host->tap_num * 2 >= sizeof(host->taps) * BITS_PER_BYTE) {
734 		dev_warn_once(&host->pdev->dev,
735 			"Too many taps, skipping tuning. Please consider updating size of taps field of tmio_mmc_host\n");
736 		goto out;
737 	}
738 
739 	bitmap_zero(host->taps, host->tap_num * 2);
740 
741 	/* Issue CMD19 twice for each tap */
742 	for (i = 0; i < 2 * host->tap_num; i++) {
743 		if (host->prepare_tuning)
744 			host->prepare_tuning(host, i % host->tap_num);
745 
746 		ret = mmc_send_tuning(mmc, opcode, NULL);
747 		if (ret == 0)
748 			set_bit(i, host->taps);
749 	}
750 
751 	ret = host->select_tuning(host);
752 
753 out:
754 	if (ret < 0) {
755 		dev_warn(&host->pdev->dev, "Tuning procedure failed\n");
756 		tmio_mmc_hw_reset(mmc);
757 	}
758 
759 	return ret;
760 }
761 
tmio_process_mrq(struct tmio_mmc_host * host,struct mmc_request * mrq)762 static void tmio_process_mrq(struct tmio_mmc_host *host,
763 			     struct mmc_request *mrq)
764 {
765 	struct mmc_command *cmd;
766 	int ret;
767 
768 	if (mrq->sbc && host->cmd != mrq->sbc) {
769 		cmd = mrq->sbc;
770 	} else {
771 		cmd = mrq->cmd;
772 		if (mrq->data) {
773 			ret = tmio_mmc_start_data(host, mrq->data);
774 			if (ret)
775 				goto fail;
776 		}
777 	}
778 
779 	ret = tmio_mmc_start_command(host, cmd);
780 	if (ret)
781 		goto fail;
782 
783 	schedule_delayed_work(&host->delayed_reset_work,
784 			      msecs_to_jiffies(CMDREQ_TIMEOUT));
785 	return;
786 
787 fail:
788 	host->mrq = NULL;
789 	mrq->cmd->error = ret;
790 	mmc_request_done(host->mmc, mrq);
791 }
792 
793 /* Process requests from the MMC layer */
tmio_mmc_request(struct mmc_host * mmc,struct mmc_request * mrq)794 static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
795 {
796 	struct tmio_mmc_host *host = mmc_priv(mmc);
797 	unsigned long flags;
798 
799 	spin_lock_irqsave(&host->lock, flags);
800 
801 	if (host->mrq) {
802 		pr_debug("request not null\n");
803 		if (IS_ERR(host->mrq)) {
804 			spin_unlock_irqrestore(&host->lock, flags);
805 			mrq->cmd->error = -EAGAIN;
806 			mmc_request_done(mmc, mrq);
807 			return;
808 		}
809 	}
810 
811 	host->last_req_ts = jiffies;
812 	wmb();
813 	host->mrq = mrq;
814 
815 	spin_unlock_irqrestore(&host->lock, flags);
816 
817 	tmio_process_mrq(host, mrq);
818 }
819 
tmio_mmc_finish_request(struct tmio_mmc_host * host)820 static void tmio_mmc_finish_request(struct tmio_mmc_host *host)
821 {
822 	struct mmc_request *mrq;
823 	unsigned long flags;
824 
825 	spin_lock_irqsave(&host->lock, flags);
826 
827 	mrq = host->mrq;
828 	if (IS_ERR_OR_NULL(mrq)) {
829 		spin_unlock_irqrestore(&host->lock, flags);
830 		return;
831 	}
832 
833 	/* If not SET_BLOCK_COUNT, clear old data */
834 	if (host->cmd != mrq->sbc) {
835 		host->cmd = NULL;
836 		host->data = NULL;
837 		host->mrq = NULL;
838 	}
839 
840 	cancel_delayed_work(&host->delayed_reset_work);
841 
842 	spin_unlock_irqrestore(&host->lock, flags);
843 
844 	if (mrq->cmd->error || (mrq->data && mrq->data->error))
845 		tmio_mmc_abort_dma(host);
846 
847 	/* SCC error means retune, but executed command was still successful */
848 	if (host->check_scc_error && host->check_scc_error(host))
849 		mmc_retune_needed(host->mmc);
850 
851 	/* If SET_BLOCK_COUNT, continue with main command */
852 	if (host->mrq && !mrq->cmd->error) {
853 		tmio_process_mrq(host, mrq);
854 		return;
855 	}
856 
857 	mmc_request_done(host->mmc, mrq);
858 }
859 
tmio_mmc_done_work(struct work_struct * work)860 static void tmio_mmc_done_work(struct work_struct *work)
861 {
862 	struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
863 						  done);
864 	tmio_mmc_finish_request(host);
865 }
866 
tmio_mmc_power_on(struct tmio_mmc_host * host,unsigned short vdd)867 static void tmio_mmc_power_on(struct tmio_mmc_host *host, unsigned short vdd)
868 {
869 	struct mmc_host *mmc = host->mmc;
870 	int ret = 0;
871 
872 	/* .set_ios() is returning void, so, no chance to report an error */
873 
874 	if (host->set_pwr)
875 		host->set_pwr(host->pdev, 1);
876 
877 	if (!IS_ERR(mmc->supply.vmmc)) {
878 		ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
879 		/*
880 		 * Attention: empiric value. With a b43 WiFi SDIO card this
881 		 * delay proved necessary for reliable card-insertion probing.
882 		 * 100us were not enough. Is this the same 140us delay, as in
883 		 * tmio_mmc_set_ios()?
884 		 */
885 		usleep_range(200, 300);
886 	}
887 	/*
888 	 * It seems, VccQ should be switched on after Vcc, this is also what the
889 	 * omap_hsmmc.c driver does.
890 	 */
891 	if (!IS_ERR(mmc->supply.vqmmc) && !ret) {
892 		ret = regulator_enable(mmc->supply.vqmmc);
893 		usleep_range(200, 300);
894 	}
895 
896 	if (ret < 0)
897 		dev_dbg(&host->pdev->dev, "Regulators failed to power up: %d\n",
898 			ret);
899 }
900 
tmio_mmc_power_off(struct tmio_mmc_host * host)901 static void tmio_mmc_power_off(struct tmio_mmc_host *host)
902 {
903 	struct mmc_host *mmc = host->mmc;
904 
905 	if (!IS_ERR(mmc->supply.vqmmc))
906 		regulator_disable(mmc->supply.vqmmc);
907 
908 	if (!IS_ERR(mmc->supply.vmmc))
909 		mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
910 
911 	if (host->set_pwr)
912 		host->set_pwr(host->pdev, 0);
913 }
914 
tmio_mmc_set_bus_width(struct tmio_mmc_host * host,unsigned char bus_width)915 static void tmio_mmc_set_bus_width(struct tmio_mmc_host *host,
916 				   unsigned char bus_width)
917 {
918 	u16 reg = sd_ctrl_read16(host, CTL_SD_MEM_CARD_OPT)
919 				& ~(CARD_OPT_WIDTH | CARD_OPT_WIDTH8);
920 
921 	/* reg now applies to MMC_BUS_WIDTH_4 */
922 	if (bus_width == MMC_BUS_WIDTH_1)
923 		reg |= CARD_OPT_WIDTH;
924 	else if (bus_width == MMC_BUS_WIDTH_8)
925 		reg |= CARD_OPT_WIDTH8;
926 
927 	sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, reg);
928 }
929 
930 /* Set MMC clock / power.
931  * Note: This controller uses a simple divider scheme therefore it cannot
932  * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
933  * MMC wont run that fast, it has to be clocked at 12MHz which is the next
934  * slowest setting.
935  */
tmio_mmc_set_ios(struct mmc_host * mmc,struct mmc_ios * ios)936 static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
937 {
938 	struct tmio_mmc_host *host = mmc_priv(mmc);
939 	struct device *dev = &host->pdev->dev;
940 	unsigned long flags;
941 
942 	mutex_lock(&host->ios_lock);
943 
944 	spin_lock_irqsave(&host->lock, flags);
945 	if (host->mrq) {
946 		if (IS_ERR(host->mrq)) {
947 			dev_dbg(dev,
948 				"%s.%d: concurrent .set_ios(), clk %u, mode %u\n",
949 				current->comm, task_pid_nr(current),
950 				ios->clock, ios->power_mode);
951 			host->mrq = ERR_PTR(-EINTR);
952 		} else {
953 			dev_dbg(dev,
954 				"%s.%d: CMD%u active since %lu, now %lu!\n",
955 				current->comm, task_pid_nr(current),
956 				host->mrq->cmd->opcode, host->last_req_ts,
957 				jiffies);
958 		}
959 		spin_unlock_irqrestore(&host->lock, flags);
960 
961 		mutex_unlock(&host->ios_lock);
962 		return;
963 	}
964 
965 	host->mrq = ERR_PTR(-EBUSY);
966 
967 	spin_unlock_irqrestore(&host->lock, flags);
968 
969 	switch (ios->power_mode) {
970 	case MMC_POWER_OFF:
971 		tmio_mmc_power_off(host);
972 		host->set_clock(host, 0);
973 		break;
974 	case MMC_POWER_UP:
975 		tmio_mmc_power_on(host, ios->vdd);
976 		host->set_clock(host, ios->clock);
977 		tmio_mmc_set_bus_width(host, ios->bus_width);
978 		break;
979 	case MMC_POWER_ON:
980 		host->set_clock(host, ios->clock);
981 		tmio_mmc_set_bus_width(host, ios->bus_width);
982 		break;
983 	}
984 
985 	/* Let things settle. delay taken from winCE driver */
986 	usleep_range(140, 200);
987 	if (PTR_ERR(host->mrq) == -EINTR)
988 		dev_dbg(&host->pdev->dev,
989 			"%s.%d: IOS interrupted: clk %u, mode %u",
990 			current->comm, task_pid_nr(current),
991 			ios->clock, ios->power_mode);
992 	host->mrq = NULL;
993 
994 	host->clk_cache = ios->clock;
995 
996 	mutex_unlock(&host->ios_lock);
997 }
998 
tmio_mmc_get_ro(struct mmc_host * mmc)999 static int tmio_mmc_get_ro(struct mmc_host *mmc)
1000 {
1001 	struct tmio_mmc_host *host = mmc_priv(mmc);
1002 
1003 	return !(sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) &
1004 		 TMIO_STAT_WRPROTECT);
1005 }
1006 
tmio_mmc_get_cd(struct mmc_host * mmc)1007 static int tmio_mmc_get_cd(struct mmc_host *mmc)
1008 {
1009 	struct tmio_mmc_host *host = mmc_priv(mmc);
1010 
1011 	return !!(sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) &
1012 		  TMIO_STAT_SIGSTATE);
1013 }
1014 
tmio_multi_io_quirk(struct mmc_card * card,unsigned int direction,int blk_size)1015 static int tmio_multi_io_quirk(struct mmc_card *card,
1016 			       unsigned int direction, int blk_size)
1017 {
1018 	struct tmio_mmc_host *host = mmc_priv(card->host);
1019 
1020 	if (host->multi_io_quirk)
1021 		return host->multi_io_quirk(card, direction, blk_size);
1022 
1023 	return blk_size;
1024 }
1025 
tmio_mmc_prepare_hs400_tuning(struct mmc_host * mmc,struct mmc_ios * ios)1026 static int tmio_mmc_prepare_hs400_tuning(struct mmc_host *mmc,
1027 					 struct mmc_ios *ios)
1028 {
1029 	struct tmio_mmc_host *host = mmc_priv(mmc);
1030 
1031 	if (host->prepare_hs400_tuning)
1032 		host->prepare_hs400_tuning(host);
1033 
1034 	return 0;
1035 }
1036 
tmio_mmc_hs400_downgrade(struct mmc_host * mmc)1037 static void tmio_mmc_hs400_downgrade(struct mmc_host *mmc)
1038 {
1039 	struct tmio_mmc_host *host = mmc_priv(mmc);
1040 
1041 	if (host->hs400_downgrade)
1042 		host->hs400_downgrade(host);
1043 }
1044 
tmio_mmc_hs400_complete(struct mmc_host * mmc)1045 static void tmio_mmc_hs400_complete(struct mmc_host *mmc)
1046 {
1047 	struct tmio_mmc_host *host = mmc_priv(mmc);
1048 
1049 	if (host->hs400_complete)
1050 		host->hs400_complete(host);
1051 }
1052 
1053 static const struct mmc_host_ops tmio_mmc_ops = {
1054 	.request	= tmio_mmc_request,
1055 	.set_ios	= tmio_mmc_set_ios,
1056 	.get_ro         = tmio_mmc_get_ro,
1057 	.get_cd		= tmio_mmc_get_cd,
1058 	.enable_sdio_irq = tmio_mmc_enable_sdio_irq,
1059 	.multi_io_quirk	= tmio_multi_io_quirk,
1060 	.hw_reset	= tmio_mmc_hw_reset,
1061 	.execute_tuning = tmio_mmc_execute_tuning,
1062 	.prepare_hs400_tuning = tmio_mmc_prepare_hs400_tuning,
1063 	.hs400_downgrade = tmio_mmc_hs400_downgrade,
1064 	.hs400_complete	= tmio_mmc_hs400_complete,
1065 };
1066 
tmio_mmc_init_ocr(struct tmio_mmc_host * host)1067 static int tmio_mmc_init_ocr(struct tmio_mmc_host *host)
1068 {
1069 	struct tmio_mmc_data *pdata = host->pdata;
1070 	struct mmc_host *mmc = host->mmc;
1071 	int err;
1072 
1073 	err = mmc_regulator_get_supply(mmc);
1074 	if (err)
1075 		return err;
1076 
1077 	/* use ocr_mask if no regulator */
1078 	if (!mmc->ocr_avail)
1079 		mmc->ocr_avail = pdata->ocr_mask;
1080 
1081 	/*
1082 	 * try again.
1083 	 * There is possibility that regulator has not been probed
1084 	 */
1085 	if (!mmc->ocr_avail)
1086 		return -EPROBE_DEFER;
1087 
1088 	return 0;
1089 }
1090 
tmio_mmc_of_parse(struct platform_device * pdev,struct mmc_host * mmc)1091 static void tmio_mmc_of_parse(struct platform_device *pdev,
1092 			      struct mmc_host *mmc)
1093 {
1094 	const struct device_node *np = pdev->dev.of_node;
1095 
1096 	if (!np)
1097 		return;
1098 
1099 	/*
1100 	 * DEPRECATED:
1101 	 * For new platforms, please use "disable-wp" instead of
1102 	 * "toshiba,mmc-wrprotect-disable"
1103 	 */
1104 	if (of_get_property(np, "toshiba,mmc-wrprotect-disable", NULL))
1105 		mmc->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
1106 }
1107 
tmio_mmc_host_alloc(struct platform_device * pdev,struct tmio_mmc_data * pdata)1108 struct tmio_mmc_host *tmio_mmc_host_alloc(struct platform_device *pdev,
1109 					  struct tmio_mmc_data *pdata)
1110 {
1111 	struct tmio_mmc_host *host;
1112 	struct mmc_host *mmc;
1113 	struct resource *res;
1114 	void __iomem *ctl;
1115 	int ret;
1116 
1117 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1118 	ctl = devm_ioremap_resource(&pdev->dev, res);
1119 	if (IS_ERR(ctl))
1120 		return ERR_CAST(ctl);
1121 
1122 	mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &pdev->dev);
1123 	if (!mmc)
1124 		return ERR_PTR(-ENOMEM);
1125 
1126 	host = mmc_priv(mmc);
1127 	host->ctl = ctl;
1128 	host->mmc = mmc;
1129 	host->pdev = pdev;
1130 	host->pdata = pdata;
1131 	host->ops = tmio_mmc_ops;
1132 	mmc->ops = &host->ops;
1133 
1134 	ret = mmc_of_parse(host->mmc);
1135 	if (ret) {
1136 		host = ERR_PTR(ret);
1137 		goto free;
1138 	}
1139 
1140 	tmio_mmc_of_parse(pdev, mmc);
1141 
1142 	platform_set_drvdata(pdev, host);
1143 
1144 	return host;
1145 free:
1146 	mmc_free_host(mmc);
1147 
1148 	return host;
1149 }
1150 EXPORT_SYMBOL_GPL(tmio_mmc_host_alloc);
1151 
tmio_mmc_host_free(struct tmio_mmc_host * host)1152 void tmio_mmc_host_free(struct tmio_mmc_host *host)
1153 {
1154 	mmc_free_host(host->mmc);
1155 }
1156 EXPORT_SYMBOL_GPL(tmio_mmc_host_free);
1157 
tmio_mmc_host_probe(struct tmio_mmc_host * _host)1158 int tmio_mmc_host_probe(struct tmio_mmc_host *_host)
1159 {
1160 	struct platform_device *pdev = _host->pdev;
1161 	struct tmio_mmc_data *pdata = _host->pdata;
1162 	struct mmc_host *mmc = _host->mmc;
1163 	int ret;
1164 
1165 	/*
1166 	 * Check the sanity of mmc->f_min to prevent host->set_clock() from
1167 	 * looping forever...
1168 	 */
1169 	if (mmc->f_min == 0)
1170 		return -EINVAL;
1171 
1172 	if (!(pdata->flags & TMIO_MMC_HAS_IDLE_WAIT))
1173 		_host->write16_hook = NULL;
1174 
1175 	_host->set_pwr = pdata->set_pwr;
1176 
1177 	ret = tmio_mmc_init_ocr(_host);
1178 	if (ret < 0)
1179 		return ret;
1180 
1181 	/*
1182 	 * Look for a card detect GPIO, if it fails with anything
1183 	 * else than a probe deferral, just live without it.
1184 	 */
1185 	ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0, NULL);
1186 	if (ret == -EPROBE_DEFER)
1187 		return ret;
1188 
1189 	mmc->caps |= MMC_CAP_ERASE | MMC_CAP_4_BIT_DATA | pdata->capabilities;
1190 	mmc->caps2 |= pdata->capabilities2;
1191 	mmc->max_segs = pdata->max_segs ? : 32;
1192 	mmc->max_blk_size = TMIO_MAX_BLK_SIZE;
1193 	mmc->max_blk_count = pdata->max_blk_count ? :
1194 		(PAGE_SIZE / mmc->max_blk_size) * mmc->max_segs;
1195 	mmc->max_req_size = min_t(size_t,
1196 				  mmc->max_blk_size * mmc->max_blk_count,
1197 				  dma_max_mapping_size(&pdev->dev));
1198 	mmc->max_seg_size = mmc->max_req_size;
1199 
1200 	if (mmc_can_gpio_ro(mmc))
1201 		_host->ops.get_ro = mmc_gpio_get_ro;
1202 
1203 	if (mmc_can_gpio_cd(mmc))
1204 		_host->ops.get_cd = mmc_gpio_get_cd;
1205 
1206 	_host->native_hotplug = !(mmc_can_gpio_cd(mmc) ||
1207 				  mmc->caps & MMC_CAP_NEEDS_POLL ||
1208 				  !mmc_card_is_removable(mmc));
1209 
1210 	if (!_host->reset)
1211 		_host->reset = tmio_mmc_reset;
1212 
1213 	/*
1214 	 * On Gen2+, eMMC with NONREMOVABLE currently fails because native
1215 	 * hotplug gets disabled. It seems RuntimePM related yet we need further
1216 	 * research. Since we are planning a PM overhaul anyway, let's enforce
1217 	 * for now the device being active by enabling native hotplug always.
1218 	 */
1219 	if (pdata->flags & TMIO_MMC_MIN_RCAR2)
1220 		_host->native_hotplug = true;
1221 
1222 	/*
1223 	 * While using internal tmio hardware logic for card detection, we need
1224 	 * to ensure it stays powered for it to work.
1225 	 */
1226 	if (_host->native_hotplug)
1227 		pm_runtime_get_noresume(&pdev->dev);
1228 
1229 	_host->sdio_irq_enabled = false;
1230 	if (pdata->flags & TMIO_MMC_SDIO_IRQ)
1231 		_host->sdio_irq_mask = TMIO_SDIO_MASK_ALL;
1232 
1233 	_host->set_clock(_host, 0);
1234 	tmio_mmc_hw_reset(mmc);
1235 
1236 	_host->sdcard_irq_mask = sd_ctrl_read16_and_16_as_32(_host, CTL_IRQ_MASK);
1237 	tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL);
1238 
1239 	if (_host->native_hotplug)
1240 		tmio_mmc_enable_mmc_irqs(_host,
1241 				TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT);
1242 
1243 	spin_lock_init(&_host->lock);
1244 	mutex_init(&_host->ios_lock);
1245 
1246 	/* Init delayed work for request timeouts */
1247 	INIT_DELAYED_WORK(&_host->delayed_reset_work, tmio_mmc_reset_work);
1248 	INIT_WORK(&_host->done, tmio_mmc_done_work);
1249 
1250 	/* See if we also get DMA */
1251 	tmio_mmc_request_dma(_host, pdata);
1252 
1253 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1254 	pm_runtime_use_autosuspend(&pdev->dev);
1255 	pm_runtime_enable(&pdev->dev);
1256 	pm_runtime_get_sync(&pdev->dev);
1257 
1258 	ret = mmc_add_host(mmc);
1259 	if (ret)
1260 		goto remove_host;
1261 
1262 	dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
1263 	pm_runtime_put(&pdev->dev);
1264 
1265 	return 0;
1266 
1267 remove_host:
1268 	pm_runtime_put_noidle(&pdev->dev);
1269 	tmio_mmc_host_remove(_host);
1270 	return ret;
1271 }
1272 EXPORT_SYMBOL_GPL(tmio_mmc_host_probe);
1273 
tmio_mmc_host_remove(struct tmio_mmc_host * host)1274 void tmio_mmc_host_remove(struct tmio_mmc_host *host)
1275 {
1276 	struct platform_device *pdev = host->pdev;
1277 	struct mmc_host *mmc = host->mmc;
1278 
1279 	pm_runtime_get_sync(&pdev->dev);
1280 
1281 	if (host->pdata->flags & TMIO_MMC_SDIO_IRQ)
1282 		sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000);
1283 
1284 	dev_pm_qos_hide_latency_limit(&pdev->dev);
1285 
1286 	mmc_remove_host(mmc);
1287 	cancel_work_sync(&host->done);
1288 	cancel_delayed_work_sync(&host->delayed_reset_work);
1289 	tmio_mmc_release_dma(host);
1290 	tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_ALL);
1291 
1292 	if (host->native_hotplug)
1293 		pm_runtime_put_noidle(&pdev->dev);
1294 
1295 	pm_runtime_disable(&pdev->dev);
1296 	pm_runtime_dont_use_autosuspend(&pdev->dev);
1297 	pm_runtime_put_noidle(&pdev->dev);
1298 }
1299 EXPORT_SYMBOL_GPL(tmio_mmc_host_remove);
1300 
1301 #ifdef CONFIG_PM
tmio_mmc_clk_enable(struct tmio_mmc_host * host)1302 static int tmio_mmc_clk_enable(struct tmio_mmc_host *host)
1303 {
1304 	if (!host->clk_enable)
1305 		return -ENOTSUPP;
1306 
1307 	return host->clk_enable(host);
1308 }
1309 
tmio_mmc_clk_disable(struct tmio_mmc_host * host)1310 static void tmio_mmc_clk_disable(struct tmio_mmc_host *host)
1311 {
1312 	if (host->clk_disable)
1313 		host->clk_disable(host);
1314 }
1315 
tmio_mmc_host_runtime_suspend(struct device * dev)1316 int tmio_mmc_host_runtime_suspend(struct device *dev)
1317 {
1318 	struct tmio_mmc_host *host = dev_get_drvdata(dev);
1319 
1320 	tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_ALL);
1321 
1322 	if (host->clk_cache)
1323 		host->set_clock(host, 0);
1324 
1325 	tmio_mmc_clk_disable(host);
1326 
1327 	return 0;
1328 }
1329 EXPORT_SYMBOL_GPL(tmio_mmc_host_runtime_suspend);
1330 
tmio_mmc_can_retune(struct tmio_mmc_host * host)1331 static bool tmio_mmc_can_retune(struct tmio_mmc_host *host)
1332 {
1333 	return host->tap_num && mmc_can_retune(host->mmc);
1334 }
1335 
tmio_mmc_host_runtime_resume(struct device * dev)1336 int tmio_mmc_host_runtime_resume(struct device *dev)
1337 {
1338 	struct tmio_mmc_host *host = dev_get_drvdata(dev);
1339 
1340 	if (!host->runtime_synced) {
1341 		host->runtime_synced = true;
1342 		return 0;
1343 	}
1344 
1345 	tmio_mmc_clk_enable(host);
1346 	tmio_mmc_hw_reset(host->mmc);
1347 
1348 	if (host->clk_cache)
1349 		host->set_clock(host, host->clk_cache);
1350 
1351 	if (host->native_hotplug)
1352 		tmio_mmc_enable_mmc_irqs(host,
1353 				TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT);
1354 
1355 	tmio_mmc_enable_dma(host, true);
1356 
1357 	if (tmio_mmc_can_retune(host) && host->select_tuning(host))
1358 		dev_warn(&host->pdev->dev, "Tuning selection failed\n");
1359 
1360 	return 0;
1361 }
1362 EXPORT_SYMBOL_GPL(tmio_mmc_host_runtime_resume);
1363 #endif
1364 
1365 MODULE_LICENSE("GPL v2");
1366