blob: a4dd43f117f0bceadec9ffa33e4b561b199bec1d [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Denis Karpovd900f712009-09-22 16:44:38 -070020#include <linux/debugfs.h>
21#include <linux/seq_file.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010022#include <linux/interrupt.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/platform_device.h>
26#include <linux/workqueue.h>
27#include <linux/timer.h>
28#include <linux/clk.h>
29#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070030#include <linux/mmc/core.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010031#include <linux/io.h>
32#include <linux/semaphore.h>
33#include <mach/dma.h>
34#include <mach/hardware.h>
35#include <mach/board.h>
36#include <mach/mmc.h>
37#include <mach/cpu.h>
38
39/* OMAP HSMMC Host Controller Registers */
40#define OMAP_HSMMC_SYSCONFIG 0x0010
Denis Karpov11dd62a2009-09-22 16:44:43 -070041#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010042#define OMAP_HSMMC_CON 0x002C
43#define OMAP_HSMMC_BLK 0x0104
44#define OMAP_HSMMC_ARG 0x0108
45#define OMAP_HSMMC_CMD 0x010C
46#define OMAP_HSMMC_RSP10 0x0110
47#define OMAP_HSMMC_RSP32 0x0114
48#define OMAP_HSMMC_RSP54 0x0118
49#define OMAP_HSMMC_RSP76 0x011C
50#define OMAP_HSMMC_DATA 0x0120
51#define OMAP_HSMMC_HCTL 0x0128
52#define OMAP_HSMMC_SYSCTL 0x012C
53#define OMAP_HSMMC_STAT 0x0130
54#define OMAP_HSMMC_IE 0x0134
55#define OMAP_HSMMC_ISE 0x0138
56#define OMAP_HSMMC_CAPA 0x0140
57
58#define VS18 (1 << 26)
59#define VS30 (1 << 25)
60#define SDVS18 (0x5 << 9)
61#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080062#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010063#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010064#define SDVSCLR 0xFFFFF1FF
65#define SDVSDET 0x00000400
66#define AUTOIDLE 0x1
67#define SDBP (1 << 8)
68#define DTO 0xe
69#define ICE 0x1
70#define ICS 0x2
71#define CEN (1 << 2)
72#define CLKD_MASK 0x0000FFC0
73#define CLKD_SHIFT 6
74#define DTO_MASK 0x000F0000
75#define DTO_SHIFT 16
76#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070077#define BWR_ENABLE (1 << 4)
78#define BRR_ENABLE (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010079#define INIT_STREAM (1 << 1)
80#define DP_SELECT (1 << 21)
81#define DDIR (1 << 4)
82#define DMA_EN 0x1
83#define MSBS (1 << 5)
84#define BCE (1 << 1)
85#define FOUR_BIT (1 << 1)
Jarkko Lavinen73153012008-11-21 16:49:54 +020086#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010087#define CC 0x1
88#define TC 0x02
89#define OD 0x1
90#define ERR (1 << 15)
91#define CMD_TIMEOUT (1 << 16)
92#define DATA_TIMEOUT (1 << 20)
93#define CMD_CRC (1 << 17)
94#define DATA_CRC (1 << 21)
95#define CARD_ERR (1 << 28)
96#define STAT_CLEAR 0xFFFFFFFF
97#define INIT_STREAM_CMD 0x00000000
98#define DUAL_VOLT_OCR_BIT 7
99#define SRC (1 << 25)
100#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700101#define SOFTRESET (1 << 1)
102#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100103
104/*
105 * FIXME: Most likely all the data using these _DEVID defines should come
106 * from the platform_data, or implemented in controller and slot specific
107 * functions.
108 */
109#define OMAP_MMC1_DEVID 0
110#define OMAP_MMC2_DEVID 1
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000111#define OMAP_MMC3_DEVID 2
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100112
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100113#define MMC_TIMEOUT_MS 20
114#define OMAP_MMC_MASTER_CLOCK 96000000
115#define DRIVER_NAME "mmci-omap-hs"
116
Denis Karpovdd498ef2009-09-22 16:44:49 -0700117/* Timeouts for entering power saving states on inactivity, msec */
118#define OMAP_MMC_DISABLED_TIMEOUT 100
Jarkko Lavinen13189e72009-09-22 16:44:53 -0700119#define OMAP_MMC_SLEEP_TIMEOUT 1000
120#define OMAP_MMC_OFF_TIMEOUT 8000
Denis Karpovdd498ef2009-09-22 16:44:49 -0700121
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100122/*
123 * One controller can have multiple slots, like on some omap boards using
124 * omap.c controller driver. Luckily this is not currently done on any known
125 * omap_hsmmc.c device.
126 */
127#define mmc_slot(host) (host->pdata->slots[host->slot_id])
128
129/*
130 * MMC Host controller read/write API's
131 */
132#define OMAP_HSMMC_READ(base, reg) \
133 __raw_readl((base) + OMAP_HSMMC_##reg)
134
135#define OMAP_HSMMC_WRITE(base, reg, val) \
136 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
137
Denis Karpov70a33412009-09-22 16:44:59 -0700138struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100139 struct device *dev;
140 struct mmc_host *mmc;
141 struct mmc_request *mrq;
142 struct mmc_command *cmd;
143 struct mmc_data *data;
144 struct clk *fclk;
145 struct clk *iclk;
146 struct clk *dbclk;
147 struct semaphore sem;
148 struct work_struct mmc_carddetect_work;
149 void __iomem *base;
150 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700151 spinlock_t irq_lock; /* Prevent races with irq handler */
152 unsigned long flags;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100153 unsigned int id;
154 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200155 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100156 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700157 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100158 u32 *buffer;
159 u32 bytesleft;
160 int suspended;
161 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100162 int use_dma, dma_ch;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000163 int dma_line_tx, dma_line_rx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100164 int slot_id;
Adrian Hunter2bec0892009-09-22 16:45:02 -0700165 int got_dbclk;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200166 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700167 int context_loss;
Denis Karpovdd498ef2009-09-22 16:44:49 -0700168 int dpm_state;
Adrian Hunter623821f2009-09-22 16:44:51 -0700169 int vdd;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700170 int protect_card;
171 int reqs_blocked;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700172
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100173 struct omap_mmc_platform_data *pdata;
174};
175
176/*
177 * Stop clock to the card
178 */
Denis Karpov70a33412009-09-22 16:44:59 -0700179static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100180{
181 OMAP_HSMMC_WRITE(host->base, SYSCTL,
182 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
183 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
184 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
185}
186
Denis Karpov11dd62a2009-09-22 16:44:43 -0700187#ifdef CONFIG_PM
188
189/*
190 * Restore the MMC host context, if it was lost as result of a
191 * power state change.
192 */
Denis Karpov70a33412009-09-22 16:44:59 -0700193static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700194{
195 struct mmc_ios *ios = &host->mmc->ios;
196 struct omap_mmc_platform_data *pdata = host->pdata;
197 int context_loss = 0;
198 u32 hctl, capa, con;
199 u16 dsor = 0;
200 unsigned long timeout;
201
202 if (pdata->get_context_loss_count) {
203 context_loss = pdata->get_context_loss_count(host->dev);
204 if (context_loss < 0)
205 return 1;
206 }
207
208 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
209 context_loss == host->context_loss ? "not " : "");
210 if (host->context_loss == context_loss)
211 return 1;
212
213 /* Wait for hardware reset */
214 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
215 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
216 && time_before(jiffies, timeout))
217 ;
218
219 /* Do software reset */
220 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
221 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
222 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
223 && time_before(jiffies, timeout))
224 ;
225
226 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
227 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
228
229 if (host->id == OMAP_MMC1_DEVID) {
230 if (host->power_mode != MMC_POWER_OFF &&
231 (1 << ios->vdd) <= MMC_VDD_23_24)
232 hctl = SDVS18;
233 else
234 hctl = SDVS30;
235 capa = VS30 | VS18;
236 } else {
237 hctl = SDVS18;
238 capa = VS18;
239 }
240
241 OMAP_HSMMC_WRITE(host->base, HCTL,
242 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
243
244 OMAP_HSMMC_WRITE(host->base, CAPA,
245 OMAP_HSMMC_READ(host->base, CAPA) | capa);
246
247 OMAP_HSMMC_WRITE(host->base, HCTL,
248 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
249
250 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
251 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
252 && time_before(jiffies, timeout))
253 ;
254
255 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
256 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
257 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
258
259 /* Do not initialize card-specific things if the power is off */
260 if (host->power_mode == MMC_POWER_OFF)
261 goto out;
262
263 con = OMAP_HSMMC_READ(host->base, CON);
264 switch (ios->bus_width) {
265 case MMC_BUS_WIDTH_8:
266 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
267 break;
268 case MMC_BUS_WIDTH_4:
269 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
270 OMAP_HSMMC_WRITE(host->base, HCTL,
271 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
272 break;
273 case MMC_BUS_WIDTH_1:
274 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
275 OMAP_HSMMC_WRITE(host->base, HCTL,
276 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
277 break;
278 }
279
280 if (ios->clock) {
281 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
282 if (dsor < 1)
283 dsor = 1;
284
285 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
286 dsor++;
287
288 if (dsor > 250)
289 dsor = 250;
290 }
291
292 OMAP_HSMMC_WRITE(host->base, SYSCTL,
293 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
294 OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
295 OMAP_HSMMC_WRITE(host->base, SYSCTL,
296 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
297
298 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
299 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
300 && time_before(jiffies, timeout))
301 ;
302
303 OMAP_HSMMC_WRITE(host->base, SYSCTL,
304 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
305
306 con = OMAP_HSMMC_READ(host->base, CON);
307 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
308 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
309 else
310 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
311out:
312 host->context_loss = context_loss;
313
314 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
315 return 0;
316}
317
318/*
319 * Save the MMC host context (store the number of power state changes so far).
320 */
Denis Karpov70a33412009-09-22 16:44:59 -0700321static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700322{
323 struct omap_mmc_platform_data *pdata = host->pdata;
324 int context_loss;
325
326 if (pdata->get_context_loss_count) {
327 context_loss = pdata->get_context_loss_count(host->dev);
328 if (context_loss < 0)
329 return;
330 host->context_loss = context_loss;
331 }
332}
333
334#else
335
Denis Karpov70a33412009-09-22 16:44:59 -0700336static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700337{
338 return 0;
339}
340
Denis Karpov70a33412009-09-22 16:44:59 -0700341static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700342{
343}
344
345#endif
346
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100347/*
348 * Send init stream sequence to card
349 * before sending IDLE command
350 */
Denis Karpov70a33412009-09-22 16:44:59 -0700351static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100352{
353 int reg = 0;
354 unsigned long timeout;
355
Adrian Hunterb62f6222009-09-22 16:45:01 -0700356 if (host->protect_card)
357 return;
358
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100359 disable_irq(host->irq);
360 OMAP_HSMMC_WRITE(host->base, CON,
361 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
362 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
363
364 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
365 while ((reg != CC) && time_before(jiffies, timeout))
366 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
367
368 OMAP_HSMMC_WRITE(host->base, CON,
369 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700370
371 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
372 OMAP_HSMMC_READ(host->base, STAT);
373
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100374 enable_irq(host->irq);
375}
376
377static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700378int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100379{
380 int r = 1;
381
Denis Karpov191d1f12009-09-22 16:44:55 -0700382 if (mmc_slot(host).get_cover_state)
383 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100384 return r;
385}
386
387static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700388omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100389 char *buf)
390{
391 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700392 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100393
Denis Karpov70a33412009-09-22 16:44:59 -0700394 return sprintf(buf, "%s\n",
395 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100396}
397
Denis Karpov70a33412009-09-22 16:44:59 -0700398static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100399
400static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700401omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100402 char *buf)
403{
404 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700405 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100406
Denis Karpov191d1f12009-09-22 16:44:55 -0700407 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100408}
409
Denis Karpov70a33412009-09-22 16:44:59 -0700410static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100411
412/*
413 * Configure the response type and send the cmd.
414 */
415static void
Denis Karpov70a33412009-09-22 16:44:59 -0700416omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100417 struct mmc_data *data)
418{
419 int cmdreg = 0, resptype = 0, cmdtype = 0;
420
421 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
422 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
423 host->cmd = cmd;
424
425 /*
426 * Clear status bits and enable interrupts
427 */
428 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
429 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -0700430
431 if (host->use_dma)
432 OMAP_HSMMC_WRITE(host->base, IE,
433 INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE));
434 else
435 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100436
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200437 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100438 if (cmd->flags & MMC_RSP_PRESENT) {
439 if (cmd->flags & MMC_RSP_136)
440 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200441 else if (cmd->flags & MMC_RSP_BUSY) {
442 resptype = 3;
443 host->response_busy = 1;
444 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100445 resptype = 2;
446 }
447
448 /*
449 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
450 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
451 * a val of 0x3, rest 0x0.
452 */
453 if (cmd == host->mrq->stop)
454 cmdtype = 0x3;
455
456 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
457
458 if (data) {
459 cmdreg |= DP_SELECT | MSBS | BCE;
460 if (data->flags & MMC_DATA_READ)
461 cmdreg |= DDIR;
462 else
463 cmdreg &= ~(DDIR);
464 }
465
466 if (host->use_dma)
467 cmdreg |= DMA_EN;
468
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700469 /*
470 * In an interrupt context (i.e. STOP command), the spinlock is unlocked
471 * by the interrupt handler, otherwise (i.e. for a new request) it is
472 * unlocked here.
473 */
474 if (!in_interrupt())
475 spin_unlock_irqrestore(&host->irq_lock, host->flags);
476
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100477 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
478 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
479}
480
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200481static int
Denis Karpov70a33412009-09-22 16:44:59 -0700482omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200483{
484 if (data->flags & MMC_DATA_WRITE)
485 return DMA_TO_DEVICE;
486 else
487 return DMA_FROM_DEVICE;
488}
489
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100490/*
491 * Notify the transfer complete to MMC core
492 */
493static void
Denis Karpov70a33412009-09-22 16:44:59 -0700494omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100495{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200496 if (!data) {
497 struct mmc_request *mrq = host->mrq;
498
Adrian Hunter23050102009-09-22 16:44:57 -0700499 /* TC before CC from CMD6 - don't know why, but it happens */
500 if (host->cmd && host->cmd->opcode == 6 &&
501 host->response_busy) {
502 host->response_busy = 0;
503 return;
504 }
505
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200506 host->mrq = NULL;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200507 mmc_request_done(host->mmc, mrq);
508 return;
509 }
510
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100511 host->data = NULL;
512
513 if (host->use_dma && host->dma_ch != -1)
514 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700515 omap_hsmmc_get_dma_dir(host, data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100516
517 if (!data->error)
518 data->bytes_xfered += data->blocks * (data->blksz);
519 else
520 data->bytes_xfered = 0;
521
522 if (!data->stop) {
523 host->mrq = NULL;
524 mmc_request_done(host->mmc, data->mrq);
525 return;
526 }
Denis Karpov70a33412009-09-22 16:44:59 -0700527 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100528}
529
530/*
531 * Notify the core about command completion
532 */
533static void
Denis Karpov70a33412009-09-22 16:44:59 -0700534omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100535{
536 host->cmd = NULL;
537
538 if (cmd->flags & MMC_RSP_PRESENT) {
539 if (cmd->flags & MMC_RSP_136) {
540 /* response type 2 */
541 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
542 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
543 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
544 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
545 } else {
546 /* response types 1, 1b, 3, 4, 5, 6 */
547 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
548 }
549 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200550 if ((host->data == NULL && !host->response_busy) || cmd->error) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100551 host->mrq = NULL;
552 mmc_request_done(host->mmc, cmd->mrq);
553 }
554}
555
556/*
557 * DMA clean up for command errors
558 */
Denis Karpov70a33412009-09-22 16:44:59 -0700559static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100560{
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200561 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100562
563 if (host->use_dma && host->dma_ch != -1) {
564 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700565 omap_hsmmc_get_dma_dir(host, host->data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100566 omap_free_dma(host->dma_ch);
567 host->dma_ch = -1;
568 up(&host->sem);
569 }
570 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100571}
572
573/*
574 * Readable error output
575 */
576#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -0700577static void omap_hsmmc_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100578{
579 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700580 static const char *omap_hsmmc_status_bits[] = {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100581 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
582 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
583 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
584 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
585 };
586 char res[256];
587 char *buf = res;
588 int len, i;
589
590 len = sprintf(buf, "MMC IRQ 0x%x :", status);
591 buf += len;
592
Denis Karpov70a33412009-09-22 16:44:59 -0700593 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100594 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700595 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100596 buf += len;
597 }
598
599 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
600}
601#endif /* CONFIG_MMC_DEBUG */
602
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100603/*
604 * MMC controller internal state machines reset
605 *
606 * Used to reset command or data internal state machines, using respectively
607 * SRC or SRD bit of SYSCTL register
608 * Can be called from interrupt context
609 */
Denis Karpov70a33412009-09-22 16:44:59 -0700610static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
611 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100612{
613 unsigned long i = 0;
614 unsigned long limit = (loops_per_jiffy *
615 msecs_to_jiffies(MMC_TIMEOUT_MS));
616
617 OMAP_HSMMC_WRITE(host->base, SYSCTL,
618 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
619
620 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
621 (i++ < limit))
622 cpu_relax();
623
624 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
625 dev_err(mmc_dev(host->mmc),
626 "Timeout waiting on controller reset in %s\n",
627 __func__);
628}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100629
630/*
631 * MMC controller IRQ handler
632 */
Denis Karpov70a33412009-09-22 16:44:59 -0700633static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100634{
Denis Karpov70a33412009-09-22 16:44:59 -0700635 struct omap_hsmmc_host *host = dev_id;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100636 struct mmc_data *data;
637 int end_cmd = 0, end_trans = 0, status;
638
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700639 spin_lock(&host->irq_lock);
640
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200641 if (host->mrq == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100642 OMAP_HSMMC_WRITE(host->base, STAT,
643 OMAP_HSMMC_READ(host->base, STAT));
Kevin Hilman00adadc2009-04-06 15:01:19 +0300644 /* Flush posted write */
645 OMAP_HSMMC_READ(host->base, STAT);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700646 spin_unlock(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100647 return IRQ_HANDLED;
648 }
649
650 data = host->data;
651 status = OMAP_HSMMC_READ(host->base, STAT);
652 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
653
654 if (status & ERR) {
655#ifdef CONFIG_MMC_DEBUG
Denis Karpov70a33412009-09-22 16:44:59 -0700656 omap_hsmmc_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100657#endif
658 if ((status & CMD_TIMEOUT) ||
659 (status & CMD_CRC)) {
660 if (host->cmd) {
661 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -0700662 omap_hsmmc_reset_controller_fsm(host,
663 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100664 host->cmd->error = -ETIMEDOUT;
665 } else {
666 host->cmd->error = -EILSEQ;
667 }
668 end_cmd = 1;
669 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200670 if (host->data || host->response_busy) {
671 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -0700672 omap_hsmmc_dma_cleanup(host,
673 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200674 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -0700675 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -0800676 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100677 }
678 if ((status & DATA_TIMEOUT) ||
679 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200680 if (host->data || host->response_busy) {
681 int err = (status & DATA_TIMEOUT) ?
682 -ETIMEDOUT : -EILSEQ;
683
684 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -0700685 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100686 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200687 host->mrq->cmd->error = err;
688 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -0700689 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100690 end_trans = 1;
691 }
692 }
693 if (status & CARD_ERR) {
694 dev_dbg(mmc_dev(host->mmc),
695 "Ignoring card err CMD%d\n", host->cmd->opcode);
696 if (host->cmd)
697 end_cmd = 1;
698 if (host->data)
699 end_trans = 1;
700 }
701 }
702
703 OMAP_HSMMC_WRITE(host->base, STAT, status);
Kevin Hilman00adadc2009-04-06 15:01:19 +0300704 /* Flush posted write */
705 OMAP_HSMMC_READ(host->base, STAT);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100706
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +0300707 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -0700708 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -0700709 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -0700710 omap_hsmmc_xfer_done(host, data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100711
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700712 spin_unlock(&host->irq_lock);
713
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100714 return IRQ_HANDLED;
715}
716
Denis Karpov70a33412009-09-22 16:44:59 -0700717static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +0200718{
719 unsigned long i;
720
721 OMAP_HSMMC_WRITE(host->base, HCTL,
722 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
723 for (i = 0; i < loops_per_jiffy; i++) {
724 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
725 break;
726 cpu_relax();
727 }
728}
729
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100730/*
David Brownelleb250822009-02-17 14:49:01 -0800731 * Switch MMC interface voltage ... only relevant for MMC1.
732 *
733 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
734 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
735 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100736 */
Denis Karpov70a33412009-09-22 16:44:59 -0700737static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100738{
739 u32 reg_val = 0;
740 int ret;
741
742 /* Disable the clocks */
743 clk_disable(host->fclk);
744 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -0700745 if (host->got_dbclk)
746 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100747
748 /* Turn the power off */
749 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100750
751 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -0700752 if (!ret)
753 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
754 vdd);
755 clk_enable(host->iclk);
756 clk_enable(host->fclk);
757 if (host->got_dbclk)
758 clk_enable(host->dbclk);
759
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100760 if (ret != 0)
761 goto err;
762
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100763 OMAP_HSMMC_WRITE(host->base, HCTL,
764 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
765 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -0800766
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100767 /*
768 * If a MMC dual voltage card is detected, the set_ios fn calls
769 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -0700770 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100771 *
David Brownelleb250822009-02-17 14:49:01 -0800772 * Cope with a bit of slop in the range ... per data sheets:
773 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
774 * but recommended values are 1.71V to 1.89V
775 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
776 * but recommended values are 2.7V to 3.3V
777 *
778 * Board setup code shouldn't permit anything very out-of-range.
779 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
780 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100781 */
David Brownelleb250822009-02-17 14:49:01 -0800782 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100783 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -0800784 else
785 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100786
787 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +0200788 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100789
790 return 0;
791err:
792 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
793 return ret;
794}
795
Adrian Hunterb62f6222009-09-22 16:45:01 -0700796/* Protect the card while the cover is open */
797static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
798{
799 if (!mmc_slot(host).get_cover_state)
800 return;
801
802 host->reqs_blocked = 0;
803 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
804 if (host->protect_card) {
805 printk(KERN_INFO "%s: cover is closed, "
806 "card is now accessible\n",
807 mmc_hostname(host->mmc));
808 host->protect_card = 0;
809 }
810 } else {
811 if (!host->protect_card) {
812 printk(KERN_INFO "%s: cover is open, "
813 "card is now inaccessible\n",
814 mmc_hostname(host->mmc));
815 host->protect_card = 1;
816 }
817 }
818}
819
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100820/*
821 * Work Item to notify the core about card insertion/removal
822 */
Denis Karpov70a33412009-09-22 16:44:59 -0700823static void omap_hsmmc_detect(struct work_struct *work)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100824{
Denis Karpov70a33412009-09-22 16:44:59 -0700825 struct omap_hsmmc_host *host =
826 container_of(work, struct omap_hsmmc_host, mmc_carddetect_work);
David Brownell249d0fa2009-02-04 14:42:03 -0800827 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -0700828 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -0800829
Adrian Huntera6b22402009-09-22 16:44:45 -0700830 if (host->suspended)
831 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100832
833 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -0700834
Denis Karpov191d1f12009-09-22 16:44:55 -0700835 if (slot->card_detect)
Adrian Huntera6b22402009-09-22 16:44:45 -0700836 carddetect = slot->card_detect(slot->card_detect_irq);
Adrian Hunterb62f6222009-09-22 16:45:01 -0700837 else {
838 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -0700839 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700840 }
Adrian Huntera6b22402009-09-22 16:44:45 -0700841
842 if (carddetect) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100843 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
844 } else {
Adrian Hunter5e2ea612009-09-22 16:44:39 -0700845 mmc_host_enable(host->mmc);
Denis Karpov70a33412009-09-22 16:44:59 -0700846 omap_hsmmc_reset_controller_fsm(host, SRD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -0700847 mmc_host_lazy_disable(host->mmc);
Denis Karpov70a33412009-09-22 16:44:59 -0700848
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100849 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
850 }
851}
852
853/*
854 * ISR for handling card insertion and removal
855 */
Denis Karpov70a33412009-09-22 16:44:59 -0700856static irqreturn_t omap_hsmmc_cd_handler(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100857{
Denis Karpov70a33412009-09-22 16:44:59 -0700858 struct omap_hsmmc_host *host = (struct omap_hsmmc_host *)dev_id;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100859
Adrian Huntera6b22402009-09-22 16:44:45 -0700860 if (host->suspended)
861 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100862 schedule_work(&host->mmc_carddetect_work);
863
864 return IRQ_HANDLED;
865}
866
Denis Karpov70a33412009-09-22 16:44:59 -0700867static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200868 struct mmc_data *data)
869{
870 int sync_dev;
871
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000872 if (data->flags & MMC_DATA_WRITE)
873 sync_dev = host->dma_line_tx;
874 else
875 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200876 return sync_dev;
877}
878
Denis Karpov70a33412009-09-22 16:44:59 -0700879static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200880 struct mmc_data *data,
881 struct scatterlist *sgl)
882{
883 int blksz, nblk, dma_ch;
884
885 dma_ch = host->dma_ch;
886 if (data->flags & MMC_DATA_WRITE) {
887 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
888 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
889 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
890 sg_dma_address(sgl), 0, 0);
891 } else {
892 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -0700893 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200894 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
895 sg_dma_address(sgl), 0, 0);
896 }
897
898 blksz = host->data->blksz;
899 nblk = sg_dma_len(sgl) / blksz;
900
901 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
902 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -0700903 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200904 !(data->flags & MMC_DATA_WRITE));
905
906 omap_start_dma(dma_ch);
907}
908
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100909/*
910 * DMA call back function
911 */
Denis Karpov70a33412009-09-22 16:44:59 -0700912static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100913{
Denis Karpov70a33412009-09-22 16:44:59 -0700914 struct omap_hsmmc_host *host = data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100915
916 if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ)
917 dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n");
918
919 if (host->dma_ch < 0)
920 return;
921
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200922 host->dma_sg_idx++;
923 if (host->dma_sg_idx < host->dma_len) {
924 /* Fire up the next transfer. */
Denis Karpov70a33412009-09-22 16:44:59 -0700925 omap_hsmmc_config_dma_params(host, host->data,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200926 host->data->sg + host->dma_sg_idx);
927 return;
928 }
929
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100930 omap_free_dma(host->dma_ch);
931 host->dma_ch = -1;
932 /*
933 * DMA Callback: run in interrupt context.
Anand Gadiyar85b84322009-04-15 17:44:58 +0530934 * mutex_unlock will throw a kernel warning if used.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100935 */
936 up(&host->sem);
937}
938
939/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100940 * Routine to configure and start DMA for the MMC card
941 */
Denis Karpov70a33412009-09-22 16:44:59 -0700942static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
943 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100944{
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200945 int dma_ch = 0, ret = 0, err = 1, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100946 struct mmc_data *data = req->data;
947
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200948 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -0700949 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200950 struct scatterlist *sgl;
951
952 sgl = data->sg + i;
953 if (sgl->length % data->blksz)
954 return -EINVAL;
955 }
956 if ((data->blksz % 4) != 0)
957 /* REVISIT: The MMC buffer increments only when MSB is written.
958 * Return error for blksz which is non multiple of four.
959 */
960 return -EINVAL;
961
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100962 /*
963 * If for some reason the DMA transfer is still active,
964 * we wait for timeout period and free the dma
965 */
966 if (host->dma_ch != -1) {
967 set_current_state(TASK_UNINTERRUPTIBLE);
968 schedule_timeout(100);
969 if (down_trylock(&host->sem)) {
970 omap_free_dma(host->dma_ch);
971 host->dma_ch = -1;
972 up(&host->sem);
973 return err;
974 }
975 } else {
976 if (down_trylock(&host->sem))
977 return err;
978 }
979
Denis Karpov70a33412009-09-22 16:44:59 -0700980 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
981 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100982 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200983 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100984 "%s: omap_request_dma() failed with %d\n",
985 mmc_hostname(host->mmc), ret);
986 return ret;
987 }
988
989 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
Denis Karpov70a33412009-09-22 16:44:59 -0700990 data->sg_len, omap_hsmmc_get_dma_dir(host, data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100991 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200992 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100993
Denis Karpov70a33412009-09-22 16:44:59 -0700994 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100995
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100996 return 0;
997}
998
Denis Karpov70a33412009-09-22 16:44:59 -0700999static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001000 unsigned int timeout_ns,
1001 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001002{
1003 unsigned int timeout, cycle_ns;
1004 uint32_t reg, clkd, dto = 0;
1005
1006 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1007 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1008 if (clkd == 0)
1009 clkd = 1;
1010
1011 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001012 timeout = timeout_ns / cycle_ns;
1013 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001014 if (timeout) {
1015 while ((timeout & 0x80000000) == 0) {
1016 dto += 1;
1017 timeout <<= 1;
1018 }
1019 dto = 31 - dto;
1020 timeout <<= 1;
1021 if (timeout && dto)
1022 dto += 1;
1023 if (dto >= 13)
1024 dto -= 13;
1025 else
1026 dto = 0;
1027 if (dto > 14)
1028 dto = 14;
1029 }
1030
1031 reg &= ~DTO_MASK;
1032 reg |= dto << DTO_SHIFT;
1033 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1034}
1035
1036/*
1037 * Configure block length for MMC/SD cards and initiate the transfer.
1038 */
1039static int
Denis Karpov70a33412009-09-22 16:44:59 -07001040omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001041{
1042 int ret;
1043 host->data = req->data;
1044
1045 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001046 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001047 /*
1048 * Set an arbitrary 100ms data timeout for commands with
1049 * busy signal.
1050 */
1051 if (req->cmd->flags & MMC_RSP_BUSY)
1052 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001053 return 0;
1054 }
1055
1056 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1057 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001058 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001059
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001060 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001061 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001062 if (ret != 0) {
1063 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1064 return ret;
1065 }
1066 }
1067 return 0;
1068}
1069
1070/*
1071 * Request function. for read/write operation
1072 */
Denis Karpov70a33412009-09-22 16:44:59 -07001073static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001074{
Denis Karpov70a33412009-09-22 16:44:59 -07001075 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001076 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001077
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001078 /*
1079 * Prevent races with the interrupt handler because of unexpected
1080 * interrupts, but not if we are already in interrupt context i.e.
1081 * retries.
1082 */
Adrian Hunterb62f6222009-09-22 16:45:01 -07001083 if (!in_interrupt()) {
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001084 spin_lock_irqsave(&host->irq_lock, host->flags);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001085 /*
1086 * Protect the card from I/O if there is a possibility
1087 * it can be removed.
1088 */
1089 if (host->protect_card) {
1090 if (host->reqs_blocked < 3) {
1091 /*
1092 * Ensure the controller is left in a consistent
1093 * state by resetting the command and data state
1094 * machines.
1095 */
1096 omap_hsmmc_reset_controller_fsm(host, SRD);
1097 omap_hsmmc_reset_controller_fsm(host, SRC);
1098 host->reqs_blocked += 1;
1099 }
1100 req->cmd->error = -EBADF;
1101 if (req->data)
1102 req->data->error = -EBADF;
1103 spin_unlock_irqrestore(&host->irq_lock, host->flags);
1104 mmc_request_done(mmc, req);
1105 return;
1106 } else if (host->reqs_blocked)
1107 host->reqs_blocked = 0;
1108 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001109 WARN_ON(host->mrq != NULL);
1110 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001111 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001112 if (err) {
1113 req->cmd->error = err;
1114 if (req->data)
1115 req->data->error = err;
1116 host->mrq = NULL;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001117 if (!in_interrupt())
1118 spin_unlock_irqrestore(&host->irq_lock, host->flags);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001119 mmc_request_done(mmc, req);
1120 return;
1121 }
1122
Denis Karpov70a33412009-09-22 16:44:59 -07001123 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001124}
1125
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001126/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001127static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001128{
Denis Karpov70a33412009-09-22 16:44:59 -07001129 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001130 u16 dsor = 0;
1131 unsigned long regval;
1132 unsigned long timeout;
Jarkko Lavinen73153012008-11-21 16:49:54 +02001133 u32 con;
Adrian Huntera3621462009-09-22 16:44:42 -07001134 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001135
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001136 mmc_host_enable(host->mmc);
1137
Adrian Huntera3621462009-09-22 16:44:42 -07001138 if (ios->power_mode != host->power_mode) {
1139 switch (ios->power_mode) {
1140 case MMC_POWER_OFF:
1141 mmc_slot(host).set_power(host->dev, host->slot_id,
1142 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001143 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001144 break;
1145 case MMC_POWER_UP:
1146 mmc_slot(host).set_power(host->dev, host->slot_id,
1147 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001148 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001149 break;
1150 case MMC_POWER_ON:
1151 do_send_init_stream = 1;
1152 break;
1153 }
1154 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001155 }
1156
Denis Karpovdd498ef2009-09-22 16:44:49 -07001157 /* FIXME: set registers based only on changes to ios */
1158
Jarkko Lavinen73153012008-11-21 16:49:54 +02001159 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001160 switch (mmc->ios.bus_width) {
Jarkko Lavinen73153012008-11-21 16:49:54 +02001161 case MMC_BUS_WIDTH_8:
1162 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1163 break;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001164 case MMC_BUS_WIDTH_4:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001165 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001166 OMAP_HSMMC_WRITE(host->base, HCTL,
1167 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1168 break;
1169 case MMC_BUS_WIDTH_1:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001170 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001171 OMAP_HSMMC_WRITE(host->base, HCTL,
1172 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1173 break;
1174 }
1175
1176 if (host->id == OMAP_MMC1_DEVID) {
David Brownelleb250822009-02-17 14:49:01 -08001177 /* Only MMC1 can interface at 3V without some flavor
1178 * of external transceiver; but they all handle 1.8V.
1179 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001180 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1181 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1182 /*
1183 * The mmc_select_voltage fn of the core does
1184 * not seem to set the power_mode to
1185 * MMC_POWER_UP upon recalculating the voltage.
1186 * vdd 1.8v.
1187 */
Denis Karpov70a33412009-09-22 16:44:59 -07001188 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1189 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001190 "Switch operation failed\n");
1191 }
1192 }
1193
1194 if (ios->clock) {
1195 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1196 if (dsor < 1)
1197 dsor = 1;
1198
1199 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1200 dsor++;
1201
1202 if (dsor > 250)
1203 dsor = 250;
1204 }
Denis Karpov70a33412009-09-22 16:44:59 -07001205 omap_hsmmc_stop_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001206 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1207 regval = regval & ~(CLKD_MASK);
1208 regval = regval | (dsor << 6) | (DTO << 16);
1209 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1210 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1211 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1212
1213 /* Wait till the ICS bit is set */
1214 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001215 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001216 && time_before(jiffies, timeout))
1217 msleep(1);
1218
1219 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1220 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1221
Adrian Huntera3621462009-09-22 16:44:42 -07001222 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001223 send_init_stream(host);
1224
Denis Karpovabb28e72009-09-22 16:44:44 -07001225 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001226 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
Denis Karpovabb28e72009-09-22 16:44:44 -07001227 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1228 else
1229 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001230
Denis Karpovdd498ef2009-09-22 16:44:49 -07001231 if (host->power_mode == MMC_POWER_OFF)
1232 mmc_host_disable(host->mmc);
1233 else
1234 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001235}
1236
1237static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1238{
Denis Karpov70a33412009-09-22 16:44:59 -07001239 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001240
Denis Karpov191d1f12009-09-22 16:44:55 -07001241 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001242 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001243 return mmc_slot(host).card_detect(mmc_slot(host).card_detect_irq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001244}
1245
1246static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1247{
Denis Karpov70a33412009-09-22 16:44:59 -07001248 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001249
Denis Karpov191d1f12009-09-22 16:44:55 -07001250 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001251 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001252 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001253}
1254
Denis Karpov70a33412009-09-22 16:44:59 -07001255static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001256{
1257 u32 hctl, capa, value;
1258
1259 /* Only MMC1 supports 3.0V */
1260 if (host->id == OMAP_MMC1_DEVID) {
1261 hctl = SDVS30;
1262 capa = VS30 | VS18;
1263 } else {
1264 hctl = SDVS18;
1265 capa = VS18;
1266 }
1267
1268 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1269 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1270
1271 value = OMAP_HSMMC_READ(host->base, CAPA);
1272 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1273
1274 /* Set the controller to AUTO IDLE mode */
1275 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1276 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1277
1278 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001279 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001280}
1281
Denis Karpovdd498ef2009-09-22 16:44:49 -07001282/*
1283 * Dynamic power saving handling, FSM:
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001284 * ENABLED -> DISABLED -> CARDSLEEP / REGSLEEP -> OFF
1285 * ^___________| | |
1286 * |______________________|______________________|
Denis Karpovdd498ef2009-09-22 16:44:49 -07001287 *
1288 * ENABLED: mmc host is fully functional
1289 * DISABLED: fclk is off
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001290 * CARDSLEEP: fclk is off, card is asleep, voltage regulator is asleep
1291 * REGSLEEP: fclk is off, voltage regulator is asleep
1292 * OFF: fclk is off, voltage regulator is off
Denis Karpovdd498ef2009-09-22 16:44:49 -07001293 *
1294 * Transition handlers return the timeout for the next state transition
1295 * or negative error.
1296 */
1297
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001298enum {ENABLED = 0, DISABLED, CARDSLEEP, REGSLEEP, OFF};
Denis Karpovdd498ef2009-09-22 16:44:49 -07001299
1300/* Handler for [ENABLED -> DISABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001301static int omap_hsmmc_enabled_to_disabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001302{
Denis Karpov70a33412009-09-22 16:44:59 -07001303 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001304 clk_disable(host->fclk);
1305 host->dpm_state = DISABLED;
1306
1307 dev_dbg(mmc_dev(host->mmc), "ENABLED -> DISABLED\n");
1308
1309 if (host->power_mode == MMC_POWER_OFF)
1310 return 0;
1311
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001312 return msecs_to_jiffies(OMAP_MMC_SLEEP_TIMEOUT);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001313}
1314
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001315/* Handler for [DISABLED -> REGSLEEP / CARDSLEEP] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001316static int omap_hsmmc_disabled_to_sleep(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001317{
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001318 int err, new_state;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001319
1320 if (!mmc_try_claim_host(host->mmc))
1321 return 0;
1322
1323 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001324 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001325 if (mmc_card_can_sleep(host->mmc)) {
1326 err = mmc_card_sleep(host->mmc);
1327 if (err < 0) {
1328 clk_disable(host->fclk);
1329 mmc_release_host(host->mmc);
1330 return err;
1331 }
1332 new_state = CARDSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001333 } else {
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001334 new_state = REGSLEEP;
Denis Karpov70a33412009-09-22 16:44:59 -07001335 }
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001336 if (mmc_slot(host).set_sleep)
1337 mmc_slot(host).set_sleep(host->dev, host->slot_id, 1, 0,
1338 new_state == CARDSLEEP);
1339 /* FIXME: turn off bus power and perhaps interrupts too */
1340 clk_disable(host->fclk);
1341 host->dpm_state = new_state;
1342
1343 mmc_release_host(host->mmc);
1344
1345 dev_dbg(mmc_dev(host->mmc), "DISABLED -> %s\n",
1346 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001347
1348 if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1349 mmc_slot(host).card_detect ||
1350 (mmc_slot(host).get_cover_state &&
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001351 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))
1352 return msecs_to_jiffies(OMAP_MMC_OFF_TIMEOUT);
1353
1354 return 0;
1355}
1356
1357/* Handler for [REGSLEEP / CARDSLEEP -> OFF] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001358static int omap_hsmmc_sleep_to_off(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001359{
1360 if (!mmc_try_claim_host(host->mmc))
1361 return 0;
1362
1363 if (!((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1364 mmc_slot(host).card_detect ||
1365 (mmc_slot(host).get_cover_state &&
1366 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))) {
1367 mmc_release_host(host->mmc);
1368 return 0;
Adrian Hunter623821f2009-09-22 16:44:51 -07001369 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001370
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001371 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
1372 host->vdd = 0;
1373 host->power_mode = MMC_POWER_OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001374
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001375 dev_dbg(mmc_dev(host->mmc), "%s -> OFF\n",
1376 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001377
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001378 host->dpm_state = OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001379
1380 mmc_release_host(host->mmc);
1381
1382 return 0;
1383}
1384
1385/* Handler for [DISABLED -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001386static int omap_hsmmc_disabled_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001387{
1388 int err;
1389
1390 err = clk_enable(host->fclk);
1391 if (err < 0)
1392 return err;
1393
Denis Karpov70a33412009-09-22 16:44:59 -07001394 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001395 host->dpm_state = ENABLED;
1396
1397 dev_dbg(mmc_dev(host->mmc), "DISABLED -> ENABLED\n");
1398
1399 return 0;
1400}
1401
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001402/* Handler for [SLEEP -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001403static int omap_hsmmc_sleep_to_enabled(struct omap_hsmmc_host *host)
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001404{
1405 if (!mmc_try_claim_host(host->mmc))
1406 return 0;
1407
1408 clk_enable(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001409 omap_hsmmc_context_restore(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001410 if (mmc_slot(host).set_sleep)
1411 mmc_slot(host).set_sleep(host->dev, host->slot_id, 0,
1412 host->vdd, host->dpm_state == CARDSLEEP);
1413 if (mmc_card_can_sleep(host->mmc))
1414 mmc_card_awake(host->mmc);
1415
1416 dev_dbg(mmc_dev(host->mmc), "%s -> ENABLED\n",
1417 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
1418
1419 host->dpm_state = ENABLED;
1420
1421 mmc_release_host(host->mmc);
1422
1423 return 0;
1424}
1425
Denis Karpovdd498ef2009-09-22 16:44:49 -07001426/* Handler for [OFF -> ENABLED] transition */
Denis Karpov70a33412009-09-22 16:44:59 -07001427static int omap_hsmmc_off_to_enabled(struct omap_hsmmc_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001428{
1429 clk_enable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001430
Denis Karpov70a33412009-09-22 16:44:59 -07001431 omap_hsmmc_context_restore(host);
1432 omap_hsmmc_conf_bus_power(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001433 mmc_power_restore_host(host->mmc);
1434
1435 host->dpm_state = ENABLED;
1436
1437 dev_dbg(mmc_dev(host->mmc), "OFF -> ENABLED\n");
1438
1439 return 0;
1440}
1441
1442/*
1443 * Bring MMC host to ENABLED from any other PM state.
1444 */
Denis Karpov70a33412009-09-22 16:44:59 -07001445static int omap_hsmmc_enable(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001446{
Denis Karpov70a33412009-09-22 16:44:59 -07001447 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001448
1449 switch (host->dpm_state) {
1450 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001451 return omap_hsmmc_disabled_to_enabled(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001452 case CARDSLEEP:
Adrian Hunter623821f2009-09-22 16:44:51 -07001453 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001454 return omap_hsmmc_sleep_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001455 case OFF:
Denis Karpov70a33412009-09-22 16:44:59 -07001456 return omap_hsmmc_off_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001457 default:
1458 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1459 return -EINVAL;
1460 }
1461}
1462
1463/*
1464 * Bring MMC host in PM state (one level deeper).
1465 */
Denis Karpov70a33412009-09-22 16:44:59 -07001466static int omap_hsmmc_disable(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001467{
Denis Karpov70a33412009-09-22 16:44:59 -07001468 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001469
1470 switch (host->dpm_state) {
1471 case ENABLED: {
1472 int delay;
1473
Denis Karpov70a33412009-09-22 16:44:59 -07001474 delay = omap_hsmmc_enabled_to_disabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001475 if (lazy || delay < 0)
1476 return delay;
1477 return 0;
1478 }
1479 case DISABLED:
Denis Karpov70a33412009-09-22 16:44:59 -07001480 return omap_hsmmc_disabled_to_sleep(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001481 case CARDSLEEP:
1482 case REGSLEEP:
Denis Karpov70a33412009-09-22 16:44:59 -07001483 return omap_hsmmc_sleep_to_off(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001484 default:
1485 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1486 return -EINVAL;
1487 }
1488}
1489
Denis Karpov70a33412009-09-22 16:44:59 -07001490static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001491{
Denis Karpov70a33412009-09-22 16:44:59 -07001492 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001493 int err;
1494
1495 err = clk_enable(host->fclk);
1496 if (err)
1497 return err;
1498 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
Denis Karpov70a33412009-09-22 16:44:59 -07001499 omap_hsmmc_context_restore(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001500 return 0;
1501}
1502
Denis Karpov70a33412009-09-22 16:44:59 -07001503static int omap_hsmmc_disable_fclk(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001504{
Denis Karpov70a33412009-09-22 16:44:59 -07001505 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001506
Denis Karpov70a33412009-09-22 16:44:59 -07001507 omap_hsmmc_context_save(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001508 clk_disable(host->fclk);
1509 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
1510 return 0;
1511}
1512
Denis Karpov70a33412009-09-22 16:44:59 -07001513static const struct mmc_host_ops omap_hsmmc_ops = {
1514 .enable = omap_hsmmc_enable_fclk,
1515 .disable = omap_hsmmc_disable_fclk,
1516 .request = omap_hsmmc_request,
1517 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001518 .get_cd = omap_hsmmc_get_cd,
1519 .get_ro = omap_hsmmc_get_ro,
1520 /* NYET -- enable_sdio_irq */
1521};
1522
Denis Karpov70a33412009-09-22 16:44:59 -07001523static const struct mmc_host_ops omap_hsmmc_ps_ops = {
1524 .enable = omap_hsmmc_enable,
1525 .disable = omap_hsmmc_disable,
1526 .request = omap_hsmmc_request,
1527 .set_ios = omap_hsmmc_set_ios,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001528 .get_cd = omap_hsmmc_get_cd,
1529 .get_ro = omap_hsmmc_get_ro,
1530 /* NYET -- enable_sdio_irq */
1531};
1532
Denis Karpovd900f712009-09-22 16:44:38 -07001533#ifdef CONFIG_DEBUG_FS
1534
Denis Karpov70a33412009-09-22 16:44:59 -07001535static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001536{
1537 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001538 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001539 int context_loss = 0;
1540
Denis Karpov70a33412009-09-22 16:44:59 -07001541 if (host->pdata->get_context_loss_count)
1542 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001543
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001544 seq_printf(s, "mmc%d:\n"
1545 " enabled:\t%d\n"
Denis Karpovdd498ef2009-09-22 16:44:49 -07001546 " dpm_state:\t%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001547 " nesting_cnt:\t%d\n"
Denis Karpov11dd62a2009-09-22 16:44:43 -07001548 " ctx_loss:\t%d:%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001549 "\nregs:\n",
Denis Karpovdd498ef2009-09-22 16:44:49 -07001550 mmc->index, mmc->enabled ? 1 : 0,
1551 host->dpm_state, mmc->nesting_cnt,
Denis Karpov11dd62a2009-09-22 16:44:43 -07001552 host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001553
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001554 if (host->suspended || host->dpm_state == OFF) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001555 seq_printf(s, "host suspended, can't read registers\n");
1556 return 0;
1557 }
1558
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001559 if (clk_enable(host->fclk) != 0) {
1560 seq_printf(s, "can't read the regs\n");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001561 return 0;
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001562 }
Denis Karpovd900f712009-09-22 16:44:38 -07001563
1564 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1565 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1566 seq_printf(s, "CON:\t\t0x%08x\n",
1567 OMAP_HSMMC_READ(host->base, CON));
1568 seq_printf(s, "HCTL:\t\t0x%08x\n",
1569 OMAP_HSMMC_READ(host->base, HCTL));
1570 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1571 OMAP_HSMMC_READ(host->base, SYSCTL));
1572 seq_printf(s, "IE:\t\t0x%08x\n",
1573 OMAP_HSMMC_READ(host->base, IE));
1574 seq_printf(s, "ISE:\t\t0x%08x\n",
1575 OMAP_HSMMC_READ(host->base, ISE));
1576 seq_printf(s, "CAPA:\t\t0x%08x\n",
1577 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001578
1579 clk_disable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001580
Denis Karpovd900f712009-09-22 16:44:38 -07001581 return 0;
1582}
1583
Denis Karpov70a33412009-09-22 16:44:59 -07001584static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001585{
Denis Karpov70a33412009-09-22 16:44:59 -07001586 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001587}
1588
1589static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001590 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001591 .read = seq_read,
1592 .llseek = seq_lseek,
1593 .release = single_release,
1594};
1595
Denis Karpov70a33412009-09-22 16:44:59 -07001596static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001597{
1598 if (mmc->debugfs_root)
1599 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1600 mmc, &mmc_regs_fops);
1601}
1602
1603#else
1604
Denis Karpov70a33412009-09-22 16:44:59 -07001605static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001606{
1607}
1608
1609#endif
1610
Denis Karpov70a33412009-09-22 16:44:59 -07001611static int __init omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001612{
1613 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1614 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001615 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001616 struct resource *res;
1617 int ret = 0, irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001618
1619 if (pdata == NULL) {
1620 dev_err(&pdev->dev, "Platform Data is missing\n");
1621 return -ENXIO;
1622 }
1623
1624 if (pdata->nr_slots == 0) {
1625 dev_err(&pdev->dev, "No Slots\n");
1626 return -ENXIO;
1627 }
1628
1629 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1630 irq = platform_get_irq(pdev, 0);
1631 if (res == NULL || irq < 0)
1632 return -ENXIO;
1633
1634 res = request_mem_region(res->start, res->end - res->start + 1,
1635 pdev->name);
1636 if (res == NULL)
1637 return -EBUSY;
1638
Denis Karpov70a33412009-09-22 16:44:59 -07001639 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001640 if (!mmc) {
1641 ret = -ENOMEM;
1642 goto err;
1643 }
1644
1645 host = mmc_priv(mmc);
1646 host->mmc = mmc;
1647 host->pdata = pdata;
1648 host->dev = &pdev->dev;
1649 host->use_dma = 1;
1650 host->dev->dma_mask = &pdata->dma_mask;
1651 host->dma_ch = -1;
1652 host->irq = irq;
1653 host->id = pdev->id;
1654 host->slot_id = 0;
1655 host->mapbase = res->start;
1656 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Huntera3621462009-09-22 16:44:42 -07001657 host->power_mode = -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001658
1659 platform_set_drvdata(pdev, host);
Denis Karpov70a33412009-09-22 16:44:59 -07001660 INIT_WORK(&host->mmc_carddetect_work, omap_hsmmc_detect);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001661
Denis Karpov191d1f12009-09-22 16:44:55 -07001662 if (mmc_slot(host).power_saving)
Denis Karpov70a33412009-09-22 16:44:59 -07001663 mmc->ops = &omap_hsmmc_ps_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001664 else
Denis Karpov70a33412009-09-22 16:44:59 -07001665 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001666
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001667 mmc->f_min = 400000;
1668 mmc->f_max = 52000000;
1669
1670 sema_init(&host->sem, 1);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001671 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001672
Russell King6f7607c2009-01-28 10:22:50 +00001673 host->iclk = clk_get(&pdev->dev, "ick");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001674 if (IS_ERR(host->iclk)) {
1675 ret = PTR_ERR(host->iclk);
1676 host->iclk = NULL;
1677 goto err1;
1678 }
Russell King6f7607c2009-01-28 10:22:50 +00001679 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001680 if (IS_ERR(host->fclk)) {
1681 ret = PTR_ERR(host->fclk);
1682 host->fclk = NULL;
1683 clk_put(host->iclk);
1684 goto err1;
1685 }
1686
Denis Karpov70a33412009-09-22 16:44:59 -07001687 omap_hsmmc_context_save(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001688
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001689 mmc->caps |= MMC_CAP_DISABLE;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001690 mmc_set_disable_delay(mmc, OMAP_MMC_DISABLED_TIMEOUT);
1691 /* we start off in DISABLED state */
1692 host->dpm_state = DISABLED;
1693
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001694 if (mmc_host_enable(host->mmc) != 0) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001695 clk_put(host->iclk);
1696 clk_put(host->fclk);
1697 goto err1;
1698 }
1699
1700 if (clk_enable(host->iclk) != 0) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001701 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001702 clk_put(host->iclk);
1703 clk_put(host->fclk);
1704 goto err1;
1705 }
1706
Adrian Hunter2bec0892009-09-22 16:45:02 -07001707 if (cpu_is_omap2430()) {
1708 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1709 /*
1710 * MMC can still work without debounce clock.
1711 */
1712 if (IS_ERR(host->dbclk))
1713 dev_warn(mmc_dev(host->mmc),
1714 "Failed to get debounce clock\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001715 else
Adrian Hunter2bec0892009-09-22 16:45:02 -07001716 host->got_dbclk = 1;
1717
1718 if (host->got_dbclk)
1719 if (clk_enable(host->dbclk) != 0)
1720 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
1721 " clk failed\n");
1722 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001723
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001724 /* Since we do only SG emulation, we can have as many segs
1725 * as we want. */
1726 mmc->max_phys_segs = 1024;
1727 mmc->max_hw_segs = 1024;
1728
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001729 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1730 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1731 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1732 mmc->max_seg_size = mmc->max_req_size;
1733
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001734 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
1735 MMC_CAP_WAIT_WHILE_BUSY;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001736
Denis Karpov191d1f12009-09-22 16:44:55 -07001737 if (mmc_slot(host).wires >= 8)
Jarkko Lavinen73153012008-11-21 16:49:54 +02001738 mmc->caps |= MMC_CAP_8_BIT_DATA;
Denis Karpov191d1f12009-09-22 16:44:55 -07001739 else if (mmc_slot(host).wires >= 4)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001740 mmc->caps |= MMC_CAP_4_BIT_DATA;
1741
Denis Karpov191d1f12009-09-22 16:44:55 -07001742 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001743 mmc->caps |= MMC_CAP_NONREMOVABLE;
1744
Denis Karpov70a33412009-09-22 16:44:59 -07001745 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001746
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001747 /* Select DMA lines */
1748 switch (host->id) {
1749 case OMAP_MMC1_DEVID:
1750 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
1751 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
1752 break;
1753 case OMAP_MMC2_DEVID:
1754 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
1755 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
1756 break;
1757 case OMAP_MMC3_DEVID:
1758 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
1759 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
1760 break;
1761 default:
1762 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
1763 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001764 }
1765
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001766 /* Request IRQ for MMC operations */
Denis Karpov70a33412009-09-22 16:44:59 -07001767 ret = request_irq(host->irq, omap_hsmmc_irq, IRQF_DISABLED,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001768 mmc_hostname(mmc), host);
1769 if (ret) {
1770 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1771 goto err_irq;
1772 }
1773
David Brownellb583f262009-05-28 14:04:03 -07001774 /* initialize power supplies, gpios, etc */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001775 if (pdata->init != NULL) {
1776 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07001777 dev_dbg(mmc_dev(host->mmc),
1778 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001779 goto err_irq_cd_init;
1780 }
1781 }
David Brownellb583f262009-05-28 14:04:03 -07001782 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001783
1784 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001785 if ((mmc_slot(host).card_detect_irq)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001786 ret = request_irq(mmc_slot(host).card_detect_irq,
Denis Karpov70a33412009-09-22 16:44:59 -07001787 omap_hsmmc_cd_handler,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001788 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
1789 | IRQF_DISABLED,
1790 mmc_hostname(mmc), host);
1791 if (ret) {
1792 dev_dbg(mmc_dev(host->mmc),
1793 "Unable to grab MMC CD IRQ\n");
1794 goto err_irq_cd;
1795 }
1796 }
1797
1798 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
1799 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
1800
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001801 mmc_host_lazy_disable(host->mmc);
1802
Adrian Hunterb62f6222009-09-22 16:45:01 -07001803 omap_hsmmc_protect_card(host);
1804
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001805 mmc_add_host(mmc);
1806
Denis Karpov191d1f12009-09-22 16:44:55 -07001807 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001808 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1809 if (ret < 0)
1810 goto err_slot_name;
1811 }
Denis Karpov191d1f12009-09-22 16:44:55 -07001812 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001813 ret = device_create_file(&mmc->class_dev,
1814 &dev_attr_cover_switch);
1815 if (ret < 0)
1816 goto err_cover_switch;
1817 }
1818
Denis Karpov70a33412009-09-22 16:44:59 -07001819 omap_hsmmc_debugfs(mmc);
Denis Karpovd900f712009-09-22 16:44:38 -07001820
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001821 return 0;
1822
1823err_cover_switch:
1824 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1825err_slot_name:
1826 mmc_remove_host(mmc);
1827err_irq_cd:
1828 free_irq(mmc_slot(host).card_detect_irq, host);
1829err_irq_cd_init:
1830 free_irq(host->irq, host);
1831err_irq:
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001832 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001833 clk_disable(host->iclk);
1834 clk_put(host->fclk);
1835 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001836 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001837 clk_disable(host->dbclk);
1838 clk_put(host->dbclk);
1839 }
1840
1841err1:
1842 iounmap(host->base);
1843err:
1844 dev_dbg(mmc_dev(host->mmc), "Probe Failed\n");
1845 release_mem_region(res->start, res->end - res->start + 1);
1846 if (host)
1847 mmc_free_host(mmc);
1848 return ret;
1849}
1850
Denis Karpov70a33412009-09-22 16:44:59 -07001851static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001852{
Denis Karpov70a33412009-09-22 16:44:59 -07001853 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001854 struct resource *res;
1855
1856 if (host) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001857 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001858 mmc_remove_host(host->mmc);
1859 if (host->pdata->cleanup)
1860 host->pdata->cleanup(&pdev->dev);
1861 free_irq(host->irq, host);
1862 if (mmc_slot(host).card_detect_irq)
1863 free_irq(mmc_slot(host).card_detect_irq, host);
1864 flush_scheduled_work();
1865
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001866 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001867 clk_disable(host->iclk);
1868 clk_put(host->fclk);
1869 clk_put(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001870 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001871 clk_disable(host->dbclk);
1872 clk_put(host->dbclk);
1873 }
1874
1875 mmc_free_host(host->mmc);
1876 iounmap(host->base);
1877 }
1878
1879 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1880 if (res)
1881 release_mem_region(res->start, res->end - res->start + 1);
1882 platform_set_drvdata(pdev, NULL);
1883
1884 return 0;
1885}
1886
1887#ifdef CONFIG_PM
Denis Karpov70a33412009-09-22 16:44:59 -07001888static int omap_hsmmc_suspend(struct platform_device *pdev, pm_message_t state)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001889{
1890 int ret = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001891 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001892
1893 if (host && host->suspended)
1894 return 0;
1895
1896 if (host) {
Adrian Huntera6b22402009-09-22 16:44:45 -07001897 host->suspended = 1;
1898 if (host->pdata->suspend) {
1899 ret = host->pdata->suspend(&pdev->dev,
1900 host->slot_id);
1901 if (ret) {
1902 dev_dbg(mmc_dev(host->mmc),
1903 "Unable to handle MMC board"
1904 " level suspend\n");
1905 host->suspended = 0;
1906 return ret;
1907 }
1908 }
1909 cancel_work_sync(&host->mmc_carddetect_work);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001910 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001911 ret = mmc_suspend_host(host->mmc, state);
1912 if (ret == 0) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001913 OMAP_HSMMC_WRITE(host->base, ISE, 0);
1914 OMAP_HSMMC_WRITE(host->base, IE, 0);
1915
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001916
Jarkko Lavinen0683af42009-03-12 15:30:58 +02001917 OMAP_HSMMC_WRITE(host->base, HCTL,
Denis Karpov191d1f12009-09-22 16:44:55 -07001918 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001919 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001920 clk_disable(host->iclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001921 if (host->got_dbclk)
1922 clk_disable(host->dbclk);
Adrian Huntera6b22402009-09-22 16:44:45 -07001923 } else {
1924 host->suspended = 0;
1925 if (host->pdata->resume) {
1926 ret = host->pdata->resume(&pdev->dev,
1927 host->slot_id);
1928 if (ret)
1929 dev_dbg(mmc_dev(host->mmc),
1930 "Unmask interrupt failed\n");
1931 }
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001932 mmc_host_disable(host->mmc);
Adrian Huntera6b22402009-09-22 16:44:45 -07001933 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001934
1935 }
1936 return ret;
1937}
1938
1939/* Routine to resume the MMC device */
Denis Karpov70a33412009-09-22 16:44:59 -07001940static int omap_hsmmc_resume(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001941{
1942 int ret = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001943 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001944
1945 if (host && !host->suspended)
1946 return 0;
1947
1948 if (host) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001949 ret = clk_enable(host->iclk);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001950 if (ret)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001951 goto clk_en_err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001952
Denis Karpov11dd62a2009-09-22 16:44:43 -07001953 if (mmc_host_enable(host->mmc) != 0) {
1954 clk_disable(host->iclk);
1955 goto clk_en_err;
1956 }
1957
Adrian Hunter2bec0892009-09-22 16:45:02 -07001958 if (host->got_dbclk)
1959 clk_enable(host->dbclk);
1960
Denis Karpov70a33412009-09-22 16:44:59 -07001961 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001962
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001963 if (host->pdata->resume) {
1964 ret = host->pdata->resume(&pdev->dev, host->slot_id);
1965 if (ret)
1966 dev_dbg(mmc_dev(host->mmc),
1967 "Unmask interrupt failed\n");
1968 }
1969
Adrian Hunterb62f6222009-09-22 16:45:01 -07001970 omap_hsmmc_protect_card(host);
1971
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001972 /* Notify the core to resume the host */
1973 ret = mmc_resume_host(host->mmc);
1974 if (ret == 0)
1975 host->suspended = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001976
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001977 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001978 }
1979
1980 return ret;
1981
1982clk_en_err:
1983 dev_dbg(mmc_dev(host->mmc),
1984 "Failed to enable MMC clocks during resume\n");
1985 return ret;
1986}
1987
1988#else
Denis Karpov70a33412009-09-22 16:44:59 -07001989#define omap_hsmmc_suspend NULL
1990#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001991#endif
1992
Denis Karpov70a33412009-09-22 16:44:59 -07001993static struct platform_driver omap_hsmmc_driver = {
1994 .remove = omap_hsmmc_remove,
1995 .suspend = omap_hsmmc_suspend,
1996 .resume = omap_hsmmc_resume,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001997 .driver = {
1998 .name = DRIVER_NAME,
1999 .owner = THIS_MODULE,
2000 },
2001};
2002
Denis Karpov70a33412009-09-22 16:44:59 -07002003static int __init omap_hsmmc_init(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002004{
2005 /* Register the MMC driver */
Denis Karpov70a33412009-09-22 16:44:59 -07002006 return platform_driver_register(&omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002007}
2008
Denis Karpov70a33412009-09-22 16:44:59 -07002009static void __exit omap_hsmmc_cleanup(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002010{
2011 /* Unregister MMC driver */
Denis Karpov70a33412009-09-22 16:44:59 -07002012 platform_driver_unregister(&omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002013}
2014
Denis Karpov70a33412009-09-22 16:44:59 -07002015module_init(omap_hsmmc_init);
2016module_exit(omap_hsmmc_cleanup);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002017
2018MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2019MODULE_LICENSE("GPL");
2020MODULE_ALIAS("platform:" DRIVER_NAME);
2021MODULE_AUTHOR("Texas Instruments Inc");