blob: 4254975f931d6e210153f5c9ba1de75680edfe2e [file] [log] [blame]
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/omap.c
Carlos Aguiar730c9b72006-03-29 09:21:00 +01003 *
4 * Copyright (C) 2004 Nokia Corporation
Al Virod36b6912011-12-29 17:09:01 -05005 * Written by Tuukka Tikkanen and Juha Yrjölä<juha.yrjola@nokia.com>
Carlos Aguiar730c9b72006-03-29 09:21:00 +01006 * Misc hacks here and there by Tony Lindgren <tony@atomide.com>
7 * Other hacks (DMA, SD, etc) by David Brownell
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Carlos Aguiar730c9b72006-03-29 09:21:00 +010014#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/ioport.h>
18#include <linux/platform_device.h>
19#include <linux/interrupt.h>
Russell King3451c062012-04-21 22:35:42 +010020#include <linux/dmaengine.h>
Carlos Aguiar730c9b72006-03-29 09:21:00 +010021#include <linux/dma-mapping.h>
22#include <linux/delay.h>
23#include <linux/spinlock.h>
24#include <linux/timer.h>
Russell King3451c062012-04-21 22:35:42 +010025#include <linux/omap-dma.h>
Carlos Aguiar730c9b72006-03-29 09:21:00 +010026#include <linux/mmc/host.h>
Carlos Aguiar730c9b72006-03-29 09:21:00 +010027#include <linux/mmc/card.h>
28#include <linux/clk.h>
Jens Axboe45711f12007-10-22 21:19:53 +020029#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Tony Lindgren68f39e72012-10-15 12:09:43 -070031#include <linux/platform_data/mmc-omap.h>
Carlos Aguiar730c9b72006-03-29 09:21:00 +010032
Carlos Aguiar730c9b72006-03-29 09:21:00 +010033
Juha Yrjola juha.yrjola0551f4d2006-11-11 23:38:36 +010034#define OMAP_MMC_REG_CMD 0x00
Marek Belisko0e950fa62010-05-26 14:41:49 -070035#define OMAP_MMC_REG_ARGL 0x01
36#define OMAP_MMC_REG_ARGH 0x02
37#define OMAP_MMC_REG_CON 0x03
38#define OMAP_MMC_REG_STAT 0x04
39#define OMAP_MMC_REG_IE 0x05
40#define OMAP_MMC_REG_CTO 0x06
41#define OMAP_MMC_REG_DTO 0x07
42#define OMAP_MMC_REG_DATA 0x08
43#define OMAP_MMC_REG_BLEN 0x09
44#define OMAP_MMC_REG_NBLK 0x0a
45#define OMAP_MMC_REG_BUF 0x0b
46#define OMAP_MMC_REG_SDIO 0x0d
47#define OMAP_MMC_REG_REV 0x0f
48#define OMAP_MMC_REG_RSP0 0x10
49#define OMAP_MMC_REG_RSP1 0x11
50#define OMAP_MMC_REG_RSP2 0x12
51#define OMAP_MMC_REG_RSP3 0x13
52#define OMAP_MMC_REG_RSP4 0x14
53#define OMAP_MMC_REG_RSP5 0x15
54#define OMAP_MMC_REG_RSP6 0x16
55#define OMAP_MMC_REG_RSP7 0x17
56#define OMAP_MMC_REG_IOSR 0x18
57#define OMAP_MMC_REG_SYSC 0x19
58#define OMAP_MMC_REG_SYSS 0x1a
Juha Yrjola juha.yrjola0551f4d2006-11-11 23:38:36 +010059
60#define OMAP_MMC_STAT_CARD_ERR (1 << 14)
61#define OMAP_MMC_STAT_CARD_IRQ (1 << 13)
62#define OMAP_MMC_STAT_OCR_BUSY (1 << 12)
63#define OMAP_MMC_STAT_A_EMPTY (1 << 11)
64#define OMAP_MMC_STAT_A_FULL (1 << 10)
65#define OMAP_MMC_STAT_CMD_CRC (1 << 8)
66#define OMAP_MMC_STAT_CMD_TOUT (1 << 7)
67#define OMAP_MMC_STAT_DATA_CRC (1 << 6)
68#define OMAP_MMC_STAT_DATA_TOUT (1 << 5)
69#define OMAP_MMC_STAT_END_BUSY (1 << 4)
70#define OMAP_MMC_STAT_END_OF_DATA (1 << 3)
71#define OMAP_MMC_STAT_CARD_BUSY (1 << 2)
72#define OMAP_MMC_STAT_END_OF_CMD (1 << 0)
73
Tony Lindgren53db20d2012-10-15 12:10:33 -070074#define mmc_omap7xx() (host->features & MMC_OMAP7XX)
75#define mmc_omap15xx() (host->features & MMC_OMAP15XX)
76#define mmc_omap16xx() (host->features & MMC_OMAP16XX)
77#define MMC_OMAP1_MASK (MMC_OMAP7XX | MMC_OMAP15XX | MMC_OMAP16XX)
78#define mmc_omap1() (host->features & MMC_OMAP1_MASK)
79#define mmc_omap2() (!mmc_omap1())
80
Marek Belisko0e950fa62010-05-26 14:41:49 -070081#define OMAP_MMC_REG(host, reg) (OMAP_MMC_REG_##reg << (host)->reg_shift)
82#define OMAP_MMC_READ(host, reg) __raw_readw((host)->virt_base + OMAP_MMC_REG(host, reg))
83#define OMAP_MMC_WRITE(host, reg, val) __raw_writew((val), (host)->virt_base + OMAP_MMC_REG(host, reg))
Juha Yrjola juha.yrjola0551f4d2006-11-11 23:38:36 +010084
85/*
86 * Command types
87 */
88#define OMAP_MMC_CMDTYPE_BC 0
89#define OMAP_MMC_CMDTYPE_BCR 1
90#define OMAP_MMC_CMDTYPE_AC 2
91#define OMAP_MMC_CMDTYPE_ADTC 3
92
Lokesh Vutla8c4cc002012-10-15 14:01:01 -070093#define OMAP_DMA_MMC_TX 21
94#define OMAP_DMA_MMC_RX 22
95#define OMAP_DMA_MMC2_TX 54
96#define OMAP_DMA_MMC2_RX 55
Carlos Aguiar730c9b72006-03-29 09:21:00 +010097
Lokesh Vutlad5e7c862012-10-15 14:03:51 -070098#define OMAP24XX_DMA_MMC2_TX 47
99#define OMAP24XX_DMA_MMC2_RX 48
100#define OMAP24XX_DMA_MMC1_TX 61
101#define OMAP24XX_DMA_MMC1_RX 62
102
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100103
104#define DRIVER_NAME "mmci-omap"
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100105
106/* Specifies how often in millisecs to poll for card status changes
107 * when the cover switch is open */
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400108#define OMAP_MMC_COVER_POLL_DELAY 500
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100109
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400110struct mmc_omap_host;
111
112struct mmc_omap_slot {
113 int id;
114 unsigned int vdd;
115 u16 saved_con;
116 u16 bus_mode;
117 unsigned int fclk_freq;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400118
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400119 struct tasklet_struct cover_tasklet;
120 struct timer_list cover_timer;
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400121 unsigned cover_open;
122
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400123 struct mmc_request *mrq;
124 struct mmc_omap_host *host;
125 struct mmc_host *mmc;
126 struct omap_mmc_slot_data *pdata;
127};
128
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100129struct mmc_omap_host {
130 int initialized;
131 int suspended;
132 struct mmc_request * mrq;
133 struct mmc_command * cmd;
134 struct mmc_data * data;
135 struct mmc_host * mmc;
136 struct device * dev;
137 unsigned char id; /* 16xx chips have 2 MMC blocks */
138 struct clk * iclk;
139 struct clk * fclk;
Russell King3451c062012-04-21 22:35:42 +0100140 struct dma_chan *dma_rx;
141 u32 dma_rx_burst;
142 struct dma_chan *dma_tx;
143 u32 dma_tx_burst;
Juha Yrjola juha.yrjola89783b1e42006-11-11 23:36:01 +0100144 struct resource *mem_res;
145 void __iomem *virt_base;
146 unsigned int phys_base;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100147 int irq;
148 unsigned char bus_mode;
Marek Belisko0e950fa62010-05-26 14:41:49 -0700149 unsigned int reg_shift;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100150
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400151 struct work_struct cmd_abort_work;
152 unsigned abort:1;
153 struct timer_list cmd_abort_timer;
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400154
Jarkko Lavinen0f602ec2008-03-26 16:09:58 -0400155 struct work_struct slot_release_work;
156 struct mmc_omap_slot *next_slot;
157 struct work_struct send_stop_work;
158 struct mmc_data *stop_data;
159
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100160 unsigned int sg_len;
161 int sg_idx;
162 u16 * buffer;
163 u32 buffer_bytes_left;
164 u32 total_bytes_left;
165
Tony Lindgren53db20d2012-10-15 12:10:33 -0700166 unsigned features;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100167 unsigned use_dma:1;
168 unsigned brs_received:1, dma_done:1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100169 unsigned dma_in_use:1;
Russell King3451c062012-04-21 22:35:42 +0100170 spinlock_t dma_lock;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100171
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400172 struct mmc_omap_slot *slots[OMAP_MMC_MAX_SLOTS];
173 struct mmc_omap_slot *current_slot;
174 spinlock_t slot_lock;
175 wait_queue_head_t slot_wq;
176 int nr_slots;
177
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400178 struct timer_list clk_timer;
179 spinlock_t clk_lock; /* for changing enabled state */
180 unsigned int fclk_enabled:1;
Venkatraman Sb01a4f12012-05-08 17:05:33 +0530181 struct workqueue_struct *mmc_omap_wq;
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400182
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400183 struct omap_mmc_platform_data *pdata;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100184};
185
Tejun Heo0d9ee5b2010-12-24 16:00:17 +0100186
Russell King7c8ad982008-09-05 15:13:24 +0100187static void mmc_omap_fclk_offdelay(struct mmc_omap_slot *slot)
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400188{
189 unsigned long tick_ns;
190
191 if (slot != NULL && slot->host->fclk_enabled && slot->fclk_freq > 0) {
192 tick_ns = (1000000000 + slot->fclk_freq - 1) / slot->fclk_freq;
193 ndelay(8 * tick_ns);
194 }
195}
196
Russell King7c8ad982008-09-05 15:13:24 +0100197static void mmc_omap_fclk_enable(struct mmc_omap_host *host, unsigned int enable)
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400198{
199 unsigned long flags;
200
201 spin_lock_irqsave(&host->clk_lock, flags);
202 if (host->fclk_enabled != enable) {
203 host->fclk_enabled = enable;
204 if (enable)
205 clk_enable(host->fclk);
206 else
207 clk_disable(host->fclk);
208 }
209 spin_unlock_irqrestore(&host->clk_lock, flags);
210}
211
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400212static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
213{
214 struct mmc_omap_host *host = slot->host;
215 unsigned long flags;
216
217 if (claimed)
218 goto no_claim;
219 spin_lock_irqsave(&host->slot_lock, flags);
220 while (host->mmc != NULL) {
221 spin_unlock_irqrestore(&host->slot_lock, flags);
222 wait_event(host->slot_wq, host->mmc == NULL);
223 spin_lock_irqsave(&host->slot_lock, flags);
224 }
225 host->mmc = slot->mmc;
226 spin_unlock_irqrestore(&host->slot_lock, flags);
227no_claim:
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400228 del_timer(&host->clk_timer);
229 if (host->current_slot != slot || !claimed)
230 mmc_omap_fclk_offdelay(host->current_slot);
231
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400232 if (host->current_slot != slot) {
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400233 OMAP_MMC_WRITE(host, CON, slot->saved_con & 0xFC00);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400234 if (host->pdata->switch_slot != NULL)
235 host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
236 host->current_slot = slot;
237 }
238
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400239 if (claimed) {
240 mmc_omap_fclk_enable(host, 1);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400241
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400242 /* Doing the dummy read here seems to work around some bug
243 * at least in OMAP24xx silicon where the command would not
244 * start after writing the CMD register. Sigh. */
245 OMAP_MMC_READ(host, CON);
246
247 OMAP_MMC_WRITE(host, CON, slot->saved_con);
248 } else
249 mmc_omap_fclk_enable(host, 0);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400250}
251
252static void mmc_omap_start_request(struct mmc_omap_host *host,
253 struct mmc_request *req);
254
Jarkko Lavinen0f602ec2008-03-26 16:09:58 -0400255static void mmc_omap_slot_release_work(struct work_struct *work)
256{
257 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
258 slot_release_work);
259 struct mmc_omap_slot *next_slot = host->next_slot;
260 struct mmc_request *rq;
261
262 host->next_slot = NULL;
263 mmc_omap_select_slot(next_slot, 1);
264
265 rq = next_slot->mrq;
266 next_slot->mrq = NULL;
267 mmc_omap_start_request(host, rq);
268}
269
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400270static void mmc_omap_release_slot(struct mmc_omap_slot *slot, int clk_enabled)
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400271{
272 struct mmc_omap_host *host = slot->host;
273 unsigned long flags;
274 int i;
275
276 BUG_ON(slot == NULL || host->mmc == NULL);
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400277
278 if (clk_enabled)
279 /* Keeps clock running for at least 8 cycles on valid freq */
280 mod_timer(&host->clk_timer, jiffies + HZ/10);
281 else {
282 del_timer(&host->clk_timer);
283 mmc_omap_fclk_offdelay(slot);
284 mmc_omap_fclk_enable(host, 0);
285 }
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400286
287 spin_lock_irqsave(&host->slot_lock, flags);
288 /* Check for any pending requests */
289 for (i = 0; i < host->nr_slots; i++) {
290 struct mmc_omap_slot *new_slot;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400291
292 if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
293 continue;
294
Jarkko Lavinen0f602ec2008-03-26 16:09:58 -0400295 BUG_ON(host->next_slot != NULL);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400296 new_slot = host->slots[i];
297 /* The current slot should not have a request in queue */
298 BUG_ON(new_slot == host->current_slot);
299
Jarkko Lavinen0f602ec2008-03-26 16:09:58 -0400300 host->next_slot = new_slot;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400301 host->mmc = new_slot->mmc;
302 spin_unlock_irqrestore(&host->slot_lock, flags);
Venkatraman Sb01a4f12012-05-08 17:05:33 +0530303 queue_work(host->mmc_omap_wq, &host->slot_release_work);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400304 return;
305 }
306
307 host->mmc = NULL;
308 wake_up(&host->slot_wq);
309 spin_unlock_irqrestore(&host->slot_lock, flags);
310}
311
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400312static inline
313int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
314{
Kyungmin Park8348f002008-03-26 16:09:38 -0400315 if (slot->pdata->get_cover_state)
316 return slot->pdata->get_cover_state(mmc_dev(slot->mmc),
317 slot->id);
318 return 0;
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400319}
320
321static ssize_t
322mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
323 char *buf)
324{
325 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
326 struct mmc_omap_slot *slot = mmc_priv(mmc);
327
328 return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
329 "closed");
330}
331
332static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
333
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400334static ssize_t
335mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
336 char *buf)
337{
338 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
339 struct mmc_omap_slot *slot = mmc_priv(mmc);
340
341 return sprintf(buf, "%s\n", slot->pdata->name);
342}
343
344static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
345
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100346static void
347mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
348{
349 u32 cmdreg;
350 u32 resptype;
351 u32 cmdtype;
352
353 host->cmd = cmd;
354
355 resptype = 0;
356 cmdtype = 0;
357
358 /* Our hardware needs to know exact type */
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100359 switch (mmc_resp_type(cmd)) {
360 case MMC_RSP_NONE:
361 break;
362 case MMC_RSP_R1:
363 case MMC_RSP_R1B:
Philip Langdale6f949902007-01-04 07:04:47 -0800364 /* resp 1, 1b, 6, 7 */
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100365 resptype = 1;
366 break;
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100367 case MMC_RSP_R2:
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100368 resptype = 2;
369 break;
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100370 case MMC_RSP_R3:
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100371 resptype = 3;
372 break;
373 default:
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100374 dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100375 break;
376 }
377
378 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
379 cmdtype = OMAP_MMC_CMDTYPE_ADTC;
380 } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
381 cmdtype = OMAP_MMC_CMDTYPE_BC;
382 } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
383 cmdtype = OMAP_MMC_CMDTYPE_BCR;
384 } else {
385 cmdtype = OMAP_MMC_CMDTYPE_AC;
386 }
387
388 cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
389
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400390 if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100391 cmdreg |= 1 << 6;
392
393 if (cmd->flags & MMC_RSP_BUSY)
394 cmdreg |= 1 << 11;
395
396 if (host->data && !(host->data->flags & MMC_DATA_WRITE))
397 cmdreg |= 1 << 15;
398
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400399 mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400400
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100401 OMAP_MMC_WRITE(host, CTO, 200);
402 OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
403 OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
404 OMAP_MMC_WRITE(host, IE,
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100405 OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
406 OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
407 OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
408 OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
409 OMAP_MMC_STAT_END_OF_DATA);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100410 OMAP_MMC_WRITE(host, CMD, cmdreg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100411}
412
413static void
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400414mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
415 int abort)
416{
417 enum dma_data_direction dma_data_dir;
Russell King3451c062012-04-21 22:35:42 +0100418 struct device *dev = mmc_dev(host->mmc);
419 struct dma_chan *c;
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400420
Russell King3451c062012-04-21 22:35:42 +0100421 if (data->flags & MMC_DATA_WRITE) {
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400422 dma_data_dir = DMA_TO_DEVICE;
Russell King3451c062012-04-21 22:35:42 +0100423 c = host->dma_tx;
424 } else {
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400425 dma_data_dir = DMA_FROM_DEVICE;
Russell King3451c062012-04-21 22:35:42 +0100426 c = host->dma_rx;
427 }
428 if (c) {
429 if (data->error) {
430 dmaengine_terminate_all(c);
431 /* Claim nothing transferred on error... */
432 data->bytes_xfered = 0;
433 }
434 dev = c->device->dev;
435 }
436 dma_unmap_sg(dev, data->sg, host->sg_len, dma_data_dir);
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400437}
438
Jarkko Lavinen0f602ec2008-03-26 16:09:58 -0400439static void mmc_omap_send_stop_work(struct work_struct *work)
440{
441 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
442 send_stop_work);
443 struct mmc_omap_slot *slot = host->current_slot;
444 struct mmc_data *data = host->stop_data;
445 unsigned long tick_ns;
446
447 tick_ns = (1000000000 + slot->fclk_freq - 1)/slot->fclk_freq;
448 ndelay(8*tick_ns);
449
450 mmc_omap_start_command(host, data->stop);
451}
452
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400453static void
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100454mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
455{
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400456 if (host->dma_in_use)
457 mmc_omap_release_dma(host, data, data->error);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100458
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100459 host->data = NULL;
460 host->sg_len = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100461
462 /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
463 * dozens of requests until the card finishes writing data.
464 * It'd be cheaper to just wait till an EOFB interrupt arrives...
465 */
466
467 if (!data->stop) {
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400468 struct mmc_host *mmc;
469
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100470 host->mrq = NULL;
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400471 mmc = host->mmc;
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400472 mmc_omap_release_slot(host->current_slot, 1);
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400473 mmc_request_done(mmc, data->mrq);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100474 return;
475 }
476
Jarkko Lavinen0f602ec2008-03-26 16:09:58 -0400477 host->stop_data = data;
Venkatraman Sb01a4f12012-05-08 17:05:33 +0530478 queue_work(host->mmc_omap_wq, &host->send_stop_work);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100479}
480
481static void
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400482mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400483{
484 struct mmc_omap_slot *slot = host->current_slot;
485 unsigned int restarts, passes, timeout;
486 u16 stat = 0;
487
488 /* Sending abort takes 80 clocks. Have some extra and round up */
489 timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
490 restarts = 0;
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400491 while (restarts < maxloops) {
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400492 OMAP_MMC_WRITE(host, STAT, 0xFFFF);
493 OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
494
495 passes = 0;
496 while (passes < timeout) {
497 stat = OMAP_MMC_READ(host, STAT);
498 if (stat & OMAP_MMC_STAT_END_OF_CMD)
499 goto out;
500 udelay(1);
501 passes++;
502 }
503
504 restarts++;
505 }
506out:
507 OMAP_MMC_WRITE(host, STAT, stat);
508}
509
510static void
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400511mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
512{
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400513 if (host->dma_in_use)
514 mmc_omap_release_dma(host, data, 1);
515
516 host->data = NULL;
517 host->sg_len = 0;
518
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400519 mmc_omap_send_abort(host, 10000);
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400520}
521
522static void
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100523mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
524{
525 unsigned long flags;
526 int done;
527
528 if (!host->dma_in_use) {
529 mmc_omap_xfer_done(host, data);
530 return;
531 }
532 done = 0;
533 spin_lock_irqsave(&host->dma_lock, flags);
534 if (host->dma_done)
535 done = 1;
536 else
537 host->brs_received = 1;
538 spin_unlock_irqrestore(&host->dma_lock, flags);
539 if (done)
540 mmc_omap_xfer_done(host, data);
541}
542
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100543static void
544mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
545{
546 unsigned long flags;
547 int done;
548
549 done = 0;
550 spin_lock_irqsave(&host->dma_lock, flags);
551 if (host->brs_received)
552 done = 1;
553 else
554 host->dma_done = 1;
555 spin_unlock_irqrestore(&host->dma_lock, flags);
556 if (done)
557 mmc_omap_xfer_done(host, data);
558}
559
560static void
561mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
562{
563 host->cmd = NULL;
564
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400565 del_timer(&host->cmd_abort_timer);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400566
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100567 if (cmd->flags & MMC_RSP_PRESENT) {
568 if (cmd->flags & MMC_RSP_136) {
569 /* response type 2 */
570 cmd->resp[3] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100571 OMAP_MMC_READ(host, RSP0) |
572 (OMAP_MMC_READ(host, RSP1) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100573 cmd->resp[2] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100574 OMAP_MMC_READ(host, RSP2) |
575 (OMAP_MMC_READ(host, RSP3) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100576 cmd->resp[1] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100577 OMAP_MMC_READ(host, RSP4) |
578 (OMAP_MMC_READ(host, RSP5) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100579 cmd->resp[0] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100580 OMAP_MMC_READ(host, RSP6) |
581 (OMAP_MMC_READ(host, RSP7) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100582 } else {
583 /* response types 1, 1b, 3, 4, 5, 6 */
584 cmd->resp[0] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100585 OMAP_MMC_READ(host, RSP6) |
586 (OMAP_MMC_READ(host, RSP7) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100587 }
588 }
589
Pierre Ossman17b04292007-07-22 22:18:46 +0200590 if (host->data == NULL || cmd->error) {
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400591 struct mmc_host *mmc;
592
593 if (host->data != NULL)
594 mmc_omap_abort_xfer(host, host->data);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100595 host->mrq = NULL;
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400596 mmc = host->mmc;
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400597 mmc_omap_release_slot(host->current_slot, 1);
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400598 mmc_request_done(mmc, cmd->mrq);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100599 }
600}
601
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400602/*
603 * Abort stuck command. Can occur when card is removed while it is being
604 * read.
605 */
606static void mmc_omap_abort_command(struct work_struct *work)
607{
608 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400609 cmd_abort_work);
610 BUG_ON(!host->cmd);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400611
612 dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
613 host->cmd->opcode);
614
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400615 if (host->cmd->error == 0)
616 host->cmd->error = -ETIMEDOUT;
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400617
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400618 if (host->data == NULL) {
619 struct mmc_command *cmd;
620 struct mmc_host *mmc;
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400621
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400622 cmd = host->cmd;
623 host->cmd = NULL;
624 mmc_omap_send_abort(host, 10000);
625
626 host->mrq = NULL;
627 mmc = host->mmc;
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400628 mmc_omap_release_slot(host->current_slot, 1);
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400629 mmc_request_done(mmc, cmd->mrq);
630 } else
631 mmc_omap_cmd_done(host, host->cmd);
632
633 host->abort = 0;
634 enable_irq(host->irq);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400635}
636
637static void
638mmc_omap_cmd_timer(unsigned long data)
639{
640 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400641 unsigned long flags;
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400642
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400643 spin_lock_irqsave(&host->slot_lock, flags);
644 if (host->cmd != NULL && !host->abort) {
645 OMAP_MMC_WRITE(host, IE, 0);
646 disable_irq(host->irq);
647 host->abort = 1;
Venkatraman Sb01a4f12012-05-08 17:05:33 +0530648 queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400649 }
650 spin_unlock_irqrestore(&host->slot_lock, flags);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400651}
652
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100653/* PIO only */
654static void
655mmc_omap_sg_to_buf(struct mmc_omap_host *host)
656{
657 struct scatterlist *sg;
658
659 sg = host->data->sg + host->sg_idx;
660 host->buffer_bytes_left = sg->length;
Jens Axboe45711f12007-10-22 21:19:53 +0200661 host->buffer = sg_virt(sg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100662 if (host->buffer_bytes_left > host->total_bytes_left)
663 host->buffer_bytes_left = host->total_bytes_left;
664}
665
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400666static void
667mmc_omap_clk_timer(unsigned long data)
668{
669 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
670
671 mmc_omap_fclk_enable(host, 0);
672}
673
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100674/* PIO only */
675static void
676mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
677{
Paul Walmsley75b53ae2012-08-24 06:00:18 +0000678 int n, nwords;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100679
680 if (host->buffer_bytes_left == 0) {
681 host->sg_idx++;
682 BUG_ON(host->sg_idx == host->sg_len);
683 mmc_omap_sg_to_buf(host);
684 }
685 n = 64;
686 if (n > host->buffer_bytes_left)
687 n = host->buffer_bytes_left;
Paul Walmsley75b53ae2012-08-24 06:00:18 +0000688
689 nwords = n / 2;
690 nwords += n & 1; /* handle odd number of bytes to transfer */
691
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100692 host->buffer_bytes_left -= n;
693 host->total_bytes_left -= n;
694 host->data->bytes_xfered += n;
695
696 if (write) {
Paul Walmsley75b53ae2012-08-24 06:00:18 +0000697 __raw_writesw(host->virt_base + OMAP_MMC_REG(host, DATA),
698 host->buffer, nwords);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100699 } else {
Paul Walmsley75b53ae2012-08-24 06:00:18 +0000700 __raw_readsw(host->virt_base + OMAP_MMC_REG(host, DATA),
701 host->buffer, nwords);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100702 }
Paul Walmsley75b53ae2012-08-24 06:00:18 +0000703
704 host->buffer += nwords;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100705}
706
Venkatraman S75d569d2012-08-07 19:03:01 +0530707#ifdef CONFIG_MMC_DEBUG
708static void mmc_omap_report_irq(struct mmc_omap_host *host, u16 status)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100709{
710 static const char *mmc_omap_status_bits[] = {
711 "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
712 "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
713 };
Venkatraman S75d569d2012-08-07 19:03:01 +0530714 int i;
715 char res[64], *buf = res;
716
717 buf += sprintf(buf, "MMC IRQ 0x%x:", status);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100718
719 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
Venkatraman S75d569d2012-08-07 19:03:01 +0530720 if (status & (1 << i))
721 buf += sprintf(buf, " %s", mmc_omap_status_bits[i]);
722 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100723}
Venkatraman S75d569d2012-08-07 19:03:01 +0530724#else
725static void mmc_omap_report_irq(struct mmc_omap_host *host, u16 status)
726{
727}
728#endif
729
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100730
David Howells7d12e782006-10-05 14:55:46 +0100731static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100732{
733 struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
734 u16 status;
735 int end_command;
736 int end_transfer;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400737 int transfer_error, cmd_error;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100738
739 if (host->cmd == NULL && host->data == NULL) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100740 status = OMAP_MMC_READ(host, STAT);
Juha Yrjola2a50b882008-03-26 16:09:26 -0400741 dev_info(mmc_dev(host->slots[0]->mmc),
742 "Spurious IRQ 0x%04x\n", status);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100743 if (status != 0) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100744 OMAP_MMC_WRITE(host, STAT, status);
745 OMAP_MMC_WRITE(host, IE, 0);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100746 }
747 return IRQ_HANDLED;
748 }
749
750 end_command = 0;
751 end_transfer = 0;
752 transfer_error = 0;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400753 cmd_error = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100754
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100755 while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
Juha Yrjola2a50b882008-03-26 16:09:26 -0400756 int cmd;
757
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100758 OMAP_MMC_WRITE(host, STAT, status);
Juha Yrjola2a50b882008-03-26 16:09:26 -0400759 if (host->cmd != NULL)
760 cmd = host->cmd->opcode;
761 else
762 cmd = -1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100763 dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
Juha Yrjola2a50b882008-03-26 16:09:26 -0400764 status, cmd);
Venkatraman S75d569d2012-08-07 19:03:01 +0530765 mmc_omap_report_irq(host, status);
766
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100767 if (host->total_bytes_left) {
768 if ((status & OMAP_MMC_STAT_A_FULL) ||
769 (status & OMAP_MMC_STAT_END_OF_DATA))
770 mmc_omap_xfer_data(host, 0);
771 if (status & OMAP_MMC_STAT_A_EMPTY)
772 mmc_omap_xfer_data(host, 1);
773 }
774
Juha Yrjola2a50b882008-03-26 16:09:26 -0400775 if (status & OMAP_MMC_STAT_END_OF_DATA)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100776 end_transfer = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100777
778 if (status & OMAP_MMC_STAT_DATA_TOUT) {
Juha Yrjola2a50b882008-03-26 16:09:26 -0400779 dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
780 cmd);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100781 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200782 host->data->error = -ETIMEDOUT;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100783 transfer_error = 1;
784 }
785 }
786
787 if (status & OMAP_MMC_STAT_DATA_CRC) {
788 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200789 host->data->error = -EILSEQ;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100790 dev_dbg(mmc_dev(host->mmc),
791 "data CRC error, bytes left %d\n",
792 host->total_bytes_left);
793 transfer_error = 1;
794 } else {
795 dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
796 }
797 }
798
799 if (status & OMAP_MMC_STAT_CMD_TOUT) {
800 /* Timeouts are routine with some commands */
801 if (host->cmd) {
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400802 struct mmc_omap_slot *slot =
803 host->current_slot;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400804 if (slot == NULL ||
805 !mmc_omap_cover_is_open(slot))
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400806 dev_err(mmc_dev(host->mmc),
Juha Yrjola2a50b882008-03-26 16:09:26 -0400807 "command timeout (CMD%d)\n",
808 cmd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200809 host->cmd->error = -ETIMEDOUT;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100810 end_command = 1;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400811 cmd_error = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100812 }
813 }
814
815 if (status & OMAP_MMC_STAT_CMD_CRC) {
816 if (host->cmd) {
817 dev_err(mmc_dev(host->mmc),
818 "command CRC error (CMD%d, arg 0x%08x)\n",
Juha Yrjola2a50b882008-03-26 16:09:26 -0400819 cmd, host->cmd->arg);
Pierre Ossman17b04292007-07-22 22:18:46 +0200820 host->cmd->error = -EILSEQ;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100821 end_command = 1;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400822 cmd_error = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100823 } else
824 dev_err(mmc_dev(host->mmc),
825 "command CRC error without cmd?\n");
826 }
827
828 if (status & OMAP_MMC_STAT_CARD_ERR) {
Ragner Magalhaes0107a4b2007-06-13 19:09:28 +0200829 dev_dbg(mmc_dev(host->mmc),
830 "ignoring card status error (CMD%d)\n",
Juha Yrjola2a50b882008-03-26 16:09:26 -0400831 cmd);
Ragner Magalhaes0107a4b2007-06-13 19:09:28 +0200832 end_command = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100833 }
834
835 /*
836 * NOTE: On 1610 the END_OF_CMD may come too early when
Juha Yrjola2a50b882008-03-26 16:09:26 -0400837 * starting a write
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100838 */
839 if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
840 (!(status & OMAP_MMC_STAT_A_EMPTY))) {
841 end_command = 1;
842 }
843 }
844
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400845 if (cmd_error && host->data) {
846 del_timer(&host->cmd_abort_timer);
847 host->abort = 1;
848 OMAP_MMC_WRITE(host, IE, 0);
Ben Nizettee749c6f2009-04-16 15:55:21 +1000849 disable_irq_nosync(host->irq);
Venkatraman Sb01a4f12012-05-08 17:05:33 +0530850 queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400851 return IRQ_HANDLED;
852 }
853
Michael Bueschf6947512011-04-11 17:00:44 -0400854 if (end_command && host->cmd)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100855 mmc_omap_cmd_done(host, host->cmd);
Juha Yrjola2a50b882008-03-26 16:09:26 -0400856 if (host->data != NULL) {
857 if (transfer_error)
858 mmc_omap_xfer_done(host, host->data);
859 else if (end_transfer)
860 mmc_omap_end_of_data(host, host->data);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100861 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100862
863 return IRQ_HANDLED;
864}
865
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400866void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400867{
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400868 int cover_open;
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400869 struct mmc_omap_host *host = dev_get_drvdata(dev);
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400870 struct mmc_omap_slot *slot = host->slots[num];
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400871
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400872 BUG_ON(num >= host->nr_slots);
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400873
874 /* Other subsystems can call in here before we're initialised. */
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400875 if (host->nr_slots == 0 || !host->slots[num])
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400876 return;
877
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400878 cover_open = mmc_omap_cover_is_open(slot);
879 if (cover_open != slot->cover_open) {
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400880 slot->cover_open = cover_open;
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400881 sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400882 }
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400883
884 tasklet_hi_schedule(&slot->cover_tasklet);
885}
886
887static void mmc_omap_cover_timer(unsigned long arg)
888{
889 struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
890 tasklet_schedule(&slot->cover_tasklet);
891}
892
893static void mmc_omap_cover_handler(unsigned long param)
894{
895 struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
896 int cover_open = mmc_omap_cover_is_open(slot);
897
898 mmc_detect_change(slot->mmc, 0);
899 if (!cover_open)
900 return;
901
902 /*
903 * If no card is inserted, we postpone polling until
904 * the cover has been closed.
905 */
906 if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
907 return;
908
909 mod_timer(&slot->cover_timer,
910 jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400911}
912
Russell King3451c062012-04-21 22:35:42 +0100913static void mmc_omap_dma_callback(void *priv)
914{
915 struct mmc_omap_host *host = priv;
916 struct mmc_data *data = host->data;
917
918 /* If we got to the end of DMA, assume everything went well */
919 data->bytes_xfered += data->blocks * data->blksz;
920
921 mmc_omap_dma_done(host, data);
922}
923
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100924static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
925{
926 u16 reg;
927
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100928 reg = OMAP_MMC_READ(host, SDIO);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100929 reg &= ~(1 << 5);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100930 OMAP_MMC_WRITE(host, SDIO, reg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100931 /* Set maximum timeout */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100932 OMAP_MMC_WRITE(host, CTO, 0xff);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100933}
934
935static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
936{
Juha Yrjolab8f9f0e2008-03-26 16:09:16 -0400937 unsigned int timeout, cycle_ns;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100938 u16 reg;
939
Juha Yrjolab8f9f0e2008-03-26 16:09:16 -0400940 cycle_ns = 1000000000 / host->current_slot->fclk_freq;
941 timeout = req->data->timeout_ns / cycle_ns;
942 timeout += req->data->timeout_clks;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100943
944 /* Check if we need to use timeout multiplier register */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100945 reg = OMAP_MMC_READ(host, SDIO);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100946 if (timeout > 0xffff) {
947 reg |= (1 << 5);
948 timeout /= 1024;
949 } else
950 reg &= ~(1 << 5);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100951 OMAP_MMC_WRITE(host, SDIO, reg);
952 OMAP_MMC_WRITE(host, DTO, timeout);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100953}
954
955static void
956mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
957{
958 struct mmc_data *data = req->data;
959 int i, use_dma, block_size;
960 unsigned sg_len;
961
962 host->data = data;
963 if (data == NULL) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100964 OMAP_MMC_WRITE(host, BLEN, 0);
965 OMAP_MMC_WRITE(host, NBLK, 0);
966 OMAP_MMC_WRITE(host, BUF, 0);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100967 host->dma_in_use = 0;
968 set_cmd_timeout(host, req);
969 return;
970 }
971
Russell Kinga3fd4a12006-06-04 17:51:15 +0100972 block_size = data->blksz;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100973
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100974 OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
975 OMAP_MMC_WRITE(host, BLEN, block_size - 1);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100976 set_data_timeout(host, req);
977
978 /* cope with calling layer confusion; it issues "single
979 * block" writes using multi-block scatterlists.
980 */
981 sg_len = (data->blocks == 1) ? 1 : data->sg_len;
982
983 /* Only do DMA for entire blocks */
984 use_dma = host->use_dma;
985 if (use_dma) {
986 for (i = 0; i < sg_len; i++) {
987 if ((data->sg[i].length % block_size) != 0) {
988 use_dma = 0;
989 break;
990 }
991 }
992 }
993
994 host->sg_idx = 0;
995 if (use_dma) {
Russell King3451c062012-04-21 22:35:42 +0100996 enum dma_data_direction dma_data_dir;
997 struct dma_async_tx_descriptor *tx;
998 struct dma_chan *c;
999 u32 burst, *bp;
1000 u16 buf;
1001
1002 /*
1003 * FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx
1004 * and 24xx. Use 16 or 32 word frames when the
1005 * blocksize is at least that large. Blocksize is
1006 * usually 512 bytes; but not for some SD reads.
1007 */
Tony Lindgren53db20d2012-10-15 12:10:33 -07001008 burst = mmc_omap15xx() ? 32 : 64;
Russell King3451c062012-04-21 22:35:42 +01001009 if (burst > data->blksz)
1010 burst = data->blksz;
1011
1012 burst >>= 1;
1013
1014 if (data->flags & MMC_DATA_WRITE) {
1015 c = host->dma_tx;
1016 bp = &host->dma_tx_burst;
1017 buf = 0x0f80 | (burst - 1) << 0;
1018 dma_data_dir = DMA_TO_DEVICE;
1019 } else {
1020 c = host->dma_rx;
1021 bp = &host->dma_rx_burst;
1022 buf = 0x800f | (burst - 1) << 8;
1023 dma_data_dir = DMA_FROM_DEVICE;
1024 }
1025
1026 if (!c)
1027 goto use_pio;
1028
1029 /* Only reconfigure if we have a different burst size */
1030 if (*bp != burst) {
1031 struct dma_slave_config cfg;
1032
1033 cfg.src_addr = host->phys_base + OMAP_MMC_REG(host, DATA);
1034 cfg.dst_addr = host->phys_base + OMAP_MMC_REG(host, DATA);
1035 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1036 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1037 cfg.src_maxburst = burst;
1038 cfg.dst_maxburst = burst;
1039
1040 if (dmaengine_slave_config(c, &cfg))
1041 goto use_pio;
1042
1043 *bp = burst;
1044 }
1045
1046 host->sg_len = dma_map_sg(c->device->dev, data->sg, sg_len,
1047 dma_data_dir);
1048 if (host->sg_len == 0)
1049 goto use_pio;
1050
1051 tx = dmaengine_prep_slave_sg(c, data->sg, host->sg_len,
1052 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1053 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1054 if (!tx)
1055 goto use_pio;
1056
1057 OMAP_MMC_WRITE(host, BUF, buf);
1058
1059 tx->callback = mmc_omap_dma_callback;
1060 tx->callback_param = host;
1061 dmaengine_submit(tx);
1062 host->brs_received = 0;
1063 host->dma_done = 0;
1064 host->dma_in_use = 1;
1065 return;
1066 }
1067 use_pio:
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001068
1069 /* Revert to PIO? */
Russell King4e078fb2012-04-21 22:41:10 +01001070 OMAP_MMC_WRITE(host, BUF, 0x1f1f);
1071 host->total_bytes_left = data->blocks * block_size;
1072 host->sg_len = sg_len;
1073 mmc_omap_sg_to_buf(host);
1074 host->dma_in_use = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001075}
1076
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001077static void mmc_omap_start_request(struct mmc_omap_host *host,
1078 struct mmc_request *req)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001079{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001080 BUG_ON(host->mrq != NULL);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001081
1082 host->mrq = req;
1083
1084 /* only touch fifo AFTER the controller readies it */
1085 mmc_omap_prepare_data(host, req);
1086 mmc_omap_start_command(host, req->cmd);
Russell King3451c062012-04-21 22:35:42 +01001087 if (host->dma_in_use) {
1088 struct dma_chan *c = host->data->flags & MMC_DATA_WRITE ?
1089 host->dma_tx : host->dma_rx;
1090
Russell King4e078fb2012-04-21 22:41:10 +01001091 dma_async_issue_pending(c);
Russell King3451c062012-04-21 22:35:42 +01001092 }
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001093}
1094
1095static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
1096{
1097 struct mmc_omap_slot *slot = mmc_priv(mmc);
1098 struct mmc_omap_host *host = slot->host;
1099 unsigned long flags;
1100
1101 spin_lock_irqsave(&host->slot_lock, flags);
1102 if (host->mmc != NULL) {
1103 BUG_ON(slot->mrq != NULL);
1104 slot->mrq = req;
1105 spin_unlock_irqrestore(&host->slot_lock, flags);
1106 return;
1107 } else
1108 host->mmc = mmc;
1109 spin_unlock_irqrestore(&host->slot_lock, flags);
1110 mmc_omap_select_slot(slot, 1);
1111 mmc_omap_start_request(host, req);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001112}
1113
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001114static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
1115 int vdd)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001116{
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001117 struct mmc_omap_host *host;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001118
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001119 host = slot->host;
1120
1121 if (slot->pdata->set_power != NULL)
1122 slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
1123 vdd);
Tony Lindgren53db20d2012-10-15 12:10:33 -07001124 if (mmc_omap2()) {
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001125 u16 w;
1126
1127 if (power_on) {
1128 w = OMAP_MMC_READ(host, CON);
1129 OMAP_MMC_WRITE(host, CON, w | (1 << 11));
1130 } else {
1131 w = OMAP_MMC_READ(host, CON);
1132 OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
1133 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001134 }
1135}
1136
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001137static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
1138{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001139 struct mmc_omap_slot *slot = mmc_priv(mmc);
1140 struct mmc_omap_host *host = slot->host;
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001141 int func_clk_rate = clk_get_rate(host->fclk);
1142 int dsor;
1143
1144 if (ios->clock == 0)
1145 return 0;
1146
1147 dsor = func_clk_rate / ios->clock;
1148 if (dsor < 1)
1149 dsor = 1;
1150
1151 if (func_clk_rate / dsor > ios->clock)
1152 dsor++;
1153
1154 if (dsor > 250)
1155 dsor = 250;
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001156
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001157 slot->fclk_freq = func_clk_rate / dsor;
1158
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001159 if (ios->bus_width == MMC_BUS_WIDTH_4)
1160 dsor |= 1 << 15;
1161
1162 return dsor;
1163}
1164
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001165static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1166{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001167 struct mmc_omap_slot *slot = mmc_priv(mmc);
1168 struct mmc_omap_host *host = slot->host;
1169 int i, dsor;
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001170 int clk_enabled;
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001171
1172 mmc_omap_select_slot(slot, 0);
1173
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001174 dsor = mmc_omap_calc_divisor(mmc, ios);
1175
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001176 if (ios->vdd != slot->vdd)
1177 slot->vdd = ios->vdd;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001178
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001179 clk_enabled = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001180 switch (ios->power_mode) {
1181 case MMC_POWER_OFF:
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001182 mmc_omap_set_power(slot, 0, ios->vdd);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001183 break;
1184 case MMC_POWER_UP:
Tony Lindgren46a67302007-05-01 16:34:16 +02001185 /* Cannot touch dsor yet, just power up MMC */
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001186 mmc_omap_set_power(slot, 1, ios->vdd);
1187 goto exit;
Tony Lindgren46a67302007-05-01 16:34:16 +02001188 case MMC_POWER_ON:
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001189 mmc_omap_fclk_enable(host, 1);
1190 clk_enabled = 1;
Juha Yrjola juha.yrjolac5cb4312006-11-11 23:42:39 +01001191 dsor |= 1 << 11;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001192 break;
1193 }
1194
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001195 if (slot->bus_mode != ios->bus_mode) {
1196 if (slot->pdata->set_bus_mode != NULL)
1197 slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
1198 ios->bus_mode);
1199 slot->bus_mode = ios->bus_mode;
1200 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001201
1202 /* On insanely high arm_per frequencies something sometimes
1203 * goes somehow out of sync, and the POW bit is not being set,
1204 * which results in the while loop below getting stuck.
1205 * Writing to the CON register twice seems to do the trick. */
1206 for (i = 0; i < 2; i++)
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001207 OMAP_MMC_WRITE(host, CON, dsor);
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001208 slot->saved_con = dsor;
Tony Lindgren46a67302007-05-01 16:34:16 +02001209 if (ios->power_mode == MMC_POWER_ON) {
Jarkko Lavinen9d7c6ee2008-03-26 16:10:02 -04001210 /* worst case at 400kHz, 80 cycles makes 200 microsecs */
1211 int usecs = 250;
1212
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001213 /* Send clock cycles, poll completion */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001214 OMAP_MMC_WRITE(host, IE, 0);
1215 OMAP_MMC_WRITE(host, STAT, 0xffff);
Juha Yrjola juha.yrjolac5cb4312006-11-11 23:42:39 +01001216 OMAP_MMC_WRITE(host, CMD, 1 << 7);
Jarkko Lavinen9d7c6ee2008-03-26 16:10:02 -04001217 while (usecs > 0 && (OMAP_MMC_READ(host, STAT) & 1) == 0) {
1218 udelay(1);
1219 usecs--;
1220 }
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001221 OMAP_MMC_WRITE(host, STAT, 1);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001222 }
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001223
1224exit:
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001225 mmc_omap_release_slot(slot, clk_enabled);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001226}
1227
David Brownellab7aefd2006-11-12 17:55:30 -08001228static const struct mmc_host_ops mmc_omap_ops = {
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001229 .request = mmc_omap_request,
1230 .set_ios = mmc_omap_set_ios,
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001231};
1232
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001233static int mmc_omap_new_slot(struct mmc_omap_host *host, int id)
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001234{
1235 struct mmc_omap_slot *slot = NULL;
1236 struct mmc_host *mmc;
1237 int r;
1238
1239 mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
1240 if (mmc == NULL)
1241 return -ENOMEM;
1242
1243 slot = mmc_priv(mmc);
1244 slot->host = host;
1245 slot->mmc = mmc;
1246 slot->id = id;
1247 slot->pdata = &host->pdata->slots[id];
1248
1249 host->slots[id] = slot;
1250
Pierre Ossman23af6032008-07-06 01:10:27 +02001251 mmc->caps = 0;
Tony Lindgren90c62bf2008-12-10 17:37:17 -08001252 if (host->pdata->slots[id].wires >= 4)
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001253 mmc->caps |= MMC_CAP_4_BIT_DATA;
1254
1255 mmc->ops = &mmc_omap_ops;
1256 mmc->f_min = 400000;
1257
Tony Lindgren53db20d2012-10-15 12:10:33 -07001258 if (mmc_omap2())
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001259 mmc->f_max = 48000000;
1260 else
1261 mmc->f_max = 24000000;
1262 if (host->pdata->max_freq)
1263 mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
1264 mmc->ocr_avail = slot->pdata->ocr_mask;
1265
1266 /* Use scatterlist DMA to reduce per-transfer costs.
1267 * NOTE max_seg_size assumption that small blocks aren't
1268 * normally used (except e.g. for reading SD registers).
1269 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001270 mmc->max_segs = 32;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001271 mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
1272 mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
1273 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1274 mmc->max_seg_size = mmc->max_req_size;
1275
1276 r = mmc_add_host(mmc);
1277 if (r < 0)
1278 goto err_remove_host;
1279
1280 if (slot->pdata->name != NULL) {
1281 r = device_create_file(&mmc->class_dev,
1282 &dev_attr_slot_name);
1283 if (r < 0)
1284 goto err_remove_host;
1285 }
1286
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001287 if (slot->pdata->get_cover_state != NULL) {
1288 r = device_create_file(&mmc->class_dev,
1289 &dev_attr_cover_switch);
1290 if (r < 0)
1291 goto err_remove_slot_name;
1292
Jarkko Lavinen7584d272008-03-26 16:09:42 -04001293 setup_timer(&slot->cover_timer, mmc_omap_cover_timer,
1294 (unsigned long)slot);
1295 tasklet_init(&slot->cover_tasklet, mmc_omap_cover_handler,
1296 (unsigned long)slot);
1297 tasklet_schedule(&slot->cover_tasklet);
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001298 }
1299
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001300 return 0;
1301
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001302err_remove_slot_name:
1303 if (slot->pdata->name != NULL)
1304 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001305err_remove_host:
1306 mmc_remove_host(mmc);
1307 mmc_free_host(mmc);
1308 return r;
1309}
1310
1311static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
1312{
1313 struct mmc_host *mmc = slot->mmc;
1314
1315 if (slot->pdata->name != NULL)
1316 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001317 if (slot->pdata->get_cover_state != NULL)
1318 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1319
Jarkko Lavinen7584d272008-03-26 16:09:42 -04001320 tasklet_kill(&slot->cover_tasklet);
1321 del_timer_sync(&slot->cover_timer);
Venkatraman Sb01a4f12012-05-08 17:05:33 +05301322 flush_workqueue(slot->host->mmc_omap_wq);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001323
1324 mmc_remove_host(mmc);
1325 mmc_free_host(mmc);
1326}
1327
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001328static int mmc_omap_probe(struct platform_device *pdev)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001329{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001330 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001331 struct mmc_omap_host *host = NULL;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001332 struct resource *res;
Russell King3451c062012-04-21 22:35:42 +01001333 dma_cap_mask_t mask;
1334 unsigned sig;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001335 int i, ret = 0;
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001336 int irq;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001337
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001338 if (pdata == NULL) {
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001339 dev_err(&pdev->dev, "platform data missing\n");
1340 return -ENXIO;
1341 }
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001342 if (pdata->nr_slots == 0) {
1343 dev_err(&pdev->dev, "no slots\n");
1344 return -ENXIO;
1345 }
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001346
1347 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001348 irq = platform_get_irq(pdev, 0);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001349 if (res == NULL || irq < 0)
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001350 return -ENXIO;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001351
Chris Ball20920142011-03-22 16:34:41 -07001352 res = request_mem_region(res->start, resource_size(res),
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001353 pdev->name);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001354 if (res == NULL)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001355 return -EBUSY;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001356
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001357 host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
1358 if (host == NULL) {
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001359 ret = -ENOMEM;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001360 goto err_free_mem_region;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001361 }
1362
Jarkko Lavinen0f602ec2008-03-26 16:09:58 -04001363 INIT_WORK(&host->slot_release_work, mmc_omap_slot_release_work);
1364 INIT_WORK(&host->send_stop_work, mmc_omap_send_stop_work);
1365
Jarkko Lavinen0fb47232008-03-26 16:09:48 -04001366 INIT_WORK(&host->cmd_abort_work, mmc_omap_abort_command);
1367 setup_timer(&host->cmd_abort_timer, mmc_omap_cmd_timer,
1368 (unsigned long) host);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -04001369
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001370 spin_lock_init(&host->clk_lock);
1371 setup_timer(&host->clk_timer, mmc_omap_clk_timer, (unsigned long) host);
1372
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001373 spin_lock_init(&host->dma_lock);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001374 spin_lock_init(&host->slot_lock);
1375 init_waitqueue_head(&host->slot_wq);
1376
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001377 host->pdata = pdata;
Tony Lindgren53db20d2012-10-15 12:10:33 -07001378 host->features = host->pdata->slots[0].features;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001379 host->dev = &pdev->dev;
1380 platform_set_drvdata(pdev, host);
1381
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001382 host->id = pdev->id;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001383 host->mem_res = res;
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001384 host->irq = irq;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001385 host->use_dma = 1;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001386 host->irq = irq;
1387 host->phys_base = host->mem_res->start;
Chris Ball20920142011-03-22 16:34:41 -07001388 host->virt_base = ioremap(res->start, resource_size(res));
Russell King55c381e2008-09-04 14:07:22 +01001389 if (!host->virt_base)
1390 goto err_ioremap;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001391
Russell Kingd4a36645a2009-01-23 19:03:37 +00001392 host->iclk = clk_get(&pdev->dev, "ick");
Ladislav Michle799acb2009-12-14 18:01:24 -08001393 if (IS_ERR(host->iclk)) {
1394 ret = PTR_ERR(host->iclk);
Russell Kingd4a36645a2009-01-23 19:03:37 +00001395 goto err_free_mmc_host;
Ladislav Michle799acb2009-12-14 18:01:24 -08001396 }
Russell Kingd4a36645a2009-01-23 19:03:37 +00001397 clk_enable(host->iclk);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001398
Russell King5c9e02b2009-01-19 20:53:30 +00001399 host->fclk = clk_get(&pdev->dev, "fck");
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001400 if (IS_ERR(host->fclk)) {
1401 ret = PTR_ERR(host->fclk);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001402 goto err_free_iclk;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001403 }
1404
Russell King3451c062012-04-21 22:35:42 +01001405 dma_cap_zero(mask);
1406 dma_cap_set(DMA_SLAVE, mask);
1407
1408 host->dma_tx_burst = -1;
1409 host->dma_rx_burst = -1;
1410
Tony Lindgren53db20d2012-10-15 12:10:33 -07001411 if (mmc_omap2())
Russell King3451c062012-04-21 22:35:42 +01001412 sig = host->id == 0 ? OMAP24XX_DMA_MMC1_TX : OMAP24XX_DMA_MMC2_TX;
1413 else
1414 sig = host->id == 0 ? OMAP_DMA_MMC_TX : OMAP_DMA_MMC2_TX;
1415 host->dma_tx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
1416#if 0
1417 if (!host->dma_tx) {
1418 dev_err(host->dev, "unable to obtain TX DMA engine channel %u\n",
1419 sig);
1420 goto err_dma;
1421 }
1422#else
1423 if (!host->dma_tx)
1424 dev_warn(host->dev, "unable to obtain TX DMA engine channel %u\n",
1425 sig);
1426#endif
Tony Lindgren53db20d2012-10-15 12:10:33 -07001427 if (mmc_omap2())
Russell King3451c062012-04-21 22:35:42 +01001428 sig = host->id == 0 ? OMAP24XX_DMA_MMC1_RX : OMAP24XX_DMA_MMC2_RX;
1429 else
1430 sig = host->id == 0 ? OMAP_DMA_MMC_RX : OMAP_DMA_MMC2_RX;
1431 host->dma_rx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
1432#if 0
1433 if (!host->dma_rx) {
1434 dev_err(host->dev, "unable to obtain RX DMA engine channel %u\n",
1435 sig);
1436 goto err_dma;
1437 }
1438#else
1439 if (!host->dma_rx)
1440 dev_warn(host->dev, "unable to obtain RX DMA engine channel %u\n",
1441 sig);
1442#endif
1443
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001444 ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
1445 if (ret)
Russell King3451c062012-04-21 22:35:42 +01001446 goto err_free_dma;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001447
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001448 if (pdata->init != NULL) {
1449 ret = pdata->init(&pdev->dev);
1450 if (ret < 0)
1451 goto err_free_irq;
1452 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001453
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001454 host->nr_slots = pdata->nr_slots;
Tony Lindgren53db20d2012-10-15 12:10:33 -07001455 host->reg_shift = (mmc_omap7xx() ? 1 : 2);
Tony Lindgren3caf4142012-06-06 09:45:50 -04001456
1457 host->mmc_omap_wq = alloc_workqueue("mmc_omap", 0, 0);
1458 if (!host->mmc_omap_wq)
1459 goto err_plat_cleanup;
1460
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001461 for (i = 0; i < pdata->nr_slots; i++) {
1462 ret = mmc_omap_new_slot(host, i);
1463 if (ret < 0) {
1464 while (--i >= 0)
1465 mmc_omap_remove_slot(host->slots[i]);
1466
Tony Lindgren3caf4142012-06-06 09:45:50 -04001467 goto err_destroy_wq;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001468 }
1469 }
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001470
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001471 return 0;
1472
Tony Lindgren3caf4142012-06-06 09:45:50 -04001473err_destroy_wq:
1474 destroy_workqueue(host->mmc_omap_wq);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001475err_plat_cleanup:
1476 if (pdata->cleanup)
1477 pdata->cleanup(&pdev->dev);
1478err_free_irq:
1479 free_irq(host->irq, host);
Russell King3451c062012-04-21 22:35:42 +01001480err_free_dma:
1481 if (host->dma_tx)
1482 dma_release_channel(host->dma_tx);
1483 if (host->dma_rx)
1484 dma_release_channel(host->dma_rx);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001485 clk_put(host->fclk);
1486err_free_iclk:
Ladislav Michle799acb2009-12-14 18:01:24 -08001487 clk_disable(host->iclk);
1488 clk_put(host->iclk);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001489err_free_mmc_host:
Russell King55c381e2008-09-04 14:07:22 +01001490 iounmap(host->virt_base);
1491err_ioremap:
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001492 kfree(host);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001493err_free_mem_region:
Chris Ball20920142011-03-22 16:34:41 -07001494 release_mem_region(res->start, resource_size(res));
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001495 return ret;
1496}
1497
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001498static int mmc_omap_remove(struct platform_device *pdev)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001499{
1500 struct mmc_omap_host *host = platform_get_drvdata(pdev);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001501 int i;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001502
1503 platform_set_drvdata(pdev, NULL);
1504
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001505 BUG_ON(host == NULL);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001506
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001507 for (i = 0; i < host->nr_slots; i++)
1508 mmc_omap_remove_slot(host->slots[i]);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001509
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001510 if (host->pdata->cleanup)
1511 host->pdata->cleanup(&pdev->dev);
1512
Russell Kingd4a36645a2009-01-23 19:03:37 +00001513 mmc_omap_fclk_enable(host, 0);
Ladislav Michl49c1d9d2009-11-11 14:26:43 -08001514 free_irq(host->irq, host);
Russell Kingd4a36645a2009-01-23 19:03:37 +00001515 clk_put(host->fclk);
1516 clk_disable(host->iclk);
1517 clk_put(host->iclk);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001518
Russell King3451c062012-04-21 22:35:42 +01001519 if (host->dma_tx)
1520 dma_release_channel(host->dma_tx);
1521 if (host->dma_rx)
1522 dma_release_channel(host->dma_rx);
1523
Russell King55c381e2008-09-04 14:07:22 +01001524 iounmap(host->virt_base);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001525 release_mem_region(pdev->resource[0].start,
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001526 pdev->resource[0].end - pdev->resource[0].start + 1);
Venkatraman Sb01a4f12012-05-08 17:05:33 +05301527 destroy_workqueue(host->mmc_omap_wq);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001528
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001529 kfree(host);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001530
1531 return 0;
1532}
1533
1534#ifdef CONFIG_PM
1535static int mmc_omap_suspend(struct platform_device *pdev, pm_message_t mesg)
1536{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001537 int i, ret = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001538 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1539
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001540 if (host == NULL || host->suspended)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001541 return 0;
1542
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001543 for (i = 0; i < host->nr_slots; i++) {
1544 struct mmc_omap_slot *slot;
1545
1546 slot = host->slots[i];
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001547 ret = mmc_suspend_host(slot->mmc);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001548 if (ret < 0) {
1549 while (--i >= 0) {
1550 slot = host->slots[i];
1551 mmc_resume_host(slot->mmc);
1552 }
1553 return ret;
1554 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001555 }
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001556 host->suspended = 1;
1557 return 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001558}
1559
1560static int mmc_omap_resume(struct platform_device *pdev)
1561{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001562 int i, ret = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001563 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1564
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001565 if (host == NULL || !host->suspended)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001566 return 0;
1567
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001568 for (i = 0; i < host->nr_slots; i++) {
1569 struct mmc_omap_slot *slot;
1570 slot = host->slots[i];
1571 ret = mmc_resume_host(slot->mmc);
1572 if (ret < 0)
1573 return ret;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001574
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001575 host->suspended = 0;
1576 }
1577 return 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001578}
1579#else
1580#define mmc_omap_suspend NULL
1581#define mmc_omap_resume NULL
1582#endif
1583
1584static struct platform_driver mmc_omap_driver = {
Venkatraman Sb6e07032012-05-08 17:05:34 +05301585 .probe = mmc_omap_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001586 .remove = mmc_omap_remove,
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001587 .suspend = mmc_omap_suspend,
1588 .resume = mmc_omap_resume,
1589 .driver = {
1590 .name = DRIVER_NAME,
Kay Sieversbc65c722008-04-15 14:34:28 -07001591 .owner = THIS_MODULE,
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001592 },
1593};
1594
Venkatraman S680f1b52012-05-08 17:05:35 +05301595module_platform_driver(mmc_omap_driver);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001596MODULE_DESCRIPTION("OMAP Multimedia Card driver");
1597MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001598MODULE_ALIAS("platform:" DRIVER_NAME);
Al Virod36b6912011-12-29 17:09:01 -05001599MODULE_AUTHOR("Juha Yrjölä");