xref: /linux/drivers/mmc/host/omap.c (revision b13d1f0f9ad64bc498ced07344495ba27321419e)
11c6a0718SPierre Ossman /*
270f10482SPierre Ossman  *  linux/drivers/mmc/host/omap.c
31c6a0718SPierre Ossman  *
41c6a0718SPierre Ossman  *  Copyright (C) 2004 Nokia Corporation
5d36b6910SAl Viro  *  Written by Tuukka Tikkanen and Juha Yrjölä<juha.yrjola@nokia.com>
61c6a0718SPierre Ossman  *  Misc hacks here and there by Tony Lindgren <tony@atomide.com>
71c6a0718SPierre Ossman  *  Other hacks (DMA, SD, etc) by David Brownell
81c6a0718SPierre Ossman  *
91c6a0718SPierre Ossman  * This program is free software; you can redistribute it and/or modify
101c6a0718SPierre Ossman  * it under the terms of the GNU General Public License version 2 as
111c6a0718SPierre Ossman  * published by the Free Software Foundation.
121c6a0718SPierre Ossman  */
131c6a0718SPierre Ossman 
141c6a0718SPierre Ossman #include <linux/module.h>
151c6a0718SPierre Ossman #include <linux/moduleparam.h>
161c6a0718SPierre Ossman #include <linux/init.h>
171c6a0718SPierre Ossman #include <linux/ioport.h>
181c6a0718SPierre Ossman #include <linux/platform_device.h>
191c6a0718SPierre Ossman #include <linux/interrupt.h>
203451c067SRussell King #include <linux/dmaengine.h>
211c6a0718SPierre Ossman #include <linux/dma-mapping.h>
221c6a0718SPierre Ossman #include <linux/delay.h>
231c6a0718SPierre Ossman #include <linux/spinlock.h>
241c6a0718SPierre Ossman #include <linux/timer.h>
259cb238c0STony Lindgren #include <linux/of.h>
263451c067SRussell King #include <linux/omap-dma.h>
271c6a0718SPierre Ossman #include <linux/mmc/host.h>
281c6a0718SPierre Ossman #include <linux/mmc/card.h>
29*b13d1f0fSJarkko Nikula #include <linux/mmc/mmc.h>
301c6a0718SPierre Ossman #include <linux/clk.h>
3145711f1aSJens Axboe #include <linux/scatterlist.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
3368f39e74STony Lindgren #include <linux/platform_data/mmc-omap.h>
341c6a0718SPierre Ossman 
351c6a0718SPierre Ossman 
361c6a0718SPierre Ossman #define	OMAP_MMC_REG_CMD	0x00
370e950fa6SMarek Belisko #define	OMAP_MMC_REG_ARGL	0x01
380e950fa6SMarek Belisko #define	OMAP_MMC_REG_ARGH	0x02
390e950fa6SMarek Belisko #define	OMAP_MMC_REG_CON	0x03
400e950fa6SMarek Belisko #define	OMAP_MMC_REG_STAT	0x04
410e950fa6SMarek Belisko #define	OMAP_MMC_REG_IE		0x05
420e950fa6SMarek Belisko #define	OMAP_MMC_REG_CTO	0x06
430e950fa6SMarek Belisko #define	OMAP_MMC_REG_DTO	0x07
440e950fa6SMarek Belisko #define	OMAP_MMC_REG_DATA	0x08
450e950fa6SMarek Belisko #define	OMAP_MMC_REG_BLEN	0x09
460e950fa6SMarek Belisko #define	OMAP_MMC_REG_NBLK	0x0a
470e950fa6SMarek Belisko #define	OMAP_MMC_REG_BUF	0x0b
480e950fa6SMarek Belisko #define	OMAP_MMC_REG_SDIO	0x0d
490e950fa6SMarek Belisko #define	OMAP_MMC_REG_REV	0x0f
500e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP0	0x10
510e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP1	0x11
520e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP2	0x12
530e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP3	0x13
540e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP4	0x14
550e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP5	0x15
560e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP6	0x16
570e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP7	0x17
580e950fa6SMarek Belisko #define	OMAP_MMC_REG_IOSR	0x18
590e950fa6SMarek Belisko #define	OMAP_MMC_REG_SYSC	0x19
600e950fa6SMarek Belisko #define	OMAP_MMC_REG_SYSS	0x1a
611c6a0718SPierre Ossman 
621c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CARD_ERR		(1 << 14)
631c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CARD_IRQ		(1 << 13)
641c6a0718SPierre Ossman #define	OMAP_MMC_STAT_OCR_BUSY		(1 << 12)
651c6a0718SPierre Ossman #define	OMAP_MMC_STAT_A_EMPTY		(1 << 11)
661c6a0718SPierre Ossman #define	OMAP_MMC_STAT_A_FULL		(1 << 10)
671c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CMD_CRC		(1 <<  8)
681c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CMD_TOUT		(1 <<  7)
691c6a0718SPierre Ossman #define	OMAP_MMC_STAT_DATA_CRC		(1 <<  6)
701c6a0718SPierre Ossman #define	OMAP_MMC_STAT_DATA_TOUT		(1 <<  5)
711c6a0718SPierre Ossman #define	OMAP_MMC_STAT_END_BUSY		(1 <<  4)
721c6a0718SPierre Ossman #define	OMAP_MMC_STAT_END_OF_DATA	(1 <<  3)
731c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CARD_BUSY		(1 <<  2)
741c6a0718SPierre Ossman #define	OMAP_MMC_STAT_END_OF_CMD	(1 <<  0)
751c6a0718SPierre Ossman 
7653db20d1STony Lindgren #define mmc_omap7xx()	(host->features & MMC_OMAP7XX)
7753db20d1STony Lindgren #define mmc_omap15xx()	(host->features & MMC_OMAP15XX)
7853db20d1STony Lindgren #define mmc_omap16xx()	(host->features & MMC_OMAP16XX)
7953db20d1STony Lindgren #define MMC_OMAP1_MASK	(MMC_OMAP7XX | MMC_OMAP15XX | MMC_OMAP16XX)
8053db20d1STony Lindgren #define mmc_omap1()	(host->features & MMC_OMAP1_MASK)
8153db20d1STony Lindgren #define mmc_omap2()	(!mmc_omap1())
8253db20d1STony Lindgren 
830e950fa6SMarek Belisko #define OMAP_MMC_REG(host, reg)		(OMAP_MMC_REG_##reg << (host)->reg_shift)
840e950fa6SMarek Belisko #define OMAP_MMC_READ(host, reg)	__raw_readw((host)->virt_base + OMAP_MMC_REG(host, reg))
850e950fa6SMarek Belisko #define OMAP_MMC_WRITE(host, reg, val)	__raw_writew((val), (host)->virt_base + OMAP_MMC_REG(host, reg))
861c6a0718SPierre Ossman 
871c6a0718SPierre Ossman /*
881c6a0718SPierre Ossman  * Command types
891c6a0718SPierre Ossman  */
901c6a0718SPierre Ossman #define OMAP_MMC_CMDTYPE_BC	0
911c6a0718SPierre Ossman #define OMAP_MMC_CMDTYPE_BCR	1
921c6a0718SPierre Ossman #define OMAP_MMC_CMDTYPE_AC	2
931c6a0718SPierre Ossman #define OMAP_MMC_CMDTYPE_ADTC	3
941c6a0718SPierre Ossman 
951c6a0718SPierre Ossman #define DRIVER_NAME "mmci-omap"
961c6a0718SPierre Ossman 
971c6a0718SPierre Ossman /* Specifies how often in millisecs to poll for card status changes
981c6a0718SPierre Ossman  * when the cover switch is open */
997584d276SJarkko Lavinen #define OMAP_MMC_COVER_POLL_DELAY	500
1001c6a0718SPierre Ossman 
101abfbe5f7SJuha Yrjola struct mmc_omap_host;
102abfbe5f7SJuha Yrjola 
103abfbe5f7SJuha Yrjola struct mmc_omap_slot {
104abfbe5f7SJuha Yrjola 	int			id;
105abfbe5f7SJuha Yrjola 	unsigned int		vdd;
106abfbe5f7SJuha Yrjola 	u16			saved_con;
107abfbe5f7SJuha Yrjola 	u16			bus_mode;
108abfbe5f7SJuha Yrjola 	unsigned int		fclk_freq;
109abfbe5f7SJuha Yrjola 
1107584d276SJarkko Lavinen 	struct tasklet_struct	cover_tasklet;
1117584d276SJarkko Lavinen 	struct timer_list       cover_timer;
1125a0f3f1fSJuha Yrjola 	unsigned		cover_open;
1135a0f3f1fSJuha Yrjola 
114abfbe5f7SJuha Yrjola 	struct mmc_request      *mrq;
115abfbe5f7SJuha Yrjola 	struct mmc_omap_host    *host;
116abfbe5f7SJuha Yrjola 	struct mmc_host		*mmc;
117abfbe5f7SJuha Yrjola 	struct omap_mmc_slot_data *pdata;
118abfbe5f7SJuha Yrjola };
119abfbe5f7SJuha Yrjola 
1201c6a0718SPierre Ossman struct mmc_omap_host {
1211c6a0718SPierre Ossman 	int			initialized;
1221c6a0718SPierre Ossman 	struct mmc_request *	mrq;
1231c6a0718SPierre Ossman 	struct mmc_command *	cmd;
1241c6a0718SPierre Ossman 	struct mmc_data *	data;
1251c6a0718SPierre Ossman 	struct mmc_host *	mmc;
1261c6a0718SPierre Ossman 	struct device *		dev;
1271c6a0718SPierre Ossman 	unsigned char		id; /* 16xx chips have 2 MMC blocks */
1281c6a0718SPierre Ossman 	struct clk *		iclk;
1291c6a0718SPierre Ossman 	struct clk *		fclk;
1303451c067SRussell King 	struct dma_chan		*dma_rx;
1313451c067SRussell King 	u32			dma_rx_burst;
1323451c067SRussell King 	struct dma_chan		*dma_tx;
1333451c067SRussell King 	u32			dma_tx_burst;
1341c6a0718SPierre Ossman 	void __iomem		*virt_base;
1351c6a0718SPierre Ossman 	unsigned int		phys_base;
1361c6a0718SPierre Ossman 	int			irq;
1371c6a0718SPierre Ossman 	unsigned char		bus_mode;
1380e950fa6SMarek Belisko 	unsigned int		reg_shift;
1391c6a0718SPierre Ossman 
1400fb4723dSJarkko Lavinen 	struct work_struct	cmd_abort_work;
1410fb4723dSJarkko Lavinen 	unsigned		abort:1;
1420fb4723dSJarkko Lavinen 	struct timer_list	cmd_abort_timer;
143eb1860bcSJarkko Lavinen 
1440f602ec7SJarkko Lavinen 	struct work_struct      slot_release_work;
1450f602ec7SJarkko Lavinen 	struct mmc_omap_slot    *next_slot;
1460f602ec7SJarkko Lavinen 	struct work_struct      send_stop_work;
1470f602ec7SJarkko Lavinen 	struct mmc_data		*stop_data;
1480f602ec7SJarkko Lavinen 
1491c6a0718SPierre Ossman 	unsigned int		sg_len;
1501c6a0718SPierre Ossman 	int			sg_idx;
1511c6a0718SPierre Ossman 	u16 *			buffer;
1521c6a0718SPierre Ossman 	u32			buffer_bytes_left;
1531c6a0718SPierre Ossman 	u32			total_bytes_left;
1541c6a0718SPierre Ossman 
15553db20d1STony Lindgren 	unsigned		features;
1561c6a0718SPierre Ossman 	unsigned		brs_received:1, dma_done:1;
1571c6a0718SPierre Ossman 	unsigned		dma_in_use:1;
1583451c067SRussell King 	spinlock_t		dma_lock;
1591c6a0718SPierre Ossman 
160abfbe5f7SJuha Yrjola 	struct mmc_omap_slot    *slots[OMAP_MMC_MAX_SLOTS];
161abfbe5f7SJuha Yrjola 	struct mmc_omap_slot    *current_slot;
162abfbe5f7SJuha Yrjola 	spinlock_t              slot_lock;
163abfbe5f7SJuha Yrjola 	wait_queue_head_t       slot_wq;
164abfbe5f7SJuha Yrjola 	int                     nr_slots;
165abfbe5f7SJuha Yrjola 
1660807a9b5SJarkko Lavinen 	struct timer_list       clk_timer;
1670807a9b5SJarkko Lavinen 	spinlock_t		clk_lock;     /* for changing enabled state */
1680807a9b5SJarkko Lavinen 	unsigned int            fclk_enabled:1;
169b01a4f1cSVenkatraman S 	struct workqueue_struct *mmc_omap_wq;
1700807a9b5SJarkko Lavinen 
171abfbe5f7SJuha Yrjola 	struct omap_mmc_platform_data *pdata;
1721c6a0718SPierre Ossman };
1731c6a0718SPierre Ossman 
1740d9ee5b2STejun Heo 
1757c8ad982SRussell King static void mmc_omap_fclk_offdelay(struct mmc_omap_slot *slot)
1760807a9b5SJarkko Lavinen {
1770807a9b5SJarkko Lavinen 	unsigned long tick_ns;
1780807a9b5SJarkko Lavinen 
1790807a9b5SJarkko Lavinen 	if (slot != NULL && slot->host->fclk_enabled && slot->fclk_freq > 0) {
1800807a9b5SJarkko Lavinen 		tick_ns = (1000000000 + slot->fclk_freq - 1) / slot->fclk_freq;
1810807a9b5SJarkko Lavinen 		ndelay(8 * tick_ns);
1820807a9b5SJarkko Lavinen 	}
1830807a9b5SJarkko Lavinen }
1840807a9b5SJarkko Lavinen 
1857c8ad982SRussell King static void mmc_omap_fclk_enable(struct mmc_omap_host *host, unsigned int enable)
1860807a9b5SJarkko Lavinen {
1870807a9b5SJarkko Lavinen 	unsigned long flags;
1880807a9b5SJarkko Lavinen 
1890807a9b5SJarkko Lavinen 	spin_lock_irqsave(&host->clk_lock, flags);
1900807a9b5SJarkko Lavinen 	if (host->fclk_enabled != enable) {
1910807a9b5SJarkko Lavinen 		host->fclk_enabled = enable;
1920807a9b5SJarkko Lavinen 		if (enable)
1930807a9b5SJarkko Lavinen 			clk_enable(host->fclk);
1940807a9b5SJarkko Lavinen 		else
1950807a9b5SJarkko Lavinen 			clk_disable(host->fclk);
1960807a9b5SJarkko Lavinen 	}
1970807a9b5SJarkko Lavinen 	spin_unlock_irqrestore(&host->clk_lock, flags);
1980807a9b5SJarkko Lavinen }
1990807a9b5SJarkko Lavinen 
200abfbe5f7SJuha Yrjola static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
201abfbe5f7SJuha Yrjola {
202abfbe5f7SJuha Yrjola 	struct mmc_omap_host *host = slot->host;
203abfbe5f7SJuha Yrjola 	unsigned long flags;
204abfbe5f7SJuha Yrjola 
205abfbe5f7SJuha Yrjola 	if (claimed)
206abfbe5f7SJuha Yrjola 		goto no_claim;
207abfbe5f7SJuha Yrjola 	spin_lock_irqsave(&host->slot_lock, flags);
208abfbe5f7SJuha Yrjola 	while (host->mmc != NULL) {
209abfbe5f7SJuha Yrjola 		spin_unlock_irqrestore(&host->slot_lock, flags);
210abfbe5f7SJuha Yrjola 		wait_event(host->slot_wq, host->mmc == NULL);
211abfbe5f7SJuha Yrjola 		spin_lock_irqsave(&host->slot_lock, flags);
212abfbe5f7SJuha Yrjola 	}
213abfbe5f7SJuha Yrjola 	host->mmc = slot->mmc;
214abfbe5f7SJuha Yrjola 	spin_unlock_irqrestore(&host->slot_lock, flags);
215abfbe5f7SJuha Yrjola no_claim:
2160807a9b5SJarkko Lavinen 	del_timer(&host->clk_timer);
2170807a9b5SJarkko Lavinen 	if (host->current_slot != slot || !claimed)
2180807a9b5SJarkko Lavinen 		mmc_omap_fclk_offdelay(host->current_slot);
2190807a9b5SJarkko Lavinen 
220abfbe5f7SJuha Yrjola 	if (host->current_slot != slot) {
2210807a9b5SJarkko Lavinen 		OMAP_MMC_WRITE(host, CON, slot->saved_con & 0xFC00);
222abfbe5f7SJuha Yrjola 		if (host->pdata->switch_slot != NULL)
223abfbe5f7SJuha Yrjola 			host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
224abfbe5f7SJuha Yrjola 		host->current_slot = slot;
225abfbe5f7SJuha Yrjola 	}
226abfbe5f7SJuha Yrjola 
2270807a9b5SJarkko Lavinen 	if (claimed) {
2280807a9b5SJarkko Lavinen 		mmc_omap_fclk_enable(host, 1);
2290807a9b5SJarkko Lavinen 
230abfbe5f7SJuha Yrjola 		/* Doing the dummy read here seems to work around some bug
231abfbe5f7SJuha Yrjola 		 * at least in OMAP24xx silicon where the command would not
232abfbe5f7SJuha Yrjola 		 * start after writing the CMD register. Sigh. */
233abfbe5f7SJuha Yrjola 		OMAP_MMC_READ(host, CON);
234abfbe5f7SJuha Yrjola 
235abfbe5f7SJuha Yrjola 		OMAP_MMC_WRITE(host, CON, slot->saved_con);
2360807a9b5SJarkko Lavinen 	} else
2370807a9b5SJarkko Lavinen 		mmc_omap_fclk_enable(host, 0);
238abfbe5f7SJuha Yrjola }
239abfbe5f7SJuha Yrjola 
240abfbe5f7SJuha Yrjola static void mmc_omap_start_request(struct mmc_omap_host *host,
241abfbe5f7SJuha Yrjola 				   struct mmc_request *req);
242abfbe5f7SJuha Yrjola 
2430f602ec7SJarkko Lavinen static void mmc_omap_slot_release_work(struct work_struct *work)
2440f602ec7SJarkko Lavinen {
2450f602ec7SJarkko Lavinen 	struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
2460f602ec7SJarkko Lavinen 						  slot_release_work);
2470f602ec7SJarkko Lavinen 	struct mmc_omap_slot *next_slot = host->next_slot;
2480f602ec7SJarkko Lavinen 	struct mmc_request *rq;
2490f602ec7SJarkko Lavinen 
2500f602ec7SJarkko Lavinen 	host->next_slot = NULL;
2510f602ec7SJarkko Lavinen 	mmc_omap_select_slot(next_slot, 1);
2520f602ec7SJarkko Lavinen 
2530f602ec7SJarkko Lavinen 	rq = next_slot->mrq;
2540f602ec7SJarkko Lavinen 	next_slot->mrq = NULL;
2550f602ec7SJarkko Lavinen 	mmc_omap_start_request(host, rq);
2560f602ec7SJarkko Lavinen }
2570f602ec7SJarkko Lavinen 
2580807a9b5SJarkko Lavinen static void mmc_omap_release_slot(struct mmc_omap_slot *slot, int clk_enabled)
259abfbe5f7SJuha Yrjola {
260abfbe5f7SJuha Yrjola 	struct mmc_omap_host *host = slot->host;
261abfbe5f7SJuha Yrjola 	unsigned long flags;
262abfbe5f7SJuha Yrjola 	int i;
263abfbe5f7SJuha Yrjola 
264abfbe5f7SJuha Yrjola 	BUG_ON(slot == NULL || host->mmc == NULL);
2650807a9b5SJarkko Lavinen 
2660807a9b5SJarkko Lavinen 	if (clk_enabled)
2670807a9b5SJarkko Lavinen 		/* Keeps clock running for at least 8 cycles on valid freq */
2680807a9b5SJarkko Lavinen 		mod_timer(&host->clk_timer, jiffies  + HZ/10);
2690807a9b5SJarkko Lavinen 	else {
2700807a9b5SJarkko Lavinen 		del_timer(&host->clk_timer);
2710807a9b5SJarkko Lavinen 		mmc_omap_fclk_offdelay(slot);
2720807a9b5SJarkko Lavinen 		mmc_omap_fclk_enable(host, 0);
2730807a9b5SJarkko Lavinen 	}
274abfbe5f7SJuha Yrjola 
275abfbe5f7SJuha Yrjola 	spin_lock_irqsave(&host->slot_lock, flags);
276abfbe5f7SJuha Yrjola 	/* Check for any pending requests */
277abfbe5f7SJuha Yrjola 	for (i = 0; i < host->nr_slots; i++) {
278abfbe5f7SJuha Yrjola 		struct mmc_omap_slot *new_slot;
279abfbe5f7SJuha Yrjola 
280abfbe5f7SJuha Yrjola 		if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
281abfbe5f7SJuha Yrjola 			continue;
282abfbe5f7SJuha Yrjola 
2830f602ec7SJarkko Lavinen 		BUG_ON(host->next_slot != NULL);
284abfbe5f7SJuha Yrjola 		new_slot = host->slots[i];
285abfbe5f7SJuha Yrjola 		/* The current slot should not have a request in queue */
286abfbe5f7SJuha Yrjola 		BUG_ON(new_slot == host->current_slot);
287abfbe5f7SJuha Yrjola 
2880f602ec7SJarkko Lavinen 		host->next_slot = new_slot;
289abfbe5f7SJuha Yrjola 		host->mmc = new_slot->mmc;
290abfbe5f7SJuha Yrjola 		spin_unlock_irqrestore(&host->slot_lock, flags);
291b01a4f1cSVenkatraman S 		queue_work(host->mmc_omap_wq, &host->slot_release_work);
292abfbe5f7SJuha Yrjola 		return;
293abfbe5f7SJuha Yrjola 	}
294abfbe5f7SJuha Yrjola 
295abfbe5f7SJuha Yrjola 	host->mmc = NULL;
296abfbe5f7SJuha Yrjola 	wake_up(&host->slot_wq);
297abfbe5f7SJuha Yrjola 	spin_unlock_irqrestore(&host->slot_lock, flags);
298abfbe5f7SJuha Yrjola }
299abfbe5f7SJuha Yrjola 
3005a0f3f1fSJuha Yrjola static inline
3015a0f3f1fSJuha Yrjola int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
3025a0f3f1fSJuha Yrjola {
3038348f002SKyungmin Park 	if (slot->pdata->get_cover_state)
3048348f002SKyungmin Park 		return slot->pdata->get_cover_state(mmc_dev(slot->mmc),
3058348f002SKyungmin Park 						    slot->id);
3068348f002SKyungmin Park 	return 0;
3075a0f3f1fSJuha Yrjola }
3085a0f3f1fSJuha Yrjola 
3095a0f3f1fSJuha Yrjola static ssize_t
3105a0f3f1fSJuha Yrjola mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
3115a0f3f1fSJuha Yrjola 			   char *buf)
3125a0f3f1fSJuha Yrjola {
3135a0f3f1fSJuha Yrjola 	struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
3145a0f3f1fSJuha Yrjola 	struct mmc_omap_slot *slot = mmc_priv(mmc);
3155a0f3f1fSJuha Yrjola 
3165a0f3f1fSJuha Yrjola 	return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
3175a0f3f1fSJuha Yrjola 		       "closed");
3185a0f3f1fSJuha Yrjola }
3195a0f3f1fSJuha Yrjola 
3205a0f3f1fSJuha Yrjola static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
3215a0f3f1fSJuha Yrjola 
322abfbe5f7SJuha Yrjola static ssize_t
323abfbe5f7SJuha Yrjola mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
324abfbe5f7SJuha Yrjola 			char *buf)
325abfbe5f7SJuha Yrjola {
326abfbe5f7SJuha Yrjola 	struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
327abfbe5f7SJuha Yrjola 	struct mmc_omap_slot *slot = mmc_priv(mmc);
328abfbe5f7SJuha Yrjola 
329abfbe5f7SJuha Yrjola 	return sprintf(buf, "%s\n", slot->pdata->name);
330abfbe5f7SJuha Yrjola }
331abfbe5f7SJuha Yrjola 
332abfbe5f7SJuha Yrjola static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
333abfbe5f7SJuha Yrjola 
3341c6a0718SPierre Ossman static void
3351c6a0718SPierre Ossman mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
3361c6a0718SPierre Ossman {
3371c6a0718SPierre Ossman 	u32 cmdreg;
3381c6a0718SPierre Ossman 	u32 resptype;
3391c6a0718SPierre Ossman 	u32 cmdtype;
340*b13d1f0fSJarkko Nikula 	u16 irq_mask;
3411c6a0718SPierre Ossman 
3421c6a0718SPierre Ossman 	host->cmd = cmd;
3431c6a0718SPierre Ossman 
3441c6a0718SPierre Ossman 	resptype = 0;
3451c6a0718SPierre Ossman 	cmdtype = 0;
3461c6a0718SPierre Ossman 
3471c6a0718SPierre Ossman 	/* Our hardware needs to know exact type */
3481c6a0718SPierre Ossman 	switch (mmc_resp_type(cmd)) {
3491c6a0718SPierre Ossman 	case MMC_RSP_NONE:
3501c6a0718SPierre Ossman 		break;
3511c6a0718SPierre Ossman 	case MMC_RSP_R1:
3521c6a0718SPierre Ossman 	case MMC_RSP_R1B:
3531c6a0718SPierre Ossman 		/* resp 1, 1b, 6, 7 */
3541c6a0718SPierre Ossman 		resptype = 1;
3551c6a0718SPierre Ossman 		break;
3561c6a0718SPierre Ossman 	case MMC_RSP_R2:
3571c6a0718SPierre Ossman 		resptype = 2;
3581c6a0718SPierre Ossman 		break;
3591c6a0718SPierre Ossman 	case MMC_RSP_R3:
3601c6a0718SPierre Ossman 		resptype = 3;
3611c6a0718SPierre Ossman 		break;
3621c6a0718SPierre Ossman 	default:
3631c6a0718SPierre Ossman 		dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
3641c6a0718SPierre Ossman 		break;
3651c6a0718SPierre Ossman 	}
3661c6a0718SPierre Ossman 
3671c6a0718SPierre Ossman 	if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
3681c6a0718SPierre Ossman 		cmdtype = OMAP_MMC_CMDTYPE_ADTC;
3691c6a0718SPierre Ossman 	} else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
3701c6a0718SPierre Ossman 		cmdtype = OMAP_MMC_CMDTYPE_BC;
3711c6a0718SPierre Ossman 	} else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
3721c6a0718SPierre Ossman 		cmdtype = OMAP_MMC_CMDTYPE_BCR;
3731c6a0718SPierre Ossman 	} else {
3741c6a0718SPierre Ossman 		cmdtype = OMAP_MMC_CMDTYPE_AC;
3751c6a0718SPierre Ossman 	}
3761c6a0718SPierre Ossman 
3771c6a0718SPierre Ossman 	cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
3781c6a0718SPierre Ossman 
379abfbe5f7SJuha Yrjola 	if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
3801c6a0718SPierre Ossman 		cmdreg |= 1 << 6;
3811c6a0718SPierre Ossman 
3821c6a0718SPierre Ossman 	if (cmd->flags & MMC_RSP_BUSY)
3831c6a0718SPierre Ossman 		cmdreg |= 1 << 11;
3841c6a0718SPierre Ossman 
3851c6a0718SPierre Ossman 	if (host->data && !(host->data->flags & MMC_DATA_WRITE))
3861c6a0718SPierre Ossman 		cmdreg |= 1 << 15;
3871c6a0718SPierre Ossman 
3880fb4723dSJarkko Lavinen 	mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
389eb1860bcSJarkko Lavinen 
3901c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, CTO, 200);
3911c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
3921c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
393*b13d1f0fSJarkko Nikula 	irq_mask = OMAP_MMC_STAT_A_EMPTY    | OMAP_MMC_STAT_A_FULL    |
3941c6a0718SPierre Ossman 		   OMAP_MMC_STAT_CMD_CRC    | OMAP_MMC_STAT_CMD_TOUT  |
3951c6a0718SPierre Ossman 		   OMAP_MMC_STAT_DATA_CRC   | OMAP_MMC_STAT_DATA_TOUT |
3961c6a0718SPierre Ossman 		   OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR  |
397*b13d1f0fSJarkko Nikula 		   OMAP_MMC_STAT_END_OF_DATA;
398*b13d1f0fSJarkko Nikula 	if (cmd->opcode == MMC_ERASE)
399*b13d1f0fSJarkko Nikula 		irq_mask &= ~OMAP_MMC_STAT_DATA_TOUT;
400*b13d1f0fSJarkko Nikula 	OMAP_MMC_WRITE(host, IE, irq_mask);
4011c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, CMD, cmdreg);
4021c6a0718SPierre Ossman }
4031c6a0718SPierre Ossman 
4041c6a0718SPierre Ossman static void
405a914ded2SJuha Yrjola mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
406a914ded2SJuha Yrjola 		     int abort)
4071c6a0718SPierre Ossman {
4081c6a0718SPierre Ossman 	enum dma_data_direction dma_data_dir;
4093451c067SRussell King 	struct device *dev = mmc_dev(host->mmc);
4103451c067SRussell King 	struct dma_chan *c;
4111c6a0718SPierre Ossman 
4123451c067SRussell King 	if (data->flags & MMC_DATA_WRITE) {
4131c6a0718SPierre Ossman 		dma_data_dir = DMA_TO_DEVICE;
4143451c067SRussell King 		c = host->dma_tx;
4153451c067SRussell King 	} else {
4161c6a0718SPierre Ossman 		dma_data_dir = DMA_FROM_DEVICE;
4173451c067SRussell King 		c = host->dma_rx;
4183451c067SRussell King 	}
4193451c067SRussell King 	if (c) {
4203451c067SRussell King 		if (data->error) {
4213451c067SRussell King 			dmaengine_terminate_all(c);
4223451c067SRussell King 			/* Claim nothing transferred on error... */
4233451c067SRussell King 			data->bytes_xfered = 0;
4243451c067SRussell King 		}
4253451c067SRussell King 		dev = c->device->dev;
4263451c067SRussell King 	}
4273451c067SRussell King 	dma_unmap_sg(dev, data->sg, host->sg_len, dma_data_dir);
4281c6a0718SPierre Ossman }
429a914ded2SJuha Yrjola 
4300f602ec7SJarkko Lavinen static void mmc_omap_send_stop_work(struct work_struct *work)
4310f602ec7SJarkko Lavinen {
4320f602ec7SJarkko Lavinen 	struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
4330f602ec7SJarkko Lavinen 						  send_stop_work);
4340f602ec7SJarkko Lavinen 	struct mmc_omap_slot *slot = host->current_slot;
4350f602ec7SJarkko Lavinen 	struct mmc_data *data = host->stop_data;
4360f602ec7SJarkko Lavinen 	unsigned long tick_ns;
4370f602ec7SJarkko Lavinen 
4380f602ec7SJarkko Lavinen 	tick_ns = (1000000000 + slot->fclk_freq - 1)/slot->fclk_freq;
4390f602ec7SJarkko Lavinen 	ndelay(8*tick_ns);
4400f602ec7SJarkko Lavinen 
4410f602ec7SJarkko Lavinen 	mmc_omap_start_command(host, data->stop);
4420f602ec7SJarkko Lavinen }
4430f602ec7SJarkko Lavinen 
444a914ded2SJuha Yrjola static void
445a914ded2SJuha Yrjola mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
446a914ded2SJuha Yrjola {
447a914ded2SJuha Yrjola 	if (host->dma_in_use)
448a914ded2SJuha Yrjola 		mmc_omap_release_dma(host, data, data->error);
449a914ded2SJuha Yrjola 
4501c6a0718SPierre Ossman 	host->data = NULL;
4511c6a0718SPierre Ossman 	host->sg_len = 0;
4521c6a0718SPierre Ossman 
4531c6a0718SPierre Ossman 	/* NOTE:  MMC layer will sometimes poll-wait CMD13 next, issuing
4541c6a0718SPierre Ossman 	 * dozens of requests until the card finishes writing data.
4551c6a0718SPierre Ossman 	 * It'd be cheaper to just wait till an EOFB interrupt arrives...
4561c6a0718SPierre Ossman 	 */
4571c6a0718SPierre Ossman 
4581c6a0718SPierre Ossman 	if (!data->stop) {
459a914ded2SJuha Yrjola 		struct mmc_host *mmc;
460a914ded2SJuha Yrjola 
4611c6a0718SPierre Ossman 		host->mrq = NULL;
462a914ded2SJuha Yrjola 		mmc = host->mmc;
4630807a9b5SJarkko Lavinen 		mmc_omap_release_slot(host->current_slot, 1);
464a914ded2SJuha Yrjola 		mmc_request_done(mmc, data->mrq);
4651c6a0718SPierre Ossman 		return;
4661c6a0718SPierre Ossman 	}
4671c6a0718SPierre Ossman 
4680f602ec7SJarkko Lavinen 	host->stop_data = data;
469b01a4f1cSVenkatraman S 	queue_work(host->mmc_omap_wq, &host->send_stop_work);
4701c6a0718SPierre Ossman }
4711c6a0718SPierre Ossman 
4721c6a0718SPierre Ossman static void
4730fb4723dSJarkko Lavinen mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
474eb1860bcSJarkko Lavinen {
475eb1860bcSJarkko Lavinen 	struct mmc_omap_slot *slot = host->current_slot;
476eb1860bcSJarkko Lavinen 	unsigned int restarts, passes, timeout;
477eb1860bcSJarkko Lavinen 	u16 stat = 0;
478eb1860bcSJarkko Lavinen 
479eb1860bcSJarkko Lavinen 	/* Sending abort takes 80 clocks. Have some extra and round up */
480eb1860bcSJarkko Lavinen 	timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
481eb1860bcSJarkko Lavinen 	restarts = 0;
4820fb4723dSJarkko Lavinen 	while (restarts < maxloops) {
483eb1860bcSJarkko Lavinen 		OMAP_MMC_WRITE(host, STAT, 0xFFFF);
484eb1860bcSJarkko Lavinen 		OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
485eb1860bcSJarkko Lavinen 
486eb1860bcSJarkko Lavinen 		passes = 0;
487eb1860bcSJarkko Lavinen 		while (passes < timeout) {
488eb1860bcSJarkko Lavinen 			stat = OMAP_MMC_READ(host, STAT);
489eb1860bcSJarkko Lavinen 			if (stat & OMAP_MMC_STAT_END_OF_CMD)
490eb1860bcSJarkko Lavinen 				goto out;
491eb1860bcSJarkko Lavinen 			udelay(1);
492eb1860bcSJarkko Lavinen 			passes++;
493eb1860bcSJarkko Lavinen 		}
494eb1860bcSJarkko Lavinen 
495eb1860bcSJarkko Lavinen 		restarts++;
496eb1860bcSJarkko Lavinen 	}
497eb1860bcSJarkko Lavinen out:
498eb1860bcSJarkko Lavinen 	OMAP_MMC_WRITE(host, STAT, stat);
499eb1860bcSJarkko Lavinen }
500eb1860bcSJarkko Lavinen 
501eb1860bcSJarkko Lavinen static void
502a914ded2SJuha Yrjola mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
503a914ded2SJuha Yrjola {
504a914ded2SJuha Yrjola 	if (host->dma_in_use)
505a914ded2SJuha Yrjola 		mmc_omap_release_dma(host, data, 1);
506a914ded2SJuha Yrjola 
507a914ded2SJuha Yrjola 	host->data = NULL;
508a914ded2SJuha Yrjola 	host->sg_len = 0;
509a914ded2SJuha Yrjola 
5100fb4723dSJarkko Lavinen 	mmc_omap_send_abort(host, 10000);
511a914ded2SJuha Yrjola }
512a914ded2SJuha Yrjola 
513a914ded2SJuha Yrjola static void
5141c6a0718SPierre Ossman mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
5151c6a0718SPierre Ossman {
5161c6a0718SPierre Ossman 	unsigned long flags;
5171c6a0718SPierre Ossman 	int done;
5181c6a0718SPierre Ossman 
5191c6a0718SPierre Ossman 	if (!host->dma_in_use) {
5201c6a0718SPierre Ossman 		mmc_omap_xfer_done(host, data);
5211c6a0718SPierre Ossman 		return;
5221c6a0718SPierre Ossman 	}
5231c6a0718SPierre Ossman 	done = 0;
5241c6a0718SPierre Ossman 	spin_lock_irqsave(&host->dma_lock, flags);
5251c6a0718SPierre Ossman 	if (host->dma_done)
5261c6a0718SPierre Ossman 		done = 1;
5271c6a0718SPierre Ossman 	else
5281c6a0718SPierre Ossman 		host->brs_received = 1;
5291c6a0718SPierre Ossman 	spin_unlock_irqrestore(&host->dma_lock, flags);
5301c6a0718SPierre Ossman 	if (done)
5311c6a0718SPierre Ossman 		mmc_omap_xfer_done(host, data);
5321c6a0718SPierre Ossman }
5331c6a0718SPierre Ossman 
5341c6a0718SPierre Ossman static void
5351c6a0718SPierre Ossman mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
5361c6a0718SPierre Ossman {
5371c6a0718SPierre Ossman 	unsigned long flags;
5381c6a0718SPierre Ossman 	int done;
5391c6a0718SPierre Ossman 
5401c6a0718SPierre Ossman 	done = 0;
5411c6a0718SPierre Ossman 	spin_lock_irqsave(&host->dma_lock, flags);
5421c6a0718SPierre Ossman 	if (host->brs_received)
5431c6a0718SPierre Ossman 		done = 1;
5441c6a0718SPierre Ossman 	else
5451c6a0718SPierre Ossman 		host->dma_done = 1;
5461c6a0718SPierre Ossman 	spin_unlock_irqrestore(&host->dma_lock, flags);
5471c6a0718SPierre Ossman 	if (done)
5481c6a0718SPierre Ossman 		mmc_omap_xfer_done(host, data);
5491c6a0718SPierre Ossman }
5501c6a0718SPierre Ossman 
5511c6a0718SPierre Ossman static void
5521c6a0718SPierre Ossman mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
5531c6a0718SPierre Ossman {
5541c6a0718SPierre Ossman 	host->cmd = NULL;
5551c6a0718SPierre Ossman 
5560fb4723dSJarkko Lavinen 	del_timer(&host->cmd_abort_timer);
557eb1860bcSJarkko Lavinen 
5581c6a0718SPierre Ossman 	if (cmd->flags & MMC_RSP_PRESENT) {
5591c6a0718SPierre Ossman 		if (cmd->flags & MMC_RSP_136) {
5601c6a0718SPierre Ossman 			/* response type 2 */
5611c6a0718SPierre Ossman 			cmd->resp[3] =
5621c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP0) |
5631c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP1) << 16);
5641c6a0718SPierre Ossman 			cmd->resp[2] =
5651c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP2) |
5661c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP3) << 16);
5671c6a0718SPierre Ossman 			cmd->resp[1] =
5681c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP4) |
5691c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP5) << 16);
5701c6a0718SPierre Ossman 			cmd->resp[0] =
5711c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP6) |
5721c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP7) << 16);
5731c6a0718SPierre Ossman 		} else {
5741c6a0718SPierre Ossman 			/* response types 1, 1b, 3, 4, 5, 6 */
5751c6a0718SPierre Ossman 			cmd->resp[0] =
5761c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP6) |
5771c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP7) << 16);
5781c6a0718SPierre Ossman 		}
5791c6a0718SPierre Ossman 	}
5801c6a0718SPierre Ossman 
58117b0429dSPierre Ossman 	if (host->data == NULL || cmd->error) {
582a914ded2SJuha Yrjola 		struct mmc_host *mmc;
583a914ded2SJuha Yrjola 
584a914ded2SJuha Yrjola 		if (host->data != NULL)
585a914ded2SJuha Yrjola 			mmc_omap_abort_xfer(host, host->data);
5861c6a0718SPierre Ossman 		host->mrq = NULL;
587a914ded2SJuha Yrjola 		mmc = host->mmc;
5880807a9b5SJarkko Lavinen 		mmc_omap_release_slot(host->current_slot, 1);
589a914ded2SJuha Yrjola 		mmc_request_done(mmc, cmd->mrq);
5901c6a0718SPierre Ossman 	}
5911c6a0718SPierre Ossman }
5921c6a0718SPierre Ossman 
593eb1860bcSJarkko Lavinen /*
594eb1860bcSJarkko Lavinen  * Abort stuck command. Can occur when card is removed while it is being
595eb1860bcSJarkko Lavinen  * read.
596eb1860bcSJarkko Lavinen  */
597eb1860bcSJarkko Lavinen static void mmc_omap_abort_command(struct work_struct *work)
598eb1860bcSJarkko Lavinen {
599eb1860bcSJarkko Lavinen 	struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
6000fb4723dSJarkko Lavinen 						  cmd_abort_work);
6010fb4723dSJarkko Lavinen 	BUG_ON(!host->cmd);
602eb1860bcSJarkko Lavinen 
603eb1860bcSJarkko Lavinen 	dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
604eb1860bcSJarkko Lavinen 		host->cmd->opcode);
605eb1860bcSJarkko Lavinen 
6060fb4723dSJarkko Lavinen 	if (host->cmd->error == 0)
607eb1860bcSJarkko Lavinen 		host->cmd->error = -ETIMEDOUT;
6080fb4723dSJarkko Lavinen 
6090fb4723dSJarkko Lavinen 	if (host->data == NULL) {
6100fb4723dSJarkko Lavinen 		struct mmc_command *cmd;
6110fb4723dSJarkko Lavinen 		struct mmc_host    *mmc;
6120fb4723dSJarkko Lavinen 
6130fb4723dSJarkko Lavinen 		cmd = host->cmd;
6140fb4723dSJarkko Lavinen 		host->cmd = NULL;
6150fb4723dSJarkko Lavinen 		mmc_omap_send_abort(host, 10000);
6160fb4723dSJarkko Lavinen 
6170fb4723dSJarkko Lavinen 		host->mrq = NULL;
6180fb4723dSJarkko Lavinen 		mmc = host->mmc;
6190807a9b5SJarkko Lavinen 		mmc_omap_release_slot(host->current_slot, 1);
6200fb4723dSJarkko Lavinen 		mmc_request_done(mmc, cmd->mrq);
6210fb4723dSJarkko Lavinen 	} else
622eb1860bcSJarkko Lavinen 		mmc_omap_cmd_done(host, host->cmd);
6230fb4723dSJarkko Lavinen 
6240fb4723dSJarkko Lavinen 	host->abort = 0;
6250fb4723dSJarkko Lavinen 	enable_irq(host->irq);
626eb1860bcSJarkko Lavinen }
627eb1860bcSJarkko Lavinen 
628eb1860bcSJarkko Lavinen static void
629eb1860bcSJarkko Lavinen mmc_omap_cmd_timer(unsigned long data)
630eb1860bcSJarkko Lavinen {
631eb1860bcSJarkko Lavinen 	struct mmc_omap_host *host = (struct mmc_omap_host *) data;
6320fb4723dSJarkko Lavinen 	unsigned long flags;
633eb1860bcSJarkko Lavinen 
6340fb4723dSJarkko Lavinen 	spin_lock_irqsave(&host->slot_lock, flags);
6350fb4723dSJarkko Lavinen 	if (host->cmd != NULL && !host->abort) {
6360fb4723dSJarkko Lavinen 		OMAP_MMC_WRITE(host, IE, 0);
6370fb4723dSJarkko Lavinen 		disable_irq(host->irq);
6380fb4723dSJarkko Lavinen 		host->abort = 1;
639b01a4f1cSVenkatraman S 		queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
6400fb4723dSJarkko Lavinen 	}
6410fb4723dSJarkko Lavinen 	spin_unlock_irqrestore(&host->slot_lock, flags);
642eb1860bcSJarkko Lavinen }
643eb1860bcSJarkko Lavinen 
6441c6a0718SPierre Ossman /* PIO only */
6451c6a0718SPierre Ossman static void
6461c6a0718SPierre Ossman mmc_omap_sg_to_buf(struct mmc_omap_host *host)
6471c6a0718SPierre Ossman {
6481c6a0718SPierre Ossman 	struct scatterlist *sg;
6491c6a0718SPierre Ossman 
6501c6a0718SPierre Ossman 	sg = host->data->sg + host->sg_idx;
6511c6a0718SPierre Ossman 	host->buffer_bytes_left = sg->length;
65245711f1aSJens Axboe 	host->buffer = sg_virt(sg);
6531c6a0718SPierre Ossman 	if (host->buffer_bytes_left > host->total_bytes_left)
6541c6a0718SPierre Ossman 		host->buffer_bytes_left = host->total_bytes_left;
6551c6a0718SPierre Ossman }
6561c6a0718SPierre Ossman 
6570807a9b5SJarkko Lavinen static void
6580807a9b5SJarkko Lavinen mmc_omap_clk_timer(unsigned long data)
6590807a9b5SJarkko Lavinen {
6600807a9b5SJarkko Lavinen 	struct mmc_omap_host *host = (struct mmc_omap_host *) data;
6610807a9b5SJarkko Lavinen 
6620807a9b5SJarkko Lavinen 	mmc_omap_fclk_enable(host, 0);
6630807a9b5SJarkko Lavinen }
6640807a9b5SJarkko Lavinen 
6651c6a0718SPierre Ossman /* PIO only */
6661c6a0718SPierre Ossman static void
6671c6a0718SPierre Ossman mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
6681c6a0718SPierre Ossman {
66975b53aeeSPaul Walmsley 	int n, nwords;
6701c6a0718SPierre Ossman 
6711c6a0718SPierre Ossman 	if (host->buffer_bytes_left == 0) {
6721c6a0718SPierre Ossman 		host->sg_idx++;
6731c6a0718SPierre Ossman 		BUG_ON(host->sg_idx == host->sg_len);
6741c6a0718SPierre Ossman 		mmc_omap_sg_to_buf(host);
6751c6a0718SPierre Ossman 	}
6761c6a0718SPierre Ossman 	n = 64;
6771c6a0718SPierre Ossman 	if (n > host->buffer_bytes_left)
6781c6a0718SPierre Ossman 		n = host->buffer_bytes_left;
67975b53aeeSPaul Walmsley 
68075b53aeeSPaul Walmsley 	nwords = n / 2;
68175b53aeeSPaul Walmsley 	nwords += n & 1; /* handle odd number of bytes to transfer */
68275b53aeeSPaul Walmsley 
6831c6a0718SPierre Ossman 	host->buffer_bytes_left -= n;
6841c6a0718SPierre Ossman 	host->total_bytes_left -= n;
6851c6a0718SPierre Ossman 	host->data->bytes_xfered += n;
6861c6a0718SPierre Ossman 
6871c6a0718SPierre Ossman 	if (write) {
68875b53aeeSPaul Walmsley 		__raw_writesw(host->virt_base + OMAP_MMC_REG(host, DATA),
68975b53aeeSPaul Walmsley 			      host->buffer, nwords);
6901c6a0718SPierre Ossman 	} else {
69175b53aeeSPaul Walmsley 		__raw_readsw(host->virt_base + OMAP_MMC_REG(host, DATA),
69275b53aeeSPaul Walmsley 			     host->buffer, nwords);
6931c6a0718SPierre Ossman 	}
69475b53aeeSPaul Walmsley 
69575b53aeeSPaul Walmsley 	host->buffer += nwords;
6961c6a0718SPierre Ossman }
6971c6a0718SPierre Ossman 
69875d569d3SVenkatraman S #ifdef CONFIG_MMC_DEBUG
69975d569d3SVenkatraman S static void mmc_omap_report_irq(struct mmc_omap_host *host, u16 status)
7001c6a0718SPierre Ossman {
7011c6a0718SPierre Ossman 	static const char *mmc_omap_status_bits[] = {
7021c6a0718SPierre Ossman 		"EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
7031c6a0718SPierre Ossman 		"CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
7041c6a0718SPierre Ossman 	};
70575d569d3SVenkatraman S 	int i;
70675d569d3SVenkatraman S 	char res[64], *buf = res;
70775d569d3SVenkatraman S 
70875d569d3SVenkatraman S 	buf += sprintf(buf, "MMC IRQ 0x%x:", status);
7091c6a0718SPierre Ossman 
7101c6a0718SPierre Ossman 	for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
71175d569d3SVenkatraman S 		if (status & (1 << i))
71275d569d3SVenkatraman S 			buf += sprintf(buf, " %s", mmc_omap_status_bits[i]);
71375d569d3SVenkatraman S 	dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
7141c6a0718SPierre Ossman }
71575d569d3SVenkatraman S #else
71675d569d3SVenkatraman S static void mmc_omap_report_irq(struct mmc_omap_host *host, u16 status)
71775d569d3SVenkatraman S {
7181c6a0718SPierre Ossman }
71975d569d3SVenkatraman S #endif
72075d569d3SVenkatraman S 
7211c6a0718SPierre Ossman 
7221c6a0718SPierre Ossman static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
7231c6a0718SPierre Ossman {
7241c6a0718SPierre Ossman 	struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
7251c6a0718SPierre Ossman 	u16 status;
7261c6a0718SPierre Ossman 	int end_command;
7271c6a0718SPierre Ossman 	int end_transfer;
7282a50b888SJuha Yrjola 	int transfer_error, cmd_error;
7291c6a0718SPierre Ossman 
7301c6a0718SPierre Ossman 	if (host->cmd == NULL && host->data == NULL) {
7311c6a0718SPierre Ossman 		status = OMAP_MMC_READ(host, STAT);
7322a50b888SJuha Yrjola 		dev_info(mmc_dev(host->slots[0]->mmc),
7332a50b888SJuha Yrjola 			 "Spurious IRQ 0x%04x\n", status);
7341c6a0718SPierre Ossman 		if (status != 0) {
7351c6a0718SPierre Ossman 			OMAP_MMC_WRITE(host, STAT, status);
7361c6a0718SPierre Ossman 			OMAP_MMC_WRITE(host, IE, 0);
7371c6a0718SPierre Ossman 		}
7381c6a0718SPierre Ossman 		return IRQ_HANDLED;
7391c6a0718SPierre Ossman 	}
7401c6a0718SPierre Ossman 
7411c6a0718SPierre Ossman 	end_command = 0;
7421c6a0718SPierre Ossman 	end_transfer = 0;
7431c6a0718SPierre Ossman 	transfer_error = 0;
7442a50b888SJuha Yrjola 	cmd_error = 0;
7451c6a0718SPierre Ossman 
7461c6a0718SPierre Ossman 	while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
7472a50b888SJuha Yrjola 		int cmd;
7482a50b888SJuha Yrjola 
7491c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, STAT, status);
7502a50b888SJuha Yrjola 		if (host->cmd != NULL)
7512a50b888SJuha Yrjola 			cmd = host->cmd->opcode;
7522a50b888SJuha Yrjola 		else
7532a50b888SJuha Yrjola 			cmd = -1;
7541c6a0718SPierre Ossman 		dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
7552a50b888SJuha Yrjola 			status, cmd);
75675d569d3SVenkatraman S 		mmc_omap_report_irq(host, status);
75775d569d3SVenkatraman S 
7581c6a0718SPierre Ossman 		if (host->total_bytes_left) {
7591c6a0718SPierre Ossman 			if ((status & OMAP_MMC_STAT_A_FULL) ||
7601c6a0718SPierre Ossman 			    (status & OMAP_MMC_STAT_END_OF_DATA))
7611c6a0718SPierre Ossman 				mmc_omap_xfer_data(host, 0);
7621c6a0718SPierre Ossman 			if (status & OMAP_MMC_STAT_A_EMPTY)
7631c6a0718SPierre Ossman 				mmc_omap_xfer_data(host, 1);
7641c6a0718SPierre Ossman 		}
7651c6a0718SPierre Ossman 
7662a50b888SJuha Yrjola 		if (status & OMAP_MMC_STAT_END_OF_DATA)
7671c6a0718SPierre Ossman 			end_transfer = 1;
7681c6a0718SPierre Ossman 
7691c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_DATA_TOUT) {
7702a50b888SJuha Yrjola 			dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
7712a50b888SJuha Yrjola 				cmd);
7721c6a0718SPierre Ossman 			if (host->data) {
77317b0429dSPierre Ossman 				host->data->error = -ETIMEDOUT;
7741c6a0718SPierre Ossman 				transfer_error = 1;
7751c6a0718SPierre Ossman 			}
7761c6a0718SPierre Ossman 		}
7771c6a0718SPierre Ossman 
7781c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_DATA_CRC) {
7791c6a0718SPierre Ossman 			if (host->data) {
78017b0429dSPierre Ossman 				host->data->error = -EILSEQ;
7811c6a0718SPierre Ossman 				dev_dbg(mmc_dev(host->mmc),
7821c6a0718SPierre Ossman 					 "data CRC error, bytes left %d\n",
7831c6a0718SPierre Ossman 					host->total_bytes_left);
7841c6a0718SPierre Ossman 				transfer_error = 1;
7851c6a0718SPierre Ossman 			} else {
7861c6a0718SPierre Ossman 				dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
7871c6a0718SPierre Ossman 			}
7881c6a0718SPierre Ossman 		}
7891c6a0718SPierre Ossman 
7901c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_CMD_TOUT) {
7911c6a0718SPierre Ossman 			/* Timeouts are routine with some commands */
7921c6a0718SPierre Ossman 			if (host->cmd) {
793abfbe5f7SJuha Yrjola 				struct mmc_omap_slot *slot =
794abfbe5f7SJuha Yrjola 					host->current_slot;
7952a50b888SJuha Yrjola 				if (slot == NULL ||
7962a50b888SJuha Yrjola 				    !mmc_omap_cover_is_open(slot))
7971c6a0718SPierre Ossman 					dev_err(mmc_dev(host->mmc),
7982a50b888SJuha Yrjola 						"command timeout (CMD%d)\n",
7992a50b888SJuha Yrjola 						cmd);
80017b0429dSPierre Ossman 				host->cmd->error = -ETIMEDOUT;
8011c6a0718SPierre Ossman 				end_command = 1;
8022a50b888SJuha Yrjola 				cmd_error = 1;
8031c6a0718SPierre Ossman 			}
8041c6a0718SPierre Ossman 		}
8051c6a0718SPierre Ossman 
8061c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_CMD_CRC) {
8071c6a0718SPierre Ossman 			if (host->cmd) {
8081c6a0718SPierre Ossman 				dev_err(mmc_dev(host->mmc),
8091c6a0718SPierre Ossman 					"command CRC error (CMD%d, arg 0x%08x)\n",
8102a50b888SJuha Yrjola 					cmd, host->cmd->arg);
81117b0429dSPierre Ossman 				host->cmd->error = -EILSEQ;
8121c6a0718SPierre Ossman 				end_command = 1;
8132a50b888SJuha Yrjola 				cmd_error = 1;
8141c6a0718SPierre Ossman 			} else
8151c6a0718SPierre Ossman 				dev_err(mmc_dev(host->mmc),
8161c6a0718SPierre Ossman 					"command CRC error without cmd?\n");
8171c6a0718SPierre Ossman 		}
8181c6a0718SPierre Ossman 
8191c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_CARD_ERR) {
8200107a4b3SRagner Magalhaes 			dev_dbg(mmc_dev(host->mmc),
8210107a4b3SRagner Magalhaes 				"ignoring card status error (CMD%d)\n",
8222a50b888SJuha Yrjola 				cmd);
8231c6a0718SPierre Ossman 			end_command = 1;
8241c6a0718SPierre Ossman 		}
8251c6a0718SPierre Ossman 
8261c6a0718SPierre Ossman 		/*
8271c6a0718SPierre Ossman 		 * NOTE: On 1610 the END_OF_CMD may come too early when
8281c6a0718SPierre Ossman 		 * starting a write
8291c6a0718SPierre Ossman 		 */
8301c6a0718SPierre Ossman 		if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
8311c6a0718SPierre Ossman 		    (!(status & OMAP_MMC_STAT_A_EMPTY))) {
8321c6a0718SPierre Ossman 			end_command = 1;
8331c6a0718SPierre Ossman 		}
8341c6a0718SPierre Ossman 	}
8351c6a0718SPierre Ossman 
8360fb4723dSJarkko Lavinen 	if (cmd_error && host->data) {
8370fb4723dSJarkko Lavinen 		del_timer(&host->cmd_abort_timer);
8380fb4723dSJarkko Lavinen 		host->abort = 1;
8390fb4723dSJarkko Lavinen 		OMAP_MMC_WRITE(host, IE, 0);
840e749c6f2SBen Nizette 		disable_irq_nosync(host->irq);
841b01a4f1cSVenkatraman S 		queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
8420fb4723dSJarkko Lavinen 		return IRQ_HANDLED;
8430fb4723dSJarkko Lavinen 	}
8440fb4723dSJarkko Lavinen 
845f6947514SMichael Buesch 	if (end_command && host->cmd)
8461c6a0718SPierre Ossman 		mmc_omap_cmd_done(host, host->cmd);
8472a50b888SJuha Yrjola 	if (host->data != NULL) {
8481c6a0718SPierre Ossman 		if (transfer_error)
8491c6a0718SPierre Ossman 			mmc_omap_xfer_done(host, host->data);
8501c6a0718SPierre Ossman 		else if (end_transfer)
8511c6a0718SPierre Ossman 			mmc_omap_end_of_data(host, host->data);
8522a50b888SJuha Yrjola 	}
8531c6a0718SPierre Ossman 
8541c6a0718SPierre Ossman 	return IRQ_HANDLED;
8551c6a0718SPierre Ossman }
8561c6a0718SPierre Ossman 
8577584d276SJarkko Lavinen void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
8585a0f3f1fSJuha Yrjola {
8597584d276SJarkko Lavinen 	int cover_open;
8605a0f3f1fSJuha Yrjola 	struct mmc_omap_host *host = dev_get_drvdata(dev);
8617584d276SJarkko Lavinen 	struct mmc_omap_slot *slot = host->slots[num];
8625a0f3f1fSJuha Yrjola 
8637584d276SJarkko Lavinen 	BUG_ON(num >= host->nr_slots);
8645a0f3f1fSJuha Yrjola 
8655a0f3f1fSJuha Yrjola 	/* Other subsystems can call in here before we're initialised. */
8667584d276SJarkko Lavinen 	if (host->nr_slots == 0 || !host->slots[num])
8675a0f3f1fSJuha Yrjola 		return;
8685a0f3f1fSJuha Yrjola 
8695a0f3f1fSJuha Yrjola 	cover_open = mmc_omap_cover_is_open(slot);
8705a0f3f1fSJuha Yrjola 	if (cover_open != slot->cover_open) {
8715a0f3f1fSJuha Yrjola 		slot->cover_open = cover_open;
8727584d276SJarkko Lavinen 		sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
8735a0f3f1fSJuha Yrjola 	}
8747584d276SJarkko Lavinen 
8757584d276SJarkko Lavinen 	tasklet_hi_schedule(&slot->cover_tasklet);
8767584d276SJarkko Lavinen }
8777584d276SJarkko Lavinen 
8787584d276SJarkko Lavinen static void mmc_omap_cover_timer(unsigned long arg)
8797584d276SJarkko Lavinen {
8807584d276SJarkko Lavinen 	struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
8817584d276SJarkko Lavinen 	tasklet_schedule(&slot->cover_tasklet);
8827584d276SJarkko Lavinen }
8837584d276SJarkko Lavinen 
8847584d276SJarkko Lavinen static void mmc_omap_cover_handler(unsigned long param)
8857584d276SJarkko Lavinen {
8867584d276SJarkko Lavinen 	struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
8877584d276SJarkko Lavinen 	int cover_open = mmc_omap_cover_is_open(slot);
8887584d276SJarkko Lavinen 
8897584d276SJarkko Lavinen 	mmc_detect_change(slot->mmc, 0);
8907584d276SJarkko Lavinen 	if (!cover_open)
8917584d276SJarkko Lavinen 		return;
8927584d276SJarkko Lavinen 
8937584d276SJarkko Lavinen 	/*
8947584d276SJarkko Lavinen 	 * If no card is inserted, we postpone polling until
8957584d276SJarkko Lavinen 	 * the cover has been closed.
8967584d276SJarkko Lavinen 	 */
8977584d276SJarkko Lavinen 	if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
8987584d276SJarkko Lavinen 		return;
8997584d276SJarkko Lavinen 
9007584d276SJarkko Lavinen 	mod_timer(&slot->cover_timer,
9017584d276SJarkko Lavinen 		  jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
9025a0f3f1fSJuha Yrjola }
9035a0f3f1fSJuha Yrjola 
9043451c067SRussell King static void mmc_omap_dma_callback(void *priv)
9053451c067SRussell King {
9063451c067SRussell King 	struct mmc_omap_host *host = priv;
9073451c067SRussell King 	struct mmc_data *data = host->data;
9083451c067SRussell King 
9093451c067SRussell King 	/* If we got to the end of DMA, assume everything went well */
9103451c067SRussell King 	data->bytes_xfered += data->blocks * data->blksz;
9113451c067SRussell King 
9123451c067SRussell King 	mmc_omap_dma_done(host, data);
9133451c067SRussell King }
9143451c067SRussell King 
9151c6a0718SPierre Ossman static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
9161c6a0718SPierre Ossman {
9171c6a0718SPierre Ossman 	u16 reg;
9181c6a0718SPierre Ossman 
9191c6a0718SPierre Ossman 	reg = OMAP_MMC_READ(host, SDIO);
9201c6a0718SPierre Ossman 	reg &= ~(1 << 5);
9211c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, SDIO, reg);
9221c6a0718SPierre Ossman 	/* Set maximum timeout */
9231c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, CTO, 0xff);
9241c6a0718SPierre Ossman }
9251c6a0718SPierre Ossman 
9261c6a0718SPierre Ossman static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
9271c6a0718SPierre Ossman {
928b8f9f0e9SJuha Yrjola 	unsigned int timeout, cycle_ns;
9291c6a0718SPierre Ossman 	u16 reg;
9301c6a0718SPierre Ossman 
931b8f9f0e9SJuha Yrjola 	cycle_ns = 1000000000 / host->current_slot->fclk_freq;
932b8f9f0e9SJuha Yrjola 	timeout = req->data->timeout_ns / cycle_ns;
933b8f9f0e9SJuha Yrjola 	timeout += req->data->timeout_clks;
9341c6a0718SPierre Ossman 
9351c6a0718SPierre Ossman 	/* Check if we need to use timeout multiplier register */
9361c6a0718SPierre Ossman 	reg = OMAP_MMC_READ(host, SDIO);
9371c6a0718SPierre Ossman 	if (timeout > 0xffff) {
9381c6a0718SPierre Ossman 		reg |= (1 << 5);
9391c6a0718SPierre Ossman 		timeout /= 1024;
9401c6a0718SPierre Ossman 	} else
9411c6a0718SPierre Ossman 		reg &= ~(1 << 5);
9421c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, SDIO, reg);
9431c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, DTO, timeout);
9441c6a0718SPierre Ossman }
9451c6a0718SPierre Ossman 
9461c6a0718SPierre Ossman static void
9471c6a0718SPierre Ossman mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
9481c6a0718SPierre Ossman {
9491c6a0718SPierre Ossman 	struct mmc_data *data = req->data;
950a6c668fbSJarkko Nikula 	int i, use_dma = 1, block_size;
9511c6a0718SPierre Ossman 	unsigned sg_len;
9521c6a0718SPierre Ossman 
9531c6a0718SPierre Ossman 	host->data = data;
9541c6a0718SPierre Ossman 	if (data == NULL) {
9551c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, BLEN, 0);
9561c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, NBLK, 0);
9571c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, BUF, 0);
9581c6a0718SPierre Ossman 		host->dma_in_use = 0;
9591c6a0718SPierre Ossman 		set_cmd_timeout(host, req);
9601c6a0718SPierre Ossman 		return;
9611c6a0718SPierre Ossman 	}
9621c6a0718SPierre Ossman 
9631c6a0718SPierre Ossman 	block_size = data->blksz;
9641c6a0718SPierre Ossman 
9651c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
9661c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, BLEN, block_size - 1);
9671c6a0718SPierre Ossman 	set_data_timeout(host, req);
9681c6a0718SPierre Ossman 
9691c6a0718SPierre Ossman 	/* cope with calling layer confusion; it issues "single
9701c6a0718SPierre Ossman 	 * block" writes using multi-block scatterlists.
9711c6a0718SPierre Ossman 	 */
9721c6a0718SPierre Ossman 	sg_len = (data->blocks == 1) ? 1 : data->sg_len;
9731c6a0718SPierre Ossman 
9741c6a0718SPierre Ossman 	/* Only do DMA for entire blocks */
9751c6a0718SPierre Ossman 	for (i = 0; i < sg_len; i++) {
9761c6a0718SPierre Ossman 		if ((data->sg[i].length % block_size) != 0) {
9771c6a0718SPierre Ossman 			use_dma = 0;
9781c6a0718SPierre Ossman 			break;
9791c6a0718SPierre Ossman 		}
9801c6a0718SPierre Ossman 	}
9811c6a0718SPierre Ossman 
9821c6a0718SPierre Ossman 	host->sg_idx = 0;
9831c6a0718SPierre Ossman 	if (use_dma) {
9843451c067SRussell King 		enum dma_data_direction dma_data_dir;
9853451c067SRussell King 		struct dma_async_tx_descriptor *tx;
9863451c067SRussell King 		struct dma_chan *c;
9873451c067SRussell King 		u32 burst, *bp;
9883451c067SRussell King 		u16 buf;
9893451c067SRussell King 
9903451c067SRussell King 		/*
9913451c067SRussell King 		 * FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx
9923451c067SRussell King 		 * and 24xx. Use 16 or 32 word frames when the
9933451c067SRussell King 		 * blocksize is at least that large. Blocksize is
9943451c067SRussell King 		 * usually 512 bytes; but not for some SD reads.
9953451c067SRussell King 		 */
99653db20d1STony Lindgren 		burst = mmc_omap15xx() ? 32 : 64;
9973451c067SRussell King 		if (burst > data->blksz)
9983451c067SRussell King 			burst = data->blksz;
9993451c067SRussell King 
10003451c067SRussell King 		burst >>= 1;
10013451c067SRussell King 
10023451c067SRussell King 		if (data->flags & MMC_DATA_WRITE) {
10033451c067SRussell King 			c = host->dma_tx;
10043451c067SRussell King 			bp = &host->dma_tx_burst;
10053451c067SRussell King 			buf = 0x0f80 | (burst - 1) << 0;
10063451c067SRussell King 			dma_data_dir = DMA_TO_DEVICE;
10073451c067SRussell King 		} else {
10083451c067SRussell King 			c = host->dma_rx;
10093451c067SRussell King 			bp = &host->dma_rx_burst;
10103451c067SRussell King 			buf = 0x800f | (burst - 1) << 8;
10113451c067SRussell King 			dma_data_dir = DMA_FROM_DEVICE;
10123451c067SRussell King 		}
10133451c067SRussell King 
10143451c067SRussell King 		if (!c)
10153451c067SRussell King 			goto use_pio;
10163451c067SRussell King 
10173451c067SRussell King 		/* Only reconfigure if we have a different burst size */
10183451c067SRussell King 		if (*bp != burst) {
10193451c067SRussell King 			struct dma_slave_config cfg;
10203451c067SRussell King 
10213451c067SRussell King 			cfg.src_addr = host->phys_base + OMAP_MMC_REG(host, DATA);
10223451c067SRussell King 			cfg.dst_addr = host->phys_base + OMAP_MMC_REG(host, DATA);
10233451c067SRussell King 			cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
10243451c067SRussell King 			cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
10253451c067SRussell King 			cfg.src_maxburst = burst;
10263451c067SRussell King 			cfg.dst_maxburst = burst;
10273451c067SRussell King 
10283451c067SRussell King 			if (dmaengine_slave_config(c, &cfg))
10293451c067SRussell King 				goto use_pio;
10303451c067SRussell King 
10313451c067SRussell King 			*bp = burst;
10323451c067SRussell King 		}
10333451c067SRussell King 
10343451c067SRussell King 		host->sg_len = dma_map_sg(c->device->dev, data->sg, sg_len,
10353451c067SRussell King 					  dma_data_dir);
10363451c067SRussell King 		if (host->sg_len == 0)
10373451c067SRussell King 			goto use_pio;
10383451c067SRussell King 
10393451c067SRussell King 		tx = dmaengine_prep_slave_sg(c, data->sg, host->sg_len,
10403451c067SRussell King 			data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
10413451c067SRussell King 			DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
10423451c067SRussell King 		if (!tx)
10433451c067SRussell King 			goto use_pio;
10443451c067SRussell King 
10453451c067SRussell King 		OMAP_MMC_WRITE(host, BUF, buf);
10463451c067SRussell King 
10473451c067SRussell King 		tx->callback = mmc_omap_dma_callback;
10483451c067SRussell King 		tx->callback_param = host;
10493451c067SRussell King 		dmaengine_submit(tx);
10503451c067SRussell King 		host->brs_received = 0;
10513451c067SRussell King 		host->dma_done = 0;
10523451c067SRussell King 		host->dma_in_use = 1;
10533451c067SRussell King 		return;
10543451c067SRussell King 	}
10553451c067SRussell King  use_pio:
10561c6a0718SPierre Ossman 
10571c6a0718SPierre Ossman 	/* Revert to PIO? */
10581c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, BUF, 0x1f1f);
10591c6a0718SPierre Ossman 	host->total_bytes_left = data->blocks * block_size;
10601c6a0718SPierre Ossman 	host->sg_len = sg_len;
10611c6a0718SPierre Ossman 	mmc_omap_sg_to_buf(host);
10621c6a0718SPierre Ossman 	host->dma_in_use = 0;
10631c6a0718SPierre Ossman }
10641c6a0718SPierre Ossman 
1065abfbe5f7SJuha Yrjola static void mmc_omap_start_request(struct mmc_omap_host *host,
1066abfbe5f7SJuha Yrjola 				   struct mmc_request *req)
10671c6a0718SPierre Ossman {
1068abfbe5f7SJuha Yrjola 	BUG_ON(host->mrq != NULL);
10691c6a0718SPierre Ossman 
10701c6a0718SPierre Ossman 	host->mrq = req;
10711c6a0718SPierre Ossman 
10721c6a0718SPierre Ossman 	/* only touch fifo AFTER the controller readies it */
10731c6a0718SPierre Ossman 	mmc_omap_prepare_data(host, req);
10741c6a0718SPierre Ossman 	mmc_omap_start_command(host, req->cmd);
10753451c067SRussell King 	if (host->dma_in_use) {
10763451c067SRussell King 		struct dma_chan *c = host->data->flags & MMC_DATA_WRITE ?
10773451c067SRussell King 				host->dma_tx : host->dma_rx;
10783451c067SRussell King 
10793451c067SRussell King 		dma_async_issue_pending(c);
10803451c067SRussell King 	}
1081abfbe5f7SJuha Yrjola }
1082abfbe5f7SJuha Yrjola 
1083abfbe5f7SJuha Yrjola static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
1084abfbe5f7SJuha Yrjola {
1085abfbe5f7SJuha Yrjola 	struct mmc_omap_slot *slot = mmc_priv(mmc);
1086abfbe5f7SJuha Yrjola 	struct mmc_omap_host *host = slot->host;
1087abfbe5f7SJuha Yrjola 	unsigned long flags;
1088abfbe5f7SJuha Yrjola 
1089abfbe5f7SJuha Yrjola 	spin_lock_irqsave(&host->slot_lock, flags);
1090abfbe5f7SJuha Yrjola 	if (host->mmc != NULL) {
1091abfbe5f7SJuha Yrjola 		BUG_ON(slot->mrq != NULL);
1092abfbe5f7SJuha Yrjola 		slot->mrq = req;
1093abfbe5f7SJuha Yrjola 		spin_unlock_irqrestore(&host->slot_lock, flags);
1094abfbe5f7SJuha Yrjola 		return;
1095abfbe5f7SJuha Yrjola 	} else
1096abfbe5f7SJuha Yrjola 		host->mmc = mmc;
1097abfbe5f7SJuha Yrjola 	spin_unlock_irqrestore(&host->slot_lock, flags);
1098abfbe5f7SJuha Yrjola 	mmc_omap_select_slot(slot, 1);
1099abfbe5f7SJuha Yrjola 	mmc_omap_start_request(host, req);
11001c6a0718SPierre Ossman }
11011c6a0718SPierre Ossman 
110265b5b6e5SJuha Yrjola static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
110365b5b6e5SJuha Yrjola 				int vdd)
11041c6a0718SPierre Ossman {
110565b5b6e5SJuha Yrjola 	struct mmc_omap_host *host;
11061c6a0718SPierre Ossman 
110765b5b6e5SJuha Yrjola 	host = slot->host;
110865b5b6e5SJuha Yrjola 
110965b5b6e5SJuha Yrjola 	if (slot->pdata->set_power != NULL)
111065b5b6e5SJuha Yrjola 		slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
111165b5b6e5SJuha Yrjola 					vdd);
111253db20d1STony Lindgren 	if (mmc_omap2()) {
111365b5b6e5SJuha Yrjola 		u16 w;
111465b5b6e5SJuha Yrjola 
111565b5b6e5SJuha Yrjola 		if (power_on) {
111665b5b6e5SJuha Yrjola 			w = OMAP_MMC_READ(host, CON);
111765b5b6e5SJuha Yrjola 			OMAP_MMC_WRITE(host, CON, w | (1 << 11));
11181c6a0718SPierre Ossman 		} else {
111965b5b6e5SJuha Yrjola 			w = OMAP_MMC_READ(host, CON);
112065b5b6e5SJuha Yrjola 			OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
112165b5b6e5SJuha Yrjola 		}
11221c6a0718SPierre Ossman 	}
11231c6a0718SPierre Ossman }
11241c6a0718SPierre Ossman 
1125d3af5abeSTony Lindgren static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
11261c6a0718SPierre Ossman {
1127abfbe5f7SJuha Yrjola 	struct mmc_omap_slot *slot = mmc_priv(mmc);
1128abfbe5f7SJuha Yrjola 	struct mmc_omap_host *host = slot->host;
1129d3af5abeSTony Lindgren 	int func_clk_rate = clk_get_rate(host->fclk);
11301c6a0718SPierre Ossman 	int dsor;
11311c6a0718SPierre Ossman 
11321c6a0718SPierre Ossman 	if (ios->clock == 0)
1133d3af5abeSTony Lindgren 		return 0;
11341c6a0718SPierre Ossman 
1135d3af5abeSTony Lindgren 	dsor = func_clk_rate / ios->clock;
11361c6a0718SPierre Ossman 	if (dsor < 1)
11371c6a0718SPierre Ossman 		dsor = 1;
11381c6a0718SPierre Ossman 
1139d3af5abeSTony Lindgren 	if (func_clk_rate / dsor > ios->clock)
11401c6a0718SPierre Ossman 		dsor++;
11411c6a0718SPierre Ossman 
11421c6a0718SPierre Ossman 	if (dsor > 250)
11431c6a0718SPierre Ossman 		dsor = 250;
11441c6a0718SPierre Ossman 
1145abfbe5f7SJuha Yrjola 	slot->fclk_freq = func_clk_rate / dsor;
1146abfbe5f7SJuha Yrjola 
11471c6a0718SPierre Ossman 	if (ios->bus_width == MMC_BUS_WIDTH_4)
11481c6a0718SPierre Ossman 		dsor |= 1 << 15;
1149d3af5abeSTony Lindgren 
1150d3af5abeSTony Lindgren 	return dsor;
11511c6a0718SPierre Ossman }
11521c6a0718SPierre Ossman 
1153d3af5abeSTony Lindgren static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1154d3af5abeSTony Lindgren {
1155abfbe5f7SJuha Yrjola 	struct mmc_omap_slot *slot = mmc_priv(mmc);
1156abfbe5f7SJuha Yrjola 	struct mmc_omap_host *host = slot->host;
1157abfbe5f7SJuha Yrjola 	int i, dsor;
11580807a9b5SJarkko Lavinen 	int clk_enabled;
115965b5b6e5SJuha Yrjola 
116065b5b6e5SJuha Yrjola 	mmc_omap_select_slot(slot, 0);
116165b5b6e5SJuha Yrjola 
11620807a9b5SJarkko Lavinen 	dsor = mmc_omap_calc_divisor(mmc, ios);
11630807a9b5SJarkko Lavinen 
116465b5b6e5SJuha Yrjola 	if (ios->vdd != slot->vdd)
116565b5b6e5SJuha Yrjola 		slot->vdd = ios->vdd;
1166d3af5abeSTony Lindgren 
11670807a9b5SJarkko Lavinen 	clk_enabled = 0;
11681c6a0718SPierre Ossman 	switch (ios->power_mode) {
11691c6a0718SPierre Ossman 	case MMC_POWER_OFF:
117065b5b6e5SJuha Yrjola 		mmc_omap_set_power(slot, 0, ios->vdd);
11711c6a0718SPierre Ossman 		break;
11721c6a0718SPierre Ossman 	case MMC_POWER_UP:
117346a6730eSTony Lindgren 		/* Cannot touch dsor yet, just power up MMC */
117465b5b6e5SJuha Yrjola 		mmc_omap_set_power(slot, 1, ios->vdd);
117565b5b6e5SJuha Yrjola 		goto exit;
117646a6730eSTony Lindgren 	case MMC_POWER_ON:
11770807a9b5SJarkko Lavinen 		mmc_omap_fclk_enable(host, 1);
11780807a9b5SJarkko Lavinen 		clk_enabled = 1;
11791c6a0718SPierre Ossman 		dsor |= 1 << 11;
11801c6a0718SPierre Ossman 		break;
11811c6a0718SPierre Ossman 	}
11821c6a0718SPierre Ossman 
118365b5b6e5SJuha Yrjola 	if (slot->bus_mode != ios->bus_mode) {
118465b5b6e5SJuha Yrjola 		if (slot->pdata->set_bus_mode != NULL)
118565b5b6e5SJuha Yrjola 			slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
118665b5b6e5SJuha Yrjola 						  ios->bus_mode);
118765b5b6e5SJuha Yrjola 		slot->bus_mode = ios->bus_mode;
118865b5b6e5SJuha Yrjola 	}
11891c6a0718SPierre Ossman 
11901c6a0718SPierre Ossman 	/* On insanely high arm_per frequencies something sometimes
11911c6a0718SPierre Ossman 	 * goes somehow out of sync, and the POW bit is not being set,
11921c6a0718SPierre Ossman 	 * which results in the while loop below getting stuck.
11931c6a0718SPierre Ossman 	 * Writing to the CON register twice seems to do the trick. */
11941c6a0718SPierre Ossman 	for (i = 0; i < 2; i++)
11951c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, CON, dsor);
119665b5b6e5SJuha Yrjola 	slot->saved_con = dsor;
119746a6730eSTony Lindgren 	if (ios->power_mode == MMC_POWER_ON) {
11989d7c6eeeSJarkko Lavinen 		/* worst case at 400kHz, 80 cycles makes 200 microsecs */
11999d7c6eeeSJarkko Lavinen 		int usecs = 250;
12009d7c6eeeSJarkko Lavinen 
12011c6a0718SPierre Ossman 		/* Send clock cycles, poll completion */
12021c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, IE, 0);
12031c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, STAT, 0xffff);
12041c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, CMD, 1 << 7);
12059d7c6eeeSJarkko Lavinen 		while (usecs > 0 && (OMAP_MMC_READ(host, STAT) & 1) == 0) {
12069d7c6eeeSJarkko Lavinen 			udelay(1);
12079d7c6eeeSJarkko Lavinen 			usecs--;
12089d7c6eeeSJarkko Lavinen 		}
12091c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, STAT, 1);
12101c6a0718SPierre Ossman 	}
121165b5b6e5SJuha Yrjola 
121265b5b6e5SJuha Yrjola exit:
12130807a9b5SJarkko Lavinen 	mmc_omap_release_slot(slot, clk_enabled);
12141c6a0718SPierre Ossman }
12151c6a0718SPierre Ossman 
12161c6a0718SPierre Ossman static const struct mmc_host_ops mmc_omap_ops = {
12171c6a0718SPierre Ossman 	.request	= mmc_omap_request,
12181c6a0718SPierre Ossman 	.set_ios	= mmc_omap_set_ios,
12191c6a0718SPierre Ossman };
12201c6a0718SPierre Ossman 
1221c3be1efdSBill Pemberton static int mmc_omap_new_slot(struct mmc_omap_host *host, int id)
1222abfbe5f7SJuha Yrjola {
1223abfbe5f7SJuha Yrjola 	struct mmc_omap_slot *slot = NULL;
1224abfbe5f7SJuha Yrjola 	struct mmc_host *mmc;
1225abfbe5f7SJuha Yrjola 	int r;
1226abfbe5f7SJuha Yrjola 
1227abfbe5f7SJuha Yrjola 	mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
1228abfbe5f7SJuha Yrjola 	if (mmc == NULL)
1229abfbe5f7SJuha Yrjola 		return -ENOMEM;
1230abfbe5f7SJuha Yrjola 
1231abfbe5f7SJuha Yrjola 	slot = mmc_priv(mmc);
1232abfbe5f7SJuha Yrjola 	slot->host = host;
1233abfbe5f7SJuha Yrjola 	slot->mmc = mmc;
1234abfbe5f7SJuha Yrjola 	slot->id = id;
1235abfbe5f7SJuha Yrjola 	slot->pdata = &host->pdata->slots[id];
1236abfbe5f7SJuha Yrjola 
1237abfbe5f7SJuha Yrjola 	host->slots[id] = slot;
1238abfbe5f7SJuha Yrjola 
123923af6039SPierre Ossman 	mmc->caps = 0;
124090c62bf0STony Lindgren 	if (host->pdata->slots[id].wires >= 4)
1241*b13d1f0fSJarkko Nikula 		mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_ERASE;
1242abfbe5f7SJuha Yrjola 
1243abfbe5f7SJuha Yrjola 	mmc->ops = &mmc_omap_ops;
1244abfbe5f7SJuha Yrjola 	mmc->f_min = 400000;
1245abfbe5f7SJuha Yrjola 
124653db20d1STony Lindgren 	if (mmc_omap2())
1247abfbe5f7SJuha Yrjola 		mmc->f_max = 48000000;
1248abfbe5f7SJuha Yrjola 	else
1249abfbe5f7SJuha Yrjola 		mmc->f_max = 24000000;
1250abfbe5f7SJuha Yrjola 	if (host->pdata->max_freq)
1251abfbe5f7SJuha Yrjola 		mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
1252abfbe5f7SJuha Yrjola 	mmc->ocr_avail = slot->pdata->ocr_mask;
1253abfbe5f7SJuha Yrjola 
1254abfbe5f7SJuha Yrjola 	/* Use scatterlist DMA to reduce per-transfer costs.
1255abfbe5f7SJuha Yrjola 	 * NOTE max_seg_size assumption that small blocks aren't
1256abfbe5f7SJuha Yrjola 	 * normally used (except e.g. for reading SD registers).
1257abfbe5f7SJuha Yrjola 	 */
1258a36274e0SMartin K. Petersen 	mmc->max_segs = 32;
1259abfbe5f7SJuha Yrjola 	mmc->max_blk_size = 2048;	/* BLEN is 11 bits (+1) */
1260abfbe5f7SJuha Yrjola 	mmc->max_blk_count = 2048;	/* NBLK is 11 bits (+1) */
1261abfbe5f7SJuha Yrjola 	mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1262abfbe5f7SJuha Yrjola 	mmc->max_seg_size = mmc->max_req_size;
1263abfbe5f7SJuha Yrjola 
12640e5c93e0SJarkko Nikula 	if (slot->pdata->get_cover_state != NULL) {
12650e5c93e0SJarkko Nikula 		setup_timer(&slot->cover_timer, mmc_omap_cover_timer,
12660e5c93e0SJarkko Nikula 			    (unsigned long)slot);
12670e5c93e0SJarkko Nikula 		tasklet_init(&slot->cover_tasklet, mmc_omap_cover_handler,
12680e5c93e0SJarkko Nikula 			     (unsigned long)slot);
12690e5c93e0SJarkko Nikula 	}
12700e5c93e0SJarkko Nikula 
1271abfbe5f7SJuha Yrjola 	r = mmc_add_host(mmc);
1272abfbe5f7SJuha Yrjola 	if (r < 0)
1273abfbe5f7SJuha Yrjola 		goto err_remove_host;
1274abfbe5f7SJuha Yrjola 
1275abfbe5f7SJuha Yrjola 	if (slot->pdata->name != NULL) {
1276abfbe5f7SJuha Yrjola 		r = device_create_file(&mmc->class_dev,
1277abfbe5f7SJuha Yrjola 					&dev_attr_slot_name);
1278abfbe5f7SJuha Yrjola 		if (r < 0)
1279abfbe5f7SJuha Yrjola 			goto err_remove_host;
1280abfbe5f7SJuha Yrjola 	}
1281abfbe5f7SJuha Yrjola 
12825a0f3f1fSJuha Yrjola 	if (slot->pdata->get_cover_state != NULL) {
12835a0f3f1fSJuha Yrjola 		r = device_create_file(&mmc->class_dev,
12845a0f3f1fSJuha Yrjola 					&dev_attr_cover_switch);
12855a0f3f1fSJuha Yrjola 		if (r < 0)
12865a0f3f1fSJuha Yrjola 			goto err_remove_slot_name;
12877584d276SJarkko Lavinen 		tasklet_schedule(&slot->cover_tasklet);
12885a0f3f1fSJuha Yrjola 	}
12895a0f3f1fSJuha Yrjola 
1290abfbe5f7SJuha Yrjola 	return 0;
1291abfbe5f7SJuha Yrjola 
12925a0f3f1fSJuha Yrjola err_remove_slot_name:
12935a0f3f1fSJuha Yrjola 	if (slot->pdata->name != NULL)
12945a0f3f1fSJuha Yrjola 		device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1295abfbe5f7SJuha Yrjola err_remove_host:
1296abfbe5f7SJuha Yrjola 	mmc_remove_host(mmc);
1297abfbe5f7SJuha Yrjola 	mmc_free_host(mmc);
1298abfbe5f7SJuha Yrjola 	return r;
1299abfbe5f7SJuha Yrjola }
1300abfbe5f7SJuha Yrjola 
1301abfbe5f7SJuha Yrjola static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
1302abfbe5f7SJuha Yrjola {
1303abfbe5f7SJuha Yrjola 	struct mmc_host *mmc = slot->mmc;
1304abfbe5f7SJuha Yrjola 
1305abfbe5f7SJuha Yrjola 	if (slot->pdata->name != NULL)
1306abfbe5f7SJuha Yrjola 		device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
13075a0f3f1fSJuha Yrjola 	if (slot->pdata->get_cover_state != NULL)
13085a0f3f1fSJuha Yrjola 		device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
13095a0f3f1fSJuha Yrjola 
13107584d276SJarkko Lavinen 	tasklet_kill(&slot->cover_tasklet);
13117584d276SJarkko Lavinen 	del_timer_sync(&slot->cover_timer);
1312b01a4f1cSVenkatraman S 	flush_workqueue(slot->host->mmc_omap_wq);
1313abfbe5f7SJuha Yrjola 
1314abfbe5f7SJuha Yrjola 	mmc_remove_host(mmc);
1315abfbe5f7SJuha Yrjola 	mmc_free_host(mmc);
1316abfbe5f7SJuha Yrjola }
1317abfbe5f7SJuha Yrjola 
1318c3be1efdSBill Pemberton static int mmc_omap_probe(struct platform_device *pdev)
13191c6a0718SPierre Ossman {
1320abfbe5f7SJuha Yrjola 	struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
13211c6a0718SPierre Ossman 	struct mmc_omap_host *host = NULL;
13221c6a0718SPierre Ossman 	struct resource *res;
13233451c067SRussell King 	dma_cap_mask_t mask;
132431ee9181STony Lindgren 	unsigned sig = 0;
1325abfbe5f7SJuha Yrjola 	int i, ret = 0;
13261c6a0718SPierre Ossman 	int irq;
13271c6a0718SPierre Ossman 
1328abfbe5f7SJuha Yrjola 	if (pdata == NULL) {
13291c6a0718SPierre Ossman 		dev_err(&pdev->dev, "platform data missing\n");
13301c6a0718SPierre Ossman 		return -ENXIO;
13311c6a0718SPierre Ossman 	}
1332abfbe5f7SJuha Yrjola 	if (pdata->nr_slots == 0) {
1333abfbe5f7SJuha Yrjola 		dev_err(&pdev->dev, "no slots\n");
13349cb238c0STony Lindgren 		return -EPROBE_DEFER;
1335abfbe5f7SJuha Yrjola 	}
13361c6a0718SPierre Ossman 
1337ae9b79c6SJarkko Nikula 	host = devm_kzalloc(&pdev->dev, sizeof(struct mmc_omap_host),
1338ae9b79c6SJarkko Nikula 			    GFP_KERNEL);
133964ac16ecSJarkko Nikula 	if (host == NULL)
134064ac16ecSJarkko Nikula 		return -ENOMEM;
134164ac16ecSJarkko Nikula 
134264ac16ecSJarkko Nikula 	irq = platform_get_irq(pdev, 0);
134364ac16ecSJarkko Nikula 	if (irq < 0)
134464ac16ecSJarkko Nikula 		return -ENXIO;
134564ac16ecSJarkko Nikula 
134664ac16ecSJarkko Nikula 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
134764ac16ecSJarkko Nikula 	host->virt_base = devm_ioremap_resource(&pdev->dev, res);
134864ac16ecSJarkko Nikula 	if (IS_ERR(host->virt_base))
134964ac16ecSJarkko Nikula 		return PTR_ERR(host->virt_base);
13501c6a0718SPierre Ossman 
13510f602ec7SJarkko Lavinen 	INIT_WORK(&host->slot_release_work, mmc_omap_slot_release_work);
13520f602ec7SJarkko Lavinen 	INIT_WORK(&host->send_stop_work, mmc_omap_send_stop_work);
13530f602ec7SJarkko Lavinen 
13540fb4723dSJarkko Lavinen 	INIT_WORK(&host->cmd_abort_work, mmc_omap_abort_command);
13550fb4723dSJarkko Lavinen 	setup_timer(&host->cmd_abort_timer, mmc_omap_cmd_timer,
13560fb4723dSJarkko Lavinen 		    (unsigned long) host);
1357eb1860bcSJarkko Lavinen 
13580807a9b5SJarkko Lavinen 	spin_lock_init(&host->clk_lock);
13590807a9b5SJarkko Lavinen 	setup_timer(&host->clk_timer, mmc_omap_clk_timer, (unsigned long) host);
13600807a9b5SJarkko Lavinen 
13611c6a0718SPierre Ossman 	spin_lock_init(&host->dma_lock);
1362abfbe5f7SJuha Yrjola 	spin_lock_init(&host->slot_lock);
1363abfbe5f7SJuha Yrjola 	init_waitqueue_head(&host->slot_wq);
1364abfbe5f7SJuha Yrjola 
1365abfbe5f7SJuha Yrjola 	host->pdata = pdata;
136653db20d1STony Lindgren 	host->features = host->pdata->slots[0].features;
1367abfbe5f7SJuha Yrjola 	host->dev = &pdev->dev;
1368abfbe5f7SJuha Yrjola 	platform_set_drvdata(pdev, host);
1369abfbe5f7SJuha Yrjola 
13701c6a0718SPierre Ossman 	host->id = pdev->id;
13711c6a0718SPierre Ossman 	host->irq = irq;
13722ca5dc6fSJarkko Nikula 	host->phys_base = res->start;
13735c9e02b1SRussell King 	host->iclk = clk_get(&pdev->dev, "ick");
137464ac16ecSJarkko Nikula 	if (IS_ERR(host->iclk))
137564ac16ecSJarkko Nikula 		return PTR_ERR(host->iclk);
13761c6a0718SPierre Ossman 	clk_enable(host->iclk);
13771c6a0718SPierre Ossman 
13785c9e02b1SRussell King 	host->fclk = clk_get(&pdev->dev, "fck");
13791c6a0718SPierre Ossman 	if (IS_ERR(host->fclk)) {
13801c6a0718SPierre Ossman 		ret = PTR_ERR(host->fclk);
13811c6a0718SPierre Ossman 		goto err_free_iclk;
13821c6a0718SPierre Ossman 	}
13831c6a0718SPierre Ossman 
13843451c067SRussell King 	dma_cap_zero(mask);
13853451c067SRussell King 	dma_cap_set(DMA_SLAVE, mask);
13863451c067SRussell King 
13873451c067SRussell King 	host->dma_tx_burst = -1;
13883451c067SRussell King 	host->dma_rx_burst = -1;
13893451c067SRussell King 
139031ee9181STony Lindgren 	res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
139131ee9181STony Lindgren 	if (res)
139231ee9181STony Lindgren 		sig = res->start;
139331ee9181STony Lindgren 	host->dma_tx = dma_request_slave_channel_compat(mask,
139431ee9181STony Lindgren 				omap_dma_filter_fn, &sig, &pdev->dev, "tx");
13953451c067SRussell King 	if (!host->dma_tx)
13963451c067SRussell King 		dev_warn(host->dev, "unable to obtain TX DMA engine channel %u\n",
13973451c067SRussell King 			sig);
139831ee9181STony Lindgren 
139931ee9181STony Lindgren 	res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
140031ee9181STony Lindgren 	if (res)
140131ee9181STony Lindgren 		sig = res->start;
140231ee9181STony Lindgren 	host->dma_rx = dma_request_slave_channel_compat(mask,
140331ee9181STony Lindgren 				omap_dma_filter_fn, &sig, &pdev->dev, "rx");
14043451c067SRussell King 	if (!host->dma_rx)
14053451c067SRussell King 		dev_warn(host->dev, "unable to obtain RX DMA engine channel %u\n",
14063451c067SRussell King 			sig);
14073451c067SRussell King 
14081c6a0718SPierre Ossman 	ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
14091c6a0718SPierre Ossman 	if (ret)
14103451c067SRussell King 		goto err_free_dma;
14111c6a0718SPierre Ossman 
1412abfbe5f7SJuha Yrjola 	if (pdata->init != NULL) {
1413abfbe5f7SJuha Yrjola 		ret = pdata->init(&pdev->dev);
1414abfbe5f7SJuha Yrjola 		if (ret < 0)
1415abfbe5f7SJuha Yrjola 			goto err_free_irq;
1416abfbe5f7SJuha Yrjola 	}
14171c6a0718SPierre Ossman 
1418abfbe5f7SJuha Yrjola 	host->nr_slots = pdata->nr_slots;
141953db20d1STony Lindgren 	host->reg_shift = (mmc_omap7xx() ? 1 : 2);
14203caf4140STony Lindgren 
14213caf4140STony Lindgren 	host->mmc_omap_wq = alloc_workqueue("mmc_omap", 0, 0);
14223caf4140STony Lindgren 	if (!host->mmc_omap_wq)
14233caf4140STony Lindgren 		goto err_plat_cleanup;
14243caf4140STony Lindgren 
1425abfbe5f7SJuha Yrjola 	for (i = 0; i < pdata->nr_slots; i++) {
1426abfbe5f7SJuha Yrjola 		ret = mmc_omap_new_slot(host, i);
1427abfbe5f7SJuha Yrjola 		if (ret < 0) {
1428abfbe5f7SJuha Yrjola 			while (--i >= 0)
1429abfbe5f7SJuha Yrjola 				mmc_omap_remove_slot(host->slots[i]);
1430abfbe5f7SJuha Yrjola 
14313caf4140STony Lindgren 			goto err_destroy_wq;
1432abfbe5f7SJuha Yrjola 		}
1433abfbe5f7SJuha Yrjola 	}
14341c6a0718SPierre Ossman 
14351c6a0718SPierre Ossman 	return 0;
14361c6a0718SPierre Ossman 
14373caf4140STony Lindgren err_destroy_wq:
14383caf4140STony Lindgren 	destroy_workqueue(host->mmc_omap_wq);
1439abfbe5f7SJuha Yrjola err_plat_cleanup:
1440abfbe5f7SJuha Yrjola 	if (pdata->cleanup)
1441abfbe5f7SJuha Yrjola 		pdata->cleanup(&pdev->dev);
1442abfbe5f7SJuha Yrjola err_free_irq:
1443abfbe5f7SJuha Yrjola 	free_irq(host->irq, host);
14443451c067SRussell King err_free_dma:
14453451c067SRussell King 	if (host->dma_tx)
14463451c067SRussell King 		dma_release_channel(host->dma_tx);
14473451c067SRussell King 	if (host->dma_rx)
14483451c067SRussell King 		dma_release_channel(host->dma_rx);
14491c6a0718SPierre Ossman 	clk_put(host->fclk);
14501c6a0718SPierre Ossman err_free_iclk:
14511c6a0718SPierre Ossman 	clk_disable(host->iclk);
14521c6a0718SPierre Ossman 	clk_put(host->iclk);
14531c6a0718SPierre Ossman 	return ret;
14541c6a0718SPierre Ossman }
14551c6a0718SPierre Ossman 
14566e0ee714SBill Pemberton static int mmc_omap_remove(struct platform_device *pdev)
14571c6a0718SPierre Ossman {
14581c6a0718SPierre Ossman 	struct mmc_omap_host *host = platform_get_drvdata(pdev);
1459abfbe5f7SJuha Yrjola 	int i;
14601c6a0718SPierre Ossman 
14611c6a0718SPierre Ossman 	BUG_ON(host == NULL);
14621c6a0718SPierre Ossman 
1463abfbe5f7SJuha Yrjola 	for (i = 0; i < host->nr_slots; i++)
1464abfbe5f7SJuha Yrjola 		mmc_omap_remove_slot(host->slots[i]);
14651c6a0718SPierre Ossman 
1466abfbe5f7SJuha Yrjola 	if (host->pdata->cleanup)
1467abfbe5f7SJuha Yrjola 		host->pdata->cleanup(&pdev->dev);
1468abfbe5f7SJuha Yrjola 
1469d4a36645SRussell King 	mmc_omap_fclk_enable(host, 0);
147049c1d9daSLadislav Michl 	free_irq(host->irq, host);
14711c6a0718SPierre Ossman 	clk_put(host->fclk);
1472d4a36645SRussell King 	clk_disable(host->iclk);
1473d4a36645SRussell King 	clk_put(host->iclk);
14741c6a0718SPierre Ossman 
14753451c067SRussell King 	if (host->dma_tx)
14763451c067SRussell King 		dma_release_channel(host->dma_tx);
14773451c067SRussell King 	if (host->dma_rx)
14783451c067SRussell King 		dma_release_channel(host->dma_rx);
14793451c067SRussell King 
1480b01a4f1cSVenkatraman S 	destroy_workqueue(host->mmc_omap_wq);
14811c6a0718SPierre Ossman 
14821c6a0718SPierre Ossman 	return 0;
14831c6a0718SPierre Ossman }
14841c6a0718SPierre Ossman 
14859cb238c0STony Lindgren #if IS_BUILTIN(CONFIG_OF)
14869cb238c0STony Lindgren static const struct of_device_id mmc_omap_match[] = {
14879cb238c0STony Lindgren 	{ .compatible = "ti,omap2420-mmc", },
14889cb238c0STony Lindgren 	{ },
14899cb238c0STony Lindgren };
14909cb238c0STony Lindgren #endif
14919cb238c0STony Lindgren 
14921c6a0718SPierre Ossman static struct platform_driver mmc_omap_driver = {
1493b6e0703bSVenkatraman S 	.probe		= mmc_omap_probe,
14940433c143SBill Pemberton 	.remove		= mmc_omap_remove,
14951c6a0718SPierre Ossman 	.driver		= {
14961c6a0718SPierre Ossman 		.name	= DRIVER_NAME,
1497bc65c724SKay Sievers 		.owner	= THIS_MODULE,
14989cb238c0STony Lindgren 		.of_match_table = of_match_ptr(mmc_omap_match),
14991c6a0718SPierre Ossman 	},
15001c6a0718SPierre Ossman };
15011c6a0718SPierre Ossman 
1502680f1b5bSVenkatraman S module_platform_driver(mmc_omap_driver);
15031c6a0718SPierre Ossman MODULE_DESCRIPTION("OMAP Multimedia Card driver");
15041c6a0718SPierre Ossman MODULE_LICENSE("GPL");
1505bc65c724SKay Sievers MODULE_ALIAS("platform:" DRIVER_NAME);
1506d36b6910SAl Viro MODULE_AUTHOR("Juha Yrjölä");
1507