xref: /linux/drivers/mmc/host/omap.c (revision b01a4f1ce2cde9acd97ce5c06e35bc9c1eacded7)
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>
201c6a0718SPierre Ossman #include <linux/dma-mapping.h>
211c6a0718SPierre Ossman #include <linux/delay.h>
221c6a0718SPierre Ossman #include <linux/spinlock.h>
231c6a0718SPierre Ossman #include <linux/timer.h>
241c6a0718SPierre Ossman #include <linux/mmc/host.h>
251c6a0718SPierre Ossman #include <linux/mmc/card.h>
261c6a0718SPierre Ossman #include <linux/clk.h>
2745711f1aSJens Axboe #include <linux/scatterlist.h>
286d16bfb5SDavid Brownell #include <linux/i2c/tps65010.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
301c6a0718SPierre Ossman 
311c6a0718SPierre Ossman #include <asm/io.h>
321c6a0718SPierre Ossman #include <asm/irq.h>
331c6a0718SPierre Ossman 
34ce491cf8STony Lindgren #include <plat/board.h>
35ce491cf8STony Lindgren #include <plat/mmc.h>
361bc857f7SRussell King #include <asm/gpio.h>
37ce491cf8STony Lindgren #include <plat/dma.h>
38ce491cf8STony Lindgren #include <plat/mux.h>
39ce491cf8STony Lindgren #include <plat/fpga.h>
401c6a0718SPierre Ossman 
411c6a0718SPierre Ossman #define	OMAP_MMC_REG_CMD	0x00
420e950fa6SMarek Belisko #define	OMAP_MMC_REG_ARGL	0x01
430e950fa6SMarek Belisko #define	OMAP_MMC_REG_ARGH	0x02
440e950fa6SMarek Belisko #define	OMAP_MMC_REG_CON	0x03
450e950fa6SMarek Belisko #define	OMAP_MMC_REG_STAT	0x04
460e950fa6SMarek Belisko #define	OMAP_MMC_REG_IE		0x05
470e950fa6SMarek Belisko #define	OMAP_MMC_REG_CTO	0x06
480e950fa6SMarek Belisko #define	OMAP_MMC_REG_DTO	0x07
490e950fa6SMarek Belisko #define	OMAP_MMC_REG_DATA	0x08
500e950fa6SMarek Belisko #define	OMAP_MMC_REG_BLEN	0x09
510e950fa6SMarek Belisko #define	OMAP_MMC_REG_NBLK	0x0a
520e950fa6SMarek Belisko #define	OMAP_MMC_REG_BUF	0x0b
530e950fa6SMarek Belisko #define	OMAP_MMC_REG_SDIO	0x0d
540e950fa6SMarek Belisko #define	OMAP_MMC_REG_REV	0x0f
550e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP0	0x10
560e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP1	0x11
570e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP2	0x12
580e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP3	0x13
590e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP4	0x14
600e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP5	0x15
610e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP6	0x16
620e950fa6SMarek Belisko #define	OMAP_MMC_REG_RSP7	0x17
630e950fa6SMarek Belisko #define	OMAP_MMC_REG_IOSR	0x18
640e950fa6SMarek Belisko #define	OMAP_MMC_REG_SYSC	0x19
650e950fa6SMarek Belisko #define	OMAP_MMC_REG_SYSS	0x1a
661c6a0718SPierre Ossman 
671c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CARD_ERR		(1 << 14)
681c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CARD_IRQ		(1 << 13)
691c6a0718SPierre Ossman #define	OMAP_MMC_STAT_OCR_BUSY		(1 << 12)
701c6a0718SPierre Ossman #define	OMAP_MMC_STAT_A_EMPTY		(1 << 11)
711c6a0718SPierre Ossman #define	OMAP_MMC_STAT_A_FULL		(1 << 10)
721c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CMD_CRC		(1 <<  8)
731c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CMD_TOUT		(1 <<  7)
741c6a0718SPierre Ossman #define	OMAP_MMC_STAT_DATA_CRC		(1 <<  6)
751c6a0718SPierre Ossman #define	OMAP_MMC_STAT_DATA_TOUT		(1 <<  5)
761c6a0718SPierre Ossman #define	OMAP_MMC_STAT_END_BUSY		(1 <<  4)
771c6a0718SPierre Ossman #define	OMAP_MMC_STAT_END_OF_DATA	(1 <<  3)
781c6a0718SPierre Ossman #define	OMAP_MMC_STAT_CARD_BUSY		(1 <<  2)
791c6a0718SPierre Ossman #define	OMAP_MMC_STAT_END_OF_CMD	(1 <<  0)
801c6a0718SPierre Ossman 
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 
941c6a0718SPierre Ossman #define DRIVER_NAME "mmci-omap"
951c6a0718SPierre Ossman 
961c6a0718SPierre Ossman /* Specifies how often in millisecs to poll for card status changes
971c6a0718SPierre Ossman  * when the cover switch is open */
987584d276SJarkko Lavinen #define OMAP_MMC_COVER_POLL_DELAY	500
991c6a0718SPierre Ossman 
100abfbe5f7SJuha Yrjola struct mmc_omap_host;
101abfbe5f7SJuha Yrjola 
102abfbe5f7SJuha Yrjola struct mmc_omap_slot {
103abfbe5f7SJuha Yrjola 	int			id;
104abfbe5f7SJuha Yrjola 	unsigned int		vdd;
105abfbe5f7SJuha Yrjola 	u16			saved_con;
106abfbe5f7SJuha Yrjola 	u16			bus_mode;
107abfbe5f7SJuha Yrjola 	unsigned int		fclk_freq;
108abfbe5f7SJuha Yrjola 	unsigned		powered:1;
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 	int			suspended;
1231c6a0718SPierre Ossman 	struct mmc_request *	mrq;
1241c6a0718SPierre Ossman 	struct mmc_command *	cmd;
1251c6a0718SPierre Ossman 	struct mmc_data *	data;
1261c6a0718SPierre Ossman 	struct mmc_host *	mmc;
1271c6a0718SPierre Ossman 	struct device *		dev;
1281c6a0718SPierre Ossman 	unsigned char		id; /* 16xx chips have 2 MMC blocks */
1291c6a0718SPierre Ossman 	struct clk *		iclk;
1301c6a0718SPierre Ossman 	struct clk *		fclk;
1311c6a0718SPierre Ossman 	struct resource		*mem_res;
1321c6a0718SPierre Ossman 	void __iomem		*virt_base;
1331c6a0718SPierre Ossman 	unsigned int		phys_base;
1341c6a0718SPierre Ossman 	int			irq;
1351c6a0718SPierre Ossman 	unsigned char		bus_mode;
1361c6a0718SPierre Ossman 	unsigned char		hw_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 
1541c6a0718SPierre Ossman 	unsigned		use_dma:1;
1551c6a0718SPierre Ossman 	unsigned		brs_received:1, dma_done:1;
1561c6a0718SPierre Ossman 	unsigned		dma_is_read:1;
1571c6a0718SPierre Ossman 	unsigned		dma_in_use:1;
1581c6a0718SPierre Ossman 	int			dma_ch;
1591c6a0718SPierre Ossman 	spinlock_t		dma_lock;
1601c6a0718SPierre Ossman 	struct timer_list	dma_timer;
1611c6a0718SPierre Ossman 	unsigned		dma_len;
1621c6a0718SPierre Ossman 
163abfbe5f7SJuha Yrjola 	struct mmc_omap_slot    *slots[OMAP_MMC_MAX_SLOTS];
164abfbe5f7SJuha Yrjola 	struct mmc_omap_slot    *current_slot;
165abfbe5f7SJuha Yrjola 	spinlock_t              slot_lock;
166abfbe5f7SJuha Yrjola 	wait_queue_head_t       slot_wq;
167abfbe5f7SJuha Yrjola 	int                     nr_slots;
168abfbe5f7SJuha Yrjola 
1690807a9b5SJarkko Lavinen 	struct timer_list       clk_timer;
1700807a9b5SJarkko Lavinen 	spinlock_t		clk_lock;     /* for changing enabled state */
1710807a9b5SJarkko Lavinen 	unsigned int            fclk_enabled:1;
172*b01a4f1cSVenkatraman S 	struct workqueue_struct *mmc_omap_wq;
1730807a9b5SJarkko Lavinen 
174abfbe5f7SJuha Yrjola 	struct omap_mmc_platform_data *pdata;
1751c6a0718SPierre Ossman };
1761c6a0718SPierre Ossman 
1770d9ee5b2STejun Heo 
1787c8ad982SRussell King static void mmc_omap_fclk_offdelay(struct mmc_omap_slot *slot)
1790807a9b5SJarkko Lavinen {
1800807a9b5SJarkko Lavinen 	unsigned long tick_ns;
1810807a9b5SJarkko Lavinen 
1820807a9b5SJarkko Lavinen 	if (slot != NULL && slot->host->fclk_enabled && slot->fclk_freq > 0) {
1830807a9b5SJarkko Lavinen 		tick_ns = (1000000000 + slot->fclk_freq - 1) / slot->fclk_freq;
1840807a9b5SJarkko Lavinen 		ndelay(8 * tick_ns);
1850807a9b5SJarkko Lavinen 	}
1860807a9b5SJarkko Lavinen }
1870807a9b5SJarkko Lavinen 
1887c8ad982SRussell King static void mmc_omap_fclk_enable(struct mmc_omap_host *host, unsigned int enable)
1890807a9b5SJarkko Lavinen {
1900807a9b5SJarkko Lavinen 	unsigned long flags;
1910807a9b5SJarkko Lavinen 
1920807a9b5SJarkko Lavinen 	spin_lock_irqsave(&host->clk_lock, flags);
1930807a9b5SJarkko Lavinen 	if (host->fclk_enabled != enable) {
1940807a9b5SJarkko Lavinen 		host->fclk_enabled = enable;
1950807a9b5SJarkko Lavinen 		if (enable)
1960807a9b5SJarkko Lavinen 			clk_enable(host->fclk);
1970807a9b5SJarkko Lavinen 		else
1980807a9b5SJarkko Lavinen 			clk_disable(host->fclk);
1990807a9b5SJarkko Lavinen 	}
2000807a9b5SJarkko Lavinen 	spin_unlock_irqrestore(&host->clk_lock, flags);
2010807a9b5SJarkko Lavinen }
2020807a9b5SJarkko Lavinen 
203abfbe5f7SJuha Yrjola static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
204abfbe5f7SJuha Yrjola {
205abfbe5f7SJuha Yrjola 	struct mmc_omap_host *host = slot->host;
206abfbe5f7SJuha Yrjola 	unsigned long flags;
207abfbe5f7SJuha Yrjola 
208abfbe5f7SJuha Yrjola 	if (claimed)
209abfbe5f7SJuha Yrjola 		goto no_claim;
210abfbe5f7SJuha Yrjola 	spin_lock_irqsave(&host->slot_lock, flags);
211abfbe5f7SJuha Yrjola 	while (host->mmc != NULL) {
212abfbe5f7SJuha Yrjola 		spin_unlock_irqrestore(&host->slot_lock, flags);
213abfbe5f7SJuha Yrjola 		wait_event(host->slot_wq, host->mmc == NULL);
214abfbe5f7SJuha Yrjola 		spin_lock_irqsave(&host->slot_lock, flags);
215abfbe5f7SJuha Yrjola 	}
216abfbe5f7SJuha Yrjola 	host->mmc = slot->mmc;
217abfbe5f7SJuha Yrjola 	spin_unlock_irqrestore(&host->slot_lock, flags);
218abfbe5f7SJuha Yrjola no_claim:
2190807a9b5SJarkko Lavinen 	del_timer(&host->clk_timer);
2200807a9b5SJarkko Lavinen 	if (host->current_slot != slot || !claimed)
2210807a9b5SJarkko Lavinen 		mmc_omap_fclk_offdelay(host->current_slot);
2220807a9b5SJarkko Lavinen 
223abfbe5f7SJuha Yrjola 	if (host->current_slot != slot) {
2240807a9b5SJarkko Lavinen 		OMAP_MMC_WRITE(host, CON, slot->saved_con & 0xFC00);
225abfbe5f7SJuha Yrjola 		if (host->pdata->switch_slot != NULL)
226abfbe5f7SJuha Yrjola 			host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
227abfbe5f7SJuha Yrjola 		host->current_slot = slot;
228abfbe5f7SJuha Yrjola 	}
229abfbe5f7SJuha Yrjola 
2300807a9b5SJarkko Lavinen 	if (claimed) {
2310807a9b5SJarkko Lavinen 		mmc_omap_fclk_enable(host, 1);
2320807a9b5SJarkko Lavinen 
233abfbe5f7SJuha Yrjola 		/* Doing the dummy read here seems to work around some bug
234abfbe5f7SJuha Yrjola 		 * at least in OMAP24xx silicon where the command would not
235abfbe5f7SJuha Yrjola 		 * start after writing the CMD register. Sigh. */
236abfbe5f7SJuha Yrjola 		OMAP_MMC_READ(host, CON);
237abfbe5f7SJuha Yrjola 
238abfbe5f7SJuha Yrjola 		OMAP_MMC_WRITE(host, CON, slot->saved_con);
2390807a9b5SJarkko Lavinen 	} else
2400807a9b5SJarkko Lavinen 		mmc_omap_fclk_enable(host, 0);
241abfbe5f7SJuha Yrjola }
242abfbe5f7SJuha Yrjola 
243abfbe5f7SJuha Yrjola static void mmc_omap_start_request(struct mmc_omap_host *host,
244abfbe5f7SJuha Yrjola 				   struct mmc_request *req);
245abfbe5f7SJuha Yrjola 
2460f602ec7SJarkko Lavinen static void mmc_omap_slot_release_work(struct work_struct *work)
2470f602ec7SJarkko Lavinen {
2480f602ec7SJarkko Lavinen 	struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
2490f602ec7SJarkko Lavinen 						  slot_release_work);
2500f602ec7SJarkko Lavinen 	struct mmc_omap_slot *next_slot = host->next_slot;
2510f602ec7SJarkko Lavinen 	struct mmc_request *rq;
2520f602ec7SJarkko Lavinen 
2530f602ec7SJarkko Lavinen 	host->next_slot = NULL;
2540f602ec7SJarkko Lavinen 	mmc_omap_select_slot(next_slot, 1);
2550f602ec7SJarkko Lavinen 
2560f602ec7SJarkko Lavinen 	rq = next_slot->mrq;
2570f602ec7SJarkko Lavinen 	next_slot->mrq = NULL;
2580f602ec7SJarkko Lavinen 	mmc_omap_start_request(host, rq);
2590f602ec7SJarkko Lavinen }
2600f602ec7SJarkko Lavinen 
2610807a9b5SJarkko Lavinen static void mmc_omap_release_slot(struct mmc_omap_slot *slot, int clk_enabled)
262abfbe5f7SJuha Yrjola {
263abfbe5f7SJuha Yrjola 	struct mmc_omap_host *host = slot->host;
264abfbe5f7SJuha Yrjola 	unsigned long flags;
265abfbe5f7SJuha Yrjola 	int i;
266abfbe5f7SJuha Yrjola 
267abfbe5f7SJuha Yrjola 	BUG_ON(slot == NULL || host->mmc == NULL);
2680807a9b5SJarkko Lavinen 
2690807a9b5SJarkko Lavinen 	if (clk_enabled)
2700807a9b5SJarkko Lavinen 		/* Keeps clock running for at least 8 cycles on valid freq */
2710807a9b5SJarkko Lavinen 		mod_timer(&host->clk_timer, jiffies  + HZ/10);
2720807a9b5SJarkko Lavinen 	else {
2730807a9b5SJarkko Lavinen 		del_timer(&host->clk_timer);
2740807a9b5SJarkko Lavinen 		mmc_omap_fclk_offdelay(slot);
2750807a9b5SJarkko Lavinen 		mmc_omap_fclk_enable(host, 0);
2760807a9b5SJarkko Lavinen 	}
277abfbe5f7SJuha Yrjola 
278abfbe5f7SJuha Yrjola 	spin_lock_irqsave(&host->slot_lock, flags);
279abfbe5f7SJuha Yrjola 	/* Check for any pending requests */
280abfbe5f7SJuha Yrjola 	for (i = 0; i < host->nr_slots; i++) {
281abfbe5f7SJuha Yrjola 		struct mmc_omap_slot *new_slot;
282abfbe5f7SJuha Yrjola 
283abfbe5f7SJuha Yrjola 		if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
284abfbe5f7SJuha Yrjola 			continue;
285abfbe5f7SJuha Yrjola 
2860f602ec7SJarkko Lavinen 		BUG_ON(host->next_slot != NULL);
287abfbe5f7SJuha Yrjola 		new_slot = host->slots[i];
288abfbe5f7SJuha Yrjola 		/* The current slot should not have a request in queue */
289abfbe5f7SJuha Yrjola 		BUG_ON(new_slot == host->current_slot);
290abfbe5f7SJuha Yrjola 
2910f602ec7SJarkko Lavinen 		host->next_slot = new_slot;
292abfbe5f7SJuha Yrjola 		host->mmc = new_slot->mmc;
293abfbe5f7SJuha Yrjola 		spin_unlock_irqrestore(&host->slot_lock, flags);
294*b01a4f1cSVenkatraman S 		queue_work(host->mmc_omap_wq, &host->slot_release_work);
295abfbe5f7SJuha Yrjola 		return;
296abfbe5f7SJuha Yrjola 	}
297abfbe5f7SJuha Yrjola 
298abfbe5f7SJuha Yrjola 	host->mmc = NULL;
299abfbe5f7SJuha Yrjola 	wake_up(&host->slot_wq);
300abfbe5f7SJuha Yrjola 	spin_unlock_irqrestore(&host->slot_lock, flags);
301abfbe5f7SJuha Yrjola }
302abfbe5f7SJuha Yrjola 
3035a0f3f1fSJuha Yrjola static inline
3045a0f3f1fSJuha Yrjola int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
3055a0f3f1fSJuha Yrjola {
3068348f002SKyungmin Park 	if (slot->pdata->get_cover_state)
3078348f002SKyungmin Park 		return slot->pdata->get_cover_state(mmc_dev(slot->mmc),
3088348f002SKyungmin Park 						    slot->id);
3098348f002SKyungmin Park 	return 0;
3105a0f3f1fSJuha Yrjola }
3115a0f3f1fSJuha Yrjola 
3125a0f3f1fSJuha Yrjola static ssize_t
3135a0f3f1fSJuha Yrjola mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
3145a0f3f1fSJuha Yrjola 			   char *buf)
3155a0f3f1fSJuha Yrjola {
3165a0f3f1fSJuha Yrjola 	struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
3175a0f3f1fSJuha Yrjola 	struct mmc_omap_slot *slot = mmc_priv(mmc);
3185a0f3f1fSJuha Yrjola 
3195a0f3f1fSJuha Yrjola 	return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
3205a0f3f1fSJuha Yrjola 		       "closed");
3215a0f3f1fSJuha Yrjola }
3225a0f3f1fSJuha Yrjola 
3235a0f3f1fSJuha Yrjola static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
3245a0f3f1fSJuha Yrjola 
325abfbe5f7SJuha Yrjola static ssize_t
326abfbe5f7SJuha Yrjola mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
327abfbe5f7SJuha Yrjola 			char *buf)
328abfbe5f7SJuha Yrjola {
329abfbe5f7SJuha Yrjola 	struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
330abfbe5f7SJuha Yrjola 	struct mmc_omap_slot *slot = mmc_priv(mmc);
331abfbe5f7SJuha Yrjola 
332abfbe5f7SJuha Yrjola 	return sprintf(buf, "%s\n", slot->pdata->name);
333abfbe5f7SJuha Yrjola }
334abfbe5f7SJuha Yrjola 
335abfbe5f7SJuha Yrjola static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
336abfbe5f7SJuha Yrjola 
3371c6a0718SPierre Ossman static void
3381c6a0718SPierre Ossman mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
3391c6a0718SPierre Ossman {
3401c6a0718SPierre Ossman 	u32 cmdreg;
3411c6a0718SPierre Ossman 	u32 resptype;
3421c6a0718SPierre Ossman 	u32 cmdtype;
3431c6a0718SPierre Ossman 
3441c6a0718SPierre Ossman 	host->cmd = cmd;
3451c6a0718SPierre Ossman 
3461c6a0718SPierre Ossman 	resptype = 0;
3471c6a0718SPierre Ossman 	cmdtype = 0;
3481c6a0718SPierre Ossman 
3491c6a0718SPierre Ossman 	/* Our hardware needs to know exact type */
3501c6a0718SPierre Ossman 	switch (mmc_resp_type(cmd)) {
3511c6a0718SPierre Ossman 	case MMC_RSP_NONE:
3521c6a0718SPierre Ossman 		break;
3531c6a0718SPierre Ossman 	case MMC_RSP_R1:
3541c6a0718SPierre Ossman 	case MMC_RSP_R1B:
3551c6a0718SPierre Ossman 		/* resp 1, 1b, 6, 7 */
3561c6a0718SPierre Ossman 		resptype = 1;
3571c6a0718SPierre Ossman 		break;
3581c6a0718SPierre Ossman 	case MMC_RSP_R2:
3591c6a0718SPierre Ossman 		resptype = 2;
3601c6a0718SPierre Ossman 		break;
3611c6a0718SPierre Ossman 	case MMC_RSP_R3:
3621c6a0718SPierre Ossman 		resptype = 3;
3631c6a0718SPierre Ossman 		break;
3641c6a0718SPierre Ossman 	default:
3651c6a0718SPierre Ossman 		dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
3661c6a0718SPierre Ossman 		break;
3671c6a0718SPierre Ossman 	}
3681c6a0718SPierre Ossman 
3691c6a0718SPierre Ossman 	if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
3701c6a0718SPierre Ossman 		cmdtype = OMAP_MMC_CMDTYPE_ADTC;
3711c6a0718SPierre Ossman 	} else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
3721c6a0718SPierre Ossman 		cmdtype = OMAP_MMC_CMDTYPE_BC;
3731c6a0718SPierre Ossman 	} else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
3741c6a0718SPierre Ossman 		cmdtype = OMAP_MMC_CMDTYPE_BCR;
3751c6a0718SPierre Ossman 	} else {
3761c6a0718SPierre Ossman 		cmdtype = OMAP_MMC_CMDTYPE_AC;
3771c6a0718SPierre Ossman 	}
3781c6a0718SPierre Ossman 
3791c6a0718SPierre Ossman 	cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
3801c6a0718SPierre Ossman 
381abfbe5f7SJuha Yrjola 	if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
3821c6a0718SPierre Ossman 		cmdreg |= 1 << 6;
3831c6a0718SPierre Ossman 
3841c6a0718SPierre Ossman 	if (cmd->flags & MMC_RSP_BUSY)
3851c6a0718SPierre Ossman 		cmdreg |= 1 << 11;
3861c6a0718SPierre Ossman 
3871c6a0718SPierre Ossman 	if (host->data && !(host->data->flags & MMC_DATA_WRITE))
3881c6a0718SPierre Ossman 		cmdreg |= 1 << 15;
3891c6a0718SPierre Ossman 
3900fb4723dSJarkko Lavinen 	mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
391eb1860bcSJarkko Lavinen 
3921c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, CTO, 200);
3931c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
3941c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
3951c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, IE,
3961c6a0718SPierre Ossman 		       OMAP_MMC_STAT_A_EMPTY    | OMAP_MMC_STAT_A_FULL    |
3971c6a0718SPierre Ossman 		       OMAP_MMC_STAT_CMD_CRC    | OMAP_MMC_STAT_CMD_TOUT  |
3981c6a0718SPierre Ossman 		       OMAP_MMC_STAT_DATA_CRC   | OMAP_MMC_STAT_DATA_TOUT |
3991c6a0718SPierre Ossman 		       OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR  |
4001c6a0718SPierre Ossman 		       OMAP_MMC_STAT_END_OF_DATA);
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;
4091c6a0718SPierre Ossman 
4101c6a0718SPierre Ossman 	BUG_ON(host->dma_ch < 0);
41117b0429dSPierre Ossman 	if (data->error)
4121c6a0718SPierre Ossman 		omap_stop_dma(host->dma_ch);
4131c6a0718SPierre Ossman 	/* Release DMA channel lazily */
4141c6a0718SPierre Ossman 	mod_timer(&host->dma_timer, jiffies + HZ);
4151c6a0718SPierre Ossman 	if (data->flags & MMC_DATA_WRITE)
4161c6a0718SPierre Ossman 		dma_data_dir = DMA_TO_DEVICE;
4171c6a0718SPierre Ossman 	else
4181c6a0718SPierre Ossman 		dma_data_dir = DMA_FROM_DEVICE;
4191c6a0718SPierre Ossman 	dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len,
4201c6a0718SPierre Ossman 		     dma_data_dir);
4211c6a0718SPierre Ossman }
422a914ded2SJuha Yrjola 
4230f602ec7SJarkko Lavinen static void mmc_omap_send_stop_work(struct work_struct *work)
4240f602ec7SJarkko Lavinen {
4250f602ec7SJarkko Lavinen 	struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
4260f602ec7SJarkko Lavinen 						  send_stop_work);
4270f602ec7SJarkko Lavinen 	struct mmc_omap_slot *slot = host->current_slot;
4280f602ec7SJarkko Lavinen 	struct mmc_data *data = host->stop_data;
4290f602ec7SJarkko Lavinen 	unsigned long tick_ns;
4300f602ec7SJarkko Lavinen 
4310f602ec7SJarkko Lavinen 	tick_ns = (1000000000 + slot->fclk_freq - 1)/slot->fclk_freq;
4320f602ec7SJarkko Lavinen 	ndelay(8*tick_ns);
4330f602ec7SJarkko Lavinen 
4340f602ec7SJarkko Lavinen 	mmc_omap_start_command(host, data->stop);
4350f602ec7SJarkko Lavinen }
4360f602ec7SJarkko Lavinen 
437a914ded2SJuha Yrjola static void
438a914ded2SJuha Yrjola mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
439a914ded2SJuha Yrjola {
440a914ded2SJuha Yrjola 	if (host->dma_in_use)
441a914ded2SJuha Yrjola 		mmc_omap_release_dma(host, data, data->error);
442a914ded2SJuha Yrjola 
4431c6a0718SPierre Ossman 	host->data = NULL;
4441c6a0718SPierre Ossman 	host->sg_len = 0;
4451c6a0718SPierre Ossman 
4461c6a0718SPierre Ossman 	/* NOTE:  MMC layer will sometimes poll-wait CMD13 next, issuing
4471c6a0718SPierre Ossman 	 * dozens of requests until the card finishes writing data.
4481c6a0718SPierre Ossman 	 * It'd be cheaper to just wait till an EOFB interrupt arrives...
4491c6a0718SPierre Ossman 	 */
4501c6a0718SPierre Ossman 
4511c6a0718SPierre Ossman 	if (!data->stop) {
452a914ded2SJuha Yrjola 		struct mmc_host *mmc;
453a914ded2SJuha Yrjola 
4541c6a0718SPierre Ossman 		host->mrq = NULL;
455a914ded2SJuha Yrjola 		mmc = host->mmc;
4560807a9b5SJarkko Lavinen 		mmc_omap_release_slot(host->current_slot, 1);
457a914ded2SJuha Yrjola 		mmc_request_done(mmc, data->mrq);
4581c6a0718SPierre Ossman 		return;
4591c6a0718SPierre Ossman 	}
4601c6a0718SPierre Ossman 
4610f602ec7SJarkko Lavinen 	host->stop_data = data;
462*b01a4f1cSVenkatraman S 	queue_work(host->mmc_omap_wq, &host->send_stop_work);
4631c6a0718SPierre Ossman }
4641c6a0718SPierre Ossman 
4651c6a0718SPierre Ossman static void
4660fb4723dSJarkko Lavinen mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
467eb1860bcSJarkko Lavinen {
468eb1860bcSJarkko Lavinen 	struct mmc_omap_slot *slot = host->current_slot;
469eb1860bcSJarkko Lavinen 	unsigned int restarts, passes, timeout;
470eb1860bcSJarkko Lavinen 	u16 stat = 0;
471eb1860bcSJarkko Lavinen 
472eb1860bcSJarkko Lavinen 	/* Sending abort takes 80 clocks. Have some extra and round up */
473eb1860bcSJarkko Lavinen 	timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
474eb1860bcSJarkko Lavinen 	restarts = 0;
4750fb4723dSJarkko Lavinen 	while (restarts < maxloops) {
476eb1860bcSJarkko Lavinen 		OMAP_MMC_WRITE(host, STAT, 0xFFFF);
477eb1860bcSJarkko Lavinen 		OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
478eb1860bcSJarkko Lavinen 
479eb1860bcSJarkko Lavinen 		passes = 0;
480eb1860bcSJarkko Lavinen 		while (passes < timeout) {
481eb1860bcSJarkko Lavinen 			stat = OMAP_MMC_READ(host, STAT);
482eb1860bcSJarkko Lavinen 			if (stat & OMAP_MMC_STAT_END_OF_CMD)
483eb1860bcSJarkko Lavinen 				goto out;
484eb1860bcSJarkko Lavinen 			udelay(1);
485eb1860bcSJarkko Lavinen 			passes++;
486eb1860bcSJarkko Lavinen 		}
487eb1860bcSJarkko Lavinen 
488eb1860bcSJarkko Lavinen 		restarts++;
489eb1860bcSJarkko Lavinen 	}
490eb1860bcSJarkko Lavinen out:
491eb1860bcSJarkko Lavinen 	OMAP_MMC_WRITE(host, STAT, stat);
492eb1860bcSJarkko Lavinen }
493eb1860bcSJarkko Lavinen 
494eb1860bcSJarkko Lavinen static void
495a914ded2SJuha Yrjola mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
496a914ded2SJuha Yrjola {
497a914ded2SJuha Yrjola 	if (host->dma_in_use)
498a914ded2SJuha Yrjola 		mmc_omap_release_dma(host, data, 1);
499a914ded2SJuha Yrjola 
500a914ded2SJuha Yrjola 	host->data = NULL;
501a914ded2SJuha Yrjola 	host->sg_len = 0;
502a914ded2SJuha Yrjola 
5030fb4723dSJarkko Lavinen 	mmc_omap_send_abort(host, 10000);
504a914ded2SJuha Yrjola }
505a914ded2SJuha Yrjola 
506a914ded2SJuha Yrjola static void
5071c6a0718SPierre Ossman mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
5081c6a0718SPierre Ossman {
5091c6a0718SPierre Ossman 	unsigned long flags;
5101c6a0718SPierre Ossman 	int done;
5111c6a0718SPierre Ossman 
5121c6a0718SPierre Ossman 	if (!host->dma_in_use) {
5131c6a0718SPierre Ossman 		mmc_omap_xfer_done(host, data);
5141c6a0718SPierre Ossman 		return;
5151c6a0718SPierre Ossman 	}
5161c6a0718SPierre Ossman 	done = 0;
5171c6a0718SPierre Ossman 	spin_lock_irqsave(&host->dma_lock, flags);
5181c6a0718SPierre Ossman 	if (host->dma_done)
5191c6a0718SPierre Ossman 		done = 1;
5201c6a0718SPierre Ossman 	else
5211c6a0718SPierre Ossman 		host->brs_received = 1;
5221c6a0718SPierre Ossman 	spin_unlock_irqrestore(&host->dma_lock, flags);
5231c6a0718SPierre Ossman 	if (done)
5241c6a0718SPierre Ossman 		mmc_omap_xfer_done(host, data);
5251c6a0718SPierre Ossman }
5261c6a0718SPierre Ossman 
5271c6a0718SPierre Ossman static void
5281c6a0718SPierre Ossman mmc_omap_dma_timer(unsigned long data)
5291c6a0718SPierre Ossman {
5301c6a0718SPierre Ossman 	struct mmc_omap_host *host = (struct mmc_omap_host *) data;
5311c6a0718SPierre Ossman 
5321c6a0718SPierre Ossman 	BUG_ON(host->dma_ch < 0);
5331c6a0718SPierre Ossman 	omap_free_dma(host->dma_ch);
5341c6a0718SPierre Ossman 	host->dma_ch = -1;
5351c6a0718SPierre Ossman }
5361c6a0718SPierre Ossman 
5371c6a0718SPierre Ossman static void
5381c6a0718SPierre Ossman mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
5391c6a0718SPierre Ossman {
5401c6a0718SPierre Ossman 	unsigned long flags;
5411c6a0718SPierre Ossman 	int done;
5421c6a0718SPierre Ossman 
5431c6a0718SPierre Ossman 	done = 0;
5441c6a0718SPierre Ossman 	spin_lock_irqsave(&host->dma_lock, flags);
5451c6a0718SPierre Ossman 	if (host->brs_received)
5461c6a0718SPierre Ossman 		done = 1;
5471c6a0718SPierre Ossman 	else
5481c6a0718SPierre Ossman 		host->dma_done = 1;
5491c6a0718SPierre Ossman 	spin_unlock_irqrestore(&host->dma_lock, flags);
5501c6a0718SPierre Ossman 	if (done)
5511c6a0718SPierre Ossman 		mmc_omap_xfer_done(host, data);
5521c6a0718SPierre Ossman }
5531c6a0718SPierre Ossman 
5541c6a0718SPierre Ossman static void
5551c6a0718SPierre Ossman mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
5561c6a0718SPierre Ossman {
5571c6a0718SPierre Ossman 	host->cmd = NULL;
5581c6a0718SPierre Ossman 
5590fb4723dSJarkko Lavinen 	del_timer(&host->cmd_abort_timer);
560eb1860bcSJarkko Lavinen 
5611c6a0718SPierre Ossman 	if (cmd->flags & MMC_RSP_PRESENT) {
5621c6a0718SPierre Ossman 		if (cmd->flags & MMC_RSP_136) {
5631c6a0718SPierre Ossman 			/* response type 2 */
5641c6a0718SPierre Ossman 			cmd->resp[3] =
5651c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP0) |
5661c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP1) << 16);
5671c6a0718SPierre Ossman 			cmd->resp[2] =
5681c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP2) |
5691c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP3) << 16);
5701c6a0718SPierre Ossman 			cmd->resp[1] =
5711c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP4) |
5721c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP5) << 16);
5731c6a0718SPierre Ossman 			cmd->resp[0] =
5741c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP6) |
5751c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP7) << 16);
5761c6a0718SPierre Ossman 		} else {
5771c6a0718SPierre Ossman 			/* response types 1, 1b, 3, 4, 5, 6 */
5781c6a0718SPierre Ossman 			cmd->resp[0] =
5791c6a0718SPierre Ossman 				OMAP_MMC_READ(host, RSP6) |
5801c6a0718SPierre Ossman 				(OMAP_MMC_READ(host, RSP7) << 16);
5811c6a0718SPierre Ossman 		}
5821c6a0718SPierre Ossman 	}
5831c6a0718SPierre Ossman 
58417b0429dSPierre Ossman 	if (host->data == NULL || cmd->error) {
585a914ded2SJuha Yrjola 		struct mmc_host *mmc;
586a914ded2SJuha Yrjola 
587a914ded2SJuha Yrjola 		if (host->data != NULL)
588a914ded2SJuha Yrjola 			mmc_omap_abort_xfer(host, host->data);
5891c6a0718SPierre Ossman 		host->mrq = NULL;
590a914ded2SJuha Yrjola 		mmc = host->mmc;
5910807a9b5SJarkko Lavinen 		mmc_omap_release_slot(host->current_slot, 1);
592a914ded2SJuha Yrjola 		mmc_request_done(mmc, cmd->mrq);
5931c6a0718SPierre Ossman 	}
5941c6a0718SPierre Ossman }
5951c6a0718SPierre Ossman 
596eb1860bcSJarkko Lavinen /*
597eb1860bcSJarkko Lavinen  * Abort stuck command. Can occur when card is removed while it is being
598eb1860bcSJarkko Lavinen  * read.
599eb1860bcSJarkko Lavinen  */
600eb1860bcSJarkko Lavinen static void mmc_omap_abort_command(struct work_struct *work)
601eb1860bcSJarkko Lavinen {
602eb1860bcSJarkko Lavinen 	struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
6030fb4723dSJarkko Lavinen 						  cmd_abort_work);
6040fb4723dSJarkko Lavinen 	BUG_ON(!host->cmd);
605eb1860bcSJarkko Lavinen 
606eb1860bcSJarkko Lavinen 	dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
607eb1860bcSJarkko Lavinen 		host->cmd->opcode);
608eb1860bcSJarkko Lavinen 
6090fb4723dSJarkko Lavinen 	if (host->cmd->error == 0)
610eb1860bcSJarkko Lavinen 		host->cmd->error = -ETIMEDOUT;
6110fb4723dSJarkko Lavinen 
6120fb4723dSJarkko Lavinen 	if (host->data == NULL) {
6130fb4723dSJarkko Lavinen 		struct mmc_command *cmd;
6140fb4723dSJarkko Lavinen 		struct mmc_host    *mmc;
6150fb4723dSJarkko Lavinen 
6160fb4723dSJarkko Lavinen 		cmd = host->cmd;
6170fb4723dSJarkko Lavinen 		host->cmd = NULL;
6180fb4723dSJarkko Lavinen 		mmc_omap_send_abort(host, 10000);
6190fb4723dSJarkko Lavinen 
6200fb4723dSJarkko Lavinen 		host->mrq = NULL;
6210fb4723dSJarkko Lavinen 		mmc = host->mmc;
6220807a9b5SJarkko Lavinen 		mmc_omap_release_slot(host->current_slot, 1);
6230fb4723dSJarkko Lavinen 		mmc_request_done(mmc, cmd->mrq);
6240fb4723dSJarkko Lavinen 	} else
625eb1860bcSJarkko Lavinen 		mmc_omap_cmd_done(host, host->cmd);
6260fb4723dSJarkko Lavinen 
6270fb4723dSJarkko Lavinen 	host->abort = 0;
6280fb4723dSJarkko Lavinen 	enable_irq(host->irq);
629eb1860bcSJarkko Lavinen }
630eb1860bcSJarkko Lavinen 
631eb1860bcSJarkko Lavinen static void
632eb1860bcSJarkko Lavinen mmc_omap_cmd_timer(unsigned long data)
633eb1860bcSJarkko Lavinen {
634eb1860bcSJarkko Lavinen 	struct mmc_omap_host *host = (struct mmc_omap_host *) data;
6350fb4723dSJarkko Lavinen 	unsigned long flags;
636eb1860bcSJarkko Lavinen 
6370fb4723dSJarkko Lavinen 	spin_lock_irqsave(&host->slot_lock, flags);
6380fb4723dSJarkko Lavinen 	if (host->cmd != NULL && !host->abort) {
6390fb4723dSJarkko Lavinen 		OMAP_MMC_WRITE(host, IE, 0);
6400fb4723dSJarkko Lavinen 		disable_irq(host->irq);
6410fb4723dSJarkko Lavinen 		host->abort = 1;
642*b01a4f1cSVenkatraman S 		queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
6430fb4723dSJarkko Lavinen 	}
6440fb4723dSJarkko Lavinen 	spin_unlock_irqrestore(&host->slot_lock, flags);
645eb1860bcSJarkko Lavinen }
646eb1860bcSJarkko Lavinen 
6471c6a0718SPierre Ossman /* PIO only */
6481c6a0718SPierre Ossman static void
6491c6a0718SPierre Ossman mmc_omap_sg_to_buf(struct mmc_omap_host *host)
6501c6a0718SPierre Ossman {
6511c6a0718SPierre Ossman 	struct scatterlist *sg;
6521c6a0718SPierre Ossman 
6531c6a0718SPierre Ossman 	sg = host->data->sg + host->sg_idx;
6541c6a0718SPierre Ossman 	host->buffer_bytes_left = sg->length;
65545711f1aSJens Axboe 	host->buffer = sg_virt(sg);
6561c6a0718SPierre Ossman 	if (host->buffer_bytes_left > host->total_bytes_left)
6571c6a0718SPierre Ossman 		host->buffer_bytes_left = host->total_bytes_left;
6581c6a0718SPierre Ossman }
6591c6a0718SPierre Ossman 
6600807a9b5SJarkko Lavinen static void
6610807a9b5SJarkko Lavinen mmc_omap_clk_timer(unsigned long data)
6620807a9b5SJarkko Lavinen {
6630807a9b5SJarkko Lavinen 	struct mmc_omap_host *host = (struct mmc_omap_host *) data;
6640807a9b5SJarkko Lavinen 
6650807a9b5SJarkko Lavinen 	mmc_omap_fclk_enable(host, 0);
6660807a9b5SJarkko Lavinen }
6670807a9b5SJarkko Lavinen 
6681c6a0718SPierre Ossman /* PIO only */
6691c6a0718SPierre Ossman static void
6701c6a0718SPierre Ossman mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
6711c6a0718SPierre Ossman {
6721c6a0718SPierre Ossman 	int n;
6731c6a0718SPierre Ossman 
6741c6a0718SPierre Ossman 	if (host->buffer_bytes_left == 0) {
6751c6a0718SPierre Ossman 		host->sg_idx++;
6761c6a0718SPierre Ossman 		BUG_ON(host->sg_idx == host->sg_len);
6771c6a0718SPierre Ossman 		mmc_omap_sg_to_buf(host);
6781c6a0718SPierre Ossman 	}
6791c6a0718SPierre Ossman 	n = 64;
6801c6a0718SPierre Ossman 	if (n > host->buffer_bytes_left)
6811c6a0718SPierre Ossman 		n = host->buffer_bytes_left;
6821c6a0718SPierre Ossman 	host->buffer_bytes_left -= n;
6831c6a0718SPierre Ossman 	host->total_bytes_left -= n;
6841c6a0718SPierre Ossman 	host->data->bytes_xfered += n;
6851c6a0718SPierre Ossman 
6861c6a0718SPierre Ossman 	if (write) {
6870e950fa6SMarek Belisko 		__raw_writesw(host->virt_base + OMAP_MMC_REG(host, DATA), host->buffer, n);
6881c6a0718SPierre Ossman 	} else {
6890e950fa6SMarek Belisko 		__raw_readsw(host->virt_base + OMAP_MMC_REG(host, DATA), host->buffer, n);
6901c6a0718SPierre Ossman 	}
6911c6a0718SPierre Ossman }
6921c6a0718SPierre Ossman 
6931c6a0718SPierre Ossman static inline void mmc_omap_report_irq(u16 status)
6941c6a0718SPierre Ossman {
6951c6a0718SPierre Ossman 	static const char *mmc_omap_status_bits[] = {
6961c6a0718SPierre Ossman 		"EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
6971c6a0718SPierre Ossman 		"CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
6981c6a0718SPierre Ossman 	};
6991c6a0718SPierre Ossman 	int i, c = 0;
7001c6a0718SPierre Ossman 
7011c6a0718SPierre Ossman 	for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
7021c6a0718SPierre Ossman 		if (status & (1 << i)) {
7031c6a0718SPierre Ossman 			if (c)
7041c6a0718SPierre Ossman 				printk(" ");
7051c6a0718SPierre Ossman 			printk("%s", mmc_omap_status_bits[i]);
7061c6a0718SPierre Ossman 			c++;
7071c6a0718SPierre Ossman 		}
7081c6a0718SPierre Ossman }
7091c6a0718SPierre Ossman 
7101c6a0718SPierre Ossman static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
7111c6a0718SPierre Ossman {
7121c6a0718SPierre Ossman 	struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
7131c6a0718SPierre Ossman 	u16 status;
7141c6a0718SPierre Ossman 	int end_command;
7151c6a0718SPierre Ossman 	int end_transfer;
7162a50b888SJuha Yrjola 	int transfer_error, cmd_error;
7171c6a0718SPierre Ossman 
7181c6a0718SPierre Ossman 	if (host->cmd == NULL && host->data == NULL) {
7191c6a0718SPierre Ossman 		status = OMAP_MMC_READ(host, STAT);
7202a50b888SJuha Yrjola 		dev_info(mmc_dev(host->slots[0]->mmc),
7212a50b888SJuha Yrjola 			 "Spurious IRQ 0x%04x\n", status);
7221c6a0718SPierre Ossman 		if (status != 0) {
7231c6a0718SPierre Ossman 			OMAP_MMC_WRITE(host, STAT, status);
7241c6a0718SPierre Ossman 			OMAP_MMC_WRITE(host, IE, 0);
7251c6a0718SPierre Ossman 		}
7261c6a0718SPierre Ossman 		return IRQ_HANDLED;
7271c6a0718SPierre Ossman 	}
7281c6a0718SPierre Ossman 
7291c6a0718SPierre Ossman 	end_command = 0;
7301c6a0718SPierre Ossman 	end_transfer = 0;
7311c6a0718SPierre Ossman 	transfer_error = 0;
7322a50b888SJuha Yrjola 	cmd_error = 0;
7331c6a0718SPierre Ossman 
7341c6a0718SPierre Ossman 	while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
7352a50b888SJuha Yrjola 		int cmd;
7362a50b888SJuha Yrjola 
7371c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, STAT, status);
7382a50b888SJuha Yrjola 		if (host->cmd != NULL)
7392a50b888SJuha Yrjola 			cmd = host->cmd->opcode;
7402a50b888SJuha Yrjola 		else
7412a50b888SJuha Yrjola 			cmd = -1;
7421c6a0718SPierre Ossman #ifdef CONFIG_MMC_DEBUG
7431c6a0718SPierre Ossman 		dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
7442a50b888SJuha Yrjola 			status, cmd);
7451c6a0718SPierre Ossman 		mmc_omap_report_irq(status);
7461c6a0718SPierre Ossman 		printk("\n");
7471c6a0718SPierre Ossman #endif
7481c6a0718SPierre Ossman 		if (host->total_bytes_left) {
7491c6a0718SPierre Ossman 			if ((status & OMAP_MMC_STAT_A_FULL) ||
7501c6a0718SPierre Ossman 			    (status & OMAP_MMC_STAT_END_OF_DATA))
7511c6a0718SPierre Ossman 				mmc_omap_xfer_data(host, 0);
7521c6a0718SPierre Ossman 			if (status & OMAP_MMC_STAT_A_EMPTY)
7531c6a0718SPierre Ossman 				mmc_omap_xfer_data(host, 1);
7541c6a0718SPierre Ossman 		}
7551c6a0718SPierre Ossman 
7562a50b888SJuha Yrjola 		if (status & OMAP_MMC_STAT_END_OF_DATA)
7571c6a0718SPierre Ossman 			end_transfer = 1;
7581c6a0718SPierre Ossman 
7591c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_DATA_TOUT) {
7602a50b888SJuha Yrjola 			dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
7612a50b888SJuha Yrjola 				cmd);
7621c6a0718SPierre Ossman 			if (host->data) {
76317b0429dSPierre Ossman 				host->data->error = -ETIMEDOUT;
7641c6a0718SPierre Ossman 				transfer_error = 1;
7651c6a0718SPierre Ossman 			}
7661c6a0718SPierre Ossman 		}
7671c6a0718SPierre Ossman 
7681c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_DATA_CRC) {
7691c6a0718SPierre Ossman 			if (host->data) {
77017b0429dSPierre Ossman 				host->data->error = -EILSEQ;
7711c6a0718SPierre Ossman 				dev_dbg(mmc_dev(host->mmc),
7721c6a0718SPierre Ossman 					 "data CRC error, bytes left %d\n",
7731c6a0718SPierre Ossman 					host->total_bytes_left);
7741c6a0718SPierre Ossman 				transfer_error = 1;
7751c6a0718SPierre Ossman 			} else {
7761c6a0718SPierre Ossman 				dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
7771c6a0718SPierre Ossman 			}
7781c6a0718SPierre Ossman 		}
7791c6a0718SPierre Ossman 
7801c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_CMD_TOUT) {
7811c6a0718SPierre Ossman 			/* Timeouts are routine with some commands */
7821c6a0718SPierre Ossman 			if (host->cmd) {
783abfbe5f7SJuha Yrjola 				struct mmc_omap_slot *slot =
784abfbe5f7SJuha Yrjola 					host->current_slot;
7852a50b888SJuha Yrjola 				if (slot == NULL ||
7862a50b888SJuha Yrjola 				    !mmc_omap_cover_is_open(slot))
7871c6a0718SPierre Ossman 					dev_err(mmc_dev(host->mmc),
7882a50b888SJuha Yrjola 						"command timeout (CMD%d)\n",
7892a50b888SJuha Yrjola 						cmd);
79017b0429dSPierre Ossman 				host->cmd->error = -ETIMEDOUT;
7911c6a0718SPierre Ossman 				end_command = 1;
7922a50b888SJuha Yrjola 				cmd_error = 1;
7931c6a0718SPierre Ossman 			}
7941c6a0718SPierre Ossman 		}
7951c6a0718SPierre Ossman 
7961c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_CMD_CRC) {
7971c6a0718SPierre Ossman 			if (host->cmd) {
7981c6a0718SPierre Ossman 				dev_err(mmc_dev(host->mmc),
7991c6a0718SPierre Ossman 					"command CRC error (CMD%d, arg 0x%08x)\n",
8002a50b888SJuha Yrjola 					cmd, host->cmd->arg);
80117b0429dSPierre Ossman 				host->cmd->error = -EILSEQ;
8021c6a0718SPierre Ossman 				end_command = 1;
8032a50b888SJuha Yrjola 				cmd_error = 1;
8041c6a0718SPierre Ossman 			} else
8051c6a0718SPierre Ossman 				dev_err(mmc_dev(host->mmc),
8061c6a0718SPierre Ossman 					"command CRC error without cmd?\n");
8071c6a0718SPierre Ossman 		}
8081c6a0718SPierre Ossman 
8091c6a0718SPierre Ossman 		if (status & OMAP_MMC_STAT_CARD_ERR) {
8100107a4b3SRagner Magalhaes 			dev_dbg(mmc_dev(host->mmc),
8110107a4b3SRagner Magalhaes 				"ignoring card status error (CMD%d)\n",
8122a50b888SJuha Yrjola 				cmd);
8131c6a0718SPierre Ossman 			end_command = 1;
8141c6a0718SPierre Ossman 		}
8151c6a0718SPierre Ossman 
8161c6a0718SPierre Ossman 		/*
8171c6a0718SPierre Ossman 		 * NOTE: On 1610 the END_OF_CMD may come too early when
8181c6a0718SPierre Ossman 		 * starting a write
8191c6a0718SPierre Ossman 		 */
8201c6a0718SPierre Ossman 		if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
8211c6a0718SPierre Ossman 		    (!(status & OMAP_MMC_STAT_A_EMPTY))) {
8221c6a0718SPierre Ossman 			end_command = 1;
8231c6a0718SPierre Ossman 		}
8241c6a0718SPierre Ossman 	}
8251c6a0718SPierre Ossman 
8260fb4723dSJarkko Lavinen 	if (cmd_error && host->data) {
8270fb4723dSJarkko Lavinen 		del_timer(&host->cmd_abort_timer);
8280fb4723dSJarkko Lavinen 		host->abort = 1;
8290fb4723dSJarkko Lavinen 		OMAP_MMC_WRITE(host, IE, 0);
830e749c6f2SBen Nizette 		disable_irq_nosync(host->irq);
831*b01a4f1cSVenkatraman S 		queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
8320fb4723dSJarkko Lavinen 		return IRQ_HANDLED;
8330fb4723dSJarkko Lavinen 	}
8340fb4723dSJarkko Lavinen 
835f6947514SMichael Buesch 	if (end_command && host->cmd)
8361c6a0718SPierre Ossman 		mmc_omap_cmd_done(host, host->cmd);
8372a50b888SJuha Yrjola 	if (host->data != NULL) {
8381c6a0718SPierre Ossman 		if (transfer_error)
8391c6a0718SPierre Ossman 			mmc_omap_xfer_done(host, host->data);
8401c6a0718SPierre Ossman 		else if (end_transfer)
8411c6a0718SPierre Ossman 			mmc_omap_end_of_data(host, host->data);
8422a50b888SJuha Yrjola 	}
8431c6a0718SPierre Ossman 
8441c6a0718SPierre Ossman 	return IRQ_HANDLED;
8451c6a0718SPierre Ossman }
8461c6a0718SPierre Ossman 
8477584d276SJarkko Lavinen void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
8485a0f3f1fSJuha Yrjola {
8497584d276SJarkko Lavinen 	int cover_open;
8505a0f3f1fSJuha Yrjola 	struct mmc_omap_host *host = dev_get_drvdata(dev);
8517584d276SJarkko Lavinen 	struct mmc_omap_slot *slot = host->slots[num];
8525a0f3f1fSJuha Yrjola 
8537584d276SJarkko Lavinen 	BUG_ON(num >= host->nr_slots);
8545a0f3f1fSJuha Yrjola 
8555a0f3f1fSJuha Yrjola 	/* Other subsystems can call in here before we're initialised. */
8567584d276SJarkko Lavinen 	if (host->nr_slots == 0 || !host->slots[num])
8575a0f3f1fSJuha Yrjola 		return;
8585a0f3f1fSJuha Yrjola 
8595a0f3f1fSJuha Yrjola 	cover_open = mmc_omap_cover_is_open(slot);
8605a0f3f1fSJuha Yrjola 	if (cover_open != slot->cover_open) {
8615a0f3f1fSJuha Yrjola 		slot->cover_open = cover_open;
8627584d276SJarkko Lavinen 		sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
8635a0f3f1fSJuha Yrjola 	}
8647584d276SJarkko Lavinen 
8657584d276SJarkko Lavinen 	tasklet_hi_schedule(&slot->cover_tasklet);
8667584d276SJarkko Lavinen }
8677584d276SJarkko Lavinen 
8687584d276SJarkko Lavinen static void mmc_omap_cover_timer(unsigned long arg)
8697584d276SJarkko Lavinen {
8707584d276SJarkko Lavinen 	struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
8717584d276SJarkko Lavinen 	tasklet_schedule(&slot->cover_tasklet);
8727584d276SJarkko Lavinen }
8737584d276SJarkko Lavinen 
8747584d276SJarkko Lavinen static void mmc_omap_cover_handler(unsigned long param)
8757584d276SJarkko Lavinen {
8767584d276SJarkko Lavinen 	struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
8777584d276SJarkko Lavinen 	int cover_open = mmc_omap_cover_is_open(slot);
8787584d276SJarkko Lavinen 
8797584d276SJarkko Lavinen 	mmc_detect_change(slot->mmc, 0);
8807584d276SJarkko Lavinen 	if (!cover_open)
8817584d276SJarkko Lavinen 		return;
8827584d276SJarkko Lavinen 
8837584d276SJarkko Lavinen 	/*
8847584d276SJarkko Lavinen 	 * If no card is inserted, we postpone polling until
8857584d276SJarkko Lavinen 	 * the cover has been closed.
8867584d276SJarkko Lavinen 	 */
8877584d276SJarkko Lavinen 	if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
8887584d276SJarkko Lavinen 		return;
8897584d276SJarkko Lavinen 
8907584d276SJarkko Lavinen 	mod_timer(&slot->cover_timer,
8917584d276SJarkko Lavinen 		  jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
8925a0f3f1fSJuha Yrjola }
8935a0f3f1fSJuha Yrjola 
8941c6a0718SPierre Ossman /* Prepare to transfer the next segment of a scatterlist */
8951c6a0718SPierre Ossman static void
8961c6a0718SPierre Ossman mmc_omap_prepare_dma(struct mmc_omap_host *host, struct mmc_data *data)
8971c6a0718SPierre Ossman {
8981c6a0718SPierre Ossman 	int dma_ch = host->dma_ch;
8991c6a0718SPierre Ossman 	unsigned long data_addr;
9001c6a0718SPierre Ossman 	u16 buf, frame;
9011c6a0718SPierre Ossman 	u32 count;
9021c6a0718SPierre Ossman 	struct scatterlist *sg = &data->sg[host->sg_idx];
9031c6a0718SPierre Ossman 	int src_port = 0;
9041c6a0718SPierre Ossman 	int dst_port = 0;
9051c6a0718SPierre Ossman 	int sync_dev = 0;
9061c6a0718SPierre Ossman 
9070e950fa6SMarek Belisko 	data_addr = host->phys_base + OMAP_MMC_REG(host, DATA);
9081c6a0718SPierre Ossman 	frame = data->blksz;
9091c6a0718SPierre Ossman 	count = sg_dma_len(sg);
9101c6a0718SPierre Ossman 
9111c6a0718SPierre Ossman 	if ((data->blocks == 1) && (count > data->blksz))
9121c6a0718SPierre Ossman 		count = frame;
9131c6a0718SPierre Ossman 
9141c6a0718SPierre Ossman 	host->dma_len = count;
9151c6a0718SPierre Ossman 
9161c6a0718SPierre Ossman 	/* FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx and 24xx.
9171c6a0718SPierre Ossman 	 * Use 16 or 32 word frames when the blocksize is at least that large.
9181c6a0718SPierre Ossman 	 * Blocksize is usually 512 bytes; but not for some SD reads.
9191c6a0718SPierre Ossman 	 */
9201c6a0718SPierre Ossman 	if (cpu_is_omap15xx() && frame > 32)
9211c6a0718SPierre Ossman 		frame = 32;
9221c6a0718SPierre Ossman 	else if (frame > 64)
9231c6a0718SPierre Ossman 		frame = 64;
9241c6a0718SPierre Ossman 	count /= frame;
9251c6a0718SPierre Ossman 	frame >>= 1;
9261c6a0718SPierre Ossman 
9271c6a0718SPierre Ossman 	if (!(data->flags & MMC_DATA_WRITE)) {
9281c6a0718SPierre Ossman 		buf = 0x800f | ((frame - 1) << 8);
9291c6a0718SPierre Ossman 
9301c6a0718SPierre Ossman 		if (cpu_class_is_omap1()) {
9311c6a0718SPierre Ossman 			src_port = OMAP_DMA_PORT_TIPB;
9321c6a0718SPierre Ossman 			dst_port = OMAP_DMA_PORT_EMIFF;
9331c6a0718SPierre Ossman 		}
9341c6a0718SPierre Ossman 		if (cpu_is_omap24xx())
9351c6a0718SPierre Ossman 			sync_dev = OMAP24XX_DMA_MMC1_RX;
9361c6a0718SPierre Ossman 
9371c6a0718SPierre Ossman 		omap_set_dma_src_params(dma_ch, src_port,
9381c6a0718SPierre Ossman 					OMAP_DMA_AMODE_CONSTANT,
9391c6a0718SPierre Ossman 					data_addr, 0, 0);
9401c6a0718SPierre Ossman 		omap_set_dma_dest_params(dma_ch, dst_port,
9411c6a0718SPierre Ossman 					 OMAP_DMA_AMODE_POST_INC,
9421c6a0718SPierre Ossman 					 sg_dma_address(sg), 0, 0);
9431c6a0718SPierre Ossman 		omap_set_dma_dest_data_pack(dma_ch, 1);
9441c6a0718SPierre Ossman 		omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
9451c6a0718SPierre Ossman 	} else {
9461c6a0718SPierre Ossman 		buf = 0x0f80 | ((frame - 1) << 0);
9471c6a0718SPierre Ossman 
9481c6a0718SPierre Ossman 		if (cpu_class_is_omap1()) {
9491c6a0718SPierre Ossman 			src_port = OMAP_DMA_PORT_EMIFF;
9501c6a0718SPierre Ossman 			dst_port = OMAP_DMA_PORT_TIPB;
9511c6a0718SPierre Ossman 		}
9521c6a0718SPierre Ossman 		if (cpu_is_omap24xx())
9531c6a0718SPierre Ossman 			sync_dev = OMAP24XX_DMA_MMC1_TX;
9541c6a0718SPierre Ossman 
9551c6a0718SPierre Ossman 		omap_set_dma_dest_params(dma_ch, dst_port,
9561c6a0718SPierre Ossman 					 OMAP_DMA_AMODE_CONSTANT,
9571c6a0718SPierre Ossman 					 data_addr, 0, 0);
9581c6a0718SPierre Ossman 		omap_set_dma_src_params(dma_ch, src_port,
9591c6a0718SPierre Ossman 					OMAP_DMA_AMODE_POST_INC,
9601c6a0718SPierre Ossman 					sg_dma_address(sg), 0, 0);
9611c6a0718SPierre Ossman 		omap_set_dma_src_data_pack(dma_ch, 1);
9621c6a0718SPierre Ossman 		omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
9631c6a0718SPierre Ossman 	}
9641c6a0718SPierre Ossman 
9651c6a0718SPierre Ossman 	/* Max limit for DMA frame count is 0xffff */
9661c6a0718SPierre Ossman 	BUG_ON(count > 0xffff);
9671c6a0718SPierre Ossman 
9681c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, BUF, buf);
9691c6a0718SPierre Ossman 	omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S16,
9701c6a0718SPierre Ossman 				     frame, count, OMAP_DMA_SYNC_FRAME,
9711c6a0718SPierre Ossman 				     sync_dev, 0);
9721c6a0718SPierre Ossman }
9731c6a0718SPierre Ossman 
9741c6a0718SPierre Ossman /* A scatterlist segment completed */
9751c6a0718SPierre Ossman static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
9761c6a0718SPierre Ossman {
9771c6a0718SPierre Ossman 	struct mmc_omap_host *host = (struct mmc_omap_host *) data;
9781c6a0718SPierre Ossman 	struct mmc_data *mmcdat = host->data;
9791c6a0718SPierre Ossman 
9801c6a0718SPierre Ossman 	if (unlikely(host->dma_ch < 0)) {
9811c6a0718SPierre Ossman 		dev_err(mmc_dev(host->mmc),
9821c6a0718SPierre Ossman 			"DMA callback while DMA not enabled\n");
9831c6a0718SPierre Ossman 		return;
9841c6a0718SPierre Ossman 	}
9851c6a0718SPierre Ossman 	/* FIXME: We really should do something to _handle_ the errors */
9861c6a0718SPierre Ossman 	if (ch_status & OMAP1_DMA_TOUT_IRQ) {
9871c6a0718SPierre Ossman 		dev_err(mmc_dev(host->mmc),"DMA timeout\n");
9881c6a0718SPierre Ossman 		return;
9891c6a0718SPierre Ossman 	}
9901c6a0718SPierre Ossman 	if (ch_status & OMAP_DMA_DROP_IRQ) {
9911c6a0718SPierre Ossman 		dev_err(mmc_dev(host->mmc), "DMA sync error\n");
9921c6a0718SPierre Ossman 		return;
9931c6a0718SPierre Ossman 	}
9941c6a0718SPierre Ossman 	if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
9951c6a0718SPierre Ossman 		return;
9961c6a0718SPierre Ossman 	}
9971c6a0718SPierre Ossman 	mmcdat->bytes_xfered += host->dma_len;
9981c6a0718SPierre Ossman 	host->sg_idx++;
9991c6a0718SPierre Ossman 	if (host->sg_idx < host->sg_len) {
10001c6a0718SPierre Ossman 		mmc_omap_prepare_dma(host, host->data);
10011c6a0718SPierre Ossman 		omap_start_dma(host->dma_ch);
10021c6a0718SPierre Ossman 	} else
10031c6a0718SPierre Ossman 		mmc_omap_dma_done(host, host->data);
10041c6a0718SPierre Ossman }
10051c6a0718SPierre Ossman 
10061c6a0718SPierre Ossman static int mmc_omap_get_dma_channel(struct mmc_omap_host *host, struct mmc_data *data)
10071c6a0718SPierre Ossman {
1008df48dd02STony Lindgren 	const char *dma_dev_name;
10091c6a0718SPierre Ossman 	int sync_dev, dma_ch, is_read, r;
10101c6a0718SPierre Ossman 
10111c6a0718SPierre Ossman 	is_read = !(data->flags & MMC_DATA_WRITE);
10121c6a0718SPierre Ossman 	del_timer_sync(&host->dma_timer);
10131c6a0718SPierre Ossman 	if (host->dma_ch >= 0) {
10141c6a0718SPierre Ossman 		if (is_read == host->dma_is_read)
10151c6a0718SPierre Ossman 			return 0;
10161c6a0718SPierre Ossman 		omap_free_dma(host->dma_ch);
10171c6a0718SPierre Ossman 		host->dma_ch = -1;
10181c6a0718SPierre Ossman 	}
10191c6a0718SPierre Ossman 
10201c6a0718SPierre Ossman 	if (is_read) {
1021d8874665STony Lindgren 		if (host->id == 0) {
10221c6a0718SPierre Ossman 			sync_dev = OMAP_DMA_MMC_RX;
1023df48dd02STony Lindgren 			dma_dev_name = "MMC1 read";
10241c6a0718SPierre Ossman 		} else {
10251c6a0718SPierre Ossman 			sync_dev = OMAP_DMA_MMC2_RX;
1026df48dd02STony Lindgren 			dma_dev_name = "MMC2 read";
10271c6a0718SPierre Ossman 		}
10281c6a0718SPierre Ossman 	} else {
1029d8874665STony Lindgren 		if (host->id == 0) {
10301c6a0718SPierre Ossman 			sync_dev = OMAP_DMA_MMC_TX;
1031df48dd02STony Lindgren 			dma_dev_name = "MMC1 write";
10321c6a0718SPierre Ossman 		} else {
10331c6a0718SPierre Ossman 			sync_dev = OMAP_DMA_MMC2_TX;
1034df48dd02STony Lindgren 			dma_dev_name = "MMC2 write";
10351c6a0718SPierre Ossman 		}
10361c6a0718SPierre Ossman 	}
1037df48dd02STony Lindgren 	r = omap_request_dma(sync_dev, dma_dev_name, mmc_omap_dma_cb,
10381c6a0718SPierre Ossman 			     host, &dma_ch);
10391c6a0718SPierre Ossman 	if (r != 0) {
10401c6a0718SPierre Ossman 		dev_dbg(mmc_dev(host->mmc), "omap_request_dma() failed with %d\n", r);
10411c6a0718SPierre Ossman 		return r;
10421c6a0718SPierre Ossman 	}
10431c6a0718SPierre Ossman 	host->dma_ch = dma_ch;
10441c6a0718SPierre Ossman 	host->dma_is_read = is_read;
10451c6a0718SPierre Ossman 
10461c6a0718SPierre Ossman 	return 0;
10471c6a0718SPierre Ossman }
10481c6a0718SPierre Ossman 
10491c6a0718SPierre Ossman static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
10501c6a0718SPierre Ossman {
10511c6a0718SPierre Ossman 	u16 reg;
10521c6a0718SPierre Ossman 
10531c6a0718SPierre Ossman 	reg = OMAP_MMC_READ(host, SDIO);
10541c6a0718SPierre Ossman 	reg &= ~(1 << 5);
10551c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, SDIO, reg);
10561c6a0718SPierre Ossman 	/* Set maximum timeout */
10571c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, CTO, 0xff);
10581c6a0718SPierre Ossman }
10591c6a0718SPierre Ossman 
10601c6a0718SPierre Ossman static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
10611c6a0718SPierre Ossman {
1062b8f9f0e9SJuha Yrjola 	unsigned int timeout, cycle_ns;
10631c6a0718SPierre Ossman 	u16 reg;
10641c6a0718SPierre Ossman 
1065b8f9f0e9SJuha Yrjola 	cycle_ns = 1000000000 / host->current_slot->fclk_freq;
1066b8f9f0e9SJuha Yrjola 	timeout = req->data->timeout_ns / cycle_ns;
1067b8f9f0e9SJuha Yrjola 	timeout += req->data->timeout_clks;
10681c6a0718SPierre Ossman 
10691c6a0718SPierre Ossman 	/* Check if we need to use timeout multiplier register */
10701c6a0718SPierre Ossman 	reg = OMAP_MMC_READ(host, SDIO);
10711c6a0718SPierre Ossman 	if (timeout > 0xffff) {
10721c6a0718SPierre Ossman 		reg |= (1 << 5);
10731c6a0718SPierre Ossman 		timeout /= 1024;
10741c6a0718SPierre Ossman 	} else
10751c6a0718SPierre Ossman 		reg &= ~(1 << 5);
10761c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, SDIO, reg);
10771c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, DTO, timeout);
10781c6a0718SPierre Ossman }
10791c6a0718SPierre Ossman 
10801c6a0718SPierre Ossman static void
10811c6a0718SPierre Ossman mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
10821c6a0718SPierre Ossman {
10831c6a0718SPierre Ossman 	struct mmc_data *data = req->data;
10841c6a0718SPierre Ossman 	int i, use_dma, block_size;
10851c6a0718SPierre Ossman 	unsigned sg_len;
10861c6a0718SPierre Ossman 
10871c6a0718SPierre Ossman 	host->data = data;
10881c6a0718SPierre Ossman 	if (data == NULL) {
10891c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, BLEN, 0);
10901c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, NBLK, 0);
10911c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, BUF, 0);
10921c6a0718SPierre Ossman 		host->dma_in_use = 0;
10931c6a0718SPierre Ossman 		set_cmd_timeout(host, req);
10941c6a0718SPierre Ossman 		return;
10951c6a0718SPierre Ossman 	}
10961c6a0718SPierre Ossman 
10971c6a0718SPierre Ossman 	block_size = data->blksz;
10981c6a0718SPierre Ossman 
10991c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
11001c6a0718SPierre Ossman 	OMAP_MMC_WRITE(host, BLEN, block_size - 1);
11011c6a0718SPierre Ossman 	set_data_timeout(host, req);
11021c6a0718SPierre Ossman 
11031c6a0718SPierre Ossman 	/* cope with calling layer confusion; it issues "single
11041c6a0718SPierre Ossman 	 * block" writes using multi-block scatterlists.
11051c6a0718SPierre Ossman 	 */
11061c6a0718SPierre Ossman 	sg_len = (data->blocks == 1) ? 1 : data->sg_len;
11071c6a0718SPierre Ossman 
11081c6a0718SPierre Ossman 	/* Only do DMA for entire blocks */
11091c6a0718SPierre Ossman 	use_dma = host->use_dma;
11101c6a0718SPierre Ossman 	if (use_dma) {
11111c6a0718SPierre Ossman 		for (i = 0; i < sg_len; i++) {
11121c6a0718SPierre Ossman 			if ((data->sg[i].length % block_size) != 0) {
11131c6a0718SPierre Ossman 				use_dma = 0;
11141c6a0718SPierre Ossman 				break;
11151c6a0718SPierre Ossman 			}
11161c6a0718SPierre Ossman 		}
11171c6a0718SPierre Ossman 	}
11181c6a0718SPierre Ossman 
11191c6a0718SPierre Ossman 	host->sg_idx = 0;
11201c6a0718SPierre Ossman 	if (use_dma) {
11211c6a0718SPierre Ossman 		if (mmc_omap_get_dma_channel(host, data) == 0) {
11221c6a0718SPierre Ossman 			enum dma_data_direction dma_data_dir;
11231c6a0718SPierre Ossman 
11241c6a0718SPierre Ossman 			if (data->flags & MMC_DATA_WRITE)
11251c6a0718SPierre Ossman 				dma_data_dir = DMA_TO_DEVICE;
11261c6a0718SPierre Ossman 			else
11271c6a0718SPierre Ossman 				dma_data_dir = DMA_FROM_DEVICE;
11281c6a0718SPierre Ossman 
11291c6a0718SPierre Ossman 			host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
11301c6a0718SPierre Ossman 						sg_len, dma_data_dir);
11311c6a0718SPierre Ossman 			host->total_bytes_left = 0;
11321c6a0718SPierre Ossman 			mmc_omap_prepare_dma(host, req->data);
11331c6a0718SPierre Ossman 			host->brs_received = 0;
11341c6a0718SPierre Ossman 			host->dma_done = 0;
11351c6a0718SPierre Ossman 			host->dma_in_use = 1;
11361c6a0718SPierre Ossman 		} else
11371c6a0718SPierre Ossman 			use_dma = 0;
11381c6a0718SPierre Ossman 	}
11391c6a0718SPierre Ossman 
11401c6a0718SPierre Ossman 	/* Revert to PIO? */
11411c6a0718SPierre Ossman 	if (!use_dma) {
11421c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, BUF, 0x1f1f);
11431c6a0718SPierre Ossman 		host->total_bytes_left = data->blocks * block_size;
11441c6a0718SPierre Ossman 		host->sg_len = sg_len;
11451c6a0718SPierre Ossman 		mmc_omap_sg_to_buf(host);
11461c6a0718SPierre Ossman 		host->dma_in_use = 0;
11471c6a0718SPierre Ossman 	}
11481c6a0718SPierre Ossman }
11491c6a0718SPierre Ossman 
1150abfbe5f7SJuha Yrjola static void mmc_omap_start_request(struct mmc_omap_host *host,
1151abfbe5f7SJuha Yrjola 				   struct mmc_request *req)
11521c6a0718SPierre Ossman {
1153abfbe5f7SJuha Yrjola 	BUG_ON(host->mrq != NULL);
11541c6a0718SPierre Ossman 
11551c6a0718SPierre Ossman 	host->mrq = req;
11561c6a0718SPierre Ossman 
11571c6a0718SPierre Ossman 	/* only touch fifo AFTER the controller readies it */
11581c6a0718SPierre Ossman 	mmc_omap_prepare_data(host, req);
11591c6a0718SPierre Ossman 	mmc_omap_start_command(host, req->cmd);
11601c6a0718SPierre Ossman 	if (host->dma_in_use)
11611c6a0718SPierre Ossman 		omap_start_dma(host->dma_ch);
1162abfbe5f7SJuha Yrjola }
1163abfbe5f7SJuha Yrjola 
1164abfbe5f7SJuha Yrjola static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
1165abfbe5f7SJuha Yrjola {
1166abfbe5f7SJuha Yrjola 	struct mmc_omap_slot *slot = mmc_priv(mmc);
1167abfbe5f7SJuha Yrjola 	struct mmc_omap_host *host = slot->host;
1168abfbe5f7SJuha Yrjola 	unsigned long flags;
1169abfbe5f7SJuha Yrjola 
1170abfbe5f7SJuha Yrjola 	spin_lock_irqsave(&host->slot_lock, flags);
1171abfbe5f7SJuha Yrjola 	if (host->mmc != NULL) {
1172abfbe5f7SJuha Yrjola 		BUG_ON(slot->mrq != NULL);
1173abfbe5f7SJuha Yrjola 		slot->mrq = req;
1174abfbe5f7SJuha Yrjola 		spin_unlock_irqrestore(&host->slot_lock, flags);
1175abfbe5f7SJuha Yrjola 		return;
1176abfbe5f7SJuha Yrjola 	} else
1177abfbe5f7SJuha Yrjola 		host->mmc = mmc;
1178abfbe5f7SJuha Yrjola 	spin_unlock_irqrestore(&host->slot_lock, flags);
1179abfbe5f7SJuha Yrjola 	mmc_omap_select_slot(slot, 1);
1180abfbe5f7SJuha Yrjola 	mmc_omap_start_request(host, req);
11811c6a0718SPierre Ossman }
11821c6a0718SPierre Ossman 
118365b5b6e5SJuha Yrjola static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
118465b5b6e5SJuha Yrjola 				int vdd)
11851c6a0718SPierre Ossman {
118665b5b6e5SJuha Yrjola 	struct mmc_omap_host *host;
11871c6a0718SPierre Ossman 
118865b5b6e5SJuha Yrjola 	host = slot->host;
118965b5b6e5SJuha Yrjola 
119065b5b6e5SJuha Yrjola 	if (slot->pdata->set_power != NULL)
119165b5b6e5SJuha Yrjola 		slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
119265b5b6e5SJuha Yrjola 					vdd);
119365b5b6e5SJuha Yrjola 
119465b5b6e5SJuha Yrjola 	if (cpu_is_omap24xx()) {
119565b5b6e5SJuha Yrjola 		u16 w;
119665b5b6e5SJuha Yrjola 
119765b5b6e5SJuha Yrjola 		if (power_on) {
119865b5b6e5SJuha Yrjola 			w = OMAP_MMC_READ(host, CON);
119965b5b6e5SJuha Yrjola 			OMAP_MMC_WRITE(host, CON, w | (1 << 11));
12001c6a0718SPierre Ossman 		} else {
120165b5b6e5SJuha Yrjola 			w = OMAP_MMC_READ(host, CON);
120265b5b6e5SJuha Yrjola 			OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
120365b5b6e5SJuha Yrjola 		}
12041c6a0718SPierre Ossman 	}
12051c6a0718SPierre Ossman }
12061c6a0718SPierre Ossman 
1207d3af5abeSTony Lindgren static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
12081c6a0718SPierre Ossman {
1209abfbe5f7SJuha Yrjola 	struct mmc_omap_slot *slot = mmc_priv(mmc);
1210abfbe5f7SJuha Yrjola 	struct mmc_omap_host *host = slot->host;
1211d3af5abeSTony Lindgren 	int func_clk_rate = clk_get_rate(host->fclk);
12121c6a0718SPierre Ossman 	int dsor;
12131c6a0718SPierre Ossman 
12141c6a0718SPierre Ossman 	if (ios->clock == 0)
1215d3af5abeSTony Lindgren 		return 0;
12161c6a0718SPierre Ossman 
1217d3af5abeSTony Lindgren 	dsor = func_clk_rate / ios->clock;
12181c6a0718SPierre Ossman 	if (dsor < 1)
12191c6a0718SPierre Ossman 		dsor = 1;
12201c6a0718SPierre Ossman 
1221d3af5abeSTony Lindgren 	if (func_clk_rate / dsor > ios->clock)
12221c6a0718SPierre Ossman 		dsor++;
12231c6a0718SPierre Ossman 
12241c6a0718SPierre Ossman 	if (dsor > 250)
12251c6a0718SPierre Ossman 		dsor = 250;
12261c6a0718SPierre Ossman 
1227abfbe5f7SJuha Yrjola 	slot->fclk_freq = func_clk_rate / dsor;
1228abfbe5f7SJuha Yrjola 
12291c6a0718SPierre Ossman 	if (ios->bus_width == MMC_BUS_WIDTH_4)
12301c6a0718SPierre Ossman 		dsor |= 1 << 15;
1231d3af5abeSTony Lindgren 
1232d3af5abeSTony Lindgren 	return dsor;
12331c6a0718SPierre Ossman }
12341c6a0718SPierre Ossman 
1235d3af5abeSTony Lindgren static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1236d3af5abeSTony Lindgren {
1237abfbe5f7SJuha Yrjola 	struct mmc_omap_slot *slot = mmc_priv(mmc);
1238abfbe5f7SJuha Yrjola 	struct mmc_omap_host *host = slot->host;
1239abfbe5f7SJuha Yrjola 	int i, dsor;
12400807a9b5SJarkko Lavinen 	int clk_enabled;
124165b5b6e5SJuha Yrjola 
124265b5b6e5SJuha Yrjola 	mmc_omap_select_slot(slot, 0);
124365b5b6e5SJuha Yrjola 
12440807a9b5SJarkko Lavinen 	dsor = mmc_omap_calc_divisor(mmc, ios);
12450807a9b5SJarkko Lavinen 
124665b5b6e5SJuha Yrjola 	if (ios->vdd != slot->vdd)
124765b5b6e5SJuha Yrjola 		slot->vdd = ios->vdd;
1248d3af5abeSTony Lindgren 
12490807a9b5SJarkko Lavinen 	clk_enabled = 0;
12501c6a0718SPierre Ossman 	switch (ios->power_mode) {
12511c6a0718SPierre Ossman 	case MMC_POWER_OFF:
125265b5b6e5SJuha Yrjola 		mmc_omap_set_power(slot, 0, ios->vdd);
12531c6a0718SPierre Ossman 		break;
12541c6a0718SPierre Ossman 	case MMC_POWER_UP:
125546a6730eSTony Lindgren 		/* Cannot touch dsor yet, just power up MMC */
125665b5b6e5SJuha Yrjola 		mmc_omap_set_power(slot, 1, ios->vdd);
125765b5b6e5SJuha Yrjola 		goto exit;
125846a6730eSTony Lindgren 	case MMC_POWER_ON:
12590807a9b5SJarkko Lavinen 		mmc_omap_fclk_enable(host, 1);
12600807a9b5SJarkko Lavinen 		clk_enabled = 1;
12611c6a0718SPierre Ossman 		dsor |= 1 << 11;
12621c6a0718SPierre Ossman 		break;
12631c6a0718SPierre Ossman 	}
12641c6a0718SPierre Ossman 
126565b5b6e5SJuha Yrjola 	if (slot->bus_mode != ios->bus_mode) {
126665b5b6e5SJuha Yrjola 		if (slot->pdata->set_bus_mode != NULL)
126765b5b6e5SJuha Yrjola 			slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
126865b5b6e5SJuha Yrjola 						  ios->bus_mode);
126965b5b6e5SJuha Yrjola 		slot->bus_mode = ios->bus_mode;
127065b5b6e5SJuha Yrjola 	}
12711c6a0718SPierre Ossman 
12721c6a0718SPierre Ossman 	/* On insanely high arm_per frequencies something sometimes
12731c6a0718SPierre Ossman 	 * goes somehow out of sync, and the POW bit is not being set,
12741c6a0718SPierre Ossman 	 * which results in the while loop below getting stuck.
12751c6a0718SPierre Ossman 	 * Writing to the CON register twice seems to do the trick. */
12761c6a0718SPierre Ossman 	for (i = 0; i < 2; i++)
12771c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, CON, dsor);
127865b5b6e5SJuha Yrjola 	slot->saved_con = dsor;
127946a6730eSTony Lindgren 	if (ios->power_mode == MMC_POWER_ON) {
12809d7c6eeeSJarkko Lavinen 		/* worst case at 400kHz, 80 cycles makes 200 microsecs */
12819d7c6eeeSJarkko Lavinen 		int usecs = 250;
12829d7c6eeeSJarkko Lavinen 
12831c6a0718SPierre Ossman 		/* Send clock cycles, poll completion */
12841c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, IE, 0);
12851c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, STAT, 0xffff);
12861c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, CMD, 1 << 7);
12879d7c6eeeSJarkko Lavinen 		while (usecs > 0 && (OMAP_MMC_READ(host, STAT) & 1) == 0) {
12889d7c6eeeSJarkko Lavinen 			udelay(1);
12899d7c6eeeSJarkko Lavinen 			usecs--;
12909d7c6eeeSJarkko Lavinen 		}
12911c6a0718SPierre Ossman 		OMAP_MMC_WRITE(host, STAT, 1);
12921c6a0718SPierre Ossman 	}
129365b5b6e5SJuha Yrjola 
129465b5b6e5SJuha Yrjola exit:
12950807a9b5SJarkko Lavinen 	mmc_omap_release_slot(slot, clk_enabled);
12961c6a0718SPierre Ossman }
12971c6a0718SPierre Ossman 
12981c6a0718SPierre Ossman static const struct mmc_host_ops mmc_omap_ops = {
12991c6a0718SPierre Ossman 	.request	= mmc_omap_request,
13001c6a0718SPierre Ossman 	.set_ios	= mmc_omap_set_ios,
13011c6a0718SPierre Ossman };
13021c6a0718SPierre Ossman 
1303abfbe5f7SJuha Yrjola static int __init mmc_omap_new_slot(struct mmc_omap_host *host, int id)
1304abfbe5f7SJuha Yrjola {
1305abfbe5f7SJuha Yrjola 	struct mmc_omap_slot *slot = NULL;
1306abfbe5f7SJuha Yrjola 	struct mmc_host *mmc;
1307abfbe5f7SJuha Yrjola 	int r;
1308abfbe5f7SJuha Yrjola 
1309abfbe5f7SJuha Yrjola 	mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
1310abfbe5f7SJuha Yrjola 	if (mmc == NULL)
1311abfbe5f7SJuha Yrjola 		return -ENOMEM;
1312abfbe5f7SJuha Yrjola 
1313abfbe5f7SJuha Yrjola 	slot = mmc_priv(mmc);
1314abfbe5f7SJuha Yrjola 	slot->host = host;
1315abfbe5f7SJuha Yrjola 	slot->mmc = mmc;
1316abfbe5f7SJuha Yrjola 	slot->id = id;
1317abfbe5f7SJuha Yrjola 	slot->pdata = &host->pdata->slots[id];
1318abfbe5f7SJuha Yrjola 
1319abfbe5f7SJuha Yrjola 	host->slots[id] = slot;
1320abfbe5f7SJuha Yrjola 
132123af6039SPierre Ossman 	mmc->caps = 0;
132290c62bf0STony Lindgren 	if (host->pdata->slots[id].wires >= 4)
1323abfbe5f7SJuha Yrjola 		mmc->caps |= MMC_CAP_4_BIT_DATA;
1324abfbe5f7SJuha Yrjola 
1325abfbe5f7SJuha Yrjola 	mmc->ops = &mmc_omap_ops;
1326abfbe5f7SJuha Yrjola 	mmc->f_min = 400000;
1327abfbe5f7SJuha Yrjola 
1328abfbe5f7SJuha Yrjola 	if (cpu_class_is_omap2())
1329abfbe5f7SJuha Yrjola 		mmc->f_max = 48000000;
1330abfbe5f7SJuha Yrjola 	else
1331abfbe5f7SJuha Yrjola 		mmc->f_max = 24000000;
1332abfbe5f7SJuha Yrjola 	if (host->pdata->max_freq)
1333abfbe5f7SJuha Yrjola 		mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
1334abfbe5f7SJuha Yrjola 	mmc->ocr_avail = slot->pdata->ocr_mask;
1335abfbe5f7SJuha Yrjola 
1336abfbe5f7SJuha Yrjola 	/* Use scatterlist DMA to reduce per-transfer costs.
1337abfbe5f7SJuha Yrjola 	 * NOTE max_seg_size assumption that small blocks aren't
1338abfbe5f7SJuha Yrjola 	 * normally used (except e.g. for reading SD registers).
1339abfbe5f7SJuha Yrjola 	 */
1340a36274e0SMartin K. Petersen 	mmc->max_segs = 32;
1341abfbe5f7SJuha Yrjola 	mmc->max_blk_size = 2048;	/* BLEN is 11 bits (+1) */
1342abfbe5f7SJuha Yrjola 	mmc->max_blk_count = 2048;	/* NBLK is 11 bits (+1) */
1343abfbe5f7SJuha Yrjola 	mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1344abfbe5f7SJuha Yrjola 	mmc->max_seg_size = mmc->max_req_size;
1345abfbe5f7SJuha Yrjola 
1346abfbe5f7SJuha Yrjola 	r = mmc_add_host(mmc);
1347abfbe5f7SJuha Yrjola 	if (r < 0)
1348abfbe5f7SJuha Yrjola 		goto err_remove_host;
1349abfbe5f7SJuha Yrjola 
1350abfbe5f7SJuha Yrjola 	if (slot->pdata->name != NULL) {
1351abfbe5f7SJuha Yrjola 		r = device_create_file(&mmc->class_dev,
1352abfbe5f7SJuha Yrjola 					&dev_attr_slot_name);
1353abfbe5f7SJuha Yrjola 		if (r < 0)
1354abfbe5f7SJuha Yrjola 			goto err_remove_host;
1355abfbe5f7SJuha Yrjola 	}
1356abfbe5f7SJuha Yrjola 
13575a0f3f1fSJuha Yrjola 	if (slot->pdata->get_cover_state != NULL) {
13585a0f3f1fSJuha Yrjola 		r = device_create_file(&mmc->class_dev,
13595a0f3f1fSJuha Yrjola 					&dev_attr_cover_switch);
13605a0f3f1fSJuha Yrjola 		if (r < 0)
13615a0f3f1fSJuha Yrjola 			goto err_remove_slot_name;
13625a0f3f1fSJuha Yrjola 
13637584d276SJarkko Lavinen 		setup_timer(&slot->cover_timer, mmc_omap_cover_timer,
136401e77e13SCarlos Eduardo Aguiar 			    (unsigned long)slot);
13657584d276SJarkko Lavinen 		tasklet_init(&slot->cover_tasklet, mmc_omap_cover_handler,
13667584d276SJarkko Lavinen 			     (unsigned long)slot);
13677584d276SJarkko Lavinen 		tasklet_schedule(&slot->cover_tasklet);
13685a0f3f1fSJuha Yrjola 	}
13695a0f3f1fSJuha Yrjola 
1370abfbe5f7SJuha Yrjola 	return 0;
1371abfbe5f7SJuha Yrjola 
13725a0f3f1fSJuha Yrjola err_remove_slot_name:
13735a0f3f1fSJuha Yrjola 	if (slot->pdata->name != NULL)
13745a0f3f1fSJuha Yrjola 		device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
1375abfbe5f7SJuha Yrjola err_remove_host:
1376abfbe5f7SJuha Yrjola 	mmc_remove_host(mmc);
1377abfbe5f7SJuha Yrjola 	mmc_free_host(mmc);
1378abfbe5f7SJuha Yrjola 	return r;
1379abfbe5f7SJuha Yrjola }
1380abfbe5f7SJuha Yrjola 
1381abfbe5f7SJuha Yrjola static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
1382abfbe5f7SJuha Yrjola {
1383abfbe5f7SJuha Yrjola 	struct mmc_host *mmc = slot->mmc;
1384abfbe5f7SJuha Yrjola 
1385abfbe5f7SJuha Yrjola 	if (slot->pdata->name != NULL)
1386abfbe5f7SJuha Yrjola 		device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
13875a0f3f1fSJuha Yrjola 	if (slot->pdata->get_cover_state != NULL)
13885a0f3f1fSJuha Yrjola 		device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
13895a0f3f1fSJuha Yrjola 
13907584d276SJarkko Lavinen 	tasklet_kill(&slot->cover_tasklet);
13917584d276SJarkko Lavinen 	del_timer_sync(&slot->cover_timer);
1392*b01a4f1cSVenkatraman S 	flush_workqueue(slot->host->mmc_omap_wq);
1393abfbe5f7SJuha Yrjola 
1394abfbe5f7SJuha Yrjola 	mmc_remove_host(mmc);
1395abfbe5f7SJuha Yrjola 	mmc_free_host(mmc);
1396abfbe5f7SJuha Yrjola }
1397abfbe5f7SJuha Yrjola 
13981c6a0718SPierre Ossman static int __init mmc_omap_probe(struct platform_device *pdev)
13991c6a0718SPierre Ossman {
1400abfbe5f7SJuha Yrjola 	struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
14011c6a0718SPierre Ossman 	struct mmc_omap_host *host = NULL;
14021c6a0718SPierre Ossman 	struct resource *res;
1403abfbe5f7SJuha Yrjola 	int i, ret = 0;
14041c6a0718SPierre Ossman 	int irq;
14051c6a0718SPierre Ossman 
1406abfbe5f7SJuha Yrjola 	if (pdata == NULL) {
14071c6a0718SPierre Ossman 		dev_err(&pdev->dev, "platform data missing\n");
14081c6a0718SPierre Ossman 		return -ENXIO;
14091c6a0718SPierre Ossman 	}
1410abfbe5f7SJuha Yrjola 	if (pdata->nr_slots == 0) {
1411abfbe5f7SJuha Yrjola 		dev_err(&pdev->dev, "no slots\n");
1412abfbe5f7SJuha Yrjola 		return -ENXIO;
1413abfbe5f7SJuha Yrjola 	}
14141c6a0718SPierre Ossman 
14151c6a0718SPierre Ossman 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
14161c6a0718SPierre Ossman 	irq = platform_get_irq(pdev, 0);
14171c6a0718SPierre Ossman 	if (res == NULL || irq < 0)
14181c6a0718SPierre Ossman 		return -ENXIO;
14191c6a0718SPierre Ossman 
14202092014dSChris Ball 	res = request_mem_region(res->start, resource_size(res),
14211c6a0718SPierre Ossman 				 pdev->name);
14221c6a0718SPierre Ossman 	if (res == NULL)
14231c6a0718SPierre Ossman 		return -EBUSY;
14241c6a0718SPierre Ossman 
1425abfbe5f7SJuha Yrjola 	host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
1426abfbe5f7SJuha Yrjola 	if (host == NULL) {
14271c6a0718SPierre Ossman 		ret = -ENOMEM;
14281c6a0718SPierre Ossman 		goto err_free_mem_region;
14291c6a0718SPierre Ossman 	}
14301c6a0718SPierre Ossman 
14310f602ec7SJarkko Lavinen 	INIT_WORK(&host->slot_release_work, mmc_omap_slot_release_work);
14320f602ec7SJarkko Lavinen 	INIT_WORK(&host->send_stop_work, mmc_omap_send_stop_work);
14330f602ec7SJarkko Lavinen 
14340fb4723dSJarkko Lavinen 	INIT_WORK(&host->cmd_abort_work, mmc_omap_abort_command);
14350fb4723dSJarkko Lavinen 	setup_timer(&host->cmd_abort_timer, mmc_omap_cmd_timer,
14360fb4723dSJarkko Lavinen 		    (unsigned long) host);
1437eb1860bcSJarkko Lavinen 
14380807a9b5SJarkko Lavinen 	spin_lock_init(&host->clk_lock);
14390807a9b5SJarkko Lavinen 	setup_timer(&host->clk_timer, mmc_omap_clk_timer, (unsigned long) host);
14400807a9b5SJarkko Lavinen 
14411c6a0718SPierre Ossman 	spin_lock_init(&host->dma_lock);
144201e77e13SCarlos Eduardo Aguiar 	setup_timer(&host->dma_timer, mmc_omap_dma_timer, (unsigned long) host);
1443abfbe5f7SJuha Yrjola 	spin_lock_init(&host->slot_lock);
1444abfbe5f7SJuha Yrjola 	init_waitqueue_head(&host->slot_wq);
1445abfbe5f7SJuha Yrjola 
1446abfbe5f7SJuha Yrjola 	host->pdata = pdata;
1447abfbe5f7SJuha Yrjola 	host->dev = &pdev->dev;
1448abfbe5f7SJuha Yrjola 	platform_set_drvdata(pdev, host);
1449abfbe5f7SJuha Yrjola 
14501c6a0718SPierre Ossman 	host->id = pdev->id;
14511c6a0718SPierre Ossman 	host->mem_res = res;
14521c6a0718SPierre Ossman 	host->irq = irq;
14531c6a0718SPierre Ossman 
1454abfbe5f7SJuha Yrjola 	host->use_dma = 1;
1455d8874665STony Lindgren 	host->dev->dma_mask = &pdata->dma_mask;
1456abfbe5f7SJuha Yrjola 	host->dma_ch = -1;
1457abfbe5f7SJuha Yrjola 
1458abfbe5f7SJuha Yrjola 	host->irq = irq;
1459abfbe5f7SJuha Yrjola 	host->phys_base = host->mem_res->start;
14602092014dSChris Ball 	host->virt_base = ioremap(res->start, resource_size(res));
146155c381e4SRussell King 	if (!host->virt_base)
146255c381e4SRussell King 		goto err_ioremap;
1463abfbe5f7SJuha Yrjola 
14645c9e02b1SRussell King 	host->iclk = clk_get(&pdev->dev, "ick");
1465e799acb2SLadislav Michl 	if (IS_ERR(host->iclk)) {
1466e799acb2SLadislav Michl 		ret = PTR_ERR(host->iclk);
14671c6a0718SPierre Ossman 		goto err_free_mmc_host;
1468e799acb2SLadislav Michl 	}
14691c6a0718SPierre Ossman 	clk_enable(host->iclk);
14701c6a0718SPierre Ossman 
14715c9e02b1SRussell King 	host->fclk = clk_get(&pdev->dev, "fck");
14721c6a0718SPierre Ossman 	if (IS_ERR(host->fclk)) {
14731c6a0718SPierre Ossman 		ret = PTR_ERR(host->fclk);
14741c6a0718SPierre Ossman 		goto err_free_iclk;
14751c6a0718SPierre Ossman 	}
14761c6a0718SPierre Ossman 
14771c6a0718SPierre Ossman 	ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
14781c6a0718SPierre Ossman 	if (ret)
1479abfbe5f7SJuha Yrjola 		goto err_free_fclk;
14801c6a0718SPierre Ossman 
1481abfbe5f7SJuha Yrjola 	if (pdata->init != NULL) {
1482abfbe5f7SJuha Yrjola 		ret = pdata->init(&pdev->dev);
1483abfbe5f7SJuha Yrjola 		if (ret < 0)
1484abfbe5f7SJuha Yrjola 			goto err_free_irq;
1485abfbe5f7SJuha Yrjola 	}
14861c6a0718SPierre Ossman 
1487abfbe5f7SJuha Yrjola 	host->nr_slots = pdata->nr_slots;
1488abfbe5f7SJuha Yrjola 	for (i = 0; i < pdata->nr_slots; i++) {
1489abfbe5f7SJuha Yrjola 		ret = mmc_omap_new_slot(host, i);
1490abfbe5f7SJuha Yrjola 		if (ret < 0) {
1491abfbe5f7SJuha Yrjola 			while (--i >= 0)
1492abfbe5f7SJuha Yrjola 				mmc_omap_remove_slot(host->slots[i]);
1493abfbe5f7SJuha Yrjola 
1494abfbe5f7SJuha Yrjola 			goto err_plat_cleanup;
1495abfbe5f7SJuha Yrjola 		}
1496abfbe5f7SJuha Yrjola 	}
14971c6a0718SPierre Ossman 
14980e950fa6SMarek Belisko 	host->reg_shift = (cpu_is_omap7xx() ? 1 : 2);
14990e950fa6SMarek Belisko 
1500*b01a4f1cSVenkatraman S 	host->mmc_omap_wq = alloc_workqueue("mmc_omap", 0, 0);
1501*b01a4f1cSVenkatraman S 	if (!host->mmc_omap_wq)
1502*b01a4f1cSVenkatraman S 		goto err_plat_cleanup;
1503*b01a4f1cSVenkatraman S 
15041c6a0718SPierre Ossman 	return 0;
15051c6a0718SPierre Ossman 
1506abfbe5f7SJuha Yrjola err_plat_cleanup:
1507abfbe5f7SJuha Yrjola 	if (pdata->cleanup)
1508abfbe5f7SJuha Yrjola 		pdata->cleanup(&pdev->dev);
1509abfbe5f7SJuha Yrjola err_free_irq:
1510abfbe5f7SJuha Yrjola 	free_irq(host->irq, host);
15111c6a0718SPierre Ossman err_free_fclk:
15121c6a0718SPierre Ossman 	clk_put(host->fclk);
15131c6a0718SPierre Ossman err_free_iclk:
15141c6a0718SPierre Ossman 	clk_disable(host->iclk);
15151c6a0718SPierre Ossman 	clk_put(host->iclk);
15161c6a0718SPierre Ossman err_free_mmc_host:
151755c381e4SRussell King 	iounmap(host->virt_base);
151855c381e4SRussell King err_ioremap:
1519abfbe5f7SJuha Yrjola 	kfree(host);
15201c6a0718SPierre Ossman err_free_mem_region:
15212092014dSChris Ball 	release_mem_region(res->start, resource_size(res));
15221c6a0718SPierre Ossman 	return ret;
15231c6a0718SPierre Ossman }
15241c6a0718SPierre Ossman 
15251c6a0718SPierre Ossman static int mmc_omap_remove(struct platform_device *pdev)
15261c6a0718SPierre Ossman {
15271c6a0718SPierre Ossman 	struct mmc_omap_host *host = platform_get_drvdata(pdev);
1528abfbe5f7SJuha Yrjola 	int i;
15291c6a0718SPierre Ossman 
15301c6a0718SPierre Ossman 	platform_set_drvdata(pdev, NULL);
15311c6a0718SPierre Ossman 
15321c6a0718SPierre Ossman 	BUG_ON(host == NULL);
15331c6a0718SPierre Ossman 
1534abfbe5f7SJuha Yrjola 	for (i = 0; i < host->nr_slots; i++)
1535abfbe5f7SJuha Yrjola 		mmc_omap_remove_slot(host->slots[i]);
15361c6a0718SPierre Ossman 
1537abfbe5f7SJuha Yrjola 	if (host->pdata->cleanup)
1538abfbe5f7SJuha Yrjola 		host->pdata->cleanup(&pdev->dev);
1539abfbe5f7SJuha Yrjola 
1540d4a36645SRussell King 	mmc_omap_fclk_enable(host, 0);
154149c1d9daSLadislav Michl 	free_irq(host->irq, host);
15421c6a0718SPierre Ossman 	clk_put(host->fclk);
1543d4a36645SRussell King 	clk_disable(host->iclk);
1544d4a36645SRussell King 	clk_put(host->iclk);
15451c6a0718SPierre Ossman 
154655c381e4SRussell King 	iounmap(host->virt_base);
15471c6a0718SPierre Ossman 	release_mem_region(pdev->resource[0].start,
15481c6a0718SPierre Ossman 			   pdev->resource[0].end - pdev->resource[0].start + 1);
1549*b01a4f1cSVenkatraman S 	destroy_workqueue(host->mmc_omap_wq);
15501c6a0718SPierre Ossman 
1551abfbe5f7SJuha Yrjola 	kfree(host);
15521c6a0718SPierre Ossman 
15531c6a0718SPierre Ossman 	return 0;
15541c6a0718SPierre Ossman }
15551c6a0718SPierre Ossman 
15561c6a0718SPierre Ossman #ifdef CONFIG_PM
15571c6a0718SPierre Ossman static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
15581c6a0718SPierre Ossman {
1559abfbe5f7SJuha Yrjola 	int i, ret = 0;
15601c6a0718SPierre Ossman 	struct mmc_omap_host *host = platform_get_drvdata(pdev);
15611c6a0718SPierre Ossman 
1562abfbe5f7SJuha Yrjola 	if (host == NULL || host->suspended)
15631c6a0718SPierre Ossman 		return 0;
15641c6a0718SPierre Ossman 
1565abfbe5f7SJuha Yrjola 	for (i = 0; i < host->nr_slots; i++) {
1566abfbe5f7SJuha Yrjola 		struct mmc_omap_slot *slot;
1567abfbe5f7SJuha Yrjola 
1568abfbe5f7SJuha Yrjola 		slot = host->slots[i];
15691a13f8faSMatt Fleming 		ret = mmc_suspend_host(slot->mmc);
1570abfbe5f7SJuha Yrjola 		if (ret < 0) {
1571abfbe5f7SJuha Yrjola 			while (--i >= 0) {
1572abfbe5f7SJuha Yrjola 				slot = host->slots[i];
1573abfbe5f7SJuha Yrjola 				mmc_resume_host(slot->mmc);
15741c6a0718SPierre Ossman 			}
15751c6a0718SPierre Ossman 			return ret;
15761c6a0718SPierre Ossman 		}
1577abfbe5f7SJuha Yrjola 	}
1578abfbe5f7SJuha Yrjola 	host->suspended = 1;
1579abfbe5f7SJuha Yrjola 	return 0;
1580abfbe5f7SJuha Yrjola }
15811c6a0718SPierre Ossman 
15821c6a0718SPierre Ossman static int mmc_omap_resume(struct platform_device *pdev)
15831c6a0718SPierre Ossman {
1584abfbe5f7SJuha Yrjola 	int i, ret = 0;
15851c6a0718SPierre Ossman 	struct mmc_omap_host *host = platform_get_drvdata(pdev);
15861c6a0718SPierre Ossman 
1587abfbe5f7SJuha Yrjola 	if (host == NULL || !host->suspended)
15881c6a0718SPierre Ossman 		return 0;
15891c6a0718SPierre Ossman 
1590abfbe5f7SJuha Yrjola 	for (i = 0; i < host->nr_slots; i++) {
1591abfbe5f7SJuha Yrjola 		struct mmc_omap_slot *slot;
1592abfbe5f7SJuha Yrjola 		slot = host->slots[i];
1593abfbe5f7SJuha Yrjola 		ret = mmc_resume_host(slot->mmc);
1594abfbe5f7SJuha Yrjola 		if (ret < 0)
1595abfbe5f7SJuha Yrjola 			return ret;
1596abfbe5f7SJuha Yrjola 
15971c6a0718SPierre Ossman 		host->suspended = 0;
15981c6a0718SPierre Ossman 	}
1599abfbe5f7SJuha Yrjola 	return 0;
16001c6a0718SPierre Ossman }
16011c6a0718SPierre Ossman #else
16021c6a0718SPierre Ossman #define mmc_omap_suspend	NULL
16031c6a0718SPierre Ossman #define mmc_omap_resume		NULL
16041c6a0718SPierre Ossman #endif
16051c6a0718SPierre Ossman 
16061c6a0718SPierre Ossman static struct platform_driver mmc_omap_driver = {
16071c6a0718SPierre Ossman 	.remove		= mmc_omap_remove,
16081c6a0718SPierre Ossman 	.suspend	= mmc_omap_suspend,
16091c6a0718SPierre Ossman 	.resume		= mmc_omap_resume,
16101c6a0718SPierre Ossman 	.driver		= {
16111c6a0718SPierre Ossman 		.name	= DRIVER_NAME,
1612bc65c724SKay Sievers 		.owner	= THIS_MODULE,
16131c6a0718SPierre Ossman 	},
16141c6a0718SPierre Ossman };
16151c6a0718SPierre Ossman 
16161c6a0718SPierre Ossman static int __init mmc_omap_init(void)
16171c6a0718SPierre Ossman {
1618*b01a4f1cSVenkatraman S 	return platform_driver_probe(&mmc_omap_driver, mmc_omap_probe);
16191c6a0718SPierre Ossman }
16201c6a0718SPierre Ossman 
16211c6a0718SPierre Ossman static void __exit mmc_omap_exit(void)
16221c6a0718SPierre Ossman {
16231c6a0718SPierre Ossman 	platform_driver_unregister(&mmc_omap_driver);
16241c6a0718SPierre Ossman }
16251c6a0718SPierre Ossman 
16261c6a0718SPierre Ossman module_init(mmc_omap_init);
16271c6a0718SPierre Ossman module_exit(mmc_omap_exit);
16281c6a0718SPierre Ossman 
16291c6a0718SPierre Ossman MODULE_DESCRIPTION("OMAP Multimedia Card driver");
16301c6a0718SPierre Ossman MODULE_LICENSE("GPL");
1631bc65c724SKay Sievers MODULE_ALIAS("platform:" DRIVER_NAME);
1632d36b6910SAl Viro MODULE_AUTHOR("Juha Yrjölä");
1633