blob: a5bca725e414c79983c5517ec6bd8a3951d05545 [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Andy Shevchenkoac330f42011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
22#include <linux/seq_file.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010023#include <linux/interrupt.h>
24#include <linux/delay.h>
25#include <linux/dma-mapping.h>
26#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010027#include <linux/timer.h>
28#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053029#include <linux/of.h>
30#include <linux/of_gpio.h>
31#include <linux/of_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010032#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070033#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070034#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010035#include <linux/io.h>
36#include <linux/semaphore.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080037#include <linux/gpio.h>
38#include <linux/regulator/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053039#include <linux/pm_runtime.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070040#include <plat/dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010041#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070042#include <plat/board.h>
43#include <plat/mmc.h>
44#include <plat/cpu.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010045
46/* OMAP HSMMC Host Controller Registers */
47#define OMAP_HSMMC_SYSCONFIG 0x0010
Denis Karpov11dd62a2009-09-22 16:44:43 -070048#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010049#define OMAP_HSMMC_CON 0x002C
50#define OMAP_HSMMC_BLK 0x0104
51#define OMAP_HSMMC_ARG 0x0108
52#define OMAP_HSMMC_CMD 0x010C
53#define OMAP_HSMMC_RSP10 0x0110
54#define OMAP_HSMMC_RSP32 0x0114
55#define OMAP_HSMMC_RSP54 0x0118
56#define OMAP_HSMMC_RSP76 0x011C
57#define OMAP_HSMMC_DATA 0x0120
58#define OMAP_HSMMC_HCTL 0x0128
59#define OMAP_HSMMC_SYSCTL 0x012C
60#define OMAP_HSMMC_STAT 0x0130
61#define OMAP_HSMMC_IE 0x0134
62#define OMAP_HSMMC_ISE 0x0138
63#define OMAP_HSMMC_CAPA 0x0140
64
65#define VS18 (1 << 26)
66#define VS30 (1 << 25)
67#define SDVS18 (0x5 << 9)
68#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080069#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010070#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010071#define SDVSCLR 0xFFFFF1FF
72#define SDVSDET 0x00000400
73#define AUTOIDLE 0x1
74#define SDBP (1 << 8)
75#define DTO 0xe
76#define ICE 0x1
77#define ICS 0x2
78#define CEN (1 << 2)
79#define CLKD_MASK 0x0000FFC0
80#define CLKD_SHIFT 6
81#define DTO_MASK 0x000F0000
82#define DTO_SHIFT 16
83#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070084#define BWR_ENABLE (1 << 4)
85#define BRR_ENABLE (1 << 5)
Adrian Hunter93caf8e692010-08-11 14:17:48 -070086#define DTO_ENABLE (1 << 20)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010087#define INIT_STREAM (1 << 1)
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 K03b5d922012-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 Hunter2bec0892009-09-22 16:45:02 -0700173 int got_dbclk;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200174 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700175 int context_loss;
Adrian Hunter623821f2009-09-22 16:44:51 -0700176 int vdd;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700177 int protect_card;
178 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800179 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700180 int req_in_progress;
Balaji T Kdba3c292012-04-09 12:08:32 +0530181 unsigned int flags;
Per Forlin9782aff2011-07-01 18:55:23 +0200182 struct omap_hsmmc_next next_data;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700183
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100184 struct omap_mmc_platform_data *pdata;
185};
186
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800187static int omap_hsmmc_card_detect(struct device *dev, int slot)
188{
189 struct omap_mmc_platform_data *mmc = dev->platform_data;
190
191 /* NOTE: assumes card detect signal is active-low */
192 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
193}
194
195static int omap_hsmmc_get_wp(struct device *dev, int slot)
196{
197 struct omap_mmc_platform_data *mmc = dev->platform_data;
198
199 /* NOTE: assumes write protect signal is active-high */
200 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
201}
202
203static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
204{
205 struct omap_mmc_platform_data *mmc = dev->platform_data;
206
207 /* NOTE: assumes card detect signal is active-low */
208 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
209}
210
211#ifdef CONFIG_PM
212
213static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
214{
215 struct omap_mmc_platform_data *mmc = dev->platform_data;
216
217 disable_irq(mmc->slots[0].card_detect_irq);
218 return 0;
219}
220
221static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
222{
223 struct omap_mmc_platform_data *mmc = dev->platform_data;
224
225 enable_irq(mmc->slots[0].card_detect_irq);
226 return 0;
227}
228
229#else
230
231#define omap_hsmmc_suspend_cdirq NULL
232#define omap_hsmmc_resume_cdirq NULL
233
234#endif
235
Adrian Hunterb702b102010-02-15 10:03:35 -0800236#ifdef CONFIG_REGULATOR
237
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500238static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800239 int vdd)
240{
241 struct omap_hsmmc_host *host =
242 platform_get_drvdata(to_platform_device(dev));
243 int ret = 0;
244
245 /*
246 * If we don't see a Vcc regulator, assume it's a fixed
247 * voltage always-on regulator.
248 */
249 if (!host->vcc)
250 return 0;
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530251 /*
252 * With DT, never turn OFF the regulator. This is because
253 * the pbias cell programming support is still missing when
254 * booting with Device tree
255 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +0530256 if (dev->of_node && !vdd)
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530257 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800258
259 if (mmc_slot(host).before_set_reg)
260 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
261
262 /*
263 * Assume Vcc regulator is used only to power the card ... OMAP
264 * VDDS is used to power the pins, optionally with a transceiver to
265 * support cards using voltages other than VDDS (1.8V nominal). When a
266 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
267 *
268 * In some cases this regulator won't support enable/disable;
269 * e.g. it's a fixed rail for a WLAN chip.
270 *
271 * In other cases vcc_aux switches interface power. Example, for
272 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
273 * chips/cards need an interface voltage rail too.
274 */
275 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400276 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800277 /* Enable interface voltage rail, if needed */
278 if (ret == 0 && host->vcc_aux) {
279 ret = regulator_enable(host->vcc_aux);
280 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400281 ret = mmc_regulator_set_ocr(host->mmc,
282 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800283 }
284 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400285 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800286 if (host->vcc_aux)
287 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400288 if (!ret) {
289 /* Then proceed to shut down the local regulator */
290 ret = mmc_regulator_set_ocr(host->mmc,
291 host->vcc, 0);
292 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800293 }
294
295 if (mmc_slot(host).after_set_reg)
296 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
297
298 return ret;
299}
300
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800301static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
302{
303 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700304 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800305
Rajendra Nayak1cb9af42012-03-07 09:55:31 -0500306 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800307
308 reg = regulator_get(host->dev, "vmmc");
309 if (IS_ERR(reg)) {
310 dev_dbg(host->dev, "vmmc regulator missing\n");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800311 } else {
312 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700313 ocr_value = mmc_regulator_get_ocrmask(reg);
314 if (!mmc_slot(host).ocr_mask) {
315 mmc_slot(host).ocr_mask = ocr_value;
316 } else {
317 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530318 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500319 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700320 mmc_slot(host).ocr_mask = 0;
321 return -EINVAL;
322 }
323 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800324
325 /* Allow an aux regulator */
326 reg = regulator_get(host->dev, "vmmc_aux");
327 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
328
Balaji T Kb1c1df72011-05-30 19:55:34 +0530329 /* For eMMC do not power off when not in sleep state */
330 if (mmc_slot(host).no_regulator_off_init)
331 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800332 /*
333 * UGLY HACK: workaround regulator framework bugs.
334 * When the bootloader leaves a supply active, it's
335 * initialized with zero usecount ... and we can't
336 * disable it without first enabling it. Until the
337 * framework is fixed, we need a workaround like this
338 * (which is safe for MMC, but not in general).
339 */
Adrian Huntere840ce12011-05-06 12:14:10 +0300340 if (regulator_is_enabled(host->vcc) > 0 ||
341 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
342 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
343
344 mmc_slot(host).set_power(host->dev, host->slot_id,
345 1, vdd);
346 mmc_slot(host).set_power(host->dev, host->slot_id,
347 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800348 }
349 }
350
351 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800352}
353
354static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
355{
356 regulator_put(host->vcc);
357 regulator_put(host->vcc_aux);
358 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800359}
360
Adrian Hunterb702b102010-02-15 10:03:35 -0800361static inline int omap_hsmmc_have_reg(void)
362{
363 return 1;
364}
365
366#else
367
368static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
369{
370 return -EINVAL;
371}
372
373static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
374{
375}
376
377static inline int omap_hsmmc_have_reg(void)
378{
379 return 0;
380}
381
382#endif
383
384static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
385{
386 int ret;
387
388 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800389 if (pdata->slots[0].cover)
390 pdata->slots[0].get_cover_state =
391 omap_hsmmc_get_cover_state;
392 else
393 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
394 pdata->slots[0].card_detect_irq =
395 gpio_to_irq(pdata->slots[0].switch_pin);
396 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
397 if (ret)
398 return ret;
399 ret = gpio_direction_input(pdata->slots[0].switch_pin);
400 if (ret)
401 goto err_free_sp;
402 } else
403 pdata->slots[0].switch_pin = -EINVAL;
404
405 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
406 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
407 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
408 if (ret)
409 goto err_free_cd;
410 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
411 if (ret)
412 goto err_free_wp;
413 } else
414 pdata->slots[0].gpio_wp = -EINVAL;
415
416 return 0;
417
418err_free_wp:
419 gpio_free(pdata->slots[0].gpio_wp);
420err_free_cd:
421 if (gpio_is_valid(pdata->slots[0].switch_pin))
422err_free_sp:
423 gpio_free(pdata->slots[0].switch_pin);
424 return ret;
425}
426
427static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
428{
429 if (gpio_is_valid(pdata->slots[0].gpio_wp))
430 gpio_free(pdata->slots[0].gpio_wp);
431 if (gpio_is_valid(pdata->slots[0].switch_pin))
432 gpio_free(pdata->slots[0].switch_pin);
433}
434
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100435/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300436 * Start clock to the card
437 */
438static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
439{
440 OMAP_HSMMC_WRITE(host->base, SYSCTL,
441 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
442}
443
444/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100445 * Stop clock to the card
446 */
Denis Karpov70a33412009-09-22 16:44:59 -0700447static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100448{
449 OMAP_HSMMC_WRITE(host->base, SYSCTL,
450 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
451 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
452 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
453}
454
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700455static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
456 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700457{
458 unsigned int irq_mask;
459
460 if (host->use_dma)
461 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
462 else
463 irq_mask = INT_EN_MASK;
464
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700465 /* Disable timeout for erases */
466 if (cmd->opcode == MMC_ERASE)
467 irq_mask &= ~DTO_ENABLE;
468
Adrian Hunterb4175772010-05-26 14:42:06 -0700469 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
470 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
471 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
472}
473
474static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
475{
476 OMAP_HSMMC_WRITE(host->base, ISE, 0);
477 OMAP_HSMMC_WRITE(host->base, IE, 0);
478 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
479}
480
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300481/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530482static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300483{
484 u16 dsor = 0;
485
486 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530487 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300488 if (dsor > 250)
489 dsor = 250;
490 }
491
492 return dsor;
493}
494
Andy Shevchenko5934df22011-05-06 12:14:06 +0300495static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
496{
497 struct mmc_ios *ios = &host->mmc->ios;
498 unsigned long regval;
499 unsigned long timeout;
500
501 dev_dbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
502
503 omap_hsmmc_stop_clock(host);
504
505 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
506 regval = regval & ~(CLKD_MASK | DTO_MASK);
Balaji TKd83b6e02011-12-20 15:12:00 +0530507 regval = regval | (calc_divisor(host, ios) << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300508 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
509 OMAP_HSMMC_WRITE(host->base, SYSCTL,
510 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
511
512 /* Wait till the ICS bit is set */
513 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
514 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
515 && time_before(jiffies, timeout))
516 cpu_relax();
517
518 omap_hsmmc_start_clock(host);
519}
520
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400521static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
522{
523 struct mmc_ios *ios = &host->mmc->ios;
524 u32 con;
525
526 con = OMAP_HSMMC_READ(host->base, CON);
Balaji T K03b5d922012-04-09 12:08:33 +0530527 if (ios->timing == MMC_TIMING_UHS_DDR50)
528 con |= DDR; /* configure in DDR mode */
529 else
530 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400531 switch (ios->bus_width) {
532 case MMC_BUS_WIDTH_8:
533 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
534 break;
535 case MMC_BUS_WIDTH_4:
536 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
537 OMAP_HSMMC_WRITE(host->base, HCTL,
538 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
539 break;
540 case MMC_BUS_WIDTH_1:
541 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
542 OMAP_HSMMC_WRITE(host->base, HCTL,
543 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
544 break;
545 }
546}
547
548static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
549{
550 struct mmc_ios *ios = &host->mmc->ios;
551 u32 con;
552
553 con = OMAP_HSMMC_READ(host->base, CON);
554 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
555 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
556 else
557 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
558}
559
Denis Karpov11dd62a2009-09-22 16:44:43 -0700560#ifdef CONFIG_PM
561
562/*
563 * Restore the MMC host context, if it was lost as result of a
564 * power state change.
565 */
Denis Karpov70a33412009-09-22 16:44:59 -0700566static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700567{
568 struct mmc_ios *ios = &host->mmc->ios;
569 struct omap_mmc_platform_data *pdata = host->pdata;
570 int context_loss = 0;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400571 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700572 unsigned long timeout;
573
574 if (pdata->get_context_loss_count) {
575 context_loss = pdata->get_context_loss_count(host->dev);
576 if (context_loss < 0)
577 return 1;
578 }
579
580 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
581 context_loss == host->context_loss ? "not " : "");
582 if (host->context_loss == context_loss)
583 return 1;
584
585 /* Wait for hardware reset */
586 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
587 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
588 && time_before(jiffies, timeout))
589 ;
590
591 /* Do software reset */
592 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
593 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
594 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
595 && time_before(jiffies, timeout))
596 ;
597
598 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
599 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
600
Balaji T Kc2200ef2012-03-07 09:55:30 -0500601 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700602 if (host->power_mode != MMC_POWER_OFF &&
603 (1 << ios->vdd) <= MMC_VDD_23_24)
604 hctl = SDVS18;
605 else
606 hctl = SDVS30;
607 capa = VS30 | VS18;
608 } else {
609 hctl = SDVS18;
610 capa = VS18;
611 }
612
613 OMAP_HSMMC_WRITE(host->base, HCTL,
614 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
615
616 OMAP_HSMMC_WRITE(host->base, CAPA,
617 OMAP_HSMMC_READ(host->base, CAPA) | capa);
618
619 OMAP_HSMMC_WRITE(host->base, HCTL,
620 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
621
622 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
623 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
624 && time_before(jiffies, timeout))
625 ;
626
Adrian Hunterb4175772010-05-26 14:42:06 -0700627 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700628
629 /* Do not initialize card-specific things if the power is off */
630 if (host->power_mode == MMC_POWER_OFF)
631 goto out;
632
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400633 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700634
Andy Shevchenko5934df22011-05-06 12:14:06 +0300635 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700636
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400637 omap_hsmmc_set_bus_mode(host);
638
Denis Karpov11dd62a2009-09-22 16:44:43 -0700639out:
640 host->context_loss = context_loss;
641
642 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
643 return 0;
644}
645
646/*
647 * Save the MMC host context (store the number of power state changes so far).
648 */
Denis Karpov70a33412009-09-22 16:44:59 -0700649static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700650{
651 struct omap_mmc_platform_data *pdata = host->pdata;
652 int context_loss;
653
654 if (pdata->get_context_loss_count) {
655 context_loss = pdata->get_context_loss_count(host->dev);
656 if (context_loss < 0)
657 return;
658 host->context_loss = context_loss;
659 }
660}
661
662#else
663
Denis Karpov70a33412009-09-22 16:44:59 -0700664static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700665{
666 return 0;
667}
668
Denis Karpov70a33412009-09-22 16:44:59 -0700669static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700670{
671}
672
673#endif
674
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100675/*
676 * Send init stream sequence to card
677 * before sending IDLE command
678 */
Denis Karpov70a33412009-09-22 16:44:59 -0700679static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100680{
681 int reg = 0;
682 unsigned long timeout;
683
Adrian Hunterb62f6222009-09-22 16:45:01 -0700684 if (host->protect_card)
685 return;
686
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100687 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700688
689 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100690 OMAP_HSMMC_WRITE(host->base, CON,
691 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
692 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
693
694 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
695 while ((reg != CC) && time_before(jiffies, timeout))
696 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
697
698 OMAP_HSMMC_WRITE(host->base, CON,
699 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700700
701 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
702 OMAP_HSMMC_READ(host->base, STAT);
703
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100704 enable_irq(host->irq);
705}
706
707static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700708int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100709{
710 int r = 1;
711
Denis Karpov191d1f12009-09-22 16:44:55 -0700712 if (mmc_slot(host).get_cover_state)
713 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100714 return r;
715}
716
717static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700718omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100719 char *buf)
720{
721 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700722 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100723
Denis Karpov70a33412009-09-22 16:44:59 -0700724 return sprintf(buf, "%s\n",
725 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100726}
727
Denis Karpov70a33412009-09-22 16:44:59 -0700728static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100729
730static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700731omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100732 char *buf)
733{
734 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700735 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100736
Denis Karpov191d1f12009-09-22 16:44:55 -0700737 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100738}
739
Denis Karpov70a33412009-09-22 16:44:59 -0700740static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100741
742/*
743 * Configure the response type and send the cmd.
744 */
745static void
Denis Karpov70a33412009-09-22 16:44:59 -0700746omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100747 struct mmc_data *data)
748{
749 int cmdreg = 0, resptype = 0, cmdtype = 0;
750
751 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
752 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
753 host->cmd = cmd;
754
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700755 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100756
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200757 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100758 if (cmd->flags & MMC_RSP_PRESENT) {
759 if (cmd->flags & MMC_RSP_136)
760 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200761 else if (cmd->flags & MMC_RSP_BUSY) {
762 resptype = 3;
763 host->response_busy = 1;
764 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100765 resptype = 2;
766 }
767
768 /*
769 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
770 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
771 * a val of 0x3, rest 0x0.
772 */
773 if (cmd == host->mrq->stop)
774 cmdtype = 0x3;
775
776 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
Balaji T Kdba3c292012-04-09 12:08:32 +0530777 if ((host->flags & AUTO_CMD12) && mmc_op_multi(cmd->opcode))
778 cmdreg |= ACEN_ACMD12;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100779
780 if (data) {
781 cmdreg |= DP_SELECT | MSBS | BCE;
782 if (data->flags & MMC_DATA_READ)
783 cmdreg |= DDIR;
784 else
785 cmdreg &= ~(DDIR);
786 }
787
788 if (host->use_dma)
789 cmdreg |= DMA_EN;
790
Adrian Hunterb4175772010-05-26 14:42:06 -0700791 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700792
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100793 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
794 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
795}
796
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200797static int
Denis Karpov70a33412009-09-22 16:44:59 -0700798omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200799{
800 if (data->flags & MMC_DATA_WRITE)
801 return DMA_TO_DEVICE;
802 else
803 return DMA_FROM_DEVICE;
804}
805
Adrian Hunterb4175772010-05-26 14:42:06 -0700806static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
807{
808 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530809 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700810
Venkatraman S31463b12012-04-09 12:08:34 +0530811 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700812 host->req_in_progress = 0;
813 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530814 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700815
816 omap_hsmmc_disable_irq(host);
817 /* Do not complete the request if DMA is still in progress */
818 if (mrq->data && host->use_dma && dma_ch != -1)
819 return;
820 host->mrq = NULL;
821 mmc_request_done(host->mmc, mrq);
822}
823
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100824/*
825 * Notify the transfer complete to MMC core
826 */
827static void
Denis Karpov70a33412009-09-22 16:44:59 -0700828omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100829{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200830 if (!data) {
831 struct mmc_request *mrq = host->mrq;
832
Adrian Hunter23050102009-09-22 16:44:57 -0700833 /* TC before CC from CMD6 - don't know why, but it happens */
834 if (host->cmd && host->cmd->opcode == 6 &&
835 host->response_busy) {
836 host->response_busy = 0;
837 return;
838 }
839
Adrian Hunterb4175772010-05-26 14:42:06 -0700840 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200841 return;
842 }
843
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100844 host->data = NULL;
845
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100846 if (!data->error)
847 data->bytes_xfered += data->blocks * (data->blksz);
848 else
849 data->bytes_xfered = 0;
850
Balaji T Kdba3c292012-04-09 12:08:32 +0530851 if (data->stop && ((!(host->flags & AUTO_CMD12)) || data->error)) {
852 omap_hsmmc_start_command(host, data->stop, NULL);
853 } else {
854 if (data->stop)
855 data->stop->resp[0] = OMAP_HSMMC_READ(host->base,
856 RSP76);
Adrian Hunterb4175772010-05-26 14:42:06 -0700857 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100858 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100859}
860
861/*
862 * Notify the core about command completion
863 */
864static void
Denis Karpov70a33412009-09-22 16:44:59 -0700865omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100866{
867 host->cmd = NULL;
868
869 if (cmd->flags & MMC_RSP_PRESENT) {
870 if (cmd->flags & MMC_RSP_136) {
871 /* response type 2 */
872 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
873 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
874 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
875 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
876 } else {
877 /* response types 1, 1b, 3, 4, 5, 6 */
878 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
879 }
880 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700881 if ((host->data == NULL && !host->response_busy) || cmd->error)
882 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100883}
884
885/*
886 * DMA clean up for command errors
887 */
Denis Karpov70a33412009-09-22 16:44:59 -0700888static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100889{
Adrian Hunterb4175772010-05-26 14:42:06 -0700890 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530891 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700892
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200893 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100894
Venkatraman S31463b12012-04-09 12:08:34 +0530895 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700896 dma_ch = host->dma_ch;
897 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530898 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700899
900 if (host->use_dma && dma_ch != -1) {
Per Forlina9120c32011-06-17 20:14:21 +0200901 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg,
902 host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700903 omap_hsmmc_get_dma_dir(host, host->data));
Adrian Hunterb4175772010-05-26 14:42:06 -0700904 omap_free_dma(dma_ch);
Per Forlin053bf342011-11-07 21:55:11 +0530905 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100906 }
907 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100908}
909
910/*
911 * Readable error output
912 */
913#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300914static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100915{
916 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700917 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300918 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
919 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
920 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
921 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100922 };
923 char res[256];
924 char *buf = res;
925 int len, i;
926
927 len = sprintf(buf, "MMC IRQ 0x%x :", status);
928 buf += len;
929
Denis Karpov70a33412009-09-22 16:44:59 -0700930 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100931 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700932 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100933 buf += len;
934 }
935
936 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
937}
Adrian Hunter699b9582011-05-06 12:14:01 +0300938#else
939static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
940 u32 status)
941{
942}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100943#endif /* CONFIG_MMC_DEBUG */
944
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100945/*
946 * MMC controller internal state machines reset
947 *
948 * Used to reset command or data internal state machines, using respectively
949 * SRC or SRD bit of SYSCTL register
950 * Can be called from interrupt context
951 */
Denis Karpov70a33412009-09-22 16:44:59 -0700952static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
953 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100954{
955 unsigned long i = 0;
956 unsigned long limit = (loops_per_jiffy *
957 msecs_to_jiffies(MMC_TIMEOUT_MS));
958
959 OMAP_HSMMC_WRITE(host->base, SYSCTL,
960 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
961
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700962 /*
963 * OMAP4 ES2 and greater has an updated reset logic.
964 * Monitor a 0->1 transition first
965 */
966 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -0500967 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700968 && (i++ < limit))
969 cpu_relax();
970 }
971 i = 0;
972
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100973 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
974 (i++ < limit))
975 cpu_relax();
976
977 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
978 dev_err(mmc_dev(host->mmc),
979 "Timeout waiting on controller reset in %s\n",
980 __func__);
981}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100982
Adrian Hunterb4175772010-05-26 14:42:06 -0700983static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100984{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100985 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -0700986 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100987
Adrian Hunterb4175772010-05-26 14:42:06 -0700988 if (!host->req_in_progress) {
989 do {
990 OMAP_HSMMC_WRITE(host->base, STAT, status);
991 /* Flush posted write */
992 status = OMAP_HSMMC_READ(host->base, STAT);
993 } while (status & INT_EN_MASK);
994 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100995 }
996
997 data = host->data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100998 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
999
1000 if (status & ERR) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001001 omap_hsmmc_dbg_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001002 if ((status & CMD_TIMEOUT) ||
1003 (status & CMD_CRC)) {
1004 if (host->cmd) {
1005 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -07001006 omap_hsmmc_reset_controller_fsm(host,
1007 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001008 host->cmd->error = -ETIMEDOUT;
1009 } else {
1010 host->cmd->error = -EILSEQ;
1011 }
1012 end_cmd = 1;
1013 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001014 if (host->data || host->response_busy) {
1015 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001016 omap_hsmmc_dma_cleanup(host,
1017 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001018 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001019 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -08001020 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001021 }
1022 if ((status & DATA_TIMEOUT) ||
1023 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001024 if (host->data || host->response_busy) {
1025 int err = (status & DATA_TIMEOUT) ?
1026 -ETIMEDOUT : -EILSEQ;
1027
1028 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001029 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001030 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001031 host->mrq->cmd->error = err;
1032 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001033 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001034 end_trans = 1;
1035 }
1036 }
1037 if (status & CARD_ERR) {
1038 dev_dbg(mmc_dev(host->mmc),
1039 "Ignoring card err CMD%d\n", host->cmd->opcode);
1040 if (host->cmd)
1041 end_cmd = 1;
1042 if (host->data)
1043 end_trans = 1;
1044 }
1045 }
1046
1047 OMAP_HSMMC_WRITE(host->base, STAT, status);
1048
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001049 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001050 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001051 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001052 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001053}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001054
Adrian Hunterb4175772010-05-26 14:42:06 -07001055/*
1056 * MMC controller IRQ handler
1057 */
1058static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1059{
1060 struct omap_hsmmc_host *host = dev_id;
1061 int status;
1062
1063 status = OMAP_HSMMC_READ(host->base, STAT);
1064 do {
1065 omap_hsmmc_do_irq(host, status);
1066 /* Flush posted write */
1067 status = OMAP_HSMMC_READ(host->base, STAT);
1068 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001069
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001070 return IRQ_HANDLED;
1071}
1072
Denis Karpov70a33412009-09-22 16:44:59 -07001073static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001074{
1075 unsigned long i;
1076
1077 OMAP_HSMMC_WRITE(host->base, HCTL,
1078 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1079 for (i = 0; i < loops_per_jiffy; i++) {
1080 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1081 break;
1082 cpu_relax();
1083 }
1084}
1085
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001086/*
David Brownelleb250822009-02-17 14:49:01 -08001087 * Switch MMC interface voltage ... only relevant for MMC1.
1088 *
1089 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1090 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1091 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001092 */
Denis Karpov70a33412009-09-22 16:44:59 -07001093static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001094{
1095 u32 reg_val = 0;
1096 int ret;
1097
1098 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301099 pm_runtime_put_sync(host->dev);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001100 if (host->got_dbclk)
1101 clk_disable(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001102
1103 /* Turn the power off */
1104 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001105
1106 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001107 if (!ret)
1108 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1109 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301110 pm_runtime_get_sync(host->dev);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001111 if (host->got_dbclk)
1112 clk_enable(host->dbclk);
1113
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001114 if (ret != 0)
1115 goto err;
1116
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001117 OMAP_HSMMC_WRITE(host->base, HCTL,
1118 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1119 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001120
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001121 /*
1122 * If a MMC dual voltage card is detected, the set_ios fn calls
1123 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001124 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001125 *
David Brownelleb250822009-02-17 14:49:01 -08001126 * Cope with a bit of slop in the range ... per data sheets:
1127 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1128 * but recommended values are 1.71V to 1.89V
1129 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1130 * but recommended values are 2.7V to 3.3V
1131 *
1132 * Board setup code shouldn't permit anything very out-of-range.
1133 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1134 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001135 */
David Brownelleb250822009-02-17 14:49:01 -08001136 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001137 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001138 else
1139 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001140
1141 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001142 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001143
1144 return 0;
1145err:
1146 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1147 return ret;
1148}
1149
Adrian Hunterb62f6222009-09-22 16:45:01 -07001150/* Protect the card while the cover is open */
1151static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1152{
1153 if (!mmc_slot(host).get_cover_state)
1154 return;
1155
1156 host->reqs_blocked = 0;
1157 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1158 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301159 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001160 "card is now accessible\n",
1161 mmc_hostname(host->mmc));
1162 host->protect_card = 0;
1163 }
1164 } else {
1165 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301166 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001167 "card is now inaccessible\n",
1168 mmc_hostname(host->mmc));
1169 host->protect_card = 1;
1170 }
1171 }
1172}
1173
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001174/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001175 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001176 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001177static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001178{
NeilBrown7efab4f2011-12-30 12:35:13 +11001179 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001180 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001181 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001182
Adrian Huntera6b22402009-09-22 16:44:45 -07001183 if (host->suspended)
NeilBrown7efab4f2011-12-30 12:35:13 +11001184 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001185
1186 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001187
Denis Karpov191d1f12009-09-22 16:44:55 -07001188 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001189 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001190 else {
1191 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001192 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001193 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001194
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001195 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001196 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001197 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001198 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001199 return IRQ_HANDLED;
1200}
1201
Denis Karpov70a33412009-09-22 16:44:59 -07001202static int omap_hsmmc_get_dma_sync_dev(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001203 struct mmc_data *data)
1204{
1205 int sync_dev;
1206
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001207 if (data->flags & MMC_DATA_WRITE)
1208 sync_dev = host->dma_line_tx;
1209 else
1210 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001211 return sync_dev;
1212}
1213
Denis Karpov70a33412009-09-22 16:44:59 -07001214static void omap_hsmmc_config_dma_params(struct omap_hsmmc_host *host,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001215 struct mmc_data *data,
1216 struct scatterlist *sgl)
1217{
1218 int blksz, nblk, dma_ch;
1219
1220 dma_ch = host->dma_ch;
1221 if (data->flags & MMC_DATA_WRITE) {
1222 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
1223 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
1224 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1225 sg_dma_address(sgl), 0, 0);
1226 } else {
1227 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -07001228 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001229 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
1230 sg_dma_address(sgl), 0, 0);
1231 }
1232
1233 blksz = host->data->blksz;
1234 nblk = sg_dma_len(sgl) / blksz;
1235
1236 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
1237 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
Denis Karpov70a33412009-09-22 16:44:59 -07001238 omap_hsmmc_get_dma_sync_dev(host, data),
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001239 !(data->flags & MMC_DATA_WRITE));
1240
1241 omap_start_dma(dma_ch);
1242}
1243
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001244/*
1245 * DMA call back function
1246 */
Adrian Hunterb4175772010-05-26 14:42:06 -07001247static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001248{
Adrian Hunterb4175772010-05-26 14:42:06 -07001249 struct omap_hsmmc_host *host = cb_data;
Adrian Hunter770d7432011-05-06 12:14:11 +03001250 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001251 int dma_ch, req_in_progress;
Venkatraman S31463b12012-04-09 12:08:34 +05301252 unsigned long flags;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001253
Venkatraman Sf3584e52010-08-10 18:01:54 -07001254 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1255 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1256 ch_status);
1257 return;
1258 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001259
Venkatraman S31463b12012-04-09 12:08:34 +05301260 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001261 if (host->dma_ch < 0) {
Venkatraman S31463b12012-04-09 12:08:34 +05301262 spin_unlock_irqrestore(&host->irq_lock, flags);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001263 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001264 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001265
Adrian Hunter770d7432011-05-06 12:14:11 +03001266 data = host->mrq->data;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001267 host->dma_sg_idx++;
1268 if (host->dma_sg_idx < host->dma_len) {
1269 /* Fire up the next transfer. */
Adrian Hunterb4175772010-05-26 14:42:06 -07001270 omap_hsmmc_config_dma_params(host, data,
1271 data->sg + host->dma_sg_idx);
Venkatraman S31463b12012-04-09 12:08:34 +05301272 spin_unlock_irqrestore(&host->irq_lock, flags);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001273 return;
1274 }
1275
Per Forlin9782aff2011-07-01 18:55:23 +02001276 if (!data->host_cookie)
1277 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1278 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001279
1280 req_in_progress = host->req_in_progress;
1281 dma_ch = host->dma_ch;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001282 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +05301283 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001284
1285 omap_free_dma(dma_ch);
1286
1287 /* If DMA has finished after TC, complete the request */
1288 if (!req_in_progress) {
1289 struct mmc_request *mrq = host->mrq;
1290
1291 host->mrq = NULL;
1292 mmc_request_done(host->mmc, mrq);
1293 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001294}
1295
Per Forlin9782aff2011-07-01 18:55:23 +02001296static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1297 struct mmc_data *data,
1298 struct omap_hsmmc_next *next)
1299{
1300 int dma_len;
1301
1302 if (!next && data->host_cookie &&
1303 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301304 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001305 " host->next_data.cookie %d\n",
1306 __func__, data->host_cookie, host->next_data.cookie);
1307 data->host_cookie = 0;
1308 }
1309
1310 /* Check if next job is already prepared */
1311 if (next ||
1312 (!next && data->host_cookie != host->next_data.cookie)) {
1313 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
1314 data->sg_len,
1315 omap_hsmmc_get_dma_dir(host, data));
1316
1317 } else {
1318 dma_len = host->next_data.dma_len;
1319 host->next_data.dma_len = 0;
1320 }
1321
1322
1323 if (dma_len == 0)
1324 return -EINVAL;
1325
1326 if (next) {
1327 next->dma_len = dma_len;
1328 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1329 } else
1330 host->dma_len = dma_len;
1331
1332 return 0;
1333}
1334
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001335/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001336 * Routine to configure and start DMA for the MMC card
1337 */
Denis Karpov70a33412009-09-22 16:44:59 -07001338static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1339 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001340{
Adrian Hunterb4175772010-05-26 14:42:06 -07001341 int dma_ch = 0, ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001342 struct mmc_data *data = req->data;
1343
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001344 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001345 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001346 struct scatterlist *sgl;
1347
1348 sgl = data->sg + i;
1349 if (sgl->length % data->blksz)
1350 return -EINVAL;
1351 }
1352 if ((data->blksz % 4) != 0)
1353 /* REVISIT: The MMC buffer increments only when MSB is written.
1354 * Return error for blksz which is non multiple of four.
1355 */
1356 return -EINVAL;
1357
Adrian Hunterb4175772010-05-26 14:42:06 -07001358 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001359
Denis Karpov70a33412009-09-22 16:44:59 -07001360 ret = omap_request_dma(omap_hsmmc_get_dma_sync_dev(host, data),
1361 "MMC/SD", omap_hsmmc_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001362 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001363 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001364 "%s: omap_request_dma() failed with %d\n",
1365 mmc_hostname(host->mmc), ret);
1366 return ret;
1367 }
Per Forlin9782aff2011-07-01 18:55:23 +02001368 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL);
1369 if (ret)
1370 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001371
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001372 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001373 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001374
Denis Karpov70a33412009-09-22 16:44:59 -07001375 omap_hsmmc_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001376
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001377 return 0;
1378}
1379
Denis Karpov70a33412009-09-22 16:44:59 -07001380static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001381 unsigned int timeout_ns,
1382 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001383{
1384 unsigned int timeout, cycle_ns;
1385 uint32_t reg, clkd, dto = 0;
1386
1387 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1388 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1389 if (clkd == 0)
1390 clkd = 1;
1391
1392 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001393 timeout = timeout_ns / cycle_ns;
1394 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001395 if (timeout) {
1396 while ((timeout & 0x80000000) == 0) {
1397 dto += 1;
1398 timeout <<= 1;
1399 }
1400 dto = 31 - dto;
1401 timeout <<= 1;
1402 if (timeout && dto)
1403 dto += 1;
1404 if (dto >= 13)
1405 dto -= 13;
1406 else
1407 dto = 0;
1408 if (dto > 14)
1409 dto = 14;
1410 }
1411
1412 reg &= ~DTO_MASK;
1413 reg |= dto << DTO_SHIFT;
1414 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1415}
1416
1417/*
1418 * Configure block length for MMC/SD cards and initiate the transfer.
1419 */
1420static int
Denis Karpov70a33412009-09-22 16:44:59 -07001421omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001422{
1423 int ret;
1424 host->data = req->data;
1425
1426 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001427 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001428 /*
1429 * Set an arbitrary 100ms data timeout for commands with
1430 * busy signal.
1431 */
1432 if (req->cmd->flags & MMC_RSP_BUSY)
1433 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001434 return 0;
1435 }
1436
1437 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1438 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001439 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001440
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001441 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001442 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001443 if (ret != 0) {
1444 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1445 return ret;
1446 }
1447 }
1448 return 0;
1449}
1450
Per Forlin9782aff2011-07-01 18:55:23 +02001451static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1452 int err)
1453{
1454 struct omap_hsmmc_host *host = mmc_priv(mmc);
1455 struct mmc_data *data = mrq->data;
1456
1457 if (host->use_dma) {
Per Forlin053bf342011-11-07 21:55:11 +05301458 if (data->host_cookie)
1459 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
1460 data->sg_len,
1461 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001462 data->host_cookie = 0;
1463 }
1464}
1465
1466static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1467 bool is_first_req)
1468{
1469 struct omap_hsmmc_host *host = mmc_priv(mmc);
1470
1471 if (mrq->data->host_cookie) {
1472 mrq->data->host_cookie = 0;
1473 return ;
1474 }
1475
1476 if (host->use_dma)
1477 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
1478 &host->next_data))
1479 mrq->data->host_cookie = 0;
1480}
1481
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001482/*
1483 * Request function. for read/write operation
1484 */
Denis Karpov70a33412009-09-22 16:44:59 -07001485static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001486{
Denis Karpov70a33412009-09-22 16:44:59 -07001487 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001488 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001489
Adrian Hunterb4175772010-05-26 14:42:06 -07001490 BUG_ON(host->req_in_progress);
1491 BUG_ON(host->dma_ch != -1);
1492 if (host->protect_card) {
1493 if (host->reqs_blocked < 3) {
1494 /*
1495 * Ensure the controller is left in a consistent
1496 * state by resetting the command and data state
1497 * machines.
1498 */
1499 omap_hsmmc_reset_controller_fsm(host, SRD);
1500 omap_hsmmc_reset_controller_fsm(host, SRC);
1501 host->reqs_blocked += 1;
1502 }
1503 req->cmd->error = -EBADF;
1504 if (req->data)
1505 req->data->error = -EBADF;
1506 req->cmd->retries = 0;
1507 mmc_request_done(mmc, req);
1508 return;
1509 } else if (host->reqs_blocked)
1510 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001511 WARN_ON(host->mrq != NULL);
1512 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001513 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001514 if (err) {
1515 req->cmd->error = err;
1516 if (req->data)
1517 req->data->error = err;
1518 host->mrq = NULL;
1519 mmc_request_done(mmc, req);
1520 return;
1521 }
1522
Denis Karpov70a33412009-09-22 16:44:59 -07001523 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001524}
1525
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001526/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001527static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001528{
Denis Karpov70a33412009-09-22 16:44:59 -07001529 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001530 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001531
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301532 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001533
Adrian Huntera3621462009-09-22 16:44:42 -07001534 if (ios->power_mode != host->power_mode) {
1535 switch (ios->power_mode) {
1536 case MMC_POWER_OFF:
1537 mmc_slot(host).set_power(host->dev, host->slot_id,
1538 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001539 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001540 break;
1541 case MMC_POWER_UP:
1542 mmc_slot(host).set_power(host->dev, host->slot_id,
1543 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001544 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001545 break;
1546 case MMC_POWER_ON:
1547 do_send_init_stream = 1;
1548 break;
1549 }
1550 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001551 }
1552
Denis Karpovdd498ef2009-09-22 16:44:49 -07001553 /* FIXME: set registers based only on changes to ios */
1554
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001555 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001556
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301557 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001558 /* Only MMC1 can interface at 3V without some flavor
1559 * of external transceiver; but they all handle 1.8V.
1560 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001561 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Rajendra Nayak1f84b712012-03-12 20:32:38 +05301562 (ios->vdd == DUAL_VOLT_OCR_BIT) &&
1563 /*
1564 * With pbias cell programming missing, this
1565 * can't be allowed when booting with device
1566 * tree.
1567 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +05301568 !host->dev->of_node) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001569 /*
1570 * The mmc_select_voltage fn of the core does
1571 * not seem to set the power_mode to
1572 * MMC_POWER_UP upon recalculating the voltage.
1573 * vdd 1.8v.
1574 */
Denis Karpov70a33412009-09-22 16:44:59 -07001575 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1576 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001577 "Switch operation failed\n");
1578 }
1579 }
1580
Andy Shevchenko5934df22011-05-06 12:14:06 +03001581 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001582
Adrian Huntera3621462009-09-22 16:44:42 -07001583 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001584 send_init_stream(host);
1585
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001586 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001587
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301588 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001589}
1590
1591static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1592{
Denis Karpov70a33412009-09-22 16:44:59 -07001593 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001594
Denis Karpov191d1f12009-09-22 16:44:55 -07001595 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001596 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001597 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001598}
1599
1600static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1601{
Denis Karpov70a33412009-09-22 16:44:59 -07001602 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001603
Denis Karpov191d1f12009-09-22 16:44:55 -07001604 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001605 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001606 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001607}
1608
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001609static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1610{
1611 struct omap_hsmmc_host *host = mmc_priv(mmc);
1612
1613 if (mmc_slot(host).init_card)
1614 mmc_slot(host).init_card(card);
1615}
1616
Denis Karpov70a33412009-09-22 16:44:59 -07001617static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001618{
1619 u32 hctl, capa, value;
1620
1621 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301622 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001623 hctl = SDVS30;
1624 capa = VS30 | VS18;
1625 } else {
1626 hctl = SDVS18;
1627 capa = VS18;
1628 }
1629
1630 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1631 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1632
1633 value = OMAP_HSMMC_READ(host->base, CAPA);
1634 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1635
1636 /* Set the controller to AUTO IDLE mode */
1637 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1638 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1639
1640 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001641 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001642}
1643
Denis Karpov70a33412009-09-22 16:44:59 -07001644static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001645{
Denis Karpov70a33412009-09-22 16:44:59 -07001646 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001647
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301648 pm_runtime_get_sync(host->dev);
1649
Denis Karpovdd498ef2009-09-22 16:44:49 -07001650 return 0;
1651}
1652
Adrian Hunter907d2e72012-02-29 09:17:21 +02001653static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001654{
Denis Karpov70a33412009-09-22 16:44:59 -07001655 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001656
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301657 pm_runtime_mark_last_busy(host->dev);
1658 pm_runtime_put_autosuspend(host->dev);
1659
Denis Karpovdd498ef2009-09-22 16:44:49 -07001660 return 0;
1661}
1662
Denis Karpov70a33412009-09-22 16:44:59 -07001663static const struct mmc_host_ops omap_hsmmc_ops = {
1664 .enable = omap_hsmmc_enable_fclk,
1665 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001666 .post_req = omap_hsmmc_post_req,
1667 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001668 .request = omap_hsmmc_request,
1669 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001670 .get_cd = omap_hsmmc_get_cd,
1671 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001672 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001673 /* NYET -- enable_sdio_irq */
1674};
1675
Denis Karpovd900f712009-09-22 16:44:38 -07001676#ifdef CONFIG_DEBUG_FS
1677
Denis Karpov70a33412009-09-22 16:44:59 -07001678static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001679{
1680 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001681 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001682 int context_loss = 0;
1683
Denis Karpov70a33412009-09-22 16:44:59 -07001684 if (host->pdata->get_context_loss_count)
1685 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001686
Adrian Hunter907d2e72012-02-29 09:17:21 +02001687 seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1688 mmc->index, host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001689
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301690 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001691 seq_printf(s, "host suspended, can't read registers\n");
1692 return 0;
1693 }
1694
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301695 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001696
1697 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1698 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1699 seq_printf(s, "CON:\t\t0x%08x\n",
1700 OMAP_HSMMC_READ(host->base, CON));
1701 seq_printf(s, "HCTL:\t\t0x%08x\n",
1702 OMAP_HSMMC_READ(host->base, HCTL));
1703 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1704 OMAP_HSMMC_READ(host->base, SYSCTL));
1705 seq_printf(s, "IE:\t\t0x%08x\n",
1706 OMAP_HSMMC_READ(host->base, IE));
1707 seq_printf(s, "ISE:\t\t0x%08x\n",
1708 OMAP_HSMMC_READ(host->base, ISE));
1709 seq_printf(s, "CAPA:\t\t0x%08x\n",
1710 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001711
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301712 pm_runtime_mark_last_busy(host->dev);
1713 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001714
Denis Karpovd900f712009-09-22 16:44:38 -07001715 return 0;
1716}
1717
Denis Karpov70a33412009-09-22 16:44:59 -07001718static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001719{
Denis Karpov70a33412009-09-22 16:44:59 -07001720 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001721}
1722
1723static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001724 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001725 .read = seq_read,
1726 .llseek = seq_lseek,
1727 .release = single_release,
1728};
1729
Denis Karpov70a33412009-09-22 16:44:59 -07001730static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001731{
1732 if (mmc->debugfs_root)
1733 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1734 mmc, &mmc_regs_fops);
1735}
1736
1737#else
1738
Denis Karpov70a33412009-09-22 16:44:59 -07001739static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001740{
1741}
1742
1743#endif
1744
Rajendra Nayak46856a62012-03-12 20:32:37 +05301745#ifdef CONFIG_OF
1746static u16 omap4_reg_offset = 0x100;
1747
1748static const struct of_device_id omap_mmc_of_match[] = {
1749 {
1750 .compatible = "ti,omap2-hsmmc",
1751 },
1752 {
1753 .compatible = "ti,omap3-hsmmc",
1754 },
1755 {
1756 .compatible = "ti,omap4-hsmmc",
1757 .data = &omap4_reg_offset,
1758 },
1759 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001760};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301761MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1762
1763static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1764{
1765 struct omap_mmc_platform_data *pdata;
1766 struct device_node *np = dev->of_node;
1767 u32 bus_width;
1768
1769 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1770 if (!pdata)
1771 return NULL; /* out of memory */
1772
1773 if (of_find_property(np, "ti,dual-volt", NULL))
1774 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1775
1776 /* This driver only supports 1 slot */
1777 pdata->nr_slots = 1;
1778 pdata->slots[0].switch_pin = of_get_named_gpio(np, "cd-gpios", 0);
1779 pdata->slots[0].gpio_wp = of_get_named_gpio(np, "wp-gpios", 0);
1780
1781 if (of_find_property(np, "ti,non-removable", NULL)) {
1782 pdata->slots[0].nonremovable = true;
1783 pdata->slots[0].no_regulator_off_init = true;
1784 }
1785 of_property_read_u32(np, "ti,bus-width", &bus_width);
1786 if (bus_width == 4)
1787 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1788 else if (bus_width == 8)
1789 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1790
1791 if (of_find_property(np, "ti,needs-special-reset", NULL))
1792 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1793
1794 return pdata;
1795}
1796#else
1797static inline struct omap_mmc_platform_data
1798 *of_get_hsmmc_pdata(struct device *dev)
1799{
1800 return NULL;
1801}
1802#endif
1803
Felipe Balbiefa25fd2012-03-14 11:18:28 +02001804static int __devinit omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001805{
1806 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1807 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001808 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001809 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001810 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301811 const struct of_device_id *match;
1812
1813 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1814 if (match) {
1815 pdata = of_get_hsmmc_pdata(&pdev->dev);
1816 if (match->data) {
1817 u16 *offsetp = match->data;
1818 pdata->reg_offset = *offsetp;
1819 }
1820 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001821
1822 if (pdata == NULL) {
1823 dev_err(&pdev->dev, "Platform Data is missing\n");
1824 return -ENXIO;
1825 }
1826
1827 if (pdata->nr_slots == 0) {
1828 dev_err(&pdev->dev, "No Slots\n");
1829 return -ENXIO;
1830 }
1831
1832 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1833 irq = platform_get_irq(pdev, 0);
1834 if (res == NULL || irq < 0)
1835 return -ENXIO;
1836
Chris Ball984b2032011-03-22 16:34:42 -07001837 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001838 if (res == NULL)
1839 return -EBUSY;
1840
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001841 ret = omap_hsmmc_gpio_init(pdata);
1842 if (ret)
1843 goto err;
1844
Denis Karpov70a33412009-09-22 16:44:59 -07001845 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001846 if (!mmc) {
1847 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001848 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001849 }
1850
1851 host = mmc_priv(mmc);
1852 host->mmc = mmc;
1853 host->pdata = pdata;
1854 host->dev = &pdev->dev;
1855 host->use_dma = 1;
1856 host->dev->dma_mask = &pdata->dma_mask;
1857 host->dma_ch = -1;
1858 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001859 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05301860 host->mapbase = res->start + pdata->reg_offset;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001861 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001862 host->power_mode = MMC_POWER_OFF;
Balaji T Kdba3c292012-04-09 12:08:32 +05301863 host->flags = AUTO_CMD12;
Per Forlin9782aff2011-07-01 18:55:23 +02001864 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001865
1866 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001867
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301868 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001869
Adrian Huntere0eb2422010-02-15 10:03:34 -08001870 /*
1871 * If regulator_disable can only put vcc_aux to sleep then there is
1872 * no off state.
1873 */
1874 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1875 mmc_slot(host).no_off = 1;
1876
Daniel Mackd418ed82012-02-19 13:20:33 +01001877 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1878
1879 if (pdata->max_freq > 0)
1880 mmc->f_max = pdata->max_freq;
1881 else
1882 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001883
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001884 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001885
Russell King6f7607c2009-01-28 10:22:50 +00001886 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001887 if (IS_ERR(host->fclk)) {
1888 ret = PTR_ERR(host->fclk);
1889 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001890 goto err1;
1891 }
1892
Paul Walmsley9b682562011-10-06 14:50:35 -06001893 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1894 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1895 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1896 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001897
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301898 pm_runtime_enable(host->dev);
1899 pm_runtime_get_sync(host->dev);
1900 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1901 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001902
Balaji T K92a3aeb2012-02-24 21:14:34 +05301903 omap_hsmmc_context_save(host);
1904
Adrian Hunter2bec0892009-09-22 16:45:02 -07001905 if (cpu_is_omap2430()) {
1906 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1907 /*
1908 * MMC can still work without debounce clock.
1909 */
1910 if (IS_ERR(host->dbclk))
1911 dev_warn(mmc_dev(host->mmc),
1912 "Failed to get debounce clock\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001913 else
Adrian Hunter2bec0892009-09-22 16:45:02 -07001914 host->got_dbclk = 1;
1915
1916 if (host->got_dbclk)
1917 if (clk_enable(host->dbclk) != 0)
1918 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
1919 " clk failed\n");
1920 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001921
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001922 /* Since we do only SG emulation, we can have as many segs
1923 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001924 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001925
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001926 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1927 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1928 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1929 mmc->max_seg_size = mmc->max_req_size;
1930
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001931 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001932 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001933
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001934 mmc->caps |= mmc_slot(host).caps;
1935 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001936 mmc->caps |= MMC_CAP_4_BIT_DATA;
1937
Denis Karpov191d1f12009-09-22 16:44:55 -07001938 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001939 mmc->caps |= MMC_CAP_NONREMOVABLE;
1940
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001941 mmc->pm_caps = mmc_slot(host).pm_caps;
1942
Denis Karpov70a33412009-09-22 16:44:59 -07001943 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001944
Balaji T Kb7bf7732012-03-07 09:55:30 -05001945 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1946 if (!res) {
1947 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001948 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001949 }
Balaji T Kb7bf7732012-03-07 09:55:30 -05001950 host->dma_line_tx = res->start;
1951
1952 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1953 if (!res) {
1954 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
1955 goto err_irq;
1956 }
1957 host->dma_line_rx = res->start;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001958
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001959 /* Request IRQ for MMC operations */
Yong Zhangd9618e92011-09-22 16:59:04 +08001960 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001961 mmc_hostname(mmc), host);
1962 if (ret) {
1963 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1964 goto err_irq;
1965 }
1966
1967 if (pdata->init != NULL) {
1968 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07001969 dev_dbg(mmc_dev(host->mmc),
1970 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001971 goto err_irq_cd_init;
1972 }
1973 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001974
Adrian Hunterb702b102010-02-15 10:03:35 -08001975 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001976 ret = omap_hsmmc_reg_get(host);
1977 if (ret)
1978 goto err_reg;
1979 host->use_reg = 1;
1980 }
1981
David Brownellb583f262009-05-28 14:04:03 -07001982 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001983
1984 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001985 if ((mmc_slot(host).card_detect_irq)) {
NeilBrown7efab4f2011-12-30 12:35:13 +11001986 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1987 NULL,
1988 omap_hsmmc_detect,
1989 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1990 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001991 if (ret) {
1992 dev_dbg(mmc_dev(host->mmc),
1993 "Unable to grab MMC CD IRQ\n");
1994 goto err_irq_cd;
1995 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00001996 pdata->suspend = omap_hsmmc_suspend_cdirq;
1997 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001998 }
1999
Adrian Hunterb4175772010-05-26 14:42:06 -07002000 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002001
Adrian Hunterb62f6222009-09-22 16:45:01 -07002002 omap_hsmmc_protect_card(host);
2003
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002004 mmc_add_host(mmc);
2005
Denis Karpov191d1f12009-09-22 16:44:55 -07002006 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002007 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2008 if (ret < 0)
2009 goto err_slot_name;
2010 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002011 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002012 ret = device_create_file(&mmc->class_dev,
2013 &dev_attr_cover_switch);
2014 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002015 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002016 }
2017
Denis Karpov70a33412009-09-22 16:44:59 -07002018 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302019 pm_runtime_mark_last_busy(host->dev);
2020 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002021
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002022 return 0;
2023
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002024err_slot_name:
2025 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002026 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002027err_irq_cd:
2028 if (host->use_reg)
2029 omap_hsmmc_reg_put(host);
2030err_reg:
2031 if (host->pdata->cleanup)
2032 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002033err_irq_cd_init:
2034 free_irq(host->irq, host);
2035err_irq:
Balaji T Kd59d77e2012-02-24 21:14:33 +05302036 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002037 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002038 clk_put(host->fclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002039 if (host->got_dbclk) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002040 clk_disable(host->dbclk);
2041 clk_put(host->dbclk);
2042 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002043err1:
2044 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002045 platform_set_drvdata(pdev, NULL);
2046 mmc_free_host(mmc);
2047err_alloc:
2048 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002049err:
Chris Ball984b2032011-03-22 16:34:42 -07002050 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002051 return ret;
2052}
2053
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002054static int __devexit omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002055{
Denis Karpov70a33412009-09-22 16:44:59 -07002056 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002057 struct resource *res;
2058
Felipe Balbi927ce942012-03-14 11:18:27 +02002059 pm_runtime_get_sync(host->dev);
2060 mmc_remove_host(host->mmc);
2061 if (host->use_reg)
2062 omap_hsmmc_reg_put(host);
2063 if (host->pdata->cleanup)
2064 host->pdata->cleanup(&pdev->dev);
2065 free_irq(host->irq, host);
2066 if (mmc_slot(host).card_detect_irq)
2067 free_irq(mmc_slot(host).card_detect_irq, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002068
Felipe Balbi927ce942012-03-14 11:18:27 +02002069 pm_runtime_put_sync(host->dev);
2070 pm_runtime_disable(host->dev);
2071 clk_put(host->fclk);
2072 if (host->got_dbclk) {
2073 clk_disable(host->dbclk);
2074 clk_put(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002075 }
2076
Felipe Balbi927ce942012-03-14 11:18:27 +02002077 mmc_free_host(host->mmc);
2078 iounmap(host->base);
2079 omap_hsmmc_gpio_free(pdev->dev.platform_data);
2080
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002081 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2082 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002083 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002084 platform_set_drvdata(pdev, NULL);
2085
2086 return 0;
2087}
2088
2089#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002090static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002091{
2092 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002093 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2094
2095 if (!host)
2096 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002097
2098 if (host && host->suspended)
2099 return 0;
2100
Felipe Balbi927ce942012-03-14 11:18:27 +02002101 pm_runtime_get_sync(host->dev);
2102 host->suspended = 1;
2103 if (host->pdata->suspend) {
2104 ret = host->pdata->suspend(dev, host->slot_id);
Eliad Peller31f9d462011-11-22 16:02:17 +02002105 if (ret) {
Felipe Balbi927ce942012-03-14 11:18:27 +02002106 dev_dbg(dev, "Unable to handle MMC board"
2107 " level suspend\n");
Adrian Huntera6b22402009-09-22 16:44:45 -07002108 host->suspended = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002109 return ret;
Adrian Huntera6b22402009-09-22 16:44:45 -07002110 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002111 }
Felipe Balbi927ce942012-03-14 11:18:27 +02002112 ret = mmc_suspend_host(host->mmc);
2113
2114 if (ret) {
2115 host->suspended = 0;
2116 if (host->pdata->resume) {
2117 ret = host->pdata->resume(dev, host->slot_id);
2118 if (ret)
2119 dev_dbg(dev, "Unmask interrupt failed\n");
2120 }
2121 goto err;
2122 }
2123
2124 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2125 omap_hsmmc_disable_irq(host);
2126 OMAP_HSMMC_WRITE(host->base, HCTL,
2127 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2128 }
2129
2130 if (host->got_dbclk)
2131 clk_disable(host->dbclk);
Eliad Peller31f9d462011-11-22 16:02:17 +02002132err:
2133 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002134 return ret;
2135}
2136
2137/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002138static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002139{
2140 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002141 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2142
2143 if (!host)
2144 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002145
2146 if (host && !host->suspended)
2147 return 0;
2148
Felipe Balbi927ce942012-03-14 11:18:27 +02002149 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002150
Felipe Balbi927ce942012-03-14 11:18:27 +02002151 if (host->got_dbclk)
2152 clk_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002153
Felipe Balbi927ce942012-03-14 11:18:27 +02002154 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2155 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002156
Felipe Balbi927ce942012-03-14 11:18:27 +02002157 if (host->pdata->resume) {
2158 ret = host->pdata->resume(dev, host->slot_id);
2159 if (ret)
2160 dev_dbg(dev, "Unmask interrupt failed\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002161 }
2162
Felipe Balbi927ce942012-03-14 11:18:27 +02002163 omap_hsmmc_protect_card(host);
2164
2165 /* Notify the core to resume the host */
2166 ret = mmc_resume_host(host->mmc);
2167 if (ret == 0)
2168 host->suspended = 0;
2169
2170 pm_runtime_mark_last_busy(host->dev);
2171 pm_runtime_put_autosuspend(host->dev);
2172
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002173 return ret;
2174
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002175}
2176
2177#else
Denis Karpov70a33412009-09-22 16:44:59 -07002178#define omap_hsmmc_suspend NULL
2179#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002180#endif
2181
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302182static int omap_hsmmc_runtime_suspend(struct device *dev)
2183{
2184 struct omap_hsmmc_host *host;
2185
2186 host = platform_get_drvdata(to_platform_device(dev));
2187 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002188 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302189
2190 return 0;
2191}
2192
2193static int omap_hsmmc_runtime_resume(struct device *dev)
2194{
2195 struct omap_hsmmc_host *host;
2196
2197 host = platform_get_drvdata(to_platform_device(dev));
2198 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002199 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302200
2201 return 0;
2202}
2203
Kevin Hilmana791daa2010-05-26 14:42:07 -07002204static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002205 .suspend = omap_hsmmc_suspend,
2206 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302207 .runtime_suspend = omap_hsmmc_runtime_suspend,
2208 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002209};
2210
2211static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002212 .probe = omap_hsmmc_probe,
2213 .remove = __devexit_p(omap_hsmmc_remove),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002214 .driver = {
2215 .name = DRIVER_NAME,
2216 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002217 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302218 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002219 },
2220};
2221
Felipe Balbib7964502012-03-14 11:18:32 +02002222module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002223MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2224MODULE_LICENSE("GPL");
2225MODULE_ALIAS("platform:" DRIVER_NAME);
2226MODULE_AUTHOR("Texas Instruments Inc");