blob: 98b6b6ef7e5c9d737a749e9a3ddf631f5d92c1c0 [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>
Tony Lindgren9cb238c2013-11-26 15:50:33 -080025#include <linux/of.h>
Russell King3451c062012-04-21 22:35:42 +010026#include <linux/omap-dma.h>
Carlos Aguiar730c9b72006-03-29 09:21:00 +010027#include <linux/mmc/host.h>
Carlos Aguiar730c9b72006-03-29 09:21:00 +010028#include <linux/mmc/card.h>
29#include <linux/clk.h>
Jens Axboe45711f12007-10-22 21:19:53 +020030#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Tony Lindgren68f39e72012-10-15 12:09:43 -070032#include <linux/platform_data/mmc-omap.h>
Carlos Aguiar730c9b72006-03-29 09:21:00 +010033
Carlos Aguiar730c9b72006-03-29 09:21:00 +010034
Juha Yrjola juha.yrjola0551f4d2006-11-11 23:38:36 +010035#define OMAP_MMC_REG_CMD 0x00
Marek Belisko0e950fa62010-05-26 14:41:49 -070036#define OMAP_MMC_REG_ARGL 0x01
37#define OMAP_MMC_REG_ARGH 0x02
38#define OMAP_MMC_REG_CON 0x03
39#define OMAP_MMC_REG_STAT 0x04
40#define OMAP_MMC_REG_IE 0x05
41#define OMAP_MMC_REG_CTO 0x06
42#define OMAP_MMC_REG_DTO 0x07
43#define OMAP_MMC_REG_DATA 0x08
44#define OMAP_MMC_REG_BLEN 0x09
45#define OMAP_MMC_REG_NBLK 0x0a
46#define OMAP_MMC_REG_BUF 0x0b
47#define OMAP_MMC_REG_SDIO 0x0d
48#define OMAP_MMC_REG_REV 0x0f
49#define OMAP_MMC_REG_RSP0 0x10
50#define OMAP_MMC_REG_RSP1 0x11
51#define OMAP_MMC_REG_RSP2 0x12
52#define OMAP_MMC_REG_RSP3 0x13
53#define OMAP_MMC_REG_RSP4 0x14
54#define OMAP_MMC_REG_RSP5 0x15
55#define OMAP_MMC_REG_RSP6 0x16
56#define OMAP_MMC_REG_RSP7 0x17
57#define OMAP_MMC_REG_IOSR 0x18
58#define OMAP_MMC_REG_SYSC 0x19
59#define OMAP_MMC_REG_SYSS 0x1a
Juha Yrjola juha.yrjola0551f4d2006-11-11 23:38:36 +010060
61#define OMAP_MMC_STAT_CARD_ERR (1 << 14)
62#define OMAP_MMC_STAT_CARD_IRQ (1 << 13)
63#define OMAP_MMC_STAT_OCR_BUSY (1 << 12)
64#define OMAP_MMC_STAT_A_EMPTY (1 << 11)
65#define OMAP_MMC_STAT_A_FULL (1 << 10)
66#define OMAP_MMC_STAT_CMD_CRC (1 << 8)
67#define OMAP_MMC_STAT_CMD_TOUT (1 << 7)
68#define OMAP_MMC_STAT_DATA_CRC (1 << 6)
69#define OMAP_MMC_STAT_DATA_TOUT (1 << 5)
70#define OMAP_MMC_STAT_END_BUSY (1 << 4)
71#define OMAP_MMC_STAT_END_OF_DATA (1 << 3)
72#define OMAP_MMC_STAT_CARD_BUSY (1 << 2)
73#define OMAP_MMC_STAT_END_OF_CMD (1 << 0)
74
Tony Lindgren53db20d2012-10-15 12:10:33 -070075#define mmc_omap7xx() (host->features & MMC_OMAP7XX)
76#define mmc_omap15xx() (host->features & MMC_OMAP15XX)
77#define mmc_omap16xx() (host->features & MMC_OMAP16XX)
78#define MMC_OMAP1_MASK (MMC_OMAP7XX | MMC_OMAP15XX | MMC_OMAP16XX)
79#define mmc_omap1() (host->features & MMC_OMAP1_MASK)
80#define mmc_omap2() (!mmc_omap1())
81
Marek Belisko0e950fa62010-05-26 14:41:49 -070082#define OMAP_MMC_REG(host, reg) (OMAP_MMC_REG_##reg << (host)->reg_shift)
83#define OMAP_MMC_READ(host, reg) __raw_readw((host)->virt_base + OMAP_MMC_REG(host, reg))
84#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 +010085
86/*
87 * Command types
88 */
89#define OMAP_MMC_CMDTYPE_BC 0
90#define OMAP_MMC_CMDTYPE_BCR 1
91#define OMAP_MMC_CMDTYPE_AC 2
92#define OMAP_MMC_CMDTYPE_ADTC 3
93
Carlos Aguiar730c9b72006-03-29 09:21:00 +010094#define DRIVER_NAME "mmci-omap"
Carlos Aguiar730c9b72006-03-29 09:21:00 +010095
96/* Specifies how often in millisecs to poll for card status changes
97 * when the cover switch is open */
Jarkko Lavinen7584d272008-03-26 16:09:42 -040098#define OMAP_MMC_COVER_POLL_DELAY 500
Carlos Aguiar730c9b72006-03-29 09:21:00 +010099
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400100struct mmc_omap_host;
101
102struct mmc_omap_slot {
103 int id;
104 unsigned int vdd;
105 u16 saved_con;
106 u16 bus_mode;
107 unsigned int fclk_freq;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400108
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400109 struct tasklet_struct cover_tasklet;
110 struct timer_list cover_timer;
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400111 unsigned cover_open;
112
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400113 struct mmc_request *mrq;
114 struct mmc_omap_host *host;
115 struct mmc_host *mmc;
116 struct omap_mmc_slot_data *pdata;
117};
118
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100119struct mmc_omap_host {
120 int initialized;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100121 struct mmc_request * mrq;
122 struct mmc_command * cmd;
123 struct mmc_data * data;
124 struct mmc_host * mmc;
125 struct device * dev;
126 unsigned char id; /* 16xx chips have 2 MMC blocks */
127 struct clk * iclk;
128 struct clk * fclk;
Russell King3451c062012-04-21 22:35:42 +0100129 struct dma_chan *dma_rx;
130 u32 dma_rx_burst;
131 struct dma_chan *dma_tx;
132 u32 dma_tx_burst;
Juha Yrjola juha.yrjola89783b1e42006-11-11 23:36:01 +0100133 struct resource *mem_res;
134 void __iomem *virt_base;
135 unsigned int phys_base;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100136 int irq;
137 unsigned char bus_mode;
Marek Belisko0e950fa62010-05-26 14:41:49 -0700138 unsigned int reg_shift;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100139
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400140 struct work_struct cmd_abort_work;
141 unsigned abort:1;
142 struct timer_list cmd_abort_timer;
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400143
Jarkko Lavinen0f602ec2008-03-26 16:09:58 -0400144 struct work_struct slot_release_work;
145 struct mmc_omap_slot *next_slot;
146 struct work_struct send_stop_work;
147 struct mmc_data *stop_data;
148
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100149 unsigned int sg_len;
150 int sg_idx;
151 u16 * buffer;
152 u32 buffer_bytes_left;
153 u32 total_bytes_left;
154
Tony Lindgren53db20d2012-10-15 12:10:33 -0700155 unsigned features;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100156 unsigned use_dma:1;
157 unsigned brs_received:1, dma_done:1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100158 unsigned dma_in_use:1;
Russell King3451c062012-04-21 22:35:42 +0100159 spinlock_t dma_lock;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100160
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400161 struct mmc_omap_slot *slots[OMAP_MMC_MAX_SLOTS];
162 struct mmc_omap_slot *current_slot;
163 spinlock_t slot_lock;
164 wait_queue_head_t slot_wq;
165 int nr_slots;
166
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400167 struct timer_list clk_timer;
168 spinlock_t clk_lock; /* for changing enabled state */
169 unsigned int fclk_enabled:1;
Venkatraman Sb01a4f12012-05-08 17:05:33 +0530170 struct workqueue_struct *mmc_omap_wq;
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400171
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400172 struct omap_mmc_platform_data *pdata;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100173};
174
Tejun Heo0d9ee5b2010-12-24 16:00:17 +0100175
Russell King7c8ad982008-09-05 15:13:24 +0100176static void mmc_omap_fclk_offdelay(struct mmc_omap_slot *slot)
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400177{
178 unsigned long tick_ns;
179
180 if (slot != NULL && slot->host->fclk_enabled && slot->fclk_freq > 0) {
181 tick_ns = (1000000000 + slot->fclk_freq - 1) / slot->fclk_freq;
182 ndelay(8 * tick_ns);
183 }
184}
185
Russell King7c8ad982008-09-05 15:13:24 +0100186static void mmc_omap_fclk_enable(struct mmc_omap_host *host, unsigned int enable)
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400187{
188 unsigned long flags;
189
190 spin_lock_irqsave(&host->clk_lock, flags);
191 if (host->fclk_enabled != enable) {
192 host->fclk_enabled = enable;
193 if (enable)
194 clk_enable(host->fclk);
195 else
196 clk_disable(host->fclk);
197 }
198 spin_unlock_irqrestore(&host->clk_lock, flags);
199}
200
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400201static void mmc_omap_select_slot(struct mmc_omap_slot *slot, int claimed)
202{
203 struct mmc_omap_host *host = slot->host;
204 unsigned long flags;
205
206 if (claimed)
207 goto no_claim;
208 spin_lock_irqsave(&host->slot_lock, flags);
209 while (host->mmc != NULL) {
210 spin_unlock_irqrestore(&host->slot_lock, flags);
211 wait_event(host->slot_wq, host->mmc == NULL);
212 spin_lock_irqsave(&host->slot_lock, flags);
213 }
214 host->mmc = slot->mmc;
215 spin_unlock_irqrestore(&host->slot_lock, flags);
216no_claim:
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400217 del_timer(&host->clk_timer);
218 if (host->current_slot != slot || !claimed)
219 mmc_omap_fclk_offdelay(host->current_slot);
220
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400221 if (host->current_slot != slot) {
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400222 OMAP_MMC_WRITE(host, CON, slot->saved_con & 0xFC00);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400223 if (host->pdata->switch_slot != NULL)
224 host->pdata->switch_slot(mmc_dev(slot->mmc), slot->id);
225 host->current_slot = slot;
226 }
227
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400228 if (claimed) {
229 mmc_omap_fclk_enable(host, 1);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400230
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400231 /* Doing the dummy read here seems to work around some bug
232 * at least in OMAP24xx silicon where the command would not
233 * start after writing the CMD register. Sigh. */
234 OMAP_MMC_READ(host, CON);
235
236 OMAP_MMC_WRITE(host, CON, slot->saved_con);
237 } else
238 mmc_omap_fclk_enable(host, 0);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400239}
240
241static void mmc_omap_start_request(struct mmc_omap_host *host,
242 struct mmc_request *req);
243
Jarkko Lavinen0f602ec2008-03-26 16:09:58 -0400244static void mmc_omap_slot_release_work(struct work_struct *work)
245{
246 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
247 slot_release_work);
248 struct mmc_omap_slot *next_slot = host->next_slot;
249 struct mmc_request *rq;
250
251 host->next_slot = NULL;
252 mmc_omap_select_slot(next_slot, 1);
253
254 rq = next_slot->mrq;
255 next_slot->mrq = NULL;
256 mmc_omap_start_request(host, rq);
257}
258
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400259static void mmc_omap_release_slot(struct mmc_omap_slot *slot, int clk_enabled)
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400260{
261 struct mmc_omap_host *host = slot->host;
262 unsigned long flags;
263 int i;
264
265 BUG_ON(slot == NULL || host->mmc == NULL);
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400266
267 if (clk_enabled)
268 /* Keeps clock running for at least 8 cycles on valid freq */
269 mod_timer(&host->clk_timer, jiffies + HZ/10);
270 else {
271 del_timer(&host->clk_timer);
272 mmc_omap_fclk_offdelay(slot);
273 mmc_omap_fclk_enable(host, 0);
274 }
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400275
276 spin_lock_irqsave(&host->slot_lock, flags);
277 /* Check for any pending requests */
278 for (i = 0; i < host->nr_slots; i++) {
279 struct mmc_omap_slot *new_slot;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400280
281 if (host->slots[i] == NULL || host->slots[i]->mrq == NULL)
282 continue;
283
Jarkko Lavinen0f602ec2008-03-26 16:09:58 -0400284 BUG_ON(host->next_slot != NULL);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400285 new_slot = host->slots[i];
286 /* The current slot should not have a request in queue */
287 BUG_ON(new_slot == host->current_slot);
288
Jarkko Lavinen0f602ec2008-03-26 16:09:58 -0400289 host->next_slot = new_slot;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400290 host->mmc = new_slot->mmc;
291 spin_unlock_irqrestore(&host->slot_lock, flags);
Venkatraman Sb01a4f12012-05-08 17:05:33 +0530292 queue_work(host->mmc_omap_wq, &host->slot_release_work);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400293 return;
294 }
295
296 host->mmc = NULL;
297 wake_up(&host->slot_wq);
298 spin_unlock_irqrestore(&host->slot_lock, flags);
299}
300
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400301static inline
302int mmc_omap_cover_is_open(struct mmc_omap_slot *slot)
303{
Kyungmin Park8348f002008-03-26 16:09:38 -0400304 if (slot->pdata->get_cover_state)
305 return slot->pdata->get_cover_state(mmc_dev(slot->mmc),
306 slot->id);
307 return 0;
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400308}
309
310static ssize_t
311mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
312 char *buf)
313{
314 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
315 struct mmc_omap_slot *slot = mmc_priv(mmc);
316
317 return sprintf(buf, "%s\n", mmc_omap_cover_is_open(slot) ? "open" :
318 "closed");
319}
320
321static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
322
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400323static ssize_t
324mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
325 char *buf)
326{
327 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
328 struct mmc_omap_slot *slot = mmc_priv(mmc);
329
330 return sprintf(buf, "%s\n", slot->pdata->name);
331}
332
333static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
334
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100335static void
336mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd)
337{
338 u32 cmdreg;
339 u32 resptype;
340 u32 cmdtype;
341
342 host->cmd = cmd;
343
344 resptype = 0;
345 cmdtype = 0;
346
347 /* Our hardware needs to know exact type */
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100348 switch (mmc_resp_type(cmd)) {
349 case MMC_RSP_NONE:
350 break;
351 case MMC_RSP_R1:
352 case MMC_RSP_R1B:
Philip Langdale6f949902007-01-04 07:04:47 -0800353 /* resp 1, 1b, 6, 7 */
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100354 resptype = 1;
355 break;
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100356 case MMC_RSP_R2:
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100357 resptype = 2;
358 break;
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100359 case MMC_RSP_R3:
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100360 resptype = 3;
361 break;
362 default:
Carlos Eduardo Aguiar1b3b2632007-01-15 06:38:15 +0100363 dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100364 break;
365 }
366
367 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
368 cmdtype = OMAP_MMC_CMDTYPE_ADTC;
369 } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
370 cmdtype = OMAP_MMC_CMDTYPE_BC;
371 } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
372 cmdtype = OMAP_MMC_CMDTYPE_BCR;
373 } else {
374 cmdtype = OMAP_MMC_CMDTYPE_AC;
375 }
376
377 cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
378
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400379 if (host->current_slot->bus_mode == MMC_BUSMODE_OPENDRAIN)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100380 cmdreg |= 1 << 6;
381
382 if (cmd->flags & MMC_RSP_BUSY)
383 cmdreg |= 1 << 11;
384
385 if (host->data && !(host->data->flags & MMC_DATA_WRITE))
386 cmdreg |= 1 << 15;
387
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400388 mod_timer(&host->cmd_abort_timer, jiffies + HZ/2);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400389
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100390 OMAP_MMC_WRITE(host, CTO, 200);
391 OMAP_MMC_WRITE(host, ARGL, cmd->arg & 0xffff);
392 OMAP_MMC_WRITE(host, ARGH, cmd->arg >> 16);
393 OMAP_MMC_WRITE(host, IE,
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100394 OMAP_MMC_STAT_A_EMPTY | OMAP_MMC_STAT_A_FULL |
395 OMAP_MMC_STAT_CMD_CRC | OMAP_MMC_STAT_CMD_TOUT |
396 OMAP_MMC_STAT_DATA_CRC | OMAP_MMC_STAT_DATA_TOUT |
397 OMAP_MMC_STAT_END_OF_CMD | OMAP_MMC_STAT_CARD_ERR |
398 OMAP_MMC_STAT_END_OF_DATA);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100399 OMAP_MMC_WRITE(host, CMD, cmdreg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100400}
401
402static void
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400403mmc_omap_release_dma(struct mmc_omap_host *host, struct mmc_data *data,
404 int abort)
405{
406 enum dma_data_direction dma_data_dir;
Russell King3451c062012-04-21 22:35:42 +0100407 struct device *dev = mmc_dev(host->mmc);
408 struct dma_chan *c;
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400409
Russell King3451c062012-04-21 22:35:42 +0100410 if (data->flags & MMC_DATA_WRITE) {
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400411 dma_data_dir = DMA_TO_DEVICE;
Russell King3451c062012-04-21 22:35:42 +0100412 c = host->dma_tx;
413 } else {
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400414 dma_data_dir = DMA_FROM_DEVICE;
Russell King3451c062012-04-21 22:35:42 +0100415 c = host->dma_rx;
416 }
417 if (c) {
418 if (data->error) {
419 dmaengine_terminate_all(c);
420 /* Claim nothing transferred on error... */
421 data->bytes_xfered = 0;
422 }
423 dev = c->device->dev;
424 }
425 dma_unmap_sg(dev, data->sg, host->sg_len, dma_data_dir);
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400426}
427
Jarkko Lavinen0f602ec2008-03-26 16:09:58 -0400428static void mmc_omap_send_stop_work(struct work_struct *work)
429{
430 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
431 send_stop_work);
432 struct mmc_omap_slot *slot = host->current_slot;
433 struct mmc_data *data = host->stop_data;
434 unsigned long tick_ns;
435
436 tick_ns = (1000000000 + slot->fclk_freq - 1)/slot->fclk_freq;
437 ndelay(8*tick_ns);
438
439 mmc_omap_start_command(host, data->stop);
440}
441
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400442static void
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100443mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
444{
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400445 if (host->dma_in_use)
446 mmc_omap_release_dma(host, data, data->error);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100447
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100448 host->data = NULL;
449 host->sg_len = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100450
451 /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
452 * dozens of requests until the card finishes writing data.
453 * It'd be cheaper to just wait till an EOFB interrupt arrives...
454 */
455
456 if (!data->stop) {
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400457 struct mmc_host *mmc;
458
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100459 host->mrq = NULL;
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400460 mmc = host->mmc;
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400461 mmc_omap_release_slot(host->current_slot, 1);
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400462 mmc_request_done(mmc, data->mrq);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100463 return;
464 }
465
Jarkko Lavinen0f602ec2008-03-26 16:09:58 -0400466 host->stop_data = data;
Venkatraman Sb01a4f12012-05-08 17:05:33 +0530467 queue_work(host->mmc_omap_wq, &host->send_stop_work);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100468}
469
470static void
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400471mmc_omap_send_abort(struct mmc_omap_host *host, int maxloops)
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400472{
473 struct mmc_omap_slot *slot = host->current_slot;
474 unsigned int restarts, passes, timeout;
475 u16 stat = 0;
476
477 /* Sending abort takes 80 clocks. Have some extra and round up */
478 timeout = (120*1000000 + slot->fclk_freq - 1)/slot->fclk_freq;
479 restarts = 0;
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400480 while (restarts < maxloops) {
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400481 OMAP_MMC_WRITE(host, STAT, 0xFFFF);
482 OMAP_MMC_WRITE(host, CMD, (3 << 12) | (1 << 7));
483
484 passes = 0;
485 while (passes < timeout) {
486 stat = OMAP_MMC_READ(host, STAT);
487 if (stat & OMAP_MMC_STAT_END_OF_CMD)
488 goto out;
489 udelay(1);
490 passes++;
491 }
492
493 restarts++;
494 }
495out:
496 OMAP_MMC_WRITE(host, STAT, stat);
497}
498
499static void
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400500mmc_omap_abort_xfer(struct mmc_omap_host *host, struct mmc_data *data)
501{
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400502 if (host->dma_in_use)
503 mmc_omap_release_dma(host, data, 1);
504
505 host->data = NULL;
506 host->sg_len = 0;
507
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400508 mmc_omap_send_abort(host, 10000);
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400509}
510
511static void
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100512mmc_omap_end_of_data(struct mmc_omap_host *host, struct mmc_data *data)
513{
514 unsigned long flags;
515 int done;
516
517 if (!host->dma_in_use) {
518 mmc_omap_xfer_done(host, data);
519 return;
520 }
521 done = 0;
522 spin_lock_irqsave(&host->dma_lock, flags);
523 if (host->dma_done)
524 done = 1;
525 else
526 host->brs_received = 1;
527 spin_unlock_irqrestore(&host->dma_lock, flags);
528 if (done)
529 mmc_omap_xfer_done(host, data);
530}
531
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100532static void
533mmc_omap_dma_done(struct mmc_omap_host *host, struct mmc_data *data)
534{
535 unsigned long flags;
536 int done;
537
538 done = 0;
539 spin_lock_irqsave(&host->dma_lock, flags);
540 if (host->brs_received)
541 done = 1;
542 else
543 host->dma_done = 1;
544 spin_unlock_irqrestore(&host->dma_lock, flags);
545 if (done)
546 mmc_omap_xfer_done(host, data);
547}
548
549static void
550mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
551{
552 host->cmd = NULL;
553
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400554 del_timer(&host->cmd_abort_timer);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400555
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100556 if (cmd->flags & MMC_RSP_PRESENT) {
557 if (cmd->flags & MMC_RSP_136) {
558 /* response type 2 */
559 cmd->resp[3] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100560 OMAP_MMC_READ(host, RSP0) |
561 (OMAP_MMC_READ(host, RSP1) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100562 cmd->resp[2] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100563 OMAP_MMC_READ(host, RSP2) |
564 (OMAP_MMC_READ(host, RSP3) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100565 cmd->resp[1] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100566 OMAP_MMC_READ(host, RSP4) |
567 (OMAP_MMC_READ(host, RSP5) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100568 cmd->resp[0] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100569 OMAP_MMC_READ(host, RSP6) |
570 (OMAP_MMC_READ(host, RSP7) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100571 } else {
572 /* response types 1, 1b, 3, 4, 5, 6 */
573 cmd->resp[0] =
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100574 OMAP_MMC_READ(host, RSP6) |
575 (OMAP_MMC_READ(host, RSP7) << 16);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100576 }
577 }
578
Pierre Ossman17b04292007-07-22 22:18:46 +0200579 if (host->data == NULL || cmd->error) {
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400580 struct mmc_host *mmc;
581
582 if (host->data != NULL)
583 mmc_omap_abort_xfer(host, host->data);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100584 host->mrq = NULL;
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400585 mmc = host->mmc;
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400586 mmc_omap_release_slot(host->current_slot, 1);
Juha Yrjolaa914ded2008-03-26 16:09:12 -0400587 mmc_request_done(mmc, cmd->mrq);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100588 }
589}
590
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400591/*
592 * Abort stuck command. Can occur when card is removed while it is being
593 * read.
594 */
595static void mmc_omap_abort_command(struct work_struct *work)
596{
597 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400598 cmd_abort_work);
599 BUG_ON(!host->cmd);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400600
601 dev_dbg(mmc_dev(host->mmc), "Aborting stuck command CMD%d\n",
602 host->cmd->opcode);
603
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400604 if (host->cmd->error == 0)
605 host->cmd->error = -ETIMEDOUT;
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400606
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400607 if (host->data == NULL) {
608 struct mmc_command *cmd;
609 struct mmc_host *mmc;
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400610
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400611 cmd = host->cmd;
612 host->cmd = NULL;
613 mmc_omap_send_abort(host, 10000);
614
615 host->mrq = NULL;
616 mmc = host->mmc;
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400617 mmc_omap_release_slot(host->current_slot, 1);
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400618 mmc_request_done(mmc, cmd->mrq);
619 } else
620 mmc_omap_cmd_done(host, host->cmd);
621
622 host->abort = 0;
623 enable_irq(host->irq);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400624}
625
626static void
627mmc_omap_cmd_timer(unsigned long data)
628{
629 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400630 unsigned long flags;
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400631
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400632 spin_lock_irqsave(&host->slot_lock, flags);
633 if (host->cmd != NULL && !host->abort) {
634 OMAP_MMC_WRITE(host, IE, 0);
635 disable_irq(host->irq);
636 host->abort = 1;
Venkatraman Sb01a4f12012-05-08 17:05:33 +0530637 queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400638 }
639 spin_unlock_irqrestore(&host->slot_lock, flags);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -0400640}
641
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100642/* PIO only */
643static void
644mmc_omap_sg_to_buf(struct mmc_omap_host *host)
645{
646 struct scatterlist *sg;
647
648 sg = host->data->sg + host->sg_idx;
649 host->buffer_bytes_left = sg->length;
Jens Axboe45711f12007-10-22 21:19:53 +0200650 host->buffer = sg_virt(sg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100651 if (host->buffer_bytes_left > host->total_bytes_left)
652 host->buffer_bytes_left = host->total_bytes_left;
653}
654
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -0400655static void
656mmc_omap_clk_timer(unsigned long data)
657{
658 struct mmc_omap_host *host = (struct mmc_omap_host *) data;
659
660 mmc_omap_fclk_enable(host, 0);
661}
662
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100663/* PIO only */
664static void
665mmc_omap_xfer_data(struct mmc_omap_host *host, int write)
666{
Paul Walmsley75b53ae2012-08-24 06:00:18 +0000667 int n, nwords;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100668
669 if (host->buffer_bytes_left == 0) {
670 host->sg_idx++;
671 BUG_ON(host->sg_idx == host->sg_len);
672 mmc_omap_sg_to_buf(host);
673 }
674 n = 64;
675 if (n > host->buffer_bytes_left)
676 n = host->buffer_bytes_left;
Paul Walmsley75b53ae2012-08-24 06:00:18 +0000677
678 nwords = n / 2;
679 nwords += n & 1; /* handle odd number of bytes to transfer */
680
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100681 host->buffer_bytes_left -= n;
682 host->total_bytes_left -= n;
683 host->data->bytes_xfered += n;
684
685 if (write) {
Paul Walmsley75b53ae2012-08-24 06:00:18 +0000686 __raw_writesw(host->virt_base + OMAP_MMC_REG(host, DATA),
687 host->buffer, nwords);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100688 } else {
Paul Walmsley75b53ae2012-08-24 06:00:18 +0000689 __raw_readsw(host->virt_base + OMAP_MMC_REG(host, DATA),
690 host->buffer, nwords);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100691 }
Paul Walmsley75b53ae2012-08-24 06:00:18 +0000692
693 host->buffer += nwords;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100694}
695
Venkatraman S75d569d2012-08-07 19:03:01 +0530696#ifdef CONFIG_MMC_DEBUG
697static void mmc_omap_report_irq(struct mmc_omap_host *host, u16 status)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100698{
699 static const char *mmc_omap_status_bits[] = {
700 "EOC", "CD", "CB", "BRS", "EOFB", "DTO", "DCRC", "CTO",
701 "CCRC", "CRW", "AF", "AE", "OCRB", "CIRQ", "CERR"
702 };
Venkatraman S75d569d2012-08-07 19:03:01 +0530703 int i;
704 char res[64], *buf = res;
705
706 buf += sprintf(buf, "MMC IRQ 0x%x:", status);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100707
708 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
Venkatraman S75d569d2012-08-07 19:03:01 +0530709 if (status & (1 << i))
710 buf += sprintf(buf, " %s", mmc_omap_status_bits[i]);
711 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100712}
Venkatraman S75d569d2012-08-07 19:03:01 +0530713#else
714static void mmc_omap_report_irq(struct mmc_omap_host *host, u16 status)
715{
716}
717#endif
718
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100719
David Howells7d12e782006-10-05 14:55:46 +0100720static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100721{
722 struct mmc_omap_host * host = (struct mmc_omap_host *)dev_id;
723 u16 status;
724 int end_command;
725 int end_transfer;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400726 int transfer_error, cmd_error;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100727
728 if (host->cmd == NULL && host->data == NULL) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100729 status = OMAP_MMC_READ(host, STAT);
Juha Yrjola2a50b882008-03-26 16:09:26 -0400730 dev_info(mmc_dev(host->slots[0]->mmc),
731 "Spurious IRQ 0x%04x\n", status);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100732 if (status != 0) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100733 OMAP_MMC_WRITE(host, STAT, status);
734 OMAP_MMC_WRITE(host, IE, 0);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100735 }
736 return IRQ_HANDLED;
737 }
738
739 end_command = 0;
740 end_transfer = 0;
741 transfer_error = 0;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400742 cmd_error = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100743
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100744 while ((status = OMAP_MMC_READ(host, STAT)) != 0) {
Juha Yrjola2a50b882008-03-26 16:09:26 -0400745 int cmd;
746
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100747 OMAP_MMC_WRITE(host, STAT, status);
Juha Yrjola2a50b882008-03-26 16:09:26 -0400748 if (host->cmd != NULL)
749 cmd = host->cmd->opcode;
750 else
751 cmd = -1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100752 dev_dbg(mmc_dev(host->mmc), "MMC IRQ %04x (CMD %d): ",
Juha Yrjola2a50b882008-03-26 16:09:26 -0400753 status, cmd);
Venkatraman S75d569d2012-08-07 19:03:01 +0530754 mmc_omap_report_irq(host, status);
755
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100756 if (host->total_bytes_left) {
757 if ((status & OMAP_MMC_STAT_A_FULL) ||
758 (status & OMAP_MMC_STAT_END_OF_DATA))
759 mmc_omap_xfer_data(host, 0);
760 if (status & OMAP_MMC_STAT_A_EMPTY)
761 mmc_omap_xfer_data(host, 1);
762 }
763
Juha Yrjola2a50b882008-03-26 16:09:26 -0400764 if (status & OMAP_MMC_STAT_END_OF_DATA)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100765 end_transfer = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100766
767 if (status & OMAP_MMC_STAT_DATA_TOUT) {
Juha Yrjola2a50b882008-03-26 16:09:26 -0400768 dev_dbg(mmc_dev(host->mmc), "data timeout (CMD%d)\n",
769 cmd);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100770 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200771 host->data->error = -ETIMEDOUT;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100772 transfer_error = 1;
773 }
774 }
775
776 if (status & OMAP_MMC_STAT_DATA_CRC) {
777 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200778 host->data->error = -EILSEQ;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100779 dev_dbg(mmc_dev(host->mmc),
780 "data CRC error, bytes left %d\n",
781 host->total_bytes_left);
782 transfer_error = 1;
783 } else {
784 dev_dbg(mmc_dev(host->mmc), "data CRC error\n");
785 }
786 }
787
788 if (status & OMAP_MMC_STAT_CMD_TOUT) {
789 /* Timeouts are routine with some commands */
790 if (host->cmd) {
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -0400791 struct mmc_omap_slot *slot =
792 host->current_slot;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400793 if (slot == NULL ||
794 !mmc_omap_cover_is_open(slot))
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400795 dev_err(mmc_dev(host->mmc),
Juha Yrjola2a50b882008-03-26 16:09:26 -0400796 "command timeout (CMD%d)\n",
797 cmd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200798 host->cmd->error = -ETIMEDOUT;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100799 end_command = 1;
Juha Yrjola2a50b882008-03-26 16:09:26 -0400800 cmd_error = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100801 }
802 }
803
804 if (status & OMAP_MMC_STAT_CMD_CRC) {
805 if (host->cmd) {
806 dev_err(mmc_dev(host->mmc),
807 "command CRC error (CMD%d, arg 0x%08x)\n",
Juha Yrjola2a50b882008-03-26 16:09:26 -0400808 cmd, host->cmd->arg);
Pierre Ossman17b04292007-07-22 22:18:46 +0200809 host->cmd->error = -EILSEQ;
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 } else
813 dev_err(mmc_dev(host->mmc),
814 "command CRC error without cmd?\n");
815 }
816
817 if (status & OMAP_MMC_STAT_CARD_ERR) {
Ragner Magalhaes0107a4b2007-06-13 19:09:28 +0200818 dev_dbg(mmc_dev(host->mmc),
819 "ignoring card status error (CMD%d)\n",
Juha Yrjola2a50b882008-03-26 16:09:26 -0400820 cmd);
Ragner Magalhaes0107a4b2007-06-13 19:09:28 +0200821 end_command = 1;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100822 }
823
824 /*
825 * NOTE: On 1610 the END_OF_CMD may come too early when
Juha Yrjola2a50b882008-03-26 16:09:26 -0400826 * starting a write
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100827 */
828 if ((status & OMAP_MMC_STAT_END_OF_CMD) &&
829 (!(status & OMAP_MMC_STAT_A_EMPTY))) {
830 end_command = 1;
831 }
832 }
833
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400834 if (cmd_error && host->data) {
835 del_timer(&host->cmd_abort_timer);
836 host->abort = 1;
837 OMAP_MMC_WRITE(host, IE, 0);
Ben Nizettee749c6f2009-04-16 15:55:21 +1000838 disable_irq_nosync(host->irq);
Venkatraman Sb01a4f12012-05-08 17:05:33 +0530839 queue_work(host->mmc_omap_wq, &host->cmd_abort_work);
Jarkko Lavinen0fb47232008-03-26 16:09:48 -0400840 return IRQ_HANDLED;
841 }
842
Michael Bueschf6947512011-04-11 17:00:44 -0400843 if (end_command && host->cmd)
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100844 mmc_omap_cmd_done(host, host->cmd);
Juha Yrjola2a50b882008-03-26 16:09:26 -0400845 if (host->data != NULL) {
846 if (transfer_error)
847 mmc_omap_xfer_done(host, host->data);
848 else if (end_transfer)
849 mmc_omap_end_of_data(host, host->data);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100850 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100851
852 return IRQ_HANDLED;
853}
854
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400855void omap_mmc_notify_cover_event(struct device *dev, int num, int is_closed)
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400856{
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400857 int cover_open;
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400858 struct mmc_omap_host *host = dev_get_drvdata(dev);
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400859 struct mmc_omap_slot *slot = host->slots[num];
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400860
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400861 BUG_ON(num >= host->nr_slots);
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400862
863 /* Other subsystems can call in here before we're initialised. */
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400864 if (host->nr_slots == 0 || !host->slots[num])
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400865 return;
866
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400867 cover_open = mmc_omap_cover_is_open(slot);
868 if (cover_open != slot->cover_open) {
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400869 slot->cover_open = cover_open;
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400870 sysfs_notify(&slot->mmc->class_dev.kobj, NULL, "cover_switch");
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400871 }
Jarkko Lavinen7584d272008-03-26 16:09:42 -0400872
873 tasklet_hi_schedule(&slot->cover_tasklet);
874}
875
876static void mmc_omap_cover_timer(unsigned long arg)
877{
878 struct mmc_omap_slot *slot = (struct mmc_omap_slot *) arg;
879 tasklet_schedule(&slot->cover_tasklet);
880}
881
882static void mmc_omap_cover_handler(unsigned long param)
883{
884 struct mmc_omap_slot *slot = (struct mmc_omap_slot *)param;
885 int cover_open = mmc_omap_cover_is_open(slot);
886
887 mmc_detect_change(slot->mmc, 0);
888 if (!cover_open)
889 return;
890
891 /*
892 * If no card is inserted, we postpone polling until
893 * the cover has been closed.
894 */
895 if (slot->mmc->card == NULL || !mmc_card_present(slot->mmc->card))
896 return;
897
898 mod_timer(&slot->cover_timer,
899 jiffies + msecs_to_jiffies(OMAP_MMC_COVER_POLL_DELAY));
Juha Yrjola5a0f3f12008-03-26 16:09:08 -0400900}
901
Russell King3451c062012-04-21 22:35:42 +0100902static void mmc_omap_dma_callback(void *priv)
903{
904 struct mmc_omap_host *host = priv;
905 struct mmc_data *data = host->data;
906
907 /* If we got to the end of DMA, assume everything went well */
908 data->bytes_xfered += data->blocks * data->blksz;
909
910 mmc_omap_dma_done(host, data);
911}
912
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100913static inline void set_cmd_timeout(struct mmc_omap_host *host, struct mmc_request *req)
914{
915 u16 reg;
916
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100917 reg = OMAP_MMC_READ(host, SDIO);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100918 reg &= ~(1 << 5);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100919 OMAP_MMC_WRITE(host, SDIO, reg);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100920 /* Set maximum timeout */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100921 OMAP_MMC_WRITE(host, CTO, 0xff);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100922}
923
924static inline void set_data_timeout(struct mmc_omap_host *host, struct mmc_request *req)
925{
Juha Yrjolab8f9f0e2008-03-26 16:09:16 -0400926 unsigned int timeout, cycle_ns;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100927 u16 reg;
928
Juha Yrjolab8f9f0e2008-03-26 16:09:16 -0400929 cycle_ns = 1000000000 / host->current_slot->fclk_freq;
930 timeout = req->data->timeout_ns / cycle_ns;
931 timeout += req->data->timeout_clks;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100932
933 /* Check if we need to use timeout multiplier register */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100934 reg = OMAP_MMC_READ(host, SDIO);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100935 if (timeout > 0xffff) {
936 reg |= (1 << 5);
937 timeout /= 1024;
938 } else
939 reg &= ~(1 << 5);
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100940 OMAP_MMC_WRITE(host, SDIO, reg);
941 OMAP_MMC_WRITE(host, DTO, timeout);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100942}
943
944static void
945mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
946{
947 struct mmc_data *data = req->data;
948 int i, use_dma, block_size;
949 unsigned sg_len;
950
951 host->data = data;
952 if (data == NULL) {
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100953 OMAP_MMC_WRITE(host, BLEN, 0);
954 OMAP_MMC_WRITE(host, NBLK, 0);
955 OMAP_MMC_WRITE(host, BUF, 0);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100956 host->dma_in_use = 0;
957 set_cmd_timeout(host, req);
958 return;
959 }
960
Russell Kinga3fd4a12006-06-04 17:51:15 +0100961 block_size = data->blksz;
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100962
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +0100963 OMAP_MMC_WRITE(host, NBLK, data->blocks - 1);
964 OMAP_MMC_WRITE(host, BLEN, block_size - 1);
Carlos Aguiar730c9b72006-03-29 09:21:00 +0100965 set_data_timeout(host, req);
966
967 /* cope with calling layer confusion; it issues "single
968 * block" writes using multi-block scatterlists.
969 */
970 sg_len = (data->blocks == 1) ? 1 : data->sg_len;
971
972 /* Only do DMA for entire blocks */
973 use_dma = host->use_dma;
974 if (use_dma) {
975 for (i = 0; i < sg_len; i++) {
976 if ((data->sg[i].length % block_size) != 0) {
977 use_dma = 0;
978 break;
979 }
980 }
981 }
982
983 host->sg_idx = 0;
984 if (use_dma) {
Russell King3451c062012-04-21 22:35:42 +0100985 enum dma_data_direction dma_data_dir;
986 struct dma_async_tx_descriptor *tx;
987 struct dma_chan *c;
988 u32 burst, *bp;
989 u16 buf;
990
991 /*
992 * FIFO is 16x2 bytes on 15xx, and 32x2 bytes on 16xx
993 * and 24xx. Use 16 or 32 word frames when the
994 * blocksize is at least that large. Blocksize is
995 * usually 512 bytes; but not for some SD reads.
996 */
Tony Lindgren53db20d2012-10-15 12:10:33 -0700997 burst = mmc_omap15xx() ? 32 : 64;
Russell King3451c062012-04-21 22:35:42 +0100998 if (burst > data->blksz)
999 burst = data->blksz;
1000
1001 burst >>= 1;
1002
1003 if (data->flags & MMC_DATA_WRITE) {
1004 c = host->dma_tx;
1005 bp = &host->dma_tx_burst;
1006 buf = 0x0f80 | (burst - 1) << 0;
1007 dma_data_dir = DMA_TO_DEVICE;
1008 } else {
1009 c = host->dma_rx;
1010 bp = &host->dma_rx_burst;
1011 buf = 0x800f | (burst - 1) << 8;
1012 dma_data_dir = DMA_FROM_DEVICE;
1013 }
1014
1015 if (!c)
1016 goto use_pio;
1017
1018 /* Only reconfigure if we have a different burst size */
1019 if (*bp != burst) {
1020 struct dma_slave_config cfg;
1021
1022 cfg.src_addr = host->phys_base + OMAP_MMC_REG(host, DATA);
1023 cfg.dst_addr = host->phys_base + OMAP_MMC_REG(host, DATA);
1024 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1025 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
1026 cfg.src_maxburst = burst;
1027 cfg.dst_maxburst = burst;
1028
1029 if (dmaengine_slave_config(c, &cfg))
1030 goto use_pio;
1031
1032 *bp = burst;
1033 }
1034
1035 host->sg_len = dma_map_sg(c->device->dev, data->sg, sg_len,
1036 dma_data_dir);
1037 if (host->sg_len == 0)
1038 goto use_pio;
1039
1040 tx = dmaengine_prep_slave_sg(c, data->sg, host->sg_len,
1041 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1042 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1043 if (!tx)
1044 goto use_pio;
1045
1046 OMAP_MMC_WRITE(host, BUF, buf);
1047
1048 tx->callback = mmc_omap_dma_callback;
1049 tx->callback_param = host;
1050 dmaengine_submit(tx);
1051 host->brs_received = 0;
1052 host->dma_done = 0;
1053 host->dma_in_use = 1;
1054 return;
1055 }
1056 use_pio:
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001057
1058 /* Revert to PIO? */
Russell King4e078fb2012-04-21 22:41:10 +01001059 OMAP_MMC_WRITE(host, BUF, 0x1f1f);
1060 host->total_bytes_left = data->blocks * block_size;
1061 host->sg_len = sg_len;
1062 mmc_omap_sg_to_buf(host);
1063 host->dma_in_use = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001064}
1065
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001066static void mmc_omap_start_request(struct mmc_omap_host *host,
1067 struct mmc_request *req)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001068{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001069 BUG_ON(host->mrq != NULL);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001070
1071 host->mrq = req;
1072
1073 /* only touch fifo AFTER the controller readies it */
1074 mmc_omap_prepare_data(host, req);
1075 mmc_omap_start_command(host, req->cmd);
Russell King3451c062012-04-21 22:35:42 +01001076 if (host->dma_in_use) {
1077 struct dma_chan *c = host->data->flags & MMC_DATA_WRITE ?
1078 host->dma_tx : host->dma_rx;
1079
Russell King4e078fb2012-04-21 22:41:10 +01001080 dma_async_issue_pending(c);
Russell King3451c062012-04-21 22:35:42 +01001081 }
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001082}
1083
1084static void mmc_omap_request(struct mmc_host *mmc, struct mmc_request *req)
1085{
1086 struct mmc_omap_slot *slot = mmc_priv(mmc);
1087 struct mmc_omap_host *host = slot->host;
1088 unsigned long flags;
1089
1090 spin_lock_irqsave(&host->slot_lock, flags);
1091 if (host->mmc != NULL) {
1092 BUG_ON(slot->mrq != NULL);
1093 slot->mrq = req;
1094 spin_unlock_irqrestore(&host->slot_lock, flags);
1095 return;
1096 } else
1097 host->mmc = mmc;
1098 spin_unlock_irqrestore(&host->slot_lock, flags);
1099 mmc_omap_select_slot(slot, 1);
1100 mmc_omap_start_request(host, req);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001101}
1102
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001103static void mmc_omap_set_power(struct mmc_omap_slot *slot, int power_on,
1104 int vdd)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001105{
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001106 struct mmc_omap_host *host;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001107
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001108 host = slot->host;
1109
1110 if (slot->pdata->set_power != NULL)
1111 slot->pdata->set_power(mmc_dev(slot->mmc), slot->id, power_on,
1112 vdd);
Tony Lindgren53db20d2012-10-15 12:10:33 -07001113 if (mmc_omap2()) {
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001114 u16 w;
1115
1116 if (power_on) {
1117 w = OMAP_MMC_READ(host, CON);
1118 OMAP_MMC_WRITE(host, CON, w | (1 << 11));
1119 } else {
1120 w = OMAP_MMC_READ(host, CON);
1121 OMAP_MMC_WRITE(host, CON, w & ~(1 << 11));
1122 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001123 }
1124}
1125
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001126static int mmc_omap_calc_divisor(struct mmc_host *mmc, struct mmc_ios *ios)
1127{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001128 struct mmc_omap_slot *slot = mmc_priv(mmc);
1129 struct mmc_omap_host *host = slot->host;
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001130 int func_clk_rate = clk_get_rate(host->fclk);
1131 int dsor;
1132
1133 if (ios->clock == 0)
1134 return 0;
1135
1136 dsor = func_clk_rate / ios->clock;
1137 if (dsor < 1)
1138 dsor = 1;
1139
1140 if (func_clk_rate / dsor > ios->clock)
1141 dsor++;
1142
1143 if (dsor > 250)
1144 dsor = 250;
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001145
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001146 slot->fclk_freq = func_clk_rate / dsor;
1147
Tony Lindgrend3af5ab2007-05-01 16:36:00 +02001148 if (ios->bus_width == MMC_BUS_WIDTH_4)
1149 dsor |= 1 << 15;
1150
1151 return dsor;
1152}
1153
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001154static void mmc_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1155{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001156 struct mmc_omap_slot *slot = mmc_priv(mmc);
1157 struct mmc_omap_host *host = slot->host;
1158 int i, dsor;
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001159 int clk_enabled;
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001160
1161 mmc_omap_select_slot(slot, 0);
1162
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001163 dsor = mmc_omap_calc_divisor(mmc, ios);
1164
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001165 if (ios->vdd != slot->vdd)
1166 slot->vdd = ios->vdd;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001167
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001168 clk_enabled = 0;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001169 switch (ios->power_mode) {
1170 case MMC_POWER_OFF:
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001171 mmc_omap_set_power(slot, 0, ios->vdd);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001172 break;
1173 case MMC_POWER_UP:
Tony Lindgren46a67302007-05-01 16:34:16 +02001174 /* Cannot touch dsor yet, just power up MMC */
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001175 mmc_omap_set_power(slot, 1, ios->vdd);
1176 goto exit;
Tony Lindgren46a67302007-05-01 16:34:16 +02001177 case MMC_POWER_ON:
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001178 mmc_omap_fclk_enable(host, 1);
1179 clk_enabled = 1;
Juha Yrjola juha.yrjolac5cb4312006-11-11 23:42:39 +01001180 dsor |= 1 << 11;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001181 break;
1182 }
1183
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001184 if (slot->bus_mode != ios->bus_mode) {
1185 if (slot->pdata->set_bus_mode != NULL)
1186 slot->pdata->set_bus_mode(mmc_dev(mmc), slot->id,
1187 ios->bus_mode);
1188 slot->bus_mode = ios->bus_mode;
1189 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001190
1191 /* On insanely high arm_per frequencies something sometimes
1192 * goes somehow out of sync, and the POW bit is not being set,
1193 * which results in the while loop below getting stuck.
1194 * Writing to the CON register twice seems to do the trick. */
1195 for (i = 0; i < 2; i++)
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001196 OMAP_MMC_WRITE(host, CON, dsor);
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001197 slot->saved_con = dsor;
Tony Lindgren46a67302007-05-01 16:34:16 +02001198 if (ios->power_mode == MMC_POWER_ON) {
Jarkko Lavinen9d7c6ee2008-03-26 16:10:02 -04001199 /* worst case at 400kHz, 80 cycles makes 200 microsecs */
1200 int usecs = 250;
1201
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001202 /* Send clock cycles, poll completion */
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001203 OMAP_MMC_WRITE(host, IE, 0);
1204 OMAP_MMC_WRITE(host, STAT, 0xffff);
Juha Yrjola juha.yrjolac5cb4312006-11-11 23:42:39 +01001205 OMAP_MMC_WRITE(host, CMD, 1 << 7);
Jarkko Lavinen9d7c6ee2008-03-26 16:10:02 -04001206 while (usecs > 0 && (OMAP_MMC_READ(host, STAT) & 1) == 0) {
1207 udelay(1);
1208 usecs--;
1209 }
Juha Yrjola juha.yrjola3342ee82006-11-11 23:36:52 +01001210 OMAP_MMC_WRITE(host, STAT, 1);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001211 }
Juha Yrjola65b5b6e2008-03-26 16:09:22 -04001212
1213exit:
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001214 mmc_omap_release_slot(slot, clk_enabled);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001215}
1216
David Brownellab7aefd2006-11-12 17:55:30 -08001217static const struct mmc_host_ops mmc_omap_ops = {
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001218 .request = mmc_omap_request,
1219 .set_ios = mmc_omap_set_ios,
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001220};
1221
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001222static int mmc_omap_new_slot(struct mmc_omap_host *host, int id)
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001223{
1224 struct mmc_omap_slot *slot = NULL;
1225 struct mmc_host *mmc;
1226 int r;
1227
1228 mmc = mmc_alloc_host(sizeof(struct mmc_omap_slot), host->dev);
1229 if (mmc == NULL)
1230 return -ENOMEM;
1231
1232 slot = mmc_priv(mmc);
1233 slot->host = host;
1234 slot->mmc = mmc;
1235 slot->id = id;
1236 slot->pdata = &host->pdata->slots[id];
1237
1238 host->slots[id] = slot;
1239
Pierre Ossman23af6032008-07-06 01:10:27 +02001240 mmc->caps = 0;
Tony Lindgren90c62bf2008-12-10 17:37:17 -08001241 if (host->pdata->slots[id].wires >= 4)
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001242 mmc->caps |= MMC_CAP_4_BIT_DATA;
1243
1244 mmc->ops = &mmc_omap_ops;
1245 mmc->f_min = 400000;
1246
Tony Lindgren53db20d2012-10-15 12:10:33 -07001247 if (mmc_omap2())
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001248 mmc->f_max = 48000000;
1249 else
1250 mmc->f_max = 24000000;
1251 if (host->pdata->max_freq)
1252 mmc->f_max = min(host->pdata->max_freq, mmc->f_max);
1253 mmc->ocr_avail = slot->pdata->ocr_mask;
1254
1255 /* Use scatterlist DMA to reduce per-transfer costs.
1256 * NOTE max_seg_size assumption that small blocks aren't
1257 * normally used (except e.g. for reading SD registers).
1258 */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001259 mmc->max_segs = 32;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001260 mmc->max_blk_size = 2048; /* BLEN is 11 bits (+1) */
1261 mmc->max_blk_count = 2048; /* NBLK is 11 bits (+1) */
1262 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1263 mmc->max_seg_size = mmc->max_req_size;
1264
1265 r = mmc_add_host(mmc);
1266 if (r < 0)
1267 goto err_remove_host;
1268
1269 if (slot->pdata->name != NULL) {
1270 r = device_create_file(&mmc->class_dev,
1271 &dev_attr_slot_name);
1272 if (r < 0)
1273 goto err_remove_host;
1274 }
1275
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001276 if (slot->pdata->get_cover_state != NULL) {
1277 r = device_create_file(&mmc->class_dev,
1278 &dev_attr_cover_switch);
1279 if (r < 0)
1280 goto err_remove_slot_name;
1281
Jarkko Lavinen7584d272008-03-26 16:09:42 -04001282 setup_timer(&slot->cover_timer, mmc_omap_cover_timer,
1283 (unsigned long)slot);
1284 tasklet_init(&slot->cover_tasklet, mmc_omap_cover_handler,
1285 (unsigned long)slot);
1286 tasklet_schedule(&slot->cover_tasklet);
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001287 }
1288
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001289 return 0;
1290
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001291err_remove_slot_name:
1292 if (slot->pdata->name != NULL)
1293 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001294err_remove_host:
1295 mmc_remove_host(mmc);
1296 mmc_free_host(mmc);
1297 return r;
1298}
1299
1300static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
1301{
1302 struct mmc_host *mmc = slot->mmc;
1303
1304 if (slot->pdata->name != NULL)
1305 device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
Juha Yrjola5a0f3f12008-03-26 16:09:08 -04001306 if (slot->pdata->get_cover_state != NULL)
1307 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1308
Jarkko Lavinen7584d272008-03-26 16:09:42 -04001309 tasklet_kill(&slot->cover_tasklet);
1310 del_timer_sync(&slot->cover_timer);
Venkatraman Sb01a4f12012-05-08 17:05:33 +05301311 flush_workqueue(slot->host->mmc_omap_wq);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001312
1313 mmc_remove_host(mmc);
1314 mmc_free_host(mmc);
1315}
1316
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001317static int mmc_omap_probe(struct platform_device *pdev)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001318{
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001319 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001320 struct mmc_omap_host *host = NULL;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001321 struct resource *res;
Russell King3451c062012-04-21 22:35:42 +01001322 dma_cap_mask_t mask;
Tony Lindgren31ee9182013-11-26 15:50:33 -08001323 unsigned sig = 0;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001324 int i, ret = 0;
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001325 int irq;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001326
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001327 if (pdata == NULL) {
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001328 dev_err(&pdev->dev, "platform data missing\n");
1329 return -ENXIO;
1330 }
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001331 if (pdata->nr_slots == 0) {
1332 dev_err(&pdev->dev, "no slots\n");
Tony Lindgren9cb238c2013-11-26 15:50:33 -08001333 return -EPROBE_DEFER;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001334 }
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001335
1336 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001337 irq = platform_get_irq(pdev, 0);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001338 if (res == NULL || irq < 0)
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001339 return -ENXIO;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001340
Chris Ball20920142011-03-22 16:34:41 -07001341 res = request_mem_region(res->start, resource_size(res),
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001342 pdev->name);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001343 if (res == NULL)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001344 return -EBUSY;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001345
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001346 host = kzalloc(sizeof(struct mmc_omap_host), GFP_KERNEL);
1347 if (host == NULL) {
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001348 ret = -ENOMEM;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001349 goto err_free_mem_region;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001350 }
1351
Jarkko Lavinen0f602ec2008-03-26 16:09:58 -04001352 INIT_WORK(&host->slot_release_work, mmc_omap_slot_release_work);
1353 INIT_WORK(&host->send_stop_work, mmc_omap_send_stop_work);
1354
Jarkko Lavinen0fb47232008-03-26 16:09:48 -04001355 INIT_WORK(&host->cmd_abort_work, mmc_omap_abort_command);
1356 setup_timer(&host->cmd_abort_timer, mmc_omap_cmd_timer,
1357 (unsigned long) host);
Jarkko Lavineneb1860b2008-03-26 16:09:29 -04001358
Jarkko Lavinen0807a9b2008-03-26 16:09:52 -04001359 spin_lock_init(&host->clk_lock);
1360 setup_timer(&host->clk_timer, mmc_omap_clk_timer, (unsigned long) host);
1361
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001362 spin_lock_init(&host->dma_lock);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001363 spin_lock_init(&host->slot_lock);
1364 init_waitqueue_head(&host->slot_wq);
1365
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001366 host->pdata = pdata;
Tony Lindgren53db20d2012-10-15 12:10:33 -07001367 host->features = host->pdata->slots[0].features;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001368 host->dev = &pdev->dev;
1369 platform_set_drvdata(pdev, host);
1370
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001371 host->id = pdev->id;
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001372 host->mem_res = res;
Tony Lindgrence9c1a82006-07-01 19:56:44 +01001373 host->irq = irq;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001374 host->use_dma = 1;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001375 host->irq = irq;
1376 host->phys_base = host->mem_res->start;
Chris Ball20920142011-03-22 16:34:41 -07001377 host->virt_base = ioremap(res->start, resource_size(res));
Russell King55c381e2008-09-04 14:07:22 +01001378 if (!host->virt_base)
1379 goto err_ioremap;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001380
Russell Kingd4a36645a2009-01-23 19:03:37 +00001381 host->iclk = clk_get(&pdev->dev, "ick");
Ladislav Michle799acb2009-12-14 18:01:24 -08001382 if (IS_ERR(host->iclk)) {
1383 ret = PTR_ERR(host->iclk);
Russell Kingd4a36645a2009-01-23 19:03:37 +00001384 goto err_free_mmc_host;
Ladislav Michle799acb2009-12-14 18:01:24 -08001385 }
Russell Kingd4a36645a2009-01-23 19:03:37 +00001386 clk_enable(host->iclk);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001387
Russell King5c9e02b2009-01-19 20:53:30 +00001388 host->fclk = clk_get(&pdev->dev, "fck");
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001389 if (IS_ERR(host->fclk)) {
1390 ret = PTR_ERR(host->fclk);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001391 goto err_free_iclk;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001392 }
1393
Russell King3451c062012-04-21 22:35:42 +01001394 dma_cap_zero(mask);
1395 dma_cap_set(DMA_SLAVE, mask);
1396
1397 host->dma_tx_burst = -1;
1398 host->dma_rx_burst = -1;
1399
Tony Lindgren31ee9182013-11-26 15:50:33 -08001400 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1401 if (res)
1402 sig = res->start;
1403 host->dma_tx = dma_request_slave_channel_compat(mask,
1404 omap_dma_filter_fn, &sig, &pdev->dev, "tx");
Russell King3451c062012-04-21 22:35:42 +01001405 if (!host->dma_tx)
1406 dev_warn(host->dev, "unable to obtain TX DMA engine channel %u\n",
1407 sig);
Tony Lindgren31ee9182013-11-26 15:50:33 -08001408
1409 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1410 if (res)
1411 sig = res->start;
1412 host->dma_rx = dma_request_slave_channel_compat(mask,
1413 omap_dma_filter_fn, &sig, &pdev->dev, "rx");
Russell King3451c062012-04-21 22:35:42 +01001414 if (!host->dma_rx)
1415 dev_warn(host->dev, "unable to obtain RX DMA engine channel %u\n",
1416 sig);
Russell King3451c062012-04-21 22:35:42 +01001417
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001418 ret = request_irq(host->irq, mmc_omap_irq, 0, DRIVER_NAME, host);
1419 if (ret)
Russell King3451c062012-04-21 22:35:42 +01001420 goto err_free_dma;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001421
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001422 if (pdata->init != NULL) {
1423 ret = pdata->init(&pdev->dev);
1424 if (ret < 0)
1425 goto err_free_irq;
1426 }
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001427
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001428 host->nr_slots = pdata->nr_slots;
Tony Lindgren53db20d2012-10-15 12:10:33 -07001429 host->reg_shift = (mmc_omap7xx() ? 1 : 2);
Tony Lindgren3caf4142012-06-06 09:45:50 -04001430
1431 host->mmc_omap_wq = alloc_workqueue("mmc_omap", 0, 0);
1432 if (!host->mmc_omap_wq)
1433 goto err_plat_cleanup;
1434
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001435 for (i = 0; i < pdata->nr_slots; i++) {
1436 ret = mmc_omap_new_slot(host, i);
1437 if (ret < 0) {
1438 while (--i >= 0)
1439 mmc_omap_remove_slot(host->slots[i]);
1440
Tony Lindgren3caf4142012-06-06 09:45:50 -04001441 goto err_destroy_wq;
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001442 }
1443 }
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001444
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001445 return 0;
1446
Tony Lindgren3caf4142012-06-06 09:45:50 -04001447err_destroy_wq:
1448 destroy_workqueue(host->mmc_omap_wq);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001449err_plat_cleanup:
1450 if (pdata->cleanup)
1451 pdata->cleanup(&pdev->dev);
1452err_free_irq:
1453 free_irq(host->irq, host);
Russell King3451c062012-04-21 22:35:42 +01001454err_free_dma:
1455 if (host->dma_tx)
1456 dma_release_channel(host->dma_tx);
1457 if (host->dma_rx)
1458 dma_release_channel(host->dma_rx);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001459 clk_put(host->fclk);
1460err_free_iclk:
Ladislav Michle799acb2009-12-14 18:01:24 -08001461 clk_disable(host->iclk);
1462 clk_put(host->iclk);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001463err_free_mmc_host:
Russell King55c381e2008-09-04 14:07:22 +01001464 iounmap(host->virt_base);
1465err_ioremap:
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001466 kfree(host);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001467err_free_mem_region:
Chris Ball20920142011-03-22 16:34:41 -07001468 release_mem_region(res->start, resource_size(res));
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001469 return ret;
1470}
1471
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001472static int mmc_omap_remove(struct platform_device *pdev)
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001473{
1474 struct mmc_omap_host *host = platform_get_drvdata(pdev);
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001475 int i;
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001476
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001477 BUG_ON(host == NULL);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001478
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001479 for (i = 0; i < host->nr_slots; i++)
1480 mmc_omap_remove_slot(host->slots[i]);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001481
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001482 if (host->pdata->cleanup)
1483 host->pdata->cleanup(&pdev->dev);
1484
Russell Kingd4a36645a2009-01-23 19:03:37 +00001485 mmc_omap_fclk_enable(host, 0);
Ladislav Michl49c1d9d2009-11-11 14:26:43 -08001486 free_irq(host->irq, host);
Russell Kingd4a36645a2009-01-23 19:03:37 +00001487 clk_put(host->fclk);
1488 clk_disable(host->iclk);
1489 clk_put(host->iclk);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001490
Russell King3451c062012-04-21 22:35:42 +01001491 if (host->dma_tx)
1492 dma_release_channel(host->dma_tx);
1493 if (host->dma_rx)
1494 dma_release_channel(host->dma_rx);
1495
Russell King55c381e2008-09-04 14:07:22 +01001496 iounmap(host->virt_base);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001497 release_mem_region(pdev->resource[0].start,
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001498 pdev->resource[0].end - pdev->resource[0].start + 1);
Venkatraman Sb01a4f12012-05-08 17:05:33 +05301499 destroy_workqueue(host->mmc_omap_wq);
Juha Yrjola juha.yrjola81ca7032006-11-11 23:39:20 +01001500
Juha Yrjolaabfbe5f2008-03-26 16:08:57 -04001501 kfree(host);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001502
1503 return 0;
1504}
1505
Tony Lindgren9cb238c2013-11-26 15:50:33 -08001506#if IS_BUILTIN(CONFIG_OF)
1507static const struct of_device_id mmc_omap_match[] = {
1508 { .compatible = "ti,omap2420-mmc", },
1509 { },
1510};
1511#endif
1512
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001513static struct platform_driver mmc_omap_driver = {
Venkatraman Sb6e07032012-05-08 17:05:34 +05301514 .probe = mmc_omap_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05001515 .remove = mmc_omap_remove,
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001516 .driver = {
1517 .name = DRIVER_NAME,
Kay Sieversbc65c722008-04-15 14:34:28 -07001518 .owner = THIS_MODULE,
Tony Lindgren9cb238c2013-11-26 15:50:33 -08001519 .of_match_table = of_match_ptr(mmc_omap_match),
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001520 },
1521};
1522
Venkatraman S680f1b52012-05-08 17:05:35 +05301523module_platform_driver(mmc_omap_driver);
Carlos Aguiar730c9b72006-03-29 09:21:00 +01001524MODULE_DESCRIPTION("OMAP Multimedia Card driver");
1525MODULE_LICENSE("GPL");
Kay Sieversbc65c722008-04-15 14:34:28 -07001526MODULE_ALIAS("platform:" DRIVER_NAME);
Al Virod36b6912011-12-29 17:09:01 -05001527MODULE_AUTHOR("Juha Yrjölä");