blob: e5501704b2db6e0272bb01f91b7dd81c7df403c6 [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>
Andy Shevchenkoac330f42011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
22#include <linux/seq_file.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010023#include <linux/interrupt.h>
24#include <linux/delay.h>
25#include <linux/dma-mapping.h>
26#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010027#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>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070031#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010032#include <linux/io.h>
33#include <linux/semaphore.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080034#include <linux/gpio.h>
35#include <linux/regulator/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053036#include <linux/pm_runtime.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070037#include <plat/dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010038#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070039#include <plat/board.h>
40#include <plat/mmc.h>
41#include <plat/cpu.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010042
43/* OMAP HSMMC Host Controller Registers */
44#define OMAP_HSMMC_SYSCONFIG 0x0010
Denis Karpov11dd62a2009-09-22 16:44:43 -070045#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010046#define OMAP_HSMMC_CON 0x002C
47#define OMAP_HSMMC_BLK 0x0104
48#define OMAP_HSMMC_ARG 0x0108
49#define OMAP_HSMMC_CMD 0x010C
50#define OMAP_HSMMC_RSP10 0x0110
51#define OMAP_HSMMC_RSP32 0x0114
52#define OMAP_HSMMC_RSP54 0x0118
53#define OMAP_HSMMC_RSP76 0x011C
54#define OMAP_HSMMC_DATA 0x0120
55#define OMAP_HSMMC_HCTL 0x0128
56#define OMAP_HSMMC_SYSCTL 0x012C
57#define OMAP_HSMMC_STAT 0x0130
58#define OMAP_HSMMC_IE 0x0134
59#define OMAP_HSMMC_ISE 0x0138
60#define OMAP_HSMMC_CAPA 0x0140
61
62#define VS18 (1 << 26)
63#define VS30 (1 << 25)
64#define SDVS18 (0x5 << 9)
65#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080066#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010067#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010068#define SDVSCLR 0xFFFFF1FF
69#define SDVSDET 0x00000400
70#define AUTOIDLE 0x1
71#define SDBP (1 << 8)
72#define DTO 0xe
73#define ICE 0x1
74#define ICS 0x2
75#define CEN (1 << 2)
76#define CLKD_MASK 0x0000FFC0
77#define CLKD_SHIFT 6
78#define DTO_MASK 0x000F0000
79#define DTO_SHIFT 16
80#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070081#define BWR_ENABLE (1 << 4)
82#define BRR_ENABLE (1 << 5)
Adrian Hunter93caf8e692010-08-11 14:17:48 -070083#define DTO_ENABLE (1 << 20)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010084#define INIT_STREAM (1 << 1)
85#define DP_SELECT (1 << 21)
86#define DDIR (1 << 4)
87#define DMA_EN 0x1
88#define MSBS (1 << 5)
89#define BCE (1 << 1)
90#define FOUR_BIT (1 << 1)
Jarkko Lavinen73153012008-11-21 16:49:54 +020091#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010092#define CC 0x1
93#define TC 0x02
94#define OD 0x1
95#define ERR (1 << 15)
96#define CMD_TIMEOUT (1 << 16)
97#define DATA_TIMEOUT (1 << 20)
98#define CMD_CRC (1 << 17)
99#define DATA_CRC (1 << 21)
100#define CARD_ERR (1 << 28)
101#define STAT_CLEAR 0xFFFFFFFF
102#define INIT_STREAM_CMD 0x00000000
103#define DUAL_VOLT_OCR_BIT 7
104#define SRC (1 << 25)
105#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700106#define SOFTRESET (1 << 1)
107#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100108
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530109#define MMC_AUTOSUSPEND_DELAY 100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100110#define MMC_TIMEOUT_MS 20
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400111#define OMAP_MMC_MIN_CLOCK 400000
112#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530113#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100114
115/*
116 * One controller can have multiple slots, like on some omap boards using
117 * omap.c controller driver. Luckily this is not currently done on any known
118 * omap_hsmmc.c device.
119 */
120#define mmc_slot(host) (host->pdata->slots[host->slot_id])
121
122/*
123 * MMC Host controller read/write API's
124 */
125#define OMAP_HSMMC_READ(base, reg) \
126 __raw_readl((base) + OMAP_HSMMC_##reg)
127
128#define OMAP_HSMMC_WRITE(base, reg, val) \
129 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
130
Per Forlin9782aff2011-07-01 18:55:23 +0200131struct omap_hsmmc_next {
132 unsigned int dma_len;
133 s32 cookie;
134};
135
Denis Karpov70a33412009-09-22 16:44:59 -0700136struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100137 struct device *dev;
138 struct mmc_host *mmc;
139 struct mmc_request *mrq;
140 struct mmc_command *cmd;
141 struct mmc_data *data;
142 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100143 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800144 /*
145 * vcc == configured supply
146 * vcc_aux == optional
147 * - MMC1, supply for DAT4..DAT7
148 * - MMC2/MMC2, external level shifter voltage supply, for
149 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
150 */
151 struct regulator *vcc;
152 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100153 void __iomem *base;
154 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700155 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100156 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200157 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100158 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700159 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100160 u32 *buffer;
161 u32 bytesleft;
162 int suspended;
163 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100164 int use_dma, dma_ch;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000165 int dma_line_tx, dma_line_rx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100166 int slot_id;
Adrian Hunter2bec0892009-09-22 16:45:02 -0700167 int got_dbclk;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200168 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700169 int context_loss;
Denis Karpovdd498ef2009-09-22 16:44:49 -0700170 int dpm_state;
Adrian Hunter623821f2009-09-22 16:44:51 -0700171 int vdd;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700172 int protect_card;
173 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800174 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700175 int req_in_progress;
Per Forlin9782aff2011-07-01 18:55:23 +0200176 struct omap_hsmmc_next next_data;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700177
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100178 struct omap_mmc_platform_data *pdata;
179};
180
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800181static int omap_hsmmc_card_detect(struct device *dev, int slot)
182{
183 struct omap_mmc_platform_data *mmc = dev->platform_data;
184
185 /* NOTE: assumes card detect signal is active-low */
186 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
187}
188
189static int omap_hsmmc_get_wp(struct device *dev, int slot)
190{
191 struct omap_mmc_platform_data *mmc = dev->platform_data;
192
193 /* NOTE: assumes write protect signal is active-high */
194 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
195}
196
197static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
198{
199 struct omap_mmc_platform_data *mmc = dev->platform_data;
200
201 /* NOTE: assumes card detect signal is active-low */
202 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
203}
204
205#ifdef CONFIG_PM
206
207static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
208{
209 struct omap_mmc_platform_data *mmc = dev->platform_data;
210
211 disable_irq(mmc->slots[0].card_detect_irq);
212 return 0;
213}
214
215static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
216{
217 struct omap_mmc_platform_data *mmc = dev->platform_data;
218
219 enable_irq(mmc->slots[0].card_detect_irq);
220 return 0;
221}
222
223#else
224
225#define omap_hsmmc_suspend_cdirq NULL
226#define omap_hsmmc_resume_cdirq NULL
227
228#endif
229
Adrian Hunterb702b102010-02-15 10:03:35 -0800230#ifdef CONFIG_REGULATOR
231
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500232static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800233 int vdd)
234{
235 struct omap_hsmmc_host *host =
236 platform_get_drvdata(to_platform_device(dev));
237 int ret = 0;
238
239 /*
240 * If we don't see a Vcc regulator, assume it's a fixed
241 * voltage always-on regulator.
242 */
243 if (!host->vcc)
244 return 0;
245
246 if (mmc_slot(host).before_set_reg)
247 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
248
249 /*
250 * Assume Vcc regulator is used only to power the card ... OMAP
251 * VDDS is used to power the pins, optionally with a transceiver to
252 * support cards using voltages other than VDDS (1.8V nominal). When a
253 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
254 *
255 * In some cases this regulator won't support enable/disable;
256 * e.g. it's a fixed rail for a WLAN chip.
257 *
258 * In other cases vcc_aux switches interface power. Example, for
259 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
260 * chips/cards need an interface voltage rail too.
261 */
262 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400263 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800264 /* Enable interface voltage rail, if needed */
265 if (ret == 0 && host->vcc_aux) {
266 ret = regulator_enable(host->vcc_aux);
267 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400268 ret = mmc_regulator_set_ocr(host->mmc,
269 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800270 }
271 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400272 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800273 if (host->vcc_aux)
274 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400275 if (!ret) {
276 /* Then proceed to shut down the local regulator */
277 ret = mmc_regulator_set_ocr(host->mmc,
278 host->vcc, 0);
279 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800280 }
281
282 if (mmc_slot(host).after_set_reg)
283 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
284
285 return ret;
286}
287
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800288static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
289{
290 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700291 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800292
Rajendra Nayak1cb9af42012-03-07 09:55:31 -0500293 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800294
295 reg = regulator_get(host->dev, "vmmc");
296 if (IS_ERR(reg)) {
297 dev_dbg(host->dev, "vmmc regulator missing\n");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800298 } else {
299 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700300 ocr_value = mmc_regulator_get_ocrmask(reg);
301 if (!mmc_slot(host).ocr_mask) {
302 mmc_slot(host).ocr_mask = ocr_value;
303 } else {
304 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500305 pr_err("MMC ocrmask %x is not supported\n",
306 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700307 mmc_slot(host).ocr_mask = 0;
308 return -EINVAL;
309 }
310 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800311
312 /* Allow an aux regulator */
313 reg = regulator_get(host->dev, "vmmc_aux");
314 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
315
Balaji T Kb1c1df72011-05-30 19:55:34 +0530316 /* For eMMC do not power off when not in sleep state */
317 if (mmc_slot(host).no_regulator_off_init)
318 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800319 /*
320 * UGLY HACK: workaround regulator framework bugs.
321 * When the bootloader leaves a supply active, it's
322 * initialized with zero usecount ... and we can't
323 * disable it without first enabling it. Until the
324 * framework is fixed, we need a workaround like this
325 * (which is safe for MMC, but not in general).
326 */
Adrian Huntere840ce12011-05-06 12:14:10 +0300327 if (regulator_is_enabled(host->vcc) > 0 ||
328 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
329 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
330
331 mmc_slot(host).set_power(host->dev, host->slot_id,
332 1, vdd);
333 mmc_slot(host).set_power(host->dev, host->slot_id,
334 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800335 }
336 }
337
338 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800339}
340
341static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
342{
343 regulator_put(host->vcc);
344 regulator_put(host->vcc_aux);
345 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800346}
347
Adrian Hunterb702b102010-02-15 10:03:35 -0800348static inline int omap_hsmmc_have_reg(void)
349{
350 return 1;
351}
352
353#else
354
355static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
356{
357 return -EINVAL;
358}
359
360static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
361{
362}
363
364static inline int omap_hsmmc_have_reg(void)
365{
366 return 0;
367}
368
369#endif
370
371static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
372{
373 int ret;
374
375 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800376 if (pdata->slots[0].cover)
377 pdata->slots[0].get_cover_state =
378 omap_hsmmc_get_cover_state;
379 else
380 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
381 pdata->slots[0].card_detect_irq =
382 gpio_to_irq(pdata->slots[0].switch_pin);
383 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
384 if (ret)
385 return ret;
386 ret = gpio_direction_input(pdata->slots[0].switch_pin);
387 if (ret)
388 goto err_free_sp;
389 } else
390 pdata->slots[0].switch_pin = -EINVAL;
391
392 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
393 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
394 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
395 if (ret)
396 goto err_free_cd;
397 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
398 if (ret)
399 goto err_free_wp;
400 } else
401 pdata->slots[0].gpio_wp = -EINVAL;
402
403 return 0;
404
405err_free_wp:
406 gpio_free(pdata->slots[0].gpio_wp);
407err_free_cd:
408 if (gpio_is_valid(pdata->slots[0].switch_pin))
409err_free_sp:
410 gpio_free(pdata->slots[0].switch_pin);
411 return ret;
412}
413
414static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
415{
416 if (gpio_is_valid(pdata->slots[0].gpio_wp))
417 gpio_free(pdata->slots[0].gpio_wp);
418 if (gpio_is_valid(pdata->slots[0].switch_pin))
419 gpio_free(pdata->slots[0].switch_pin);
420}
421
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100422/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300423 * Start clock to the card
424 */
425static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
426{
427 OMAP_HSMMC_WRITE(host->base, SYSCTL,
428 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
429}
430
431/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100432 * Stop clock to the card
433 */
Denis Karpov70a33412009-09-22 16:44:59 -0700434static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100435{
436 OMAP_HSMMC_WRITE(host->base, SYSCTL,
437 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
438 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
439 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
440}
441
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700442static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
443 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700444{
445 unsigned int irq_mask;
446
447 if (host->use_dma)
448 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
449 else
450 irq_mask = INT_EN_MASK;
451
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700452 /* Disable timeout for erases */
453 if (cmd->opcode == MMC_ERASE)
454 irq_mask &= ~DTO_ENABLE;
455
Adrian Hunterb4175772010-05-26 14:42:06 -0700456 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
457 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
458 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
459}
460
461static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
462{
463 OMAP_HSMMC_WRITE(host->base, ISE, 0);
464 OMAP_HSMMC_WRITE(host->base, IE, 0);
465 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
466}
467
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300468/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530469static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300470{
471 u16 dsor = 0;
472
473 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530474 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300475 if (dsor > 250)
476 dsor = 250;
477 }
478
479 return dsor;
480}
481
Andy Shevchenko5934df22011-05-06 12:14:06 +0300482static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
483{
484 struct mmc_ios *ios = &host->mmc->ios;
485 unsigned long regval;
486 unsigned long timeout;
487
488 dev_dbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
489
490 omap_hsmmc_stop_clock(host);
491
492 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
493 regval = regval & ~(CLKD_MASK | DTO_MASK);
Balaji TKd83b6e02011-12-20 15:12:00 +0530494 regval = regval | (calc_divisor(host, ios) << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300495 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
496 OMAP_HSMMC_WRITE(host->base, SYSCTL,
497 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
498
499 /* Wait till the ICS bit is set */
500 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
501 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
502 && time_before(jiffies, timeout))
503 cpu_relax();
504
505 omap_hsmmc_start_clock(host);
506}
507
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400508static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
509{
510 struct mmc_ios *ios = &host->mmc->ios;
511 u32 con;
512
513 con = OMAP_HSMMC_READ(host->base, CON);
514 switch (ios->bus_width) {
515 case MMC_BUS_WIDTH_8:
516 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
517 break;
518 case MMC_BUS_WIDTH_4:
519 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
520 OMAP_HSMMC_WRITE(host->base, HCTL,
521 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
522 break;
523 case MMC_BUS_WIDTH_1:
524 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
525 OMAP_HSMMC_WRITE(host->base, HCTL,
526 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
527 break;
528 }
529}
530
531static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
532{
533 struct mmc_ios *ios = &host->mmc->ios;
534 u32 con;
535
536 con = OMAP_HSMMC_READ(host->base, CON);
537 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
538 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
539 else
540 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
541}
542
Denis Karpov11dd62a2009-09-22 16:44:43 -0700543#ifdef CONFIG_PM
544
545/*
546 * Restore the MMC host context, if it was lost as result of a
547 * power state change.
548 */
Denis Karpov70a33412009-09-22 16:44:59 -0700549static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700550{
551 struct mmc_ios *ios = &host->mmc->ios;
552 struct omap_mmc_platform_data *pdata = host->pdata;
553 int context_loss = 0;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400554 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700555 unsigned long timeout;
556
557 if (pdata->get_context_loss_count) {
558 context_loss = pdata->get_context_loss_count(host->dev);
559 if (context_loss < 0)
560 return 1;
561 }
562
563 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
564 context_loss == host->context_loss ? "not " : "");
565 if (host->context_loss == context_loss)
566 return 1;
567
568 /* Wait for hardware reset */
569 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
570 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
571 && time_before(jiffies, timeout))
572 ;
573
574 /* Do software reset */
575 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
576 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
577 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
578 && time_before(jiffies, timeout))
579 ;
580
581 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
582 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
583
Balaji T Kc2200ef2012-03-07 09:55:30 -0500584 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700585 if (host->power_mode != MMC_POWER_OFF &&
586 (1 << ios->vdd) <= MMC_VDD_23_24)
587 hctl = SDVS18;
588 else
589 hctl = SDVS30;
590 capa = VS30 | VS18;
591 } else {
592 hctl = SDVS18;
593 capa = VS18;
594 }
595
596 OMAP_HSMMC_WRITE(host->base, HCTL,
597 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
598
599 OMAP_HSMMC_WRITE(host->base, CAPA,
600 OMAP_HSMMC_READ(host->base, CAPA) | capa);
601
602 OMAP_HSMMC_WRITE(host->base, HCTL,
603 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
604
605 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
606 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
607 && time_before(jiffies, timeout))
608 ;
609
Adrian Hunterb4175772010-05-26 14:42:06 -0700610 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700611
612 /* Do not initialize card-specific things if the power is off */
613 if (host->power_mode == MMC_POWER_OFF)
614 goto out;
615
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400616 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700617
Andy Shevchenko5934df22011-05-06 12:14:06 +0300618 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700619
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400620 omap_hsmmc_set_bus_mode(host);
621
Denis Karpov11dd62a2009-09-22 16:44:43 -0700622out:
623 host->context_loss = context_loss;
624
625 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
626 return 0;
627}
628
629/*
630 * Save the MMC host context (store the number of power state changes so far).
631 */
Denis Karpov70a33412009-09-22 16:44:59 -0700632static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700633{
634 struct omap_mmc_platform_data *pdata = host->pdata;
635 int context_loss;
636
637 if (pdata->get_context_loss_count) {
638 context_loss = pdata->get_context_loss_count(host->dev);
639 if (context_loss < 0)
640 return;
641 host->context_loss = context_loss;
642 }
643}
644
645#else
646
Denis Karpov70a33412009-09-22 16:44:59 -0700647static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700648{
649 return 0;
650}
651
Denis Karpov70a33412009-09-22 16:44:59 -0700652static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700653{
654}
655
656#endif
657
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100658/*
659 * Send init stream sequence to card
660 * before sending IDLE command
661 */
Denis Karpov70a33412009-09-22 16:44:59 -0700662static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100663{
664 int reg = 0;
665 unsigned long timeout;
666
Adrian Hunterb62f6222009-09-22 16:45:01 -0700667 if (host->protect_card)
668 return;
669
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100670 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700671
672 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100673 OMAP_HSMMC_WRITE(host->base, CON,
674 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
675 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
676
677 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
678 while ((reg != CC) && time_before(jiffies, timeout))
679 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
680
681 OMAP_HSMMC_WRITE(host->base, CON,
682 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700683
684 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
685 OMAP_HSMMC_READ(host->base, STAT);
686
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100687 enable_irq(host->irq);
688}
689
690static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700691int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100692{
693 int r = 1;
694
Denis Karpov191d1f12009-09-22 16:44:55 -0700695 if (mmc_slot(host).get_cover_state)
696 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100697 return r;
698}
699
700static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700701omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100702 char *buf)
703{
704 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700705 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100706
Denis Karpov70a33412009-09-22 16:44:59 -0700707 return sprintf(buf, "%s\n",
708 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100709}
710
Denis Karpov70a33412009-09-22 16:44:59 -0700711static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100712
713static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700714omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100715 char *buf)
716{
717 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700718 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100719
Denis Karpov191d1f12009-09-22 16:44:55 -0700720 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100721}
722
Denis Karpov70a33412009-09-22 16:44:59 -0700723static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100724
725/*
726 * Configure the response type and send the cmd.
727 */
728static void
Denis Karpov70a33412009-09-22 16:44:59 -0700729omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100730 struct mmc_data *data)
731{
732 int cmdreg = 0, resptype = 0, cmdtype = 0;
733
734 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
735 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
736 host->cmd = cmd;
737
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700738 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100739
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200740 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100741 if (cmd->flags & MMC_RSP_PRESENT) {
742 if (cmd->flags & MMC_RSP_136)
743 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200744 else if (cmd->flags & MMC_RSP_BUSY) {
745 resptype = 3;
746 host->response_busy = 1;
747 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100748 resptype = 2;
749 }
750
751 /*
752 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
753 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
754 * a val of 0x3, rest 0x0.
755 */
756 if (cmd == host->mrq->stop)
757 cmdtype = 0x3;
758
759 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
760
761 if (data) {
762 cmdreg |= DP_SELECT | MSBS | BCE;
763 if (data->flags & MMC_DATA_READ)
764 cmdreg |= DDIR;
765 else
766 cmdreg &= ~(DDIR);
767 }
768
769 if (host->use_dma)
770 cmdreg |= DMA_EN;
771
Adrian Hunterb4175772010-05-26 14:42:06 -0700772 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700773
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100774 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
775 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
776}
777
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200778static int
Denis Karpov70a33412009-09-22 16:44:59 -0700779omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200780{
781 if (data->flags & MMC_DATA_WRITE)
782 return DMA_TO_DEVICE;
783 else
784 return DMA_FROM_DEVICE;
785}
786
Adrian Hunterb4175772010-05-26 14:42:06 -0700787static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
788{
789 int dma_ch;
790
791 spin_lock(&host->irq_lock);
792 host->req_in_progress = 0;
793 dma_ch = host->dma_ch;
794 spin_unlock(&host->irq_lock);
795
796 omap_hsmmc_disable_irq(host);
797 /* Do not complete the request if DMA is still in progress */
798 if (mrq->data && host->use_dma && dma_ch != -1)
799 return;
800 host->mrq = NULL;
801 mmc_request_done(host->mmc, mrq);
802}
803
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100804/*
805 * Notify the transfer complete to MMC core
806 */
807static void
Denis Karpov70a33412009-09-22 16:44:59 -0700808omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100809{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200810 if (!data) {
811 struct mmc_request *mrq = host->mrq;
812
Adrian Hunter23050102009-09-22 16:44:57 -0700813 /* TC before CC from CMD6 - don't know why, but it happens */
814 if (host->cmd && host->cmd->opcode == 6 &&
815 host->response_busy) {
816 host->response_busy = 0;
817 return;
818 }
819
Adrian Hunterb4175772010-05-26 14:42:06 -0700820 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200821 return;
822 }
823
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100824 host->data = NULL;
825
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100826 if (!data->error)
827 data->bytes_xfered += data->blocks * (data->blksz);
828 else
829 data->bytes_xfered = 0;
830
831 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700832 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100833 return;
834 }
Denis Karpov70a33412009-09-22 16:44:59 -0700835 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100836}
837
838/*
839 * Notify the core about command completion
840 */
841static void
Denis Karpov70a33412009-09-22 16:44:59 -0700842omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100843{
844 host->cmd = NULL;
845
846 if (cmd->flags & MMC_RSP_PRESENT) {
847 if (cmd->flags & MMC_RSP_136) {
848 /* response type 2 */
849 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
850 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
851 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
852 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
853 } else {
854 /* response types 1, 1b, 3, 4, 5, 6 */
855 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
856 }
857 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700858 if ((host->data == NULL && !host->response_busy) || cmd->error)
859 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100860}
861
862/*
863 * DMA clean up for command errors
864 */
Denis Karpov70a33412009-09-22 16:44:59 -0700865static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100866{
Adrian Hunterb4175772010-05-26 14:42:06 -0700867 int dma_ch;
868
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200869 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100870
Adrian Hunterb4175772010-05-26 14:42:06 -0700871 spin_lock(&host->irq_lock);
872 dma_ch = host->dma_ch;
873 host->dma_ch = -1;
874 spin_unlock(&host->irq_lock);
875
876 if (host->use_dma && dma_ch != -1) {
Per Forlina9120c32011-06-17 20:14:21 +0200877 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg,
878 host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700879 omap_hsmmc_get_dma_dir(host, host->data));
Adrian Hunterb4175772010-05-26 14:42:06 -0700880 omap_free_dma(dma_ch);
Per Forlin053bf342011-11-07 21:55:11 +0530881 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100882 }
883 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100884}
885
886/*
887 * Readable error output
888 */
889#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300890static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100891{
892 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700893 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300894 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
895 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
896 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
897 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100898 };
899 char res[256];
900 char *buf = res;
901 int len, i;
902
903 len = sprintf(buf, "MMC IRQ 0x%x :", status);
904 buf += len;
905
Denis Karpov70a33412009-09-22 16:44:59 -0700906 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100907 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700908 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100909 buf += len;
910 }
911
912 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
913}
Adrian Hunter699b9582011-05-06 12:14:01 +0300914#else
915static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
916 u32 status)
917{
918}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100919#endif /* CONFIG_MMC_DEBUG */
920
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100921/*
922 * MMC controller internal state machines reset
923 *
924 * Used to reset command or data internal state machines, using respectively
925 * SRC or SRD bit of SYSCTL register
926 * Can be called from interrupt context
927 */
Denis Karpov70a33412009-09-22 16:44:59 -0700928static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
929 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100930{
931 unsigned long i = 0;
932 unsigned long limit = (loops_per_jiffy *
933 msecs_to_jiffies(MMC_TIMEOUT_MS));
934
935 OMAP_HSMMC_WRITE(host->base, SYSCTL,
936 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
937
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700938 /*
939 * OMAP4 ES2 and greater has an updated reset logic.
940 * Monitor a 0->1 transition first
941 */
942 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -0500943 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700944 && (i++ < limit))
945 cpu_relax();
946 }
947 i = 0;
948
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100949 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
950 (i++ < limit))
951 cpu_relax();
952
953 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
954 dev_err(mmc_dev(host->mmc),
955 "Timeout waiting on controller reset in %s\n",
956 __func__);
957}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100958
Adrian Hunterb4175772010-05-26 14:42:06 -0700959static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100960{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100961 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -0700962 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100963
Adrian Hunterb4175772010-05-26 14:42:06 -0700964 if (!host->req_in_progress) {
965 do {
966 OMAP_HSMMC_WRITE(host->base, STAT, status);
967 /* Flush posted write */
968 status = OMAP_HSMMC_READ(host->base, STAT);
969 } while (status & INT_EN_MASK);
970 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100971 }
972
973 data = host->data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100974 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
975
976 if (status & ERR) {
Adrian Hunter699b9582011-05-06 12:14:01 +0300977 omap_hsmmc_dbg_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100978 if ((status & CMD_TIMEOUT) ||
979 (status & CMD_CRC)) {
980 if (host->cmd) {
981 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -0700982 omap_hsmmc_reset_controller_fsm(host,
983 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100984 host->cmd->error = -ETIMEDOUT;
985 } else {
986 host->cmd->error = -EILSEQ;
987 }
988 end_cmd = 1;
989 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200990 if (host->data || host->response_busy) {
991 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -0700992 omap_hsmmc_dma_cleanup(host,
993 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200994 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -0700995 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -0800996 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100997 }
998 if ((status & DATA_TIMEOUT) ||
999 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001000 if (host->data || host->response_busy) {
1001 int err = (status & DATA_TIMEOUT) ?
1002 -ETIMEDOUT : -EILSEQ;
1003
1004 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001005 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001006 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001007 host->mrq->cmd->error = err;
1008 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001009 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001010 end_trans = 1;
1011 }
1012 }
1013 if (status & CARD_ERR) {
1014 dev_dbg(mmc_dev(host->mmc),
1015 "Ignoring card err CMD%d\n", host->cmd->opcode);
1016 if (host->cmd)
1017 end_cmd = 1;
1018 if (host->data)
1019 end_trans = 1;
1020 }
1021 }
1022
1023 OMAP_HSMMC_WRITE(host->base, STAT, status);
1024
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001025 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001026 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001027 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001028 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001029}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001030
Adrian Hunterb4175772010-05-26 14:42:06 -07001031/*
1032 * MMC controller IRQ handler
1033 */
1034static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1035{
1036 struct omap_hsmmc_host *host = dev_id;
1037 int status;
1038
1039 status = OMAP_HSMMC_READ(host->base, STAT);
1040 do {
1041 omap_hsmmc_do_irq(host, status);
1042 /* Flush posted write */
1043 status = OMAP_HSMMC_READ(host->base, STAT);
1044 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001045
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001046 return IRQ_HANDLED;
1047}
1048
Denis Karpov70a33412009-09-22 16:44:59 -07001049static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001050{
1051 unsigned long i;
1052
1053 OMAP_HSMMC_WRITE(host->base, HCTL,
1054 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1055 for (i = 0; i < loops_per_jiffy; i++) {
1056 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1057 break;
1058 cpu_relax();
1059 }
1060}
1061
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001062/*
David Brownelleb250822009-02-17 14:49:01 -08001063 * Switch MMC interface voltage ... only relevant for MMC1.
1064 *
1065 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1066 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1067 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001068 */
Denis Karpov70a33412009-09-22 16:44:59 -07001069static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001070{
1071 u32 reg_val = 0;
1072 int ret;
1073
1074 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301075 pm_runtime_put_sync(host->dev);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001076 if (host->got_dbclk)
1077 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001078
1079 /* Turn the power off */
1080 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001081
1082 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001083 if (!ret)
1084 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1085 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301086 pm_runtime_get_sync(host->dev);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001087 if (host->got_dbclk)
1088 clk_enable(host->dbclk);
1089
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001090 if (ret != 0)
1091 goto err;
1092
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001093 OMAP_HSMMC_WRITE(host->base, HCTL,
1094 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1095 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001096
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001097 /*
1098 * If a MMC dual voltage card is detected, the set_ios fn calls
1099 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001100 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001101 *
David Brownelleb250822009-02-17 14:49:01 -08001102 * Cope with a bit of slop in the range ... per data sheets:
1103 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1104 * but recommended values are 1.71V to 1.89V
1105 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1106 * but recommended values are 2.7V to 3.3V
1107 *
1108 * Board setup code shouldn't permit anything very out-of-range.
1109 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1110 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001111 */
David Brownelleb250822009-02-17 14:49:01 -08001112 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001113 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001114 else
1115 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001116
1117 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001118 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001119
1120 return 0;
1121err:
1122 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1123 return ret;
1124}
1125
Adrian Hunterb62f6222009-09-22 16:45:01 -07001126/* Protect the card while the cover is open */
1127static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1128{
1129 if (!mmc_slot(host).get_cover_state)
1130 return;
1131
1132 host->reqs_blocked = 0;
1133 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1134 if (host->protect_card) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301135 pr_info("%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001136 "card is now accessible\n",
1137 mmc_hostname(host->mmc));
1138 host->protect_card = 0;
1139 }
1140 } else {
1141 if (!host->protect_card) {
Linus Torvalds3f8ddb02011-11-03 08:05:35 -07001142 pr_info("%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001143 "card is now inaccessible\n",
1144 mmc_hostname(host->mmc));
1145 host->protect_card = 1;
1146 }
1147 }
1148}
1149
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001150/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001151 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001152 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001153static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001154{
NeilBrown7efab4f2011-12-30 12:35:13 +11001155 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001156 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001157 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001158
Adrian Huntera6b22402009-09-22 16:44:45 -07001159 if (host->suspended)
NeilBrown7efab4f2011-12-30 12:35:13 +11001160 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001161
1162 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001163
Denis Karpov191d1f12009-09-22 16:44:55 -07001164 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001165 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001166 else {
1167 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001168 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001169 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001170
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001171 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001172 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001173 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001174 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001175 return IRQ_HANDLED;
1176}
1177
Denis Karpov70a33412009-09-22 16:44:59 -07001178static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001179 struct mmc_data *data)
1180{
1181 int sync_dev;
1182
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001183 if (data->flags & MMC_DATA_WRITE)
1184 sync_dev = host->dma_line_tx;
1185 else
1186 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001187 return sync_dev;
1188}
1189
Denis Karpov70a33412009-09-22 16:44:59 -07001190static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001191 struct mmc_data *data,
1192 struct scatterlist *sgl)
1193{
1194 int blksz, nblk, dma_ch;
1195
1196 dma_ch = host->dma_ch;
1197 if (data->flags & MMC_DATA_WRITE) {
1198 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1199 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1200 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1201 sg_dma_address(sgl), 0, 0);
1202 } else {
1203 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -07001204 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001205 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1206 sg_dma_address(sgl), 0, 0);
1207 }
1208
1209 blksz = host->data->blksz;
1210 nblk = sg_dma_len(sgl) / blksz;
1211
1212 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1213 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -07001214 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001215 !(data->flags & MMC_DATA_WRITE));
1216
1217 omap_start_dma(dma_ch);
1218}
1219
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001220/*
1221 * DMA call back function
1222 */
Adrian Hunterb4175772010-05-26 14:42:06 -07001223static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001224{
Adrian Hunterb4175772010-05-26 14:42:06 -07001225 struct omap_hsmmc_host *host = cb_data;
Adrian Hunter770d7432011-05-06 12:14:11 +03001226 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001227 int dma_ch, req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001228
Venkatraman Sf3584e52010-08-10 18:01:54 -07001229 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1230 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1231 ch_status);
1232 return;
1233 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001234
Adrian Hunterb4175772010-05-26 14:42:06 -07001235 spin_lock(&host->irq_lock);
1236 if (host->dma_ch < 0) {
1237 spin_unlock(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001238 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001239 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001240
Adrian Hunter770d7432011-05-06 12:14:11 +03001241 data = host->mrq->data;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001242 host->dma_sg_idx++;
1243 if (host->dma_sg_idx < host->dma_len) {
1244 /* Fire up the next transfer. */
Adrian Hunterb4175772010-05-26 14:42:06 -07001245 omap_hsmmc_config_dma_params(host, data,
1246 data->sg + host->dma_sg_idx);
1247 spin_unlock(&host->irq_lock);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001248 return;
1249 }
1250
Per Forlin9782aff2011-07-01 18:55:23 +02001251 if (!data->host_cookie)
1252 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1253 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001254
1255 req_in_progress = host->req_in_progress;
1256 dma_ch = host->dma_ch;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001257 host->dma_ch = -1;
Adrian Hunterb4175772010-05-26 14:42:06 -07001258 spin_unlock(&host->irq_lock);
1259
1260 omap_free_dma(dma_ch);
1261
1262 /* If DMA has finished after TC, complete the request */
1263 if (!req_in_progress) {
1264 struct mmc_request *mrq = host->mrq;
1265
1266 host->mrq = NULL;
1267 mmc_request_done(host->mmc, mrq);
1268 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001269}
1270
Per Forlin9782aff2011-07-01 18:55:23 +02001271static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1272 struct mmc_data *data,
1273 struct omap_hsmmc_next *next)
1274{
1275 int dma_len;
1276
1277 if (!next && data->host_cookie &&
1278 data->host_cookie != host->next_data.cookie) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301279 pr_warning("[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001280 " host->next_data.cookie %d\n",
1281 __func__, data->host_cookie, host->next_data.cookie);
1282 data->host_cookie = 0;
1283 }
1284
1285 /* Check if next job is already prepared */
1286 if (next ||
1287 (!next && data->host_cookie != host->next_data.cookie)) {
1288 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
1289 data->sg_len,
1290 omap_hsmmc_get_dma_dir(host, data));
1291
1292 } else {
1293 dma_len = host->next_data.dma_len;
1294 host->next_data.dma_len = 0;
1295 }
1296
1297
1298 if (dma_len == 0)
1299 return -EINVAL;
1300
1301 if (next) {
1302 next->dma_len = dma_len;
1303 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1304 } else
1305 host->dma_len = dma_len;
1306
1307 return 0;
1308}
1309
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001310/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001311 * Routine to configure and start DMA for the MMC card
1312 */
Denis Karpov70a33412009-09-22 16:44:59 -07001313static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1314 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001315{
Adrian Hunterb4175772010-05-26 14:42:06 -07001316 int dma_ch = 0, ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001317 struct mmc_data *data = req->data;
1318
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001319 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001320 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001321 struct scatterlist *sgl;
1322
1323 sgl = data->sg + i;
1324 if (sgl->length % data->blksz)
1325 return -EINVAL;
1326 }
1327 if ((data->blksz % 4) != 0)
1328 /* REVISIT: The MMC buffer increments only when MSB is written.
1329 * Return error for blksz which is non multiple of four.
1330 */
1331 return -EINVAL;
1332
Adrian Hunterb4175772010-05-26 14:42:06 -07001333 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001334
Denis Karpov70a33412009-09-22 16:44:59 -07001335 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1336 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001337 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001338 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001339 "%s: omap_request_dma() failed with %d\n",
1340 mmc_hostname(host->mmc), ret);
1341 return ret;
1342 }
Per Forlin9782aff2011-07-01 18:55:23 +02001343 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL);
1344 if (ret)
1345 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001346
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001347 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001348 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001349
Denis Karpov70a33412009-09-22 16:44:59 -07001350 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001351
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001352 return 0;
1353}
1354
Denis Karpov70a33412009-09-22 16:44:59 -07001355static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001356 unsigned int timeout_ns,
1357 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001358{
1359 unsigned int timeout, cycle_ns;
1360 uint32_t reg, clkd, dto = 0;
1361
1362 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1363 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1364 if (clkd == 0)
1365 clkd = 1;
1366
1367 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001368 timeout = timeout_ns / cycle_ns;
1369 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001370 if (timeout) {
1371 while ((timeout & 0x80000000) == 0) {
1372 dto += 1;
1373 timeout <<= 1;
1374 }
1375 dto = 31 - dto;
1376 timeout <<= 1;
1377 if (timeout && dto)
1378 dto += 1;
1379 if (dto >= 13)
1380 dto -= 13;
1381 else
1382 dto = 0;
1383 if (dto > 14)
1384 dto = 14;
1385 }
1386
1387 reg &= ~DTO_MASK;
1388 reg |= dto << DTO_SHIFT;
1389 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1390}
1391
1392/*
1393 * Configure block length for MMC/SD cards and initiate the transfer.
1394 */
1395static int
Denis Karpov70a33412009-09-22 16:44:59 -07001396omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001397{
1398 int ret;
1399 host->data = req->data;
1400
1401 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001402 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001403 /*
1404 * Set an arbitrary 100ms data timeout for commands with
1405 * busy signal.
1406 */
1407 if (req->cmd->flags & MMC_RSP_BUSY)
1408 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001409 return 0;
1410 }
1411
1412 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1413 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001414 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001415
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001416 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001417 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001418 if (ret != 0) {
1419 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1420 return ret;
1421 }
1422 }
1423 return 0;
1424}
1425
Per Forlin9782aff2011-07-01 18:55:23 +02001426static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1427 int err)
1428{
1429 struct omap_hsmmc_host *host = mmc_priv(mmc);
1430 struct mmc_data *data = mrq->data;
1431
1432 if (host->use_dma) {
Per Forlin053bf342011-11-07 21:55:11 +05301433 if (data->host_cookie)
1434 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
1435 data->sg_len,
1436 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001437 data->host_cookie = 0;
1438 }
1439}
1440
1441static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1442 bool is_first_req)
1443{
1444 struct omap_hsmmc_host *host = mmc_priv(mmc);
1445
1446 if (mrq->data->host_cookie) {
1447 mrq->data->host_cookie = 0;
1448 return ;
1449 }
1450
1451 if (host->use_dma)
1452 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
1453 &host->next_data))
1454 mrq->data->host_cookie = 0;
1455}
1456
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001457/*
1458 * Request function. for read/write operation
1459 */
Denis Karpov70a33412009-09-22 16:44:59 -07001460static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001461{
Denis Karpov70a33412009-09-22 16:44:59 -07001462 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001463 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001464
Adrian Hunterb4175772010-05-26 14:42:06 -07001465 BUG_ON(host->req_in_progress);
1466 BUG_ON(host->dma_ch != -1);
1467 if (host->protect_card) {
1468 if (host->reqs_blocked < 3) {
1469 /*
1470 * Ensure the controller is left in a consistent
1471 * state by resetting the command and data state
1472 * machines.
1473 */
1474 omap_hsmmc_reset_controller_fsm(host, SRD);
1475 omap_hsmmc_reset_controller_fsm(host, SRC);
1476 host->reqs_blocked += 1;
1477 }
1478 req->cmd->error = -EBADF;
1479 if (req->data)
1480 req->data->error = -EBADF;
1481 req->cmd->retries = 0;
1482 mmc_request_done(mmc, req);
1483 return;
1484 } else if (host->reqs_blocked)
1485 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001486 WARN_ON(host->mrq != NULL);
1487 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001488 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001489 if (err) {
1490 req->cmd->error = err;
1491 if (req->data)
1492 req->data->error = err;
1493 host->mrq = NULL;
1494 mmc_request_done(mmc, req);
1495 return;
1496 }
1497
Denis Karpov70a33412009-09-22 16:44:59 -07001498 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001499}
1500
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001501/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001502static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001503{
Denis Karpov70a33412009-09-22 16:44:59 -07001504 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001505 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001506
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301507 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001508
Adrian Huntera3621462009-09-22 16:44:42 -07001509 if (ios->power_mode != host->power_mode) {
1510 switch (ios->power_mode) {
1511 case MMC_POWER_OFF:
1512 mmc_slot(host).set_power(host->dev, host->slot_id,
1513 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001514 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001515 break;
1516 case MMC_POWER_UP:
1517 mmc_slot(host).set_power(host->dev, host->slot_id,
1518 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001519 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001520 break;
1521 case MMC_POWER_ON:
1522 do_send_init_stream = 1;
1523 break;
1524 }
1525 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001526 }
1527
Denis Karpovdd498ef2009-09-22 16:44:49 -07001528 /* FIXME: set registers based only on changes to ios */
1529
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001530 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001531
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301532 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001533 /* Only MMC1 can interface at 3V without some flavor
1534 * of external transceiver; but they all handle 1.8V.
1535 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001536 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1537 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1538 /*
1539 * The mmc_select_voltage fn of the core does
1540 * not seem to set the power_mode to
1541 * MMC_POWER_UP upon recalculating the voltage.
1542 * vdd 1.8v.
1543 */
Denis Karpov70a33412009-09-22 16:44:59 -07001544 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1545 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001546 "Switch operation failed\n");
1547 }
1548 }
1549
Andy Shevchenko5934df22011-05-06 12:14:06 +03001550 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001551
Adrian Huntera3621462009-09-22 16:44:42 -07001552 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001553 send_init_stream(host);
1554
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001555 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001556
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301557 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001558}
1559
1560static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1561{
Denis Karpov70a33412009-09-22 16:44:59 -07001562 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001563
Denis Karpov191d1f12009-09-22 16:44:55 -07001564 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001565 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001566 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001567}
1568
1569static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1570{
Denis Karpov70a33412009-09-22 16:44:59 -07001571 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001572
Denis Karpov191d1f12009-09-22 16:44:55 -07001573 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001574 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001575 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001576}
1577
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001578static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1579{
1580 struct omap_hsmmc_host *host = mmc_priv(mmc);
1581
1582 if (mmc_slot(host).init_card)
1583 mmc_slot(host).init_card(card);
1584}
1585
Denis Karpov70a33412009-09-22 16:44:59 -07001586static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001587{
1588 u32 hctl, capa, value;
1589
1590 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301591 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001592 hctl = SDVS30;
1593 capa = VS30 | VS18;
1594 } else {
1595 hctl = SDVS18;
1596 capa = VS18;
1597 }
1598
1599 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1600 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1601
1602 value = OMAP_HSMMC_READ(host->base, CAPA);
1603 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1604
1605 /* Set the controller to AUTO IDLE mode */
1606 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1607 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1608
1609 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001610 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001611}
1612
Denis Karpov70a33412009-09-22 16:44:59 -07001613static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001614{
Denis Karpov70a33412009-09-22 16:44:59 -07001615 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001616
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301617 pm_runtime_get_sync(host->dev);
1618
Denis Karpovdd498ef2009-09-22 16:44:49 -07001619 return 0;
1620}
1621
Denis Karpov70a33412009-09-22 16:44:59 -07001622static int omap_hsmmc_disable_fclk(struct mmc_host *mmc, int lazy)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001623{
Denis Karpov70a33412009-09-22 16:44:59 -07001624 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001625
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301626 pm_runtime_mark_last_busy(host->dev);
1627 pm_runtime_put_autosuspend(host->dev);
1628
Denis Karpovdd498ef2009-09-22 16:44:49 -07001629 return 0;
1630}
1631
Denis Karpov70a33412009-09-22 16:44:59 -07001632static const struct mmc_host_ops omap_hsmmc_ops = {
1633 .enable = omap_hsmmc_enable_fclk,
1634 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001635 .post_req = omap_hsmmc_post_req,
1636 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001637 .request = omap_hsmmc_request,
1638 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001639 .get_cd = omap_hsmmc_get_cd,
1640 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001641 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001642 /* NYET -- enable_sdio_irq */
1643};
1644
Denis Karpovd900f712009-09-22 16:44:38 -07001645#ifdef CONFIG_DEBUG_FS
1646
Denis Karpov70a33412009-09-22 16:44:59 -07001647static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001648{
1649 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001650 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001651 int context_loss = 0;
1652
Denis Karpov70a33412009-09-22 16:44:59 -07001653 if (host->pdata->get_context_loss_count)
1654 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001655
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001656 seq_printf(s, "mmc%d:\n"
1657 " enabled:\t%d\n"
Denis Karpovdd498ef2009-09-22 16:44:49 -07001658 " dpm_state:\t%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001659 " nesting_cnt:\t%d\n"
Denis Karpov11dd62a2009-09-22 16:44:43 -07001660 " ctx_loss:\t%d:%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001661 "\nregs:\n",
Denis Karpovdd498ef2009-09-22 16:44:49 -07001662 mmc->index, mmc->enabled ? 1 : 0,
1663 host->dpm_state, mmc->nesting_cnt,
Denis Karpov11dd62a2009-09-22 16:44:43 -07001664 host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001665
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301666 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001667 seq_printf(s, "host suspended, can't read registers\n");
1668 return 0;
1669 }
1670
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301671 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001672
1673 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1674 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1675 seq_printf(s, "CON:\t\t0x%08x\n",
1676 OMAP_HSMMC_READ(host->base, CON));
1677 seq_printf(s, "HCTL:\t\t0x%08x\n",
1678 OMAP_HSMMC_READ(host->base, HCTL));
1679 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1680 OMAP_HSMMC_READ(host->base, SYSCTL));
1681 seq_printf(s, "IE:\t\t0x%08x\n",
1682 OMAP_HSMMC_READ(host->base, IE));
1683 seq_printf(s, "ISE:\t\t0x%08x\n",
1684 OMAP_HSMMC_READ(host->base, ISE));
1685 seq_printf(s, "CAPA:\t\t0x%08x\n",
1686 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001687
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301688 pm_runtime_mark_last_busy(host->dev);
1689 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001690
Denis Karpovd900f712009-09-22 16:44:38 -07001691 return 0;
1692}
1693
Denis Karpov70a33412009-09-22 16:44:59 -07001694static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001695{
Denis Karpov70a33412009-09-22 16:44:59 -07001696 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001697}
1698
1699static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001700 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001701 .read = seq_read,
1702 .llseek = seq_lseek,
1703 .release = single_release,
1704};
1705
Denis Karpov70a33412009-09-22 16:44:59 -07001706static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001707{
1708 if (mmc->debugfs_root)
1709 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1710 mmc, &mmc_regs_fops);
1711}
1712
1713#else
1714
Denis Karpov70a33412009-09-22 16:44:59 -07001715static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001716{
1717}
1718
1719#endif
1720
Denis Karpov70a33412009-09-22 16:44:59 -07001721static int __init omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001722{
1723 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1724 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001725 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001726 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001727 int ret, irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001728
1729 if (pdata == NULL) {
1730 dev_err(&pdev->dev, "Platform Data is missing\n");
1731 return -ENXIO;
1732 }
1733
1734 if (pdata->nr_slots == 0) {
1735 dev_err(&pdev->dev, "No Slots\n");
1736 return -ENXIO;
1737 }
1738
1739 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1740 irq = platform_get_irq(pdev, 0);
1741 if (res == NULL || irq < 0)
1742 return -ENXIO;
1743
kishore kadiyala91a0b082010-10-01 16:35:28 -07001744 res->start += pdata->reg_offset;
1745 res->end += pdata->reg_offset;
Chris Ball984b2032011-03-22 16:34:42 -07001746 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001747 if (res == NULL)
1748 return -EBUSY;
1749
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001750 ret = omap_hsmmc_gpio_init(pdata);
1751 if (ret)
1752 goto err;
1753
Denis Karpov70a33412009-09-22 16:44:59 -07001754 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001755 if (!mmc) {
1756 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001757 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001758 }
1759
1760 host = mmc_priv(mmc);
1761 host->mmc = mmc;
1762 host->pdata = pdata;
1763 host->dev = &pdev->dev;
1764 host->use_dma = 1;
1765 host->dev->dma_mask = &pdata->dma_mask;
1766 host->dma_ch = -1;
1767 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001768 host->slot_id = 0;
1769 host->mapbase = res->start;
1770 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001771 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001772 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001773
1774 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001775
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301776 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001777
Adrian Huntere0eb2422010-02-15 10:03:34 -08001778 /*
1779 * If regulator_disable can only put vcc_aux to sleep then there is
1780 * no off state.
1781 */
1782 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1783 mmc_slot(host).no_off = 1;
1784
Daniel Mackd418ed82012-02-19 13:20:33 +01001785 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1786
1787 if (pdata->max_freq > 0)
1788 mmc->f_max = pdata->max_freq;
1789 else
1790 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001791
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001792 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001793
Russell King6f7607c2009-01-28 10:22:50 +00001794 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001795 if (IS_ERR(host->fclk)) {
1796 ret = PTR_ERR(host->fclk);
1797 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001798 goto err1;
1799 }
1800
Denis Karpov70a33412009-09-22 16:44:59 -07001801 omap_hsmmc_context_save(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001802
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001803 mmc->caps |= MMC_CAP_DISABLE;
Paul Walmsley9b682562011-10-06 14:50:35 -06001804 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1805 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1806 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1807 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001808
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301809 pm_runtime_enable(host->dev);
1810 pm_runtime_get_sync(host->dev);
1811 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1812 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001813
Adrian Hunter2bec0892009-09-22 16:45:02 -07001814 if (cpu_is_omap2430()) {
1815 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1816 /*
1817 * MMC can still work without debounce clock.
1818 */
1819 if (IS_ERR(host->dbclk))
1820 dev_warn(mmc_dev(host->mmc),
1821 "Failed to get debounce clock\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001822 else
Adrian Hunter2bec0892009-09-22 16:45:02 -07001823 host->got_dbclk = 1;
1824
1825 if (host->got_dbclk)
1826 if (clk_enable(host->dbclk) != 0)
1827 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
1828 " clk failed\n");
1829 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001830
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001831 /* Since we do only SG emulation, we can have as many segs
1832 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001833 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001834
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001835 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1836 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1837 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1838 mmc->max_seg_size = mmc->max_req_size;
1839
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001840 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001841 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001842
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001843 mmc->caps |= mmc_slot(host).caps;
1844 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001845 mmc->caps |= MMC_CAP_4_BIT_DATA;
1846
Denis Karpov191d1f12009-09-22 16:44:55 -07001847 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001848 mmc->caps |= MMC_CAP_NONREMOVABLE;
1849
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001850 mmc->pm_caps = mmc_slot(host).pm_caps;
1851
Denis Karpov70a33412009-09-22 16:44:59 -07001852 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001853
Balaji T Kb7bf7732012-03-07 09:55:30 -05001854 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1855 if (!res) {
1856 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001857 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001858 }
Balaji T Kb7bf7732012-03-07 09:55:30 -05001859 host->dma_line_tx = res->start;
1860
1861 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1862 if (!res) {
1863 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
1864 goto err_irq;
1865 }
1866 host->dma_line_rx = res->start;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001867
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001868 /* Request IRQ for MMC operations */
Yong Zhangd9618e92011-09-22 16:59:04 +08001869 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001870 mmc_hostname(mmc), host);
1871 if (ret) {
1872 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1873 goto err_irq;
1874 }
1875
1876 if (pdata->init != NULL) {
1877 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07001878 dev_dbg(mmc_dev(host->mmc),
1879 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001880 goto err_irq_cd_init;
1881 }
1882 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001883
Adrian Hunterb702b102010-02-15 10:03:35 -08001884 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001885 ret = omap_hsmmc_reg_get(host);
1886 if (ret)
1887 goto err_reg;
1888 host->use_reg = 1;
1889 }
1890
David Brownellb583f262009-05-28 14:04:03 -07001891 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001892
1893 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001894 if ((mmc_slot(host).card_detect_irq)) {
NeilBrown7efab4f2011-12-30 12:35:13 +11001895 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1896 NULL,
1897 omap_hsmmc_detect,
1898 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1899 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001900 if (ret) {
1901 dev_dbg(mmc_dev(host->mmc),
1902 "Unable to grab MMC CD IRQ\n");
1903 goto err_irq_cd;
1904 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00001905 pdata->suspend = omap_hsmmc_suspend_cdirq;
1906 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001907 }
1908
Adrian Hunterb4175772010-05-26 14:42:06 -07001909 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001910
Adrian Hunterb62f6222009-09-22 16:45:01 -07001911 omap_hsmmc_protect_card(host);
1912
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001913 mmc_add_host(mmc);
1914
Denis Karpov191d1f12009-09-22 16:44:55 -07001915 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001916 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1917 if (ret < 0)
1918 goto err_slot_name;
1919 }
Denis Karpov191d1f12009-09-22 16:44:55 -07001920 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001921 ret = device_create_file(&mmc->class_dev,
1922 &dev_attr_cover_switch);
1923 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001924 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001925 }
1926
Denis Karpov70a33412009-09-22 16:44:59 -07001927 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301928 pm_runtime_mark_last_busy(host->dev);
1929 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001930
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001931 return 0;
1932
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001933err_slot_name:
1934 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001935 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001936err_irq_cd:
1937 if (host->use_reg)
1938 omap_hsmmc_reg_put(host);
1939err_reg:
1940 if (host->pdata->cleanup)
1941 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001942err_irq_cd_init:
1943 free_irq(host->irq, host);
1944err_irq:
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301945 pm_runtime_mark_last_busy(host->dev);
1946 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001947 clk_put(host->fclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001948 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001949 clk_disable(host->dbclk);
1950 clk_put(host->dbclk);
1951 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001952err1:
1953 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001954 platform_set_drvdata(pdev, NULL);
1955 mmc_free_host(mmc);
1956err_alloc:
1957 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001958err:
Chris Ball984b2032011-03-22 16:34:42 -07001959 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001960 return ret;
1961}
1962
Denis Karpov70a33412009-09-22 16:44:59 -07001963static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001964{
Denis Karpov70a33412009-09-22 16:44:59 -07001965 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001966 struct resource *res;
1967
1968 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301969 pm_runtime_get_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001970 mmc_remove_host(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001971 if (host->use_reg)
1972 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001973 if (host->pdata->cleanup)
1974 host->pdata->cleanup(&pdev->dev);
1975 free_irq(host->irq, host);
1976 if (mmc_slot(host).card_detect_irq)
1977 free_irq(mmc_slot(host).card_detect_irq, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001978
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301979 pm_runtime_put_sync(host->dev);
1980 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001981 clk_put(host->fclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001982 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001983 clk_disable(host->dbclk);
1984 clk_put(host->dbclk);
1985 }
1986
1987 mmc_free_host(host->mmc);
1988 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001989 omap_hsmmc_gpio_free(pdev->dev.platform_data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001990 }
1991
1992 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1993 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07001994 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001995 platform_set_drvdata(pdev, NULL);
1996
1997 return 0;
1998}
1999
2000#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002001static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002002{
2003 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002004 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002005 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002006
2007 if (host && host->suspended)
2008 return 0;
2009
2010 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302011 pm_runtime_get_sync(host->dev);
Adrian Huntera6b22402009-09-22 16:44:45 -07002012 host->suspended = 1;
2013 if (host->pdata->suspend) {
2014 ret = host->pdata->suspend(&pdev->dev,
2015 host->slot_id);
2016 if (ret) {
2017 dev_dbg(mmc_dev(host->mmc),
2018 "Unable to handle MMC board"
2019 " level suspend\n");
2020 host->suspended = 0;
2021 return ret;
2022 }
2023 }
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002024 ret = mmc_suspend_host(host->mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302025
Eliad Peller31f9d462011-11-22 16:02:17 +02002026 if (ret) {
Adrian Huntera6b22402009-09-22 16:44:45 -07002027 host->suspended = 0;
2028 if (host->pdata->resume) {
2029 ret = host->pdata->resume(&pdev->dev,
2030 host->slot_id);
2031 if (ret)
2032 dev_dbg(mmc_dev(host->mmc),
2033 "Unmask interrupt failed\n");
2034 }
Eliad Peller31f9d462011-11-22 16:02:17 +02002035 goto err;
Adrian Huntera6b22402009-09-22 16:44:45 -07002036 }
Eliad Peller31f9d462011-11-22 16:02:17 +02002037
2038 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2039 omap_hsmmc_disable_irq(host);
2040 OMAP_HSMMC_WRITE(host->base, HCTL,
2041 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2042 }
2043 if (host->got_dbclk)
2044 clk_disable(host->dbclk);
2045
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002046 }
Eliad Peller31f9d462011-11-22 16:02:17 +02002047err:
2048 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002049 return ret;
2050}
2051
2052/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002053static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002054{
2055 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002056 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002057 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002058
2059 if (host && !host->suspended)
2060 return 0;
2061
2062 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302063 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002064
Adrian Hunter2bec0892009-09-22 16:45:02 -07002065 if (host->got_dbclk)
2066 clk_enable(host->dbclk);
2067
Eliad Peller31f9d462011-11-22 16:02:17 +02002068 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2069 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002070
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002071 if (host->pdata->resume) {
2072 ret = host->pdata->resume(&pdev->dev, host->slot_id);
2073 if (ret)
2074 dev_dbg(mmc_dev(host->mmc),
2075 "Unmask interrupt failed\n");
2076 }
2077
Adrian Hunterb62f6222009-09-22 16:45:01 -07002078 omap_hsmmc_protect_card(host);
2079
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002080 /* Notify the core to resume the host */
2081 ret = mmc_resume_host(host->mmc);
2082 if (ret == 0)
2083 host->suspended = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302084
2085 pm_runtime_mark_last_busy(host->dev);
2086 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002087 }
2088
2089 return ret;
2090
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002091}
2092
2093#else
Denis Karpov70a33412009-09-22 16:44:59 -07002094#define omap_hsmmc_suspend NULL
2095#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002096#endif
2097
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302098static int omap_hsmmc_runtime_suspend(struct device *dev)
2099{
2100 struct omap_hsmmc_host *host;
2101
2102 host = platform_get_drvdata(to_platform_device(dev));
2103 omap_hsmmc_context_save(host);
2104 dev_dbg(mmc_dev(host->mmc), "disabled\n");
2105
2106 return 0;
2107}
2108
2109static int omap_hsmmc_runtime_resume(struct device *dev)
2110{
2111 struct omap_hsmmc_host *host;
2112
2113 host = platform_get_drvdata(to_platform_device(dev));
2114 omap_hsmmc_context_restore(host);
2115 dev_dbg(mmc_dev(host->mmc), "enabled\n");
2116
2117 return 0;
2118}
2119
Kevin Hilmana791daa2010-05-26 14:42:07 -07002120static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002121 .suspend = omap_hsmmc_suspend,
2122 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302123 .runtime_suspend = omap_hsmmc_runtime_suspend,
2124 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002125};
2126
2127static struct platform_driver omap_hsmmc_driver = {
2128 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002129 .driver = {
2130 .name = DRIVER_NAME,
2131 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002132 .pm = &omap_hsmmc_dev_pm_ops,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002133 },
2134};
2135
Denis Karpov70a33412009-09-22 16:44:59 -07002136static int __init omap_hsmmc_init(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002137{
2138 /* Register the MMC driver */
Roger Quadros87532982009-10-26 16:49:38 -07002139 return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002140}
2141
Denis Karpov70a33412009-09-22 16:44:59 -07002142static void __exit omap_hsmmc_cleanup(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002143{
2144 /* Unregister MMC driver */
Denis Karpov70a33412009-09-22 16:44:59 -07002145 platform_driver_unregister(&omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002146}
2147
Denis Karpov70a33412009-09-22 16:44:59 -07002148module_init(omap_hsmmc_init);
2149module_exit(omap_hsmmc_cleanup);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002150
2151MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2152MODULE_LICENSE("GPL");
2153MODULE_ALIAS("platform:" DRIVER_NAME);
2154MODULE_AUTHOR("Texas Instruments Inc");