blob: dc89aead35a5de564036a515c09c43abfa7fa78c [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Andy Shevchenkoac330f442011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
Russell Kingc5c98922012-04-13 12:14:39 +010022#include <linux/dmaengine.h>
Denis Karpovd900f712009-09-22 16:44:38 -070023#include <linux/seq_file.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010024#include <linux/interrupt.h>
25#include <linux/delay.h>
26#include <linux/dma-mapping.h>
27#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010028#include <linux/timer.h>
29#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053030#include <linux/of.h>
31#include <linux/of_gpio.h>
32#include <linux/of_device.h>
Russell King3451c062012-04-21 22:35:42 +010033#include <linux/omap-dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010034#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070035#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070036#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010037#include <linux/io.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080038#include <linux/gpio.h>
39#include <linux/regulator/consumer.h>
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;
Tony Lindgrencf5ae402013-05-10 17:42:33 +0530164 int pbias_disable;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100165 void __iomem *base;
166 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700167 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100168 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200169 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100170 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700171 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100172 int suspended;
173 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100174 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100175 struct dma_chan *tx_chan;
176 struct dma_chan *rx_chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100177 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200178 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700179 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700180 int protect_card;
181 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800182 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700183 int req_in_progress;
Per Forlin9782aff2011-07-01 18:55:23 +0200184 struct omap_hsmmc_next next_data;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700185
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100186 struct omap_mmc_platform_data *pdata;
187};
188
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800189static int omap_hsmmc_card_detect(struct device *dev, int slot)
190{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530191 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
192 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800193
194 /* NOTE: assumes card detect signal is active-low */
195 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
196}
197
198static int omap_hsmmc_get_wp(struct device *dev, int slot)
199{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530200 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
201 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800202
203 /* NOTE: assumes write protect signal is active-high */
204 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
205}
206
207static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
208{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530209 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
210 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800211
212 /* NOTE: assumes card detect signal is active-low */
213 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
214}
215
216#ifdef CONFIG_PM
217
218static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
219{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530220 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
221 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800222
223 disable_irq(mmc->slots[0].card_detect_irq);
224 return 0;
225}
226
227static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
228{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530229 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
230 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800231
232 enable_irq(mmc->slots[0].card_detect_irq);
233 return 0;
234}
235
236#else
237
238#define omap_hsmmc_suspend_cdirq NULL
239#define omap_hsmmc_resume_cdirq NULL
240
241#endif
242
Adrian Hunterb702b102010-02-15 10:03:35 -0800243#ifdef CONFIG_REGULATOR
244
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500245static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800246 int vdd)
247{
248 struct omap_hsmmc_host *host =
249 platform_get_drvdata(to_platform_device(dev));
250 int ret = 0;
251
252 /*
253 * If we don't see a Vcc regulator, assume it's a fixed
254 * voltage always-on regulator.
255 */
256 if (!host->vcc)
257 return 0;
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530258 /*
Tony Lindgrencf5ae402013-05-10 17:42:33 +0530259 * With DT, never turn OFF the regulator for MMC1. This is because
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530260 * the pbias cell programming support is still missing when
261 * booting with Device tree
262 */
Tony Lindgrencf5ae402013-05-10 17:42:33 +0530263 if (host->pbias_disable && !vdd)
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530264 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800265
266 if (mmc_slot(host).before_set_reg)
267 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
268
269 /*
270 * Assume Vcc regulator is used only to power the card ... OMAP
271 * VDDS is used to power the pins, optionally with a transceiver to
272 * support cards using voltages other than VDDS (1.8V nominal). When a
273 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
274 *
275 * In some cases this regulator won't support enable/disable;
276 * e.g. it's a fixed rail for a WLAN chip.
277 *
278 * In other cases vcc_aux switches interface power. Example, for
279 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
280 * chips/cards need an interface voltage rail too.
281 */
282 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400283 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800284 /* Enable interface voltage rail, if needed */
285 if (ret == 0 && host->vcc_aux) {
286 ret = regulator_enable(host->vcc_aux);
287 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400288 ret = mmc_regulator_set_ocr(host->mmc,
289 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800290 }
291 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400292 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800293 if (host->vcc_aux)
294 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400295 if (!ret) {
296 /* Then proceed to shut down the local regulator */
297 ret = mmc_regulator_set_ocr(host->mmc,
298 host->vcc, 0);
299 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800300 }
301
302 if (mmc_slot(host).after_set_reg)
303 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
304
305 return ret;
306}
307
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800308static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
309{
310 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700311 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800312
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800313 reg = regulator_get(host->dev, "vmmc");
314 if (IS_ERR(reg)) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +0530315 dev_err(host->dev, "vmmc regulator missing\n");
NeilBrown1fdc90f2012-08-08 00:06:00 -0400316 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800317 } else {
NeilBrown1fdc90f2012-08-08 00:06:00 -0400318 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800319 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700320 ocr_value = mmc_regulator_get_ocrmask(reg);
321 if (!mmc_slot(host).ocr_mask) {
322 mmc_slot(host).ocr_mask = ocr_value;
323 } else {
324 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530325 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500326 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700327 mmc_slot(host).ocr_mask = 0;
328 return -EINVAL;
329 }
330 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800331
332 /* Allow an aux regulator */
333 reg = regulator_get(host->dev, "vmmc_aux");
334 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
335
Balaji T Kb1c1df72011-05-30 19:55:34 +0530336 /* For eMMC do not power off when not in sleep state */
337 if (mmc_slot(host).no_regulator_off_init)
338 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800339 /*
340 * UGLY HACK: workaround regulator framework bugs.
341 * When the bootloader leaves a supply active, it's
342 * initialized with zero usecount ... and we can't
343 * disable it without first enabling it. Until the
344 * framework is fixed, we need a workaround like this
345 * (which is safe for MMC, but not in general).
346 */
Adrian Huntere840ce12011-05-06 12:14:10 +0300347 if (regulator_is_enabled(host->vcc) > 0 ||
348 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
349 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
350
351 mmc_slot(host).set_power(host->dev, host->slot_id,
352 1, vdd);
353 mmc_slot(host).set_power(host->dev, host->slot_id,
354 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800355 }
356 }
357
358 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800359}
360
361static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
362{
363 regulator_put(host->vcc);
364 regulator_put(host->vcc_aux);
365 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800366}
367
Adrian Hunterb702b102010-02-15 10:03:35 -0800368static inline int omap_hsmmc_have_reg(void)
369{
370 return 1;
371}
372
373#else
374
375static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
376{
377 return -EINVAL;
378}
379
380static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
381{
382}
383
384static inline int omap_hsmmc_have_reg(void)
385{
386 return 0;
387}
388
389#endif
390
391static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
392{
393 int ret;
394
395 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800396 if (pdata->slots[0].cover)
397 pdata->slots[0].get_cover_state =
398 omap_hsmmc_get_cover_state;
399 else
400 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
401 pdata->slots[0].card_detect_irq =
402 gpio_to_irq(pdata->slots[0].switch_pin);
403 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
404 if (ret)
405 return ret;
406 ret = gpio_direction_input(pdata->slots[0].switch_pin);
407 if (ret)
408 goto err_free_sp;
409 } else
410 pdata->slots[0].switch_pin = -EINVAL;
411
412 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
413 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
414 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
415 if (ret)
416 goto err_free_cd;
417 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
418 if (ret)
419 goto err_free_wp;
420 } else
421 pdata->slots[0].gpio_wp = -EINVAL;
422
423 return 0;
424
425err_free_wp:
426 gpio_free(pdata->slots[0].gpio_wp);
427err_free_cd:
428 if (gpio_is_valid(pdata->slots[0].switch_pin))
429err_free_sp:
430 gpio_free(pdata->slots[0].switch_pin);
431 return ret;
432}
433
434static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
435{
436 if (gpio_is_valid(pdata->slots[0].gpio_wp))
437 gpio_free(pdata->slots[0].gpio_wp);
438 if (gpio_is_valid(pdata->slots[0].switch_pin))
439 gpio_free(pdata->slots[0].switch_pin);
440}
441
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100442/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300443 * Start clock to the card
444 */
445static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
446{
447 OMAP_HSMMC_WRITE(host->base, SYSCTL,
448 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
449}
450
451/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100452 * Stop clock to the card
453 */
Denis Karpov70a33412009-09-22 16:44:59 -0700454static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100455{
456 OMAP_HSMMC_WRITE(host->base, SYSCTL,
457 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
458 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900459 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100460}
461
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700462static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
463 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700464{
465 unsigned int irq_mask;
466
467 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530468 irq_mask = INT_EN_MASK & ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700469 else
470 irq_mask = INT_EN_MASK;
471
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700472 /* Disable timeout for erases */
473 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530474 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700475
Adrian Hunterb4175772010-05-26 14:42:06 -0700476 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
477 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
478 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
479}
480
481static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
482{
483 OMAP_HSMMC_WRITE(host->base, ISE, 0);
484 OMAP_HSMMC_WRITE(host->base, IE, 0);
485 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
486}
487
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300488/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530489static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300490{
491 u16 dsor = 0;
492
493 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530494 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300495 if (dsor > 250)
496 dsor = 250;
497 }
498
499 return dsor;
500}
501
Andy Shevchenko5934df22011-05-06 12:14:06 +0300502static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
503{
504 struct mmc_ios *ios = &host->mmc->ios;
505 unsigned long regval;
506 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530507 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300508
Venkatraman S8986d312012-08-07 19:10:38 +0530509 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300510
511 omap_hsmmc_stop_clock(host);
512
513 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
514 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530515 clkdiv = calc_divisor(host, ios);
516 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300517 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
518 OMAP_HSMMC_WRITE(host->base, SYSCTL,
519 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
520
521 /* Wait till the ICS bit is set */
522 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
523 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
524 && time_before(jiffies, timeout))
525 cpu_relax();
526
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530527 /*
528 * Enable High-Speed Support
529 * Pre-Requisites
530 * - Controller should support High-Speed-Enable Bit
531 * - Controller should not be using DDR Mode
532 * - Controller should advertise that it supports High Speed
533 * in capabilities register
534 * - MMC/SD clock coming out of controller > 25MHz
535 */
536 if ((mmc_slot(host).features & HSMMC_HAS_HSPE_SUPPORT) &&
537 (ios->timing != MMC_TIMING_UHS_DDR50) &&
538 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
539 regval = OMAP_HSMMC_READ(host->base, HCTL);
540 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
541 regval |= HSPE;
542 else
543 regval &= ~HSPE;
544
545 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
546 }
547
Andy Shevchenko5934df22011-05-06 12:14:06 +0300548 omap_hsmmc_start_clock(host);
549}
550
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400551static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
552{
553 struct mmc_ios *ios = &host->mmc->ios;
554 u32 con;
555
556 con = OMAP_HSMMC_READ(host->base, CON);
Balaji T K03b5d922012-04-09 12:08:33 +0530557 if (ios->timing == MMC_TIMING_UHS_DDR50)
558 con |= DDR; /* configure in DDR mode */
559 else
560 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400561 switch (ios->bus_width) {
562 case MMC_BUS_WIDTH_8:
563 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
564 break;
565 case MMC_BUS_WIDTH_4:
566 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
567 OMAP_HSMMC_WRITE(host->base, HCTL,
568 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
569 break;
570 case MMC_BUS_WIDTH_1:
571 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
572 OMAP_HSMMC_WRITE(host->base, HCTL,
573 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
574 break;
575 }
576}
577
578static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
579{
580 struct mmc_ios *ios = &host->mmc->ios;
581 u32 con;
582
583 con = OMAP_HSMMC_READ(host->base, CON);
584 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
585 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
586 else
587 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
588}
589
Denis Karpov11dd62a2009-09-22 16:44:43 -0700590#ifdef CONFIG_PM
591
592/*
593 * Restore the MMC host context, if it was lost as result of a
594 * power state change.
595 */
Denis Karpov70a33412009-09-22 16:44:59 -0700596static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700597{
598 struct mmc_ios *ios = &host->mmc->ios;
599 struct omap_mmc_platform_data *pdata = host->pdata;
600 int context_loss = 0;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400601 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700602 unsigned long timeout;
603
604 if (pdata->get_context_loss_count) {
605 context_loss = pdata->get_context_loss_count(host->dev);
606 if (context_loss < 0)
607 return 1;
608 }
609
610 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
611 context_loss == host->context_loss ? "not " : "");
612 if (host->context_loss == context_loss)
613 return 1;
614
Venkatraman S6c31b212012-08-08 14:26:52 +0530615 if (!OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE)
616 return 1;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700617
Balaji T Kc2200ef2012-03-07 09:55:30 -0500618 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700619 if (host->power_mode != MMC_POWER_OFF &&
620 (1 << ios->vdd) <= MMC_VDD_23_24)
621 hctl = SDVS18;
622 else
623 hctl = SDVS30;
624 capa = VS30 | VS18;
625 } else {
626 hctl = SDVS18;
627 capa = VS18;
628 }
629
630 OMAP_HSMMC_WRITE(host->base, HCTL,
631 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
632
633 OMAP_HSMMC_WRITE(host->base, CAPA,
634 OMAP_HSMMC_READ(host->base, CAPA) | capa);
635
636 OMAP_HSMMC_WRITE(host->base, HCTL,
637 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
638
639 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
640 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
641 && time_before(jiffies, timeout))
642 ;
643
Adrian Hunterb4175772010-05-26 14:42:06 -0700644 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700645
646 /* Do not initialize card-specific things if the power is off */
647 if (host->power_mode == MMC_POWER_OFF)
648 goto out;
649
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400650 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700651
Andy Shevchenko5934df22011-05-06 12:14:06 +0300652 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700653
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400654 omap_hsmmc_set_bus_mode(host);
655
Denis Karpov11dd62a2009-09-22 16:44:43 -0700656out:
657 host->context_loss = context_loss;
658
659 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
660 return 0;
661}
662
663/*
664 * Save the MMC host context (store the number of power state changes so far).
665 */
Denis Karpov70a33412009-09-22 16:44:59 -0700666static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700667{
668 struct omap_mmc_platform_data *pdata = host->pdata;
669 int context_loss;
670
671 if (pdata->get_context_loss_count) {
672 context_loss = pdata->get_context_loss_count(host->dev);
673 if (context_loss < 0)
674 return;
675 host->context_loss = context_loss;
676 }
677}
678
679#else
680
Denis Karpov70a33412009-09-22 16:44:59 -0700681static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700682{
683 return 0;
684}
685
Denis Karpov70a33412009-09-22 16:44:59 -0700686static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700687{
688}
689
690#endif
691
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100692/*
693 * Send init stream sequence to card
694 * before sending IDLE command
695 */
Denis Karpov70a33412009-09-22 16:44:59 -0700696static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100697{
698 int reg = 0;
699 unsigned long timeout;
700
Adrian Hunterb62f6222009-09-22 16:45:01 -0700701 if (host->protect_card)
702 return;
703
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100704 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700705
706 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100707 OMAP_HSMMC_WRITE(host->base, CON,
708 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
709 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
710
711 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530712 while ((reg != CC_EN) && time_before(jiffies, timeout))
713 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100714
715 OMAP_HSMMC_WRITE(host->base, CON,
716 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700717
718 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
719 OMAP_HSMMC_READ(host->base, STAT);
720
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100721 enable_irq(host->irq);
722}
723
724static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700725int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100726{
727 int r = 1;
728
Denis Karpov191d1f12009-09-22 16:44:55 -0700729 if (mmc_slot(host).get_cover_state)
730 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100731 return r;
732}
733
734static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700735omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100736 char *buf)
737{
738 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700739 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100740
Denis Karpov70a33412009-09-22 16:44:59 -0700741 return sprintf(buf, "%s\n",
742 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100743}
744
Denis Karpov70a33412009-09-22 16:44:59 -0700745static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100746
747static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700748omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100749 char *buf)
750{
751 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700752 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100753
Denis Karpov191d1f12009-09-22 16:44:55 -0700754 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100755}
756
Denis Karpov70a33412009-09-22 16:44:59 -0700757static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100758
759/*
760 * Configure the response type and send the cmd.
761 */
762static void
Denis Karpov70a33412009-09-22 16:44:59 -0700763omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100764 struct mmc_data *data)
765{
766 int cmdreg = 0, resptype = 0, cmdtype = 0;
767
Venkatraman S8986d312012-08-07 19:10:38 +0530768 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100769 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
770 host->cmd = cmd;
771
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700772 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100773
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200774 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100775 if (cmd->flags & MMC_RSP_PRESENT) {
776 if (cmd->flags & MMC_RSP_136)
777 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200778 else if (cmd->flags & MMC_RSP_BUSY) {
779 resptype = 3;
780 host->response_busy = 1;
781 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100782 resptype = 2;
783 }
784
785 /*
786 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
787 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
788 * a val of 0x3, rest 0x0.
789 */
790 if (cmd == host->mrq->stop)
791 cmdtype = 0x3;
792
793 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
794
795 if (data) {
796 cmdreg |= DP_SELECT | MSBS | BCE;
797 if (data->flags & MMC_DATA_READ)
798 cmdreg |= DDIR;
799 else
800 cmdreg &= ~(DDIR);
801 }
802
803 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530804 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100805
Adrian Hunterb4175772010-05-26 14:42:06 -0700806 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700807
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100808 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
809 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
810}
811
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200812static int
Denis Karpov70a33412009-09-22 16:44:59 -0700813omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200814{
815 if (data->flags & MMC_DATA_WRITE)
816 return DMA_TO_DEVICE;
817 else
818 return DMA_FROM_DEVICE;
819}
820
Russell Kingc5c98922012-04-13 12:14:39 +0100821static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
822 struct mmc_data *data)
823{
824 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
825}
826
Adrian Hunterb4175772010-05-26 14:42:06 -0700827static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
828{
829 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530830 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700831
Venkatraman S31463b12012-04-09 12:08:34 +0530832 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700833 host->req_in_progress = 0;
834 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530835 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700836
837 omap_hsmmc_disable_irq(host);
838 /* Do not complete the request if DMA is still in progress */
839 if (mrq->data && host->use_dma && dma_ch != -1)
840 return;
841 host->mrq = NULL;
842 mmc_request_done(host->mmc, mrq);
843}
844
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100845/*
846 * Notify the transfer complete to MMC core
847 */
848static void
Denis Karpov70a33412009-09-22 16:44:59 -0700849omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100850{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200851 if (!data) {
852 struct mmc_request *mrq = host->mrq;
853
Adrian Hunter23050102009-09-22 16:44:57 -0700854 /* TC before CC from CMD6 - don't know why, but it happens */
855 if (host->cmd && host->cmd->opcode == 6 &&
856 host->response_busy) {
857 host->response_busy = 0;
858 return;
859 }
860
Adrian Hunterb4175772010-05-26 14:42:06 -0700861 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200862 return;
863 }
864
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100865 host->data = NULL;
866
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100867 if (!data->error)
868 data->bytes_xfered += data->blocks * (data->blksz);
869 else
870 data->bytes_xfered = 0;
871
Ming Leife852272012-06-22 18:49:35 +0800872 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700873 omap_hsmmc_request_done(host, data->mrq);
Ming Leife852272012-06-22 18:49:35 +0800874 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100875 }
Ming Leife852272012-06-22 18:49:35 +0800876 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100877}
878
879/*
880 * Notify the core about command completion
881 */
882static void
Denis Karpov70a33412009-09-22 16:44:59 -0700883omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100884{
885 host->cmd = NULL;
886
887 if (cmd->flags & MMC_RSP_PRESENT) {
888 if (cmd->flags & MMC_RSP_136) {
889 /* response type 2 */
890 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
891 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
892 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
893 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
894 } else {
895 /* response types 1, 1b, 3, 4, 5, 6 */
896 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
897 }
898 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700899 if ((host->data == NULL && !host->response_busy) || cmd->error)
900 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100901}
902
903/*
904 * DMA clean up for command errors
905 */
Denis Karpov70a33412009-09-22 16:44:59 -0700906static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100907{
Adrian Hunterb4175772010-05-26 14:42:06 -0700908 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530909 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700910
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200911 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100912
Venkatraman S31463b12012-04-09 12:08:34 +0530913 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700914 dma_ch = host->dma_ch;
915 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530916 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700917
918 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100919 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
920
921 dmaengine_terminate_all(chan);
922 dma_unmap_sg(chan->device->dev,
923 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700924 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100925
Per Forlin053bf342011-11-07 21:55:11 +0530926 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100927 }
928 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100929}
930
931/*
932 * Readable error output
933 */
934#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300935static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100936{
937 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700938 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300939 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
940 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
941 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
942 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100943 };
944 char res[256];
945 char *buf = res;
946 int len, i;
947
948 len = sprintf(buf, "MMC IRQ 0x%x :", status);
949 buf += len;
950
Denis Karpov70a33412009-09-22 16:44:59 -0700951 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100952 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700953 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100954 buf += len;
955 }
956
Venkatraman S8986d312012-08-07 19:10:38 +0530957 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100958}
Adrian Hunter699b9582011-05-06 12:14:01 +0300959#else
960static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
961 u32 status)
962{
963}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100964#endif /* CONFIG_MMC_DEBUG */
965
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100966/*
967 * MMC controller internal state machines reset
968 *
969 * Used to reset command or data internal state machines, using respectively
970 * SRC or SRD bit of SYSCTL register
971 * Can be called from interrupt context
972 */
Denis Karpov70a33412009-09-22 16:44:59 -0700973static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
974 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100975{
976 unsigned long i = 0;
977 unsigned long limit = (loops_per_jiffy *
978 msecs_to_jiffies(MMC_TIMEOUT_MS));
979
980 OMAP_HSMMC_WRITE(host->base, SYSCTL,
981 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
982
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700983 /*
984 * OMAP4 ES2 and greater has an updated reset logic.
985 * Monitor a 0->1 transition first
986 */
987 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -0500988 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700989 && (i++ < limit))
990 cpu_relax();
991 }
992 i = 0;
993
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100994 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
995 (i++ < limit))
996 cpu_relax();
997
998 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
999 dev_err(mmc_dev(host->mmc),
1000 "Timeout waiting on controller reset in %s\n",
1001 __func__);
1002}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001003
Balaji T K25e18972012-11-19 21:59:55 +05301004static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1005 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301006{
Balaji T K25e18972012-11-19 21:59:55 +05301007 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301008 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301009 if (host->cmd)
1010 host->cmd->error = err;
1011 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301012
1013 if (host->data) {
1014 omap_hsmmc_reset_controller_fsm(host, SRD);
1015 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301016 } else if (host->mrq && host->mrq->cmd)
1017 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301018}
1019
Adrian Hunterb4175772010-05-26 14:42:06 -07001020static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001021{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001022 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001023 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001024
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001025 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301026 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001027
Venkatraman Sa7e96872012-11-19 22:00:01 +05301028 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001029 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001030
Venkatraman Sa7e96872012-11-19 22:00:01 +05301031 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301032 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301033 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301034 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301035 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301036 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1037
Venkatraman Sae4bf782012-08-09 20:36:07 +05301038 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301039 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301040 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001041 }
1042 }
1043
Venkatraman Sa7e96872012-11-19 22:00:01 +05301044 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001045 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301046 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001047 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001048}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001049
Adrian Hunterb4175772010-05-26 14:42:06 -07001050/*
1051 * MMC controller IRQ handler
1052 */
1053static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1054{
1055 struct omap_hsmmc_host *host = dev_id;
1056 int status;
1057
1058 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301059 while (status & INT_EN_MASK && host->req_in_progress) {
Adrian Hunterb4175772010-05-26 14:42:06 -07001060 omap_hsmmc_do_irq(host, status);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301061
Adrian Hunterb4175772010-05-26 14:42:06 -07001062 /* Flush posted write */
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301063 OMAP_HSMMC_WRITE(host->base, STAT, status);
Adrian Hunterb4175772010-05-26 14:42:06 -07001064 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301065 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001066
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001067 return IRQ_HANDLED;
1068}
1069
Denis Karpov70a33412009-09-22 16:44:59 -07001070static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001071{
1072 unsigned long i;
1073
1074 OMAP_HSMMC_WRITE(host->base, HCTL,
1075 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1076 for (i = 0; i < loops_per_jiffy; i++) {
1077 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1078 break;
1079 cpu_relax();
1080 }
1081}
1082
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001083/*
David Brownelleb250822009-02-17 14:49:01 -08001084 * Switch MMC interface voltage ... only relevant for MMC1.
1085 *
1086 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1087 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1088 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001089 */
Denis Karpov70a33412009-09-22 16:44:59 -07001090static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001091{
1092 u32 reg_val = 0;
1093 int ret;
1094
1095 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301096 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301097 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301098 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001099
1100 /* Turn the power off */
1101 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001102
1103 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001104 if (!ret)
1105 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1106 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301107 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301108 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301109 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001110
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001111 if (ret != 0)
1112 goto err;
1113
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001114 OMAP_HSMMC_WRITE(host->base, HCTL,
1115 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1116 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001117
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001118 /*
1119 * If a MMC dual voltage card is detected, the set_ios fn calls
1120 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001121 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001122 *
David Brownelleb250822009-02-17 14:49:01 -08001123 * Cope with a bit of slop in the range ... per data sheets:
1124 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1125 * but recommended values are 1.71V to 1.89V
1126 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1127 * but recommended values are 2.7V to 3.3V
1128 *
1129 * Board setup code shouldn't permit anything very out-of-range.
1130 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1131 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001132 */
David Brownelleb250822009-02-17 14:49:01 -08001133 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001134 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001135 else
1136 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001137
1138 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001139 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001140
1141 return 0;
1142err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301143 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001144 return ret;
1145}
1146
Adrian Hunterb62f6222009-09-22 16:45:01 -07001147/* Protect the card while the cover is open */
1148static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1149{
1150 if (!mmc_slot(host).get_cover_state)
1151 return;
1152
1153 host->reqs_blocked = 0;
1154 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1155 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301156 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001157 "card is now accessible\n",
1158 mmc_hostname(host->mmc));
1159 host->protect_card = 0;
1160 }
1161 } else {
1162 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301163 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001164 "card is now inaccessible\n",
1165 mmc_hostname(host->mmc));
1166 host->protect_card = 1;
1167 }
1168 }
1169}
1170
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001171/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001172 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001173 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001174static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001175{
NeilBrown7efab4f2011-12-30 12:35:13 +11001176 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001177 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001178 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001179
Adrian Huntera6b22402009-09-22 16:44:45 -07001180 if (host->suspended)
NeilBrown7efab4f2011-12-30 12:35:13 +11001181 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001182
1183 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001184
Denis Karpov191d1f12009-09-22 16:44:55 -07001185 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001186 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001187 else {
1188 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001189 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001190 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001191
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001192 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001193 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001194 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001195 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001196 return IRQ_HANDLED;
1197}
1198
Russell Kingc5c98922012-04-13 12:14:39 +01001199static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001200{
Russell Kingc5c98922012-04-13 12:14:39 +01001201 struct omap_hsmmc_host *host = param;
1202 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001203 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001204 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001205
Russell Kingc5c98922012-04-13 12:14:39 +01001206 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001207 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001208 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001209 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001210 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001211
Adrian Hunter770d7432011-05-06 12:14:11 +03001212 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001213 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001214 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001215 dma_unmap_sg(chan->device->dev,
1216 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001217 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001218
1219 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001220 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001221 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001222
1223 /* If DMA has finished after TC, complete the request */
1224 if (!req_in_progress) {
1225 struct mmc_request *mrq = host->mrq;
1226
1227 host->mrq = NULL;
1228 mmc_request_done(host->mmc, mrq);
1229 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001230}
1231
Per Forlin9782aff2011-07-01 18:55:23 +02001232static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1233 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001234 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001235 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001236{
1237 int dma_len;
1238
1239 if (!next && data->host_cookie &&
1240 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301241 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001242 " host->next_data.cookie %d\n",
1243 __func__, data->host_cookie, host->next_data.cookie);
1244 data->host_cookie = 0;
1245 }
1246
1247 /* Check if next job is already prepared */
1248 if (next ||
1249 (!next && data->host_cookie != host->next_data.cookie)) {
Russell King26b88522012-04-13 12:27:37 +01001250 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001251 omap_hsmmc_get_dma_dir(host, data));
1252
1253 } else {
1254 dma_len = host->next_data.dma_len;
1255 host->next_data.dma_len = 0;
1256 }
1257
1258
1259 if (dma_len == 0)
1260 return -EINVAL;
1261
1262 if (next) {
1263 next->dma_len = dma_len;
1264 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1265 } else
1266 host->dma_len = dma_len;
1267
1268 return 0;
1269}
1270
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001271/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001272 * Routine to configure and start DMA for the MMC card
1273 */
Denis Karpov70a33412009-09-22 16:44:59 -07001274static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1275 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001276{
Russell King26b88522012-04-13 12:27:37 +01001277 struct dma_slave_config cfg;
1278 struct dma_async_tx_descriptor *tx;
1279 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001280 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001281 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001282
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001283 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001284 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001285 struct scatterlist *sgl;
1286
1287 sgl = data->sg + i;
1288 if (sgl->length % data->blksz)
1289 return -EINVAL;
1290 }
1291 if ((data->blksz % 4) != 0)
1292 /* REVISIT: The MMC buffer increments only when MSB is written.
1293 * Return error for blksz which is non multiple of four.
1294 */
1295 return -EINVAL;
1296
Adrian Hunterb4175772010-05-26 14:42:06 -07001297 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001298
Russell Kingc5c98922012-04-13 12:14:39 +01001299 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001300
Russell King26b88522012-04-13 12:27:37 +01001301 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1302 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1303 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1304 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1305 cfg.src_maxburst = data->blksz / 4;
1306 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001307
Russell King26b88522012-04-13 12:27:37 +01001308 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001309 if (ret)
1310 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001311
Russell King26b88522012-04-13 12:27:37 +01001312 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1313 if (ret)
1314 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001315
Russell King26b88522012-04-13 12:27:37 +01001316 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1317 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1318 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1319 if (!tx) {
1320 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1321 /* FIXME: cleanup */
1322 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001323 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001324
Russell King26b88522012-04-13 12:27:37 +01001325 tx->callback = omap_hsmmc_dma_callback;
1326 tx->callback_param = host;
1327
1328 /* Does not fail */
1329 dmaengine_submit(tx);
1330
1331 host->dma_ch = 1;
1332
1333 dma_async_issue_pending(chan);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001334
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001335 return 0;
1336}
1337
Denis Karpov70a33412009-09-22 16:44:59 -07001338static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001339 unsigned int timeout_ns,
1340 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001341{
1342 unsigned int timeout, cycle_ns;
1343 uint32_t reg, clkd, dto = 0;
1344
1345 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1346 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1347 if (clkd == 0)
1348 clkd = 1;
1349
1350 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001351 timeout = timeout_ns / cycle_ns;
1352 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001353 if (timeout) {
1354 while ((timeout & 0x80000000) == 0) {
1355 dto += 1;
1356 timeout <<= 1;
1357 }
1358 dto = 31 - dto;
1359 timeout <<= 1;
1360 if (timeout && dto)
1361 dto += 1;
1362 if (dto >= 13)
1363 dto -= 13;
1364 else
1365 dto = 0;
1366 if (dto > 14)
1367 dto = 14;
1368 }
1369
1370 reg &= ~DTO_MASK;
1371 reg |= dto << DTO_SHIFT;
1372 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1373}
1374
1375/*
1376 * Configure block length for MMC/SD cards and initiate the transfer.
1377 */
1378static int
Denis Karpov70a33412009-09-22 16:44:59 -07001379omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001380{
1381 int ret;
1382 host->data = req->data;
1383
1384 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001385 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001386 /*
1387 * Set an arbitrary 100ms data timeout for commands with
1388 * busy signal.
1389 */
1390 if (req->cmd->flags & MMC_RSP_BUSY)
1391 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001392 return 0;
1393 }
1394
1395 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1396 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001397 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001398
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001399 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001400 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001401 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301402 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001403 return ret;
1404 }
1405 }
1406 return 0;
1407}
1408
Per Forlin9782aff2011-07-01 18:55:23 +02001409static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1410 int err)
1411{
1412 struct omap_hsmmc_host *host = mmc_priv(mmc);
1413 struct mmc_data *data = mrq->data;
1414
Russell King26b88522012-04-13 12:27:37 +01001415 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001416 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001417
Russell King26b88522012-04-13 12:27:37 +01001418 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1419 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001420 data->host_cookie = 0;
1421 }
1422}
1423
1424static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1425 bool is_first_req)
1426{
1427 struct omap_hsmmc_host *host = mmc_priv(mmc);
1428
1429 if (mrq->data->host_cookie) {
1430 mrq->data->host_cookie = 0;
1431 return ;
1432 }
1433
Russell Kingc5c98922012-04-13 12:14:39 +01001434 if (host->use_dma) {
1435 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001436
Per Forlin9782aff2011-07-01 18:55:23 +02001437 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001438 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001439 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001440 }
Per Forlin9782aff2011-07-01 18:55:23 +02001441}
1442
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001443/*
1444 * Request function. for read/write operation
1445 */
Denis Karpov70a33412009-09-22 16:44:59 -07001446static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001447{
Denis Karpov70a33412009-09-22 16:44:59 -07001448 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001449 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001450
Adrian Hunterb4175772010-05-26 14:42:06 -07001451 BUG_ON(host->req_in_progress);
1452 BUG_ON(host->dma_ch != -1);
1453 if (host->protect_card) {
1454 if (host->reqs_blocked < 3) {
1455 /*
1456 * Ensure the controller is left in a consistent
1457 * state by resetting the command and data state
1458 * machines.
1459 */
1460 omap_hsmmc_reset_controller_fsm(host, SRD);
1461 omap_hsmmc_reset_controller_fsm(host, SRC);
1462 host->reqs_blocked += 1;
1463 }
1464 req->cmd->error = -EBADF;
1465 if (req->data)
1466 req->data->error = -EBADF;
1467 req->cmd->retries = 0;
1468 mmc_request_done(mmc, req);
1469 return;
1470 } else if (host->reqs_blocked)
1471 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001472 WARN_ON(host->mrq != NULL);
1473 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001474 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001475 if (err) {
1476 req->cmd->error = err;
1477 if (req->data)
1478 req->data->error = err;
1479 host->mrq = NULL;
1480 mmc_request_done(mmc, req);
1481 return;
1482 }
1483
Denis Karpov70a33412009-09-22 16:44:59 -07001484 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001485}
1486
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001487/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001488static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001489{
Denis Karpov70a33412009-09-22 16:44:59 -07001490 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001491 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001492
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301493 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001494
Adrian Huntera3621462009-09-22 16:44:42 -07001495 if (ios->power_mode != host->power_mode) {
1496 switch (ios->power_mode) {
1497 case MMC_POWER_OFF:
1498 mmc_slot(host).set_power(host->dev, host->slot_id,
1499 0, 0);
1500 break;
1501 case MMC_POWER_UP:
1502 mmc_slot(host).set_power(host->dev, host->slot_id,
1503 1, ios->vdd);
1504 break;
1505 case MMC_POWER_ON:
1506 do_send_init_stream = 1;
1507 break;
1508 }
1509 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001510 }
1511
Denis Karpovdd498ef2009-09-22 16:44:49 -07001512 /* FIXME: set registers based only on changes to ios */
1513
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001514 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001515
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301516 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001517 /* Only MMC1 can interface at 3V without some flavor
1518 * of external transceiver; but they all handle 1.8V.
1519 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001520 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Rajendra Nayak1f84b712012-03-12 20:32:38 +05301521 (ios->vdd == DUAL_VOLT_OCR_BIT) &&
1522 /*
1523 * With pbias cell programming missing, this
Tony Lindgrencf5ae402013-05-10 17:42:33 +05301524 * can't be allowed on MMC1 when booting with device
Rajendra Nayak1f84b712012-03-12 20:32:38 +05301525 * tree.
1526 */
Tony Lindgrencf5ae402013-05-10 17:42:33 +05301527 !host->pbias_disable) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001528 /*
1529 * The mmc_select_voltage fn of the core does
1530 * not seem to set the power_mode to
1531 * MMC_POWER_UP upon recalculating the voltage.
1532 * vdd 1.8v.
1533 */
Denis Karpov70a33412009-09-22 16:44:59 -07001534 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1535 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001536 "Switch operation failed\n");
1537 }
1538 }
1539
Andy Shevchenko5934df22011-05-06 12:14:06 +03001540 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001541
Adrian Huntera3621462009-09-22 16:44:42 -07001542 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001543 send_init_stream(host);
1544
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001545 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001546
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301547 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001548}
1549
1550static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1551{
Denis Karpov70a33412009-09-22 16:44:59 -07001552 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001553
Denis Karpov191d1f12009-09-22 16:44:55 -07001554 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001555 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001556 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001557}
1558
1559static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1560{
Denis Karpov70a33412009-09-22 16:44:59 -07001561 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001562
Denis Karpov191d1f12009-09-22 16:44:55 -07001563 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001564 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001565 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001566}
1567
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001568static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1569{
1570 struct omap_hsmmc_host *host = mmc_priv(mmc);
1571
1572 if (mmc_slot(host).init_card)
1573 mmc_slot(host).init_card(card);
1574}
1575
Denis Karpov70a33412009-09-22 16:44:59 -07001576static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001577{
1578 u32 hctl, capa, value;
1579
1580 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301581 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001582 hctl = SDVS30;
1583 capa = VS30 | VS18;
1584 } else {
1585 hctl = SDVS18;
1586 capa = VS18;
1587 }
1588
1589 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1590 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1591
1592 value = OMAP_HSMMC_READ(host->base, CAPA);
1593 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1594
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001595 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001596 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001597}
1598
Denis Karpov70a33412009-09-22 16:44:59 -07001599static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001600{
Denis Karpov70a33412009-09-22 16:44:59 -07001601 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001602
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301603 pm_runtime_get_sync(host->dev);
1604
Denis Karpovdd498ef2009-09-22 16:44:49 -07001605 return 0;
1606}
1607
Adrian Hunter907d2e72012-02-29 09:17:21 +02001608static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001609{
Denis Karpov70a33412009-09-22 16:44:59 -07001610 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001611
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301612 pm_runtime_mark_last_busy(host->dev);
1613 pm_runtime_put_autosuspend(host->dev);
1614
Denis Karpovdd498ef2009-09-22 16:44:49 -07001615 return 0;
1616}
1617
Denis Karpov70a33412009-09-22 16:44:59 -07001618static const struct mmc_host_ops omap_hsmmc_ops = {
1619 .enable = omap_hsmmc_enable_fclk,
1620 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001621 .post_req = omap_hsmmc_post_req,
1622 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001623 .request = omap_hsmmc_request,
1624 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001625 .get_cd = omap_hsmmc_get_cd,
1626 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001627 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001628 /* NYET -- enable_sdio_irq */
1629};
1630
Denis Karpovd900f712009-09-22 16:44:38 -07001631#ifdef CONFIG_DEBUG_FS
1632
Denis Karpov70a33412009-09-22 16:44:59 -07001633static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001634{
1635 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001636 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001637 int context_loss = 0;
1638
Denis Karpov70a33412009-09-22 16:44:59 -07001639 if (host->pdata->get_context_loss_count)
1640 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001641
Adrian Hunter907d2e72012-02-29 09:17:21 +02001642 seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1643 mmc->index, host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001644
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301645 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001646 seq_printf(s, "host suspended, can't read registers\n");
1647 return 0;
1648 }
1649
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301650 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001651
Denis Karpovd900f712009-09-22 16:44:38 -07001652 seq_printf(s, "CON:\t\t0x%08x\n",
1653 OMAP_HSMMC_READ(host->base, CON));
1654 seq_printf(s, "HCTL:\t\t0x%08x\n",
1655 OMAP_HSMMC_READ(host->base, HCTL));
1656 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1657 OMAP_HSMMC_READ(host->base, SYSCTL));
1658 seq_printf(s, "IE:\t\t0x%08x\n",
1659 OMAP_HSMMC_READ(host->base, IE));
1660 seq_printf(s, "ISE:\t\t0x%08x\n",
1661 OMAP_HSMMC_READ(host->base, ISE));
1662 seq_printf(s, "CAPA:\t\t0x%08x\n",
1663 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001664
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301665 pm_runtime_mark_last_busy(host->dev);
1666 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001667
Denis Karpovd900f712009-09-22 16:44:38 -07001668 return 0;
1669}
1670
Denis Karpov70a33412009-09-22 16:44:59 -07001671static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001672{
Denis Karpov70a33412009-09-22 16:44:59 -07001673 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001674}
1675
1676static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001677 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001678 .read = seq_read,
1679 .llseek = seq_lseek,
1680 .release = single_release,
1681};
1682
Denis Karpov70a33412009-09-22 16:44:59 -07001683static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001684{
1685 if (mmc->debugfs_root)
1686 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1687 mmc, &mmc_regs_fops);
1688}
1689
1690#else
1691
Denis Karpov70a33412009-09-22 16:44:59 -07001692static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001693{
1694}
1695
1696#endif
1697
Rajendra Nayak46856a62012-03-12 20:32:37 +05301698#ifdef CONFIG_OF
1699static u16 omap4_reg_offset = 0x100;
1700
1701static const struct of_device_id omap_mmc_of_match[] = {
1702 {
1703 .compatible = "ti,omap2-hsmmc",
1704 },
1705 {
1706 .compatible = "ti,omap3-hsmmc",
1707 },
1708 {
1709 .compatible = "ti,omap4-hsmmc",
1710 .data = &omap4_reg_offset,
1711 },
1712 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001713};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301714MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1715
1716static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1717{
1718 struct omap_mmc_platform_data *pdata;
1719 struct device_node *np = dev->of_node;
Daniel Mackd8714e82012-10-15 21:35:06 +05301720 u32 bus_width, max_freq;
Jan Luebbedc642c22013-01-30 10:07:17 +01001721 int cd_gpio, wp_gpio;
1722
1723 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
1724 wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1725 if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
1726 return ERR_PTR(-EPROBE_DEFER);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301727
1728 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1729 if (!pdata)
1730 return NULL; /* out of memory */
1731
1732 if (of_find_property(np, "ti,dual-volt", NULL))
1733 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1734
1735 /* This driver only supports 1 slot */
1736 pdata->nr_slots = 1;
Jan Luebbedc642c22013-01-30 10:07:17 +01001737 pdata->slots[0].switch_pin = cd_gpio;
1738 pdata->slots[0].gpio_wp = wp_gpio;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301739
1740 if (of_find_property(np, "ti,non-removable", NULL)) {
1741 pdata->slots[0].nonremovable = true;
1742 pdata->slots[0].no_regulator_off_init = true;
1743 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001744 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301745 if (bus_width == 4)
1746 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1747 else if (bus_width == 8)
1748 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1749
1750 if (of_find_property(np, "ti,needs-special-reset", NULL))
1751 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1752
Daniel Mackd8714e82012-10-15 21:35:06 +05301753 if (!of_property_read_u32(np, "max-frequency", &max_freq))
1754 pdata->max_freq = max_freq;
1755
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301756 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
1757 pdata->slots[0].features |= HSMMC_HAS_HSPE_SUPPORT;
1758
Rajendra Nayak46856a62012-03-12 20:32:37 +05301759 return pdata;
1760}
1761#else
1762static inline struct omap_mmc_platform_data
1763 *of_get_hsmmc_pdata(struct device *dev)
1764{
1765 return NULL;
1766}
1767#endif
1768
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001769static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001770{
1771 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1772 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001773 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001774 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001775 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301776 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001777 dma_cap_mask_t mask;
1778 unsigned tx_req, rx_req;
Daniel Mack46b76032012-10-15 21:35:05 +05301779 struct pinctrl *pinctrl;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301780
1781 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1782 if (match) {
1783 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01001784
1785 if (IS_ERR(pdata))
1786 return PTR_ERR(pdata);
1787
Rajendra Nayak46856a62012-03-12 20:32:37 +05301788 if (match->data) {
Uwe Kleine-Königefc9b732012-05-21 21:57:39 +02001789 const u16 *offsetp = match->data;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301790 pdata->reg_offset = *offsetp;
1791 }
1792 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001793
1794 if (pdata == NULL) {
1795 dev_err(&pdev->dev, "Platform Data is missing\n");
1796 return -ENXIO;
1797 }
1798
1799 if (pdata->nr_slots == 0) {
1800 dev_err(&pdev->dev, "No Slots\n");
1801 return -ENXIO;
1802 }
1803
1804 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1805 irq = platform_get_irq(pdev, 0);
1806 if (res == NULL || irq < 0)
1807 return -ENXIO;
1808
Chris Ball984b2032011-03-22 16:34:42 -07001809 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001810 if (res == NULL)
1811 return -EBUSY;
1812
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001813 ret = omap_hsmmc_gpio_init(pdata);
1814 if (ret)
1815 goto err;
1816
Denis Karpov70a33412009-09-22 16:44:59 -07001817 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001818 if (!mmc) {
1819 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001820 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001821 }
1822
1823 host = mmc_priv(mmc);
1824 host->mmc = mmc;
1825 host->pdata = pdata;
1826 host->dev = &pdev->dev;
1827 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001828 host->dma_ch = -1;
1829 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001830 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05301831 host->mapbase = res->start + pdata->reg_offset;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001832 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001833 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001834 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001835
1836 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001837
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301838 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001839
Adrian Huntere0eb2422010-02-15 10:03:34 -08001840 /*
1841 * If regulator_disable can only put vcc_aux to sleep then there is
1842 * no off state.
1843 */
1844 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1845 mmc_slot(host).no_off = 1;
1846
Daniel Mackd418ed82012-02-19 13:20:33 +01001847 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1848
1849 if (pdata->max_freq > 0)
1850 mmc->f_max = pdata->max_freq;
1851 else
1852 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001853
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001854 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001855
Russell King6f7607c2009-01-28 10:22:50 +00001856 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001857 if (IS_ERR(host->fclk)) {
1858 ret = PTR_ERR(host->fclk);
1859 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001860 goto err1;
1861 }
1862
Paul Walmsley9b682562011-10-06 14:50:35 -06001863 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1864 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1865 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1866 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001867
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301868 pm_runtime_enable(host->dev);
1869 pm_runtime_get_sync(host->dev);
1870 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1871 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001872
Balaji T K92a3aeb2012-02-24 21:14:34 +05301873 omap_hsmmc_context_save(host);
1874
Tony Lindgrencf5ae402013-05-10 17:42:33 +05301875 /* This can be removed once we support PBIAS with DT */
1876 if (host->dev->of_node && host->mapbase == 0x4809c000)
1877 host->pbias_disable = 1;
1878
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301879 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1880 /*
1881 * MMC can still work without debounce clock.
1882 */
1883 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301884 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05301885 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301886 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
1887 clk_put(host->dbclk);
1888 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07001889 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001890
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001891 /* Since we do only SG emulation, we can have as many segs
1892 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001893 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001894
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001895 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1896 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1897 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1898 mmc->max_seg_size = mmc->max_req_size;
1899
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001900 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001901 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001902
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001903 mmc->caps |= mmc_slot(host).caps;
1904 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001905 mmc->caps |= MMC_CAP_4_BIT_DATA;
1906
Denis Karpov191d1f12009-09-22 16:44:55 -07001907 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001908 mmc->caps |= MMC_CAP_NONREMOVABLE;
1909
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001910 mmc->pm_caps = mmc_slot(host).pm_caps;
1911
Denis Karpov70a33412009-09-22 16:44:59 -07001912 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001913
Balaji T Kb7bf7732012-03-07 09:55:30 -05001914 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1915 if (!res) {
1916 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
Kevin Hilman9c17d082012-07-10 16:40:56 -07001917 ret = -ENXIO;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001918 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001919 }
Russell King26b88522012-04-13 12:27:37 +01001920 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001921
1922 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1923 if (!res) {
1924 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
Kevin Hilman9c17d082012-07-10 16:40:56 -07001925 ret = -ENXIO;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001926 goto err_irq;
1927 }
Russell King26b88522012-04-13 12:27:37 +01001928 rx_req = res->start;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001929
Russell King26b88522012-04-13 12:27:37 +01001930 dma_cap_zero(mask);
1931 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01001932
Matt Porterd272fbf2013-05-10 17:42:34 +05301933 host->rx_chan =
1934 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
1935 &rx_req, &pdev->dev, "rx");
1936
Russell King26b88522012-04-13 12:27:37 +01001937 if (!host->rx_chan) {
1938 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001939 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001940 goto err_irq;
1941 }
1942
Matt Porterd272fbf2013-05-10 17:42:34 +05301943 host->tx_chan =
1944 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
1945 &tx_req, &pdev->dev, "tx");
1946
Russell King26b88522012-04-13 12:27:37 +01001947 if (!host->tx_chan) {
1948 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001949 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001950 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01001951 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001952
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001953 /* Request IRQ for MMC operations */
Yong Zhangd9618e92011-09-22 16:59:04 +08001954 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001955 mmc_hostname(mmc), host);
1956 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301957 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001958 goto err_irq;
1959 }
1960
1961 if (pdata->init != NULL) {
1962 if (pdata->init(&pdev->dev) != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301963 dev_err(mmc_dev(host->mmc),
Denis Karpov70a33412009-09-22 16:44:59 -07001964 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001965 goto err_irq_cd_init;
1966 }
1967 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001968
Adrian Hunterb702b102010-02-15 10:03:35 -08001969 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001970 ret = omap_hsmmc_reg_get(host);
1971 if (ret)
1972 goto err_reg;
1973 host->use_reg = 1;
1974 }
1975
David Brownellb583f262009-05-28 14:04:03 -07001976 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001977
1978 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001979 if ((mmc_slot(host).card_detect_irq)) {
NeilBrown7efab4f2011-12-30 12:35:13 +11001980 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1981 NULL,
1982 omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08001983 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11001984 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001985 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301986 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001987 "Unable to grab MMC CD IRQ\n");
1988 goto err_irq_cd;
1989 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00001990 pdata->suspend = omap_hsmmc_suspend_cdirq;
1991 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001992 }
1993
Adrian Hunterb4175772010-05-26 14:42:06 -07001994 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001995
Daniel Mack46b76032012-10-15 21:35:05 +05301996 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1997 if (IS_ERR(pinctrl))
1998 dev_warn(&pdev->dev,
1999 "pins are not configured from the driver\n");
2000
Adrian Hunterb62f6222009-09-22 16:45:01 -07002001 omap_hsmmc_protect_card(host);
2002
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002003 mmc_add_host(mmc);
2004
Denis Karpov191d1f12009-09-22 16:44:55 -07002005 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002006 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2007 if (ret < 0)
2008 goto err_slot_name;
2009 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002010 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002011 ret = device_create_file(&mmc->class_dev,
2012 &dev_attr_cover_switch);
2013 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002014 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002015 }
2016
Denis Karpov70a33412009-09-22 16:44:59 -07002017 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302018 pm_runtime_mark_last_busy(host->dev);
2019 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002020
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002021 return 0;
2022
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002023err_slot_name:
2024 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002025 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002026err_irq_cd:
2027 if (host->use_reg)
2028 omap_hsmmc_reg_put(host);
2029err_reg:
2030 if (host->pdata->cleanup)
2031 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002032err_irq_cd_init:
2033 free_irq(host->irq, host);
2034err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002035 if (host->tx_chan)
2036 dma_release_channel(host->tx_chan);
2037 if (host->rx_chan)
2038 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302039 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002040 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002041 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302042 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302043 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002044 clk_put(host->dbclk);
2045 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002046err1:
2047 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002048 platform_set_drvdata(pdev, NULL);
2049 mmc_free_host(mmc);
2050err_alloc:
2051 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002052err:
Russell King48b332f2012-04-18 11:11:57 +01002053 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2054 if (res)
2055 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002056 return ret;
2057}
2058
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002059static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002060{
Denis Karpov70a33412009-09-22 16:44:59 -07002061 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002062 struct resource *res;
2063
Felipe Balbi927ce942012-03-14 11:18:27 +02002064 pm_runtime_get_sync(host->dev);
2065 mmc_remove_host(host->mmc);
2066 if (host->use_reg)
2067 omap_hsmmc_reg_put(host);
2068 if (host->pdata->cleanup)
2069 host->pdata->cleanup(&pdev->dev);
2070 free_irq(host->irq, host);
2071 if (mmc_slot(host).card_detect_irq)
2072 free_irq(mmc_slot(host).card_detect_irq, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002073
Russell Kingc5c98922012-04-13 12:14:39 +01002074 if (host->tx_chan)
2075 dma_release_channel(host->tx_chan);
2076 if (host->rx_chan)
2077 dma_release_channel(host->rx_chan);
2078
Felipe Balbi927ce942012-03-14 11:18:27 +02002079 pm_runtime_put_sync(host->dev);
2080 pm_runtime_disable(host->dev);
2081 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302082 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302083 clk_disable_unprepare(host->dbclk);
Felipe Balbi927ce942012-03-14 11:18:27 +02002084 clk_put(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002085 }
2086
Balaji T K9ea28ec2012-10-15 21:35:08 +05302087 omap_hsmmc_gpio_free(host->pdata);
Felipe Balbi927ce942012-03-14 11:18:27 +02002088 iounmap(host->base);
Balaji T K9d1f0282012-10-15 21:35:07 +05302089 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002090
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002091 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2092 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002093 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002094 platform_set_drvdata(pdev, NULL);
2095
2096 return 0;
2097}
2098
2099#ifdef CONFIG_PM
Felipe Balbia48ce882012-11-19 21:59:59 +05302100static int omap_hsmmc_prepare(struct device *dev)
2101{
2102 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2103
2104 if (host->pdata->suspend)
2105 return host->pdata->suspend(dev, host->slot_id);
2106
2107 return 0;
2108}
2109
2110static void omap_hsmmc_complete(struct device *dev)
2111{
2112 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2113
2114 if (host->pdata->resume)
2115 host->pdata->resume(dev, host->slot_id);
2116
2117}
2118
Kevin Hilmana791daa2010-05-26 14:42:07 -07002119static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002120{
2121 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002122 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2123
2124 if (!host)
2125 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002126
2127 if (host && host->suspended)
2128 return 0;
2129
Felipe Balbi927ce942012-03-14 11:18:27 +02002130 pm_runtime_get_sync(host->dev);
2131 host->suspended = 1;
Felipe Balbi927ce942012-03-14 11:18:27 +02002132 ret = mmc_suspend_host(host->mmc);
2133
2134 if (ret) {
2135 host->suspended = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002136 goto err;
2137 }
2138
2139 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2140 omap_hsmmc_disable_irq(host);
2141 OMAP_HSMMC_WRITE(host->base, HCTL,
2142 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2143 }
2144
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302145 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302146 clk_disable_unprepare(host->dbclk);
Eliad Peller31f9d462011-11-22 16:02:17 +02002147err:
2148 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002149 return ret;
2150}
2151
2152/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002153static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002154{
2155 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002156 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2157
2158 if (!host)
2159 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002160
2161 if (host && !host->suspended)
2162 return 0;
2163
Felipe Balbi927ce942012-03-14 11:18:27 +02002164 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002165
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302166 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302167 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002168
Felipe Balbi927ce942012-03-14 11:18:27 +02002169 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2170 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002171
Felipe Balbi927ce942012-03-14 11:18:27 +02002172 omap_hsmmc_protect_card(host);
2173
2174 /* Notify the core to resume the host */
2175 ret = mmc_resume_host(host->mmc);
2176 if (ret == 0)
2177 host->suspended = 0;
2178
2179 pm_runtime_mark_last_busy(host->dev);
2180 pm_runtime_put_autosuspend(host->dev);
2181
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002182 return ret;
2183
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002184}
2185
2186#else
Felipe Balbia48ce882012-11-19 21:59:59 +05302187#define omap_hsmmc_prepare NULL
2188#define omap_hsmmc_complete NULL
Denis Karpov70a33412009-09-22 16:44:59 -07002189#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302190#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002191#endif
2192
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302193static int omap_hsmmc_runtime_suspend(struct device *dev)
2194{
2195 struct omap_hsmmc_host *host;
2196
2197 host = platform_get_drvdata(to_platform_device(dev));
2198 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002199 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302200
2201 return 0;
2202}
2203
2204static int omap_hsmmc_runtime_resume(struct device *dev)
2205{
2206 struct omap_hsmmc_host *host;
2207
2208 host = platform_get_drvdata(to_platform_device(dev));
2209 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002210 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302211
2212 return 0;
2213}
2214
Kevin Hilmana791daa2010-05-26 14:42:07 -07002215static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002216 .suspend = omap_hsmmc_suspend,
2217 .resume = omap_hsmmc_resume,
Felipe Balbia48ce882012-11-19 21:59:59 +05302218 .prepare = omap_hsmmc_prepare,
2219 .complete = omap_hsmmc_complete,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302220 .runtime_suspend = omap_hsmmc_runtime_suspend,
2221 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002222};
2223
2224static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002225 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002226 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002227 .driver = {
2228 .name = DRIVER_NAME,
2229 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002230 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302231 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002232 },
2233};
2234
Felipe Balbib7964502012-03-14 11:18:32 +02002235module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002236MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2237MODULE_LICENSE("GPL");
2238MODULE_ALIAS("platform:" DRIVER_NAME);
2239MODULE_AUTHOR("Texas Instruments Inc");