xref: /linux/drivers/mmc/host/omap.c (revision 31ee9181eb92cc727876ec5a2144a1b3cbdf5bb1)
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>
253451c067SRussell King #include <linux/omap-dma.h>
261c6a0718SPierre Ossman #include <linux/mmc/host.h>
271c6a0718SPierre Ossman #include <linux/mmc/card.h>
281c6a0718SPierre Ossman #include <linux/clk.h>
2945711f1aSJens Axboe #include <linux/scatterlist.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
3168f39e74STony Lindgren #include <linux/platform_data/mmc-omap.h>
321c6a0718SPierre Ossman 
331c6a0718SPierre Ossman 
341c6a0718SPierre Ossman #define	OMAP_MMC_REG_CMD	0x00
350e950fa6SMarek Belisko #define	OMAP_MMC_REG_ARGL	0x01
360e950fa6SMarek Belisko #define	OMAP_MMC_REG_ARGH	0x02
370e950fa6SMarek Belisko #define	OMAP_MMC_REG_CON	0x03
380e950fa6SMarek Belisko #define	OMAP_MMC_REG_STAT	0x04
390e950fa6SMarek Belisko #define	OMAP_MMC_REG_IE		0x05
400e950fa6SMarek Belisko #define	OMAP_MMC_REG_CTO	0x06
410e950fa6SMarek Belisko #define	OMAP_MMC_REG_DTO	0x07
420e950fa6SMarek Belisko #define	OMAP_MMC_REG_DATA	0x08
430e950fa6SMarek Belisko #define	OMAP_MMC_REG_BLEN	0x09
440e950fa6SMarek Belisko #define	OMAP_MMC_REG_NBLK	0x0a
450e950fa6SMarek Belisko #define	OMAP_MMC_REG_BUF	0x0b
460e950fa6SMarek Belisko #define	OMAP_MMC_REG_SDIO	0x0d
470e950fa6SMarek Belisko #define	OMAP_MMC_REG_REV	0x0f
480e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP0	0x10
490e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP1	0x11
500e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP2	0x12
510e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP3	0x13
520e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP4	0x14
530e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP5	0x15
540e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP6	0x16
550e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP7	0x17
560e950fa6SMarek Belisko #define	OMAP_MMC_REG_IOSR	0x18
570e950fa6SMarek Belisko #define	OMAP_MMC_REG_SYSC	0x19
580e950fa6SMarek Belisko #define	OMAP_MMC_REG_SYSS	0x1a
591c6a0718SPierre Ossman 
601c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CARD_ERR		(1 << 14)
611c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CARD_IRQ		(1 << 13)
621c6a0718SPierre Ossman #define	OMAP_MMC_STAT_OCR_BUSY		(1 << 12)
631c6a0718SPierre Ossman #define	OMAP_MMC_STAT_A_EMPTY		(1 << 11)
641c6a0718SPierre Ossman #define	OMAP_MMC_STAT_A_FULL		(1 << 10)
651c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CMD_CRC		(1 <<  8)
661c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CMD_TOUT		(1 <<  7)
671c6a0718SPierre Ossman #define	OMAP_MMC_STAT_DATA_CRC		(1 <<  6)
681c6a0718SPierre Ossman #define	OMAP_MMC_STAT_DATA_TOUT		(1 <<  5)
691c6a0718SPierre Ossman #define	OMAP_MMC_STAT_END_BUSY		(1 <<  4)
701c6a0718SPierre Ossman #define	OMAP_MMC_STAT_END_OF_DATA	(1 <<  3)
711c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CARD_BUSY		(1 <<  2)
721c6a0718SPierre Ossman #define	OMAP_MMC_STAT_END_OF_CMD	(1 <<  0)
731c6a0718SPierre Ossman 
7453db20d1STony Lindgren #define mmc_omap7xx()	(host->features & MMC_OMAP7XX)
7553db20d1STony Lindgren #define mmc_omap15xx()	(host->features & MMC_OMAP15XX)
7653db20d1STony Lindgren #define mmc_omap16xx()	(host->features & MMC_OMAP16XX)
7753db20d1STony Lindgren #define MMC_OMAP1_MASK	(MMC_OMAP7XX | MMC_OMAP15XX | MMC_OMAP16XX)
7853db20d1STony Lindgren #define mmc_omap1()	(host->features & MMC_OMAP1_MASK)
7953db20d1STony Lindgren #define mmc_omap2()	(!mmc_omap1())
8053db20d1STony Lindgren 
810e950fa6SMarek Belisko #define OMAP_MMC_REG(host, reg)		(OMAP_MMC_REG_##reg << (host)->reg_shift)
820e950fa6SMarek Belisko #define OMAP_MMC_READ(host, reg)	__raw_readw((host)->virt_base + OMAP_MMC_REG(host, reg))
830e950fa6SMarek Belisko #define OMAP_MMC_WRITE(host, reg, val)	__raw_writew((val), (host)->virt_base + OMAP_MMC_REG(host, reg))
841c6a0718SPierre Ossman 
851c6a0718SPierre Ossman /*
861c6a0718SPierre Ossman  * Command types
871c6a0718SPierre Ossman  */
881c6a0718SPierre Ossman #define OMAP_MMC_CMDTYPE_BC	0
891c6a0718SPierre Ossman #define OMAP_MMC_CMDTYPE_BCR	1
901c6a0718SPierre Ossman #define OMAP_MMC_CMDTYPE_AC	2
911c6a0718SPierre Ossman #define OMAP_MMC_CMDTYPE_ADTC	3
921c6a0718SPierre Ossman 
931c6a0718SPierre Ossman #define DRIVER_NAME "mmci-omap"
941c6a0718SPierre Ossman 
951c6a0718SPierre Ossman /* Specifies how often in millisecs to poll for card status changes
961c6a0718SPierre Ossman  * when the cover switch is open */
977584d276SJarkko Lavinen #define OMAP_MMC_COVER_POLL_DELAY	500
981c6a0718SPierre Ossman 
99abfbe5f7SJuha Yrjola struct mmc_omap_host;
100abfbe5f7SJuha Yrjola 
101abfbe5f7SJuha Yrjola struct mmc_omap_slot {
102abfbe5f7SJuha Yrjola 	int			id;
103abfbe5f7SJuha Yrjola 	unsigned int		vdd;
104abfbe5f7SJuha Yrjola 	u16			saved_con;
105abfbe5f7SJuha Yrjola 	u16			bus_mode;
106abfbe5f7SJuha Yrjola 	unsigned int		fclk_freq;
107abfbe5f7SJuha Yrjola 
1087584d276SJarkko Lavinen 	struct tasklet_struct	cover_tasklet;
1097584d276SJarkko Lavinen 	struct timer_list       cover_timer;
1105a0f3f1fSJuha Yrjola 	unsigned		cover_open;
1115a0f3f1fSJuha Yrjola 
112abfbe5f7SJuha Yrjola 	struct mmc_request      *mrq;
113abfbe5f7SJuha Yrjola 	struct mmc_omap_host    *host;
114abfbe5f7SJuha Yrjola 	struct mmc_host		*mmc;
115abfbe5f7SJuha Yrjola 	struct omap_mmc_slot_data *pdata;
116abfbe5f7SJuha Yrjola };
117abfbe5f7SJuha Yrjola 
1181c6a0718SPierre Ossman struct mmc_omap_host {
1191c6a0718SPierre Ossman 	int			initialized;
1201c6a0718SPierre Ossman 	struct mmc_request *	mrq;
1211c6a0718SPierre Ossman 	struct mmc_command *	cmd;
1221c6a0718SPierre Ossman 	struct mmc_data *	data;
1231c6a0718SPierre Ossman 	struct mmc_host *	mmc;
1241c6a0718SPierre Ossman 	struct device *		dev;
1251c6a0718SPierre Ossman 	unsigned char		id; /* 16xx chips have 2 MMC blocks */
1261c6a0718SPierre Ossman 	struct clk *		iclk;
1271c6a0718SPierre Ossman 	struct clk *		fclk;
1283451c067SRussell King 	struct dma_chan		*dma_rx;
1293451c067SRussell King 	u32			dma_rx_burst;
1303451c067SRussell King 	struct dma_chan		*dma_tx;
1313451c067SRussell King 	u32			dma_tx_burst;
1321c6a0718SPierre Ossman 	struct resource		*mem_res;
1331c6a0718SPierre Ossman 	void __iomem		*virt_base;
1341c6a0718SPierre Ossman 	unsigned int		phys_base;
1351c6a0718SPierre Ossman 	int			irq;
1361c6a0718SPierre Ossman 	unsigned char		bus_mode;
1370e950fa6SMarek Belisko 	unsigned int		reg_shift;
1381c6a0718SPierre Ossman 
1390fb4723dSJarkko Lavinen 	struct work_struct	cmd_abort_work;
1400fb4723dSJarkko Lavinen 	unsigned		abort:1;
1410fb4723dSJarkko Lavinen 	struct timer_list	cmd_abort_timer;
142eb1860bcSJarkko Lavinen 
1430f602ec7SJarkko Lavinen 	struct work_struct      slot_release_work;
1440f602ec7SJarkko Lavinen 	struct mmc_omap_slot    *next_slot;
1450f602ec7SJarkko Lavinen 	struct work_struct      send_stop_work;
1460f602ec7SJarkko Lavinen 	struct mmc_data		*stop_data;
1470f602ec7SJarkko Lavinen 
1481c6a0718SPierre Ossman 	unsigned int		sg_len;
1491c6a0718SPierre Ossman 	int			sg_idx;
1501c6a0718SPierre Ossman 	u16 *			buffer;
1511c6a0718SPierre Ossman 	u32			buffer_bytes_left;
1521c6a0718SPierre Ossman 	u32			total_bytes_left;
1531c6a0718SPierre Ossman 
15453db20d1STony Lindgren 	unsigned		features;
1551c6a0718SPierre Ossman 	unsigned		use_dma:1;
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;
3401c6a0718SPierre Ossman 
3411c6a0718SPierre Ossman 	host->cmd = cmd;
3421c6a0718SPierre Ossman 
3431c6a0718SPierre Ossman 	resptype = 0;
3441c6a0718SPierre Ossman 	cmdtype = 0;
3451c6a0718SPierre Ossman 
3461c6a0718SPierre Ossman 	/* Our hardware needs to know exact type */
3471c6a0718SPierre Ossman 	switch (mmc_resp_type(cmd)) {
3481c6a0718SPierre Ossman 	case MMC_RSP_NONE:
3491c6a0718SPierre Ossman 		break;
3501c6a0718SPierre Ossman 	case MMC_RSP_R1:
3511c6a0718SPierre Ossman 	case MMC_RSP_R1B:
3521c6a0718SPierre Ossman 		/* resp 1, 1b, 6, 7 */
3531c6a0718SPierre Ossman 		resptype = 1;
3541c6a0718SPierre Ossman 		break;
3551c6a0718SPierre Ossman 	case MMC_RSP_R2:
3561c6a0718SPierre Ossman 		resptype = 2;
3571c6a0718SPierre Ossman 		break;
3581c6a0718SPierre Ossman 	case MMC_RSP_R3:
3591c6a0718SPierre Ossman 		resptype = 3;
3601c6a0718SPierre Ossman 		break;
3611c6a0718SPierre Ossman 	default:
3621c6a0718SPierre Ossman 		dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
3631c6a0718SPierre Ossman 		break;
3641c6a0718SPierre Ossman 	}
3651c6a0718SPierre Ossman 
3661c6a0718SPierre Ossman 	if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
3671c6a0718SPierre Ossman 		cmdtype = OMAP_MMC_CMDTYPE_ADTC;
3681c6a0718SPierre Ossman 	} else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
3691c6a0718SPierre Ossman 		cmdtype = OMAP_MMC_CMDTYPE_BC;
3701c6a0718SPierre Ossman 	} else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
3711c6a0718SPierre Ossman 		cmdtype = OMAP_MMC_CMDTYPE_BCR;
3721c6a0718SPierre Ossman 	} else {
3731c6a0718SPierre Ossman 		cmdtype = OMAP_MMC_CMDTYPE_AC;
3741c6a0718SPierre Ossman 	}
3751c6a0718SPierre Ossman 
3761c6a0718SPierre Ossman 	cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
3771c6a0718SPierre Ossman 
378abfbe5f7SJuha Yrjola 	if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
3791c6a0718SPierre Ossman 		cmdreg |= 1 << 6;
3801c6a0718SPierre Ossman 
3811c6a0718SPierre Ossman 	if (cmd->flags & MMC_RSP_BUSY)
3821c6a0718SPierre Ossman 		cmdreg |= 1 << 11;
3831c6a0718SPierre Ossman 
3841c6a0718SPierre Ossman 	if (host->data && !(host->data->flags & MMC_DATA_WRITE))
3851c6a0718SPierre Ossman 		cmdreg |= 1 << 15;
3861c6a0718SPierre Ossman 
3870fb4723dSJarkko Lavinen 	mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
388eb1860bcSJarkko Lavinen 
3891c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, CTO, 200);
3901c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
3911c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
3921c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, IE,
3931c6a0718SPierre Ossman 		       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  |
3971c6a0718SPierre Ossman 		       OMAP_MMC_STAT_END_OF_DATA);
3981c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, CMD, cmdreg);
3991c6a0718SPierre Ossman }
4001c6a0718SPierre Ossman 
4011c6a0718SPierre Ossman static void
402a914ded2SJuha Yrjola mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
403a914ded2SJuha Yrjola 		     int abort)
4041c6a0718SPierre Ossman {
4051c6a0718SPierre Ossman 	enum dma_data_direction dma_data_dir;
4063451c067SRussell King 	struct device *dev = mmc_dev(host->mmc);
4073451c067SRussell King 	struct dma_chan *c;
4081c6a0718SPierre Ossman 
4093451c067SRussell King 	if (data->flags & MMC_DATA_WRITE) {
4101c6a0718SPierre Ossman 		dma_data_dir = DMA_TO_DEVICE;
4113451c067SRussell King 		c = host->dma_tx;
4123451c067SRussell King 	} else {
4131c6a0718SPierre Ossman 		dma_data_dir = DMA_FROM_DEVICE;
4143451c067SRussell King 		c = host->dma_rx;
4153451c067SRussell King 	}
4163451c067SRussell King 	if (c) {
4173451c067SRussell King 		if (data->error) {
4183451c067SRussell King 			dmaengine_terminate_all(c);
4193451c067SRussell King 			/* Claim nothing transferred on error... */
4203451c067SRussell King 			data->bytes_xfered = 0;
4213451c067SRussell King 		}
4223451c067SRussell King 		dev = c->device->dev;
4233451c067SRussell King 	}
4243451c067SRussell King 	dma_unmap_sg(dev, data->sg, host->sg_len, dma_data_dir);
4251c6a0718SPierre Ossman }
426a914ded2SJuha Yrjola 
4270f602ec7SJarkko Lavinen static void mmc_omap_send_stop_work(struct work_struct *work)
4280f602ec7SJarkko Lavinen {
4290f602ec7SJarkko Lavinen 	struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
4300f602ec7SJarkko Lavinen 						  send_stop_work);
4310f602ec7SJarkko Lavinen 	struct mmc_omap_slot *slot = host->current_slot;
4320f602ec7SJarkko Lavinen 	struct mmc_data *data = host->stop_data;
4330f602ec7SJarkko Lavinen 	unsigned long tick_ns;
4340f602ec7SJarkko Lavinen 
4350f602ec7SJarkko Lavinen 	tick_ns = (1000000000 + slot->fclk_freq - 1)/slot->fclk_freq;
4360f602ec7SJarkko Lavinen 	ndelay(8*tick_ns);
4370f602ec7SJarkko Lavinen 
4380f602ec7SJarkko Lavinen 	mmc_omap_start_command(host, data->stop);
4390f602ec7SJarkko Lavinen }
4400f602ec7SJarkko Lavinen 
441a914ded2SJuha Yrjola static void
442a914ded2SJuha Yrjola mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
443a914ded2SJuha Yrjola {
444a914ded2SJuha Yrjola 	if (host->dma_in_use)
445a914ded2SJuha Yrjola 		mmc_omap_release_dma(host, data, data->error);
446a914ded2SJuha Yrjola 
4471c6a0718SPierre Ossman 	host->data = NULL;
4481c6a0718SPierre Ossman 	host->sg_len = 0;
4491c6a0718SPierre Ossman 
4501c6a0718SPierre Ossman 	/* NOTE:  MMC layer will sometimes poll-wait CMD13 next, issuing
4511c6a0718SPierre Ossman 	 * dozens of requests until the card finishes writing data.
4521c6a0718SPierre Ossman 	 * It'd be cheaper to just wait till an EOFB interrupt arrives...
4531c6a0718SPierre Ossman 	 */
4541c6a0718SPierre Ossman 
4551c6a0718SPierre Ossman 	if (!data->stop) {
456a914ded2SJuha Yrjola 		struct mmc_host *mmc;
457a914ded2SJuha Yrjola 
4581c6a0718SPierre Ossman 		host->mrq = NULL;
459a914ded2SJuha Yrjola 		mmc = host->mmc;
4600807a9b5SJarkko Lavinen 		mmc_omap_release_slot(host->current_slot, 1);
461a914ded2SJuha Yrjola 		mmc_request_done(mmc, data->mrq);
4621c6a0718SPierre Ossman 		return;
4631c6a0718SPierre Ossman 	}
4641c6a0718SPierre Ossman 
4650f602ec7SJarkko Lavinen 	host->stop_data = data;
466b01a4f1cSVenkatraman S 	queue_work(host->mmc_omap_wq, &host->send_stop_work);
4671c6a0718SPierre Ossman }
4681c6a0718SPierre Ossman 
4691c6a0718SPierre Ossman static void
4700fb4723dSJarkko Lavinen mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
471eb1860bcSJarkko Lavinen {
472eb1860bcSJarkko Lavinen 	struct mmc_omap_slot *slot = host->current_slot;
473eb1860bcSJarkko Lavinen 	unsigned int restarts, passes, timeout;
474eb1860bcSJarkko Lavinen 	u16 stat = 0;
475eb1860bcSJarkko Lavinen 
476eb1860bcSJarkko Lavinen 	/* Sending abort takes 80 clocks. Have some extra and round up */
477eb1860bcSJarkko Lavinen 	timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
478eb1860bcSJarkko Lavinen 	restarts = 0;
4790fb4723dSJarkko Lavinen 	while (restarts < maxloops) {
480eb1860bcSJarkko Lavinen 		OMAP_MMC_WRITE(host, STAT, 0xFFFF);
481eb1860bcSJarkko Lavinen 		OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
482eb1860bcSJarkko Lavinen 
483eb1860bcSJarkko Lavinen 		passes = 0;
484eb1860bcSJarkko Lavinen 		while (passes < timeout) {
485eb1860bcSJarkko Lavinen 			stat = OMAP_MMC_READ(host, STAT);
486eb1860bcSJarkko Lavinen 			if (stat & OMAP_MMC_STAT_END_OF_CMD)
487eb1860bcSJarkko Lavinen 				goto out;
488eb1860bcSJarkko Lavinen 			udelay(1);
489eb1860bcSJarkko Lavinen 			passes++;
490eb1860bcSJarkko Lavinen 		}
491eb1860bcSJarkko Lavinen 
492eb1860bcSJarkko Lavinen 		restarts++;
493eb1860bcSJarkko Lavinen 	}
494eb1860bcSJarkko Lavinen out:
495eb1860bcSJarkko Lavinen 	OMAP_MMC_WRITE(host, STAT, stat);
496eb1860bcSJarkko Lavinen }
497eb1860bcSJarkko Lavinen 
498eb1860bcSJarkko Lavinen static void
499a914ded2SJuha Yrjola mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
500a914ded2SJuha Yrjola {
501a914ded2SJuha Yrjola 	if (host->dma_in_use)
502a914ded2SJuha Yrjola 		mmc_omap_release_dma(host, data, 1);
503a914ded2SJuha Yrjola 
504a914ded2SJuha Yrjola 	host->data = NULL;
505a914ded2SJuha Yrjola 	host->sg_len = 0;
506a914ded2SJuha Yrjola 
5070fb4723dSJarkko Lavinen 	mmc_omap_send_abort(host, 10000);
508a914ded2SJuha Yrjola }
509a914ded2SJuha Yrjola 
510a914ded2SJuha Yrjola static void
5111c6a0718SPierre Ossman mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
5121c6a0718SPierre Ossman {
5131c6a0718SPierre Ossman 	unsigned long flags;
5141c6a0718SPierre Ossman 	int done;
5151c6a0718SPierre Ossman 
5161c6a0718SPierre Ossman 	if (!host->dma_in_use) {
5171c6a0718SPierre Ossman 		mmc_omap_xfer_done(host, data);
5181c6a0718SPierre Ossman 		return;
5191c6a0718SPierre Ossman 	}
5201c6a0718SPierre Ossman 	done = 0;
5211c6a0718SPierre Ossman 	spin_lock_irqsave(&host->dma_lock, flags);
5221c6a0718SPierre Ossman 	if (host->dma_done)
5231c6a0718SPierre Ossman 		done = 1;
5241c6a0718SPierre Ossman 	else
5251c6a0718SPierre Ossman 		host->brs_received = 1;
5261c6a0718SPierre Ossman 	spin_unlock_irqrestore(&host->dma_lock, flags);
5271c6a0718SPierre Ossman 	if (done)
5281c6a0718SPierre Ossman 		mmc_omap_xfer_done(host, data);
5291c6a0718SPierre Ossman }
5301c6a0718SPierre Ossman 
5311c6a0718SPierre Ossman static void
5321c6a0718SPierre Ossman mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
5331c6a0718SPierre Ossman {
5341c6a0718SPierre Ossman 	unsigned long flags;
5351c6a0718SPierre Ossman 	int done;
5361c6a0718SPierre Ossman 
5371c6a0718SPierre Ossman 	done = 0;
5381c6a0718SPierre Ossman 	spin_lock_irqsave(&host->dma_lock, flags);
5391c6a0718SPierre Ossman 	if (host->brs_received)
5401c6a0718SPierre Ossman 		done = 1;
5411c6a0718SPierre Ossman 	else
5421c6a0718SPierre Ossman 		host->dma_done = 1;
5431c6a0718SPierre Ossman 	spin_unlock_irqrestore(&host->dma_lock, flags);
5441c6a0718SPierre Ossman 	if (done)
5451c6a0718SPierre Ossman 		mmc_omap_xfer_done(host, data);
5461c6a0718SPierre Ossman }
5471c6a0718SPierre Ossman 
5481c6a0718SPierre Ossman static void
5491c6a0718SPierre Ossman mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
5501c6a0718SPierre Ossman {
5511c6a0718SPierre Ossman 	host->cmd = NULL;
5521c6a0718SPierre Ossman 
5530fb4723dSJarkko Lavinen 	del_timer(&host->cmd_abort_timer);
554eb1860bcSJarkko Lavinen 
5551c6a0718SPierre Ossman 	if (cmd->flags & MMC_RSP_PRESENT) {
5561c6a0718SPierre Ossman 		if (cmd->flags & MMC_RSP_136) {
5571c6a0718SPierre Ossman 			/* response type 2 */
5581c6a0718SPierre Ossman 			cmd->resp[3] =
5591c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP0) |
5601c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP1) << 16);
5611c6a0718SPierre Ossman 			cmd->resp[2] =
5621c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP2) |
5631c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP3) << 16);
5641c6a0718SPierre Ossman 			cmd->resp[1] =
5651c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP4) |
5661c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP5) << 16);
5671c6a0718SPierre Ossman 			cmd->resp[0] =
5681c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP6) |
5691c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP7) << 16);
5701c6a0718SPierre Ossman 		} else {
5711c6a0718SPierre Ossman 			/* response types 1, 1b, 3, 4, 5, 6 */
5721c6a0718SPierre Ossman 			cmd->resp[0] =
5731c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP6) |
5741c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP7) << 16);
5751c6a0718SPierre Ossman 		}
5761c6a0718SPierre Ossman 	}
5771c6a0718SPierre Ossman 
57817b0429dSPierre Ossman 	if (host->data == NULL || cmd->error) {
579a914ded2SJuha Yrjola 		struct mmc_host *mmc;
580a914ded2SJuha Yrjola 
581a914ded2SJuha Yrjola 		if (host->data != NULL)
582a914ded2SJuha Yrjola 			mmc_omap_abort_xfer(host, host->data);
5831c6a0718SPierre Ossman 		host->mrq = NULL;
584a914ded2SJuha Yrjola 		mmc = host->mmc;
5850807a9b5SJarkko Lavinen 		mmc_omap_release_slot(host->current_slot, 1);
586a914ded2SJuha Yrjola 		mmc_request_done(mmc, cmd->mrq);
5871c6a0718SPierre Ossman 	}
5881c6a0718SPierre Ossman }
5891c6a0718SPierre Ossman 
590eb1860bcSJarkko Lavinen /*
591eb1860bcSJarkko Lavinen  * Abort stuck command. Can occur when card is removed while it is being
592eb1860bcSJarkko Lavinen  * read.
593eb1860bcSJarkko Lavinen  */
594eb1860bcSJarkko Lavinen static void mmc_omap_abort_command(struct work_struct *work)
595eb1860bcSJarkko Lavinen {
596eb1860bcSJarkko Lavinen 	struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
5970fb4723dSJarkko Lavinen 						  cmd_abort_work);
5980fb4723dSJarkko Lavinen 	BUG_ON(!host->cmd);
599eb1860bcSJarkko Lavinen 
600eb1860bcSJarkko Lavinen 	dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
601eb1860bcSJarkko Lavinen 		host->cmd->opcode);
602eb1860bcSJarkko Lavinen 
6030fb4723dSJarkko Lavinen 	if (host->cmd->error == 0)
604eb1860bcSJarkko Lavinen 		host->cmd->error = -ETIMEDOUT;
6050fb4723dSJarkko Lavinen 
6060fb4723dSJarkko Lavinen 	if (host->data == NULL) {
6070fb4723dSJarkko Lavinen 		struct mmc_command *cmd;
6080fb4723dSJarkko Lavinen 		struct mmc_host    *mmc;
6090fb4723dSJarkko Lavinen 
6100fb4723dSJarkko Lavinen 		cmd = host->cmd;
6110fb4723dSJarkko Lavinen 		host->cmd = NULL;
6120fb4723dSJarkko Lavinen 		mmc_omap_send_abort(host, 10000);
6130fb4723dSJarkko Lavinen 
6140fb4723dSJarkko Lavinen 		host->mrq = NULL;
6150fb4723dSJarkko Lavinen 		mmc = host->mmc;
6160807a9b5SJarkko Lavinen 		mmc_omap_release_slot(host->current_slot, 1);
6170fb4723dSJarkko Lavinen 		mmc_request_done(mmc, cmd->mrq);
6180fb4723dSJarkko Lavinen 	} else
619eb1860bcSJarkko Lavinen 		mmc_omap_cmd_done(host, host->cmd);
6200fb4723dSJarkko Lavinen 
6210fb4723dSJarkko Lavinen 	host->abort = 0;
6220fb4723dSJarkko Lavinen 	enable_irq(host->irq);
623eb1860bcSJarkko Lavinen }
624eb1860bcSJarkko Lavinen 
625eb1860bcSJarkko Lavinen static void
626eb1860bcSJarkko Lavinen mmc_omap_cmd_timer(unsigned long data)
627eb1860bcSJarkko Lavinen {
628eb1860bcSJarkko Lavinen 	struct mmc_omap_host *host = (struct mmc_omap_host *) data;
6290fb4723dSJarkko Lavinen 	unsigned long flags;
630eb1860bcSJarkko Lavinen 
6310fb4723dSJarkko Lavinen 	spin_lock_irqsave(&host->slot_lock, flags);
6320fb4723dSJarkko Lavinen 	if (host->cmd != NULL && !host->abort) {
6330fb4723dSJarkko Lavinen 		OMAP_MMC_WRITE(host, IE, 0);
6340fb4723dSJarkko Lavinen 		disable_irq(host->irq);
6350fb4723dSJarkko Lavinen 		host->abort = 1;
636b01a4f1cSVenkatraman S 		queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
6370fb4723dSJarkko Lavinen 	}
6380fb4723dSJarkko Lavinen 	spin_unlock_irqrestore(&host->slot_lock, flags);
639eb1860bcSJarkko Lavinen }
640eb1860bcSJarkko Lavinen 
6411c6a0718SPierre Ossman /* PIO only */
6421c6a0718SPierre Ossman static void
6431c6a0718SPierre Ossman mmc_omap_sg_to_buf(struct mmc_omap_host *host)
6441c6a0718SPierre Ossman {
6451c6a0718SPierre Ossman 	struct scatterlist *sg;
6461c6a0718SPierre Ossman 
6471c6a0718SPierre Ossman 	sg = host->data->sg + host->sg_idx;
6481c6a0718SPierre Ossman 	host->buffer_bytes_left = sg->length;
64945711f1aSJens Axboe 	host->buffer = sg_virt(sg);
6501c6a0718SPierre Ossman 	if (host->buffer_bytes_left > host->total_bytes_left)
6511c6a0718SPierre Ossman 		host->buffer_bytes_left = host->total_bytes_left;
6521c6a0718SPierre Ossman }
6531c6a0718SPierre Ossman 
6540807a9b5SJarkko Lavinen static void
6550807a9b5SJarkko Lavinen mmc_omap_clk_timer(unsigned long data)
6560807a9b5SJarkko Lavinen {
6570807a9b5SJarkko Lavinen 	struct mmc_omap_host *host = (struct mmc_omap_host *) data;
6580807a9b5SJarkko Lavinen 
6590807a9b5SJarkko Lavinen 	mmc_omap_fclk_enable(host, 0);
6600807a9b5SJarkko Lavinen }
6610807a9b5SJarkko Lavinen 
6621c6a0718SPierre Ossman /* PIO only */
6631c6a0718SPierre Ossman static void
6641c6a0718SPierre Ossman mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
6651c6a0718SPierre Ossman {
66675b53aeeSPaul Walmsley 	int n, nwords;
6671c6a0718SPierre Ossman 
6681c6a0718SPierre Ossman 	if (host->buffer_bytes_left == 0) {
6691c6a0718SPierre Ossman 		host->sg_idx++;
6701c6a0718SPierre Ossman 		BUG_ON(host->sg_idx == host->sg_len);
6711c6a0718SPierre Ossman 		mmc_omap_sg_to_buf(host);
6721c6a0718SPierre Ossman 	}
6731c6a0718SPierre Ossman 	n = 64;
6741c6a0718SPierre Ossman 	if (n > host->buffer_bytes_left)
6751c6a0718SPierre Ossman 		n = host->buffer_bytes_left;
67675b53aeeSPaul Walmsley 
67775b53aeeSPaul Walmsley 	nwords = n / 2;
67875b53aeeSPaul Walmsley 	nwords += n & 1; /* handle odd number of bytes to transfer */
67975b53aeeSPaul Walmsley 
6801c6a0718SPierre Ossman 	host->buffer_bytes_left -= n;
6811c6a0718SPierre Ossman 	host->total_bytes_left -= n;
6821c6a0718SPierre Ossman 	host->data->bytes_xfered += n;
6831c6a0718SPierre Ossman 
6841c6a0718SPierre Ossman 	if (write) {
68575b53aeeSPaul Walmsley 		__raw_writesw(host->virt_base + OMAP_MMC_REG(host, DATA),
68675b53aeeSPaul Walmsley 			      host->buffer, nwords);
6871c6a0718SPierre Ossman 	} else {
68875b53aeeSPaul Walmsley 		__raw_readsw(host->virt_base + OMAP_MMC_REG(host, DATA),
68975b53aeeSPaul Walmsley 			     host->buffer, nwords);
6901c6a0718SPierre Ossman 	}
69175b53aeeSPaul Walmsley 
69275b53aeeSPaul Walmsley 	host->buffer += nwords;
6931c6a0718SPierre Ossman }
6941c6a0718SPierre Ossman 
69575d569d3SVenkatraman S #ifdef CONFIG_MMC_DEBUG
69675d569d3SVenkatraman S static void mmc_omap_report_irq(struct mmc_omap_host *host, u16 status)
6971c6a0718SPierre Ossman {
6981c6a0718SPierre Ossman 	static const char *mmc_omap_status_bits[] = {
6991c6a0718SPierre Ossman 		"EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
7001c6a0718SPierre Ossman 		"CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
7011c6a0718SPierre Ossman 	};
70275d569d3SVenkatraman S 	int i;
70375d569d3SVenkatraman S 	char res[64], *buf = res;
70475d569d3SVenkatraman S 
70575d569d3SVenkatraman S 	buf += sprintf(buf, "MMC IRQ 0x%x:", status);
7061c6a0718SPierre Ossman 
7071c6a0718SPierre Ossman 	for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
70875d569d3SVenkatraman S 		if (status & (1 << i))
70975d569d3SVenkatraman S 			buf += sprintf(buf, " %s", mmc_omap_status_bits[i]);
71075d569d3SVenkatraman S 	dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
7111c6a0718SPierre Ossman }
71275d569d3SVenkatraman S #else
71375d569d3SVenkatraman S static void mmc_omap_report_irq(struct mmc_omap_host *host, u16 status)
71475d569d3SVenkatraman S {
7151c6a0718SPierre Ossman }
71675d569d3SVenkatraman S #endif
71775d569d3SVenkatraman S 
7181c6a0718SPierre Ossman 
7191c6a0718SPierre Ossman static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
7201c6a0718SPierre Ossman {
7211c6a0718SPierre Ossman 	struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
7221c6a0718SPierre Ossman 	u16 status;
7231c6a0718SPierre Ossman 	int end_command;
7241c6a0718SPierre Ossman 	int end_transfer;
7252a50b888SJuha Yrjola 	int transfer_error, cmd_error;
7261c6a0718SPierre Ossman 
7271c6a0718SPierre Ossman 	if (host->cmd == NULL && host->data == NULL) {
7281c6a0718SPierre Ossman 		status = OMAP_MMC_READ(host, STAT);
7292a50b888SJuha Yrjola 		dev_info(mmc_dev(host->slots[0]->mmc),
7302a50b888SJuha Yrjola 			 "Spurious IRQ 0x%04x\n", status);
7311c6a0718SPierre Ossman 		if (status != 0) {
7321c6a0718SPierre Ossman 			OMAP_MMC_WRITE(host, STAT, status);
7331c6a0718SPierre Ossman 			OMAP_MMC_WRITE(host, IE, 0);
7341c6a0718SPierre Ossman 		}
7351c6a0718SPierre Ossman 		return IRQ_HANDLED;
7361c6a0718SPierre Ossman 	}
7371c6a0718SPierre Ossman 
7381c6a0718SPierre Ossman 	end_command = 0;
7391c6a0718SPierre Ossman 	end_transfer = 0;
7401c6a0718SPierre Ossman 	transfer_error = 0;
7412a50b888SJuha Yrjola 	cmd_error = 0;
7421c6a0718SPierre Ossman 
7431c6a0718SPierre Ossman 	while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
7442a50b888SJuha Yrjola 		int cmd;
7452a50b888SJuha Yrjola 
7461c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, STAT, status);
7472a50b888SJuha Yrjola 		if (host->cmd != NULL)
7482a50b888SJuha Yrjola 			cmd = host->cmd->opcode;
7492a50b888SJuha Yrjola 		else
7502a50b888SJuha Yrjola 			cmd = -1;
7511c6a0718SPierre Ossman 		dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
7522a50b888SJuha Yrjola 			status, cmd);
75375d569d3SVenkatraman S 		mmc_omap_report_irq(host, status);
75475d569d3SVenkatraman S 
7551c6a0718SPierre Ossman 		if (host->total_bytes_left) {
7561c6a0718SPierre Ossman 			if ((status & OMAP_MMC_STAT_A_FULL) ||
7571c6a0718SPierre Ossman 			    (status & OMAP_MMC_STAT_END_OF_DATA))
7581c6a0718SPierre Ossman 				mmc_omap_xfer_data(host, 0);
7591c6a0718SPierre Ossman 			if (status & OMAP_MMC_STAT_A_EMPTY)
7601c6a0718SPierre Ossman 				mmc_omap_xfer_data(host, 1);
7611c6a0718SPierre Ossman 		}
7621c6a0718SPierre Ossman 
7632a50b888SJuha Yrjola 		if (status & OMAP_MMC_STAT_END_OF_DATA)
7641c6a0718SPierre Ossman 			end_transfer = 1;
7651c6a0718SPierre Ossman 
7661c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_DATA_TOUT) {
7672a50b888SJuha Yrjola 			dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
7682a50b888SJuha Yrjola 				cmd);
7691c6a0718SPierre Ossman 			if (host->data) {
77017b0429dSPierre Ossman 				host->data->error = -ETIMEDOUT;
7711c6a0718SPierre Ossman 				transfer_error = 1;
7721c6a0718SPierre Ossman 			}
7731c6a0718SPierre Ossman 		}
7741c6a0718SPierre Ossman 
7751c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_DATA_CRC) {
7761c6a0718SPierre Ossman 			if (host->data) {
77717b0429dSPierre Ossman 				host->data->error = -EILSEQ;
7781c6a0718SPierre Ossman 				dev_dbg(mmc_dev(host->mmc),
7791c6a0718SPierre Ossman 					 "data CRC error, bytes left %d\n",
7801c6a0718SPierre Ossman 					host->total_bytes_left);
7811c6a0718SPierre Ossman 				transfer_error = 1;
7821c6a0718SPierre Ossman 			} else {
7831c6a0718SPierre Ossman 				dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
7841c6a0718SPierre Ossman 			}
7851c6a0718SPierre Ossman 		}
7861c6a0718SPierre Ossman 
7871c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_CMD_TOUT) {
7881c6a0718SPierre Ossman 			/* Timeouts are routine with some commands */
7891c6a0718SPierre Ossman 			if (host->cmd) {
790abfbe5f7SJuha Yrjola 				struct mmc_omap_slot *slot =
791abfbe5f7SJuha Yrjola 					host->current_slot;
7922a50b888SJuha Yrjola 				if (slot == NULL ||
7932a50b888SJuha Yrjola 				    !mmc_omap_cover_is_open(slot))
7941c6a0718SPierre Ossman 					dev_err(mmc_dev(host->mmc),
7952a50b888SJuha Yrjola 						"command timeout (CMD%d)\n",
7962a50b888SJuha Yrjola 						cmd);
79717b0429dSPierre Ossman 				host->cmd->error = -ETIMEDOUT;
7981c6a0718SPierre Ossman 				end_command = 1;
7992a50b888SJuha Yrjola 				cmd_error = 1;
8001c6a0718SPierre Ossman 			}
8011c6a0718SPierre Ossman 		}
8021c6a0718SPierre Ossman 
8031c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_CMD_CRC) {
8041c6a0718SPierre Ossman 			if (host->cmd) {
8051c6a0718SPierre Ossman 				dev_err(mmc_dev(host->mmc),
8061c6a0718SPierre Ossman 					"command CRC error (CMD%d, arg 0x%08x)\n",
8072a50b888SJuha Yrjola 					cmd, host->cmd->arg);
80817b0429dSPierre Ossman 				host->cmd->error = -EILSEQ;
8091c6a0718SPierre Ossman 				end_command = 1;
8102a50b888SJuha Yrjola 				cmd_error = 1;
8111c6a0718SPierre Ossman 			} else
8121c6a0718SPierre Ossman 				dev_err(mmc_dev(host->mmc),
8131c6a0718SPierre Ossman 					"command CRC error without cmd?\n");
8141c6a0718SPierre Ossman 		}
8151c6a0718SPierre Ossman 
8161c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_CARD_ERR) {
8170107a4b3SRagner Magalhaes 			dev_dbg(mmc_dev(host->mmc),
8180107a4b3SRagner Magalhaes 				"ignoring card status error (CMD%d)\n",
8192a50b888SJuha Yrjola 				cmd);
8201c6a0718SPierre Ossman 			end_command = 1;
8211c6a0718SPierre Ossman 		}
8221c6a0718SPierre Ossman 
8231c6a0718SPierre Ossman 		/*
8241c6a0718SPierre Ossman 		 * NOTE: On 1610 the END_OF_CMD may come too early when
8251c6a0718SPierre Ossman 		 * starting a write
8261c6a0718SPierre Ossman 		 */
8271c6a0718SPierre Ossman 		if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
8281c6a0718SPierre Ossman 		    (!(status & OMAP_MMC_STAT_A_EMPTY))) {
8291c6a0718SPierre Ossman 			end_command = 1;
8301c6a0718SPierre Ossman 		}
8311c6a0718SPierre Ossman 	}
8321c6a0718SPierre Ossman 
8330fb4723dSJarkko Lavinen 	if (cmd_error && host->data) {
8340fb4723dSJarkko Lavinen 		del_timer(&host->cmd_abort_timer);
8350fb4723dSJarkko Lavinen 		host->abort = 1;
8360fb4723dSJarkko Lavinen 		OMAP_MMC_WRITE(host, IE, 0);
837e749c6f2SBen Nizette 		disable_irq_nosync(host->irq);
838b01a4f1cSVenkatraman S 		queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
8390fb4723dSJarkko Lavinen 		return IRQ_HANDLED;
8400fb4723dSJarkko Lavinen 	}
8410fb4723dSJarkko Lavinen 
842f6947514SMichael Buesch 	if (end_command && host->cmd)
8431c6a0718SPierre Ossman 		mmc_omap_cmd_done(host, host->cmd);
8442a50b888SJuha Yrjola 	if (host->data != NULL) {
8451c6a0718SPierre Ossman 		if (transfer_error)
8461c6a0718SPierre Ossman 			mmc_omap_xfer_done(host, host->data);
8471c6a0718SPierre Ossman 		else if (end_transfer)
8481c6a0718SPierre Ossman 			mmc_omap_end_of_data(host, host->data);
8492a50b888SJuha Yrjola 	}
8501c6a0718SPierre Ossman 
8511c6a0718SPierre Ossman 	return IRQ_HANDLED;
8521c6a0718SPierre Ossman }
8531c6a0718SPierre Ossman 
8547584d276SJarkko Lavinen void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
8555a0f3f1fSJuha Yrjola {
8567584d276SJarkko Lavinen 	int cover_open;
8575a0f3f1fSJuha Yrjola 	struct mmc_omap_host *host = dev_get_drvdata(dev);
8587584d276SJarkko Lavinen 	struct mmc_omap_slot *slot = host->slots[num];
8595a0f3f1fSJuha Yrjola 
8607584d276SJarkko Lavinen 	BUG_ON(num >= host->nr_slots);
8615a0f3f1fSJuha Yrjola 
8625a0f3f1fSJuha Yrjola 	/* Other subsystems can call in here before we're initialised. */
8637584d276SJarkko Lavinen 	if (host->nr_slots == 0 || !host->slots[num])
8645a0f3f1fSJuha Yrjola 		return;
8655a0f3f1fSJuha Yrjola 
8665a0f3f1fSJuha Yrjola 	cover_open = mmc_omap_cover_is_open(slot);
8675a0f3f1fSJuha Yrjola 	if (cover_open != slot->cover_open) {
8685a0f3f1fSJuha Yrjola 		slot->cover_open = cover_open;
8697584d276SJarkko Lavinen 		sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
8705a0f3f1fSJuha Yrjola 	}
8717584d276SJarkko Lavinen 
8727584d276SJarkko Lavinen 	tasklet_hi_schedule(&slot->cover_tasklet);
8737584d276SJarkko Lavinen }
8747584d276SJarkko Lavinen 
8757584d276SJarkko Lavinen static void mmc_omap_cover_timer(unsigned long arg)
8767584d276SJarkko Lavinen {
8777584d276SJarkko Lavinen 	struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
8787584d276SJarkko Lavinen 	tasklet_schedule(&slot->cover_tasklet);
8797584d276SJarkko Lavinen }
8807584d276SJarkko Lavinen 
8817584d276SJarkko Lavinen static void mmc_omap_cover_handler(unsigned long param)
8827584d276SJarkko Lavinen {
8837584d276SJarkko Lavinen 	struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
8847584d276SJarkko Lavinen 	int cover_open = mmc_omap_cover_is_open(slot);
8857584d276SJarkko Lavinen 
8867584d276SJarkko Lavinen 	mmc_detect_change(slot->mmc, 0);
8877584d276SJarkko Lavinen 	if (!cover_open)
8887584d276SJarkko Lavinen 		return;
8897584d276SJarkko Lavinen 
8907584d276SJarkko Lavinen 	/*
8917584d276SJarkko Lavinen 	 * If no card is inserted, we postpone polling until
8927584d276SJarkko Lavinen 	 * the cover has been closed.
8937584d276SJarkko Lavinen 	 */
8947584d276SJarkko Lavinen 	if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
8957584d276SJarkko Lavinen 		return;
8967584d276SJarkko Lavinen 
8977584d276SJarkko Lavinen 	mod_timer(&slot->cover_timer,
8987584d276SJarkko Lavinen 		  jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
8995a0f3f1fSJuha Yrjola }
9005a0f3f1fSJuha Yrjola 
9013451c067SRussell King static void mmc_omap_dma_callback(void *priv)
9023451c067SRussell King {
9033451c067SRussell King 	struct mmc_omap_host *host = priv;
9043451c067SRussell King 	struct mmc_data *data = host->data;
9053451c067SRussell King 
9063451c067SRussell King 	/* If we got to the end of DMA, assume everything went well */
9073451c067SRussell King 	data->bytes_xfered += data->blocks * data->blksz;
9083451c067SRussell King 
9093451c067SRussell King 	mmc_omap_dma_done(host, data);
9103451c067SRussell King }
9113451c067SRussell King 
9121c6a0718SPierre Ossman static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
9131c6a0718SPierre Ossman {
9141c6a0718SPierre Ossman 	u16 reg;
9151c6a0718SPierre Ossman 
9161c6a0718SPierre Ossman 	reg = OMAP_MMC_READ(host, SDIO);
9171c6a0718SPierre Ossman 	reg &= ~(1 << 5);
9181c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, SDIO, reg);
9191c6a0718SPierre Ossman 	/* Set maximum timeout */
9201c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, CTO, 0xff);
9211c6a0718SPierre Ossman }
9221c6a0718SPierre Ossman 
9231c6a0718SPierre Ossman static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
9241c6a0718SPierre Ossman {
925b8f9f0e9SJuha Yrjola 	unsigned int timeout, cycle_ns;
9261c6a0718SPierre Ossman 	u16 reg;
9271c6a0718SPierre Ossman 
928b8f9f0e9SJuha Yrjola 	cycle_ns = 1000000000 / host->current_slot->fclk_freq;
929b8f9f0e9SJuha Yrjola 	timeout = req->data->timeout_ns / cycle_ns;
930b8f9f0e9SJuha Yrjola 	timeout += req->data->timeout_clks;
9311c6a0718SPierre Ossman 
9321c6a0718SPierre Ossman 	/* Check if we need to use timeout multiplier register */
9331c6a0718SPierre Ossman 	reg = OMAP_MMC_READ(host, SDIO);
9341c6a0718SPierre Ossman 	if (timeout > 0xffff) {
9351c6a0718SPierre Ossman 		reg |= (1 << 5);
9361c6a0718SPierre Ossman 		timeout /= 1024;
9371c6a0718SPierre Ossman 	} else
9381c6a0718SPierre Ossman 		reg &= ~(1 << 5);
9391c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, SDIO, reg);
9401c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, DTO, timeout);
9411c6a0718SPierre Ossman }
9421c6a0718SPierre Ossman 
9431c6a0718SPierre Ossman static void
9441c6a0718SPierre Ossman mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
9451c6a0718SPierre Ossman {
9461c6a0718SPierre Ossman 	struct mmc_data *data = req->data;
9471c6a0718SPierre Ossman 	int i, use_dma, block_size;
9481c6a0718SPierre Ossman 	unsigned sg_len;
9491c6a0718SPierre Ossman 
9501c6a0718SPierre Ossman 	host->data = data;
9511c6a0718SPierre Ossman 	if (data == NULL) {
9521c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, BLEN, 0);
9531c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, NBLK, 0);
9541c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, BUF, 0);
9551c6a0718SPierre Ossman 		host->dma_in_use = 0;
9561c6a0718SPierre Ossman 		set_cmd_timeout(host, req);
9571c6a0718SPierre Ossman 		return;
9581c6a0718SPierre Ossman 	}
9591c6a0718SPierre Ossman 
9601c6a0718SPierre Ossman 	block_size = data->blksz;
9611c6a0718SPierre Ossman 
9621c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
9631c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, BLEN, block_size - 1);
9641c6a0718SPierre Ossman 	set_data_timeout(host, req);
9651c6a0718SPierre Ossman 
9661c6a0718SPierre Ossman 	/* cope with calling layer confusion; it issues "single
9671c6a0718SPierre Ossman 	 * block" writes using multi-block scatterlists.
9681c6a0718SPierre Ossman 	 */
9691c6a0718SPierre Ossman 	sg_len = (data->blocks == 1) ? 1 : data->sg_len;
9701c6a0718SPierre Ossman 
9711c6a0718SPierre Ossman 	/* Only do DMA for entire blocks */
9721c6a0718SPierre Ossman 	use_dma = host->use_dma;
9731c6a0718SPierre Ossman 	if (use_dma) {
9741c6a0718SPierre Ossman 		for (i = 0; i < sg_len; i++) {
9751c6a0718SPierre Ossman 			if ((data->sg[i].length % block_size) != 0) {
9761c6a0718SPierre Ossman 				use_dma = 0;
9771c6a0718SPierre Ossman 				break;
9781c6a0718SPierre Ossman 			}
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)
1241abfbe5f7SJuha Yrjola 		mmc->caps |= MMC_CAP_4_BIT_DATA;
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 
1264abfbe5f7SJuha Yrjola 	r = mmc_add_host(mmc);
1265abfbe5f7SJuha Yrjola 	if (r < 0)
1266abfbe5f7SJuha Yrjola 		goto err_remove_host;
1267abfbe5f7SJuha Yrjola 
1268abfbe5f7SJuha Yrjola 	if (slot->pdata->name != NULL) {
1269abfbe5f7SJuha Yrjola 		r = device_create_file(&mmc->class_dev,
1270abfbe5f7SJuha Yrjola 					&dev_attr_slot_name);
1271abfbe5f7SJuha Yrjola 		if (r < 0)
1272abfbe5f7SJuha Yrjola 			goto err_remove_host;
1273abfbe5f7SJuha Yrjola 	}
1274abfbe5f7SJuha Yrjola 
12755a0f3f1fSJuha Yrjola 	if (slot->pdata->get_cover_state != NULL) {
12765a0f3f1fSJuha Yrjola 		r = device_create_file(&mmc->class_dev,
12775a0f3f1fSJuha Yrjola 					&dev_attr_cover_switch);
12785a0f3f1fSJuha Yrjola 		if (r < 0)
12795a0f3f1fSJuha Yrjola 			goto err_remove_slot_name;
12805a0f3f1fSJuha Yrjola 
12817584d276SJarkko Lavinen 		setup_timer(&slot->cover_timer, mmc_omap_cover_timer,
128201e77e13SCarlos Eduardo Aguiar 			    (unsigned long)slot);
12837584d276SJarkko Lavinen 		tasklet_init(&slot->cover_tasklet, mmc_omap_cover_handler,
12847584d276SJarkko Lavinen 			     (unsigned long)slot);
12857584d276SJarkko Lavinen 		tasklet_schedule(&slot->cover_tasklet);
12865a0f3f1fSJuha Yrjola 	}
12875a0f3f1fSJuha Yrjola 
1288abfbe5f7SJuha Yrjola 	return 0;
1289abfbe5f7SJuha Yrjola 
12905a0f3f1fSJuha Yrjola err_remove_slot_name:
12915a0f3f1fSJuha Yrjola 	if (slot->pdata->name != NULL)
12925a0f3f1fSJuha Yrjola 		device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1293abfbe5f7SJuha Yrjola err_remove_host:
1294abfbe5f7SJuha Yrjola 	mmc_remove_host(mmc);
1295abfbe5f7SJuha Yrjola 	mmc_free_host(mmc);
1296abfbe5f7SJuha Yrjola 	return r;
1297abfbe5f7SJuha Yrjola }
1298abfbe5f7SJuha Yrjola 
1299abfbe5f7SJuha Yrjola static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
1300abfbe5f7SJuha Yrjola {
1301abfbe5f7SJuha Yrjola 	struct mmc_host *mmc = slot->mmc;
1302abfbe5f7SJuha Yrjola 
1303abfbe5f7SJuha Yrjola 	if (slot->pdata->name != NULL)
1304abfbe5f7SJuha Yrjola 		device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
13055a0f3f1fSJuha Yrjola 	if (slot->pdata->get_cover_state != NULL)
13065a0f3f1fSJuha Yrjola 		device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
13075a0f3f1fSJuha Yrjola 
13087584d276SJarkko Lavinen 	tasklet_kill(&slot->cover_tasklet);
13097584d276SJarkko Lavinen 	del_timer_sync(&slot->cover_timer);
1310b01a4f1cSVenkatraman S 	flush_workqueue(slot->host->mmc_omap_wq);
1311abfbe5f7SJuha Yrjola 
1312abfbe5f7SJuha Yrjola 	mmc_remove_host(mmc);
1313abfbe5f7SJuha Yrjola 	mmc_free_host(mmc);
1314abfbe5f7SJuha Yrjola }
1315abfbe5f7SJuha Yrjola 
1316c3be1efdSBill Pemberton static int mmc_omap_probe(struct platform_device *pdev)
13171c6a0718SPierre Ossman {
1318abfbe5f7SJuha Yrjola 	struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
13191c6a0718SPierre Ossman 	struct mmc_omap_host *host = NULL;
13201c6a0718SPierre Ossman 	struct resource *res;
13213451c067SRussell King 	dma_cap_mask_t mask;
1322*31ee9181STony Lindgren 	unsigned sig = 0;
1323abfbe5f7SJuha Yrjola 	int i, ret = 0;
13241c6a0718SPierre Ossman 	int irq;
13251c6a0718SPierre Ossman 
1326abfbe5f7SJuha Yrjola 	if (pdata == NULL) {
13271c6a0718SPierre Ossman 		dev_err(&pdev->dev, "platform data missing\n");
13281c6a0718SPierre Ossman 		return -ENXIO;
13291c6a0718SPierre Ossman 	}
1330abfbe5f7SJuha Yrjola 	if (pdata->nr_slots == 0) {
1331abfbe5f7SJuha Yrjola 		dev_err(&pdev->dev, "no slots\n");
1332abfbe5f7SJuha Yrjola 		return -ENXIO;
1333abfbe5f7SJuha Yrjola 	}
13341c6a0718SPierre Ossman 
13351c6a0718SPierre Ossman 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
13361c6a0718SPierre Ossman 	irq = platform_get_irq(pdev, 0);
13371c6a0718SPierre Ossman 	if (res == NULL || irq < 0)
13381c6a0718SPierre Ossman 		return -ENXIO;
13391c6a0718SPierre Ossman 
13402092014dSChris Ball 	res = request_mem_region(res->start, resource_size(res),
13411c6a0718SPierre Ossman 				 pdev->name);
13421c6a0718SPierre Ossman 	if (res == NULL)
13431c6a0718SPierre Ossman 		return -EBUSY;
13441c6a0718SPierre Ossman 
1345abfbe5f7SJuha Yrjola 	host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
1346abfbe5f7SJuha Yrjola 	if (host == NULL) {
13471c6a0718SPierre Ossman 		ret = -ENOMEM;
13481c6a0718SPierre Ossman 		goto err_free_mem_region;
13491c6a0718SPierre Ossman 	}
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->mem_res = res;
13721c6a0718SPierre Ossman 	host->irq = irq;
1373abfbe5f7SJuha Yrjola 	host->use_dma = 1;
1374abfbe5f7SJuha Yrjola 	host->irq = irq;
1375abfbe5f7SJuha Yrjola 	host->phys_base = host->mem_res->start;
13762092014dSChris Ball 	host->virt_base = ioremap(res->start, resource_size(res));
137755c381e4SRussell King 	if (!host->virt_base)
137855c381e4SRussell King 		goto err_ioremap;
1379abfbe5f7SJuha Yrjola 
13805c9e02b1SRussell King 	host->iclk = clk_get(&pdev->dev, "ick");
1381e799acb2SLadislav Michl 	if (IS_ERR(host->iclk)) {
1382e799acb2SLadislav Michl 		ret = PTR_ERR(host->iclk);
13831c6a0718SPierre Ossman 		goto err_free_mmc_host;
1384e799acb2SLadislav Michl 	}
13851c6a0718SPierre Ossman 	clk_enable(host->iclk);
13861c6a0718SPierre Ossman 
13875c9e02b1SRussell King 	host->fclk = clk_get(&pdev->dev, "fck");
13881c6a0718SPierre Ossman 	if (IS_ERR(host->fclk)) {
13891c6a0718SPierre Ossman 		ret = PTR_ERR(host->fclk);
13901c6a0718SPierre Ossman 		goto err_free_iclk;
13911c6a0718SPierre Ossman 	}
13921c6a0718SPierre Ossman 
13933451c067SRussell King 	dma_cap_zero(mask);
13943451c067SRussell King 	dma_cap_set(DMA_SLAVE, mask);
13953451c067SRussell King 
13963451c067SRussell King 	host->dma_tx_burst = -1;
13973451c067SRussell King 	host->dma_rx_burst = -1;
13983451c067SRussell King 
1399*31ee9181STony Lindgren 	res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1400*31ee9181STony Lindgren 	if (res)
1401*31ee9181STony Lindgren 		sig = res->start;
1402*31ee9181STony Lindgren 	host->dma_tx = dma_request_slave_channel_compat(mask,
1403*31ee9181STony Lindgren 				omap_dma_filter_fn, &sig, &pdev->dev, "tx");
14043451c067SRussell King 	if (!host->dma_tx)
14053451c067SRussell King 		dev_warn(host->dev, "unable to obtain TX DMA engine channel %u\n",
14063451c067SRussell King 			sig);
1407*31ee9181STony Lindgren 
1408*31ee9181STony Lindgren 	res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1409*31ee9181STony Lindgren 	if (res)
1410*31ee9181STony Lindgren 		sig = res->start;
1411*31ee9181STony Lindgren 	host->dma_rx = dma_request_slave_channel_compat(mask,
1412*31ee9181STony Lindgren 				omap_dma_filter_fn, &sig, &pdev->dev, "rx");
14133451c067SRussell King 	if (!host->dma_rx)
14143451c067SRussell King 		dev_warn(host->dev, "unable to obtain RX DMA engine channel %u\n",
14153451c067SRussell King 			sig);
14163451c067SRussell King 
14171c6a0718SPierre Ossman 	ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
14181c6a0718SPierre Ossman 	if (ret)
14193451c067SRussell King 		goto err_free_dma;
14201c6a0718SPierre Ossman 
1421abfbe5f7SJuha Yrjola 	if (pdata->init != NULL) {
1422abfbe5f7SJuha Yrjola 		ret = pdata->init(&pdev->dev);
1423abfbe5f7SJuha Yrjola 		if (ret < 0)
1424abfbe5f7SJuha Yrjola 			goto err_free_irq;
1425abfbe5f7SJuha Yrjola 	}
14261c6a0718SPierre Ossman 
1427abfbe5f7SJuha Yrjola 	host->nr_slots = pdata->nr_slots;
142853db20d1STony Lindgren 	host->reg_shift = (mmc_omap7xx() ? 1 : 2);
14293caf4140STony Lindgren 
14303caf4140STony Lindgren 	host->mmc_omap_wq = alloc_workqueue("mmc_omap", 0, 0);
14313caf4140STony Lindgren 	if (!host->mmc_omap_wq)
14323caf4140STony Lindgren 		goto err_plat_cleanup;
14333caf4140STony Lindgren 
1434abfbe5f7SJuha Yrjola 	for (i = 0; i < pdata->nr_slots; i++) {
1435abfbe5f7SJuha Yrjola 		ret = mmc_omap_new_slot(host, i);
1436abfbe5f7SJuha Yrjola 		if (ret < 0) {
1437abfbe5f7SJuha Yrjola 			while (--i >= 0)
1438abfbe5f7SJuha Yrjola 				mmc_omap_remove_slot(host->slots[i]);
1439abfbe5f7SJuha Yrjola 
14403caf4140STony Lindgren 			goto err_destroy_wq;
1441abfbe5f7SJuha Yrjola 		}
1442abfbe5f7SJuha Yrjola 	}
14431c6a0718SPierre Ossman 
14441c6a0718SPierre Ossman 	return 0;
14451c6a0718SPierre Ossman 
14463caf4140STony Lindgren err_destroy_wq:
14473caf4140STony Lindgren 	destroy_workqueue(host->mmc_omap_wq);
1448abfbe5f7SJuha Yrjola err_plat_cleanup:
1449abfbe5f7SJuha Yrjola 	if (pdata->cleanup)
1450abfbe5f7SJuha Yrjola 		pdata->cleanup(&pdev->dev);
1451abfbe5f7SJuha Yrjola err_free_irq:
1452abfbe5f7SJuha Yrjola 	free_irq(host->irq, host);
14533451c067SRussell King err_free_dma:
14543451c067SRussell King 	if (host->dma_tx)
14553451c067SRussell King 		dma_release_channel(host->dma_tx);
14563451c067SRussell King 	if (host->dma_rx)
14573451c067SRussell King 		dma_release_channel(host->dma_rx);
14581c6a0718SPierre Ossman 	clk_put(host->fclk);
14591c6a0718SPierre Ossman err_free_iclk:
14601c6a0718SPierre Ossman 	clk_disable(host->iclk);
14611c6a0718SPierre Ossman 	clk_put(host->iclk);
14621c6a0718SPierre Ossman err_free_mmc_host:
146355c381e4SRussell King 	iounmap(host->virt_base);
146455c381e4SRussell King err_ioremap:
1465abfbe5f7SJuha Yrjola 	kfree(host);
14661c6a0718SPierre Ossman err_free_mem_region:
14672092014dSChris Ball 	release_mem_region(res->start, resource_size(res));
14681c6a0718SPierre Ossman 	return ret;
14691c6a0718SPierre Ossman }
14701c6a0718SPierre Ossman 
14716e0ee714SBill Pemberton static int mmc_omap_remove(struct platform_device *pdev)
14721c6a0718SPierre Ossman {
14731c6a0718SPierre Ossman 	struct mmc_omap_host *host = platform_get_drvdata(pdev);
1474abfbe5f7SJuha Yrjola 	int i;
14751c6a0718SPierre Ossman 
14761c6a0718SPierre Ossman 	BUG_ON(host == NULL);
14771c6a0718SPierre Ossman 
1478abfbe5f7SJuha Yrjola 	for (i = 0; i < host->nr_slots; i++)
1479abfbe5f7SJuha Yrjola 		mmc_omap_remove_slot(host->slots[i]);
14801c6a0718SPierre Ossman 
1481abfbe5f7SJuha Yrjola 	if (host->pdata->cleanup)
1482abfbe5f7SJuha Yrjola 		host->pdata->cleanup(&pdev->dev);
1483abfbe5f7SJuha Yrjola 
1484d4a36645SRussell King 	mmc_omap_fclk_enable(host, 0);
148549c1d9daSLadislav Michl 	free_irq(host->irq, host);
14861c6a0718SPierre Ossman 	clk_put(host->fclk);
1487d4a36645SRussell King 	clk_disable(host->iclk);
1488d4a36645SRussell King 	clk_put(host->iclk);
14891c6a0718SPierre Ossman 
14903451c067SRussell King 	if (host->dma_tx)
14913451c067SRussell King 		dma_release_channel(host->dma_tx);
14923451c067SRussell King 	if (host->dma_rx)
14933451c067SRussell King 		dma_release_channel(host->dma_rx);
14943451c067SRussell King 
149555c381e4SRussell King 	iounmap(host->virt_base);
14961c6a0718SPierre Ossman 	release_mem_region(pdev->resource[0].start,
14971c6a0718SPierre Ossman 			   pdev->resource[0].end - pdev->resource[0].start + 1);
1498b01a4f1cSVenkatraman S 	destroy_workqueue(host->mmc_omap_wq);
14991c6a0718SPierre Ossman 
1500abfbe5f7SJuha Yrjola 	kfree(host);
15011c6a0718SPierre Ossman 
15021c6a0718SPierre Ossman 	return 0;
15031c6a0718SPierre Ossman }
15041c6a0718SPierre Ossman 
15051c6a0718SPierre Ossman static struct platform_driver mmc_omap_driver = {
1506b6e0703bSVenkatraman S 	.probe		= mmc_omap_probe,
15070433c143SBill Pemberton 	.remove		= mmc_omap_remove,
15081c6a0718SPierre Ossman 	.driver		= {
15091c6a0718SPierre Ossman 		.name	= DRIVER_NAME,
1510bc65c724SKay Sievers 		.owner	= THIS_MODULE,
15111c6a0718SPierre Ossman 	},
15121c6a0718SPierre Ossman };
15131c6a0718SPierre Ossman 
1514680f1b5bSVenkatraman S module_platform_driver(mmc_omap_driver);
15151c6a0718SPierre Ossman MODULE_DESCRIPTION("OMAP Multimedia Card driver");
15161c6a0718SPierre Ossman MODULE_LICENSE("GPL");
1517bc65c724SKay Sievers MODULE_ALIAS("platform:" DRIVER_NAME);
1518d36b6910SAl Viro MODULE_AUTHOR("Juha Yrjölä");
1519