blob: 6e44025acf01fcf9669567dff93b8c3d712b2519 [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Andy Shevchenkoac330f42011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
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>
Daniel Mack46b76032012-10-15 21:35:05 +053040#include <linux/pinctrl/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053041#include <linux/pm_runtime.h>
Tony Lindgren68f39e72012-10-15 12:09:43 -070042#include <linux/platform_data/mmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010043
44/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070045#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010046#define OMAP_HSMMC_CON 0x002C
47#define OMAP_HSMMC_BLK 0x0104
48#define OMAP_HSMMC_ARG 0x0108
49#define OMAP_HSMMC_CMD 0x010C
50#define OMAP_HSMMC_RSP10 0x0110
51#define OMAP_HSMMC_RSP32 0x0114
52#define OMAP_HSMMC_RSP54 0x0118
53#define OMAP_HSMMC_RSP76 0x011C
54#define OMAP_HSMMC_DATA 0x0120
55#define OMAP_HSMMC_HCTL 0x0128
56#define OMAP_HSMMC_SYSCTL 0x012C
57#define OMAP_HSMMC_STAT 0x0130
58#define OMAP_HSMMC_IE 0x0134
59#define OMAP_HSMMC_ISE 0x0138
60#define OMAP_HSMMC_CAPA 0x0140
61
62#define VS18 (1 << 26)
63#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053064#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010065#define SDVS18 (0x5 << 9)
66#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080067#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010068#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010069#define SDVSCLR 0xFFFFF1FF
70#define SDVSDET 0x00000400
71#define AUTOIDLE 0x1
72#define SDBP (1 << 8)
73#define DTO 0xe
74#define ICE 0x1
75#define ICS 0x2
76#define CEN (1 << 2)
77#define CLKD_MASK 0x0000FFC0
78#define CLKD_SHIFT 6
79#define DTO_MASK 0x000F0000
80#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010081#define INIT_STREAM (1 << 1)
82#define DP_SELECT (1 << 21)
83#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053084#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010085#define MSBS (1 << 5)
86#define BCE (1 << 1)
87#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053088#define HSPE (1 << 2)
Balaji T K03b5d922012-04-09 12:08:33 +053089#define DDR (1 << 19)
Jarkko Lavinen73153012008-11-21 16:49:54 +020090#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010091#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010092#define STAT_CLEAR 0xFFFFFFFF
93#define INIT_STREAM_CMD 0x00000000
94#define DUAL_VOLT_OCR_BIT 7
95#define SRC (1 << 25)
96#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -070097#define SOFTRESET (1 << 1)
98#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010099
Venkatraman Sa7e96872012-11-19 22:00:01 +0530100/* Interrupt masks for IE and ISE register */
101#define CC_EN (1 << 0)
102#define TC_EN (1 << 1)
103#define BWR_EN (1 << 4)
104#define BRR_EN (1 << 5)
105#define ERR_EN (1 << 15)
106#define CTO_EN (1 << 16)
107#define CCRC_EN (1 << 17)
108#define CEB_EN (1 << 18)
109#define CIE_EN (1 << 19)
110#define DTO_EN (1 << 20)
111#define DCRC_EN (1 << 21)
112#define DEB_EN (1 << 22)
113#define CERR_EN (1 << 28)
114#define BADA_EN (1 << 29)
115
116#define INT_EN_MASK (BADA_EN | CERR_EN | DEB_EN | DCRC_EN |\
117 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
118 BRR_EN | BWR_EN | TC_EN | CC_EN)
119
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530120#define MMC_AUTOSUSPEND_DELAY 100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100121#define MMC_TIMEOUT_MS 20
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400122#define OMAP_MMC_MIN_CLOCK 400000
123#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530124#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100125
126/*
127 * One controller can have multiple slots, like on some omap boards using
128 * omap.c controller driver. Luckily this is not currently done on any known
129 * omap_hsmmc.c device.
130 */
131#define mmc_slot(host) (host->pdata->slots[host->slot_id])
132
133/*
134 * MMC Host controller read/write API's
135 */
136#define OMAP_HSMMC_READ(base, reg) \
137 __raw_readl((base) + OMAP_HSMMC_##reg)
138
139#define OMAP_HSMMC_WRITE(base, reg, val) \
140 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
141
Per Forlin9782aff2011-07-01 18:55:23 +0200142struct omap_hsmmc_next {
143 unsigned int dma_len;
144 s32 cookie;
145};
146
Denis Karpov70a33412009-09-22 16:44:59 -0700147struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100148 struct device *dev;
149 struct mmc_host *mmc;
150 struct mmc_request *mrq;
151 struct mmc_command *cmd;
152 struct mmc_data *data;
153 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100154 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800155 /*
156 * vcc == configured supply
157 * vcc_aux == optional
158 * - MMC1, supply for DAT4..DAT7
159 * - MMC2/MMC2, external level shifter voltage supply, for
160 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
161 */
162 struct regulator *vcc;
163 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100164 void __iomem *base;
165 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700166 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100167 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200168 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100169 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700170 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100171 int suspended;
172 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100173 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100174 struct dma_chan *tx_chan;
175 struct dma_chan *rx_chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100176 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200177 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700178 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700179 int protect_card;
180 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800181 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700182 int req_in_progress;
Per Forlin9782aff2011-07-01 18:55:23 +0200183 struct omap_hsmmc_next next_data;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700184
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100185 struct omap_mmc_platform_data *pdata;
186};
187
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800188static int omap_hsmmc_card_detect(struct device *dev, int slot)
189{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530190 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
191 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800192
193 /* NOTE: assumes card detect signal is active-low */
194 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
195}
196
197static int omap_hsmmc_get_wp(struct device *dev, int slot)
198{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530199 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
200 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800201
202 /* NOTE: assumes write protect signal is active-high */
203 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
204}
205
206static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
207{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530208 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
209 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800210
211 /* NOTE: assumes card detect signal is active-low */
212 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
213}
214
215#ifdef CONFIG_PM
216
217static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
218{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530219 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
220 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800221
222 disable_irq(mmc->slots[0].card_detect_irq);
223 return 0;
224}
225
226static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
227{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530228 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
229 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800230
231 enable_irq(mmc->slots[0].card_detect_irq);
232 return 0;
233}
234
235#else
236
237#define omap_hsmmc_suspend_cdirq NULL
238#define omap_hsmmc_resume_cdirq NULL
239
240#endif
241
Adrian Hunterb702b102010-02-15 10:03:35 -0800242#ifdef CONFIG_REGULATOR
243
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500244static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800245 int vdd)
246{
247 struct omap_hsmmc_host *host =
248 platform_get_drvdata(to_platform_device(dev));
249 int ret = 0;
250
251 /*
252 * If we don't see a Vcc regulator, assume it's a fixed
253 * voltage always-on regulator.
254 */
255 if (!host->vcc)
256 return 0;
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530257 /*
258 * With DT, never turn OFF the regulator. This is because
259 * the pbias cell programming support is still missing when
260 * booting with Device tree
261 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +0530262 if (dev->of_node && !vdd)
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530263 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800264
265 if (mmc_slot(host).before_set_reg)
266 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
267
268 /*
269 * Assume Vcc regulator is used only to power the card ... OMAP
270 * VDDS is used to power the pins, optionally with a transceiver to
271 * support cards using voltages other than VDDS (1.8V nominal). When a
272 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
273 *
274 * In some cases this regulator won't support enable/disable;
275 * e.g. it's a fixed rail for a WLAN chip.
276 *
277 * In other cases vcc_aux switches interface power. Example, for
278 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
279 * chips/cards need an interface voltage rail too.
280 */
281 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400282 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800283 /* Enable interface voltage rail, if needed */
284 if (ret == 0 && host->vcc_aux) {
285 ret = regulator_enable(host->vcc_aux);
286 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400287 ret = mmc_regulator_set_ocr(host->mmc,
288 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800289 }
290 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400291 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800292 if (host->vcc_aux)
293 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400294 if (!ret) {
295 /* Then proceed to shut down the local regulator */
296 ret = mmc_regulator_set_ocr(host->mmc,
297 host->vcc, 0);
298 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800299 }
300
301 if (mmc_slot(host).after_set_reg)
302 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
303
304 return ret;
305}
306
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800307static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
308{
309 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700310 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800311
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800312 reg = regulator_get(host->dev, "vmmc");
313 if (IS_ERR(reg)) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +0530314 dev_err(host->dev, "vmmc regulator missing\n");
NeilBrown1fdc90f2012-08-08 00:06:00 -0400315 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800316 } else {
NeilBrown1fdc90f2012-08-08 00:06:00 -0400317 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800318 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700319 ocr_value = mmc_regulator_get_ocrmask(reg);
320 if (!mmc_slot(host).ocr_mask) {
321 mmc_slot(host).ocr_mask = ocr_value;
322 } else {
323 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530324 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500325 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700326 mmc_slot(host).ocr_mask = 0;
327 return -EINVAL;
328 }
329 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800330
331 /* Allow an aux regulator */
332 reg = regulator_get(host->dev, "vmmc_aux");
333 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
334
Balaji T Kb1c1df72011-05-30 19:55:34 +0530335 /* For eMMC do not power off when not in sleep state */
336 if (mmc_slot(host).no_regulator_off_init)
337 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800338 /*
339 * UGLY HACK: workaround regulator framework bugs.
340 * When the bootloader leaves a supply active, it's
341 * initialized with zero usecount ... and we can't
342 * disable it without first enabling it. Until the
343 * framework is fixed, we need a workaround like this
344 * (which is safe for MMC, but not in general).
345 */
Adrian Huntere840ce12011-05-06 12:14:10 +0300346 if (regulator_is_enabled(host->vcc) > 0 ||
347 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
348 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
349
350 mmc_slot(host).set_power(host->dev, host->slot_id,
351 1, vdd);
352 mmc_slot(host).set_power(host->dev, host->slot_id,
353 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800354 }
355 }
356
357 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800358}
359
360static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
361{
362 regulator_put(host->vcc);
363 regulator_put(host->vcc_aux);
364 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800365}
366
Adrian Hunterb702b102010-02-15 10:03:35 -0800367static inline int omap_hsmmc_have_reg(void)
368{
369 return 1;
370}
371
372#else
373
374static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
375{
376 return -EINVAL;
377}
378
379static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
380{
381}
382
383static inline int omap_hsmmc_have_reg(void)
384{
385 return 0;
386}
387
388#endif
389
390static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
391{
392 int ret;
393
394 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800395 if (pdata->slots[0].cover)
396 pdata->slots[0].get_cover_state =
397 omap_hsmmc_get_cover_state;
398 else
399 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
400 pdata->slots[0].card_detect_irq =
401 gpio_to_irq(pdata->slots[0].switch_pin);
402 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
403 if (ret)
404 return ret;
405 ret = gpio_direction_input(pdata->slots[0].switch_pin);
406 if (ret)
407 goto err_free_sp;
408 } else
409 pdata->slots[0].switch_pin = -EINVAL;
410
411 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
412 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
413 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
414 if (ret)
415 goto err_free_cd;
416 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
417 if (ret)
418 goto err_free_wp;
419 } else
420 pdata->slots[0].gpio_wp = -EINVAL;
421
422 return 0;
423
424err_free_wp:
425 gpio_free(pdata->slots[0].gpio_wp);
426err_free_cd:
427 if (gpio_is_valid(pdata->slots[0].switch_pin))
428err_free_sp:
429 gpio_free(pdata->slots[0].switch_pin);
430 return ret;
431}
432
433static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
434{
435 if (gpio_is_valid(pdata->slots[0].gpio_wp))
436 gpio_free(pdata->slots[0].gpio_wp);
437 if (gpio_is_valid(pdata->slots[0].switch_pin))
438 gpio_free(pdata->slots[0].switch_pin);
439}
440
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100441/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300442 * Start clock to the card
443 */
444static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
445{
446 OMAP_HSMMC_WRITE(host->base, SYSCTL,
447 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
448}
449
450/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100451 * Stop clock to the card
452 */
Denis Karpov70a33412009-09-22 16:44:59 -0700453static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100454{
455 OMAP_HSMMC_WRITE(host->base, SYSCTL,
456 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
457 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900458 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100459}
460
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700461static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
462 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700463{
464 unsigned int irq_mask;
465
466 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530467 irq_mask = INT_EN_MASK & ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700468 else
469 irq_mask = INT_EN_MASK;
470
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700471 /* Disable timeout for erases */
472 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530473 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700474
Adrian Hunterb4175772010-05-26 14:42:06 -0700475 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
476 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
477 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
478}
479
480static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
481{
482 OMAP_HSMMC_WRITE(host->base, ISE, 0);
483 OMAP_HSMMC_WRITE(host->base, IE, 0);
484 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
485}
486
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300487/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530488static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300489{
490 u16 dsor = 0;
491
492 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530493 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300494 if (dsor > 250)
495 dsor = 250;
496 }
497
498 return dsor;
499}
500
Andy Shevchenko5934df22011-05-06 12:14:06 +0300501static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
502{
503 struct mmc_ios *ios = &host->mmc->ios;
504 unsigned long regval;
505 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530506 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300507
Venkatraman S8986d312012-08-07 19:10:38 +0530508 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300509
510 omap_hsmmc_stop_clock(host);
511
512 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
513 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530514 clkdiv = calc_divisor(host, ios);
515 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300516 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
517 OMAP_HSMMC_WRITE(host->base, SYSCTL,
518 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
519
520 /* Wait till the ICS bit is set */
521 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
522 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
523 && time_before(jiffies, timeout))
524 cpu_relax();
525
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530526 /*
527 * Enable High-Speed Support
528 * Pre-Requisites
529 * - Controller should support High-Speed-Enable Bit
530 * - Controller should not be using DDR Mode
531 * - Controller should advertise that it supports High Speed
532 * in capabilities register
533 * - MMC/SD clock coming out of controller > 25MHz
534 */
535 if ((mmc_slot(host).features & HSMMC_HAS_HSPE_SUPPORT) &&
536 (ios->timing != MMC_TIMING_UHS_DDR50) &&
537 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
538 regval = OMAP_HSMMC_READ(host->base, HCTL);
539 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
540 regval |= HSPE;
541 else
542 regval &= ~HSPE;
543
544 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
545 }
546
Andy Shevchenko5934df22011-05-06 12:14:06 +0300547 omap_hsmmc_start_clock(host);
548}
549
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400550static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
551{
552 struct mmc_ios *ios = &host->mmc->ios;
553 u32 con;
554
555 con = OMAP_HSMMC_READ(host->base, CON);
Balaji T K03b5d922012-04-09 12:08:33 +0530556 if (ios->timing == MMC_TIMING_UHS_DDR50)
557 con |= DDR; /* configure in DDR mode */
558 else
559 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400560 switch (ios->bus_width) {
561 case MMC_BUS_WIDTH_8:
562 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
563 break;
564 case MMC_BUS_WIDTH_4:
565 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
566 OMAP_HSMMC_WRITE(host->base, HCTL,
567 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
568 break;
569 case MMC_BUS_WIDTH_1:
570 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
571 OMAP_HSMMC_WRITE(host->base, HCTL,
572 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
573 break;
574 }
575}
576
577static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
578{
579 struct mmc_ios *ios = &host->mmc->ios;
580 u32 con;
581
582 con = OMAP_HSMMC_READ(host->base, CON);
583 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
584 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
585 else
586 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
587}
588
Denis Karpov11dd62a2009-09-22 16:44:43 -0700589#ifdef CONFIG_PM
590
591/*
592 * Restore the MMC host context, if it was lost as result of a
593 * power state change.
594 */
Denis Karpov70a33412009-09-22 16:44:59 -0700595static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700596{
597 struct mmc_ios *ios = &host->mmc->ios;
598 struct omap_mmc_platform_data *pdata = host->pdata;
599 int context_loss = 0;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400600 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700601 unsigned long timeout;
602
603 if (pdata->get_context_loss_count) {
604 context_loss = pdata->get_context_loss_count(host->dev);
605 if (context_loss < 0)
606 return 1;
607 }
608
609 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
610 context_loss == host->context_loss ? "not " : "");
611 if (host->context_loss == context_loss)
612 return 1;
613
Venkatraman S6c31b212012-08-08 14:26:52 +0530614 if (!OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE)
615 return 1;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700616
Balaji T Kc2200ef2012-03-07 09:55:30 -0500617 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700618 if (host->power_mode != MMC_POWER_OFF &&
619 (1 << ios->vdd) <= MMC_VDD_23_24)
620 hctl = SDVS18;
621 else
622 hctl = SDVS30;
623 capa = VS30 | VS18;
624 } else {
625 hctl = SDVS18;
626 capa = VS18;
627 }
628
629 OMAP_HSMMC_WRITE(host->base, HCTL,
630 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
631
632 OMAP_HSMMC_WRITE(host->base, CAPA,
633 OMAP_HSMMC_READ(host->base, CAPA) | capa);
634
635 OMAP_HSMMC_WRITE(host->base, HCTL,
636 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
637
638 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
639 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
640 && time_before(jiffies, timeout))
641 ;
642
Adrian Hunterb4175772010-05-26 14:42:06 -0700643 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700644
645 /* Do not initialize card-specific things if the power is off */
646 if (host->power_mode == MMC_POWER_OFF)
647 goto out;
648
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400649 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700650
Andy Shevchenko5934df22011-05-06 12:14:06 +0300651 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700652
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400653 omap_hsmmc_set_bus_mode(host);
654
Denis Karpov11dd62a2009-09-22 16:44:43 -0700655out:
656 host->context_loss = context_loss;
657
658 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
659 return 0;
660}
661
662/*
663 * Save the MMC host context (store the number of power state changes so far).
664 */
Denis Karpov70a33412009-09-22 16:44:59 -0700665static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700666{
667 struct omap_mmc_platform_data *pdata = host->pdata;
668 int context_loss;
669
670 if (pdata->get_context_loss_count) {
671 context_loss = pdata->get_context_loss_count(host->dev);
672 if (context_loss < 0)
673 return;
674 host->context_loss = context_loss;
675 }
676}
677
678#else
679
Denis Karpov70a33412009-09-22 16:44:59 -0700680static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700681{
682 return 0;
683}
684
Denis Karpov70a33412009-09-22 16:44:59 -0700685static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700686{
687}
688
689#endif
690
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100691/*
692 * Send init stream sequence to card
693 * before sending IDLE command
694 */
Denis Karpov70a33412009-09-22 16:44:59 -0700695static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100696{
697 int reg = 0;
698 unsigned long timeout;
699
Adrian Hunterb62f6222009-09-22 16:45:01 -0700700 if (host->protect_card)
701 return;
702
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100703 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700704
705 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100706 OMAP_HSMMC_WRITE(host->base, CON,
707 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
708 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
709
710 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530711 while ((reg != CC_EN) && time_before(jiffies, timeout))
712 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100713
714 OMAP_HSMMC_WRITE(host->base, CON,
715 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700716
717 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
718 OMAP_HSMMC_READ(host->base, STAT);
719
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100720 enable_irq(host->irq);
721}
722
723static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700724int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100725{
726 int r = 1;
727
Denis Karpov191d1f12009-09-22 16:44:55 -0700728 if (mmc_slot(host).get_cover_state)
729 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100730 return r;
731}
732
733static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700734omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100735 char *buf)
736{
737 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700738 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100739
Denis Karpov70a33412009-09-22 16:44:59 -0700740 return sprintf(buf, "%s\n",
741 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100742}
743
Denis Karpov70a33412009-09-22 16:44:59 -0700744static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100745
746static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700747omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100748 char *buf)
749{
750 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700751 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100752
Denis Karpov191d1f12009-09-22 16:44:55 -0700753 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100754}
755
Denis Karpov70a33412009-09-22 16:44:59 -0700756static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100757
758/*
759 * Configure the response type and send the cmd.
760 */
761static void
Denis Karpov70a33412009-09-22 16:44:59 -0700762omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100763 struct mmc_data *data)
764{
765 int cmdreg = 0, resptype = 0, cmdtype = 0;
766
Venkatraman S8986d312012-08-07 19:10:38 +0530767 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100768 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
769 host->cmd = cmd;
770
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700771 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100772
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200773 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100774 if (cmd->flags & MMC_RSP_PRESENT) {
775 if (cmd->flags & MMC_RSP_136)
776 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200777 else if (cmd->flags & MMC_RSP_BUSY) {
778 resptype = 3;
779 host->response_busy = 1;
780 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100781 resptype = 2;
782 }
783
784 /*
785 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
786 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
787 * a val of 0x3, rest 0x0.
788 */
789 if (cmd == host->mrq->stop)
790 cmdtype = 0x3;
791
792 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
793
794 if (data) {
795 cmdreg |= DP_SELECT | MSBS | BCE;
796 if (data->flags & MMC_DATA_READ)
797 cmdreg |= DDIR;
798 else
799 cmdreg &= ~(DDIR);
800 }
801
802 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530803 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100804
Adrian Hunterb4175772010-05-26 14:42:06 -0700805 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700806
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100807 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
808 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
809}
810
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200811static int
Denis Karpov70a33412009-09-22 16:44:59 -0700812omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200813{
814 if (data->flags & MMC_DATA_WRITE)
815 return DMA_TO_DEVICE;
816 else
817 return DMA_FROM_DEVICE;
818}
819
Russell Kingc5c98922012-04-13 12:14:39 +0100820static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
821 struct mmc_data *data)
822{
823 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
824}
825
Adrian Hunterb4175772010-05-26 14:42:06 -0700826static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
827{
828 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530829 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700830
Venkatraman S31463b12012-04-09 12:08:34 +0530831 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700832 host->req_in_progress = 0;
833 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530834 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700835
836 omap_hsmmc_disable_irq(host);
837 /* Do not complete the request if DMA is still in progress */
838 if (mrq->data && host->use_dma && dma_ch != -1)
839 return;
840 host->mrq = NULL;
841 mmc_request_done(host->mmc, mrq);
842}
843
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100844/*
845 * Notify the transfer complete to MMC core
846 */
847static void
Denis Karpov70a33412009-09-22 16:44:59 -0700848omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100849{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200850 if (!data) {
851 struct mmc_request *mrq = host->mrq;
852
Adrian Hunter23050102009-09-22 16:44:57 -0700853 /* TC before CC from CMD6 - don't know why, but it happens */
854 if (host->cmd && host->cmd->opcode == 6 &&
855 host->response_busy) {
856 host->response_busy = 0;
857 return;
858 }
859
Adrian Hunterb4175772010-05-26 14:42:06 -0700860 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200861 return;
862 }
863
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100864 host->data = NULL;
865
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100866 if (!data->error)
867 data->bytes_xfered += data->blocks * (data->blksz);
868 else
869 data->bytes_xfered = 0;
870
Ming Leife852272012-06-22 18:49:35 +0800871 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700872 omap_hsmmc_request_done(host, data->mrq);
Ming Leife852272012-06-22 18:49:35 +0800873 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100874 }
Ming Leife852272012-06-22 18:49:35 +0800875 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100876}
877
878/*
879 * Notify the core about command completion
880 */
881static void
Denis Karpov70a33412009-09-22 16:44:59 -0700882omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100883{
884 host->cmd = NULL;
885
886 if (cmd->flags & MMC_RSP_PRESENT) {
887 if (cmd->flags & MMC_RSP_136) {
888 /* response type 2 */
889 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
890 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
891 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
892 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
893 } else {
894 /* response types 1, 1b, 3, 4, 5, 6 */
895 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
896 }
897 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700898 if ((host->data == NULL && !host->response_busy) || cmd->error)
899 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100900}
901
902/*
903 * DMA clean up for command errors
904 */
Denis Karpov70a33412009-09-22 16:44:59 -0700905static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100906{
Adrian Hunterb4175772010-05-26 14:42:06 -0700907 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530908 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700909
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200910 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100911
Venkatraman S31463b12012-04-09 12:08:34 +0530912 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700913 dma_ch = host->dma_ch;
914 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530915 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700916
917 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100918 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
919
920 dmaengine_terminate_all(chan);
921 dma_unmap_sg(chan->device->dev,
922 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700923 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100924
Per Forlin053bf342011-11-07 21:55:11 +0530925 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100926 }
927 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100928}
929
930/*
931 * Readable error output
932 */
933#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300934static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100935{
936 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700937 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300938 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
939 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
940 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
941 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100942 };
943 char res[256];
944 char *buf = res;
945 int len, i;
946
947 len = sprintf(buf, "MMC IRQ 0x%x :", status);
948 buf += len;
949
Denis Karpov70a33412009-09-22 16:44:59 -0700950 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100951 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700952 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100953 buf += len;
954 }
955
Venkatraman S8986d312012-08-07 19:10:38 +0530956 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100957}
Adrian Hunter699b9582011-05-06 12:14:01 +0300958#else
959static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
960 u32 status)
961{
962}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100963#endif /* CONFIG_MMC_DEBUG */
964
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100965/*
966 * MMC controller internal state machines reset
967 *
968 * Used to reset command or data internal state machines, using respectively
969 * SRC or SRD bit of SYSCTL register
970 * Can be called from interrupt context
971 */
Denis Karpov70a33412009-09-22 16:44:59 -0700972static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
973 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100974{
975 unsigned long i = 0;
976 unsigned long limit = (loops_per_jiffy *
977 msecs_to_jiffies(MMC_TIMEOUT_MS));
978
979 OMAP_HSMMC_WRITE(host->base, SYSCTL,
980 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
981
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700982 /*
983 * OMAP4 ES2 and greater has an updated reset logic.
984 * Monitor a 0->1 transition first
985 */
986 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -0500987 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700988 && (i++ < limit))
989 cpu_relax();
990 }
991 i = 0;
992
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100993 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
994 (i++ < limit))
995 cpu_relax();
996
997 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
998 dev_err(mmc_dev(host->mmc),
999 "Timeout waiting on controller reset in %s\n",
1000 __func__);
1001}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001002
Balaji T K25e18972012-11-19 21:59:55 +05301003static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1004 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301005{
Balaji T K25e18972012-11-19 21:59:55 +05301006 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301007 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301008 if (host->cmd)
1009 host->cmd->error = err;
1010 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301011
1012 if (host->data) {
1013 omap_hsmmc_reset_controller_fsm(host, SRD);
1014 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301015 } else if (host->mrq && host->mrq->cmd)
1016 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301017}
1018
Adrian Hunterb4175772010-05-26 14:42:06 -07001019static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001020{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001021 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001022 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001023
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001024 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301025 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001026
Venkatraman Sa7e96872012-11-19 22:00:01 +05301027 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001028 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001029
Venkatraman Sa7e96872012-11-19 22:00:01 +05301030 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301031 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301032 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301033 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301034 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301035 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1036
Venkatraman Sae4bf782012-08-09 20:36:07 +05301037 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301038 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301039 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001040 }
1041 }
1042
Venkatraman Sa7e96872012-11-19 22:00:01 +05301043 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001044 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301045 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001046 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001047}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001048
Adrian Hunterb4175772010-05-26 14:42:06 -07001049/*
1050 * MMC controller IRQ handler
1051 */
1052static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1053{
1054 struct omap_hsmmc_host *host = dev_id;
1055 int status;
1056
1057 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301058 while (status & INT_EN_MASK && host->req_in_progress) {
Adrian Hunterb4175772010-05-26 14:42:06 -07001059 omap_hsmmc_do_irq(host, status);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301060
Adrian Hunterb4175772010-05-26 14:42:06 -07001061 /* Flush posted write */
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301062 OMAP_HSMMC_WRITE(host->base, STAT, status);
Adrian Hunterb4175772010-05-26 14:42:06 -07001063 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301064 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001065
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001066 return IRQ_HANDLED;
1067}
1068
Denis Karpov70a33412009-09-22 16:44:59 -07001069static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001070{
1071 unsigned long i;
1072
1073 OMAP_HSMMC_WRITE(host->base, HCTL,
1074 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1075 for (i = 0; i < loops_per_jiffy; i++) {
1076 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1077 break;
1078 cpu_relax();
1079 }
1080}
1081
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001082/*
David Brownelleb250822009-02-17 14:49:01 -08001083 * Switch MMC interface voltage ... only relevant for MMC1.
1084 *
1085 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1086 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1087 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001088 */
Denis Karpov70a33412009-09-22 16:44:59 -07001089static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001090{
1091 u32 reg_val = 0;
1092 int ret;
1093
1094 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301095 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301096 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301097 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001098
1099 /* Turn the power off */
1100 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001101
1102 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001103 if (!ret)
1104 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1105 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301106 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301107 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301108 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001109
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001110 if (ret != 0)
1111 goto err;
1112
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001113 OMAP_HSMMC_WRITE(host->base, HCTL,
1114 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1115 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001116
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001117 /*
1118 * If a MMC dual voltage card is detected, the set_ios fn calls
1119 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001120 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001121 *
David Brownelleb250822009-02-17 14:49:01 -08001122 * Cope with a bit of slop in the range ... per data sheets:
1123 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1124 * but recommended values are 1.71V to 1.89V
1125 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1126 * but recommended values are 2.7V to 3.3V
1127 *
1128 * Board setup code shouldn't permit anything very out-of-range.
1129 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1130 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001131 */
David Brownelleb250822009-02-17 14:49:01 -08001132 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001133 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001134 else
1135 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001136
1137 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001138 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001139
1140 return 0;
1141err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301142 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001143 return ret;
1144}
1145
Adrian Hunterb62f6222009-09-22 16:45:01 -07001146/* Protect the card while the cover is open */
1147static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1148{
1149 if (!mmc_slot(host).get_cover_state)
1150 return;
1151
1152 host->reqs_blocked = 0;
1153 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1154 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301155 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001156 "card is now accessible\n",
1157 mmc_hostname(host->mmc));
1158 host->protect_card = 0;
1159 }
1160 } else {
1161 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301162 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001163 "card is now inaccessible\n",
1164 mmc_hostname(host->mmc));
1165 host->protect_card = 1;
1166 }
1167 }
1168}
1169
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001170/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001171 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001172 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001173static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001174{
NeilBrown7efab4f2011-12-30 12:35:13 +11001175 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001176 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001177 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001178
Adrian Huntera6b22402009-09-22 16:44:45 -07001179 if (host->suspended)
NeilBrown7efab4f2011-12-30 12:35:13 +11001180 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001181
1182 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001183
Denis Karpov191d1f12009-09-22 16:44:55 -07001184 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001185 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001186 else {
1187 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001188 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001189 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001190
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001191 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001192 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001193 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001194 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001195 return IRQ_HANDLED;
1196}
1197
Russell Kingc5c98922012-04-13 12:14:39 +01001198static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001199{
Russell Kingc5c98922012-04-13 12:14:39 +01001200 struct omap_hsmmc_host *host = param;
1201 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001202 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001203 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001204
Russell Kingc5c98922012-04-13 12:14:39 +01001205 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001206 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001207 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001208 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001209 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001210
Adrian Hunter770d7432011-05-06 12:14:11 +03001211 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001212 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001213 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001214 dma_unmap_sg(chan->device->dev,
1215 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001216 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001217
1218 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001219 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001220 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001221
1222 /* If DMA has finished after TC, complete the request */
1223 if (!req_in_progress) {
1224 struct mmc_request *mrq = host->mrq;
1225
1226 host->mrq = NULL;
1227 mmc_request_done(host->mmc, mrq);
1228 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001229}
1230
Per Forlin9782aff2011-07-01 18:55:23 +02001231static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1232 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001233 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001234 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001235{
1236 int dma_len;
1237
1238 if (!next && data->host_cookie &&
1239 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301240 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001241 " host->next_data.cookie %d\n",
1242 __func__, data->host_cookie, host->next_data.cookie);
1243 data->host_cookie = 0;
1244 }
1245
1246 /* Check if next job is already prepared */
1247 if (next ||
1248 (!next && data->host_cookie != host->next_data.cookie)) {
Russell King26b88522012-04-13 12:27:37 +01001249 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001250 omap_hsmmc_get_dma_dir(host, data));
1251
1252 } else {
1253 dma_len = host->next_data.dma_len;
1254 host->next_data.dma_len = 0;
1255 }
1256
1257
1258 if (dma_len == 0)
1259 return -EINVAL;
1260
1261 if (next) {
1262 next->dma_len = dma_len;
1263 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1264 } else
1265 host->dma_len = dma_len;
1266
1267 return 0;
1268}
1269
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001270/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001271 * Routine to configure and start DMA for the MMC card
1272 */
Denis Karpov70a33412009-09-22 16:44:59 -07001273static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1274 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001275{
Russell King26b88522012-04-13 12:27:37 +01001276 struct dma_slave_config cfg;
1277 struct dma_async_tx_descriptor *tx;
1278 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001279 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001280 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001281
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001282 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001283 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001284 struct scatterlist *sgl;
1285
1286 sgl = data->sg + i;
1287 if (sgl->length % data->blksz)
1288 return -EINVAL;
1289 }
1290 if ((data->blksz % 4) != 0)
1291 /* REVISIT: The MMC buffer increments only when MSB is written.
1292 * Return error for blksz which is non multiple of four.
1293 */
1294 return -EINVAL;
1295
Adrian Hunterb4175772010-05-26 14:42:06 -07001296 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001297
Russell Kingc5c98922012-04-13 12:14:39 +01001298 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001299
Russell King26b88522012-04-13 12:27:37 +01001300 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1301 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1302 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1303 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1304 cfg.src_maxburst = data->blksz / 4;
1305 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001306
Russell King26b88522012-04-13 12:27:37 +01001307 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001308 if (ret)
1309 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001310
Russell King26b88522012-04-13 12:27:37 +01001311 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1312 if (ret)
1313 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001314
Russell King26b88522012-04-13 12:27:37 +01001315 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1316 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1317 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1318 if (!tx) {
1319 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1320 /* FIXME: cleanup */
1321 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001322 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001323
Russell King26b88522012-04-13 12:27:37 +01001324 tx->callback = omap_hsmmc_dma_callback;
1325 tx->callback_param = host;
1326
1327 /* Does not fail */
1328 dmaengine_submit(tx);
1329
1330 host->dma_ch = 1;
1331
1332 dma_async_issue_pending(chan);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001333
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001334 return 0;
1335}
1336
Denis Karpov70a33412009-09-22 16:44:59 -07001337static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001338 unsigned int timeout_ns,
1339 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001340{
1341 unsigned int timeout, cycle_ns;
1342 uint32_t reg, clkd, dto = 0;
1343
1344 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1345 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1346 if (clkd == 0)
1347 clkd = 1;
1348
1349 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001350 timeout = timeout_ns / cycle_ns;
1351 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001352 if (timeout) {
1353 while ((timeout & 0x80000000) == 0) {
1354 dto += 1;
1355 timeout <<= 1;
1356 }
1357 dto = 31 - dto;
1358 timeout <<= 1;
1359 if (timeout && dto)
1360 dto += 1;
1361 if (dto >= 13)
1362 dto -= 13;
1363 else
1364 dto = 0;
1365 if (dto > 14)
1366 dto = 14;
1367 }
1368
1369 reg &= ~DTO_MASK;
1370 reg |= dto << DTO_SHIFT;
1371 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1372}
1373
1374/*
1375 * Configure block length for MMC/SD cards and initiate the transfer.
1376 */
1377static int
Denis Karpov70a33412009-09-22 16:44:59 -07001378omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001379{
1380 int ret;
1381 host->data = req->data;
1382
1383 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001384 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001385 /*
1386 * Set an arbitrary 100ms data timeout for commands with
1387 * busy signal.
1388 */
1389 if (req->cmd->flags & MMC_RSP_BUSY)
1390 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001391 return 0;
1392 }
1393
1394 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1395 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001396 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001397
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001398 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001399 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001400 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301401 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001402 return ret;
1403 }
1404 }
1405 return 0;
1406}
1407
Per Forlin9782aff2011-07-01 18:55:23 +02001408static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1409 int err)
1410{
1411 struct omap_hsmmc_host *host = mmc_priv(mmc);
1412 struct mmc_data *data = mrq->data;
1413
Russell King26b88522012-04-13 12:27:37 +01001414 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001415 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001416
Russell King26b88522012-04-13 12:27:37 +01001417 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1418 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001419 data->host_cookie = 0;
1420 }
1421}
1422
1423static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1424 bool is_first_req)
1425{
1426 struct omap_hsmmc_host *host = mmc_priv(mmc);
1427
1428 if (mrq->data->host_cookie) {
1429 mrq->data->host_cookie = 0;
1430 return ;
1431 }
1432
Russell Kingc5c98922012-04-13 12:14:39 +01001433 if (host->use_dma) {
1434 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001435
Per Forlin9782aff2011-07-01 18:55:23 +02001436 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001437 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001438 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001439 }
Per Forlin9782aff2011-07-01 18:55:23 +02001440}
1441
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001442/*
1443 * Request function. for read/write operation
1444 */
Denis Karpov70a33412009-09-22 16:44:59 -07001445static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001446{
Denis Karpov70a33412009-09-22 16:44:59 -07001447 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001448 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001449
Adrian Hunterb4175772010-05-26 14:42:06 -07001450 BUG_ON(host->req_in_progress);
1451 BUG_ON(host->dma_ch != -1);
1452 if (host->protect_card) {
1453 if (host->reqs_blocked < 3) {
1454 /*
1455 * Ensure the controller is left in a consistent
1456 * state by resetting the command and data state
1457 * machines.
1458 */
1459 omap_hsmmc_reset_controller_fsm(host, SRD);
1460 omap_hsmmc_reset_controller_fsm(host, SRC);
1461 host->reqs_blocked += 1;
1462 }
1463 req->cmd->error = -EBADF;
1464 if (req->data)
1465 req->data->error = -EBADF;
1466 req->cmd->retries = 0;
1467 mmc_request_done(mmc, req);
1468 return;
1469 } else if (host->reqs_blocked)
1470 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001471 WARN_ON(host->mrq != NULL);
1472 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001473 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001474 if (err) {
1475 req->cmd->error = err;
1476 if (req->data)
1477 req->data->error = err;
1478 host->mrq = NULL;
1479 mmc_request_done(mmc, req);
1480 return;
1481 }
1482
Denis Karpov70a33412009-09-22 16:44:59 -07001483 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001484}
1485
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001486/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001487static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001488{
Denis Karpov70a33412009-09-22 16:44:59 -07001489 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001490 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001491
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301492 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001493
Adrian Huntera3621462009-09-22 16:44:42 -07001494 if (ios->power_mode != host->power_mode) {
1495 switch (ios->power_mode) {
1496 case MMC_POWER_OFF:
1497 mmc_slot(host).set_power(host->dev, host->slot_id,
1498 0, 0);
1499 break;
1500 case MMC_POWER_UP:
1501 mmc_slot(host).set_power(host->dev, host->slot_id,
1502 1, ios->vdd);
1503 break;
1504 case MMC_POWER_ON:
1505 do_send_init_stream = 1;
1506 break;
1507 }
1508 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001509 }
1510
Denis Karpovdd498ef2009-09-22 16:44:49 -07001511 /* FIXME: set registers based only on changes to ios */
1512
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001513 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001514
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301515 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001516 /* Only MMC1 can interface at 3V without some flavor
1517 * of external transceiver; but they all handle 1.8V.
1518 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001519 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Rajendra Nayak1f84b712012-03-12 20:32:38 +05301520 (ios->vdd == DUAL_VOLT_OCR_BIT) &&
1521 /*
1522 * With pbias cell programming missing, this
1523 * can't be allowed when booting with device
1524 * tree.
1525 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +05301526 !host->dev->of_node) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001527 /*
1528 * The mmc_select_voltage fn of the core does
1529 * not seem to set the power_mode to
1530 * MMC_POWER_UP upon recalculating the voltage.
1531 * vdd 1.8v.
1532 */
Denis Karpov70a33412009-09-22 16:44:59 -07001533 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1534 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001535 "Switch operation failed\n");
1536 }
1537 }
1538
Andy Shevchenko5934df22011-05-06 12:14:06 +03001539 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001540
Adrian Huntera3621462009-09-22 16:44:42 -07001541 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001542 send_init_stream(host);
1543
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001544 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001545
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301546 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001547}
1548
1549static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1550{
Denis Karpov70a33412009-09-22 16:44:59 -07001551 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001552
Denis Karpov191d1f12009-09-22 16:44:55 -07001553 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001554 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001555 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001556}
1557
1558static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1559{
Denis Karpov70a33412009-09-22 16:44:59 -07001560 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001561
Denis Karpov191d1f12009-09-22 16:44:55 -07001562 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001563 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001564 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001565}
1566
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001567static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1568{
1569 struct omap_hsmmc_host *host = mmc_priv(mmc);
1570
1571 if (mmc_slot(host).init_card)
1572 mmc_slot(host).init_card(card);
1573}
1574
Denis Karpov70a33412009-09-22 16:44:59 -07001575static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001576{
1577 u32 hctl, capa, value;
1578
1579 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301580 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001581 hctl = SDVS30;
1582 capa = VS30 | VS18;
1583 } else {
1584 hctl = SDVS18;
1585 capa = VS18;
1586 }
1587
1588 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1589 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1590
1591 value = OMAP_HSMMC_READ(host->base, CAPA);
1592 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1593
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001594 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001595 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001596}
1597
Denis Karpov70a33412009-09-22 16:44:59 -07001598static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001599{
Denis Karpov70a33412009-09-22 16:44:59 -07001600 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001601
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301602 pm_runtime_get_sync(host->dev);
1603
Denis Karpovdd498ef2009-09-22 16:44:49 -07001604 return 0;
1605}
1606
Adrian Hunter907d2e72012-02-29 09:17:21 +02001607static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001608{
Denis Karpov70a33412009-09-22 16:44:59 -07001609 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001610
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301611 pm_runtime_mark_last_busy(host->dev);
1612 pm_runtime_put_autosuspend(host->dev);
1613
Denis Karpovdd498ef2009-09-22 16:44:49 -07001614 return 0;
1615}
1616
Denis Karpov70a33412009-09-22 16:44:59 -07001617static const struct mmc_host_ops omap_hsmmc_ops = {
1618 .enable = omap_hsmmc_enable_fclk,
1619 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001620 .post_req = omap_hsmmc_post_req,
1621 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001622 .request = omap_hsmmc_request,
1623 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001624 .get_cd = omap_hsmmc_get_cd,
1625 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001626 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001627 /* NYET -- enable_sdio_irq */
1628};
1629
Denis Karpovd900f712009-09-22 16:44:38 -07001630#ifdef CONFIG_DEBUG_FS
1631
Denis Karpov70a33412009-09-22 16:44:59 -07001632static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001633{
1634 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001635 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001636 int context_loss = 0;
1637
Denis Karpov70a33412009-09-22 16:44:59 -07001638 if (host->pdata->get_context_loss_count)
1639 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001640
Adrian Hunter907d2e72012-02-29 09:17:21 +02001641 seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1642 mmc->index, host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001643
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301644 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001645 seq_printf(s, "host suspended, can't read registers\n");
1646 return 0;
1647 }
1648
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301649 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001650
Denis Karpovd900f712009-09-22 16:44:38 -07001651 seq_printf(s, "CON:\t\t0x%08x\n",
1652 OMAP_HSMMC_READ(host->base, CON));
1653 seq_printf(s, "HCTL:\t\t0x%08x\n",
1654 OMAP_HSMMC_READ(host->base, HCTL));
1655 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1656 OMAP_HSMMC_READ(host->base, SYSCTL));
1657 seq_printf(s, "IE:\t\t0x%08x\n",
1658 OMAP_HSMMC_READ(host->base, IE));
1659 seq_printf(s, "ISE:\t\t0x%08x\n",
1660 OMAP_HSMMC_READ(host->base, ISE));
1661 seq_printf(s, "CAPA:\t\t0x%08x\n",
1662 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001663
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301664 pm_runtime_mark_last_busy(host->dev);
1665 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001666
Denis Karpovd900f712009-09-22 16:44:38 -07001667 return 0;
1668}
1669
Denis Karpov70a33412009-09-22 16:44:59 -07001670static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001671{
Denis Karpov70a33412009-09-22 16:44:59 -07001672 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001673}
1674
1675static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001676 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001677 .read = seq_read,
1678 .llseek = seq_lseek,
1679 .release = single_release,
1680};
1681
Denis Karpov70a33412009-09-22 16:44:59 -07001682static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001683{
1684 if (mmc->debugfs_root)
1685 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1686 mmc, &mmc_regs_fops);
1687}
1688
1689#else
1690
Denis Karpov70a33412009-09-22 16:44:59 -07001691static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001692{
1693}
1694
1695#endif
1696
Rajendra Nayak46856a62012-03-12 20:32:37 +05301697#ifdef CONFIG_OF
1698static u16 omap4_reg_offset = 0x100;
1699
1700static const struct of_device_id omap_mmc_of_match[] = {
1701 {
1702 .compatible = "ti,omap2-hsmmc",
1703 },
1704 {
1705 .compatible = "ti,omap3-hsmmc",
1706 },
1707 {
1708 .compatible = "ti,omap4-hsmmc",
1709 .data = &omap4_reg_offset,
1710 },
1711 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001712};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301713MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1714
1715static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1716{
1717 struct omap_mmc_platform_data *pdata;
1718 struct device_node *np = dev->of_node;
Daniel Mackd8714e82012-10-15 21:35:06 +05301719 u32 bus_width, max_freq;
Jan Luebbedc642c22013-01-30 10:07:17 +01001720 int cd_gpio, wp_gpio;
1721
1722 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
1723 wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1724 if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
1725 return ERR_PTR(-EPROBE_DEFER);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301726
1727 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1728 if (!pdata)
1729 return NULL; /* out of memory */
1730
1731 if (of_find_property(np, "ti,dual-volt", NULL))
1732 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1733
1734 /* This driver only supports 1 slot */
1735 pdata->nr_slots = 1;
Jan Luebbedc642c22013-01-30 10:07:17 +01001736 pdata->slots[0].switch_pin = cd_gpio;
1737 pdata->slots[0].gpio_wp = wp_gpio;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301738
1739 if (of_find_property(np, "ti,non-removable", NULL)) {
1740 pdata->slots[0].nonremovable = true;
1741 pdata->slots[0].no_regulator_off_init = true;
1742 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001743 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301744 if (bus_width == 4)
1745 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1746 else if (bus_width == 8)
1747 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1748
1749 if (of_find_property(np, "ti,needs-special-reset", NULL))
1750 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1751
Daniel Mackd8714e82012-10-15 21:35:06 +05301752 if (!of_property_read_u32(np, "max-frequency", &max_freq))
1753 pdata->max_freq = max_freq;
1754
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301755 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
1756 pdata->slots[0].features |= HSMMC_HAS_HSPE_SUPPORT;
1757
Rajendra Nayak46856a62012-03-12 20:32:37 +05301758 return pdata;
1759}
1760#else
1761static inline struct omap_mmc_platform_data
1762 *of_get_hsmmc_pdata(struct device *dev)
1763{
1764 return NULL;
1765}
1766#endif
1767
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001768static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001769{
1770 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1771 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001772 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001773 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001774 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301775 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001776 dma_cap_mask_t mask;
1777 unsigned tx_req, rx_req;
Daniel Mack46b76032012-10-15 21:35:05 +05301778 struct pinctrl *pinctrl;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301779
1780 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1781 if (match) {
1782 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01001783
1784 if (IS_ERR(pdata))
1785 return PTR_ERR(pdata);
1786
Rajendra Nayak46856a62012-03-12 20:32:37 +05301787 if (match->data) {
Uwe Kleine-Königefc9b732012-05-21 21:57:39 +02001788 const u16 *offsetp = match->data;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301789 pdata->reg_offset = *offsetp;
1790 }
1791 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001792
1793 if (pdata == NULL) {
1794 dev_err(&pdev->dev, "Platform Data is missing\n");
1795 return -ENXIO;
1796 }
1797
1798 if (pdata->nr_slots == 0) {
1799 dev_err(&pdev->dev, "No Slots\n");
1800 return -ENXIO;
1801 }
1802
1803 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1804 irq = platform_get_irq(pdev, 0);
1805 if (res == NULL || irq < 0)
1806 return -ENXIO;
1807
Chris Ball984b2032011-03-22 16:34:42 -07001808 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001809 if (res == NULL)
1810 return -EBUSY;
1811
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001812 ret = omap_hsmmc_gpio_init(pdata);
1813 if (ret)
1814 goto err;
1815
Denis Karpov70a33412009-09-22 16:44:59 -07001816 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001817 if (!mmc) {
1818 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001819 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001820 }
1821
1822 host = mmc_priv(mmc);
1823 host->mmc = mmc;
1824 host->pdata = pdata;
1825 host->dev = &pdev->dev;
1826 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001827 host->dma_ch = -1;
1828 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001829 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05301830 host->mapbase = res->start + pdata->reg_offset;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001831 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001832 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001833 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001834
1835 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001836
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301837 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001838
Adrian Huntere0eb2422010-02-15 10:03:34 -08001839 /*
1840 * If regulator_disable can only put vcc_aux to sleep then there is
1841 * no off state.
1842 */
1843 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1844 mmc_slot(host).no_off = 1;
1845
Daniel Mackd418ed82012-02-19 13:20:33 +01001846 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1847
1848 if (pdata->max_freq > 0)
1849 mmc->f_max = pdata->max_freq;
1850 else
1851 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001852
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001853 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001854
Russell King6f7607c2009-01-28 10:22:50 +00001855 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001856 if (IS_ERR(host->fclk)) {
1857 ret = PTR_ERR(host->fclk);
1858 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001859 goto err1;
1860 }
1861
Paul Walmsley9b682562011-10-06 14:50:35 -06001862 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1863 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1864 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1865 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001866
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301867 pm_runtime_enable(host->dev);
1868 pm_runtime_get_sync(host->dev);
1869 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1870 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001871
Balaji T K92a3aeb2012-02-24 21:14:34 +05301872 omap_hsmmc_context_save(host);
1873
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301874 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1875 /*
1876 * MMC can still work without debounce clock.
1877 */
1878 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301879 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05301880 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301881 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
1882 clk_put(host->dbclk);
1883 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07001884 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001885
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001886 /* Since we do only SG emulation, we can have as many segs
1887 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001888 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001889
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001890 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1891 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1892 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1893 mmc->max_seg_size = mmc->max_req_size;
1894
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001895 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001896 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001897
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001898 mmc->caps |= mmc_slot(host).caps;
1899 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001900 mmc->caps |= MMC_CAP_4_BIT_DATA;
1901
Denis Karpov191d1f12009-09-22 16:44:55 -07001902 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001903 mmc->caps |= MMC_CAP_NONREMOVABLE;
1904
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001905 mmc->pm_caps = mmc_slot(host).pm_caps;
1906
Denis Karpov70a33412009-09-22 16:44:59 -07001907 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001908
Balaji T Kb7bf7732012-03-07 09:55:30 -05001909 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1910 if (!res) {
1911 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
Kevin Hilman9c17d082012-07-10 16:40:56 -07001912 ret = -ENXIO;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001913 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001914 }
Russell King26b88522012-04-13 12:27:37 +01001915 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001916
1917 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1918 if (!res) {
1919 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
Kevin Hilman9c17d082012-07-10 16:40:56 -07001920 ret = -ENXIO;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001921 goto err_irq;
1922 }
Russell King26b88522012-04-13 12:27:37 +01001923 rx_req = res->start;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001924
Russell King26b88522012-04-13 12:27:37 +01001925 dma_cap_zero(mask);
1926 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01001927
Russell King26b88522012-04-13 12:27:37 +01001928 host->rx_chan = dma_request_channel(mask, omap_dma_filter_fn, &rx_req);
1929 if (!host->rx_chan) {
1930 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001931 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001932 goto err_irq;
1933 }
1934
1935 host->tx_chan = dma_request_channel(mask, omap_dma_filter_fn, &tx_req);
1936 if (!host->tx_chan) {
1937 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001938 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001939 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01001940 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001941
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001942 /* Request IRQ for MMC operations */
Yong Zhangd9618e92011-09-22 16:59:04 +08001943 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001944 mmc_hostname(mmc), host);
1945 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301946 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001947 goto err_irq;
1948 }
1949
1950 if (pdata->init != NULL) {
1951 if (pdata->init(&pdev->dev) != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301952 dev_err(mmc_dev(host->mmc),
Denis Karpov70a33412009-09-22 16:44:59 -07001953 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001954 goto err_irq_cd_init;
1955 }
1956 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001957
Adrian Hunterb702b102010-02-15 10:03:35 -08001958 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001959 ret = omap_hsmmc_reg_get(host);
1960 if (ret)
1961 goto err_reg;
1962 host->use_reg = 1;
1963 }
1964
David Brownellb583f262009-05-28 14:04:03 -07001965 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001966
1967 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001968 if ((mmc_slot(host).card_detect_irq)) {
NeilBrown7efab4f2011-12-30 12:35:13 +11001969 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1970 NULL,
1971 omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08001972 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11001973 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001974 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301975 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001976 "Unable to grab MMC CD IRQ\n");
1977 goto err_irq_cd;
1978 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00001979 pdata->suspend = omap_hsmmc_suspend_cdirq;
1980 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001981 }
1982
Adrian Hunterb4175772010-05-26 14:42:06 -07001983 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001984
Daniel Mack46b76032012-10-15 21:35:05 +05301985 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1986 if (IS_ERR(pinctrl))
1987 dev_warn(&pdev->dev,
1988 "pins are not configured from the driver\n");
1989
Adrian Hunterb62f6222009-09-22 16:45:01 -07001990 omap_hsmmc_protect_card(host);
1991
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001992 mmc_add_host(mmc);
1993
Denis Karpov191d1f12009-09-22 16:44:55 -07001994 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001995 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1996 if (ret < 0)
1997 goto err_slot_name;
1998 }
Denis Karpov191d1f12009-09-22 16:44:55 -07001999 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002000 ret = device_create_file(&mmc->class_dev,
2001 &dev_attr_cover_switch);
2002 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002003 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002004 }
2005
Denis Karpov70a33412009-09-22 16:44:59 -07002006 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302007 pm_runtime_mark_last_busy(host->dev);
2008 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002009
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002010 return 0;
2011
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002012err_slot_name:
2013 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002014 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002015err_irq_cd:
2016 if (host->use_reg)
2017 omap_hsmmc_reg_put(host);
2018err_reg:
2019 if (host->pdata->cleanup)
2020 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002021err_irq_cd_init:
2022 free_irq(host->irq, host);
2023err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002024 if (host->tx_chan)
2025 dma_release_channel(host->tx_chan);
2026 if (host->rx_chan)
2027 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302028 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002029 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002030 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302031 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302032 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002033 clk_put(host->dbclk);
2034 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002035err1:
2036 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002037 platform_set_drvdata(pdev, NULL);
2038 mmc_free_host(mmc);
2039err_alloc:
2040 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002041err:
Russell King48b332f2012-04-18 11:11:57 +01002042 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2043 if (res)
2044 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002045 return ret;
2046}
2047
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002048static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002049{
Denis Karpov70a33412009-09-22 16:44:59 -07002050 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002051 struct resource *res;
2052
Felipe Balbi927ce942012-03-14 11:18:27 +02002053 pm_runtime_get_sync(host->dev);
2054 mmc_remove_host(host->mmc);
2055 if (host->use_reg)
2056 omap_hsmmc_reg_put(host);
2057 if (host->pdata->cleanup)
2058 host->pdata->cleanup(&pdev->dev);
2059 free_irq(host->irq, host);
2060 if (mmc_slot(host).card_detect_irq)
2061 free_irq(mmc_slot(host).card_detect_irq, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002062
Russell Kingc5c98922012-04-13 12:14:39 +01002063 if (host->tx_chan)
2064 dma_release_channel(host->tx_chan);
2065 if (host->rx_chan)
2066 dma_release_channel(host->rx_chan);
2067
Felipe Balbi927ce942012-03-14 11:18:27 +02002068 pm_runtime_put_sync(host->dev);
2069 pm_runtime_disable(host->dev);
2070 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302071 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302072 clk_disable_unprepare(host->dbclk);
Felipe Balbi927ce942012-03-14 11:18:27 +02002073 clk_put(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002074 }
2075
Balaji T K9ea28ec2012-10-15 21:35:08 +05302076 omap_hsmmc_gpio_free(host->pdata);
Felipe Balbi927ce942012-03-14 11:18:27 +02002077 iounmap(host->base);
Balaji T K9d1f0282012-10-15 21:35:07 +05302078 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002079
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002080 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2081 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002082 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002083 platform_set_drvdata(pdev, NULL);
2084
2085 return 0;
2086}
2087
2088#ifdef CONFIG_PM
Felipe Balbia48ce882012-11-19 21:59:59 +05302089static int omap_hsmmc_prepare(struct device *dev)
2090{
2091 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2092
2093 if (host->pdata->suspend)
2094 return host->pdata->suspend(dev, host->slot_id);
2095
2096 return 0;
2097}
2098
2099static void omap_hsmmc_complete(struct device *dev)
2100{
2101 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2102
2103 if (host->pdata->resume)
2104 host->pdata->resume(dev, host->slot_id);
2105
2106}
2107
Kevin Hilmana791daa2010-05-26 14:42:07 -07002108static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002109{
2110 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002111 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2112
2113 if (!host)
2114 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002115
2116 if (host && host->suspended)
2117 return 0;
2118
Felipe Balbi927ce942012-03-14 11:18:27 +02002119 pm_runtime_get_sync(host->dev);
2120 host->suspended = 1;
Felipe Balbi927ce942012-03-14 11:18:27 +02002121 ret = mmc_suspend_host(host->mmc);
2122
2123 if (ret) {
2124 host->suspended = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002125 goto err;
2126 }
2127
2128 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2129 omap_hsmmc_disable_irq(host);
2130 OMAP_HSMMC_WRITE(host->base, HCTL,
2131 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2132 }
2133
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302134 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302135 clk_disable_unprepare(host->dbclk);
Eliad Peller31f9d462011-11-22 16:02:17 +02002136err:
2137 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002138 return ret;
2139}
2140
2141/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002142static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002143{
2144 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002145 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2146
2147 if (!host)
2148 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002149
2150 if (host && !host->suspended)
2151 return 0;
2152
Felipe Balbi927ce942012-03-14 11:18:27 +02002153 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002154
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302155 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302156 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002157
Felipe Balbi927ce942012-03-14 11:18:27 +02002158 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2159 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002160
Felipe Balbi927ce942012-03-14 11:18:27 +02002161 omap_hsmmc_protect_card(host);
2162
2163 /* Notify the core to resume the host */
2164 ret = mmc_resume_host(host->mmc);
2165 if (ret == 0)
2166 host->suspended = 0;
2167
2168 pm_runtime_mark_last_busy(host->dev);
2169 pm_runtime_put_autosuspend(host->dev);
2170
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002171 return ret;
2172
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002173}
2174
2175#else
Felipe Balbia48ce882012-11-19 21:59:59 +05302176#define omap_hsmmc_prepare NULL
2177#define omap_hsmmc_complete NULL
Denis Karpov70a33412009-09-22 16:44:59 -07002178#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302179#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002180#endif
2181
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302182static int omap_hsmmc_runtime_suspend(struct device *dev)
2183{
2184 struct omap_hsmmc_host *host;
2185
2186 host = platform_get_drvdata(to_platform_device(dev));
2187 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002188 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302189
2190 return 0;
2191}
2192
2193static int omap_hsmmc_runtime_resume(struct device *dev)
2194{
2195 struct omap_hsmmc_host *host;
2196
2197 host = platform_get_drvdata(to_platform_device(dev));
2198 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002199 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302200
2201 return 0;
2202}
2203
Kevin Hilmana791daa2010-05-26 14:42:07 -07002204static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002205 .suspend = omap_hsmmc_suspend,
2206 .resume = omap_hsmmc_resume,
Felipe Balbia48ce882012-11-19 21:59:59 +05302207 .prepare = omap_hsmmc_prepare,
2208 .complete = omap_hsmmc_complete,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302209 .runtime_suspend = omap_hsmmc_runtime_suspend,
2210 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002211};
2212
2213static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002214 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002215 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002216 .driver = {
2217 .name = DRIVER_NAME,
2218 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002219 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302220 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002221 },
2222};
2223
Felipe Balbib7964502012-03-14 11:18:32 +02002224module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002225MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2226MODULE_LICENSE("GPL");
2227MODULE_ALIAS("platform:" DRIVER_NAME);
2228MODULE_AUTHOR("Texas Instruments Inc");