blob: e0808d4a76819aea11350ae619695ebdc74933fd [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>
Rajendra Nayak46856a62012-03-12 20:32:37 +053029#include <linux/of.h>
30#include <linux/of_gpio.h>
31#include <linux/of_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010032#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070033#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070034#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010035#include <linux/io.h>
36#include <linux/semaphore.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080037#include <linux/gpio.h>
38#include <linux/regulator/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053039#include <linux/pm_runtime.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070040#include <plat/dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010041#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070042#include <plat/board.h>
43#include <plat/mmc.h>
44#include <plat/cpu.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010045
46/* OMAP HSMMC Host Controller Registers */
47#define OMAP_HSMMC_SYSCONFIG 0x0010
Denis Karpov11dd62a2009-09-22 16:44:43 -070048#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010049#define OMAP_HSMMC_CON 0x002C
50#define OMAP_HSMMC_BLK 0x0104
51#define OMAP_HSMMC_ARG 0x0108
52#define OMAP_HSMMC_CMD 0x010C
53#define OMAP_HSMMC_RSP10 0x0110
54#define OMAP_HSMMC_RSP32 0x0114
55#define OMAP_HSMMC_RSP54 0x0118
56#define OMAP_HSMMC_RSP76 0x011C
57#define OMAP_HSMMC_DATA 0x0120
58#define OMAP_HSMMC_HCTL 0x0128
59#define OMAP_HSMMC_SYSCTL 0x012C
60#define OMAP_HSMMC_STAT 0x0130
61#define OMAP_HSMMC_IE 0x0134
62#define OMAP_HSMMC_ISE 0x0138
63#define OMAP_HSMMC_CAPA 0x0140
64
65#define VS18 (1 << 26)
66#define VS30 (1 << 25)
67#define SDVS18 (0x5 << 9)
68#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080069#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010070#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010071#define SDVSCLR 0xFFFFF1FF
72#define SDVSDET 0x00000400
73#define AUTOIDLE 0x1
74#define SDBP (1 << 8)
75#define DTO 0xe
76#define ICE 0x1
77#define ICS 0x2
78#define CEN (1 << 2)
79#define CLKD_MASK 0x0000FFC0
80#define CLKD_SHIFT 6
81#define DTO_MASK 0x000F0000
82#define DTO_SHIFT 16
83#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070084#define BWR_ENABLE (1 << 4)
85#define BRR_ENABLE (1 << 5)
Adrian Hunter93caf8e692010-08-11 14:17:48 -070086#define DTO_ENABLE (1 << 20)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010087#define INIT_STREAM (1 << 1)
88#define DP_SELECT (1 << 21)
89#define DDIR (1 << 4)
90#define DMA_EN 0x1
91#define MSBS (1 << 5)
92#define BCE (1 << 1)
93#define FOUR_BIT (1 << 1)
Jarkko Lavinen73153012008-11-21 16:49:54 +020094#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010095#define CC 0x1
96#define TC 0x02
97#define OD 0x1
98#define ERR (1 << 15)
99#define CMD_TIMEOUT (1 << 16)
100#define DATA_TIMEOUT (1 << 20)
101#define CMD_CRC (1 << 17)
102#define DATA_CRC (1 << 21)
103#define CARD_ERR (1 << 28)
104#define STAT_CLEAR 0xFFFFFFFF
105#define INIT_STREAM_CMD 0x00000000
106#define DUAL_VOLT_OCR_BIT 7
107#define SRC (1 << 25)
108#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700109#define SOFTRESET (1 << 1)
110#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100111
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530112#define MMC_AUTOSUSPEND_DELAY 100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100113#define MMC_TIMEOUT_MS 20
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400114#define OMAP_MMC_MIN_CLOCK 400000
115#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530116#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100117
118/*
119 * One controller can have multiple slots, like on some omap boards using
120 * omap.c controller driver. Luckily this is not currently done on any known
121 * omap_hsmmc.c device.
122 */
123#define mmc_slot(host) (host->pdata->slots[host->slot_id])
124
125/*
126 * MMC Host controller read/write API's
127 */
128#define OMAP_HSMMC_READ(base, reg) \
129 __raw_readl((base) + OMAP_HSMMC_##reg)
130
131#define OMAP_HSMMC_WRITE(base, reg, val) \
132 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
133
Per Forlin9782aff2011-07-01 18:55:23 +0200134struct omap_hsmmc_next {
135 unsigned int dma_len;
136 s32 cookie;
137};
138
Denis Karpov70a33412009-09-22 16:44:59 -0700139struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100140 struct device *dev;
141 struct mmc_host *mmc;
142 struct mmc_request *mrq;
143 struct mmc_command *cmd;
144 struct mmc_data *data;
145 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100146 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800147 /*
148 * vcc == configured supply
149 * vcc_aux == optional
150 * - MMC1, supply for DAT4..DAT7
151 * - MMC2/MMC2, external level shifter voltage supply, for
152 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
153 */
154 struct regulator *vcc;
155 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100156 void __iomem *base;
157 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700158 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100159 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200160 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100161 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700162 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100163 u32 *buffer;
164 u32 bytesleft;
165 int suspended;
166 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100167 int use_dma, dma_ch;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000168 int dma_line_tx, dma_line_rx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100169 int slot_id;
Adrian Hunter2bec0892009-09-22 16:45:02 -0700170 int got_dbclk;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200171 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700172 int context_loss;
Adrian Hunter623821f2009-09-22 16:44:51 -0700173 int vdd;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700174 int protect_card;
175 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800176 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700177 int req_in_progress;
Per Forlin9782aff2011-07-01 18:55:23 +0200178 struct omap_hsmmc_next next_data;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700179
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100180 struct omap_mmc_platform_data *pdata;
181};
182
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800183static int omap_hsmmc_card_detect(struct device *dev, int slot)
184{
185 struct omap_mmc_platform_data *mmc = dev->platform_data;
186
187 /* NOTE: assumes card detect signal is active-low */
188 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
189}
190
191static int omap_hsmmc_get_wp(struct device *dev, int slot)
192{
193 struct omap_mmc_platform_data *mmc = dev->platform_data;
194
195 /* NOTE: assumes write protect signal is active-high */
196 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
197}
198
199static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
200{
201 struct omap_mmc_platform_data *mmc = dev->platform_data;
202
203 /* NOTE: assumes card detect signal is active-low */
204 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
205}
206
207#ifdef CONFIG_PM
208
209static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
210{
211 struct omap_mmc_platform_data *mmc = dev->platform_data;
212
213 disable_irq(mmc->slots[0].card_detect_irq);
214 return 0;
215}
216
217static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
218{
219 struct omap_mmc_platform_data *mmc = dev->platform_data;
220
221 enable_irq(mmc->slots[0].card_detect_irq);
222 return 0;
223}
224
225#else
226
227#define omap_hsmmc_suspend_cdirq NULL
228#define omap_hsmmc_resume_cdirq NULL
229
230#endif
231
Adrian Hunterb702b102010-02-15 10:03:35 -0800232#ifdef CONFIG_REGULATOR
233
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500234static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800235 int vdd)
236{
237 struct omap_hsmmc_host *host =
238 platform_get_drvdata(to_platform_device(dev));
239 int ret = 0;
240
241 /*
242 * If we don't see a Vcc regulator, assume it's a fixed
243 * voltage always-on regulator.
244 */
245 if (!host->vcc)
246 return 0;
247
248 if (mmc_slot(host).before_set_reg)
249 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
250
251 /*
252 * Assume Vcc regulator is used only to power the card ... OMAP
253 * VDDS is used to power the pins, optionally with a transceiver to
254 * support cards using voltages other than VDDS (1.8V nominal). When a
255 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
256 *
257 * In some cases this regulator won't support enable/disable;
258 * e.g. it's a fixed rail for a WLAN chip.
259 *
260 * In other cases vcc_aux switches interface power. Example, for
261 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
262 * chips/cards need an interface voltage rail too.
263 */
264 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400265 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800266 /* Enable interface voltage rail, if needed */
267 if (ret == 0 && host->vcc_aux) {
268 ret = regulator_enable(host->vcc_aux);
269 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400270 ret = mmc_regulator_set_ocr(host->mmc,
271 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800272 }
273 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400274 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800275 if (host->vcc_aux)
276 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400277 if (!ret) {
278 /* Then proceed to shut down the local regulator */
279 ret = mmc_regulator_set_ocr(host->mmc,
280 host->vcc, 0);
281 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800282 }
283
284 if (mmc_slot(host).after_set_reg)
285 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
286
287 return ret;
288}
289
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800290static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
291{
292 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700293 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800294
Rajendra Nayak1cb9af42012-03-07 09:55:31 -0500295 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800296
297 reg = regulator_get(host->dev, "vmmc");
298 if (IS_ERR(reg)) {
299 dev_dbg(host->dev, "vmmc regulator missing\n");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800300 } else {
301 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700302 ocr_value = mmc_regulator_get_ocrmask(reg);
303 if (!mmc_slot(host).ocr_mask) {
304 mmc_slot(host).ocr_mask = ocr_value;
305 } else {
306 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530307 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500308 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700309 mmc_slot(host).ocr_mask = 0;
310 return -EINVAL;
311 }
312 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800313
314 /* Allow an aux regulator */
315 reg = regulator_get(host->dev, "vmmc_aux");
316 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
317
Balaji T Kb1c1df72011-05-30 19:55:34 +0530318 /* For eMMC do not power off when not in sleep state */
319 if (mmc_slot(host).no_regulator_off_init)
320 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800321 /*
322 * UGLY HACK: workaround regulator framework bugs.
323 * When the bootloader leaves a supply active, it's
324 * initialized with zero usecount ... and we can't
325 * disable it without first enabling it. Until the
326 * framework is fixed, we need a workaround like this
327 * (which is safe for MMC, but not in general).
328 */
Adrian Huntere840ce12011-05-06 12:14:10 +0300329 if (regulator_is_enabled(host->vcc) > 0 ||
330 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
331 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
332
333 mmc_slot(host).set_power(host->dev, host->slot_id,
334 1, vdd);
335 mmc_slot(host).set_power(host->dev, host->slot_id,
336 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800337 }
338 }
339
340 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800341}
342
343static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
344{
345 regulator_put(host->vcc);
346 regulator_put(host->vcc_aux);
347 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800348}
349
Adrian Hunterb702b102010-02-15 10:03:35 -0800350static inline int omap_hsmmc_have_reg(void)
351{
352 return 1;
353}
354
355#else
356
357static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
358{
359 return -EINVAL;
360}
361
362static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
363{
364}
365
366static inline int omap_hsmmc_have_reg(void)
367{
368 return 0;
369}
370
371#endif
372
373static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
374{
375 int ret;
376
377 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800378 if (pdata->slots[0].cover)
379 pdata->slots[0].get_cover_state =
380 omap_hsmmc_get_cover_state;
381 else
382 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
383 pdata->slots[0].card_detect_irq =
384 gpio_to_irq(pdata->slots[0].switch_pin);
385 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
386 if (ret)
387 return ret;
388 ret = gpio_direction_input(pdata->slots[0].switch_pin);
389 if (ret)
390 goto err_free_sp;
391 } else
392 pdata->slots[0].switch_pin = -EINVAL;
393
394 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
395 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
396 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
397 if (ret)
398 goto err_free_cd;
399 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
400 if (ret)
401 goto err_free_wp;
402 } else
403 pdata->slots[0].gpio_wp = -EINVAL;
404
405 return 0;
406
407err_free_wp:
408 gpio_free(pdata->slots[0].gpio_wp);
409err_free_cd:
410 if (gpio_is_valid(pdata->slots[0].switch_pin))
411err_free_sp:
412 gpio_free(pdata->slots[0].switch_pin);
413 return ret;
414}
415
416static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
417{
418 if (gpio_is_valid(pdata->slots[0].gpio_wp))
419 gpio_free(pdata->slots[0].gpio_wp);
420 if (gpio_is_valid(pdata->slots[0].switch_pin))
421 gpio_free(pdata->slots[0].switch_pin);
422}
423
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100424/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300425 * Start clock to the card
426 */
427static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
428{
429 OMAP_HSMMC_WRITE(host->base, SYSCTL,
430 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
431}
432
433/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100434 * Stop clock to the card
435 */
Denis Karpov70a33412009-09-22 16:44:59 -0700436static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100437{
438 OMAP_HSMMC_WRITE(host->base, SYSCTL,
439 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
440 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
441 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
442}
443
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700444static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
445 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700446{
447 unsigned int irq_mask;
448
449 if (host->use_dma)
450 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
451 else
452 irq_mask = INT_EN_MASK;
453
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700454 /* Disable timeout for erases */
455 if (cmd->opcode == MMC_ERASE)
456 irq_mask &= ~DTO_ENABLE;
457
Adrian Hunterb4175772010-05-26 14:42:06 -0700458 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
459 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
460 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
461}
462
463static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
464{
465 OMAP_HSMMC_WRITE(host->base, ISE, 0);
466 OMAP_HSMMC_WRITE(host->base, IE, 0);
467 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
468}
469
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300470/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530471static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300472{
473 u16 dsor = 0;
474
475 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530476 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300477 if (dsor > 250)
478 dsor = 250;
479 }
480
481 return dsor;
482}
483
Andy Shevchenko5934df22011-05-06 12:14:06 +0300484static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
485{
486 struct mmc_ios *ios = &host->mmc->ios;
487 unsigned long regval;
488 unsigned long timeout;
489
490 dev_dbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
491
492 omap_hsmmc_stop_clock(host);
493
494 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
495 regval = regval & ~(CLKD_MASK | DTO_MASK);
Balaji TKd83b6e02011-12-20 15:12:00 +0530496 regval = regval | (calc_divisor(host, ios) << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300497 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
498 OMAP_HSMMC_WRITE(host->base, SYSCTL,
499 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
500
501 /* Wait till the ICS bit is set */
502 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
503 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
504 && time_before(jiffies, timeout))
505 cpu_relax();
506
507 omap_hsmmc_start_clock(host);
508}
509
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400510static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
511{
512 struct mmc_ios *ios = &host->mmc->ios;
513 u32 con;
514
515 con = OMAP_HSMMC_READ(host->base, CON);
516 switch (ios->bus_width) {
517 case MMC_BUS_WIDTH_8:
518 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
519 break;
520 case MMC_BUS_WIDTH_4:
521 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
522 OMAP_HSMMC_WRITE(host->base, HCTL,
523 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
524 break;
525 case MMC_BUS_WIDTH_1:
526 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
527 OMAP_HSMMC_WRITE(host->base, HCTL,
528 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
529 break;
530 }
531}
532
533static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
534{
535 struct mmc_ios *ios = &host->mmc->ios;
536 u32 con;
537
538 con = OMAP_HSMMC_READ(host->base, CON);
539 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
540 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
541 else
542 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
543}
544
Denis Karpov11dd62a2009-09-22 16:44:43 -0700545#ifdef CONFIG_PM
546
547/*
548 * Restore the MMC host context, if it was lost as result of a
549 * power state change.
550 */
Denis Karpov70a33412009-09-22 16:44:59 -0700551static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700552{
553 struct mmc_ios *ios = &host->mmc->ios;
554 struct omap_mmc_platform_data *pdata = host->pdata;
555 int context_loss = 0;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400556 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700557 unsigned long timeout;
558
559 if (pdata->get_context_loss_count) {
560 context_loss = pdata->get_context_loss_count(host->dev);
561 if (context_loss < 0)
562 return 1;
563 }
564
565 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
566 context_loss == host->context_loss ? "not " : "");
567 if (host->context_loss == context_loss)
568 return 1;
569
570 /* Wait for hardware reset */
571 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
572 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
573 && time_before(jiffies, timeout))
574 ;
575
576 /* Do software reset */
577 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
578 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
579 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
580 && time_before(jiffies, timeout))
581 ;
582
583 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
584 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
585
Balaji T Kc2200ef2012-03-07 09:55:30 -0500586 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700587 if (host->power_mode != MMC_POWER_OFF &&
588 (1 << ios->vdd) <= MMC_VDD_23_24)
589 hctl = SDVS18;
590 else
591 hctl = SDVS30;
592 capa = VS30 | VS18;
593 } else {
594 hctl = SDVS18;
595 capa = VS18;
596 }
597
598 OMAP_HSMMC_WRITE(host->base, HCTL,
599 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
600
601 OMAP_HSMMC_WRITE(host->base, CAPA,
602 OMAP_HSMMC_READ(host->base, CAPA) | capa);
603
604 OMAP_HSMMC_WRITE(host->base, HCTL,
605 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
606
607 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
608 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
609 && time_before(jiffies, timeout))
610 ;
611
Adrian Hunterb4175772010-05-26 14:42:06 -0700612 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700613
614 /* Do not initialize card-specific things if the power is off */
615 if (host->power_mode == MMC_POWER_OFF)
616 goto out;
617
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400618 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700619
Andy Shevchenko5934df22011-05-06 12:14:06 +0300620 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700621
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400622 omap_hsmmc_set_bus_mode(host);
623
Denis Karpov11dd62a2009-09-22 16:44:43 -0700624out:
625 host->context_loss = context_loss;
626
627 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
628 return 0;
629}
630
631/*
632 * Save the MMC host context (store the number of power state changes so far).
633 */
Denis Karpov70a33412009-09-22 16:44:59 -0700634static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700635{
636 struct omap_mmc_platform_data *pdata = host->pdata;
637 int context_loss;
638
639 if (pdata->get_context_loss_count) {
640 context_loss = pdata->get_context_loss_count(host->dev);
641 if (context_loss < 0)
642 return;
643 host->context_loss = context_loss;
644 }
645}
646
647#else
648
Denis Karpov70a33412009-09-22 16:44:59 -0700649static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700650{
651 return 0;
652}
653
Denis Karpov70a33412009-09-22 16:44:59 -0700654static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700655{
656}
657
658#endif
659
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100660/*
661 * Send init stream sequence to card
662 * before sending IDLE command
663 */
Denis Karpov70a33412009-09-22 16:44:59 -0700664static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100665{
666 int reg = 0;
667 unsigned long timeout;
668
Adrian Hunterb62f6222009-09-22 16:45:01 -0700669 if (host->protect_card)
670 return;
671
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100672 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700673
674 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100675 OMAP_HSMMC_WRITE(host->base, CON,
676 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
677 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
678
679 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
680 while ((reg != CC) && time_before(jiffies, timeout))
681 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
682
683 OMAP_HSMMC_WRITE(host->base, CON,
684 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700685
686 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
687 OMAP_HSMMC_READ(host->base, STAT);
688
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100689 enable_irq(host->irq);
690}
691
692static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700693int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100694{
695 int r = 1;
696
Denis Karpov191d1f12009-09-22 16:44:55 -0700697 if (mmc_slot(host).get_cover_state)
698 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100699 return r;
700}
701
702static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700703omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100704 char *buf)
705{
706 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700707 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100708
Denis Karpov70a33412009-09-22 16:44:59 -0700709 return sprintf(buf, "%s\n",
710 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100711}
712
Denis Karpov70a33412009-09-22 16:44:59 -0700713static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100714
715static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700716omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100717 char *buf)
718{
719 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700720 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100721
Denis Karpov191d1f12009-09-22 16:44:55 -0700722 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100723}
724
Denis Karpov70a33412009-09-22 16:44:59 -0700725static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100726
727/*
728 * Configure the response type and send the cmd.
729 */
730static void
Denis Karpov70a33412009-09-22 16:44:59 -0700731omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100732 struct mmc_data *data)
733{
734 int cmdreg = 0, resptype = 0, cmdtype = 0;
735
736 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
737 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
738 host->cmd = cmd;
739
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700740 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100741
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200742 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100743 if (cmd->flags & MMC_RSP_PRESENT) {
744 if (cmd->flags & MMC_RSP_136)
745 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200746 else if (cmd->flags & MMC_RSP_BUSY) {
747 resptype = 3;
748 host->response_busy = 1;
749 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100750 resptype = 2;
751 }
752
753 /*
754 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
755 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
756 * a val of 0x3, rest 0x0.
757 */
758 if (cmd == host->mrq->stop)
759 cmdtype = 0x3;
760
761 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
762
763 if (data) {
764 cmdreg |= DP_SELECT | MSBS | BCE;
765 if (data->flags & MMC_DATA_READ)
766 cmdreg |= DDIR;
767 else
768 cmdreg &= ~(DDIR);
769 }
770
771 if (host->use_dma)
772 cmdreg |= DMA_EN;
773
Adrian Hunterb4175772010-05-26 14:42:06 -0700774 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700775
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100776 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
777 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
778}
779
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200780static int
Denis Karpov70a33412009-09-22 16:44:59 -0700781omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200782{
783 if (data->flags & MMC_DATA_WRITE)
784 return DMA_TO_DEVICE;
785 else
786 return DMA_FROM_DEVICE;
787}
788
Adrian Hunterb4175772010-05-26 14:42:06 -0700789static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
790{
791 int dma_ch;
792
793 spin_lock(&host->irq_lock);
794 host->req_in_progress = 0;
795 dma_ch = host->dma_ch;
796 spin_unlock(&host->irq_lock);
797
798 omap_hsmmc_disable_irq(host);
799 /* Do not complete the request if DMA is still in progress */
800 if (mrq->data && host->use_dma && dma_ch != -1)
801 return;
802 host->mrq = NULL;
803 mmc_request_done(host->mmc, mrq);
804}
805
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100806/*
807 * Notify the transfer complete to MMC core
808 */
809static void
Denis Karpov70a33412009-09-22 16:44:59 -0700810omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100811{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200812 if (!data) {
813 struct mmc_request *mrq = host->mrq;
814
Adrian Hunter23050102009-09-22 16:44:57 -0700815 /* TC before CC from CMD6 - don't know why, but it happens */
816 if (host->cmd && host->cmd->opcode == 6 &&
817 host->response_busy) {
818 host->response_busy = 0;
819 return;
820 }
821
Adrian Hunterb4175772010-05-26 14:42:06 -0700822 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200823 return;
824 }
825
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100826 host->data = NULL;
827
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100828 if (!data->error)
829 data->bytes_xfered += data->blocks * (data->blksz);
830 else
831 data->bytes_xfered = 0;
832
833 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700834 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100835 return;
836 }
Denis Karpov70a33412009-09-22 16:44:59 -0700837 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100838}
839
840/*
841 * Notify the core about command completion
842 */
843static void
Denis Karpov70a33412009-09-22 16:44:59 -0700844omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100845{
846 host->cmd = NULL;
847
848 if (cmd->flags & MMC_RSP_PRESENT) {
849 if (cmd->flags & MMC_RSP_136) {
850 /* response type 2 */
851 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
852 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
853 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
854 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
855 } else {
856 /* response types 1, 1b, 3, 4, 5, 6 */
857 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
858 }
859 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700860 if ((host->data == NULL && !host->response_busy) || cmd->error)
861 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100862}
863
864/*
865 * DMA clean up for command errors
866 */
Denis Karpov70a33412009-09-22 16:44:59 -0700867static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100868{
Adrian Hunterb4175772010-05-26 14:42:06 -0700869 int dma_ch;
870
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200871 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100872
Adrian Hunterb4175772010-05-26 14:42:06 -0700873 spin_lock(&host->irq_lock);
874 dma_ch = host->dma_ch;
875 host->dma_ch = -1;
876 spin_unlock(&host->irq_lock);
877
878 if (host->use_dma && dma_ch != -1) {
Per Forlina9120c32011-06-17 20:14:21 +0200879 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg,
880 host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700881 omap_hsmmc_get_dma_dir(host, host->data));
Adrian Hunterb4175772010-05-26 14:42:06 -0700882 omap_free_dma(dma_ch);
Per Forlin053bf342011-11-07 21:55:11 +0530883 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100884 }
885 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100886}
887
888/*
889 * Readable error output
890 */
891#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300892static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100893{
894 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700895 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300896 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
897 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
898 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
899 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100900 };
901 char res[256];
902 char *buf = res;
903 int len, i;
904
905 len = sprintf(buf, "MMC IRQ 0x%x :", status);
906 buf += len;
907
Denis Karpov70a33412009-09-22 16:44:59 -0700908 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100909 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700910 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100911 buf += len;
912 }
913
914 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
915}
Adrian Hunter699b9582011-05-06 12:14:01 +0300916#else
917static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
918 u32 status)
919{
920}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100921#endif /* CONFIG_MMC_DEBUG */
922
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100923/*
924 * MMC controller internal state machines reset
925 *
926 * Used to reset command or data internal state machines, using respectively
927 * SRC or SRD bit of SYSCTL register
928 * Can be called from interrupt context
929 */
Denis Karpov70a33412009-09-22 16:44:59 -0700930static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
931 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100932{
933 unsigned long i = 0;
934 unsigned long limit = (loops_per_jiffy *
935 msecs_to_jiffies(MMC_TIMEOUT_MS));
936
937 OMAP_HSMMC_WRITE(host->base, SYSCTL,
938 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
939
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700940 /*
941 * OMAP4 ES2 and greater has an updated reset logic.
942 * Monitor a 0->1 transition first
943 */
944 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -0500945 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700946 && (i++ < limit))
947 cpu_relax();
948 }
949 i = 0;
950
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100951 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
952 (i++ < limit))
953 cpu_relax();
954
955 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
956 dev_err(mmc_dev(host->mmc),
957 "Timeout waiting on controller reset in %s\n",
958 __func__);
959}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100960
Adrian Hunterb4175772010-05-26 14:42:06 -0700961static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100962{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100963 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -0700964 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100965
Adrian Hunterb4175772010-05-26 14:42:06 -0700966 if (!host->req_in_progress) {
967 do {
968 OMAP_HSMMC_WRITE(host->base, STAT, status);
969 /* Flush posted write */
970 status = OMAP_HSMMC_READ(host->base, STAT);
971 } while (status & INT_EN_MASK);
972 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100973 }
974
975 data = host->data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100976 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
977
978 if (status & ERR) {
Adrian Hunter699b9582011-05-06 12:14:01 +0300979 omap_hsmmc_dbg_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100980 if ((status & CMD_TIMEOUT) ||
981 (status & CMD_CRC)) {
982 if (host->cmd) {
983 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -0700984 omap_hsmmc_reset_controller_fsm(host,
985 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100986 host->cmd->error = -ETIMEDOUT;
987 } else {
988 host->cmd->error = -EILSEQ;
989 }
990 end_cmd = 1;
991 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200992 if (host->data || host->response_busy) {
993 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -0700994 omap_hsmmc_dma_cleanup(host,
995 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200996 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -0700997 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -0800998 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100999 }
1000 if ((status & DATA_TIMEOUT) ||
1001 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001002 if (host->data || host->response_busy) {
1003 int err = (status & DATA_TIMEOUT) ?
1004 -ETIMEDOUT : -EILSEQ;
1005
1006 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001007 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001008 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001009 host->mrq->cmd->error = err;
1010 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001011 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001012 end_trans = 1;
1013 }
1014 }
1015 if (status & CARD_ERR) {
1016 dev_dbg(mmc_dev(host->mmc),
1017 "Ignoring card err CMD%d\n", host->cmd->opcode);
1018 if (host->cmd)
1019 end_cmd = 1;
1020 if (host->data)
1021 end_trans = 1;
1022 }
1023 }
1024
1025 OMAP_HSMMC_WRITE(host->base, STAT, status);
1026
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001027 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001028 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001029 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001030 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001031}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001032
Adrian Hunterb4175772010-05-26 14:42:06 -07001033/*
1034 * MMC controller IRQ handler
1035 */
1036static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1037{
1038 struct omap_hsmmc_host *host = dev_id;
1039 int status;
1040
1041 status = OMAP_HSMMC_READ(host->base, STAT);
1042 do {
1043 omap_hsmmc_do_irq(host, status);
1044 /* Flush posted write */
1045 status = OMAP_HSMMC_READ(host->base, STAT);
1046 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001047
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001048 return IRQ_HANDLED;
1049}
1050
Denis Karpov70a33412009-09-22 16:44:59 -07001051static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001052{
1053 unsigned long i;
1054
1055 OMAP_HSMMC_WRITE(host->base, HCTL,
1056 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1057 for (i = 0; i < loops_per_jiffy; i++) {
1058 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1059 break;
1060 cpu_relax();
1061 }
1062}
1063
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001064/*
David Brownelleb250822009-02-17 14:49:01 -08001065 * Switch MMC interface voltage ... only relevant for MMC1.
1066 *
1067 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1068 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1069 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001070 */
Denis Karpov70a33412009-09-22 16:44:59 -07001071static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001072{
1073 u32 reg_val = 0;
1074 int ret;
1075
1076 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301077 pm_runtime_put_sync(host->dev);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001078 if (host->got_dbclk)
1079 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001080
1081 /* Turn the power off */
1082 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001083
1084 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001085 if (!ret)
1086 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1087 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301088 pm_runtime_get_sync(host->dev);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001089 if (host->got_dbclk)
1090 clk_enable(host->dbclk);
1091
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001092 if (ret != 0)
1093 goto err;
1094
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001095 OMAP_HSMMC_WRITE(host->base, HCTL,
1096 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1097 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001098
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001099 /*
1100 * If a MMC dual voltage card is detected, the set_ios fn calls
1101 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001102 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001103 *
David Brownelleb250822009-02-17 14:49:01 -08001104 * Cope with a bit of slop in the range ... per data sheets:
1105 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1106 * but recommended values are 1.71V to 1.89V
1107 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1108 * but recommended values are 2.7V to 3.3V
1109 *
1110 * Board setup code shouldn't permit anything very out-of-range.
1111 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1112 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001113 */
David Brownelleb250822009-02-17 14:49:01 -08001114 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001115 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001116 else
1117 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001118
1119 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001120 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001121
1122 return 0;
1123err:
1124 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1125 return ret;
1126}
1127
Adrian Hunterb62f6222009-09-22 16:45:01 -07001128/* Protect the card while the cover is open */
1129static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1130{
1131 if (!mmc_slot(host).get_cover_state)
1132 return;
1133
1134 host->reqs_blocked = 0;
1135 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1136 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301137 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001138 "card is now accessible\n",
1139 mmc_hostname(host->mmc));
1140 host->protect_card = 0;
1141 }
1142 } else {
1143 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301144 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001145 "card is now inaccessible\n",
1146 mmc_hostname(host->mmc));
1147 host->protect_card = 1;
1148 }
1149 }
1150}
1151
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001152/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001153 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001154 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001155static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001156{
NeilBrown7efab4f2011-12-30 12:35:13 +11001157 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001158 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001159 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001160
Adrian Huntera6b22402009-09-22 16:44:45 -07001161 if (host->suspended)
NeilBrown7efab4f2011-12-30 12:35:13 +11001162 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001163
1164 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001165
Denis Karpov191d1f12009-09-22 16:44:55 -07001166 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001167 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001168 else {
1169 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001170 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001171 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001172
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001173 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001174 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001175 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001176 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001177 return IRQ_HANDLED;
1178}
1179
Denis Karpov70a33412009-09-22 16:44:59 -07001180static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001181 struct mmc_data *data)
1182{
1183 int sync_dev;
1184
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001185 if (data->flags & MMC_DATA_WRITE)
1186 sync_dev = host->dma_line_tx;
1187 else
1188 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001189 return sync_dev;
1190}
1191
Denis Karpov70a33412009-09-22 16:44:59 -07001192static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001193 struct mmc_data *data,
1194 struct scatterlist *sgl)
1195{
1196 int blksz, nblk, dma_ch;
1197
1198 dma_ch = host->dma_ch;
1199 if (data->flags & MMC_DATA_WRITE) {
1200 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1201 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1202 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1203 sg_dma_address(sgl), 0, 0);
1204 } else {
1205 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -07001206 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001207 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1208 sg_dma_address(sgl), 0, 0);
1209 }
1210
1211 blksz = host->data->blksz;
1212 nblk = sg_dma_len(sgl) / blksz;
1213
1214 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1215 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -07001216 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001217 !(data->flags & MMC_DATA_WRITE));
1218
1219 omap_start_dma(dma_ch);
1220}
1221
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001222/*
1223 * DMA call back function
1224 */
Adrian Hunterb4175772010-05-26 14:42:06 -07001225static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001226{
Adrian Hunterb4175772010-05-26 14:42:06 -07001227 struct omap_hsmmc_host *host = cb_data;
Adrian Hunter770d7432011-05-06 12:14:11 +03001228 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001229 int dma_ch, req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001230
Venkatraman Sf3584e52010-08-10 18:01:54 -07001231 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1232 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1233 ch_status);
1234 return;
1235 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001236
Adrian Hunterb4175772010-05-26 14:42:06 -07001237 spin_lock(&host->irq_lock);
1238 if (host->dma_ch < 0) {
1239 spin_unlock(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001240 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001241 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001242
Adrian Hunter770d7432011-05-06 12:14:11 +03001243 data = host->mrq->data;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001244 host->dma_sg_idx++;
1245 if (host->dma_sg_idx < host->dma_len) {
1246 /* Fire up the next transfer. */
Adrian Hunterb4175772010-05-26 14:42:06 -07001247 omap_hsmmc_config_dma_params(host, data,
1248 data->sg + host->dma_sg_idx);
1249 spin_unlock(&host->irq_lock);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001250 return;
1251 }
1252
Per Forlin9782aff2011-07-01 18:55:23 +02001253 if (!data->host_cookie)
1254 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1255 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001256
1257 req_in_progress = host->req_in_progress;
1258 dma_ch = host->dma_ch;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001259 host->dma_ch = -1;
Adrian Hunterb4175772010-05-26 14:42:06 -07001260 spin_unlock(&host->irq_lock);
1261
1262 omap_free_dma(dma_ch);
1263
1264 /* If DMA has finished after TC, complete the request */
1265 if (!req_in_progress) {
1266 struct mmc_request *mrq = host->mrq;
1267
1268 host->mrq = NULL;
1269 mmc_request_done(host->mmc, mrq);
1270 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001271}
1272
Per Forlin9782aff2011-07-01 18:55:23 +02001273static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1274 struct mmc_data *data,
1275 struct omap_hsmmc_next *next)
1276{
1277 int dma_len;
1278
1279 if (!next && data->host_cookie &&
1280 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301281 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001282 " host->next_data.cookie %d\n",
1283 __func__, data->host_cookie, host->next_data.cookie);
1284 data->host_cookie = 0;
1285 }
1286
1287 /* Check if next job is already prepared */
1288 if (next ||
1289 (!next && data->host_cookie != host->next_data.cookie)) {
1290 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
1291 data->sg_len,
1292 omap_hsmmc_get_dma_dir(host, data));
1293
1294 } else {
1295 dma_len = host->next_data.dma_len;
1296 host->next_data.dma_len = 0;
1297 }
1298
1299
1300 if (dma_len == 0)
1301 return -EINVAL;
1302
1303 if (next) {
1304 next->dma_len = dma_len;
1305 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1306 } else
1307 host->dma_len = dma_len;
1308
1309 return 0;
1310}
1311
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001312/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001313 * Routine to configure and start DMA for the MMC card
1314 */
Denis Karpov70a33412009-09-22 16:44:59 -07001315static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1316 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001317{
Adrian Hunterb4175772010-05-26 14:42:06 -07001318 int dma_ch = 0, ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001319 struct mmc_data *data = req->data;
1320
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001321 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001322 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001323 struct scatterlist *sgl;
1324
1325 sgl = data->sg + i;
1326 if (sgl->length % data->blksz)
1327 return -EINVAL;
1328 }
1329 if ((data->blksz % 4) != 0)
1330 /* REVISIT: The MMC buffer increments only when MSB is written.
1331 * Return error for blksz which is non multiple of four.
1332 */
1333 return -EINVAL;
1334
Adrian Hunterb4175772010-05-26 14:42:06 -07001335 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001336
Denis Karpov70a33412009-09-22 16:44:59 -07001337 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1338 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001339 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001340 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001341 "%s: omap_request_dma() failed with %d\n",
1342 mmc_hostname(host->mmc), ret);
1343 return ret;
1344 }
Per Forlin9782aff2011-07-01 18:55:23 +02001345 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL);
1346 if (ret)
1347 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001348
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001349 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001350 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001351
Denis Karpov70a33412009-09-22 16:44:59 -07001352 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001353
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001354 return 0;
1355}
1356
Denis Karpov70a33412009-09-22 16:44:59 -07001357static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001358 unsigned int timeout_ns,
1359 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001360{
1361 unsigned int timeout, cycle_ns;
1362 uint32_t reg, clkd, dto = 0;
1363
1364 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1365 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1366 if (clkd == 0)
1367 clkd = 1;
1368
1369 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001370 timeout = timeout_ns / cycle_ns;
1371 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001372 if (timeout) {
1373 while ((timeout & 0x80000000) == 0) {
1374 dto += 1;
1375 timeout <<= 1;
1376 }
1377 dto = 31 - dto;
1378 timeout <<= 1;
1379 if (timeout && dto)
1380 dto += 1;
1381 if (dto >= 13)
1382 dto -= 13;
1383 else
1384 dto = 0;
1385 if (dto > 14)
1386 dto = 14;
1387 }
1388
1389 reg &= ~DTO_MASK;
1390 reg |= dto << DTO_SHIFT;
1391 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1392}
1393
1394/*
1395 * Configure block length for MMC/SD cards and initiate the transfer.
1396 */
1397static int
Denis Karpov70a33412009-09-22 16:44:59 -07001398omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001399{
1400 int ret;
1401 host->data = req->data;
1402
1403 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001404 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001405 /*
1406 * Set an arbitrary 100ms data timeout for commands with
1407 * busy signal.
1408 */
1409 if (req->cmd->flags & MMC_RSP_BUSY)
1410 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001411 return 0;
1412 }
1413
1414 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1415 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001416 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001417
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001418 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001419 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001420 if (ret != 0) {
1421 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1422 return ret;
1423 }
1424 }
1425 return 0;
1426}
1427
Per Forlin9782aff2011-07-01 18:55:23 +02001428static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1429 int err)
1430{
1431 struct omap_hsmmc_host *host = mmc_priv(mmc);
1432 struct mmc_data *data = mrq->data;
1433
1434 if (host->use_dma) {
Per Forlin053bf342011-11-07 21:55:11 +05301435 if (data->host_cookie)
1436 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
1437 data->sg_len,
1438 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001439 data->host_cookie = 0;
1440 }
1441}
1442
1443static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1444 bool is_first_req)
1445{
1446 struct omap_hsmmc_host *host = mmc_priv(mmc);
1447
1448 if (mrq->data->host_cookie) {
1449 mrq->data->host_cookie = 0;
1450 return ;
1451 }
1452
1453 if (host->use_dma)
1454 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
1455 &host->next_data))
1456 mrq->data->host_cookie = 0;
1457}
1458
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001459/*
1460 * Request function. for read/write operation
1461 */
Denis Karpov70a33412009-09-22 16:44:59 -07001462static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001463{
Denis Karpov70a33412009-09-22 16:44:59 -07001464 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001465 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001466
Adrian Hunterb4175772010-05-26 14:42:06 -07001467 BUG_ON(host->req_in_progress);
1468 BUG_ON(host->dma_ch != -1);
1469 if (host->protect_card) {
1470 if (host->reqs_blocked < 3) {
1471 /*
1472 * Ensure the controller is left in a consistent
1473 * state by resetting the command and data state
1474 * machines.
1475 */
1476 omap_hsmmc_reset_controller_fsm(host, SRD);
1477 omap_hsmmc_reset_controller_fsm(host, SRC);
1478 host->reqs_blocked += 1;
1479 }
1480 req->cmd->error = -EBADF;
1481 if (req->data)
1482 req->data->error = -EBADF;
1483 req->cmd->retries = 0;
1484 mmc_request_done(mmc, req);
1485 return;
1486 } else if (host->reqs_blocked)
1487 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001488 WARN_ON(host->mrq != NULL);
1489 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001490 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001491 if (err) {
1492 req->cmd->error = err;
1493 if (req->data)
1494 req->data->error = err;
1495 host->mrq = NULL;
1496 mmc_request_done(mmc, req);
1497 return;
1498 }
1499
Denis Karpov70a33412009-09-22 16:44:59 -07001500 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001501}
1502
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001503/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001504static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001505{
Denis Karpov70a33412009-09-22 16:44:59 -07001506 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001507 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001508
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301509 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001510
Adrian Huntera3621462009-09-22 16:44:42 -07001511 if (ios->power_mode != host->power_mode) {
1512 switch (ios->power_mode) {
1513 case MMC_POWER_OFF:
1514 mmc_slot(host).set_power(host->dev, host->slot_id,
1515 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001516 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001517 break;
1518 case MMC_POWER_UP:
1519 mmc_slot(host).set_power(host->dev, host->slot_id,
1520 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001521 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001522 break;
1523 case MMC_POWER_ON:
1524 do_send_init_stream = 1;
1525 break;
1526 }
1527 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001528 }
1529
Denis Karpovdd498ef2009-09-22 16:44:49 -07001530 /* FIXME: set registers based only on changes to ios */
1531
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001532 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001533
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301534 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001535 /* Only MMC1 can interface at 3V without some flavor
1536 * of external transceiver; but they all handle 1.8V.
1537 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001538 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1539 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1540 /*
1541 * The mmc_select_voltage fn of the core does
1542 * not seem to set the power_mode to
1543 * MMC_POWER_UP upon recalculating the voltage.
1544 * vdd 1.8v.
1545 */
Denis Karpov70a33412009-09-22 16:44:59 -07001546 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1547 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001548 "Switch operation failed\n");
1549 }
1550 }
1551
Andy Shevchenko5934df22011-05-06 12:14:06 +03001552 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001553
Adrian Huntera3621462009-09-22 16:44:42 -07001554 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001555 send_init_stream(host);
1556
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001557 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001558
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301559 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001560}
1561
1562static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1563{
Denis Karpov70a33412009-09-22 16:44:59 -07001564 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001565
Denis Karpov191d1f12009-09-22 16:44:55 -07001566 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001567 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001568 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001569}
1570
1571static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1572{
Denis Karpov70a33412009-09-22 16:44:59 -07001573 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001574
Denis Karpov191d1f12009-09-22 16:44:55 -07001575 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001576 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001577 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001578}
1579
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001580static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1581{
1582 struct omap_hsmmc_host *host = mmc_priv(mmc);
1583
1584 if (mmc_slot(host).init_card)
1585 mmc_slot(host).init_card(card);
1586}
1587
Denis Karpov70a33412009-09-22 16:44:59 -07001588static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001589{
1590 u32 hctl, capa, value;
1591
1592 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301593 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001594 hctl = SDVS30;
1595 capa = VS30 | VS18;
1596 } else {
1597 hctl = SDVS18;
1598 capa = VS18;
1599 }
1600
1601 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1602 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1603
1604 value = OMAP_HSMMC_READ(host->base, CAPA);
1605 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1606
1607 /* Set the controller to AUTO IDLE mode */
1608 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1609 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1610
1611 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001612 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001613}
1614
Denis Karpov70a33412009-09-22 16:44:59 -07001615static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001616{
Denis Karpov70a33412009-09-22 16:44:59 -07001617 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001618
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301619 pm_runtime_get_sync(host->dev);
1620
Denis Karpovdd498ef2009-09-22 16:44:49 -07001621 return 0;
1622}
1623
Adrian Hunter907d2e72012-02-29 09:17:21 +02001624static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001625{
Denis Karpov70a33412009-09-22 16:44:59 -07001626 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001627
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301628 pm_runtime_mark_last_busy(host->dev);
1629 pm_runtime_put_autosuspend(host->dev);
1630
Denis Karpovdd498ef2009-09-22 16:44:49 -07001631 return 0;
1632}
1633
Denis Karpov70a33412009-09-22 16:44:59 -07001634static const struct mmc_host_ops omap_hsmmc_ops = {
1635 .enable = omap_hsmmc_enable_fclk,
1636 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001637 .post_req = omap_hsmmc_post_req,
1638 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001639 .request = omap_hsmmc_request,
1640 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001641 .get_cd = omap_hsmmc_get_cd,
1642 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001643 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001644 /* NYET -- enable_sdio_irq */
1645};
1646
Denis Karpovd900f712009-09-22 16:44:38 -07001647#ifdef CONFIG_DEBUG_FS
1648
Denis Karpov70a33412009-09-22 16:44:59 -07001649static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001650{
1651 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001652 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001653 int context_loss = 0;
1654
Denis Karpov70a33412009-09-22 16:44:59 -07001655 if (host->pdata->get_context_loss_count)
1656 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001657
Adrian Hunter907d2e72012-02-29 09:17:21 +02001658 seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1659 mmc->index, host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001660
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301661 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001662 seq_printf(s, "host suspended, can't read registers\n");
1663 return 0;
1664 }
1665
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301666 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001667
1668 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1669 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1670 seq_printf(s, "CON:\t\t0x%08x\n",
1671 OMAP_HSMMC_READ(host->base, CON));
1672 seq_printf(s, "HCTL:\t\t0x%08x\n",
1673 OMAP_HSMMC_READ(host->base, HCTL));
1674 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1675 OMAP_HSMMC_READ(host->base, SYSCTL));
1676 seq_printf(s, "IE:\t\t0x%08x\n",
1677 OMAP_HSMMC_READ(host->base, IE));
1678 seq_printf(s, "ISE:\t\t0x%08x\n",
1679 OMAP_HSMMC_READ(host->base, ISE));
1680 seq_printf(s, "CAPA:\t\t0x%08x\n",
1681 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001682
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301683 pm_runtime_mark_last_busy(host->dev);
1684 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001685
Denis Karpovd900f712009-09-22 16:44:38 -07001686 return 0;
1687}
1688
Denis Karpov70a33412009-09-22 16:44:59 -07001689static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001690{
Denis Karpov70a33412009-09-22 16:44:59 -07001691 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001692}
1693
1694static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001695 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001696 .read = seq_read,
1697 .llseek = seq_lseek,
1698 .release = single_release,
1699};
1700
Denis Karpov70a33412009-09-22 16:44:59 -07001701static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001702{
1703 if (mmc->debugfs_root)
1704 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1705 mmc, &mmc_regs_fops);
1706}
1707
1708#else
1709
Denis Karpov70a33412009-09-22 16:44:59 -07001710static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001711{
1712}
1713
1714#endif
1715
Rajendra Nayak46856a62012-03-12 20:32:37 +05301716#ifdef CONFIG_OF
1717static u16 omap4_reg_offset = 0x100;
1718
1719static const struct of_device_id omap_mmc_of_match[] = {
1720 {
1721 .compatible = "ti,omap2-hsmmc",
1722 },
1723 {
1724 .compatible = "ti,omap3-hsmmc",
1725 },
1726 {
1727 .compatible = "ti,omap4-hsmmc",
1728 .data = &omap4_reg_offset,
1729 },
1730 {},
1731}
1732MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1733
1734static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1735{
1736 struct omap_mmc_platform_data *pdata;
1737 struct device_node *np = dev->of_node;
1738 u32 bus_width;
1739
1740 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1741 if (!pdata)
1742 return NULL; /* out of memory */
1743
1744 if (of_find_property(np, "ti,dual-volt", NULL))
1745 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1746
1747 /* This driver only supports 1 slot */
1748 pdata->nr_slots = 1;
1749 pdata->slots[0].switch_pin = of_get_named_gpio(np, "cd-gpios", 0);
1750 pdata->slots[0].gpio_wp = of_get_named_gpio(np, "wp-gpios", 0);
1751
1752 if (of_find_property(np, "ti,non-removable", NULL)) {
1753 pdata->slots[0].nonremovable = true;
1754 pdata->slots[0].no_regulator_off_init = true;
1755 }
1756 of_property_read_u32(np, "ti,bus-width", &bus_width);
1757 if (bus_width == 4)
1758 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1759 else if (bus_width == 8)
1760 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1761
1762 if (of_find_property(np, "ti,needs-special-reset", NULL))
1763 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1764
1765 return pdata;
1766}
1767#else
1768static inline struct omap_mmc_platform_data
1769 *of_get_hsmmc_pdata(struct device *dev)
1770{
1771 return NULL;
1772}
1773#endif
1774
Denis Karpov70a33412009-09-22 16:44:59 -07001775static int __init omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001776{
1777 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1778 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001779 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001780 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001781 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301782 const struct of_device_id *match;
1783
1784 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1785 if (match) {
1786 pdata = of_get_hsmmc_pdata(&pdev->dev);
1787 if (match->data) {
1788 u16 *offsetp = match->data;
1789 pdata->reg_offset = *offsetp;
1790 }
1791 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001792
1793 if (pdata == NULL) {
1794 dev_err(&pdev->dev, "Platform Data is missing\n");
1795 return -ENXIO;
1796 }
1797
1798 if (pdata->nr_slots == 0) {
1799 dev_err(&pdev->dev, "No Slots\n");
1800 return -ENXIO;
1801 }
1802
1803 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1804 irq = platform_get_irq(pdev, 0);
1805 if (res == NULL || irq < 0)
1806 return -ENXIO;
1807
kishore kadiyala91a0b082010-10-01 16:35:28 -07001808 res->start += pdata->reg_offset;
1809 res->end += pdata->reg_offset;
Chris Ball984b2032011-03-22 16:34:42 -07001810 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001811 if (res == NULL)
1812 return -EBUSY;
1813
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001814 ret = omap_hsmmc_gpio_init(pdata);
1815 if (ret)
1816 goto err;
1817
Denis Karpov70a33412009-09-22 16:44:59 -07001818 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001819 if (!mmc) {
1820 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001821 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001822 }
1823
1824 host = mmc_priv(mmc);
1825 host->mmc = mmc;
1826 host->pdata = pdata;
1827 host->dev = &pdev->dev;
1828 host->use_dma = 1;
1829 host->dev->dma_mask = &pdata->dma_mask;
1830 host->dma_ch = -1;
1831 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001832 host->slot_id = 0;
1833 host->mapbase = res->start;
1834 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001835 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001836 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001837
1838 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001839
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301840 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001841
Adrian Huntere0eb2422010-02-15 10:03:34 -08001842 /*
1843 * If regulator_disable can only put vcc_aux to sleep then there is
1844 * no off state.
1845 */
1846 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1847 mmc_slot(host).no_off = 1;
1848
Daniel Mackd418ed82012-02-19 13:20:33 +01001849 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1850
1851 if (pdata->max_freq > 0)
1852 mmc->f_max = pdata->max_freq;
1853 else
1854 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001855
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001856 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001857
Russell King6f7607c2009-01-28 10:22:50 +00001858 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001859 if (IS_ERR(host->fclk)) {
1860 ret = PTR_ERR(host->fclk);
1861 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001862 goto err1;
1863 }
1864
Denis Karpov70a33412009-09-22 16:44:59 -07001865 omap_hsmmc_context_save(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001866
Paul Walmsley9b682562011-10-06 14:50:35 -06001867 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1868 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1869 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1870 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001871
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301872 pm_runtime_enable(host->dev);
1873 pm_runtime_get_sync(host->dev);
1874 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1875 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001876
Adrian Hunter2bec0892009-09-22 16:45:02 -07001877 if (cpu_is_omap2430()) {
1878 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1879 /*
1880 * MMC can still work without debounce clock.
1881 */
1882 if (IS_ERR(host->dbclk))
1883 dev_warn(mmc_dev(host->mmc),
1884 "Failed to get debounce clock\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001885 else
Adrian Hunter2bec0892009-09-22 16:45:02 -07001886 host->got_dbclk = 1;
1887
1888 if (host->got_dbclk)
1889 if (clk_enable(host->dbclk) != 0)
1890 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
1891 " clk failed\n");
1892 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001893
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001894 /* Since we do only SG emulation, we can have as many segs
1895 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001896 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001897
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001898 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1899 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1900 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1901 mmc->max_seg_size = mmc->max_req_size;
1902
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001903 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001904 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001905
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001906 mmc->caps |= mmc_slot(host).caps;
1907 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001908 mmc->caps |= MMC_CAP_4_BIT_DATA;
1909
Denis Karpov191d1f12009-09-22 16:44:55 -07001910 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001911 mmc->caps |= MMC_CAP_NONREMOVABLE;
1912
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001913 mmc->pm_caps = mmc_slot(host).pm_caps;
1914
Denis Karpov70a33412009-09-22 16:44:59 -07001915 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001916
Balaji T Kb7bf7732012-03-07 09:55:30 -05001917 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1918 if (!res) {
1919 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001920 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001921 }
Balaji T Kb7bf7732012-03-07 09:55:30 -05001922 host->dma_line_tx = res->start;
1923
1924 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1925 if (!res) {
1926 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
1927 goto err_irq;
1928 }
1929 host->dma_line_rx = res->start;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001930
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001931 /* Request IRQ for MMC operations */
Yong Zhangd9618e92011-09-22 16:59:04 +08001932 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001933 mmc_hostname(mmc), host);
1934 if (ret) {
1935 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1936 goto err_irq;
1937 }
1938
1939 if (pdata->init != NULL) {
1940 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07001941 dev_dbg(mmc_dev(host->mmc),
1942 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001943 goto err_irq_cd_init;
1944 }
1945 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001946
Adrian Hunterb702b102010-02-15 10:03:35 -08001947 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001948 ret = omap_hsmmc_reg_get(host);
1949 if (ret)
1950 goto err_reg;
1951 host->use_reg = 1;
1952 }
1953
David Brownellb583f262009-05-28 14:04:03 -07001954 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001955
1956 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001957 if ((mmc_slot(host).card_detect_irq)) {
NeilBrown7efab4f2011-12-30 12:35:13 +11001958 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1959 NULL,
1960 omap_hsmmc_detect,
1961 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1962 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001963 if (ret) {
1964 dev_dbg(mmc_dev(host->mmc),
1965 "Unable to grab MMC CD IRQ\n");
1966 goto err_irq_cd;
1967 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00001968 pdata->suspend = omap_hsmmc_suspend_cdirq;
1969 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001970 }
1971
Adrian Hunterb4175772010-05-26 14:42:06 -07001972 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001973
Adrian Hunterb62f6222009-09-22 16:45:01 -07001974 omap_hsmmc_protect_card(host);
1975
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001976 mmc_add_host(mmc);
1977
Denis Karpov191d1f12009-09-22 16:44:55 -07001978 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001979 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1980 if (ret < 0)
1981 goto err_slot_name;
1982 }
Denis Karpov191d1f12009-09-22 16:44:55 -07001983 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001984 ret = device_create_file(&mmc->class_dev,
1985 &dev_attr_cover_switch);
1986 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001987 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001988 }
1989
Denis Karpov70a33412009-09-22 16:44:59 -07001990 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301991 pm_runtime_mark_last_busy(host->dev);
1992 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001993
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001994 return 0;
1995
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001996err_slot_name:
1997 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001998 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001999err_irq_cd:
2000 if (host->use_reg)
2001 omap_hsmmc_reg_put(host);
2002err_reg:
2003 if (host->pdata->cleanup)
2004 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002005err_irq_cd_init:
2006 free_irq(host->irq, host);
2007err_irq:
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302008 pm_runtime_mark_last_busy(host->dev);
2009 pm_runtime_put_autosuspend(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002010 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002011 clk_put(host->fclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002012 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002013 clk_disable(host->dbclk);
2014 clk_put(host->dbclk);
2015 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002016err1:
2017 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002018 platform_set_drvdata(pdev, NULL);
2019 mmc_free_host(mmc);
2020err_alloc:
2021 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002022err:
Chris Ball984b2032011-03-22 16:34:42 -07002023 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002024 return ret;
2025}
2026
Denis Karpov70a33412009-09-22 16:44:59 -07002027static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002028{
Denis Karpov70a33412009-09-22 16:44:59 -07002029 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002030 struct resource *res;
2031
2032 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302033 pm_runtime_get_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002034 mmc_remove_host(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002035 if (host->use_reg)
2036 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002037 if (host->pdata->cleanup)
2038 host->pdata->cleanup(&pdev->dev);
2039 free_irq(host->irq, host);
2040 if (mmc_slot(host).card_detect_irq)
2041 free_irq(mmc_slot(host).card_detect_irq, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002042
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302043 pm_runtime_put_sync(host->dev);
2044 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002045 clk_put(host->fclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002046 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002047 clk_disable(host->dbclk);
2048 clk_put(host->dbclk);
2049 }
2050
2051 mmc_free_host(host->mmc);
2052 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002053 omap_hsmmc_gpio_free(pdev->dev.platform_data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002054 }
2055
2056 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2057 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002058 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002059 platform_set_drvdata(pdev, NULL);
2060
2061 return 0;
2062}
2063
2064#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002065static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002066{
2067 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002068 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002069 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002070
2071 if (host && host->suspended)
2072 return 0;
2073
2074 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302075 pm_runtime_get_sync(host->dev);
Adrian Huntera6b22402009-09-22 16:44:45 -07002076 host->suspended = 1;
2077 if (host->pdata->suspend) {
2078 ret = host->pdata->suspend(&pdev->dev,
2079 host->slot_id);
2080 if (ret) {
2081 dev_dbg(mmc_dev(host->mmc),
2082 "Unable to handle MMC board"
2083 " level suspend\n");
2084 host->suspended = 0;
2085 return ret;
2086 }
2087 }
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002088 ret = mmc_suspend_host(host->mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302089
Eliad Peller31f9d462011-11-22 16:02:17 +02002090 if (ret) {
Adrian Huntera6b22402009-09-22 16:44:45 -07002091 host->suspended = 0;
2092 if (host->pdata->resume) {
2093 ret = host->pdata->resume(&pdev->dev,
2094 host->slot_id);
2095 if (ret)
2096 dev_dbg(mmc_dev(host->mmc),
2097 "Unmask interrupt failed\n");
2098 }
Eliad Peller31f9d462011-11-22 16:02:17 +02002099 goto err;
Adrian Huntera6b22402009-09-22 16:44:45 -07002100 }
Eliad Peller31f9d462011-11-22 16:02:17 +02002101
2102 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2103 omap_hsmmc_disable_irq(host);
2104 OMAP_HSMMC_WRITE(host->base, HCTL,
2105 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2106 }
2107 if (host->got_dbclk)
2108 clk_disable(host->dbclk);
2109
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002110 }
Eliad Peller31f9d462011-11-22 16:02:17 +02002111err:
2112 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002113 return ret;
2114}
2115
2116/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002117static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002118{
2119 int ret = 0;
Kevin Hilmana791daa2010-05-26 14:42:07 -07002120 struct platform_device *pdev = to_platform_device(dev);
Denis Karpov70a33412009-09-22 16:44:59 -07002121 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002122
2123 if (host && !host->suspended)
2124 return 0;
2125
2126 if (host) {
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302127 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002128
Adrian Hunter2bec0892009-09-22 16:45:02 -07002129 if (host->got_dbclk)
2130 clk_enable(host->dbclk);
2131
Eliad Peller31f9d462011-11-22 16:02:17 +02002132 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2133 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002134
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002135 if (host->pdata->resume) {
2136 ret = host->pdata->resume(&pdev->dev, host->slot_id);
2137 if (ret)
2138 dev_dbg(mmc_dev(host->mmc),
2139 "Unmask interrupt failed\n");
2140 }
2141
Adrian Hunterb62f6222009-09-22 16:45:01 -07002142 omap_hsmmc_protect_card(host);
2143
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002144 /* Notify the core to resume the host */
2145 ret = mmc_resume_host(host->mmc);
2146 if (ret == 0)
2147 host->suspended = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302148
2149 pm_runtime_mark_last_busy(host->dev);
2150 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002151 }
2152
2153 return ret;
2154
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002155}
2156
2157#else
Denis Karpov70a33412009-09-22 16:44:59 -07002158#define omap_hsmmc_suspend NULL
2159#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002160#endif
2161
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302162static int omap_hsmmc_runtime_suspend(struct device *dev)
2163{
2164 struct omap_hsmmc_host *host;
2165
2166 host = platform_get_drvdata(to_platform_device(dev));
2167 omap_hsmmc_context_save(host);
2168 dev_dbg(mmc_dev(host->mmc), "disabled\n");
2169
2170 return 0;
2171}
2172
2173static int omap_hsmmc_runtime_resume(struct device *dev)
2174{
2175 struct omap_hsmmc_host *host;
2176
2177 host = platform_get_drvdata(to_platform_device(dev));
2178 omap_hsmmc_context_restore(host);
2179 dev_dbg(mmc_dev(host->mmc), "enabled\n");
2180
2181 return 0;
2182}
2183
Kevin Hilmana791daa2010-05-26 14:42:07 -07002184static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002185 .suspend = omap_hsmmc_suspend,
2186 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302187 .runtime_suspend = omap_hsmmc_runtime_suspend,
2188 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002189};
2190
2191static struct platform_driver omap_hsmmc_driver = {
2192 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002193 .driver = {
2194 .name = DRIVER_NAME,
2195 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002196 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302197 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002198 },
2199};
2200
Denis Karpov70a33412009-09-22 16:44:59 -07002201static int __init omap_hsmmc_init(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002202{
2203 /* Register the MMC driver */
Roger Quadros87532982009-10-26 16:49:38 -07002204 return platform_driver_probe(&omap_hsmmc_driver, omap_hsmmc_probe);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002205}
2206
Denis Karpov70a33412009-09-22 16:44:59 -07002207static void __exit omap_hsmmc_cleanup(void)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002208{
2209 /* Unregister MMC driver */
Denis Karpov70a33412009-09-22 16:44:59 -07002210 platform_driver_unregister(&omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002211}
2212
Denis Karpov70a33412009-09-22 16:44:59 -07002213module_init(omap_hsmmc_init);
2214module_exit(omap_hsmmc_cleanup);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002215
2216MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2217MODULE_LICENSE("GPL");
2218MODULE_ALIAS("platform:" DRIVER_NAME);
2219MODULE_AUTHOR("Texas Instruments Inc");