xref: /openbmc/linux/drivers/mmc/host/tmio_mmc_core.c (revision bd169abd)
1f707079dSWolfram Sang // SPDX-License-Identifier: GPL-2.0
2426e95d1SSimon Horman /*
3426e95d1SSimon Horman  * Driver for the MMC / SD / SDIO IP found in:
4426e95d1SSimon Horman  *
5426e95d1SSimon Horman  * TC6393XB, TC6391XB, TC6387XB, T7L66XB, ASIC3, SH-Mobile SoCs
6426e95d1SSimon Horman  *
7f49bdcdeSWolfram Sang  * Copyright (C) 2015-19 Renesas Electronics Corporation
8f49bdcdeSWolfram Sang  * Copyright (C) 2016-19 Sang Engineering, Wolfram Sang
987317c4dSSimon Horman  * Copyright (C) 2017 Horms Solutions, Simon Horman
10426e95d1SSimon Horman  * Copyright (C) 2011 Guennadi Liakhovetski
11426e95d1SSimon Horman  * Copyright (C) 2007 Ian Molton
12426e95d1SSimon Horman  * Copyright (C) 2004 Ian Molton
13426e95d1SSimon Horman  *
14426e95d1SSimon Horman  * This driver draws mainly on scattered spec sheets, Reverse engineering
15426e95d1SSimon Horman  * of the toshiba e800  SD driver and some parts of the 2.4 ASIC3 driver (4 bit
16426e95d1SSimon Horman  * support). (Further 4 bit support from a later datasheet).
17426e95d1SSimon Horman  *
18426e95d1SSimon Horman  * TODO:
19426e95d1SSimon Horman  *   Investigate using a workqueue for PIO transfers
20426e95d1SSimon Horman  *   Eliminate FIXMEs
21426e95d1SSimon Horman  *   Better Power management
22426e95d1SSimon Horman  *   Handle MMC errors better
23426e95d1SSimon Horman  *   double buffer support
24426e95d1SSimon Horman  *
25426e95d1SSimon Horman  */
26426e95d1SSimon Horman 
27426e95d1SSimon Horman #include <linux/delay.h>
28426e95d1SSimon Horman #include <linux/device.h>
2963624d13SYoshihiro Shimoda #include <linux/dma-mapping.h>
30426e95d1SSimon Horman #include <linux/highmem.h>
31426e95d1SSimon Horman #include <linux/interrupt.h>
32426e95d1SSimon Horman #include <linux/io.h>
33426e95d1SSimon Horman #include <linux/irq.h>
34426e95d1SSimon Horman #include <linux/mfd/tmio.h>
35426e95d1SSimon Horman #include <linux/mmc/card.h>
36426e95d1SSimon Horman #include <linux/mmc/host.h>
37426e95d1SSimon Horman #include <linux/mmc/mmc.h>
38426e95d1SSimon Horman #include <linux/mmc/slot-gpio.h>
39426e95d1SSimon Horman #include <linux/module.h>
40426e95d1SSimon Horman #include <linux/pagemap.h>
41426e95d1SSimon Horman #include <linux/platform_device.h>
42426e95d1SSimon Horman #include <linux/pm_qos.h>
43426e95d1SSimon Horman #include <linux/pm_runtime.h>
44426e95d1SSimon Horman #include <linux/regulator/consumer.h>
45426e95d1SSimon Horman #include <linux/mmc/sdio.h>
46426e95d1SSimon Horman #include <linux/scatterlist.h>
475603731aSTakeshi Saito #include <linux/sizes.h>
48426e95d1SSimon Horman #include <linux/spinlock.h>
49426e95d1SSimon Horman #include <linux/workqueue.h>
50426e95d1SSimon Horman 
51426e95d1SSimon Horman #include "tmio_mmc.h"
52426e95d1SSimon Horman 
tmio_mmc_start_dma(struct tmio_mmc_host * host,struct mmc_data * data)53426e95d1SSimon Horman static inline void tmio_mmc_start_dma(struct tmio_mmc_host *host,
54426e95d1SSimon Horman 				      struct mmc_data *data)
55426e95d1SSimon Horman {
56426e95d1SSimon Horman 	if (host->dma_ops)
57426e95d1SSimon Horman 		host->dma_ops->start(host, data);
58426e95d1SSimon Horman }
59426e95d1SSimon Horman 
tmio_mmc_end_dma(struct tmio_mmc_host * host)606dca9a9cSYoshihiro Shimoda static inline void tmio_mmc_end_dma(struct tmio_mmc_host *host)
616dca9a9cSYoshihiro Shimoda {
626dca9a9cSYoshihiro Shimoda 	if (host->dma_ops && host->dma_ops->end)
636dca9a9cSYoshihiro Shimoda 		host->dma_ops->end(host);
646dca9a9cSYoshihiro Shimoda }
656dca9a9cSYoshihiro Shimoda 
tmio_mmc_enable_dma(struct tmio_mmc_host * host,bool enable)66426e95d1SSimon Horman static inline void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
67426e95d1SSimon Horman {
68426e95d1SSimon Horman 	if (host->dma_ops)
69426e95d1SSimon Horman 		host->dma_ops->enable(host, enable);
70426e95d1SSimon Horman }
71426e95d1SSimon Horman 
tmio_mmc_request_dma(struct tmio_mmc_host * host,struct tmio_mmc_data * pdata)72426e95d1SSimon Horman static inline void tmio_mmc_request_dma(struct tmio_mmc_host *host,
73426e95d1SSimon Horman 					struct tmio_mmc_data *pdata)
74426e95d1SSimon Horman {
75426e95d1SSimon Horman 	if (host->dma_ops) {
76426e95d1SSimon Horman 		host->dma_ops->request(host, pdata);
77426e95d1SSimon Horman 	} else {
78426e95d1SSimon Horman 		host->chan_tx = NULL;
79426e95d1SSimon Horman 		host->chan_rx = NULL;
80426e95d1SSimon Horman 	}
81426e95d1SSimon Horman }
82426e95d1SSimon Horman 
tmio_mmc_release_dma(struct tmio_mmc_host * host)83426e95d1SSimon Horman static inline void tmio_mmc_release_dma(struct tmio_mmc_host *host)
84426e95d1SSimon Horman {
85426e95d1SSimon Horman 	if (host->dma_ops)
86426e95d1SSimon Horman 		host->dma_ops->release(host);
87426e95d1SSimon Horman }
88426e95d1SSimon Horman 
tmio_mmc_abort_dma(struct tmio_mmc_host * host)89426e95d1SSimon Horman static inline void tmio_mmc_abort_dma(struct tmio_mmc_host *host)
90426e95d1SSimon Horman {
91426e95d1SSimon Horman 	if (host->dma_ops)
92426e95d1SSimon Horman 		host->dma_ops->abort(host);
93426e95d1SSimon Horman }
94426e95d1SSimon Horman 
tmio_mmc_dataend_dma(struct tmio_mmc_host * host)9592d0f925SSimon Horman static inline void tmio_mmc_dataend_dma(struct tmio_mmc_host *host)
9692d0f925SSimon Horman {
9792d0f925SSimon Horman 	if (host->dma_ops)
9892d0f925SSimon Horman 		host->dma_ops->dataend(host);
9992d0f925SSimon Horman }
10092d0f925SSimon Horman 
tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host * host,u32 i)101426e95d1SSimon Horman void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
102426e95d1SSimon Horman {
103426e95d1SSimon Horman 	host->sdcard_irq_mask &= ~(i & TMIO_MASK_IRQ);
104426e95d1SSimon Horman 	sd_ctrl_write32_as_16_and_16(host, CTL_IRQ_MASK, host->sdcard_irq_mask);
105426e95d1SSimon Horman }
1066106ecf3SSimon Horman EXPORT_SYMBOL_GPL(tmio_mmc_enable_mmc_irqs);
107426e95d1SSimon Horman 
tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host * host,u32 i)108426e95d1SSimon Horman void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
109426e95d1SSimon Horman {
110426e95d1SSimon Horman 	host->sdcard_irq_mask |= (i & TMIO_MASK_IRQ);
111426e95d1SSimon Horman 	sd_ctrl_write32_as_16_and_16(host, CTL_IRQ_MASK, host->sdcard_irq_mask);
112426e95d1SSimon Horman }
1136106ecf3SSimon Horman EXPORT_SYMBOL_GPL(tmio_mmc_disable_mmc_irqs);
114426e95d1SSimon Horman 
tmio_mmc_ack_mmc_irqs(struct tmio_mmc_host * host,u32 i)115426e95d1SSimon Horman static void tmio_mmc_ack_mmc_irqs(struct tmio_mmc_host *host, u32 i)
116426e95d1SSimon Horman {
117426e95d1SSimon Horman 	sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, ~i);
118426e95d1SSimon Horman }
119426e95d1SSimon Horman 
tmio_mmc_init_sg(struct tmio_mmc_host * host,struct mmc_data * data)120426e95d1SSimon Horman static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
121426e95d1SSimon Horman {
122426e95d1SSimon Horman 	host->sg_len = data->sg_len;
123426e95d1SSimon Horman 	host->sg_ptr = data->sg;
124426e95d1SSimon Horman 	host->sg_orig = data->sg;
125426e95d1SSimon Horman 	host->sg_off = 0;
126426e95d1SSimon Horman }
127426e95d1SSimon Horman 
tmio_mmc_next_sg(struct tmio_mmc_host * host)128426e95d1SSimon Horman static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
129426e95d1SSimon Horman {
130426e95d1SSimon Horman 	host->sg_ptr = sg_next(host->sg_ptr);
131426e95d1SSimon Horman 	host->sg_off = 0;
132426e95d1SSimon Horman 	return --host->sg_len;
133426e95d1SSimon Horman }
134426e95d1SSimon Horman 
135426e95d1SSimon Horman #define CMDREQ_TIMEOUT	5000
136426e95d1SSimon Horman 
tmio_mmc_enable_sdio_irq(struct mmc_host * mmc,int enable)137426e95d1SSimon Horman static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
138426e95d1SSimon Horman {
139426e95d1SSimon Horman 	struct tmio_mmc_host *host = mmc_priv(mmc);
140426e95d1SSimon Horman 
141426e95d1SSimon Horman 	if (enable && !host->sdio_irq_enabled) {
142426e95d1SSimon Horman 		u16 sdio_status;
143426e95d1SSimon Horman 
144426e95d1SSimon Horman 		/* Keep device active while SDIO irq is enabled */
145426e95d1SSimon Horman 		pm_runtime_get_sync(mmc_dev(mmc));
146426e95d1SSimon Horman 
147426e95d1SSimon Horman 		host->sdio_irq_enabled = true;
148f2218db8SSimon Horman 		host->sdio_irq_mask = TMIO_SDIO_MASK_ALL & ~TMIO_SDIO_STAT_IOIRQ;
149426e95d1SSimon Horman 
150426e95d1SSimon Horman 		/* Clear obsolete interrupts before enabling */
151426e95d1SSimon Horman 		sdio_status = sd_ctrl_read16(host, CTL_SDIO_STATUS) & ~TMIO_SDIO_MASK_ALL;
152426e95d1SSimon Horman 		if (host->pdata->flags & TMIO_MMC_SDIO_STATUS_SETBITS)
153426e95d1SSimon Horman 			sdio_status |= TMIO_SDIO_SETBITS_MASK;
154426e95d1SSimon Horman 		sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status);
155426e95d1SSimon Horman 
156426e95d1SSimon Horman 		sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
157426e95d1SSimon Horman 	} else if (!enable && host->sdio_irq_enabled) {
158426e95d1SSimon Horman 		host->sdio_irq_mask = TMIO_SDIO_MASK_ALL;
159426e95d1SSimon Horman 		sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
160426e95d1SSimon Horman 
161426e95d1SSimon Horman 		host->sdio_irq_enabled = false;
162426e95d1SSimon Horman 		pm_runtime_mark_last_busy(mmc_dev(mmc));
163426e95d1SSimon Horman 		pm_runtime_put_autosuspend(mmc_dev(mmc));
164426e95d1SSimon Horman 	}
165426e95d1SSimon Horman }
166426e95d1SSimon Horman 
tmio_mmc_set_bus_width(struct tmio_mmc_host * host,unsigned char bus_width)1670a446288STakeshi Saito static void tmio_mmc_set_bus_width(struct tmio_mmc_host *host,
1680a446288STakeshi Saito 				   unsigned char bus_width)
1690a446288STakeshi Saito {
1700a446288STakeshi Saito 	u16 reg = sd_ctrl_read16(host, CTL_SD_MEM_CARD_OPT)
1710a446288STakeshi Saito 				& ~(CARD_OPT_WIDTH | CARD_OPT_WIDTH8);
1720a446288STakeshi Saito 
1730a446288STakeshi Saito 	/* reg now applies to MMC_BUS_WIDTH_4 */
1740a446288STakeshi Saito 	if (bus_width == MMC_BUS_WIDTH_1)
1750a446288STakeshi Saito 		reg |= CARD_OPT_WIDTH;
1760a446288STakeshi Saito 	else if (bus_width == MMC_BUS_WIDTH_8)
1770a446288STakeshi Saito 		reg |= CARD_OPT_WIDTH8;
1780a446288STakeshi Saito 
1790a446288STakeshi Saito 	sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, reg);
1800a446288STakeshi Saito }
1810a446288STakeshi Saito 
tmio_mmc_reset(struct tmio_mmc_host * host,bool preserve)1822e586f8aSWolfram Sang static void tmio_mmc_reset(struct tmio_mmc_host *host, bool preserve)
183426e95d1SSimon Horman {
1842e586f8aSWolfram Sang 	u16 card_opt, clk_ctrl, sdif_mode;
1852e586f8aSWolfram Sang 
1862e586f8aSWolfram Sang 	if (preserve) {
1872e586f8aSWolfram Sang 		card_opt = sd_ctrl_read16(host, CTL_SD_MEM_CARD_OPT);
1882e586f8aSWolfram Sang 		clk_ctrl = sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL);
1892e586f8aSWolfram Sang 		if (host->pdata->flags & TMIO_MMC_MIN_RCAR2)
1902e586f8aSWolfram Sang 			sdif_mode = sd_ctrl_read16(host, CTL_SDIF_MODE);
1912e586f8aSWolfram Sang 	}
1922e586f8aSWolfram Sang 
193426e95d1SSimon Horman 	/* FIXME - should we set stop clock reg here */
194426e95d1SSimon Horman 	sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
1951f27ddf0SMasaharu Hayakawa 	usleep_range(10000, 11000);
196426e95d1SSimon Horman 	sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
1971f27ddf0SMasaharu Hayakawa 	usleep_range(10000, 11000);
19886beb538SWolfram Sang 
199ab0cdefeSWolfram Sang 	tmio_mmc_abort_dma(host);
200ab0cdefeSWolfram Sang 
201576146eaSWolfram Sang 	if (host->reset)
2022e586f8aSWolfram Sang 		host->reset(host, preserve);
203576146eaSWolfram Sang 
2040751d56eSWolfram Sang 	sd_ctrl_write32_as_16_and_16(host, CTL_IRQ_MASK, host->sdcard_irq_mask_all);
2050751d56eSWolfram Sang 	host->sdcard_irq_mask = host->sdcard_irq_mask_all;
2060751d56eSWolfram Sang 
20790935eb3SWolfram Sang 	if (host->native_hotplug)
20890935eb3SWolfram Sang 		tmio_mmc_enable_mmc_irqs(host,
20990935eb3SWolfram Sang 				TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT);
21090935eb3SWolfram Sang 
2110a446288STakeshi Saito 	tmio_mmc_set_bus_width(host, host->mmc->ios.bus_width);
2120a446288STakeshi Saito 
21386beb538SWolfram Sang 	if (host->pdata->flags & TMIO_MMC_SDIO_IRQ) {
21486beb538SWolfram Sang 		sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
21586beb538SWolfram Sang 		sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
21686beb538SWolfram Sang 	}
2176e5c951bSWolfram Sang 
2182e586f8aSWolfram Sang 	if (preserve) {
2192e586f8aSWolfram Sang 		sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, card_opt);
2202e586f8aSWolfram Sang 		sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk_ctrl);
2212e586f8aSWolfram Sang 		if (host->pdata->flags & TMIO_MMC_MIN_RCAR2)
2222e586f8aSWolfram Sang 			sd_ctrl_write16(host, CTL_SDIF_MODE, sdif_mode);
2232e586f8aSWolfram Sang 	}
2242e586f8aSWolfram Sang 
2256e5c951bSWolfram Sang 	if (host->mmc->card)
2266e5c951bSWolfram Sang 		mmc_retune_needed(host->mmc);
227426e95d1SSimon Horman }
228426e95d1SSimon Horman 
tmio_mmc_reset_work(struct work_struct * work)229426e95d1SSimon Horman static void tmio_mmc_reset_work(struct work_struct *work)
230426e95d1SSimon Horman {
231426e95d1SSimon Horman 	struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
232426e95d1SSimon Horman 						  delayed_reset_work.work);
233426e95d1SSimon Horman 	struct mmc_request *mrq;
234426e95d1SSimon Horman 	unsigned long flags;
235426e95d1SSimon Horman 
236426e95d1SSimon Horman 	spin_lock_irqsave(&host->lock, flags);
237426e95d1SSimon Horman 	mrq = host->mrq;
238426e95d1SSimon Horman 
239426e95d1SSimon Horman 	/*
240426e95d1SSimon Horman 	 * is request already finished? Since we use a non-blocking
241426e95d1SSimon Horman 	 * cancel_delayed_work(), it can happen, that a .set_ios() call preempts
242426e95d1SSimon Horman 	 * us, so, have to check for IS_ERR(host->mrq)
243426e95d1SSimon Horman 	 */
244f2218db8SSimon Horman 	if (IS_ERR_OR_NULL(mrq) ||
245f2218db8SSimon Horman 	    time_is_after_jiffies(host->last_req_ts +
246426e95d1SSimon Horman 				  msecs_to_jiffies(CMDREQ_TIMEOUT))) {
247426e95d1SSimon Horman 		spin_unlock_irqrestore(&host->lock, flags);
248426e95d1SSimon Horman 		return;
249426e95d1SSimon Horman 	}
250426e95d1SSimon Horman 
251426e95d1SSimon Horman 	dev_warn(&host->pdev->dev,
252426e95d1SSimon Horman 		 "timeout waiting for hardware interrupt (CMD%u)\n",
253426e95d1SSimon Horman 		 mrq->cmd->opcode);
254426e95d1SSimon Horman 
255426e95d1SSimon Horman 	if (host->data)
256426e95d1SSimon Horman 		host->data->error = -ETIMEDOUT;
257426e95d1SSimon Horman 	else if (host->cmd)
258426e95d1SSimon Horman 		host->cmd->error = -ETIMEDOUT;
259426e95d1SSimon Horman 	else
260426e95d1SSimon Horman 		mrq->cmd->error = -ETIMEDOUT;
261426e95d1SSimon Horman 
262bd169abdSWolfram Sang 	/* No new calls yet, but disallow concurrent tmio_mmc_done_work() */
263bd169abdSWolfram Sang 	host->mrq = ERR_PTR(-EBUSY);
264426e95d1SSimon Horman 	host->cmd = NULL;
265426e95d1SSimon Horman 	host->data = NULL;
266426e95d1SSimon Horman 
267426e95d1SSimon Horman 	spin_unlock_irqrestore(&host->lock, flags);
268426e95d1SSimon Horman 
2692e586f8aSWolfram Sang 	tmio_mmc_reset(host, true);
270426e95d1SSimon Horman 
271426e95d1SSimon Horman 	/* Ready for new calls */
272426e95d1SSimon Horman 	host->mrq = NULL;
273426e95d1SSimon Horman 	mmc_request_done(host->mmc, mrq);
274426e95d1SSimon Horman }
275426e95d1SSimon Horman 
276426e95d1SSimon Horman /* These are the bitmasks the tmio chip requires to implement the MMC response
277426e95d1SSimon Horman  * types. Note that R1 and R6 are the same in this scheme. */
278426e95d1SSimon Horman #define APP_CMD        0x0040
279426e95d1SSimon Horman #define RESP_NONE      0x0300
280426e95d1SSimon Horman #define RESP_R1        0x0400
281426e95d1SSimon Horman #define RESP_R1B       0x0500
282426e95d1SSimon Horman #define RESP_R2        0x0600
283426e95d1SSimon Horman #define RESP_R3        0x0700
284426e95d1SSimon Horman #define DATA_PRESENT   0x0800
285426e95d1SSimon Horman #define TRANSFER_READ  0x1000
286426e95d1SSimon Horman #define TRANSFER_MULTI 0x2000
287426e95d1SSimon Horman #define SECURITY_CMD   0x4000
288426e95d1SSimon Horman #define NO_CMD12_ISSUE 0x4000 /* TMIO_MMC_HAVE_CMD12_CTRL */
289426e95d1SSimon Horman 
tmio_mmc_start_command(struct tmio_mmc_host * host,struct mmc_command * cmd)290f2218db8SSimon Horman static int tmio_mmc_start_command(struct tmio_mmc_host *host,
291f2218db8SSimon Horman 				  struct mmc_command *cmd)
292426e95d1SSimon Horman {
293426e95d1SSimon Horman 	struct mmc_data *data = host->data;
294426e95d1SSimon Horman 	int c = cmd->opcode;
295426e95d1SSimon Horman 
296426e95d1SSimon Horman 	switch (mmc_resp_type(cmd)) {
297426e95d1SSimon Horman 	case MMC_RSP_NONE: c |= RESP_NONE; break;
298426e95d1SSimon Horman 	case MMC_RSP_R1:
299426e95d1SSimon Horman 	case MMC_RSP_R1_NO_CRC:
300426e95d1SSimon Horman 			   c |= RESP_R1;   break;
301426e95d1SSimon Horman 	case MMC_RSP_R1B:  c |= RESP_R1B;  break;
302426e95d1SSimon Horman 	case MMC_RSP_R2:   c |= RESP_R2;   break;
303426e95d1SSimon Horman 	case MMC_RSP_R3:   c |= RESP_R3;   break;
304426e95d1SSimon Horman 	default:
305426e95d1SSimon Horman 		pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
306426e95d1SSimon Horman 		return -EINVAL;
307426e95d1SSimon Horman 	}
308426e95d1SSimon Horman 
309426e95d1SSimon Horman 	host->cmd = cmd;
310426e95d1SSimon Horman 
311426e95d1SSimon Horman /* FIXME - this seems to be ok commented out but the spec suggest this bit
312426e95d1SSimon Horman  *         should be set when issuing app commands.
313426e95d1SSimon Horman  *	if(cmd->flags & MMC_FLAG_ACMD)
314426e95d1SSimon Horman  *		c |= APP_CMD;
315426e95d1SSimon Horman  */
316426e95d1SSimon Horman 	if (data) {
317426e95d1SSimon Horman 		c |= DATA_PRESENT;
318426e95d1SSimon Horman 		if (data->blocks > 1) {
319426e95d1SSimon Horman 			sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, TMIO_STOP_SEC);
320426e95d1SSimon Horman 			c |= TRANSFER_MULTI;
321426e95d1SSimon Horman 
322426e95d1SSimon Horman 			/*
323f2218db8SSimon Horman 			 * Disable auto CMD12 at IO_RW_EXTENDED and
324f2218db8SSimon Horman 			 * SET_BLOCK_COUNT when doing multiple block transfer
325426e95d1SSimon Horman 			 */
326426e95d1SSimon Horman 			if ((host->pdata->flags & TMIO_MMC_HAVE_CMD12_CTRL) &&
3278b22c3c1SWolfram Sang 			    (cmd->opcode == SD_IO_RW_EXTENDED || host->mrq->sbc))
328426e95d1SSimon Horman 				c |= NO_CMD12_ISSUE;
329426e95d1SSimon Horman 		}
330426e95d1SSimon Horman 		if (data->flags & MMC_DATA_READ)
331426e95d1SSimon Horman 			c |= TRANSFER_READ;
332426e95d1SSimon Horman 	}
333426e95d1SSimon Horman 
334e401bfdaSMasahiro Yamada 	tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_CMD);
335426e95d1SSimon Horman 
336426e95d1SSimon Horman 	/* Fire off the command */
337426e95d1SSimon Horman 	sd_ctrl_write32_as_16_and_16(host, CTL_ARG_REG, cmd->arg);
338426e95d1SSimon Horman 	sd_ctrl_write16(host, CTL_SD_CMD, c);
339426e95d1SSimon Horman 
340426e95d1SSimon Horman 	return 0;
341426e95d1SSimon Horman }
342426e95d1SSimon Horman 
tmio_mmc_transfer_data(struct tmio_mmc_host * host,unsigned short * buf,unsigned int count)343426e95d1SSimon Horman static void tmio_mmc_transfer_data(struct tmio_mmc_host *host,
344426e95d1SSimon Horman 				   unsigned short *buf,
345426e95d1SSimon Horman 				   unsigned int count)
346426e95d1SSimon Horman {
347426e95d1SSimon Horman 	int is_read = host->data->flags & MMC_DATA_READ;
348426e95d1SSimon Horman 	u8  *buf8;
349426e95d1SSimon Horman 
350426e95d1SSimon Horman 	/*
351426e95d1SSimon Horman 	 * Transfer the data
352426e95d1SSimon Horman 	 */
353426e95d1SSimon Horman 	if (host->pdata->flags & TMIO_MMC_32BIT_DATA_PORT) {
3549c284c41SChris Brandt 		u32 data = 0;
3559c284c41SChris Brandt 		u32 *buf32 = (u32 *)buf;
356426e95d1SSimon Horman 
357426e95d1SSimon Horman 		if (is_read)
3589c284c41SChris Brandt 			sd_ctrl_read32_rep(host, CTL_SD_DATA_PORT, buf32,
359426e95d1SSimon Horman 					   count >> 2);
360426e95d1SSimon Horman 		else
3619c284c41SChris Brandt 			sd_ctrl_write32_rep(host, CTL_SD_DATA_PORT, buf32,
362426e95d1SSimon Horman 					    count >> 2);
363426e95d1SSimon Horman 
364426e95d1SSimon Horman 		/* if count was multiple of 4 */
365426e95d1SSimon Horman 		if (!(count & 0x3))
366426e95d1SSimon Horman 			return;
367426e95d1SSimon Horman 
3689c284c41SChris Brandt 		buf32 += count >> 2;
369426e95d1SSimon Horman 		count %= 4;
370426e95d1SSimon Horman 
371426e95d1SSimon Horman 		if (is_read) {
3729c284c41SChris Brandt 			sd_ctrl_read32_rep(host, CTL_SD_DATA_PORT, &data, 1);
3739c284c41SChris Brandt 			memcpy(buf32, &data, count);
374426e95d1SSimon Horman 		} else {
3759c284c41SChris Brandt 			memcpy(&data, buf32, count);
3769c284c41SChris Brandt 			sd_ctrl_write32_rep(host, CTL_SD_DATA_PORT, &data, 1);
377426e95d1SSimon Horman 		}
378426e95d1SSimon Horman 
379426e95d1SSimon Horman 		return;
380426e95d1SSimon Horman 	}
381426e95d1SSimon Horman 
382426e95d1SSimon Horman 	if (is_read)
383426e95d1SSimon Horman 		sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
384426e95d1SSimon Horman 	else
385426e95d1SSimon Horman 		sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
386426e95d1SSimon Horman 
387426e95d1SSimon Horman 	/* if count was even number */
388426e95d1SSimon Horman 	if (!(count & 0x1))
389426e95d1SSimon Horman 		return;
390426e95d1SSimon Horman 
391426e95d1SSimon Horman 	/* if count was odd number */
392426e95d1SSimon Horman 	buf8 = (u8 *)(buf + (count >> 1));
393426e95d1SSimon Horman 
394426e95d1SSimon Horman 	/*
395426e95d1SSimon Horman 	 * FIXME
396426e95d1SSimon Horman 	 *
397426e95d1SSimon Horman 	 * driver and this function are assuming that
398426e95d1SSimon Horman 	 * it is used as little endian
399426e95d1SSimon Horman 	 */
400426e95d1SSimon Horman 	if (is_read)
401426e95d1SSimon Horman 		*buf8 = sd_ctrl_read16(host, CTL_SD_DATA_PORT) & 0xff;
402426e95d1SSimon Horman 	else
403426e95d1SSimon Horman 		sd_ctrl_write16(host, CTL_SD_DATA_PORT, *buf8);
404426e95d1SSimon Horman }
405426e95d1SSimon Horman 
406426e95d1SSimon Horman /*
407426e95d1SSimon Horman  * This chip always returns (at least?) as much data as you ask for.
408426e95d1SSimon Horman  * I'm unsure what happens if you ask for less than a block. This should be
409426e95d1SSimon Horman  * looked into to ensure that a funny length read doesn't hose the controller.
410426e95d1SSimon Horman  */
tmio_mmc_pio_irq(struct tmio_mmc_host * host)411426e95d1SSimon Horman static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
412426e95d1SSimon Horman {
413426e95d1SSimon Horman 	struct mmc_data *data = host->data;
414426e95d1SSimon Horman 	void *sg_virt;
415426e95d1SSimon Horman 	unsigned short *buf;
416426e95d1SSimon Horman 	unsigned int count;
417426e95d1SSimon Horman 
418d3dd5db0SMasahiro Yamada 	if (host->dma_on) {
419426e95d1SSimon Horman 		pr_err("PIO IRQ in DMA mode!\n");
420426e95d1SSimon Horman 		return;
421426e95d1SSimon Horman 	} else if (!data) {
422426e95d1SSimon Horman 		pr_debug("Spurious PIO IRQ\n");
423426e95d1SSimon Horman 		return;
424426e95d1SSimon Horman 	}
425426e95d1SSimon Horman 
4268840e1c1SWolfram Sang 	sg_virt = kmap_local_page(sg_page(host->sg_ptr));
4278840e1c1SWolfram Sang 	buf = (unsigned short *)(sg_virt + host->sg_ptr->offset + host->sg_off);
428426e95d1SSimon Horman 
429426e95d1SSimon Horman 	count = host->sg_ptr->length - host->sg_off;
430426e95d1SSimon Horman 	if (count > data->blksz)
431426e95d1SSimon Horman 		count = data->blksz;
432426e95d1SSimon Horman 
433426e95d1SSimon Horman 	pr_debug("count: %08x offset: %08x flags %08x\n",
434426e95d1SSimon Horman 		 count, host->sg_off, data->flags);
435426e95d1SSimon Horman 
436426e95d1SSimon Horman 	/* Transfer the data */
437426e95d1SSimon Horman 	tmio_mmc_transfer_data(host, buf, count);
438426e95d1SSimon Horman 
439426e95d1SSimon Horman 	host->sg_off += count;
440426e95d1SSimon Horman 
4418840e1c1SWolfram Sang 	kunmap_local(sg_virt);
442426e95d1SSimon Horman 
443426e95d1SSimon Horman 	if (host->sg_off == host->sg_ptr->length)
444426e95d1SSimon Horman 		tmio_mmc_next_sg(host);
445426e95d1SSimon Horman }
446426e95d1SSimon Horman 
tmio_mmc_check_bounce_buffer(struct tmio_mmc_host * host)447426e95d1SSimon Horman static void tmio_mmc_check_bounce_buffer(struct tmio_mmc_host *host)
448426e95d1SSimon Horman {
449426e95d1SSimon Horman 	if (host->sg_ptr == &host->bounce_sg) {
4508840e1c1SWolfram Sang 		void *sg_virt = kmap_local_page(sg_page(host->sg_orig));
451f2218db8SSimon Horman 
4528840e1c1SWolfram Sang 		memcpy(sg_virt + host->sg_orig->offset, host->bounce_buf,
4538840e1c1SWolfram Sang 		       host->bounce_sg.length);
4548840e1c1SWolfram Sang 		kunmap_local(sg_virt);
455426e95d1SSimon Horman 	}
456426e95d1SSimon Horman }
457426e95d1SSimon Horman 
458426e95d1SSimon Horman /* needs to be called with host->lock held */
tmio_mmc_do_data_irq(struct tmio_mmc_host * host)459426e95d1SSimon Horman void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
460426e95d1SSimon Horman {
461426e95d1SSimon Horman 	struct mmc_data *data = host->data;
462426e95d1SSimon Horman 	struct mmc_command *stop;
463426e95d1SSimon Horman 
464426e95d1SSimon Horman 	host->data = NULL;
465426e95d1SSimon Horman 
466426e95d1SSimon Horman 	if (!data) {
467426e95d1SSimon Horman 		dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
468426e95d1SSimon Horman 		return;
469426e95d1SSimon Horman 	}
470426e95d1SSimon Horman 	stop = data->stop;
471426e95d1SSimon Horman 
472426e95d1SSimon Horman 	/* FIXME - return correct transfer count on errors */
473426e95d1SSimon Horman 	if (!data->error)
474426e95d1SSimon Horman 		data->bytes_xfered = data->blocks * data->blksz;
475426e95d1SSimon Horman 	else
476426e95d1SSimon Horman 		data->bytes_xfered = 0;
477426e95d1SSimon Horman 
478426e95d1SSimon Horman 	pr_debug("Completed data request\n");
479426e95d1SSimon Horman 
480426e95d1SSimon Horman 	/*
481426e95d1SSimon Horman 	 * FIXME: other drivers allow an optional stop command of any given type
482426e95d1SSimon Horman 	 *        which we dont do, as the chip can auto generate them.
483426e95d1SSimon Horman 	 *        Perhaps we can be smarter about when to use auto CMD12 and
484426e95d1SSimon Horman 	 *        only issue the auto request when we know this is the desired
485426e95d1SSimon Horman 	 *        stop command, allowing fallback to the stop command the
486426e95d1SSimon Horman 	 *        upper layers expect. For now, we do what works.
487426e95d1SSimon Horman 	 */
488426e95d1SSimon Horman 
489426e95d1SSimon Horman 	if (data->flags & MMC_DATA_READ) {
490d3dd5db0SMasahiro Yamada 		if (host->dma_on)
491426e95d1SSimon Horman 			tmio_mmc_check_bounce_buffer(host);
492426e95d1SSimon Horman 		dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
493426e95d1SSimon Horman 			host->mrq);
494426e95d1SSimon Horman 	} else {
495426e95d1SSimon Horman 		dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
496426e95d1SSimon Horman 			host->mrq);
497426e95d1SSimon Horman 	}
498426e95d1SSimon Horman 
4998b22c3c1SWolfram Sang 	if (stop && !host->mrq->sbc) {
500426e95d1SSimon Horman 		if (stop->opcode != MMC_STOP_TRANSMISSION || stop->arg)
501426e95d1SSimon Horman 			dev_err(&host->pdev->dev, "unsupported stop: CMD%u,0x%x. We did CMD12,0\n",
502426e95d1SSimon Horman 				stop->opcode, stop->arg);
503426e95d1SSimon Horman 
504426e95d1SSimon Horman 		/* fill in response from auto CMD12 */
505426e95d1SSimon Horman 		stop->resp[0] = sd_ctrl_read16_and_16_as_32(host, CTL_RESPONSE);
506426e95d1SSimon Horman 
507426e95d1SSimon Horman 		sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0);
508426e95d1SSimon Horman 	}
509426e95d1SSimon Horman 
510426e95d1SSimon Horman 	schedule_work(&host->done);
511426e95d1SSimon Horman }
5126106ecf3SSimon Horman EXPORT_SYMBOL_GPL(tmio_mmc_do_data_irq);
513426e95d1SSimon Horman 
tmio_mmc_data_irq(struct tmio_mmc_host * host,unsigned int stat)514426e95d1SSimon Horman static void tmio_mmc_data_irq(struct tmio_mmc_host *host, unsigned int stat)
515426e95d1SSimon Horman {
516426e95d1SSimon Horman 	struct mmc_data *data;
517f2218db8SSimon Horman 
518426e95d1SSimon Horman 	spin_lock(&host->lock);
519426e95d1SSimon Horman 	data = host->data;
520426e95d1SSimon Horman 
521426e95d1SSimon Horman 	if (!data)
522426e95d1SSimon Horman 		goto out;
523426e95d1SSimon Horman 
52435cdcd12SMasaharu Hayakawa 	if (stat & TMIO_STAT_DATATIMEOUT)
52535cdcd12SMasaharu Hayakawa 		data->error = -ETIMEDOUT;
52635cdcd12SMasaharu Hayakawa 	else if (stat & TMIO_STAT_CRCFAIL || stat & TMIO_STAT_STOPBIT_ERR ||
527426e95d1SSimon Horman 		 stat & TMIO_STAT_TXUNDERRUN)
528426e95d1SSimon Horman 		data->error = -EILSEQ;
529d3dd5db0SMasahiro Yamada 	if (host->dma_on && (data->flags & MMC_DATA_WRITE)) {
530426e95d1SSimon Horman 		u32 status = sd_ctrl_read16_and_16_as_32(host, CTL_STATUS);
531426e95d1SSimon Horman 		bool done = false;
532426e95d1SSimon Horman 
533426e95d1SSimon Horman 		/*
534426e95d1SSimon Horman 		 * Has all data been written out yet? Testing on SuperH showed,
535426e95d1SSimon Horman 		 * that in most cases the first interrupt comes already with the
536426e95d1SSimon Horman 		 * BUSY status bit clear, but on some operations, like mount or
537426e95d1SSimon Horman 		 * in the beginning of a write / sync / umount, there is one
538426e95d1SSimon Horman 		 * DATAEND interrupt with the BUSY bit set, in this cases
539426e95d1SSimon Horman 		 * waiting for one more interrupt fixes the problem.
540426e95d1SSimon Horman 		 */
541426e95d1SSimon Horman 		if (host->pdata->flags & TMIO_MMC_HAS_IDLE_WAIT) {
542426e95d1SSimon Horman 			if (status & TMIO_STAT_SCLKDIVEN)
543426e95d1SSimon Horman 				done = true;
544426e95d1SSimon Horman 		} else {
545426e95d1SSimon Horman 			if (!(status & TMIO_STAT_CMD_BUSY))
546426e95d1SSimon Horman 				done = true;
547426e95d1SSimon Horman 		}
548426e95d1SSimon Horman 
549426e95d1SSimon Horman 		if (done) {
550426e95d1SSimon Horman 			tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
55192d0f925SSimon Horman 			tmio_mmc_dataend_dma(host);
552426e95d1SSimon Horman 		}
553d3dd5db0SMasahiro Yamada 	} else if (host->dma_on && (data->flags & MMC_DATA_READ)) {
554426e95d1SSimon Horman 		tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
55592d0f925SSimon Horman 		tmio_mmc_dataend_dma(host);
556426e95d1SSimon Horman 	} else {
557426e95d1SSimon Horman 		tmio_mmc_do_data_irq(host);
558426e95d1SSimon Horman 		tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_READOP | TMIO_MASK_WRITEOP);
559426e95d1SSimon Horman 	}
560426e95d1SSimon Horman out:
561426e95d1SSimon Horman 	spin_unlock(&host->lock);
562426e95d1SSimon Horman }
563426e95d1SSimon Horman 
tmio_mmc_cmd_irq(struct tmio_mmc_host * host,unsigned int stat)564f2218db8SSimon Horman static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host, unsigned int stat)
565426e95d1SSimon Horman {
566426e95d1SSimon Horman 	struct mmc_command *cmd = host->cmd;
567426e95d1SSimon Horman 	int i, addr;
568426e95d1SSimon Horman 
569426e95d1SSimon Horman 	spin_lock(&host->lock);
570426e95d1SSimon Horman 
571426e95d1SSimon Horman 	if (!host->cmd) {
572426e95d1SSimon Horman 		pr_debug("Spurious CMD irq\n");
573426e95d1SSimon Horman 		goto out;
574426e95d1SSimon Horman 	}
575426e95d1SSimon Horman 
576426e95d1SSimon Horman 	/* This controller is sicker than the PXA one. Not only do we need to
577426e95d1SSimon Horman 	 * drop the top 8 bits of the first response word, we also need to
578426e95d1SSimon Horman 	 * modify the order of the response for short response command types.
579426e95d1SSimon Horman 	 */
580426e95d1SSimon Horman 
581426e95d1SSimon Horman 	for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
582426e95d1SSimon Horman 		cmd->resp[i] = sd_ctrl_read16_and_16_as_32(host, addr);
583426e95d1SSimon Horman 
584426e95d1SSimon Horman 	if (cmd->flags &  MMC_RSP_136) {
585426e95d1SSimon Horman 		cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
586426e95d1SSimon Horman 		cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
587426e95d1SSimon Horman 		cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
588426e95d1SSimon Horman 		cmd->resp[3] <<= 8;
589426e95d1SSimon Horman 	} else if (cmd->flags & MMC_RSP_R3) {
590426e95d1SSimon Horman 		cmd->resp[0] = cmd->resp[3];
591426e95d1SSimon Horman 	}
592426e95d1SSimon Horman 
593426e95d1SSimon Horman 	if (stat & TMIO_STAT_CMDTIMEOUT)
594426e95d1SSimon Horman 		cmd->error = -ETIMEDOUT;
595426e95d1SSimon Horman 	else if ((stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC) ||
596426e95d1SSimon Horman 		 stat & TMIO_STAT_STOPBIT_ERR ||
597426e95d1SSimon Horman 		 stat & TMIO_STAT_CMD_IDX_ERR)
598426e95d1SSimon Horman 		cmd->error = -EILSEQ;
599426e95d1SSimon Horman 
600426e95d1SSimon Horman 	/* If there is data to handle we enable data IRQs here, and
601426e95d1SSimon Horman 	 * we will ultimatley finish the request in the data_end handler.
602426e95d1SSimon Horman 	 * If theres no data or we encountered an error, finish now.
603426e95d1SSimon Horman 	 */
604426e95d1SSimon Horman 	if (host->data && (!cmd->error || cmd->error == -EILSEQ)) {
605426e95d1SSimon Horman 		if (host->data->flags & MMC_DATA_READ) {
606d3dd5db0SMasahiro Yamada 			if (!host->dma_on) {
607426e95d1SSimon Horman 				tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_READOP);
608426e95d1SSimon Horman 			} else {
609b12a7a28SMasahiro Yamada 				tmio_mmc_disable_mmc_irqs(host,
610b12a7a28SMasahiro Yamada 							  TMIO_MASK_READOP);
611426e95d1SSimon Horman 				tasklet_schedule(&host->dma_issue);
612426e95d1SSimon Horman 			}
613426e95d1SSimon Horman 		} else {
614d3dd5db0SMasahiro Yamada 			if (!host->dma_on) {
615b12a7a28SMasahiro Yamada 				tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
616b12a7a28SMasahiro Yamada 			} else {
617b12a7a28SMasahiro Yamada 				tmio_mmc_disable_mmc_irqs(host,
618b12a7a28SMasahiro Yamada 							  TMIO_MASK_WRITEOP);
619b12a7a28SMasahiro Yamada 				tasklet_schedule(&host->dma_issue);
620b12a7a28SMasahiro Yamada 			}
621b12a7a28SMasahiro Yamada 		}
622b12a7a28SMasahiro Yamada 	} else {
623426e95d1SSimon Horman 		schedule_work(&host->done);
624426e95d1SSimon Horman 	}
625426e95d1SSimon Horman 
626426e95d1SSimon Horman out:
627426e95d1SSimon Horman 	spin_unlock(&host->lock);
628426e95d1SSimon Horman }
629426e95d1SSimon Horman 
__tmio_mmc_card_detect_irq(struct tmio_mmc_host * host,int ireg,int status)630426e95d1SSimon Horman static bool __tmio_mmc_card_detect_irq(struct tmio_mmc_host *host,
631426e95d1SSimon Horman 				       int ireg, int status)
632426e95d1SSimon Horman {
633426e95d1SSimon Horman 	struct mmc_host *mmc = host->mmc;
634426e95d1SSimon Horman 
635426e95d1SSimon Horman 	/* Card insert / remove attempts */
636426e95d1SSimon Horman 	if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
637426e95d1SSimon Horman 		tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
638426e95d1SSimon Horman 			TMIO_STAT_CARD_REMOVE);
639426e95d1SSimon Horman 		if ((((ireg & TMIO_STAT_CARD_REMOVE) && mmc->card) ||
640426e95d1SSimon Horman 		     ((ireg & TMIO_STAT_CARD_INSERT) && !mmc->card)) &&
641426e95d1SSimon Horman 		    !work_pending(&mmc->detect.work))
642426e95d1SSimon Horman 			mmc_detect_change(host->mmc, msecs_to_jiffies(100));
643426e95d1SSimon Horman 		return true;
644426e95d1SSimon Horman 	}
645426e95d1SSimon Horman 
646426e95d1SSimon Horman 	return false;
647426e95d1SSimon Horman }
648426e95d1SSimon Horman 
__tmio_mmc_sdcard_irq(struct tmio_mmc_host * host,int ireg,int status)649f2218db8SSimon Horman static bool __tmio_mmc_sdcard_irq(struct tmio_mmc_host *host, int ireg,
650f2218db8SSimon Horman 				  int status)
651426e95d1SSimon Horman {
652426e95d1SSimon Horman 	/* Command completion */
653426e95d1SSimon Horman 	if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
654f2218db8SSimon Horman 		tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CMDRESPEND |
655426e95d1SSimon Horman 				      TMIO_STAT_CMDTIMEOUT);
656426e95d1SSimon Horman 		tmio_mmc_cmd_irq(host, status);
657426e95d1SSimon Horman 		return true;
658426e95d1SSimon Horman 	}
659426e95d1SSimon Horman 
660426e95d1SSimon Horman 	/* Data transfer */
661426e95d1SSimon Horman 	if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
662426e95d1SSimon Horman 		tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
663426e95d1SSimon Horman 		tmio_mmc_pio_irq(host);
664426e95d1SSimon Horman 		return true;
665426e95d1SSimon Horman 	}
666426e95d1SSimon Horman 
667426e95d1SSimon Horman 	/* Data transfer completion */
668426e95d1SSimon Horman 	if (ireg & TMIO_STAT_DATAEND) {
669426e95d1SSimon Horman 		tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_DATAEND);
670426e95d1SSimon Horman 		tmio_mmc_data_irq(host, status);
671426e95d1SSimon Horman 		return true;
672426e95d1SSimon Horman 	}
673426e95d1SSimon Horman 
674af728d7aSWolfram Sang 	if (host->dma_ops && host->dma_ops->dma_irq && host->dma_ops->dma_irq(host))
675af728d7aSWolfram Sang 		return true;
676af728d7aSWolfram Sang 
677426e95d1SSimon Horman 	return false;
678426e95d1SSimon Horman }
679426e95d1SSimon Horman 
__tmio_mmc_sdio_irq(struct tmio_mmc_host * host)6805c27ff5dSSergei Shtylyov static bool __tmio_mmc_sdio_irq(struct tmio_mmc_host *host)
681426e95d1SSimon Horman {
682426e95d1SSimon Horman 	struct mmc_host *mmc = host->mmc;
683426e95d1SSimon Horman 	struct tmio_mmc_data *pdata = host->pdata;
684426e95d1SSimon Horman 	unsigned int ireg, status;
685426e95d1SSimon Horman 	unsigned int sdio_status;
686426e95d1SSimon Horman 
687426e95d1SSimon Horman 	if (!(pdata->flags & TMIO_MMC_SDIO_IRQ))
6885c27ff5dSSergei Shtylyov 		return false;
689426e95d1SSimon Horman 
690426e95d1SSimon Horman 	status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
691426e95d1SSimon Horman 	ireg = status & TMIO_SDIO_MASK_ALL & ~host->sdio_irq_mask;
692426e95d1SSimon Horman 
693426e95d1SSimon Horman 	sdio_status = status & ~TMIO_SDIO_MASK_ALL;
694426e95d1SSimon Horman 	if (pdata->flags & TMIO_MMC_SDIO_STATUS_SETBITS)
695426e95d1SSimon Horman 		sdio_status |= TMIO_SDIO_SETBITS_MASK;
696426e95d1SSimon Horman 
697426e95d1SSimon Horman 	sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status);
698426e95d1SSimon Horman 
699426e95d1SSimon Horman 	if (mmc->caps & MMC_CAP_SDIO_IRQ && ireg & TMIO_SDIO_STAT_IOIRQ)
700426e95d1SSimon Horman 		mmc_signal_sdio_irq(mmc);
7015c27ff5dSSergei Shtylyov 
7025c27ff5dSSergei Shtylyov 	return ireg;
703426e95d1SSimon Horman }
704426e95d1SSimon Horman 
tmio_mmc_irq(int irq,void * devid)705426e95d1SSimon Horman irqreturn_t tmio_mmc_irq(int irq, void *devid)
706426e95d1SSimon Horman {
707426e95d1SSimon Horman 	struct tmio_mmc_host *host = devid;
708426e95d1SSimon Horman 	unsigned int ireg, status;
709426e95d1SSimon Horman 
710426e95d1SSimon Horman 	status = sd_ctrl_read16_and_16_as_32(host, CTL_STATUS);
711426e95d1SSimon Horman 	ireg = status & TMIO_MASK_IRQ & ~host->sdcard_irq_mask;
712426e95d1SSimon Horman 
713426e95d1SSimon Horman 	/* Clear the status except the interrupt status */
714426e95d1SSimon Horman 	sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, TMIO_MASK_IRQ);
715426e95d1SSimon Horman 
716426e95d1SSimon Horman 	if (__tmio_mmc_card_detect_irq(host, ireg, status))
717426e95d1SSimon Horman 		return IRQ_HANDLED;
718426e95d1SSimon Horman 	if (__tmio_mmc_sdcard_irq(host, ireg, status))
719426e95d1SSimon Horman 		return IRQ_HANDLED;
720426e95d1SSimon Horman 
7215c27ff5dSSergei Shtylyov 	if (__tmio_mmc_sdio_irq(host))
722426e95d1SSimon Horman 		return IRQ_HANDLED;
7235c27ff5dSSergei Shtylyov 
7245c27ff5dSSergei Shtylyov 	return IRQ_NONE;
725426e95d1SSimon Horman }
7266106ecf3SSimon Horman EXPORT_SYMBOL_GPL(tmio_mmc_irq);
727426e95d1SSimon Horman 
tmio_mmc_start_data(struct tmio_mmc_host * host,struct mmc_data * data)728426e95d1SSimon Horman static int tmio_mmc_start_data(struct tmio_mmc_host *host,
729426e95d1SSimon Horman 			       struct mmc_data *data)
730426e95d1SSimon Horman {
731426e95d1SSimon Horman 	struct tmio_mmc_data *pdata = host->pdata;
732426e95d1SSimon Horman 
733426e95d1SSimon Horman 	pr_debug("setup data transfer: blocksize %08x  nr_blocks %d\n",
734426e95d1SSimon Horman 		 data->blksz, data->blocks);
735426e95d1SSimon Horman 
736426e95d1SSimon Horman 	/* Some hardware cannot perform 2 byte requests in 4/8 bit mode */
737426e95d1SSimon Horman 	if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4 ||
738426e95d1SSimon Horman 	    host->mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
739426e95d1SSimon Horman 		int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
740426e95d1SSimon Horman 
741426e95d1SSimon Horman 		if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
742426e95d1SSimon Horman 			pr_err("%s: %d byte block unsupported in 4/8 bit mode\n",
743426e95d1SSimon Horman 			       mmc_hostname(host->mmc), data->blksz);
744426e95d1SSimon Horman 			return -EINVAL;
745426e95d1SSimon Horman 		}
746426e95d1SSimon Horman 	}
747426e95d1SSimon Horman 
748426e95d1SSimon Horman 	tmio_mmc_init_sg(host, data);
749426e95d1SSimon Horman 	host->data = data;
750d3dd5db0SMasahiro Yamada 	host->dma_on = false;
751426e95d1SSimon Horman 
752426e95d1SSimon Horman 	/* Set transfer length / blocksize */
753426e95d1SSimon Horman 	sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
7545603731aSTakeshi Saito 	if (host->mmc->max_blk_count >= SZ_64K)
7555603731aSTakeshi Saito 		sd_ctrl_write32(host, CTL_XFER_BLK_COUNT, data->blocks);
7565603731aSTakeshi Saito 	else
757426e95d1SSimon Horman 		sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
758426e95d1SSimon Horman 
759426e95d1SSimon Horman 	tmio_mmc_start_dma(host, data);
760426e95d1SSimon Horman 
761426e95d1SSimon Horman 	return 0;
762426e95d1SSimon Horman }
763426e95d1SSimon Horman 
tmio_process_mrq(struct tmio_mmc_host * host,struct mmc_request * mrq)764f2218db8SSimon Horman static void tmio_process_mrq(struct tmio_mmc_host *host,
765f2218db8SSimon Horman 			     struct mmc_request *mrq)
766426e95d1SSimon Horman {
7678b22c3c1SWolfram Sang 	struct mmc_command *cmd;
768426e95d1SSimon Horman 	int ret;
769426e95d1SSimon Horman 
7708b22c3c1SWolfram Sang 	if (mrq->sbc && host->cmd != mrq->sbc) {
7718b22c3c1SWolfram Sang 		cmd = mrq->sbc;
7728b22c3c1SWolfram Sang 	} else {
7738b22c3c1SWolfram Sang 		cmd = mrq->cmd;
774426e95d1SSimon Horman 		if (mrq->data) {
775426e95d1SSimon Horman 			ret = tmio_mmc_start_data(host, mrq->data);
776426e95d1SSimon Horman 			if (ret)
777426e95d1SSimon Horman 				goto fail;
778426e95d1SSimon Horman 		}
7798b22c3c1SWolfram Sang 	}
780426e95d1SSimon Horman 
7818b22c3c1SWolfram Sang 	ret = tmio_mmc_start_command(host, cmd);
78210c998efSWolfram Sang 	if (ret)
78310c998efSWolfram Sang 		goto fail;
78410c998efSWolfram Sang 
785426e95d1SSimon Horman 	schedule_delayed_work(&host->delayed_reset_work,
786426e95d1SSimon Horman 			      msecs_to_jiffies(CMDREQ_TIMEOUT));
787426e95d1SSimon Horman 	return;
788426e95d1SSimon Horman 
789426e95d1SSimon Horman fail:
790426e95d1SSimon Horman 	host->mrq = NULL;
791426e95d1SSimon Horman 	mrq->cmd->error = ret;
792de2a6bb9SWolfram Sang 	mmc_request_done(host->mmc, mrq);
793de2a6bb9SWolfram Sang }
794de2a6bb9SWolfram Sang 
795de2a6bb9SWolfram Sang /* Process requests from the MMC layer */
tmio_mmc_request(struct mmc_host * mmc,struct mmc_request * mrq)796de2a6bb9SWolfram Sang static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
797de2a6bb9SWolfram Sang {
798de2a6bb9SWolfram Sang 	struct tmio_mmc_host *host = mmc_priv(mmc);
799de2a6bb9SWolfram Sang 	unsigned long flags;
800de2a6bb9SWolfram Sang 
801de2a6bb9SWolfram Sang 	spin_lock_irqsave(&host->lock, flags);
802de2a6bb9SWolfram Sang 
803de2a6bb9SWolfram Sang 	if (host->mrq) {
804de2a6bb9SWolfram Sang 		pr_debug("request not null\n");
805de2a6bb9SWolfram Sang 		if (IS_ERR(host->mrq)) {
806de2a6bb9SWolfram Sang 			spin_unlock_irqrestore(&host->lock, flags);
807de2a6bb9SWolfram Sang 			mrq->cmd->error = -EAGAIN;
808426e95d1SSimon Horman 			mmc_request_done(mmc, mrq);
809de2a6bb9SWolfram Sang 			return;
810de2a6bb9SWolfram Sang 		}
811de2a6bb9SWolfram Sang 	}
812de2a6bb9SWolfram Sang 
813de2a6bb9SWolfram Sang 	host->last_req_ts = jiffies;
814de2a6bb9SWolfram Sang 	wmb();
815de2a6bb9SWolfram Sang 	host->mrq = mrq;
816de2a6bb9SWolfram Sang 
817de2a6bb9SWolfram Sang 	spin_unlock_irqrestore(&host->lock, flags);
818de2a6bb9SWolfram Sang 
819de2a6bb9SWolfram Sang 	tmio_process_mrq(host, mrq);
820426e95d1SSimon Horman }
821426e95d1SSimon Horman 
tmio_mmc_finish_request(struct tmio_mmc_host * host)822f5fdcd1dSWolfram Sang static void tmio_mmc_finish_request(struct tmio_mmc_host *host)
823f5fdcd1dSWolfram Sang {
824f5fdcd1dSWolfram Sang 	struct mmc_request *mrq;
825f5fdcd1dSWolfram Sang 	unsigned long flags;
826f5fdcd1dSWolfram Sang 
827f5fdcd1dSWolfram Sang 	spin_lock_irqsave(&host->lock, flags);
828f5fdcd1dSWolfram Sang 
8296dca9a9cSYoshihiro Shimoda 	tmio_mmc_end_dma(host);
8306dca9a9cSYoshihiro Shimoda 
831f5fdcd1dSWolfram Sang 	mrq = host->mrq;
832f5fdcd1dSWolfram Sang 	if (IS_ERR_OR_NULL(mrq)) {
833f5fdcd1dSWolfram Sang 		spin_unlock_irqrestore(&host->lock, flags);
834f5fdcd1dSWolfram Sang 		return;
835f5fdcd1dSWolfram Sang 	}
836f5fdcd1dSWolfram Sang 
8378b22c3c1SWolfram Sang 	/* If not SET_BLOCK_COUNT, clear old data */
8388b22c3c1SWolfram Sang 	if (host->cmd != mrq->sbc) {
839f5fdcd1dSWolfram Sang 		host->cmd = NULL;
840f5fdcd1dSWolfram Sang 		host->data = NULL;
8418b22c3c1SWolfram Sang 		host->mrq = NULL;
8428b22c3c1SWolfram Sang 	}
843f5fdcd1dSWolfram Sang 
844f5fdcd1dSWolfram Sang 	cancel_delayed_work(&host->delayed_reset_work);
845f5fdcd1dSWolfram Sang 
846f5fdcd1dSWolfram Sang 	spin_unlock_irqrestore(&host->lock, flags);
847f5fdcd1dSWolfram Sang 
84840e49564SMasaharu Hayakawa 	if (mrq->cmd->error || (mrq->data && mrq->data->error)) {
84940e49564SMasaharu Hayakawa 		tmio_mmc_ack_mmc_irqs(host, TMIO_MASK_IRQ); /* Clear all */
850f5fdcd1dSWolfram Sang 		tmio_mmc_abort_dma(host);
85140e49564SMasaharu Hayakawa 	}
852f5fdcd1dSWolfram Sang 
85364982b9fSWolfram Sang 	/* Error means retune, but executed command was still successful */
854ed2fab9aSYoshihiro Shimoda 	if (host->check_retune && host->check_retune(host, mrq))
85551b72656STakeshi Saito 		mmc_retune_needed(host->mmc);
856f5fdcd1dSWolfram Sang 
8578b22c3c1SWolfram Sang 	/* If SET_BLOCK_COUNT, continue with main command */
858fc167dafSMasaharu Hayakawa 	if (host->mrq && !mrq->cmd->error) {
8598b22c3c1SWolfram Sang 		tmio_process_mrq(host, mrq);
8608b22c3c1SWolfram Sang 		return;
8618b22c3c1SWolfram Sang 	}
8628b22c3c1SWolfram Sang 
863354f47b6SWolfram Sang 	if (host->fixup_request)
864354f47b6SWolfram Sang 		host->fixup_request(host, mrq);
865354f47b6SWolfram Sang 
866f5fdcd1dSWolfram Sang 	mmc_request_done(host->mmc, mrq);
867f5fdcd1dSWolfram Sang }
868f5fdcd1dSWolfram Sang 
tmio_mmc_done_work(struct work_struct * work)869f5fdcd1dSWolfram Sang static void tmio_mmc_done_work(struct work_struct *work)
870f5fdcd1dSWolfram Sang {
871f5fdcd1dSWolfram Sang 	struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
872f5fdcd1dSWolfram Sang 						  done);
873f5fdcd1dSWolfram Sang 	tmio_mmc_finish_request(host);
874f5fdcd1dSWolfram Sang }
875f5fdcd1dSWolfram Sang 
tmio_mmc_power_on(struct tmio_mmc_host * host,unsigned short vdd)876426e95d1SSimon Horman static void tmio_mmc_power_on(struct tmio_mmc_host *host, unsigned short vdd)
877426e95d1SSimon Horman {
878426e95d1SSimon Horman 	struct mmc_host *mmc = host->mmc;
879426e95d1SSimon Horman 	int ret = 0;
880426e95d1SSimon Horman 
881426e95d1SSimon Horman 	/* .set_ios() is returning void, so, no chance to report an error */
882426e95d1SSimon Horman 
883426e95d1SSimon Horman 	if (host->set_pwr)
884426e95d1SSimon Horman 		host->set_pwr(host->pdev, 1);
885426e95d1SSimon Horman 
886426e95d1SSimon Horman 	if (!IS_ERR(mmc->supply.vmmc)) {
887426e95d1SSimon Horman 		ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
888426e95d1SSimon Horman 		/*
889426e95d1SSimon Horman 		 * Attention: empiric value. With a b43 WiFi SDIO card this
890426e95d1SSimon Horman 		 * delay proved necessary for reliable card-insertion probing.
891426e95d1SSimon Horman 		 * 100us were not enough. Is this the same 140us delay, as in
892426e95d1SSimon Horman 		 * tmio_mmc_set_ios()?
893426e95d1SSimon Horman 		 */
894754febccSWolfram Sang 		usleep_range(200, 300);
895426e95d1SSimon Horman 	}
896426e95d1SSimon Horman 	/*
897426e95d1SSimon Horman 	 * It seems, VccQ should be switched on after Vcc, this is also what the
898426e95d1SSimon Horman 	 * omap_hsmmc.c driver does.
899426e95d1SSimon Horman 	 */
900426e95d1SSimon Horman 	if (!IS_ERR(mmc->supply.vqmmc) && !ret) {
901426e95d1SSimon Horman 		ret = regulator_enable(mmc->supply.vqmmc);
902754febccSWolfram Sang 		usleep_range(200, 300);
903426e95d1SSimon Horman 	}
904426e95d1SSimon Horman 
905426e95d1SSimon Horman 	if (ret < 0)
906426e95d1SSimon Horman 		dev_dbg(&host->pdev->dev, "Regulators failed to power up: %d\n",
907426e95d1SSimon Horman 			ret);
908426e95d1SSimon Horman }
909426e95d1SSimon Horman 
tmio_mmc_power_off(struct tmio_mmc_host * host)910426e95d1SSimon Horman static void tmio_mmc_power_off(struct tmio_mmc_host *host)
911426e95d1SSimon Horman {
912426e95d1SSimon Horman 	struct mmc_host *mmc = host->mmc;
913426e95d1SSimon Horman 
914426e95d1SSimon Horman 	if (!IS_ERR(mmc->supply.vqmmc))
915426e95d1SSimon Horman 		regulator_disable(mmc->supply.vqmmc);
916426e95d1SSimon Horman 
917426e95d1SSimon Horman 	if (!IS_ERR(mmc->supply.vmmc))
918426e95d1SSimon Horman 		mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
919426e95d1SSimon Horman 
920426e95d1SSimon Horman 	if (host->set_pwr)
921426e95d1SSimon Horman 		host->set_pwr(host->pdev, 0);
922426e95d1SSimon Horman }
923426e95d1SSimon Horman 
tmio_mmc_get_timeout_cycles(struct tmio_mmc_host * host)92458959f89SWolfram Sang static unsigned int tmio_mmc_get_timeout_cycles(struct tmio_mmc_host *host)
92530ae3e13SWolfram Sang {
92630ae3e13SWolfram Sang 	u16 val = sd_ctrl_read16(host, CTL_SD_MEM_CARD_OPT);
92730ae3e13SWolfram Sang 
92830ae3e13SWolfram Sang 	val = (val & CARD_OPT_TOP_MASK) >> CARD_OPT_TOP_SHIFT;
92958959f89SWolfram Sang 	return 1 << (13 + val);
93058959f89SWolfram Sang }
93130ae3e13SWolfram Sang 
tmio_mmc_max_busy_timeout(struct tmio_mmc_host * host)93258959f89SWolfram Sang static void tmio_mmc_max_busy_timeout(struct tmio_mmc_host *host)
93358959f89SWolfram Sang {
93458959f89SWolfram Sang 	unsigned int clk_rate = host->mmc->actual_clock ?: host->mmc->f_max;
93558959f89SWolfram Sang 
93658959f89SWolfram Sang 	host->mmc->max_busy_timeout = host->get_timeout_cycles(host) /
93758959f89SWolfram Sang 				      (clk_rate / MSEC_PER_SEC);
93830ae3e13SWolfram Sang }
93930ae3e13SWolfram Sang 
940426e95d1SSimon Horman /* Set MMC clock / power.
941426e95d1SSimon Horman  * Note: This controller uses a simple divider scheme therefore it cannot
942426e95d1SSimon Horman  * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
943426e95d1SSimon Horman  * MMC wont run that fast, it has to be clocked at 12MHz which is the next
944426e95d1SSimon Horman  * slowest setting.
945426e95d1SSimon Horman  */
tmio_mmc_set_ios(struct mmc_host * mmc,struct mmc_ios * ios)946426e95d1SSimon Horman static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
947426e95d1SSimon Horman {
948426e95d1SSimon Horman 	struct tmio_mmc_host *host = mmc_priv(mmc);
949426e95d1SSimon Horman 	struct device *dev = &host->pdev->dev;
950426e95d1SSimon Horman 	unsigned long flags;
951426e95d1SSimon Horman 
952426e95d1SSimon Horman 	mutex_lock(&host->ios_lock);
953426e95d1SSimon Horman 
954426e95d1SSimon Horman 	spin_lock_irqsave(&host->lock, flags);
955426e95d1SSimon Horman 	if (host->mrq) {
956426e95d1SSimon Horman 		if (IS_ERR(host->mrq)) {
957426e95d1SSimon Horman 			dev_dbg(dev,
958426e95d1SSimon Horman 				"%s.%d: concurrent .set_ios(), clk %u, mode %u\n",
959426e95d1SSimon Horman 				current->comm, task_pid_nr(current),
960426e95d1SSimon Horman 				ios->clock, ios->power_mode);
961426e95d1SSimon Horman 			host->mrq = ERR_PTR(-EINTR);
962426e95d1SSimon Horman 		} else {
963426e95d1SSimon Horman 			dev_dbg(dev,
964426e95d1SSimon Horman 				"%s.%d: CMD%u active since %lu, now %lu!\n",
965426e95d1SSimon Horman 				current->comm, task_pid_nr(current),
966f2218db8SSimon Horman 				host->mrq->cmd->opcode, host->last_req_ts,
967f2218db8SSimon Horman 				jiffies);
968426e95d1SSimon Horman 		}
969426e95d1SSimon Horman 		spin_unlock_irqrestore(&host->lock, flags);
970426e95d1SSimon Horman 
971426e95d1SSimon Horman 		mutex_unlock(&host->ios_lock);
972426e95d1SSimon Horman 		return;
973426e95d1SSimon Horman 	}
974426e95d1SSimon Horman 
975426e95d1SSimon Horman 	host->mrq = ERR_PTR(-EBUSY);
976426e95d1SSimon Horman 
977426e95d1SSimon Horman 	spin_unlock_irqrestore(&host->lock, flags);
978426e95d1SSimon Horman 
979426e95d1SSimon Horman 	switch (ios->power_mode) {
980426e95d1SSimon Horman 	case MMC_POWER_OFF:
981426e95d1SSimon Horman 		tmio_mmc_power_off(host);
98232a9e0c4SWolfram Sang 		/* For R-Car Gen2+, we need to reset SDHI specific SCC */
983e315b1f3SBiju Das 		if (host->pdata->flags & TMIO_MMC_MIN_RCAR2)
9842e586f8aSWolfram Sang 			tmio_mmc_reset(host, false);
98590935eb3SWolfram Sang 
9860196c8dbSMasahiro Yamada 		host->set_clock(host, 0);
987426e95d1SSimon Horman 		break;
988426e95d1SSimon Horman 	case MMC_POWER_UP:
989426e95d1SSimon Horman 		tmio_mmc_power_on(host, ios->vdd);
9900196c8dbSMasahiro Yamada 		host->set_clock(host, ios->clock);
991426e95d1SSimon Horman 		tmio_mmc_set_bus_width(host, ios->bus_width);
992426e95d1SSimon Horman 		break;
993426e95d1SSimon Horman 	case MMC_POWER_ON:
9940196c8dbSMasahiro Yamada 		host->set_clock(host, ios->clock);
995426e95d1SSimon Horman 		tmio_mmc_set_bus_width(host, ios->bus_width);
996426e95d1SSimon Horman 		break;
997426e95d1SSimon Horman 	}
998426e95d1SSimon Horman 
99930ae3e13SWolfram Sang 	if (host->pdata->flags & TMIO_MMC_USE_BUSY_TIMEOUT)
100030ae3e13SWolfram Sang 		tmio_mmc_max_busy_timeout(host);
100130ae3e13SWolfram Sang 
1002426e95d1SSimon Horman 	/* Let things settle. delay taken from winCE driver */
1003754febccSWolfram Sang 	usleep_range(140, 200);
1004426e95d1SSimon Horman 	if (PTR_ERR(host->mrq) == -EINTR)
1005426e95d1SSimon Horman 		dev_dbg(&host->pdev->dev,
1006426e95d1SSimon Horman 			"%s.%d: IOS interrupted: clk %u, mode %u",
1007426e95d1SSimon Horman 			current->comm, task_pid_nr(current),
1008426e95d1SSimon Horman 			ios->clock, ios->power_mode);
1009426e95d1SSimon Horman 	host->mrq = NULL;
1010426e95d1SSimon Horman 
1011426e95d1SSimon Horman 	host->clk_cache = ios->clock;
1012426e95d1SSimon Horman 
1013426e95d1SSimon Horman 	mutex_unlock(&host->ios_lock);
1014426e95d1SSimon Horman }
1015426e95d1SSimon Horman 
tmio_mmc_get_ro(struct mmc_host * mmc)1016426e95d1SSimon Horman static int tmio_mmc_get_ro(struct mmc_host *mmc)
1017426e95d1SSimon Horman {
1018426e95d1SSimon Horman 	struct tmio_mmc_host *host = mmc_priv(mmc);
1019f2218db8SSimon Horman 
1020218f6024SMasahiro Yamada 	return !(sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) &
1021218f6024SMasahiro Yamada 		 TMIO_STAT_WRPROTECT);
1022426e95d1SSimon Horman }
1023426e95d1SSimon Horman 
tmio_mmc_get_cd(struct mmc_host * mmc)1024497d1f96SMasahiro Yamada static int tmio_mmc_get_cd(struct mmc_host *mmc)
1025497d1f96SMasahiro Yamada {
1026497d1f96SMasahiro Yamada 	struct tmio_mmc_host *host = mmc_priv(mmc);
1027497d1f96SMasahiro Yamada 
1028497d1f96SMasahiro Yamada 	return !!(sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) &
1029497d1f96SMasahiro Yamada 		  TMIO_STAT_SIGSTATE);
1030497d1f96SMasahiro Yamada }
1031497d1f96SMasahiro Yamada 
tmio_multi_io_quirk(struct mmc_card * card,unsigned int direction,int blk_size)1032426e95d1SSimon Horman static int tmio_multi_io_quirk(struct mmc_card *card,
1033426e95d1SSimon Horman 			       unsigned int direction, int blk_size)
1034426e95d1SSimon Horman {
1035426e95d1SSimon Horman 	struct tmio_mmc_host *host = mmc_priv(card->host);
1036426e95d1SSimon Horman 
1037426e95d1SSimon Horman 	if (host->multi_io_quirk)
1038426e95d1SSimon Horman 		return host->multi_io_quirk(card, direction, blk_size);
1039426e95d1SSimon Horman 
1040426e95d1SSimon Horman 	return blk_size;
1041426e95d1SSimon Horman }
1042426e95d1SSimon Horman 
1043f22084b6SWolfram Sang static struct mmc_host_ops tmio_mmc_ops = {
1044426e95d1SSimon Horman 	.request	= tmio_mmc_request,
1045426e95d1SSimon Horman 	.set_ios	= tmio_mmc_set_ios,
1046426e95d1SSimon Horman 	.get_ro         = tmio_mmc_get_ro,
1047497d1f96SMasahiro Yamada 	.get_cd		= tmio_mmc_get_cd,
1048426e95d1SSimon Horman 	.enable_sdio_irq = tmio_mmc_enable_sdio_irq,
1049426e95d1SSimon Horman 	.multi_io_quirk	= tmio_multi_io_quirk,
1050426e95d1SSimon Horman };
1051426e95d1SSimon Horman 
tmio_mmc_init_ocr(struct tmio_mmc_host * host)1052426e95d1SSimon Horman static int tmio_mmc_init_ocr(struct tmio_mmc_host *host)
1053426e95d1SSimon Horman {
1054426e95d1SSimon Horman 	struct tmio_mmc_data *pdata = host->pdata;
1055426e95d1SSimon Horman 	struct mmc_host *mmc = host->mmc;
1056a3d95d1dSFabrizio Castro 	int err;
1057426e95d1SSimon Horman 
1058a3d95d1dSFabrizio Castro 	err = mmc_regulator_get_supply(mmc);
1059a3d95d1dSFabrizio Castro 	if (err)
1060a3d95d1dSFabrizio Castro 		return err;
1061426e95d1SSimon Horman 
1062426e95d1SSimon Horman 	/* use ocr_mask if no regulator */
1063426e95d1SSimon Horman 	if (!mmc->ocr_avail)
1064426e95d1SSimon Horman 		mmc->ocr_avail = pdata->ocr_mask;
1065426e95d1SSimon Horman 
1066426e95d1SSimon Horman 	/*
1067426e95d1SSimon Horman 	 * try again.
1068426e95d1SSimon Horman 	 * There is possibility that regulator has not been probed
1069426e95d1SSimon Horman 	 */
1070426e95d1SSimon Horman 	if (!mmc->ocr_avail)
1071426e95d1SSimon Horman 		return -EPROBE_DEFER;
1072426e95d1SSimon Horman 
1073426e95d1SSimon Horman 	return 0;
1074426e95d1SSimon Horman }
1075426e95d1SSimon Horman 
tmio_mmc_of_parse(struct platform_device * pdev,struct mmc_host * mmc)1076426e95d1SSimon Horman static void tmio_mmc_of_parse(struct platform_device *pdev,
10777c53b797SMasahiro Yamada 			      struct mmc_host *mmc)
1078426e95d1SSimon Horman {
1079426e95d1SSimon Horman 	const struct device_node *np = pdev->dev.of_node;
1080f2218db8SSimon Horman 
1081426e95d1SSimon Horman 	if (!np)
1082426e95d1SSimon Horman 		return;
1083426e95d1SSimon Horman 
1084788778b0SMasahiro Yamada 	/*
1085788778b0SMasahiro Yamada 	 * DEPRECATED:
1086788778b0SMasahiro Yamada 	 * For new platforms, please use "disable-wp" instead of
1087788778b0SMasahiro Yamada 	 * "toshiba,mmc-wrprotect-disable"
1088788778b0SMasahiro Yamada 	 */
1089ca6b5fe2SRob Herring 	if (of_property_read_bool(np, "toshiba,mmc-wrprotect-disable"))
10907c53b797SMasahiro Yamada 		mmc->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
1091426e95d1SSimon Horman }
1092426e95d1SSimon Horman 
tmio_mmc_host_alloc(struct platform_device * pdev,struct tmio_mmc_data * pdata)1093b21fc294SMasahiro Yamada struct tmio_mmc_host *tmio_mmc_host_alloc(struct platform_device *pdev,
1094b21fc294SMasahiro Yamada 					  struct tmio_mmc_data *pdata)
1095426e95d1SSimon Horman {
1096426e95d1SSimon Horman 	struct tmio_mmc_host *host;
1097426e95d1SSimon Horman 	struct mmc_host *mmc;
10988d09a133SMasahiro Yamada 	void __iomem *ctl;
10996fb294f7SMasahiro Yamada 	int ret;
11008d09a133SMasahiro Yamada 
11011cfb7c28SYangtao Li 	ctl = devm_platform_ioremap_resource(pdev, 0);
11028d09a133SMasahiro Yamada 	if (IS_ERR(ctl))
11038d09a133SMasahiro Yamada 		return ERR_CAST(ctl);
1104426e95d1SSimon Horman 
1105426e95d1SSimon Horman 	mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &pdev->dev);
1106426e95d1SSimon Horman 	if (!mmc)
11078d09a133SMasahiro Yamada 		return ERR_PTR(-ENOMEM);
1108426e95d1SSimon Horman 
1109426e95d1SSimon Horman 	host = mmc_priv(mmc);
11108d09a133SMasahiro Yamada 	host->ctl = ctl;
1111426e95d1SSimon Horman 	host->mmc = mmc;
1112426e95d1SSimon Horman 	host->pdev = pdev;
1113b21fc294SMasahiro Yamada 	host->pdata = pdata;
1114c055fc75SMasahiro Yamada 	host->ops = tmio_mmc_ops;
1115c055fc75SMasahiro Yamada 	mmc->ops = &host->ops;
1116426e95d1SSimon Horman 
11176fb294f7SMasahiro Yamada 	ret = mmc_of_parse(host->mmc);
11186fb294f7SMasahiro Yamada 	if (ret) {
11196fb294f7SMasahiro Yamada 		host = ERR_PTR(ret);
11206fb294f7SMasahiro Yamada 		goto free;
11216fb294f7SMasahiro Yamada 	}
11226fb294f7SMasahiro Yamada 
11237c53b797SMasahiro Yamada 	tmio_mmc_of_parse(pdev, mmc);
11246fb294f7SMasahiro Yamada 
1125b21fc294SMasahiro Yamada 	platform_set_drvdata(pdev, host);
1126b21fc294SMasahiro Yamada 
1127426e95d1SSimon Horman 	return host;
11286fb294f7SMasahiro Yamada free:
11296fb294f7SMasahiro Yamada 	mmc_free_host(mmc);
11306fb294f7SMasahiro Yamada 
11316fb294f7SMasahiro Yamada 	return host;
1132426e95d1SSimon Horman }
11336106ecf3SSimon Horman EXPORT_SYMBOL_GPL(tmio_mmc_host_alloc);
1134426e95d1SSimon Horman 
tmio_mmc_host_free(struct tmio_mmc_host * host)1135426e95d1SSimon Horman void tmio_mmc_host_free(struct tmio_mmc_host *host)
1136426e95d1SSimon Horman {
1137426e95d1SSimon Horman 	mmc_free_host(host->mmc);
1138426e95d1SSimon Horman }
11396106ecf3SSimon Horman EXPORT_SYMBOL_GPL(tmio_mmc_host_free);
1140426e95d1SSimon Horman 
tmio_mmc_host_probe(struct tmio_mmc_host * _host)1141bc45719cSMasahiro Yamada int tmio_mmc_host_probe(struct tmio_mmc_host *_host)
1142426e95d1SSimon Horman {
1143426e95d1SSimon Horman 	struct platform_device *pdev = _host->pdev;
1144b21fc294SMasahiro Yamada 	struct tmio_mmc_data *pdata = _host->pdata;
1145426e95d1SSimon Horman 	struct mmc_host *mmc = _host->mmc;
1146426e95d1SSimon Horman 	int ret;
1147426e95d1SSimon Horman 
1148b21fc294SMasahiro Yamada 	/*
11490196c8dbSMasahiro Yamada 	 * Check the sanity of mmc->f_min to prevent host->set_clock() from
1150b21fc294SMasahiro Yamada 	 * looping forever...
1151b21fc294SMasahiro Yamada 	 */
1152b21fc294SMasahiro Yamada 	if (mmc->f_min == 0)
1153b21fc294SMasahiro Yamada 		return -EINVAL;
1154b21fc294SMasahiro Yamada 
1155426e95d1SSimon Horman 	if (!(pdata->flags & TMIO_MMC_HAS_IDLE_WAIT))
1156426e95d1SSimon Horman 		_host->write16_hook = NULL;
1157426e95d1SSimon Horman 
115858959f89SWolfram Sang 	if (pdata->flags & TMIO_MMC_USE_BUSY_TIMEOUT && !_host->get_timeout_cycles)
115958959f89SWolfram Sang 		_host->get_timeout_cycles = tmio_mmc_get_timeout_cycles;
116058959f89SWolfram Sang 
1161426e95d1SSimon Horman 	_host->set_pwr = pdata->set_pwr;
1162426e95d1SSimon Horman 
1163426e95d1SSimon Horman 	ret = tmio_mmc_init_ocr(_host);
1164426e95d1SSimon Horman 	if (ret < 0)
1165426e95d1SSimon Horman 		return ret;
1166426e95d1SSimon Horman 
1167faed9303SLinus Walleij 	/*
1168faed9303SLinus Walleij 	 * Look for a card detect GPIO, if it fails with anything
1169faed9303SLinus Walleij 	 * else than a probe deferral, just live without it.
1170faed9303SLinus Walleij 	 */
1171d0052ad9SMichał Mirosław 	ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0);
1172faed9303SLinus Walleij 	if (ret == -EPROBE_DEFER)
1173cd82cd21SMasahiro Yamada 		return ret;
1174cd82cd21SMasahiro Yamada 
11751be64c79SUlf Hansson 	mmc->caps |= MMC_CAP_4_BIT_DATA | pdata->capabilities;
1176426e95d1SSimon Horman 	mmc->caps2 |= pdata->capabilities2;
1177603aa14dSYoshihiro Shimoda 	mmc->max_segs = pdata->max_segs ? : 32;
1178609e5fbaSWolfram Sang 	mmc->max_blk_size = TMIO_MAX_BLK_SIZE;
1179603aa14dSYoshihiro Shimoda 	mmc->max_blk_count = pdata->max_blk_count ? :
1180603aa14dSYoshihiro Shimoda 		(PAGE_SIZE / mmc->max_blk_size) * mmc->max_segs;
118163624d13SYoshihiro Shimoda 	mmc->max_req_size = min_t(size_t,
118263624d13SYoshihiro Shimoda 				  mmc->max_blk_size * mmc->max_blk_count,
118363624d13SYoshihiro Shimoda 				  dma_max_mapping_size(&pdev->dev));
1184426e95d1SSimon Horman 	mmc->max_seg_size = mmc->max_req_size;
1185426e95d1SSimon Horman 
11861910b87fSMasahiro Yamada 	if (mmc_can_gpio_ro(mmc))
11871910b87fSMasahiro Yamada 		_host->ops.get_ro = mmc_gpio_get_ro;
11881910b87fSMasahiro Yamada 
1189497d1f96SMasahiro Yamada 	if (mmc_can_gpio_cd(mmc))
1190497d1f96SMasahiro Yamada 		_host->ops.get_cd = mmc_gpio_get_cd;
1191497d1f96SMasahiro Yamada 
1192e315b1f3SBiju Das 	/* must be set before tmio_mmc_reset() */
1193de21dc1dSMasahiro Yamada 	_host->native_hotplug = !(mmc_can_gpio_cd(mmc) ||
1194426e95d1SSimon Horman 				  mmc->caps & MMC_CAP_NEEDS_POLL ||
1195426e95d1SSimon Horman 				  !mmc_card_is_removable(mmc));
1196426e95d1SSimon Horman 
1197426e95d1SSimon Horman 	/*
1198426e95d1SSimon Horman 	 * While using internal tmio hardware logic for card detection, we need
1199426e95d1SSimon Horman 	 * to ensure it stays powered for it to work.
1200426e95d1SSimon Horman 	 */
1201426e95d1SSimon Horman 	if (_host->native_hotplug)
1202426e95d1SSimon Horman 		pm_runtime_get_noresume(&pdev->dev);
1203426e95d1SSimon Horman 
120486beb538SWolfram Sang 	_host->sdio_irq_enabled = false;
120586beb538SWolfram Sang 	if (pdata->flags & TMIO_MMC_SDIO_IRQ)
120686beb538SWolfram Sang 		_host->sdio_irq_mask = TMIO_SDIO_MASK_ALL;
120786beb538SWolfram Sang 
12080d856c4cSWolfram Sang 	if (!_host->sdcard_irq_mask_all)
12090d856c4cSWolfram Sang 		_host->sdcard_irq_mask_all = TMIO_MASK_ALL;
12100751d56eSWolfram Sang 
12110751d56eSWolfram Sang 	_host->set_clock(_host, 0);
12122e586f8aSWolfram Sang 	tmio_mmc_reset(_host, false);
1213426e95d1SSimon Horman 
1214426e95d1SSimon Horman 	spin_lock_init(&_host->lock);
1215426e95d1SSimon Horman 	mutex_init(&_host->ios_lock);
1216426e95d1SSimon Horman 
1217426e95d1SSimon Horman 	/* Init delayed work for request timeouts */
1218426e95d1SSimon Horman 	INIT_DELAYED_WORK(&_host->delayed_reset_work, tmio_mmc_reset_work);
1219426e95d1SSimon Horman 	INIT_WORK(&_host->done, tmio_mmc_done_work);
1220426e95d1SSimon Horman 
1221426e95d1SSimon Horman 	/* See if we also get DMA */
1222426e95d1SSimon Horman 	tmio_mmc_request_dma(_host, pdata);
1223426e95d1SSimon Horman 
12241b32999eSUlf Hansson 	pm_runtime_get_noresume(&pdev->dev);
12251b32999eSUlf Hansson 	pm_runtime_set_active(&pdev->dev);
1226426e95d1SSimon Horman 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1227426e95d1SSimon Horman 	pm_runtime_use_autosuspend(&pdev->dev);
12288861474aSUlf Hansson 	pm_runtime_enable(&pdev->dev);
1229426e95d1SSimon Horman 
1230426e95d1SSimon Horman 	ret = mmc_add_host(mmc);
12317f8e446bSMarkus Elfring 	if (ret)
12327f8e446bSMarkus Elfring 		goto remove_host;
1233426e95d1SSimon Horman 
1234426e95d1SSimon Horman 	dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
1235aa86f1a3SUlf Hansson 	pm_runtime_put(&pdev->dev);
1236426e95d1SSimon Horman 
1237426e95d1SSimon Horman 	return 0;
12387f8e446bSMarkus Elfring 
12397f8e446bSMarkus Elfring remove_host:
1240aa86f1a3SUlf Hansson 	pm_runtime_put_noidle(&pdev->dev);
12417f8e446bSMarkus Elfring 	tmio_mmc_host_remove(_host);
12427f8e446bSMarkus Elfring 	return ret;
1243426e95d1SSimon Horman }
12446106ecf3SSimon Horman EXPORT_SYMBOL_GPL(tmio_mmc_host_probe);
1245426e95d1SSimon Horman 
tmio_mmc_host_remove(struct tmio_mmc_host * host)1246426e95d1SSimon Horman void tmio_mmc_host_remove(struct tmio_mmc_host *host)
1247426e95d1SSimon Horman {
1248426e95d1SSimon Horman 	struct platform_device *pdev = host->pdev;
1249426e95d1SSimon Horman 	struct mmc_host *mmc = host->mmc;
1250426e95d1SSimon Horman 
125187b5d602SUlf Hansson 	pm_runtime_get_sync(&pdev->dev);
125287b5d602SUlf Hansson 
1253426e95d1SSimon Horman 	if (host->pdata->flags & TMIO_MMC_SDIO_IRQ)
1254426e95d1SSimon Horman 		sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000);
1255426e95d1SSimon Horman 
1256426e95d1SSimon Horman 	dev_pm_qos_hide_latency_limit(&pdev->dev);
1257426e95d1SSimon Horman 
1258426e95d1SSimon Horman 	mmc_remove_host(mmc);
1259426e95d1SSimon Horman 	cancel_work_sync(&host->done);
1260426e95d1SSimon Horman 	cancel_delayed_work_sync(&host->delayed_reset_work);
1261426e95d1SSimon Horman 	tmio_mmc_release_dma(host);
12620d856c4cSWolfram Sang 	tmio_mmc_disable_mmc_irqs(host, host->sdcard_irq_mask_all);
1263426e95d1SSimon Horman 
126487b5d602SUlf Hansson 	if (host->native_hotplug)
126587b5d602SUlf Hansson 		pm_runtime_put_noidle(&pdev->dev);
12664bd78441SUlf Hansson 
12678861474aSUlf Hansson 	pm_runtime_disable(&pdev->dev);
12684bd78441SUlf Hansson 	pm_runtime_dont_use_autosuspend(&pdev->dev);
12694bd78441SUlf Hansson 	pm_runtime_put_noidle(&pdev->dev);
1270426e95d1SSimon Horman }
12716106ecf3SSimon Horman EXPORT_SYMBOL_GPL(tmio_mmc_host_remove);
1272426e95d1SSimon Horman 
1273426e95d1SSimon Horman #ifdef CONFIG_PM
tmio_mmc_clk_enable(struct tmio_mmc_host * host)12744a09d0b8SArnd Bergmann static int tmio_mmc_clk_enable(struct tmio_mmc_host *host)
12754a09d0b8SArnd Bergmann {
12764a09d0b8SArnd Bergmann 	if (!host->clk_enable)
12774a09d0b8SArnd Bergmann 		return -ENOTSUPP;
12784a09d0b8SArnd Bergmann 
12794a09d0b8SArnd Bergmann 	return host->clk_enable(host);
12804a09d0b8SArnd Bergmann }
12814a09d0b8SArnd Bergmann 
tmio_mmc_clk_disable(struct tmio_mmc_host * host)12824a09d0b8SArnd Bergmann static void tmio_mmc_clk_disable(struct tmio_mmc_host *host)
12834a09d0b8SArnd Bergmann {
12844a09d0b8SArnd Bergmann 	if (host->clk_disable)
12854a09d0b8SArnd Bergmann 		host->clk_disable(host);
12864a09d0b8SArnd Bergmann }
12874a09d0b8SArnd Bergmann 
tmio_mmc_host_runtime_suspend(struct device * dev)1288426e95d1SSimon Horman int tmio_mmc_host_runtime_suspend(struct device *dev)
1289426e95d1SSimon Horman {
1290a3b05373SMasahiro Yamada 	struct tmio_mmc_host *host = dev_get_drvdata(dev);
1291426e95d1SSimon Horman 
12920d856c4cSWolfram Sang 	tmio_mmc_disable_mmc_irqs(host, host->sdcard_irq_mask_all);
1293426e95d1SSimon Horman 
1294426e95d1SSimon Horman 	if (host->clk_cache)
12950196c8dbSMasahiro Yamada 		host->set_clock(host, 0);
1296426e95d1SSimon Horman 
1297426e95d1SSimon Horman 	tmio_mmc_clk_disable(host);
1298426e95d1SSimon Horman 
1299426e95d1SSimon Horman 	return 0;
1300426e95d1SSimon Horman }
13016106ecf3SSimon Horman EXPORT_SYMBOL_GPL(tmio_mmc_host_runtime_suspend);
1302426e95d1SSimon Horman 
tmio_mmc_host_runtime_resume(struct device * dev)1303426e95d1SSimon Horman int tmio_mmc_host_runtime_resume(struct device *dev)
1304426e95d1SSimon Horman {
1305a3b05373SMasahiro Yamada 	struct tmio_mmc_host *host = dev_get_drvdata(dev);
1306426e95d1SSimon Horman 
1307426e95d1SSimon Horman 	tmio_mmc_clk_enable(host);
13082e586f8aSWolfram Sang 	tmio_mmc_reset(host, false);
1309426e95d1SSimon Horman 
1310426e95d1SSimon Horman 	if (host->clk_cache)
13110196c8dbSMasahiro Yamada 		host->set_clock(host, host->clk_cache);
1312426e95d1SSimon Horman 
1313426e95d1SSimon Horman 	tmio_mmc_enable_dma(host, true);
1314426e95d1SSimon Horman 
1315426e95d1SSimon Horman 	return 0;
1316426e95d1SSimon Horman }
13176106ecf3SSimon Horman EXPORT_SYMBOL_GPL(tmio_mmc_host_runtime_resume);
1318426e95d1SSimon Horman #endif
1319426e95d1SSimon Horman 
1320426e95d1SSimon Horman MODULE_LICENSE("GPL v2");
1321