blob: 9b24bd46aad3ea5fd26d887e91eef1f46bdb76b5 [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Andy Shevchenkoac330f442011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
Russell Kingc5c98922012-04-13 12:14:39 +010022#include <linux/dmaengine.h>
Denis Karpovd900f712009-09-22 16:44:38 -070023#include <linux/seq_file.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010024#include <linux/interrupt.h>
25#include <linux/delay.h>
26#include <linux/dma-mapping.h>
27#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010028#include <linux/timer.h>
29#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053030#include <linux/of.h>
31#include <linux/of_gpio.h>
32#include <linux/of_device.h>
Russell King3451c062012-04-21 22:35:42 +010033#include <linux/omap-dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010034#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070035#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070036#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010037#include <linux/io.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080038#include <linux/gpio.h>
39#include <linux/regulator/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053040#include <linux/pm_runtime.h>
Tony Lindgren68f39e72012-10-15 12:09:43 -070041#include <linux/platform_data/mmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010042
43/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070044#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010045#define OMAP_HSMMC_CON 0x002C
46#define OMAP_HSMMC_BLK 0x0104
47#define OMAP_HSMMC_ARG 0x0108
48#define OMAP_HSMMC_CMD 0x010C
49#define OMAP_HSMMC_RSP10 0x0110
50#define OMAP_HSMMC_RSP32 0x0114
51#define OMAP_HSMMC_RSP54 0x0118
52#define OMAP_HSMMC_RSP76 0x011C
53#define OMAP_HSMMC_DATA 0x0120
54#define OMAP_HSMMC_HCTL 0x0128
55#define OMAP_HSMMC_SYSCTL 0x012C
56#define OMAP_HSMMC_STAT 0x0130
57#define OMAP_HSMMC_IE 0x0134
58#define OMAP_HSMMC_ISE 0x0138
59#define OMAP_HSMMC_CAPA 0x0140
60
61#define VS18 (1 << 26)
62#define VS30 (1 << 25)
63#define SDVS18 (0x5 << 9)
64#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080065#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010066#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010067#define SDVSCLR 0xFFFFF1FF
68#define SDVSDET 0x00000400
69#define AUTOIDLE 0x1
70#define SDBP (1 << 8)
71#define DTO 0xe
72#define ICE 0x1
73#define ICS 0x2
74#define CEN (1 << 2)
75#define CLKD_MASK 0x0000FFC0
76#define CLKD_SHIFT 6
77#define DTO_MASK 0x000F0000
78#define DTO_SHIFT 16
79#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070080#define BWR_ENABLE (1 << 4)
81#define BRR_ENABLE (1 << 5)
Adrian Hunter93caf8e692010-08-11 14:17:48 -070082#define DTO_ENABLE (1 << 20)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010083#define INIT_STREAM (1 << 1)
84#define DP_SELECT (1 << 21)
85#define DDIR (1 << 4)
86#define DMA_EN 0x1
87#define MSBS (1 << 5)
88#define BCE (1 << 1)
89#define FOUR_BIT (1 << 1)
Balaji T K03b5d922012-04-09 12:08:33 +053090#define DDR (1 << 19)
Jarkko Lavinen73153012008-11-21 16:49:54 +020091#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010092#define CC 0x1
93#define TC 0x02
94#define OD 0x1
95#define ERR (1 << 15)
96#define CMD_TIMEOUT (1 << 16)
97#define DATA_TIMEOUT (1 << 20)
98#define CMD_CRC (1 << 17)
99#define DATA_CRC (1 << 21)
100#define CARD_ERR (1 << 28)
101#define STAT_CLEAR 0xFFFFFFFF
102#define INIT_STREAM_CMD 0x00000000
103#define DUAL_VOLT_OCR_BIT 7
104#define SRC (1 << 25)
105#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700106#define SOFTRESET (1 << 1)
107#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100108
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530109#define MMC_AUTOSUSPEND_DELAY 100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100110#define MMC_TIMEOUT_MS 20
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400111#define OMAP_MMC_MIN_CLOCK 400000
112#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530113#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100114
115/*
116 * One controller can have multiple slots, like on some omap boards using
117 * omap.c controller driver. Luckily this is not currently done on any known
118 * omap_hsmmc.c device.
119 */
120#define mmc_slot(host) (host->pdata->slots[host->slot_id])
121
122/*
123 * MMC Host controller read/write API's
124 */
125#define OMAP_HSMMC_READ(base, reg) \
126 __raw_readl((base) + OMAP_HSMMC_##reg)
127
128#define OMAP_HSMMC_WRITE(base, reg, val) \
129 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
130
Per Forlin9782aff2011-07-01 18:55:23 +0200131struct omap_hsmmc_next {
132 unsigned int dma_len;
133 s32 cookie;
134};
135
Denis Karpov70a33412009-09-22 16:44:59 -0700136struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100137 struct device *dev;
138 struct mmc_host *mmc;
139 struct mmc_request *mrq;
140 struct mmc_command *cmd;
141 struct mmc_data *data;
142 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100143 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800144 /*
145 * vcc == configured supply
146 * vcc_aux == optional
147 * - MMC1, supply for DAT4..DAT7
148 * - MMC2/MMC2, external level shifter voltage supply, for
149 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
150 */
151 struct regulator *vcc;
152 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100153 void __iomem *base;
154 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700155 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100156 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200157 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100158 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700159 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100160 int suspended;
161 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100162 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100163 struct dma_chan *tx_chan;
164 struct dma_chan *rx_chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100165 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200166 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700167 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700168 int protect_card;
169 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800170 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700171 int req_in_progress;
Per Forlin9782aff2011-07-01 18:55:23 +0200172 struct omap_hsmmc_next next_data;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700173
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100174 struct omap_mmc_platform_data *pdata;
175};
176
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800177static int omap_hsmmc_card_detect(struct device *dev, int slot)
178{
179 struct omap_mmc_platform_data *mmc = dev->platform_data;
180
181 /* NOTE: assumes card detect signal is active-low */
182 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
183}
184
185static int omap_hsmmc_get_wp(struct device *dev, int slot)
186{
187 struct omap_mmc_platform_data *mmc = dev->platform_data;
188
189 /* NOTE: assumes write protect signal is active-high */
190 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
191}
192
193static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
194{
195 struct omap_mmc_platform_data *mmc = dev->platform_data;
196
197 /* NOTE: assumes card detect signal is active-low */
198 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
199}
200
201#ifdef CONFIG_PM
202
203static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
204{
205 struct omap_mmc_platform_data *mmc = dev->platform_data;
206
207 disable_irq(mmc->slots[0].card_detect_irq);
208 return 0;
209}
210
211static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
212{
213 struct omap_mmc_platform_data *mmc = dev->platform_data;
214
215 enable_irq(mmc->slots[0].card_detect_irq);
216 return 0;
217}
218
219#else
220
221#define omap_hsmmc_suspend_cdirq NULL
222#define omap_hsmmc_resume_cdirq NULL
223
224#endif
225
Adrian Hunterb702b102010-02-15 10:03:35 -0800226#ifdef CONFIG_REGULATOR
227
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500228static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800229 int vdd)
230{
231 struct omap_hsmmc_host *host =
232 platform_get_drvdata(to_platform_device(dev));
233 int ret = 0;
234
235 /*
236 * If we don't see a Vcc regulator, assume it's a fixed
237 * voltage always-on regulator.
238 */
239 if (!host->vcc)
240 return 0;
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530241 /*
242 * With DT, never turn OFF the regulator. This is because
243 * the pbias cell programming support is still missing when
244 * booting with Device tree
245 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +0530246 if (dev->of_node && !vdd)
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530247 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800248
249 if (mmc_slot(host).before_set_reg)
250 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
251
252 /*
253 * Assume Vcc regulator is used only to power the card ... OMAP
254 * VDDS is used to power the pins, optionally with a transceiver to
255 * support cards using voltages other than VDDS (1.8V nominal). When a
256 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
257 *
258 * In some cases this regulator won't support enable/disable;
259 * e.g. it's a fixed rail for a WLAN chip.
260 *
261 * In other cases vcc_aux switches interface power. Example, for
262 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
263 * chips/cards need an interface voltage rail too.
264 */
265 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400266 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800267 /* Enable interface voltage rail, if needed */
268 if (ret == 0 && host->vcc_aux) {
269 ret = regulator_enable(host->vcc_aux);
270 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400271 ret = mmc_regulator_set_ocr(host->mmc,
272 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800273 }
274 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400275 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800276 if (host->vcc_aux)
277 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400278 if (!ret) {
279 /* Then proceed to shut down the local regulator */
280 ret = mmc_regulator_set_ocr(host->mmc,
281 host->vcc, 0);
282 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800283 }
284
285 if (mmc_slot(host).after_set_reg)
286 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
287
288 return ret;
289}
290
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800291static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
292{
293 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700294 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800295
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800296 reg = regulator_get(host->dev, "vmmc");
297 if (IS_ERR(reg)) {
298 dev_dbg(host->dev, "vmmc regulator missing\n");
NeilBrown1fdc90f2012-08-08 00:06:00 -0400299 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800300 } else {
NeilBrown1fdc90f2012-08-08 00:06:00 -0400301 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800302 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700303 ocr_value = mmc_regulator_get_ocrmask(reg);
304 if (!mmc_slot(host).ocr_mask) {
305 mmc_slot(host).ocr_mask = ocr_value;
306 } else {
307 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530308 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500309 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700310 mmc_slot(host).ocr_mask = 0;
311 return -EINVAL;
312 }
313 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800314
315 /* Allow an aux regulator */
316 reg = regulator_get(host->dev, "vmmc_aux");
317 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
318
Balaji T Kb1c1df72011-05-30 19:55:34 +0530319 /* For eMMC do not power off when not in sleep state */
320 if (mmc_slot(host).no_regulator_off_init)
321 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800322 /*
323 * UGLY HACK: workaround regulator framework bugs.
324 * When the bootloader leaves a supply active, it's
325 * initialized with zero usecount ... and we can't
326 * disable it without first enabling it. Until the
327 * framework is fixed, we need a workaround like this
328 * (which is safe for MMC, but not in general).
329 */
Adrian Huntere840ce12011-05-06 12:14:10 +0300330 if (regulator_is_enabled(host->vcc) > 0 ||
331 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
332 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
333
334 mmc_slot(host).set_power(host->dev, host->slot_id,
335 1, vdd);
336 mmc_slot(host).set_power(host->dev, host->slot_id,
337 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800338 }
339 }
340
341 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800342}
343
344static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
345{
346 regulator_put(host->vcc);
347 regulator_put(host->vcc_aux);
348 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800349}
350
Adrian Hunterb702b102010-02-15 10:03:35 -0800351static inline int omap_hsmmc_have_reg(void)
352{
353 return 1;
354}
355
356#else
357
358static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
359{
360 return -EINVAL;
361}
362
363static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
364{
365}
366
367static inline int omap_hsmmc_have_reg(void)
368{
369 return 0;
370}
371
372#endif
373
374static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
375{
376 int ret;
377
378 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800379 if (pdata->slots[0].cover)
380 pdata->slots[0].get_cover_state =
381 omap_hsmmc_get_cover_state;
382 else
383 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
384 pdata->slots[0].card_detect_irq =
385 gpio_to_irq(pdata->slots[0].switch_pin);
386 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
387 if (ret)
388 return ret;
389 ret = gpio_direction_input(pdata->slots[0].switch_pin);
390 if (ret)
391 goto err_free_sp;
392 } else
393 pdata->slots[0].switch_pin = -EINVAL;
394
395 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
396 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
397 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
398 if (ret)
399 goto err_free_cd;
400 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
401 if (ret)
402 goto err_free_wp;
403 } else
404 pdata->slots[0].gpio_wp = -EINVAL;
405
406 return 0;
407
408err_free_wp:
409 gpio_free(pdata->slots[0].gpio_wp);
410err_free_cd:
411 if (gpio_is_valid(pdata->slots[0].switch_pin))
412err_free_sp:
413 gpio_free(pdata->slots[0].switch_pin);
414 return ret;
415}
416
417static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
418{
419 if (gpio_is_valid(pdata->slots[0].gpio_wp))
420 gpio_free(pdata->slots[0].gpio_wp);
421 if (gpio_is_valid(pdata->slots[0].switch_pin))
422 gpio_free(pdata->slots[0].switch_pin);
423}
424
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100425/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300426 * Start clock to the card
427 */
428static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
429{
430 OMAP_HSMMC_WRITE(host->base, SYSCTL,
431 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
432}
433
434/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100435 * Stop clock to the card
436 */
Denis Karpov70a33412009-09-22 16:44:59 -0700437static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100438{
439 OMAP_HSMMC_WRITE(host->base, SYSCTL,
440 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
441 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900442 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100443}
444
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700445static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
446 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700447{
448 unsigned int irq_mask;
449
450 if (host->use_dma)
451 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
452 else
453 irq_mask = INT_EN_MASK;
454
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700455 /* Disable timeout for erases */
456 if (cmd->opcode == MMC_ERASE)
457 irq_mask &= ~DTO_ENABLE;
458
Adrian Hunterb4175772010-05-26 14:42:06 -0700459 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
460 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
461 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
462}
463
464static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
465{
466 OMAP_HSMMC_WRITE(host->base, ISE, 0);
467 OMAP_HSMMC_WRITE(host->base, IE, 0);
468 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
469}
470
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300471/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530472static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300473{
474 u16 dsor = 0;
475
476 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530477 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300478 if (dsor > 250)
479 dsor = 250;
480 }
481
482 return dsor;
483}
484
Andy Shevchenko5934df22011-05-06 12:14:06 +0300485static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
486{
487 struct mmc_ios *ios = &host->mmc->ios;
488 unsigned long regval;
489 unsigned long timeout;
490
Venkatraman S8986d312012-08-07 19:10:38 +0530491 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300492
493 omap_hsmmc_stop_clock(host);
494
495 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
496 regval = regval & ~(CLKD_MASK | DTO_MASK);
Balaji TKd83b6e02011-12-20 15:12:00 +0530497 regval = regval | (calc_divisor(host, ios) << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300498 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
499 OMAP_HSMMC_WRITE(host->base, SYSCTL,
500 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
501
502 /* Wait till the ICS bit is set */
503 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
504 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
505 && time_before(jiffies, timeout))
506 cpu_relax();
507
508 omap_hsmmc_start_clock(host);
509}
510
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400511static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
512{
513 struct mmc_ios *ios = &host->mmc->ios;
514 u32 con;
515
516 con = OMAP_HSMMC_READ(host->base, CON);
Balaji T K03b5d922012-04-09 12:08:33 +0530517 if (ios->timing == MMC_TIMING_UHS_DDR50)
518 con |= DDR; /* configure in DDR mode */
519 else
520 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400521 switch (ios->bus_width) {
522 case MMC_BUS_WIDTH_8:
523 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
524 break;
525 case MMC_BUS_WIDTH_4:
526 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
527 OMAP_HSMMC_WRITE(host->base, HCTL,
528 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
529 break;
530 case MMC_BUS_WIDTH_1:
531 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
532 OMAP_HSMMC_WRITE(host->base, HCTL,
533 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
534 break;
535 }
536}
537
538static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
539{
540 struct mmc_ios *ios = &host->mmc->ios;
541 u32 con;
542
543 con = OMAP_HSMMC_READ(host->base, CON);
544 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
545 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
546 else
547 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
548}
549
Denis Karpov11dd62a2009-09-22 16:44:43 -0700550#ifdef CONFIG_PM
551
552/*
553 * Restore the MMC host context, if it was lost as result of a
554 * power state change.
555 */
Denis Karpov70a33412009-09-22 16:44:59 -0700556static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700557{
558 struct mmc_ios *ios = &host->mmc->ios;
559 struct omap_mmc_platform_data *pdata = host->pdata;
560 int context_loss = 0;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400561 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700562 unsigned long timeout;
563
564 if (pdata->get_context_loss_count) {
565 context_loss = pdata->get_context_loss_count(host->dev);
566 if (context_loss < 0)
567 return 1;
568 }
569
570 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
571 context_loss == host->context_loss ? "not " : "");
572 if (host->context_loss == context_loss)
573 return 1;
574
Venkatraman S6c31b212012-08-08 14:26:52 +0530575 if (!OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE)
576 return 1;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700577
Balaji T Kc2200ef2012-03-07 09:55:30 -0500578 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700579 if (host->power_mode != MMC_POWER_OFF &&
580 (1 << ios->vdd) <= MMC_VDD_23_24)
581 hctl = SDVS18;
582 else
583 hctl = SDVS30;
584 capa = VS30 | VS18;
585 } else {
586 hctl = SDVS18;
587 capa = VS18;
588 }
589
590 OMAP_HSMMC_WRITE(host->base, HCTL,
591 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
592
593 OMAP_HSMMC_WRITE(host->base, CAPA,
594 OMAP_HSMMC_READ(host->base, CAPA) | capa);
595
596 OMAP_HSMMC_WRITE(host->base, HCTL,
597 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
598
599 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
600 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
601 && time_before(jiffies, timeout))
602 ;
603
Adrian Hunterb4175772010-05-26 14:42:06 -0700604 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700605
606 /* Do not initialize card-specific things if the power is off */
607 if (host->power_mode == MMC_POWER_OFF)
608 goto out;
609
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400610 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700611
Andy Shevchenko5934df22011-05-06 12:14:06 +0300612 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700613
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400614 omap_hsmmc_set_bus_mode(host);
615
Denis Karpov11dd62a2009-09-22 16:44:43 -0700616out:
617 host->context_loss = context_loss;
618
619 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
620 return 0;
621}
622
623/*
624 * Save the MMC host context (store the number of power state changes so far).
625 */
Denis Karpov70a33412009-09-22 16:44:59 -0700626static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700627{
628 struct omap_mmc_platform_data *pdata = host->pdata;
629 int context_loss;
630
631 if (pdata->get_context_loss_count) {
632 context_loss = pdata->get_context_loss_count(host->dev);
633 if (context_loss < 0)
634 return;
635 host->context_loss = context_loss;
636 }
637}
638
639#else
640
Denis Karpov70a33412009-09-22 16:44:59 -0700641static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700642{
643 return 0;
644}
645
Denis Karpov70a33412009-09-22 16:44:59 -0700646static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700647{
648}
649
650#endif
651
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100652/*
653 * Send init stream sequence to card
654 * before sending IDLE command
655 */
Denis Karpov70a33412009-09-22 16:44:59 -0700656static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100657{
658 int reg = 0;
659 unsigned long timeout;
660
Adrian Hunterb62f6222009-09-22 16:45:01 -0700661 if (host->protect_card)
662 return;
663
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100664 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700665
666 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100667 OMAP_HSMMC_WRITE(host->base, CON,
668 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
669 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
670
671 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
672 while ((reg != CC) && time_before(jiffies, timeout))
673 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
674
675 OMAP_HSMMC_WRITE(host->base, CON,
676 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700677
678 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
679 OMAP_HSMMC_READ(host->base, STAT);
680
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100681 enable_irq(host->irq);
682}
683
684static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700685int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100686{
687 int r = 1;
688
Denis Karpov191d1f12009-09-22 16:44:55 -0700689 if (mmc_slot(host).get_cover_state)
690 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100691 return r;
692}
693
694static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700695omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100696 char *buf)
697{
698 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700699 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100700
Denis Karpov70a33412009-09-22 16:44:59 -0700701 return sprintf(buf, "%s\n",
702 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100703}
704
Denis Karpov70a33412009-09-22 16:44:59 -0700705static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100706
707static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700708omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100709 char *buf)
710{
711 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700712 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100713
Denis Karpov191d1f12009-09-22 16:44:55 -0700714 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100715}
716
Denis Karpov70a33412009-09-22 16:44:59 -0700717static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100718
719/*
720 * Configure the response type and send the cmd.
721 */
722static void
Denis Karpov70a33412009-09-22 16:44:59 -0700723omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100724 struct mmc_data *data)
725{
726 int cmdreg = 0, resptype = 0, cmdtype = 0;
727
Venkatraman S8986d312012-08-07 19:10:38 +0530728 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100729 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
730 host->cmd = cmd;
731
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700732 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100733
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200734 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100735 if (cmd->flags & MMC_RSP_PRESENT) {
736 if (cmd->flags & MMC_RSP_136)
737 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200738 else if (cmd->flags & MMC_RSP_BUSY) {
739 resptype = 3;
740 host->response_busy = 1;
741 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100742 resptype = 2;
743 }
744
745 /*
746 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
747 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
748 * a val of 0x3, rest 0x0.
749 */
750 if (cmd == host->mrq->stop)
751 cmdtype = 0x3;
752
753 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
754
755 if (data) {
756 cmdreg |= DP_SELECT | MSBS | BCE;
757 if (data->flags & MMC_DATA_READ)
758 cmdreg |= DDIR;
759 else
760 cmdreg &= ~(DDIR);
761 }
762
763 if (host->use_dma)
764 cmdreg |= DMA_EN;
765
Adrian Hunterb4175772010-05-26 14:42:06 -0700766 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700767
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100768 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
769 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
770}
771
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200772static int
Denis Karpov70a33412009-09-22 16:44:59 -0700773omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200774{
775 if (data->flags & MMC_DATA_WRITE)
776 return DMA_TO_DEVICE;
777 else
778 return DMA_FROM_DEVICE;
779}
780
Russell Kingc5c98922012-04-13 12:14:39 +0100781static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
782 struct mmc_data *data)
783{
784 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
785}
786
Adrian Hunterb4175772010-05-26 14:42:06 -0700787static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
788{
789 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530790 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700791
Venkatraman S31463b12012-04-09 12:08:34 +0530792 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700793 host->req_in_progress = 0;
794 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530795 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700796
797 omap_hsmmc_disable_irq(host);
798 /* Do not complete the request if DMA is still in progress */
799 if (mrq->data && host->use_dma && dma_ch != -1)
800 return;
801 host->mrq = NULL;
802 mmc_request_done(host->mmc, mrq);
803}
804
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100805/*
806 * Notify the transfer complete to MMC core
807 */
808static void
Denis Karpov70a33412009-09-22 16:44:59 -0700809omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100810{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200811 if (!data) {
812 struct mmc_request *mrq = host->mrq;
813
Adrian Hunter23050102009-09-22 16:44:57 -0700814 /* TC before CC from CMD6 - don't know why, but it happens */
815 if (host->cmd && host->cmd->opcode == 6 &&
816 host->response_busy) {
817 host->response_busy = 0;
818 return;
819 }
820
Adrian Hunterb4175772010-05-26 14:42:06 -0700821 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200822 return;
823 }
824
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100825 host->data = NULL;
826
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100827 if (!data->error)
828 data->bytes_xfered += data->blocks * (data->blksz);
829 else
830 data->bytes_xfered = 0;
831
Ming Leife852272012-06-22 18:49:35 +0800832 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700833 omap_hsmmc_request_done(host, data->mrq);
Ming Leife852272012-06-22 18:49:35 +0800834 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100835 }
Ming Leife852272012-06-22 18:49:35 +0800836 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100837}
838
839/*
840 * Notify the core about command completion
841 */
842static void
Denis Karpov70a33412009-09-22 16:44:59 -0700843omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100844{
845 host->cmd = NULL;
846
847 if (cmd->flags & MMC_RSP_PRESENT) {
848 if (cmd->flags & MMC_RSP_136) {
849 /* response type 2 */
850 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
851 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
852 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
853 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
854 } else {
855 /* response types 1, 1b, 3, 4, 5, 6 */
856 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
857 }
858 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700859 if ((host->data == NULL && !host->response_busy) || cmd->error)
860 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100861}
862
863/*
864 * DMA clean up for command errors
865 */
Denis Karpov70a33412009-09-22 16:44:59 -0700866static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100867{
Adrian Hunterb4175772010-05-26 14:42:06 -0700868 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530869 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700870
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200871 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100872
Venkatraman S31463b12012-04-09 12:08:34 +0530873 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700874 dma_ch = host->dma_ch;
875 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530876 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700877
878 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100879 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
880
881 dmaengine_terminate_all(chan);
882 dma_unmap_sg(chan->device->dev,
883 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700884 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100885
Per Forlin053bf342011-11-07 21:55:11 +0530886 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100887 }
888 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100889}
890
891/*
892 * Readable error output
893 */
894#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300895static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100896{
897 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700898 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300899 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
900 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
901 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
902 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100903 };
904 char res[256];
905 char *buf = res;
906 int len, i;
907
908 len = sprintf(buf, "MMC IRQ 0x%x :", status);
909 buf += len;
910
Denis Karpov70a33412009-09-22 16:44:59 -0700911 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100912 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700913 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100914 buf += len;
915 }
916
Venkatraman S8986d312012-08-07 19:10:38 +0530917 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100918}
Adrian Hunter699b9582011-05-06 12:14:01 +0300919#else
920static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
921 u32 status)
922{
923}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100924#endif /* CONFIG_MMC_DEBUG */
925
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100926/*
927 * MMC controller internal state machines reset
928 *
929 * Used to reset command or data internal state machines, using respectively
930 * SRC or SRD bit of SYSCTL register
931 * Can be called from interrupt context
932 */
Denis Karpov70a33412009-09-22 16:44:59 -0700933static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
934 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100935{
936 unsigned long i = 0;
937 unsigned long limit = (loops_per_jiffy *
938 msecs_to_jiffies(MMC_TIMEOUT_MS));
939
940 OMAP_HSMMC_WRITE(host->base, SYSCTL,
941 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
942
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700943 /*
944 * OMAP4 ES2 and greater has an updated reset logic.
945 * Monitor a 0->1 transition first
946 */
947 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -0500948 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700949 && (i++ < limit))
950 cpu_relax();
951 }
952 i = 0;
953
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100954 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
955 (i++ < limit))
956 cpu_relax();
957
958 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
959 dev_err(mmc_dev(host->mmc),
960 "Timeout waiting on controller reset in %s\n",
961 __func__);
962}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100963
Venkatraman Sae4bf782012-08-09 20:36:07 +0530964static void hsmmc_command_incomplete(struct omap_hsmmc_host *host, int err)
965{
966 omap_hsmmc_reset_controller_fsm(host, SRC);
967 host->cmd->error = err;
968
969 if (host->data) {
970 omap_hsmmc_reset_controller_fsm(host, SRD);
971 omap_hsmmc_dma_cleanup(host, err);
972 }
973
974}
975
Adrian Hunterb4175772010-05-26 14:42:06 -0700976static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100977{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100978 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -0700979 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100980
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100981 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +0530982 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100983
984 if (status & ERR) {
Adrian Hunter699b9582011-05-06 12:14:01 +0300985 omap_hsmmc_dbg_report_irq(host, status);
Venkatraman Sae4bf782012-08-09 20:36:07 +0530986 if (status & (CMD_TIMEOUT | DATA_TIMEOUT))
987 hsmmc_command_incomplete(host, -ETIMEDOUT);
988 else if (status & (CMD_CRC | DATA_CRC))
989 hsmmc_command_incomplete(host, -EILSEQ);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200990
Venkatraman Sae4bf782012-08-09 20:36:07 +0530991 end_cmd = 1;
992 if (host->data || host->response_busy) {
993 end_trans = 1;
994 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100995 }
996 }
997
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +0300998 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -0700999 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001000 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001001 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001002}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001003
Adrian Hunterb4175772010-05-26 14:42:06 -07001004/*
1005 * MMC controller IRQ handler
1006 */
1007static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1008{
1009 struct omap_hsmmc_host *host = dev_id;
1010 int status;
1011
1012 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301013 while (status & INT_EN_MASK && host->req_in_progress) {
Adrian Hunterb4175772010-05-26 14:42:06 -07001014 omap_hsmmc_do_irq(host, status);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301015
Adrian Hunterb4175772010-05-26 14:42:06 -07001016 /* Flush posted write */
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301017 OMAP_HSMMC_WRITE(host->base, STAT, status);
Adrian Hunterb4175772010-05-26 14:42:06 -07001018 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301019 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001020
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001021 return IRQ_HANDLED;
1022}
1023
Denis Karpov70a33412009-09-22 16:44:59 -07001024static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001025{
1026 unsigned long i;
1027
1028 OMAP_HSMMC_WRITE(host->base, HCTL,
1029 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1030 for (i = 0; i < loops_per_jiffy; i++) {
1031 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1032 break;
1033 cpu_relax();
1034 }
1035}
1036
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001037/*
David Brownelleb250822009-02-17 14:49:01 -08001038 * Switch MMC interface voltage ... only relevant for MMC1.
1039 *
1040 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1041 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1042 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001043 */
Denis Karpov70a33412009-09-22 16:44:59 -07001044static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001045{
1046 u32 reg_val = 0;
1047 int ret;
1048
1049 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301050 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301051 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301052 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001053
1054 /* Turn the power off */
1055 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001056
1057 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001058 if (!ret)
1059 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1060 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301061 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301062 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301063 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001064
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001065 if (ret != 0)
1066 goto err;
1067
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001068 OMAP_HSMMC_WRITE(host->base, HCTL,
1069 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1070 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001071
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001072 /*
1073 * If a MMC dual voltage card is detected, the set_ios fn calls
1074 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001075 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001076 *
David Brownelleb250822009-02-17 14:49:01 -08001077 * Cope with a bit of slop in the range ... per data sheets:
1078 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1079 * but recommended values are 1.71V to 1.89V
1080 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1081 * but recommended values are 2.7V to 3.3V
1082 *
1083 * Board setup code shouldn't permit anything very out-of-range.
1084 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1085 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001086 */
David Brownelleb250822009-02-17 14:49:01 -08001087 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001088 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001089 else
1090 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001091
1092 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001093 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001094
1095 return 0;
1096err:
1097 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1098 return ret;
1099}
1100
Adrian Hunterb62f6222009-09-22 16:45:01 -07001101/* Protect the card while the cover is open */
1102static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1103{
1104 if (!mmc_slot(host).get_cover_state)
1105 return;
1106
1107 host->reqs_blocked = 0;
1108 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1109 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301110 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001111 "card is now accessible\n",
1112 mmc_hostname(host->mmc));
1113 host->protect_card = 0;
1114 }
1115 } else {
1116 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301117 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001118 "card is now inaccessible\n",
1119 mmc_hostname(host->mmc));
1120 host->protect_card = 1;
1121 }
1122 }
1123}
1124
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001125/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001126 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001127 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001128static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001129{
NeilBrown7efab4f2011-12-30 12:35:13 +11001130 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001131 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001132 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001133
Adrian Huntera6b22402009-09-22 16:44:45 -07001134 if (host->suspended)
NeilBrown7efab4f2011-12-30 12:35:13 +11001135 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001136
1137 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001138
Denis Karpov191d1f12009-09-22 16:44:55 -07001139 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001140 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001141 else {
1142 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001143 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001144 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001145
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001146 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001147 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001148 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001149 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001150 return IRQ_HANDLED;
1151}
1152
Russell Kingc5c98922012-04-13 12:14:39 +01001153static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001154{
Russell Kingc5c98922012-04-13 12:14:39 +01001155 struct omap_hsmmc_host *host = param;
1156 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001157 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001158 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001159
Russell Kingc5c98922012-04-13 12:14:39 +01001160 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001161 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001162 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001163 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001164 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001165
Adrian Hunter770d7432011-05-06 12:14:11 +03001166 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001167 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001168 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001169 dma_unmap_sg(chan->device->dev,
1170 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001171 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001172
1173 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001174 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001175 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001176
1177 /* If DMA has finished after TC, complete the request */
1178 if (!req_in_progress) {
1179 struct mmc_request *mrq = host->mrq;
1180
1181 host->mrq = NULL;
1182 mmc_request_done(host->mmc, mrq);
1183 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001184}
1185
Per Forlin9782aff2011-07-01 18:55:23 +02001186static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1187 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001188 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001189 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001190{
1191 int dma_len;
1192
1193 if (!next && data->host_cookie &&
1194 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301195 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001196 " host->next_data.cookie %d\n",
1197 __func__, data->host_cookie, host->next_data.cookie);
1198 data->host_cookie = 0;
1199 }
1200
1201 /* Check if next job is already prepared */
1202 if (next ||
1203 (!next && data->host_cookie != host->next_data.cookie)) {
Russell King26b88522012-04-13 12:27:37 +01001204 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001205 omap_hsmmc_get_dma_dir(host, data));
1206
1207 } else {
1208 dma_len = host->next_data.dma_len;
1209 host->next_data.dma_len = 0;
1210 }
1211
1212
1213 if (dma_len == 0)
1214 return -EINVAL;
1215
1216 if (next) {
1217 next->dma_len = dma_len;
1218 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1219 } else
1220 host->dma_len = dma_len;
1221
1222 return 0;
1223}
1224
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001225/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001226 * Routine to configure and start DMA for the MMC card
1227 */
Denis Karpov70a33412009-09-22 16:44:59 -07001228static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1229 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001230{
Russell King26b88522012-04-13 12:27:37 +01001231 struct dma_slave_config cfg;
1232 struct dma_async_tx_descriptor *tx;
1233 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001234 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001235 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001236
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001237 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001238 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001239 struct scatterlist *sgl;
1240
1241 sgl = data->sg + i;
1242 if (sgl->length % data->blksz)
1243 return -EINVAL;
1244 }
1245 if ((data->blksz % 4) != 0)
1246 /* REVISIT: The MMC buffer increments only when MSB is written.
1247 * Return error for blksz which is non multiple of four.
1248 */
1249 return -EINVAL;
1250
Adrian Hunterb4175772010-05-26 14:42:06 -07001251 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001252
Russell Kingc5c98922012-04-13 12:14:39 +01001253 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001254
Russell King26b88522012-04-13 12:27:37 +01001255 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1256 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1257 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1258 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1259 cfg.src_maxburst = data->blksz / 4;
1260 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001261
Russell King26b88522012-04-13 12:27:37 +01001262 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001263 if (ret)
1264 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001265
Russell King26b88522012-04-13 12:27:37 +01001266 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1267 if (ret)
1268 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001269
Russell King26b88522012-04-13 12:27:37 +01001270 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1271 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1272 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1273 if (!tx) {
1274 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1275 /* FIXME: cleanup */
1276 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001277 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001278
Russell King26b88522012-04-13 12:27:37 +01001279 tx->callback = omap_hsmmc_dma_callback;
1280 tx->callback_param = host;
1281
1282 /* Does not fail */
1283 dmaengine_submit(tx);
1284
1285 host->dma_ch = 1;
1286
1287 dma_async_issue_pending(chan);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001288
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001289 return 0;
1290}
1291
Denis Karpov70a33412009-09-22 16:44:59 -07001292static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001293 unsigned int timeout_ns,
1294 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001295{
1296 unsigned int timeout, cycle_ns;
1297 uint32_t reg, clkd, dto = 0;
1298
1299 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1300 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1301 if (clkd == 0)
1302 clkd = 1;
1303
1304 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001305 timeout = timeout_ns / cycle_ns;
1306 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001307 if (timeout) {
1308 while ((timeout & 0x80000000) == 0) {
1309 dto += 1;
1310 timeout <<= 1;
1311 }
1312 dto = 31 - dto;
1313 timeout <<= 1;
1314 if (timeout && dto)
1315 dto += 1;
1316 if (dto >= 13)
1317 dto -= 13;
1318 else
1319 dto = 0;
1320 if (dto > 14)
1321 dto = 14;
1322 }
1323
1324 reg &= ~DTO_MASK;
1325 reg |= dto << DTO_SHIFT;
1326 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1327}
1328
1329/*
1330 * Configure block length for MMC/SD cards and initiate the transfer.
1331 */
1332static int
Denis Karpov70a33412009-09-22 16:44:59 -07001333omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001334{
1335 int ret;
1336 host->data = req->data;
1337
1338 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001339 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001340 /*
1341 * Set an arbitrary 100ms data timeout for commands with
1342 * busy signal.
1343 */
1344 if (req->cmd->flags & MMC_RSP_BUSY)
1345 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001346 return 0;
1347 }
1348
1349 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1350 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001351 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001352
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001353 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001354 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001355 if (ret != 0) {
1356 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1357 return ret;
1358 }
1359 }
1360 return 0;
1361}
1362
Per Forlin9782aff2011-07-01 18:55:23 +02001363static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1364 int err)
1365{
1366 struct omap_hsmmc_host *host = mmc_priv(mmc);
1367 struct mmc_data *data = mrq->data;
1368
Russell King26b88522012-04-13 12:27:37 +01001369 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001370 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001371
Russell King26b88522012-04-13 12:27:37 +01001372 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1373 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001374 data->host_cookie = 0;
1375 }
1376}
1377
1378static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1379 bool is_first_req)
1380{
1381 struct omap_hsmmc_host *host = mmc_priv(mmc);
1382
1383 if (mrq->data->host_cookie) {
1384 mrq->data->host_cookie = 0;
1385 return ;
1386 }
1387
Russell Kingc5c98922012-04-13 12:14:39 +01001388 if (host->use_dma) {
1389 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001390
Per Forlin9782aff2011-07-01 18:55:23 +02001391 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001392 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001393 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001394 }
Per Forlin9782aff2011-07-01 18:55:23 +02001395}
1396
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001397/*
1398 * Request function. for read/write operation
1399 */
Denis Karpov70a33412009-09-22 16:44:59 -07001400static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001401{
Denis Karpov70a33412009-09-22 16:44:59 -07001402 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001403 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001404
Adrian Hunterb4175772010-05-26 14:42:06 -07001405 BUG_ON(host->req_in_progress);
1406 BUG_ON(host->dma_ch != -1);
1407 if (host->protect_card) {
1408 if (host->reqs_blocked < 3) {
1409 /*
1410 * Ensure the controller is left in a consistent
1411 * state by resetting the command and data state
1412 * machines.
1413 */
1414 omap_hsmmc_reset_controller_fsm(host, SRD);
1415 omap_hsmmc_reset_controller_fsm(host, SRC);
1416 host->reqs_blocked += 1;
1417 }
1418 req->cmd->error = -EBADF;
1419 if (req->data)
1420 req->data->error = -EBADF;
1421 req->cmd->retries = 0;
1422 mmc_request_done(mmc, req);
1423 return;
1424 } else if (host->reqs_blocked)
1425 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001426 WARN_ON(host->mrq != NULL);
1427 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001428 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001429 if (err) {
1430 req->cmd->error = err;
1431 if (req->data)
1432 req->data->error = err;
1433 host->mrq = NULL;
1434 mmc_request_done(mmc, req);
1435 return;
1436 }
1437
Denis Karpov70a33412009-09-22 16:44:59 -07001438 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001439}
1440
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001441/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001442static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001443{
Denis Karpov70a33412009-09-22 16:44:59 -07001444 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001445 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001446
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301447 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001448
Adrian Huntera3621462009-09-22 16:44:42 -07001449 if (ios->power_mode != host->power_mode) {
1450 switch (ios->power_mode) {
1451 case MMC_POWER_OFF:
1452 mmc_slot(host).set_power(host->dev, host->slot_id,
1453 0, 0);
1454 break;
1455 case MMC_POWER_UP:
1456 mmc_slot(host).set_power(host->dev, host->slot_id,
1457 1, ios->vdd);
1458 break;
1459 case MMC_POWER_ON:
1460 do_send_init_stream = 1;
1461 break;
1462 }
1463 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001464 }
1465
Denis Karpovdd498ef2009-09-22 16:44:49 -07001466 /* FIXME: set registers based only on changes to ios */
1467
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001468 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001469
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301470 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001471 /* Only MMC1 can interface at 3V without some flavor
1472 * of external transceiver; but they all handle 1.8V.
1473 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001474 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Rajendra Nayak1f84b712012-03-12 20:32:38 +05301475 (ios->vdd == DUAL_VOLT_OCR_BIT) &&
1476 /*
1477 * With pbias cell programming missing, this
1478 * can't be allowed when booting with device
1479 * tree.
1480 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +05301481 !host->dev->of_node) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001482 /*
1483 * The mmc_select_voltage fn of the core does
1484 * not seem to set the power_mode to
1485 * MMC_POWER_UP upon recalculating the voltage.
1486 * vdd 1.8v.
1487 */
Denis Karpov70a33412009-09-22 16:44:59 -07001488 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1489 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001490 "Switch operation failed\n");
1491 }
1492 }
1493
Andy Shevchenko5934df22011-05-06 12:14:06 +03001494 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001495
Adrian Huntera3621462009-09-22 16:44:42 -07001496 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001497 send_init_stream(host);
1498
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001499 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001500
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301501 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001502}
1503
1504static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1505{
Denis Karpov70a33412009-09-22 16:44:59 -07001506 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001507
Denis Karpov191d1f12009-09-22 16:44:55 -07001508 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001509 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001510 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001511}
1512
1513static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1514{
Denis Karpov70a33412009-09-22 16:44:59 -07001515 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001516
Denis Karpov191d1f12009-09-22 16:44:55 -07001517 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001518 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001519 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001520}
1521
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001522static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1523{
1524 struct omap_hsmmc_host *host = mmc_priv(mmc);
1525
1526 if (mmc_slot(host).init_card)
1527 mmc_slot(host).init_card(card);
1528}
1529
Denis Karpov70a33412009-09-22 16:44:59 -07001530static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001531{
1532 u32 hctl, capa, value;
1533
1534 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301535 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001536 hctl = SDVS30;
1537 capa = VS30 | VS18;
1538 } else {
1539 hctl = SDVS18;
1540 capa = VS18;
1541 }
1542
1543 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1544 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1545
1546 value = OMAP_HSMMC_READ(host->base, CAPA);
1547 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1548
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001549 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001550 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001551}
1552
Denis Karpov70a33412009-09-22 16:44:59 -07001553static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001554{
Denis Karpov70a33412009-09-22 16:44:59 -07001555 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001556
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301557 pm_runtime_get_sync(host->dev);
1558
Denis Karpovdd498ef2009-09-22 16:44:49 -07001559 return 0;
1560}
1561
Adrian Hunter907d2e72012-02-29 09:17:21 +02001562static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001563{
Denis Karpov70a33412009-09-22 16:44:59 -07001564 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001565
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301566 pm_runtime_mark_last_busy(host->dev);
1567 pm_runtime_put_autosuspend(host->dev);
1568
Denis Karpovdd498ef2009-09-22 16:44:49 -07001569 return 0;
1570}
1571
Denis Karpov70a33412009-09-22 16:44:59 -07001572static const struct mmc_host_ops omap_hsmmc_ops = {
1573 .enable = omap_hsmmc_enable_fclk,
1574 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001575 .post_req = omap_hsmmc_post_req,
1576 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001577 .request = omap_hsmmc_request,
1578 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001579 .get_cd = omap_hsmmc_get_cd,
1580 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001581 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001582 /* NYET -- enable_sdio_irq */
1583};
1584
Denis Karpovd900f712009-09-22 16:44:38 -07001585#ifdef CONFIG_DEBUG_FS
1586
Denis Karpov70a33412009-09-22 16:44:59 -07001587static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001588{
1589 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001590 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001591 int context_loss = 0;
1592
Denis Karpov70a33412009-09-22 16:44:59 -07001593 if (host->pdata->get_context_loss_count)
1594 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001595
Adrian Hunter907d2e72012-02-29 09:17:21 +02001596 seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1597 mmc->index, host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001598
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301599 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001600 seq_printf(s, "host suspended, can't read registers\n");
1601 return 0;
1602 }
1603
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301604 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001605
Denis Karpovd900f712009-09-22 16:44:38 -07001606 seq_printf(s, "CON:\t\t0x%08x\n",
1607 OMAP_HSMMC_READ(host->base, CON));
1608 seq_printf(s, "HCTL:\t\t0x%08x\n",
1609 OMAP_HSMMC_READ(host->base, HCTL));
1610 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1611 OMAP_HSMMC_READ(host->base, SYSCTL));
1612 seq_printf(s, "IE:\t\t0x%08x\n",
1613 OMAP_HSMMC_READ(host->base, IE));
1614 seq_printf(s, "ISE:\t\t0x%08x\n",
1615 OMAP_HSMMC_READ(host->base, ISE));
1616 seq_printf(s, "CAPA:\t\t0x%08x\n",
1617 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001618
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301619 pm_runtime_mark_last_busy(host->dev);
1620 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001621
Denis Karpovd900f712009-09-22 16:44:38 -07001622 return 0;
1623}
1624
Denis Karpov70a33412009-09-22 16:44:59 -07001625static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001626{
Denis Karpov70a33412009-09-22 16:44:59 -07001627 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001628}
1629
1630static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001631 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001632 .read = seq_read,
1633 .llseek = seq_lseek,
1634 .release = single_release,
1635};
1636
Denis Karpov70a33412009-09-22 16:44:59 -07001637static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001638{
1639 if (mmc->debugfs_root)
1640 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1641 mmc, &mmc_regs_fops);
1642}
1643
1644#else
1645
Denis Karpov70a33412009-09-22 16:44:59 -07001646static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001647{
1648}
1649
1650#endif
1651
Rajendra Nayak46856a62012-03-12 20:32:37 +05301652#ifdef CONFIG_OF
1653static u16 omap4_reg_offset = 0x100;
1654
1655static const struct of_device_id omap_mmc_of_match[] = {
1656 {
1657 .compatible = "ti,omap2-hsmmc",
1658 },
1659 {
1660 .compatible = "ti,omap3-hsmmc",
1661 },
1662 {
1663 .compatible = "ti,omap4-hsmmc",
1664 .data = &omap4_reg_offset,
1665 },
1666 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001667};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301668MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1669
1670static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1671{
1672 struct omap_mmc_platform_data *pdata;
1673 struct device_node *np = dev->of_node;
1674 u32 bus_width;
1675
1676 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1677 if (!pdata)
1678 return NULL; /* out of memory */
1679
1680 if (of_find_property(np, "ti,dual-volt", NULL))
1681 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1682
1683 /* This driver only supports 1 slot */
1684 pdata->nr_slots = 1;
1685 pdata->slots[0].switch_pin = of_get_named_gpio(np, "cd-gpios", 0);
1686 pdata->slots[0].gpio_wp = of_get_named_gpio(np, "wp-gpios", 0);
1687
1688 if (of_find_property(np, "ti,non-removable", NULL)) {
1689 pdata->slots[0].nonremovable = true;
1690 pdata->slots[0].no_regulator_off_init = true;
1691 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001692 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301693 if (bus_width == 4)
1694 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1695 else if (bus_width == 8)
1696 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1697
1698 if (of_find_property(np, "ti,needs-special-reset", NULL))
1699 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1700
1701 return pdata;
1702}
1703#else
1704static inline struct omap_mmc_platform_data
1705 *of_get_hsmmc_pdata(struct device *dev)
1706{
1707 return NULL;
1708}
1709#endif
1710
Felipe Balbiefa25fd2012-03-14 11:18:28 +02001711static int __devinit omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001712{
1713 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1714 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001715 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001716 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001717 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301718 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001719 dma_cap_mask_t mask;
1720 unsigned tx_req, rx_req;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301721
1722 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1723 if (match) {
1724 pdata = of_get_hsmmc_pdata(&pdev->dev);
1725 if (match->data) {
Uwe Kleine-Königefc9b732012-05-21 21:57:39 +02001726 const u16 *offsetp = match->data;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301727 pdata->reg_offset = *offsetp;
1728 }
1729 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001730
1731 if (pdata == NULL) {
1732 dev_err(&pdev->dev, "Platform Data is missing\n");
1733 return -ENXIO;
1734 }
1735
1736 if (pdata->nr_slots == 0) {
1737 dev_err(&pdev->dev, "No Slots\n");
1738 return -ENXIO;
1739 }
1740
1741 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1742 irq = platform_get_irq(pdev, 0);
1743 if (res == NULL || irq < 0)
1744 return -ENXIO;
1745
Chris Ball984b2032011-03-22 16:34:42 -07001746 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001747 if (res == NULL)
1748 return -EBUSY;
1749
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001750 ret = omap_hsmmc_gpio_init(pdata);
1751 if (ret)
1752 goto err;
1753
Denis Karpov70a33412009-09-22 16:44:59 -07001754 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001755 if (!mmc) {
1756 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001757 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001758 }
1759
1760 host = mmc_priv(mmc);
1761 host->mmc = mmc;
1762 host->pdata = pdata;
1763 host->dev = &pdev->dev;
1764 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001765 host->dma_ch = -1;
1766 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001767 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05301768 host->mapbase = res->start + pdata->reg_offset;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001769 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001770 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001771 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001772
1773 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001774
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301775 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001776
Adrian Huntere0eb2422010-02-15 10:03:34 -08001777 /*
1778 * If regulator_disable can only put vcc_aux to sleep then there is
1779 * no off state.
1780 */
1781 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1782 mmc_slot(host).no_off = 1;
1783
Daniel Mackd418ed82012-02-19 13:20:33 +01001784 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1785
1786 if (pdata->max_freq > 0)
1787 mmc->f_max = pdata->max_freq;
1788 else
1789 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001790
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001791 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001792
Russell King6f7607c2009-01-28 10:22:50 +00001793 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001794 if (IS_ERR(host->fclk)) {
1795 ret = PTR_ERR(host->fclk);
1796 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001797 goto err1;
1798 }
1799
Paul Walmsley9b682562011-10-06 14:50:35 -06001800 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1801 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1802 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1803 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001804
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301805 pm_runtime_enable(host->dev);
1806 pm_runtime_get_sync(host->dev);
1807 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1808 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001809
Balaji T K92a3aeb2012-02-24 21:14:34 +05301810 omap_hsmmc_context_save(host);
1811
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301812 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1813 /*
1814 * MMC can still work without debounce clock.
1815 */
1816 if (IS_ERR(host->dbclk)) {
1817 dev_warn(mmc_dev(host->mmc), "Failed to get debounce clk\n");
1818 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05301819 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301820 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
1821 clk_put(host->dbclk);
1822 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07001823 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001824
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001825 /* Since we do only SG emulation, we can have as many segs
1826 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001827 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001828
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001829 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1830 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1831 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1832 mmc->max_seg_size = mmc->max_req_size;
1833
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001834 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001835 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001836
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001837 mmc->caps |= mmc_slot(host).caps;
1838 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001839 mmc->caps |= MMC_CAP_4_BIT_DATA;
1840
Denis Karpov191d1f12009-09-22 16:44:55 -07001841 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001842 mmc->caps |= MMC_CAP_NONREMOVABLE;
1843
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001844 mmc->pm_caps = mmc_slot(host).pm_caps;
1845
Denis Karpov70a33412009-09-22 16:44:59 -07001846 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001847
Balaji T Kb7bf7732012-03-07 09:55:30 -05001848 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1849 if (!res) {
1850 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
Kevin Hilman9c17d082012-07-10 16:40:56 -07001851 ret = -ENXIO;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001852 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001853 }
Russell King26b88522012-04-13 12:27:37 +01001854 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001855
1856 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1857 if (!res) {
1858 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
Kevin Hilman9c17d082012-07-10 16:40:56 -07001859 ret = -ENXIO;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001860 goto err_irq;
1861 }
Russell King26b88522012-04-13 12:27:37 +01001862 rx_req = res->start;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001863
Russell King26b88522012-04-13 12:27:37 +01001864 dma_cap_zero(mask);
1865 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01001866
Russell King26b88522012-04-13 12:27:37 +01001867 host->rx_chan = dma_request_channel(mask, omap_dma_filter_fn, &rx_req);
1868 if (!host->rx_chan) {
1869 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001870 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001871 goto err_irq;
1872 }
1873
1874 host->tx_chan = dma_request_channel(mask, omap_dma_filter_fn, &tx_req);
1875 if (!host->tx_chan) {
1876 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001877 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001878 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01001879 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001880
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001881 /* Request IRQ for MMC operations */
Yong Zhangd9618e92011-09-22 16:59:04 +08001882 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001883 mmc_hostname(mmc), host);
1884 if (ret) {
1885 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1886 goto err_irq;
1887 }
1888
1889 if (pdata->init != NULL) {
1890 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07001891 dev_dbg(mmc_dev(host->mmc),
1892 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001893 goto err_irq_cd_init;
1894 }
1895 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001896
Adrian Hunterb702b102010-02-15 10:03:35 -08001897 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001898 ret = omap_hsmmc_reg_get(host);
1899 if (ret)
1900 goto err_reg;
1901 host->use_reg = 1;
1902 }
1903
David Brownellb583f262009-05-28 14:04:03 -07001904 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001905
1906 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001907 if ((mmc_slot(host).card_detect_irq)) {
NeilBrown7efab4f2011-12-30 12:35:13 +11001908 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1909 NULL,
1910 omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08001911 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11001912 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001913 if (ret) {
1914 dev_dbg(mmc_dev(host->mmc),
1915 "Unable to grab MMC CD IRQ\n");
1916 goto err_irq_cd;
1917 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00001918 pdata->suspend = omap_hsmmc_suspend_cdirq;
1919 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001920 }
1921
Adrian Hunterb4175772010-05-26 14:42:06 -07001922 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001923
Adrian Hunterb62f6222009-09-22 16:45:01 -07001924 omap_hsmmc_protect_card(host);
1925
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001926 mmc_add_host(mmc);
1927
Denis Karpov191d1f12009-09-22 16:44:55 -07001928 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001929 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1930 if (ret < 0)
1931 goto err_slot_name;
1932 }
Denis Karpov191d1f12009-09-22 16:44:55 -07001933 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001934 ret = device_create_file(&mmc->class_dev,
1935 &dev_attr_cover_switch);
1936 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001937 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001938 }
1939
Denis Karpov70a33412009-09-22 16:44:59 -07001940 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301941 pm_runtime_mark_last_busy(host->dev);
1942 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001943
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001944 return 0;
1945
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001946err_slot_name:
1947 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001948 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001949err_irq_cd:
1950 if (host->use_reg)
1951 omap_hsmmc_reg_put(host);
1952err_reg:
1953 if (host->pdata->cleanup)
1954 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001955err_irq_cd_init:
1956 free_irq(host->irq, host);
1957err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01001958 if (host->tx_chan)
1959 dma_release_channel(host->tx_chan);
1960 if (host->rx_chan)
1961 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05301962 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05001963 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001964 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301965 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05301966 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001967 clk_put(host->dbclk);
1968 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001969err1:
1970 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001971 platform_set_drvdata(pdev, NULL);
1972 mmc_free_host(mmc);
1973err_alloc:
1974 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001975err:
Russell King48b332f2012-04-18 11:11:57 +01001976 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1977 if (res)
1978 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001979 return ret;
1980}
1981
Felipe Balbiefa25fd2012-03-14 11:18:28 +02001982static int __devexit omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001983{
Denis Karpov70a33412009-09-22 16:44:59 -07001984 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001985 struct resource *res;
1986
Felipe Balbi927ce942012-03-14 11:18:27 +02001987 pm_runtime_get_sync(host->dev);
1988 mmc_remove_host(host->mmc);
1989 if (host->use_reg)
1990 omap_hsmmc_reg_put(host);
1991 if (host->pdata->cleanup)
1992 host->pdata->cleanup(&pdev->dev);
1993 free_irq(host->irq, host);
1994 if (mmc_slot(host).card_detect_irq)
1995 free_irq(mmc_slot(host).card_detect_irq, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001996
Russell Kingc5c98922012-04-13 12:14:39 +01001997 if (host->tx_chan)
1998 dma_release_channel(host->tx_chan);
1999 if (host->rx_chan)
2000 dma_release_channel(host->rx_chan);
2001
Felipe Balbi927ce942012-03-14 11:18:27 +02002002 pm_runtime_put_sync(host->dev);
2003 pm_runtime_disable(host->dev);
2004 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302005 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302006 clk_disable_unprepare(host->dbclk);
Felipe Balbi927ce942012-03-14 11:18:27 +02002007 clk_put(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002008 }
2009
Felipe Balbi927ce942012-03-14 11:18:27 +02002010 mmc_free_host(host->mmc);
2011 iounmap(host->base);
2012 omap_hsmmc_gpio_free(pdev->dev.platform_data);
2013
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002014 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2015 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002016 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002017 platform_set_drvdata(pdev, NULL);
2018
2019 return 0;
2020}
2021
2022#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002023static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002024{
2025 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002026 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2027
2028 if (!host)
2029 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002030
2031 if (host && host->suspended)
2032 return 0;
2033
Felipe Balbi927ce942012-03-14 11:18:27 +02002034 pm_runtime_get_sync(host->dev);
2035 host->suspended = 1;
2036 if (host->pdata->suspend) {
2037 ret = host->pdata->suspend(dev, host->slot_id);
Eliad Peller31f9d462011-11-22 16:02:17 +02002038 if (ret) {
Felipe Balbi927ce942012-03-14 11:18:27 +02002039 dev_dbg(dev, "Unable to handle MMC board"
2040 " level suspend\n");
Adrian Huntera6b22402009-09-22 16:44:45 -07002041 host->suspended = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002042 return ret;
Adrian Huntera6b22402009-09-22 16:44:45 -07002043 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002044 }
Felipe Balbi927ce942012-03-14 11:18:27 +02002045 ret = mmc_suspend_host(host->mmc);
2046
2047 if (ret) {
2048 host->suspended = 0;
2049 if (host->pdata->resume) {
Vaibhav Bediac4c8eeb2012-09-13 06:31:03 +00002050 if (host->pdata->resume(dev, host->slot_id))
Felipe Balbi927ce942012-03-14 11:18:27 +02002051 dev_dbg(dev, "Unmask interrupt failed\n");
2052 }
2053 goto err;
2054 }
2055
2056 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2057 omap_hsmmc_disable_irq(host);
2058 OMAP_HSMMC_WRITE(host->base, HCTL,
2059 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2060 }
2061
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302062 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302063 clk_disable_unprepare(host->dbclk);
Eliad Peller31f9d462011-11-22 16:02:17 +02002064err:
2065 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002066 return ret;
2067}
2068
2069/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002070static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002071{
2072 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002073 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2074
2075 if (!host)
2076 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002077
2078 if (host && !host->suspended)
2079 return 0;
2080
Felipe Balbi927ce942012-03-14 11:18:27 +02002081 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002082
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302083 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302084 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002085
Felipe Balbi927ce942012-03-14 11:18:27 +02002086 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2087 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002088
Felipe Balbi927ce942012-03-14 11:18:27 +02002089 if (host->pdata->resume) {
2090 ret = host->pdata->resume(dev, host->slot_id);
2091 if (ret)
2092 dev_dbg(dev, "Unmask interrupt failed\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002093 }
2094
Felipe Balbi927ce942012-03-14 11:18:27 +02002095 omap_hsmmc_protect_card(host);
2096
2097 /* Notify the core to resume the host */
2098 ret = mmc_resume_host(host->mmc);
2099 if (ret == 0)
2100 host->suspended = 0;
2101
2102 pm_runtime_mark_last_busy(host->dev);
2103 pm_runtime_put_autosuspend(host->dev);
2104
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002105 return ret;
2106
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002107}
2108
2109#else
Denis Karpov70a33412009-09-22 16:44:59 -07002110#define omap_hsmmc_suspend NULL
2111#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002112#endif
2113
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302114static int omap_hsmmc_runtime_suspend(struct device *dev)
2115{
2116 struct omap_hsmmc_host *host;
2117
2118 host = platform_get_drvdata(to_platform_device(dev));
2119 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002120 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302121
2122 return 0;
2123}
2124
2125static int omap_hsmmc_runtime_resume(struct device *dev)
2126{
2127 struct omap_hsmmc_host *host;
2128
2129 host = platform_get_drvdata(to_platform_device(dev));
2130 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002131 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302132
2133 return 0;
2134}
2135
Kevin Hilmana791daa2010-05-26 14:42:07 -07002136static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002137 .suspend = omap_hsmmc_suspend,
2138 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302139 .runtime_suspend = omap_hsmmc_runtime_suspend,
2140 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002141};
2142
2143static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002144 .probe = omap_hsmmc_probe,
2145 .remove = __devexit_p(omap_hsmmc_remove),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002146 .driver = {
2147 .name = DRIVER_NAME,
2148 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002149 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302150 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002151 },
2152};
2153
Felipe Balbib7964502012-03-14 11:18:32 +02002154module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002155MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2156MODULE_LICENSE("GPL");
2157MODULE_ALIAS("platform:" DRIVER_NAME);
2158MODULE_AUTHOR("Texas Instruments Inc");