blob: 9a7a60aeb19ea35dc921cd43f2e796c2bfd75469 [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 Shevchenkoac330f442011-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)
Balaji T Kdba3c292012-04-09 12:08:32 +053088#define ACEN_ACMD12 (1 << 2)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010089#define DP_SELECT (1 << 21)
90#define DDIR (1 << 4)
91#define DMA_EN 0x1
92#define MSBS (1 << 5)
93#define BCE (1 << 1)
94#define FOUR_BIT (1 << 1)
Balaji T K03b5d9242012-04-09 12:08:33 +053095#define DDR (1 << 19)
Jarkko Lavinen73153012008-11-21 16:49:54 +020096#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010097#define CC 0x1
98#define TC 0x02
99#define OD 0x1
100#define ERR (1 << 15)
101#define CMD_TIMEOUT (1 << 16)
102#define DATA_TIMEOUT (1 << 20)
103#define CMD_CRC (1 << 17)
104#define DATA_CRC (1 << 21)
105#define CARD_ERR (1 << 28)
106#define STAT_CLEAR 0xFFFFFFFF
107#define INIT_STREAM_CMD 0x00000000
108#define DUAL_VOLT_OCR_BIT 7
109#define SRC (1 << 25)
110#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700111#define SOFTRESET (1 << 1)
112#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100113
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530114#define MMC_AUTOSUSPEND_DELAY 100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100115#define MMC_TIMEOUT_MS 20
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400116#define OMAP_MMC_MIN_CLOCK 400000
117#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530118#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100119
Balaji T Kdba3c292012-04-09 12:08:32 +0530120#define AUTO_CMD12 (1 << 0) /* Auto CMD12 support */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100121/*
122 * One controller can have multiple slots, like on some omap boards using
123 * omap.c controller driver. Luckily this is not currently done on any known
124 * omap_hsmmc.c device.
125 */
126#define mmc_slot(host) (host->pdata->slots[host->slot_id])
127
128/*
129 * MMC Host controller read/write API's
130 */
131#define OMAP_HSMMC_READ(base, reg) \
132 __raw_readl((base) + OMAP_HSMMC_##reg)
133
134#define OMAP_HSMMC_WRITE(base, reg, val) \
135 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
136
Per Forlin9782aff2011-07-01 18:55:23 +0200137struct omap_hsmmc_next {
138 unsigned int dma_len;
139 s32 cookie;
140};
141
Denis Karpov70a33412009-09-22 16:44:59 -0700142struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100143 struct device *dev;
144 struct mmc_host *mmc;
145 struct mmc_request *mrq;
146 struct mmc_command *cmd;
147 struct mmc_data *data;
148 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100149 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800150 /*
151 * vcc == configured supply
152 * vcc_aux == optional
153 * - MMC1, supply for DAT4..DAT7
154 * - MMC2/MMC2, external level shifter voltage supply, for
155 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
156 */
157 struct regulator *vcc;
158 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100159 void __iomem *base;
160 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700161 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100162 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200163 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100164 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700165 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100166 u32 *buffer;
167 u32 bytesleft;
168 int suspended;
169 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100170 int use_dma, dma_ch;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000171 int dma_line_tx, dma_line_rx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100172 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200173 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700174 int context_loss;
Adrian Hunter623821f2009-09-22 16:44:51 -0700175 int vdd;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700176 int protect_card;
177 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800178 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700179 int req_in_progress;
Balaji T Kdba3c292012-04-09 12:08:32 +0530180 unsigned int flags;
Per Forlin9782aff2011-07-01 18:55:23 +0200181 struct omap_hsmmc_next next_data;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700182
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100183 struct omap_mmc_platform_data *pdata;
184};
185
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800186static int omap_hsmmc_card_detect(struct device *dev, int slot)
187{
188 struct omap_mmc_platform_data *mmc = dev->platform_data;
189
190 /* NOTE: assumes card detect signal is active-low */
191 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
192}
193
194static int omap_hsmmc_get_wp(struct device *dev, int slot)
195{
196 struct omap_mmc_platform_data *mmc = dev->platform_data;
197
198 /* NOTE: assumes write protect signal is active-high */
199 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
200}
201
202static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
203{
204 struct omap_mmc_platform_data *mmc = dev->platform_data;
205
206 /* NOTE: assumes card detect signal is active-low */
207 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
208}
209
210#ifdef CONFIG_PM
211
212static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
213{
214 struct omap_mmc_platform_data *mmc = dev->platform_data;
215
216 disable_irq(mmc->slots[0].card_detect_irq);
217 return 0;
218}
219
220static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
221{
222 struct omap_mmc_platform_data *mmc = dev->platform_data;
223
224 enable_irq(mmc->slots[0].card_detect_irq);
225 return 0;
226}
227
228#else
229
230#define omap_hsmmc_suspend_cdirq NULL
231#define omap_hsmmc_resume_cdirq NULL
232
233#endif
234
Adrian Hunterb702b102010-02-15 10:03:35 -0800235#ifdef CONFIG_REGULATOR
236
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500237static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800238 int vdd)
239{
240 struct omap_hsmmc_host *host =
241 platform_get_drvdata(to_platform_device(dev));
242 int ret = 0;
243
244 /*
245 * If we don't see a Vcc regulator, assume it's a fixed
246 * voltage always-on regulator.
247 */
248 if (!host->vcc)
249 return 0;
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530250 /*
251 * With DT, never turn OFF the regulator. This is because
252 * the pbias cell programming support is still missing when
253 * booting with Device tree
254 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +0530255 if (dev->of_node && !vdd)
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530256 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800257
258 if (mmc_slot(host).before_set_reg)
259 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
260
261 /*
262 * Assume Vcc regulator is used only to power the card ... OMAP
263 * VDDS is used to power the pins, optionally with a transceiver to
264 * support cards using voltages other than VDDS (1.8V nominal). When a
265 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
266 *
267 * In some cases this regulator won't support enable/disable;
268 * e.g. it's a fixed rail for a WLAN chip.
269 *
270 * In other cases vcc_aux switches interface power. Example, for
271 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
272 * chips/cards need an interface voltage rail too.
273 */
274 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400275 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800276 /* Enable interface voltage rail, if needed */
277 if (ret == 0 && host->vcc_aux) {
278 ret = regulator_enable(host->vcc_aux);
279 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400280 ret = mmc_regulator_set_ocr(host->mmc,
281 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800282 }
283 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400284 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800285 if (host->vcc_aux)
286 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400287 if (!ret) {
288 /* Then proceed to shut down the local regulator */
289 ret = mmc_regulator_set_ocr(host->mmc,
290 host->vcc, 0);
291 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800292 }
293
294 if (mmc_slot(host).after_set_reg)
295 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
296
297 return ret;
298}
299
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800300static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
301{
302 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700303 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800304
Rajendra Nayak1cb9af42012-03-07 09:55:31 -0500305 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800306
307 reg = regulator_get(host->dev, "vmmc");
308 if (IS_ERR(reg)) {
309 dev_dbg(host->dev, "vmmc regulator missing\n");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800310 } else {
311 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700312 ocr_value = mmc_regulator_get_ocrmask(reg);
313 if (!mmc_slot(host).ocr_mask) {
314 mmc_slot(host).ocr_mask = ocr_value;
315 } else {
316 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530317 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb62012-03-07 09:55:31 -0500318 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700319 mmc_slot(host).ocr_mask = 0;
320 return -EINVAL;
321 }
322 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800323
324 /* Allow an aux regulator */
325 reg = regulator_get(host->dev, "vmmc_aux");
326 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
327
Balaji T Kb1c1df72011-05-30 19:55:34 +0530328 /* For eMMC do not power off when not in sleep state */
329 if (mmc_slot(host).no_regulator_off_init)
330 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800331 /*
332 * UGLY HACK: workaround regulator framework bugs.
333 * When the bootloader leaves a supply active, it's
334 * initialized with zero usecount ... and we can't
335 * disable it without first enabling it. Until the
336 * framework is fixed, we need a workaround like this
337 * (which is safe for MMC, but not in general).
338 */
Adrian Huntere840ce12011-05-06 12:14:10 +0300339 if (regulator_is_enabled(host->vcc) > 0 ||
340 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
341 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
342
343 mmc_slot(host).set_power(host->dev, host->slot_id,
344 1, vdd);
345 mmc_slot(host).set_power(host->dev, host->slot_id,
346 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800347 }
348 }
349
350 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800351}
352
353static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
354{
355 regulator_put(host->vcc);
356 regulator_put(host->vcc_aux);
357 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800358}
359
Adrian Hunterb702b102010-02-15 10:03:35 -0800360static inline int omap_hsmmc_have_reg(void)
361{
362 return 1;
363}
364
365#else
366
367static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
368{
369 return -EINVAL;
370}
371
372static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
373{
374}
375
376static inline int omap_hsmmc_have_reg(void)
377{
378 return 0;
379}
380
381#endif
382
383static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
384{
385 int ret;
386
387 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800388 if (pdata->slots[0].cover)
389 pdata->slots[0].get_cover_state =
390 omap_hsmmc_get_cover_state;
391 else
392 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
393 pdata->slots[0].card_detect_irq =
394 gpio_to_irq(pdata->slots[0].switch_pin);
395 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
396 if (ret)
397 return ret;
398 ret = gpio_direction_input(pdata->slots[0].switch_pin);
399 if (ret)
400 goto err_free_sp;
401 } else
402 pdata->slots[0].switch_pin = -EINVAL;
403
404 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
405 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
406 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
407 if (ret)
408 goto err_free_cd;
409 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
410 if (ret)
411 goto err_free_wp;
412 } else
413 pdata->slots[0].gpio_wp = -EINVAL;
414
415 return 0;
416
417err_free_wp:
418 gpio_free(pdata->slots[0].gpio_wp);
419err_free_cd:
420 if (gpio_is_valid(pdata->slots[0].switch_pin))
421err_free_sp:
422 gpio_free(pdata->slots[0].switch_pin);
423 return ret;
424}
425
426static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
427{
428 if (gpio_is_valid(pdata->slots[0].gpio_wp))
429 gpio_free(pdata->slots[0].gpio_wp);
430 if (gpio_is_valid(pdata->slots[0].switch_pin))
431 gpio_free(pdata->slots[0].switch_pin);
432}
433
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100434/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300435 * Start clock to the card
436 */
437static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
438{
439 OMAP_HSMMC_WRITE(host->base, SYSCTL,
440 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
441}
442
443/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100444 * Stop clock to the card
445 */
Denis Karpov70a33412009-09-22 16:44:59 -0700446static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100447{
448 OMAP_HSMMC_WRITE(host->base, SYSCTL,
449 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
450 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
451 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
452}
453
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700454static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
455 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700456{
457 unsigned int irq_mask;
458
459 if (host->use_dma)
460 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
461 else
462 irq_mask = INT_EN_MASK;
463
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700464 /* Disable timeout for erases */
465 if (cmd->opcode == MMC_ERASE)
466 irq_mask &= ~DTO_ENABLE;
467
Adrian Hunterb4175772010-05-26 14:42:06 -0700468 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
469 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
470 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
471}
472
473static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
474{
475 OMAP_HSMMC_WRITE(host->base, ISE, 0);
476 OMAP_HSMMC_WRITE(host->base, IE, 0);
477 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
478}
479
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300480/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530481static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300482{
483 u16 dsor = 0;
484
485 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530486 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300487 if (dsor > 250)
488 dsor = 250;
489 }
490
491 return dsor;
492}
493
Andy Shevchenko5934df22011-05-06 12:14:06 +0300494static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
495{
496 struct mmc_ios *ios = &host->mmc->ios;
497 unsigned long regval;
498 unsigned long timeout;
499
500 dev_dbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
501
502 omap_hsmmc_stop_clock(host);
503
504 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
505 regval = regval & ~(CLKD_MASK | DTO_MASK);
Balaji TKd83b6e02011-12-20 15:12:00 +0530506 regval = regval | (calc_divisor(host, ios) << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300507 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
508 OMAP_HSMMC_WRITE(host->base, SYSCTL,
509 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
510
511 /* Wait till the ICS bit is set */
512 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
513 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
514 && time_before(jiffies, timeout))
515 cpu_relax();
516
517 omap_hsmmc_start_clock(host);
518}
519
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400520static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
521{
522 struct mmc_ios *ios = &host->mmc->ios;
523 u32 con;
524
525 con = OMAP_HSMMC_READ(host->base, CON);
Balaji T K03b5d9242012-04-09 12:08:33 +0530526 if (ios->timing == MMC_TIMING_UHS_DDR50)
527 con |= DDR; /* configure in DDR mode */
528 else
529 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400530 switch (ios->bus_width) {
531 case MMC_BUS_WIDTH_8:
532 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
533 break;
534 case MMC_BUS_WIDTH_4:
535 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
536 OMAP_HSMMC_WRITE(host->base, HCTL,
537 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
538 break;
539 case MMC_BUS_WIDTH_1:
540 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
541 OMAP_HSMMC_WRITE(host->base, HCTL,
542 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
543 break;
544 }
545}
546
547static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
548{
549 struct mmc_ios *ios = &host->mmc->ios;
550 u32 con;
551
552 con = OMAP_HSMMC_READ(host->base, CON);
553 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
554 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
555 else
556 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
557}
558
Denis Karpov11dd62a2009-09-22 16:44:43 -0700559#ifdef CONFIG_PM
560
561/*
562 * Restore the MMC host context, if it was lost as result of a
563 * power state change.
564 */
Denis Karpov70a33412009-09-22 16:44:59 -0700565static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700566{
567 struct mmc_ios *ios = &host->mmc->ios;
568 struct omap_mmc_platform_data *pdata = host->pdata;
569 int context_loss = 0;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400570 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700571 unsigned long timeout;
572
573 if (pdata->get_context_loss_count) {
574 context_loss = pdata->get_context_loss_count(host->dev);
575 if (context_loss < 0)
576 return 1;
577 }
578
579 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
580 context_loss == host->context_loss ? "not " : "");
581 if (host->context_loss == context_loss)
582 return 1;
583
584 /* Wait for hardware reset */
585 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
586 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
587 && time_before(jiffies, timeout))
588 ;
589
590 /* Do software reset */
591 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
592 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
593 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
594 && time_before(jiffies, timeout))
595 ;
596
597 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
598 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
599
Balaji T Kc2200ef2012-03-07 09:55:30 -0500600 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700601 if (host->power_mode != MMC_POWER_OFF &&
602 (1 << ios->vdd) <= MMC_VDD_23_24)
603 hctl = SDVS18;
604 else
605 hctl = SDVS30;
606 capa = VS30 | VS18;
607 } else {
608 hctl = SDVS18;
609 capa = VS18;
610 }
611
612 OMAP_HSMMC_WRITE(host->base, HCTL,
613 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
614
615 OMAP_HSMMC_WRITE(host->base, CAPA,
616 OMAP_HSMMC_READ(host->base, CAPA) | capa);
617
618 OMAP_HSMMC_WRITE(host->base, HCTL,
619 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
620
621 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
622 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
623 && time_before(jiffies, timeout))
624 ;
625
Adrian Hunterb4175772010-05-26 14:42:06 -0700626 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700627
628 /* Do not initialize card-specific things if the power is off */
629 if (host->power_mode == MMC_POWER_OFF)
630 goto out;
631
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400632 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700633
Andy Shevchenko5934df22011-05-06 12:14:06 +0300634 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700635
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400636 omap_hsmmc_set_bus_mode(host);
637
Denis Karpov11dd62a2009-09-22 16:44:43 -0700638out:
639 host->context_loss = context_loss;
640
641 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
642 return 0;
643}
644
645/*
646 * Save the MMC host context (store the number of power state changes so far).
647 */
Denis Karpov70a33412009-09-22 16:44:59 -0700648static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700649{
650 struct omap_mmc_platform_data *pdata = host->pdata;
651 int context_loss;
652
653 if (pdata->get_context_loss_count) {
654 context_loss = pdata->get_context_loss_count(host->dev);
655 if (context_loss < 0)
656 return;
657 host->context_loss = context_loss;
658 }
659}
660
661#else
662
Denis Karpov70a33412009-09-22 16:44:59 -0700663static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700664{
665 return 0;
666}
667
Denis Karpov70a33412009-09-22 16:44:59 -0700668static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700669{
670}
671
672#endif
673
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100674/*
675 * Send init stream sequence to card
676 * before sending IDLE command
677 */
Denis Karpov70a33412009-09-22 16:44:59 -0700678static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100679{
680 int reg = 0;
681 unsigned long timeout;
682
Adrian Hunterb62f6222009-09-22 16:45:01 -0700683 if (host->protect_card)
684 return;
685
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100686 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700687
688 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100689 OMAP_HSMMC_WRITE(host->base, CON,
690 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
691 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
692
693 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
694 while ((reg != CC) && time_before(jiffies, timeout))
695 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
696
697 OMAP_HSMMC_WRITE(host->base, CON,
698 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700699
700 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
701 OMAP_HSMMC_READ(host->base, STAT);
702
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100703 enable_irq(host->irq);
704}
705
706static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700707int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100708{
709 int r = 1;
710
Denis Karpov191d1f12009-09-22 16:44:55 -0700711 if (mmc_slot(host).get_cover_state)
712 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100713 return r;
714}
715
716static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700717omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100718 char *buf)
719{
720 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700721 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100722
Denis Karpov70a33412009-09-22 16:44:59 -0700723 return sprintf(buf, "%s\n",
724 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100725}
726
Denis Karpov70a33412009-09-22 16:44:59 -0700727static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100728
729static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700730omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100731 char *buf)
732{
733 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700734 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100735
Denis Karpov191d1f12009-09-22 16:44:55 -0700736 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100737}
738
Denis Karpov70a33412009-09-22 16:44:59 -0700739static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100740
741/*
742 * Configure the response type and send the cmd.
743 */
744static void
Denis Karpov70a33412009-09-22 16:44:59 -0700745omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100746 struct mmc_data *data)
747{
748 int cmdreg = 0, resptype = 0, cmdtype = 0;
749
750 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
751 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
752 host->cmd = cmd;
753
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700754 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100755
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200756 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100757 if (cmd->flags & MMC_RSP_PRESENT) {
758 if (cmd->flags & MMC_RSP_136)
759 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200760 else if (cmd->flags & MMC_RSP_BUSY) {
761 resptype = 3;
762 host->response_busy = 1;
763 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100764 resptype = 2;
765 }
766
767 /*
768 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
769 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
770 * a val of 0x3, rest 0x0.
771 */
772 if (cmd == host->mrq->stop)
773 cmdtype = 0x3;
774
775 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
Balaji T Kdba3c292012-04-09 12:08:32 +0530776 if ((host->flags & AUTO_CMD12) && mmc_op_multi(cmd->opcode))
777 cmdreg |= ACEN_ACMD12;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100778
779 if (data) {
780 cmdreg |= DP_SELECT | MSBS | BCE;
781 if (data->flags & MMC_DATA_READ)
782 cmdreg |= DDIR;
783 else
784 cmdreg &= ~(DDIR);
785 }
786
787 if (host->use_dma)
788 cmdreg |= DMA_EN;
789
Adrian Hunterb4175772010-05-26 14:42:06 -0700790 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700791
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100792 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
793 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
794}
795
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200796static int
Denis Karpov70a33412009-09-22 16:44:59 -0700797omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200798{
799 if (data->flags & MMC_DATA_WRITE)
800 return DMA_TO_DEVICE;
801 else
802 return DMA_FROM_DEVICE;
803}
804
Adrian Hunterb4175772010-05-26 14:42:06 -0700805static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
806{
807 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530808 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700809
Venkatraman S31463b12012-04-09 12:08:34 +0530810 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700811 host->req_in_progress = 0;
812 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530813 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700814
815 omap_hsmmc_disable_irq(host);
816 /* Do not complete the request if DMA is still in progress */
817 if (mrq->data && host->use_dma && dma_ch != -1)
818 return;
819 host->mrq = NULL;
820 mmc_request_done(host->mmc, mrq);
821}
822
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100823/*
824 * Notify the transfer complete to MMC core
825 */
826static void
Denis Karpov70a33412009-09-22 16:44:59 -0700827omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100828{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200829 if (!data) {
830 struct mmc_request *mrq = host->mrq;
831
Adrian Hunter23050102009-09-22 16:44:57 -0700832 /* TC before CC from CMD6 - don't know why, but it happens */
833 if (host->cmd && host->cmd->opcode == 6 &&
834 host->response_busy) {
835 host->response_busy = 0;
836 return;
837 }
838
Adrian Hunterb4175772010-05-26 14:42:06 -0700839 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200840 return;
841 }
842
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100843 host->data = NULL;
844
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100845 if (!data->error)
846 data->bytes_xfered += data->blocks * (data->blksz);
847 else
848 data->bytes_xfered = 0;
849
Balaji T Kdba3c292012-04-09 12:08:32 +0530850 if (data->stop && ((!(host->flags & AUTO_CMD12)) || data->error)) {
851 omap_hsmmc_start_command(host, data->stop, NULL);
852 } else {
853 if (data->stop)
854 data->stop->resp[0] = OMAP_HSMMC_READ(host->base,
855 RSP76);
Adrian Hunterb4175772010-05-26 14:42:06 -0700856 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100857 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100858}
859
860/*
861 * Notify the core about command completion
862 */
863static void
Denis Karpov70a33412009-09-22 16:44:59 -0700864omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100865{
866 host->cmd = NULL;
867
868 if (cmd->flags & MMC_RSP_PRESENT) {
869 if (cmd->flags & MMC_RSP_136) {
870 /* response type 2 */
871 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
872 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
873 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
874 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
875 } else {
876 /* response types 1, 1b, 3, 4, 5, 6 */
877 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
878 }
879 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700880 if ((host->data == NULL && !host->response_busy) || cmd->error)
881 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100882}
883
884/*
885 * DMA clean up for command errors
886 */
Denis Karpov70a33412009-09-22 16:44:59 -0700887static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100888{
Adrian Hunterb4175772010-05-26 14:42:06 -0700889 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530890 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700891
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200892 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100893
Venkatraman S31463b12012-04-09 12:08:34 +0530894 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700895 dma_ch = host->dma_ch;
896 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530897 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700898
899 if (host->use_dma && dma_ch != -1) {
Per Forlina9120c32011-06-17 20:14:21 +0200900 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg,
901 host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700902 omap_hsmmc_get_dma_dir(host, host->data));
Adrian Hunterb4175772010-05-26 14:42:06 -0700903 omap_free_dma(dma_ch);
Per Forlin053bf342011-11-07 21:55:11 +0530904 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100905 }
906 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100907}
908
909/*
910 * Readable error output
911 */
912#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300913static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100914{
915 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700916 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300917 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
918 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
919 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
920 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100921 };
922 char res[256];
923 char *buf = res;
924 int len, i;
925
926 len = sprintf(buf, "MMC IRQ 0x%x :", status);
927 buf += len;
928
Denis Karpov70a33412009-09-22 16:44:59 -0700929 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100930 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700931 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100932 buf += len;
933 }
934
935 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
936}
Adrian Hunter699b9582011-05-06 12:14:01 +0300937#else
938static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
939 u32 status)
940{
941}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100942#endif /* CONFIG_MMC_DEBUG */
943
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100944/*
945 * MMC controller internal state machines reset
946 *
947 * Used to reset command or data internal state machines, using respectively
948 * SRC or SRD bit of SYSCTL register
949 * Can be called from interrupt context
950 */
Denis Karpov70a33412009-09-22 16:44:59 -0700951static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
952 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100953{
954 unsigned long i = 0;
955 unsigned long limit = (loops_per_jiffy *
956 msecs_to_jiffies(MMC_TIMEOUT_MS));
957
958 OMAP_HSMMC_WRITE(host->base, SYSCTL,
959 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
960
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700961 /*
962 * OMAP4 ES2 and greater has an updated reset logic.
963 * Monitor a 0->1 transition first
964 */
965 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -0500966 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700967 && (i++ < limit))
968 cpu_relax();
969 }
970 i = 0;
971
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100972 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
973 (i++ < limit))
974 cpu_relax();
975
976 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
977 dev_err(mmc_dev(host->mmc),
978 "Timeout waiting on controller reset in %s\n",
979 __func__);
980}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100981
Adrian Hunterb4175772010-05-26 14:42:06 -0700982static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100983{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100984 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -0700985 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100986
Adrian Hunterb4175772010-05-26 14:42:06 -0700987 if (!host->req_in_progress) {
988 do {
989 OMAP_HSMMC_WRITE(host->base, STAT, status);
990 /* Flush posted write */
991 status = OMAP_HSMMC_READ(host->base, STAT);
992 } while (status & INT_EN_MASK);
993 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100994 }
995
996 data = host->data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100997 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
998
999 if (status & ERR) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001000 omap_hsmmc_dbg_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001001 if ((status & CMD_TIMEOUT) ||
1002 (status & CMD_CRC)) {
1003 if (host->cmd) {
1004 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -07001005 omap_hsmmc_reset_controller_fsm(host,
1006 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001007 host->cmd->error = -ETIMEDOUT;
1008 } else {
1009 host->cmd->error = -EILSEQ;
1010 }
1011 end_cmd = 1;
1012 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001013 if (host->data || host->response_busy) {
1014 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001015 omap_hsmmc_dma_cleanup(host,
1016 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001017 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001018 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f4572009-02-11 13:11:39 -08001019 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001020 }
1021 if ((status & DATA_TIMEOUT) ||
1022 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001023 if (host->data || host->response_busy) {
1024 int err = (status & DATA_TIMEOUT) ?
1025 -ETIMEDOUT : -EILSEQ;
1026
1027 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001028 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001029 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001030 host->mrq->cmd->error = err;
1031 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001032 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001033 end_trans = 1;
1034 }
1035 }
1036 if (status & CARD_ERR) {
1037 dev_dbg(mmc_dev(host->mmc),
1038 "Ignoring card err CMD%d\n", host->cmd->opcode);
1039 if (host->cmd)
1040 end_cmd = 1;
1041 if (host->data)
1042 end_trans = 1;
1043 }
1044 }
1045
1046 OMAP_HSMMC_WRITE(host->base, STAT, status);
1047
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001048 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001049 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001050 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001051 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001052}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001053
Adrian Hunterb4175772010-05-26 14:42:06 -07001054/*
1055 * MMC controller IRQ handler
1056 */
1057static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1058{
1059 struct omap_hsmmc_host *host = dev_id;
1060 int status;
1061
1062 status = OMAP_HSMMC_READ(host->base, STAT);
1063 do {
1064 omap_hsmmc_do_irq(host, status);
1065 /* Flush posted write */
1066 status = OMAP_HSMMC_READ(host->base, STAT);
1067 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001068
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001069 return IRQ_HANDLED;
1070}
1071
Denis Karpov70a33412009-09-22 16:44:59 -07001072static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001073{
1074 unsigned long i;
1075
1076 OMAP_HSMMC_WRITE(host->base, HCTL,
1077 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1078 for (i = 0; i < loops_per_jiffy; i++) {
1079 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1080 break;
1081 cpu_relax();
1082 }
1083}
1084
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001085/*
David Brownelleb250822009-02-17 14:49:01 -08001086 * Switch MMC interface voltage ... only relevant for MMC1.
1087 *
1088 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1089 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1090 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001091 */
Denis Karpov70a33412009-09-22 16:44:59 -07001092static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001093{
1094 u32 reg_val = 0;
1095 int ret;
1096
1097 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301098 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301099 if (host->dbclk)
Adrian Hunter2bec0892009-09-22 16:45:02 -07001100 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001101
1102 /* Turn the power off */
1103 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001104
1105 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001106 if (!ret)
1107 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1108 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301109 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301110 if (host->dbclk)
Adrian Hunter2bec0892009-09-22 16:45:02 -07001111 clk_enable(host->dbclk);
1112
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001113 if (ret != 0)
1114 goto err;
1115
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001116 OMAP_HSMMC_WRITE(host->base, HCTL,
1117 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1118 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001119
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001120 /*
1121 * If a MMC dual voltage card is detected, the set_ios fn calls
1122 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001123 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001124 *
David Brownelleb250822009-02-17 14:49:01 -08001125 * Cope with a bit of slop in the range ... per data sheets:
1126 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1127 * but recommended values are 1.71V to 1.89V
1128 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1129 * but recommended values are 2.7V to 3.3V
1130 *
1131 * Board setup code shouldn't permit anything very out-of-range.
1132 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1133 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001134 */
David Brownelleb250822009-02-17 14:49:01 -08001135 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001136 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001137 else
1138 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001139
1140 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001141 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001142
1143 return 0;
1144err:
1145 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1146 return ret;
1147}
1148
Adrian Hunterb62f6222009-09-22 16:45:01 -07001149/* Protect the card while the cover is open */
1150static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1151{
1152 if (!mmc_slot(host).get_cover_state)
1153 return;
1154
1155 host->reqs_blocked = 0;
1156 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1157 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301158 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001159 "card is now accessible\n",
1160 mmc_hostname(host->mmc));
1161 host->protect_card = 0;
1162 }
1163 } else {
1164 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301165 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001166 "card is now inaccessible\n",
1167 mmc_hostname(host->mmc));
1168 host->protect_card = 1;
1169 }
1170 }
1171}
1172
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001173/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001174 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001175 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001176static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001177{
NeilBrown7efab4f2011-12-30 12:35:13 +11001178 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001179 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001180 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001181
Adrian Huntera6b22402009-09-22 16:44:45 -07001182 if (host->suspended)
NeilBrown7efab4f2011-12-30 12:35:13 +11001183 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001184
1185 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001186
Denis Karpov191d1f12009-09-22 16:44:55 -07001187 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001188 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001189 else {
1190 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001191 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001192 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001193
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001194 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001195 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001196 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001197 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001198 return IRQ_HANDLED;
1199}
1200
Denis Karpov70a33412009-09-22 16:44:59 -07001201static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001202 struct mmc_data *data)
1203{
1204 int sync_dev;
1205
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001206 if (data->flags & MMC_DATA_WRITE)
1207 sync_dev = host->dma_line_tx;
1208 else
1209 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001210 return sync_dev;
1211}
1212
Denis Karpov70a33412009-09-22 16:44:59 -07001213static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001214 struct mmc_data *data,
1215 struct scatterlist *sgl)
1216{
1217 int blksz, nblk, dma_ch;
1218
1219 dma_ch = host->dma_ch;
1220 if (data->flags & MMC_DATA_WRITE) {
1221 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1222 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1223 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1224 sg_dma_address(sgl), 0, 0);
1225 } else {
1226 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -07001227 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001228 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1229 sg_dma_address(sgl), 0, 0);
1230 }
1231
1232 blksz = host->data->blksz;
1233 nblk = sg_dma_len(sgl) / blksz;
1234
1235 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1236 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -07001237 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001238 !(data->flags & MMC_DATA_WRITE));
1239
1240 omap_start_dma(dma_ch);
1241}
1242
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001243/*
1244 * DMA call back function
1245 */
Adrian Hunterb4175772010-05-26 14:42:06 -07001246static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001247{
Adrian Hunterb4175772010-05-26 14:42:06 -07001248 struct omap_hsmmc_host *host = cb_data;
Adrian Hunter770d7432011-05-06 12:14:11 +03001249 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001250 int dma_ch, req_in_progress;
Venkatraman S31463b12012-04-09 12:08:34 +05301251 unsigned long flags;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001252
Venkatraman Sf3584e52010-08-10 18:01:54 -07001253 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1254 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1255 ch_status);
1256 return;
1257 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001258
Venkatraman S31463b12012-04-09 12:08:34 +05301259 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001260 if (host->dma_ch < 0) {
Venkatraman S31463b12012-04-09 12:08:34 +05301261 spin_unlock_irqrestore(&host->irq_lock, flags);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001262 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001263 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001264
Adrian Hunter770d7432011-05-06 12:14:11 +03001265 data = host->mrq->data;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001266 host->dma_sg_idx++;
1267 if (host->dma_sg_idx < host->dma_len) {
1268 /* Fire up the next transfer. */
Adrian Hunterb4175772010-05-26 14:42:06 -07001269 omap_hsmmc_config_dma_params(host, data,
1270 data->sg + host->dma_sg_idx);
Venkatraman S31463b12012-04-09 12:08:34 +05301271 spin_unlock_irqrestore(&host->irq_lock, flags);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001272 return;
1273 }
1274
Per Forlin9782aff2011-07-01 18:55:23 +02001275 if (!data->host_cookie)
1276 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1277 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001278
1279 req_in_progress = host->req_in_progress;
1280 dma_ch = host->dma_ch;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001281 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +05301282 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001283
1284 omap_free_dma(dma_ch);
1285
1286 /* If DMA has finished after TC, complete the request */
1287 if (!req_in_progress) {
1288 struct mmc_request *mrq = host->mrq;
1289
1290 host->mrq = NULL;
1291 mmc_request_done(host->mmc, mrq);
1292 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001293}
1294
Per Forlin9782aff2011-07-01 18:55:23 +02001295static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1296 struct mmc_data *data,
1297 struct omap_hsmmc_next *next)
1298{
1299 int dma_len;
1300
1301 if (!next && data->host_cookie &&
1302 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301303 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001304 " host->next_data.cookie %d\n",
1305 __func__, data->host_cookie, host->next_data.cookie);
1306 data->host_cookie = 0;
1307 }
1308
1309 /* Check if next job is already prepared */
1310 if (next ||
1311 (!next && data->host_cookie != host->next_data.cookie)) {
1312 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
1313 data->sg_len,
1314 omap_hsmmc_get_dma_dir(host, data));
1315
1316 } else {
1317 dma_len = host->next_data.dma_len;
1318 host->next_data.dma_len = 0;
1319 }
1320
1321
1322 if (dma_len == 0)
1323 return -EINVAL;
1324
1325 if (next) {
1326 next->dma_len = dma_len;
1327 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1328 } else
1329 host->dma_len = dma_len;
1330
1331 return 0;
1332}
1333
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001334/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001335 * Routine to configure and start DMA for the MMC card
1336 */
Denis Karpov70a33412009-09-22 16:44:59 -07001337static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1338 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001339{
Adrian Hunterb4175772010-05-26 14:42:06 -07001340 int dma_ch = 0, ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001341 struct mmc_data *data = req->data;
1342
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001343 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001344 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001345 struct scatterlist *sgl;
1346
1347 sgl = data->sg + i;
1348 if (sgl->length % data->blksz)
1349 return -EINVAL;
1350 }
1351 if ((data->blksz % 4) != 0)
1352 /* REVISIT: The MMC buffer increments only when MSB is written.
1353 * Return error for blksz which is non multiple of four.
1354 */
1355 return -EINVAL;
1356
Adrian Hunterb4175772010-05-26 14:42:06 -07001357 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001358
Denis Karpov70a33412009-09-22 16:44:59 -07001359 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1360 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001361 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001362 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001363 "%s: omap_request_dma() failed with %d\n",
1364 mmc_hostname(host->mmc), ret);
1365 return ret;
1366 }
Per Forlin9782aff2011-07-01 18:55:23 +02001367 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL);
1368 if (ret)
1369 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001370
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001371 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001372 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001373
Denis Karpov70a33412009-09-22 16:44:59 -07001374 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001375
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001376 return 0;
1377}
1378
Denis Karpov70a33412009-09-22 16:44:59 -07001379static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001380 unsigned int timeout_ns,
1381 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001382{
1383 unsigned int timeout, cycle_ns;
1384 uint32_t reg, clkd, dto = 0;
1385
1386 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1387 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1388 if (clkd == 0)
1389 clkd = 1;
1390
1391 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001392 timeout = timeout_ns / cycle_ns;
1393 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001394 if (timeout) {
1395 while ((timeout & 0x80000000) == 0) {
1396 dto += 1;
1397 timeout <<= 1;
1398 }
1399 dto = 31 - dto;
1400 timeout <<= 1;
1401 if (timeout && dto)
1402 dto += 1;
1403 if (dto >= 13)
1404 dto -= 13;
1405 else
1406 dto = 0;
1407 if (dto > 14)
1408 dto = 14;
1409 }
1410
1411 reg &= ~DTO_MASK;
1412 reg |= dto << DTO_SHIFT;
1413 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1414}
1415
1416/*
1417 * Configure block length for MMC/SD cards and initiate the transfer.
1418 */
1419static int
Denis Karpov70a33412009-09-22 16:44:59 -07001420omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001421{
1422 int ret;
1423 host->data = req->data;
1424
1425 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001426 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001427 /*
1428 * Set an arbitrary 100ms data timeout for commands with
1429 * busy signal.
1430 */
1431 if (req->cmd->flags & MMC_RSP_BUSY)
1432 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001433 return 0;
1434 }
1435
1436 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1437 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001438 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001439
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001440 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001441 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001442 if (ret != 0) {
1443 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1444 return ret;
1445 }
1446 }
1447 return 0;
1448}
1449
Per Forlin9782aff2011-07-01 18:55:23 +02001450static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1451 int err)
1452{
1453 struct omap_hsmmc_host *host = mmc_priv(mmc);
1454 struct mmc_data *data = mrq->data;
1455
1456 if (host->use_dma) {
Per Forlin053bf342011-11-07 21:55:11 +05301457 if (data->host_cookie)
1458 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
1459 data->sg_len,
1460 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001461 data->host_cookie = 0;
1462 }
1463}
1464
1465static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1466 bool is_first_req)
1467{
1468 struct omap_hsmmc_host *host = mmc_priv(mmc);
1469
1470 if (mrq->data->host_cookie) {
1471 mrq->data->host_cookie = 0;
1472 return ;
1473 }
1474
1475 if (host->use_dma)
1476 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
1477 &host->next_data))
1478 mrq->data->host_cookie = 0;
1479}
1480
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001481/*
1482 * Request function. for read/write operation
1483 */
Denis Karpov70a33412009-09-22 16:44:59 -07001484static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001485{
Denis Karpov70a33412009-09-22 16:44:59 -07001486 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001487 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001488
Adrian Hunterb4175772010-05-26 14:42:06 -07001489 BUG_ON(host->req_in_progress);
1490 BUG_ON(host->dma_ch != -1);
1491 if (host->protect_card) {
1492 if (host->reqs_blocked < 3) {
1493 /*
1494 * Ensure the controller is left in a consistent
1495 * state by resetting the command and data state
1496 * machines.
1497 */
1498 omap_hsmmc_reset_controller_fsm(host, SRD);
1499 omap_hsmmc_reset_controller_fsm(host, SRC);
1500 host->reqs_blocked += 1;
1501 }
1502 req->cmd->error = -EBADF;
1503 if (req->data)
1504 req->data->error = -EBADF;
1505 req->cmd->retries = 0;
1506 mmc_request_done(mmc, req);
1507 return;
1508 } else if (host->reqs_blocked)
1509 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001510 WARN_ON(host->mrq != NULL);
1511 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001512 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001513 if (err) {
1514 req->cmd->error = err;
1515 if (req->data)
1516 req->data->error = err;
1517 host->mrq = NULL;
1518 mmc_request_done(mmc, req);
1519 return;
1520 }
1521
Denis Karpov70a33412009-09-22 16:44:59 -07001522 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001523}
1524
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001525/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001526static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001527{
Denis Karpov70a33412009-09-22 16:44:59 -07001528 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001529 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001530
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301531 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001532
Adrian Huntera3621462009-09-22 16:44:42 -07001533 if (ios->power_mode != host->power_mode) {
1534 switch (ios->power_mode) {
1535 case MMC_POWER_OFF:
1536 mmc_slot(host).set_power(host->dev, host->slot_id,
1537 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001538 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001539 break;
1540 case MMC_POWER_UP:
1541 mmc_slot(host).set_power(host->dev, host->slot_id,
1542 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001543 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001544 break;
1545 case MMC_POWER_ON:
1546 do_send_init_stream = 1;
1547 break;
1548 }
1549 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001550 }
1551
Denis Karpovdd498ef2009-09-22 16:44:49 -07001552 /* FIXME: set registers based only on changes to ios */
1553
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001554 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001555
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301556 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001557 /* Only MMC1 can interface at 3V without some flavor
1558 * of external transceiver; but they all handle 1.8V.
1559 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001560 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Rajendra Nayak1f84b712012-03-12 20:32:38 +05301561 (ios->vdd == DUAL_VOLT_OCR_BIT) &&
1562 /*
1563 * With pbias cell programming missing, this
1564 * can't be allowed when booting with device
1565 * tree.
1566 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +05301567 !host->dev->of_node) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001568 /*
1569 * The mmc_select_voltage fn of the core does
1570 * not seem to set the power_mode to
1571 * MMC_POWER_UP upon recalculating the voltage.
1572 * vdd 1.8v.
1573 */
Denis Karpov70a33412009-09-22 16:44:59 -07001574 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1575 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001576 "Switch operation failed\n");
1577 }
1578 }
1579
Andy Shevchenko5934df22011-05-06 12:14:06 +03001580 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001581
Adrian Huntera3621462009-09-22 16:44:42 -07001582 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001583 send_init_stream(host);
1584
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001585 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001586
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301587 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001588}
1589
1590static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1591{
Denis Karpov70a33412009-09-22 16:44:59 -07001592 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001593
Denis Karpov191d1f12009-09-22 16:44:55 -07001594 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001595 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001596 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001597}
1598
1599static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1600{
Denis Karpov70a33412009-09-22 16:44:59 -07001601 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001602
Denis Karpov191d1f12009-09-22 16:44:55 -07001603 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001604 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001605 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001606}
1607
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001608static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1609{
1610 struct omap_hsmmc_host *host = mmc_priv(mmc);
1611
1612 if (mmc_slot(host).init_card)
1613 mmc_slot(host).init_card(card);
1614}
1615
Denis Karpov70a33412009-09-22 16:44:59 -07001616static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001617{
1618 u32 hctl, capa, value;
1619
1620 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301621 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001622 hctl = SDVS30;
1623 capa = VS30 | VS18;
1624 } else {
1625 hctl = SDVS18;
1626 capa = VS18;
1627 }
1628
1629 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1630 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1631
1632 value = OMAP_HSMMC_READ(host->base, CAPA);
1633 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1634
1635 /* Set the controller to AUTO IDLE mode */
1636 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1637 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1638
1639 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001640 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001641}
1642
Denis Karpov70a33412009-09-22 16:44:59 -07001643static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001644{
Denis Karpov70a33412009-09-22 16:44:59 -07001645 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001646
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301647 pm_runtime_get_sync(host->dev);
1648
Denis Karpovdd498ef2009-09-22 16:44:49 -07001649 return 0;
1650}
1651
Adrian Hunter907d2e72012-02-29 09:17:21 +02001652static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001653{
Denis Karpov70a33412009-09-22 16:44:59 -07001654 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001655
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301656 pm_runtime_mark_last_busy(host->dev);
1657 pm_runtime_put_autosuspend(host->dev);
1658
Denis Karpovdd498ef2009-09-22 16:44:49 -07001659 return 0;
1660}
1661
Denis Karpov70a33412009-09-22 16:44:59 -07001662static const struct mmc_host_ops omap_hsmmc_ops = {
1663 .enable = omap_hsmmc_enable_fclk,
1664 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001665 .post_req = omap_hsmmc_post_req,
1666 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001667 .request = omap_hsmmc_request,
1668 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001669 .get_cd = omap_hsmmc_get_cd,
1670 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001671 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001672 /* NYET -- enable_sdio_irq */
1673};
1674
Denis Karpovd900f712009-09-22 16:44:38 -07001675#ifdef CONFIG_DEBUG_FS
1676
Denis Karpov70a33412009-09-22 16:44:59 -07001677static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001678{
1679 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001680 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001681 int context_loss = 0;
1682
Denis Karpov70a33412009-09-22 16:44:59 -07001683 if (host->pdata->get_context_loss_count)
1684 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001685
Adrian Hunter907d2e72012-02-29 09:17:21 +02001686 seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1687 mmc->index, host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001688
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301689 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001690 seq_printf(s, "host suspended, can't read registers\n");
1691 return 0;
1692 }
1693
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301694 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001695
1696 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1697 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1698 seq_printf(s, "CON:\t\t0x%08x\n",
1699 OMAP_HSMMC_READ(host->base, CON));
1700 seq_printf(s, "HCTL:\t\t0x%08x\n",
1701 OMAP_HSMMC_READ(host->base, HCTL));
1702 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1703 OMAP_HSMMC_READ(host->base, SYSCTL));
1704 seq_printf(s, "IE:\t\t0x%08x\n",
1705 OMAP_HSMMC_READ(host->base, IE));
1706 seq_printf(s, "ISE:\t\t0x%08x\n",
1707 OMAP_HSMMC_READ(host->base, ISE));
1708 seq_printf(s, "CAPA:\t\t0x%08x\n",
1709 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001710
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301711 pm_runtime_mark_last_busy(host->dev);
1712 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001713
Denis Karpovd900f712009-09-22 16:44:38 -07001714 return 0;
1715}
1716
Denis Karpov70a33412009-09-22 16:44:59 -07001717static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001718{
Denis Karpov70a33412009-09-22 16:44:59 -07001719 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001720}
1721
1722static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001723 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001724 .read = seq_read,
1725 .llseek = seq_lseek,
1726 .release = single_release,
1727};
1728
Denis Karpov70a33412009-09-22 16:44:59 -07001729static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001730{
1731 if (mmc->debugfs_root)
1732 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1733 mmc, &mmc_regs_fops);
1734}
1735
1736#else
1737
Denis Karpov70a33412009-09-22 16:44:59 -07001738static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001739{
1740}
1741
1742#endif
1743
Rajendra Nayak46856a62012-03-12 20:32:37 +05301744#ifdef CONFIG_OF
1745static u16 omap4_reg_offset = 0x100;
1746
1747static const struct of_device_id omap_mmc_of_match[] = {
1748 {
1749 .compatible = "ti,omap2-hsmmc",
1750 },
1751 {
1752 .compatible = "ti,omap3-hsmmc",
1753 },
1754 {
1755 .compatible = "ti,omap4-hsmmc",
1756 .data = &omap4_reg_offset,
1757 },
1758 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001759};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301760MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1761
1762static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1763{
1764 struct omap_mmc_platform_data *pdata;
1765 struct device_node *np = dev->of_node;
1766 u32 bus_width;
1767
1768 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1769 if (!pdata)
1770 return NULL; /* out of memory */
1771
1772 if (of_find_property(np, "ti,dual-volt", NULL))
1773 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1774
1775 /* This driver only supports 1 slot */
1776 pdata->nr_slots = 1;
1777 pdata->slots[0].switch_pin = of_get_named_gpio(np, "cd-gpios", 0);
1778 pdata->slots[0].gpio_wp = of_get_named_gpio(np, "wp-gpios", 0);
1779
1780 if (of_find_property(np, "ti,non-removable", NULL)) {
1781 pdata->slots[0].nonremovable = true;
1782 pdata->slots[0].no_regulator_off_init = true;
1783 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001784 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301785 if (bus_width == 4)
1786 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1787 else if (bus_width == 8)
1788 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1789
1790 if (of_find_property(np, "ti,needs-special-reset", NULL))
1791 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1792
1793 return pdata;
1794}
1795#else
1796static inline struct omap_mmc_platform_data
1797 *of_get_hsmmc_pdata(struct device *dev)
1798{
1799 return NULL;
1800}
1801#endif
1802
Felipe Balbiefa25fd2012-03-14 11:18:28 +02001803static int __devinit omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001804{
1805 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1806 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001807 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001808 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001809 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301810 const struct of_device_id *match;
1811
1812 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1813 if (match) {
1814 pdata = of_get_hsmmc_pdata(&pdev->dev);
1815 if (match->data) {
1816 u16 *offsetp = match->data;
1817 pdata->reg_offset = *offsetp;
1818 }
1819 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001820
1821 if (pdata == NULL) {
1822 dev_err(&pdev->dev, "Platform Data is missing\n");
1823 return -ENXIO;
1824 }
1825
1826 if (pdata->nr_slots == 0) {
1827 dev_err(&pdev->dev, "No Slots\n");
1828 return -ENXIO;
1829 }
1830
1831 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1832 irq = platform_get_irq(pdev, 0);
1833 if (res == NULL || irq < 0)
1834 return -ENXIO;
1835
Chris Ball984b2032011-03-22 16:34:42 -07001836 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001837 if (res == NULL)
1838 return -EBUSY;
1839
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001840 ret = omap_hsmmc_gpio_init(pdata);
1841 if (ret)
1842 goto err;
1843
Denis Karpov70a33412009-09-22 16:44:59 -07001844 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001845 if (!mmc) {
1846 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001847 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001848 }
1849
1850 host = mmc_priv(mmc);
1851 host->mmc = mmc;
1852 host->pdata = pdata;
1853 host->dev = &pdev->dev;
1854 host->use_dma = 1;
1855 host->dev->dma_mask = &pdata->dma_mask;
1856 host->dma_ch = -1;
1857 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001858 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05301859 host->mapbase = res->start + pdata->reg_offset;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001860 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001861 host->power_mode = MMC_POWER_OFF;
Balaji T Kdba3c292012-04-09 12:08:32 +05301862 host->flags = AUTO_CMD12;
Per Forlin9782aff2011-07-01 18:55:23 +02001863 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001864
1865 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001866
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301867 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001868
Adrian Huntere0eb2422010-02-15 10:03:34 -08001869 /*
1870 * If regulator_disable can only put vcc_aux to sleep then there is
1871 * no off state.
1872 */
1873 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1874 mmc_slot(host).no_off = 1;
1875
Daniel Mackd418ed82012-02-19 13:20:33 +01001876 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1877
1878 if (pdata->max_freq > 0)
1879 mmc->f_max = pdata->max_freq;
1880 else
1881 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001882
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001883 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001884
Russell King6f7607c2009-01-28 10:22:50 +00001885 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001886 if (IS_ERR(host->fclk)) {
1887 ret = PTR_ERR(host->fclk);
1888 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001889 goto err1;
1890 }
1891
Paul Walmsley9b682562011-10-06 14:50:35 -06001892 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1893 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1894 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1895 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001896
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301897 pm_runtime_enable(host->dev);
1898 pm_runtime_get_sync(host->dev);
1899 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1900 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001901
Balaji T K92a3aeb2012-02-24 21:14:34 +05301902 omap_hsmmc_context_save(host);
1903
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301904 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1905 /*
1906 * MMC can still work without debounce clock.
1907 */
1908 if (IS_ERR(host->dbclk)) {
1909 dev_warn(mmc_dev(host->mmc), "Failed to get debounce clk\n");
1910 host->dbclk = NULL;
1911 } else if (clk_enable(host->dbclk) != 0) {
1912 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
1913 clk_put(host->dbclk);
1914 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07001915 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001916
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001917 /* Since we do only SG emulation, we can have as many segs
1918 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001919 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001920
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001921 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1922 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1923 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1924 mmc->max_seg_size = mmc->max_req_size;
1925
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001926 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001927 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001928
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001929 mmc->caps |= mmc_slot(host).caps;
1930 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001931 mmc->caps |= MMC_CAP_4_BIT_DATA;
1932
Denis Karpov191d1f12009-09-22 16:44:55 -07001933 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001934 mmc->caps |= MMC_CAP_NONREMOVABLE;
1935
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001936 mmc->pm_caps = mmc_slot(host).pm_caps;
1937
Denis Karpov70a33412009-09-22 16:44:59 -07001938 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001939
Balaji T Kb7bf7732012-03-07 09:55:30 -05001940 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1941 if (!res) {
1942 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001943 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001944 }
Balaji T Kb7bf7732012-03-07 09:55:30 -05001945 host->dma_line_tx = res->start;
1946
1947 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1948 if (!res) {
1949 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
1950 goto err_irq;
1951 }
1952 host->dma_line_rx = res->start;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001953
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001954 /* Request IRQ for MMC operations */
Yong Zhangd9618e92011-09-22 16:59:04 +08001955 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001956 mmc_hostname(mmc), host);
1957 if (ret) {
1958 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1959 goto err_irq;
1960 }
1961
1962 if (pdata->init != NULL) {
1963 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07001964 dev_dbg(mmc_dev(host->mmc),
1965 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001966 goto err_irq_cd_init;
1967 }
1968 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001969
Adrian Hunterb702b102010-02-15 10:03:35 -08001970 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001971 ret = omap_hsmmc_reg_get(host);
1972 if (ret)
1973 goto err_reg;
1974 host->use_reg = 1;
1975 }
1976
David Brownellb583f262009-05-28 14:04:03 -07001977 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001978
1979 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001980 if ((mmc_slot(host).card_detect_irq)) {
NeilBrown7efab4f2011-12-30 12:35:13 +11001981 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1982 NULL,
1983 omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08001984 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11001985 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001986 if (ret) {
1987 dev_dbg(mmc_dev(host->mmc),
1988 "Unable to grab MMC CD IRQ\n");
1989 goto err_irq_cd;
1990 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00001991 pdata->suspend = omap_hsmmc_suspend_cdirq;
1992 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001993 }
1994
Adrian Hunterb4175772010-05-26 14:42:06 -07001995 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001996
Adrian Hunterb62f6222009-09-22 16:45:01 -07001997 omap_hsmmc_protect_card(host);
1998
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001999 mmc_add_host(mmc);
2000
Denis Karpov191d1f12009-09-22 16:44:55 -07002001 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002002 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2003 if (ret < 0)
2004 goto err_slot_name;
2005 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002006 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002007 ret = device_create_file(&mmc->class_dev,
2008 &dev_attr_cover_switch);
2009 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002010 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002011 }
2012
Denis Karpov70a33412009-09-22 16:44:59 -07002013 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302014 pm_runtime_mark_last_busy(host->dev);
2015 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002016
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002017 return 0;
2018
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002019err_slot_name:
2020 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002021 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002022err_irq_cd:
2023 if (host->use_reg)
2024 omap_hsmmc_reg_put(host);
2025err_reg:
2026 if (host->pdata->cleanup)
2027 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002028err_irq_cd_init:
2029 free_irq(host->irq, host);
2030err_irq:
Balaji T Kd59d77e2012-02-24 21:14:33 +05302031 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002032 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002033 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302034 if (host->dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002035 clk_disable(host->dbclk);
2036 clk_put(host->dbclk);
2037 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002038err1:
2039 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002040 platform_set_drvdata(pdev, NULL);
2041 mmc_free_host(mmc);
2042err_alloc:
2043 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002044err:
Russell King48b332f2012-04-18 11:11:57 +01002045 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2046 if (res)
2047 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002048 return ret;
2049}
2050
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002051static int __devexit omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002052{
Denis Karpov70a33412009-09-22 16:44:59 -07002053 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002054 struct resource *res;
2055
Felipe Balbi927ce942012-03-14 11:18:27 +02002056 pm_runtime_get_sync(host->dev);
2057 mmc_remove_host(host->mmc);
2058 if (host->use_reg)
2059 omap_hsmmc_reg_put(host);
2060 if (host->pdata->cleanup)
2061 host->pdata->cleanup(&pdev->dev);
2062 free_irq(host->irq, host);
2063 if (mmc_slot(host).card_detect_irq)
2064 free_irq(mmc_slot(host).card_detect_irq, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002065
Felipe Balbi927ce942012-03-14 11:18:27 +02002066 pm_runtime_put_sync(host->dev);
2067 pm_runtime_disable(host->dev);
2068 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302069 if (host->dbclk) {
Felipe Balbi927ce942012-03-14 11:18:27 +02002070 clk_disable(host->dbclk);
2071 clk_put(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002072 }
2073
Felipe Balbi927ce942012-03-14 11:18:27 +02002074 mmc_free_host(host->mmc);
2075 iounmap(host->base);
2076 omap_hsmmc_gpio_free(pdev->dev.platform_data);
2077
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002078 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2079 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002080 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002081 platform_set_drvdata(pdev, NULL);
2082
2083 return 0;
2084}
2085
2086#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002087static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002088{
2089 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002090 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2091
2092 if (!host)
2093 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002094
2095 if (host && host->suspended)
2096 return 0;
2097
Felipe Balbi927ce942012-03-14 11:18:27 +02002098 pm_runtime_get_sync(host->dev);
2099 host->suspended = 1;
2100 if (host->pdata->suspend) {
2101 ret = host->pdata->suspend(dev, host->slot_id);
Eliad Peller31f9d462011-11-22 16:02:17 +02002102 if (ret) {
Felipe Balbi927ce942012-03-14 11:18:27 +02002103 dev_dbg(dev, "Unable to handle MMC board"
2104 " level suspend\n");
Adrian Huntera6b22402009-09-22 16:44:45 -07002105 host->suspended = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002106 return ret;
Adrian Huntera6b22402009-09-22 16:44:45 -07002107 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002108 }
Felipe Balbi927ce942012-03-14 11:18:27 +02002109 ret = mmc_suspend_host(host->mmc);
2110
2111 if (ret) {
2112 host->suspended = 0;
2113 if (host->pdata->resume) {
2114 ret = host->pdata->resume(dev, host->slot_id);
2115 if (ret)
2116 dev_dbg(dev, "Unmask interrupt failed\n");
2117 }
2118 goto err;
2119 }
2120
2121 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2122 omap_hsmmc_disable_irq(host);
2123 OMAP_HSMMC_WRITE(host->base, HCTL,
2124 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2125 }
2126
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302127 if (host->dbclk)
Felipe Balbi927ce942012-03-14 11:18:27 +02002128 clk_disable(host->dbclk);
Eliad Peller31f9d462011-11-22 16:02:17 +02002129err:
2130 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002131 return ret;
2132}
2133
2134/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002135static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002136{
2137 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002138 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2139
2140 if (!host)
2141 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002142
2143 if (host && !host->suspended)
2144 return 0;
2145
Felipe Balbi927ce942012-03-14 11:18:27 +02002146 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002147
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302148 if (host->dbclk)
Felipe Balbi927ce942012-03-14 11:18:27 +02002149 clk_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002150
Felipe Balbi927ce942012-03-14 11:18:27 +02002151 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2152 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002153
Felipe Balbi927ce942012-03-14 11:18:27 +02002154 if (host->pdata->resume) {
2155 ret = host->pdata->resume(dev, host->slot_id);
2156 if (ret)
2157 dev_dbg(dev, "Unmask interrupt failed\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002158 }
2159
Felipe Balbi927ce942012-03-14 11:18:27 +02002160 omap_hsmmc_protect_card(host);
2161
2162 /* Notify the core to resume the host */
2163 ret = mmc_resume_host(host->mmc);
2164 if (ret == 0)
2165 host->suspended = 0;
2166
2167 pm_runtime_mark_last_busy(host->dev);
2168 pm_runtime_put_autosuspend(host->dev);
2169
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002170 return ret;
2171
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002172}
2173
2174#else
Denis Karpov70a33412009-09-22 16:44:59 -07002175#define omap_hsmmc_suspend NULL
2176#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002177#endif
2178
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302179static int omap_hsmmc_runtime_suspend(struct device *dev)
2180{
2181 struct omap_hsmmc_host *host;
2182
2183 host = platform_get_drvdata(to_platform_device(dev));
2184 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002185 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302186
2187 return 0;
2188}
2189
2190static int omap_hsmmc_runtime_resume(struct device *dev)
2191{
2192 struct omap_hsmmc_host *host;
2193
2194 host = platform_get_drvdata(to_platform_device(dev));
2195 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002196 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302197
2198 return 0;
2199}
2200
Kevin Hilmana791daa2010-05-26 14:42:07 -07002201static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002202 .suspend = omap_hsmmc_suspend,
2203 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302204 .runtime_suspend = omap_hsmmc_runtime_suspend,
2205 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002206};
2207
2208static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002209 .probe = omap_hsmmc_probe,
2210 .remove = __devexit_p(omap_hsmmc_remove),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002211 .driver = {
2212 .name = DRIVER_NAME,
2213 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002214 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302215 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002216 },
2217};
2218
Felipe Balbib7964502012-03-14 11:18:32 +02002219module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002220MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2221MODULE_LICENSE("GPL");
2222MODULE_ALIAS("platform:" DRIVER_NAME);
2223MODULE_AUTHOR("Texas Instruments Inc");