blob: da4f5a73a3247715a5b28c42739f2c8976885482 [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Andy Shevchenkoac330f442011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
Russell Kingc5c98922012-04-13 12:14:39 +010022#include <linux/dmaengine.h>
Denis Karpovd900f712009-09-22 16:44:38 -070023#include <linux/seq_file.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010024#include <linux/interrupt.h>
25#include <linux/delay.h>
26#include <linux/dma-mapping.h>
27#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010028#include <linux/timer.h>
29#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053030#include <linux/of.h>
31#include <linux/of_gpio.h>
32#include <linux/of_device.h>
Russell King3451c062012-04-21 22:35:42 +010033#include <linux/omap-dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010034#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070035#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070036#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010037#include <linux/io.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080038#include <linux/gpio.h>
39#include <linux/regulator/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053040#include <linux/pm_runtime.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010041#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070042#include <plat/board.h>
43#include <plat/mmc.h>
44#include <plat/cpu.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010045
46/* OMAP HSMMC Host Controller Registers */
47#define OMAP_HSMMC_SYSCONFIG 0x0010
Denis Karpov11dd62a2009-09-22 16:44:43 -070048#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010049#define OMAP_HSMMC_CON 0x002C
50#define OMAP_HSMMC_BLK 0x0104
51#define OMAP_HSMMC_ARG 0x0108
52#define OMAP_HSMMC_CMD 0x010C
53#define OMAP_HSMMC_RSP10 0x0110
54#define OMAP_HSMMC_RSP32 0x0114
55#define OMAP_HSMMC_RSP54 0x0118
56#define OMAP_HSMMC_RSP76 0x011C
57#define OMAP_HSMMC_DATA 0x0120
58#define OMAP_HSMMC_HCTL 0x0128
59#define OMAP_HSMMC_SYSCTL 0x012C
60#define OMAP_HSMMC_STAT 0x0130
61#define OMAP_HSMMC_IE 0x0134
62#define OMAP_HSMMC_ISE 0x0138
63#define OMAP_HSMMC_CAPA 0x0140
64
65#define VS18 (1 << 26)
66#define VS30 (1 << 25)
67#define SDVS18 (0x5 << 9)
68#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080069#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010070#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010071#define SDVSCLR 0xFFFFF1FF
72#define SDVSDET 0x00000400
73#define AUTOIDLE 0x1
74#define SDBP (1 << 8)
75#define DTO 0xe
76#define ICE 0x1
77#define ICS 0x2
78#define CEN (1 << 2)
79#define CLKD_MASK 0x0000FFC0
80#define CLKD_SHIFT 6
81#define DTO_MASK 0x000F0000
82#define DTO_SHIFT 16
83#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070084#define BWR_ENABLE (1 << 4)
85#define BRR_ENABLE (1 << 5)
Adrian Hunter93caf8e692010-08-11 14:17:48 -070086#define DTO_ENABLE (1 << 20)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010087#define INIT_STREAM (1 << 1)
88#define DP_SELECT (1 << 21)
89#define DDIR (1 << 4)
90#define DMA_EN 0x1
91#define MSBS (1 << 5)
92#define BCE (1 << 1)
93#define FOUR_BIT (1 << 1)
Balaji T K03b5d922012-04-09 12:08:33 +053094#define DDR (1 << 19)
Jarkko Lavinen73153012008-11-21 16:49:54 +020095#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010096#define CC 0x1
97#define TC 0x02
98#define OD 0x1
99#define ERR (1 << 15)
100#define CMD_TIMEOUT (1 << 16)
101#define DATA_TIMEOUT (1 << 20)
102#define CMD_CRC (1 << 17)
103#define DATA_CRC (1 << 21)
104#define CARD_ERR (1 << 28)
105#define STAT_CLEAR 0xFFFFFFFF
106#define INIT_STREAM_CMD 0x00000000
107#define DUAL_VOLT_OCR_BIT 7
108#define SRC (1 << 25)
109#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700110#define SOFTRESET (1 << 1)
111#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100112
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530113#define MMC_AUTOSUSPEND_DELAY 100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100114#define MMC_TIMEOUT_MS 20
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400115#define OMAP_MMC_MIN_CLOCK 400000
116#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530117#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100118
119/*
120 * One controller can have multiple slots, like on some omap boards using
121 * omap.c controller driver. Luckily this is not currently done on any known
122 * omap_hsmmc.c device.
123 */
124#define mmc_slot(host) (host->pdata->slots[host->slot_id])
125
126/*
127 * MMC Host controller read/write API's
128 */
129#define OMAP_HSMMC_READ(base, reg) \
130 __raw_readl((base) + OMAP_HSMMC_##reg)
131
132#define OMAP_HSMMC_WRITE(base, reg, val) \
133 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
134
Per Forlin9782aff2011-07-01 18:55:23 +0200135struct omap_hsmmc_next {
136 unsigned int dma_len;
137 s32 cookie;
138};
139
Denis Karpov70a33412009-09-22 16:44:59 -0700140struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100141 struct device *dev;
142 struct mmc_host *mmc;
143 struct mmc_request *mrq;
144 struct mmc_command *cmd;
145 struct mmc_data *data;
146 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100147 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800148 /*
149 * vcc == configured supply
150 * vcc_aux == optional
151 * - MMC1, supply for DAT4..DAT7
152 * - MMC2/MMC2, external level shifter voltage supply, for
153 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
154 */
155 struct regulator *vcc;
156 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100157 void __iomem *base;
158 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700159 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100160 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200161 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100162 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700163 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100164 int suspended;
165 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100166 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100167 struct dma_chan *tx_chan;
168 struct dma_chan *rx_chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100169 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200170 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700171 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700172 int protect_card;
173 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800174 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700175 int req_in_progress;
Per Forlin9782aff2011-07-01 18:55:23 +0200176 struct omap_hsmmc_next next_data;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700177
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100178 struct omap_mmc_platform_data *pdata;
179};
180
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800181static int omap_hsmmc_card_detect(struct device *dev, int slot)
182{
183 struct omap_mmc_platform_data *mmc = dev->platform_data;
184
185 /* NOTE: assumes card detect signal is active-low */
186 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
187}
188
189static int omap_hsmmc_get_wp(struct device *dev, int slot)
190{
191 struct omap_mmc_platform_data *mmc = dev->platform_data;
192
193 /* NOTE: assumes write protect signal is active-high */
194 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
195}
196
197static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
198{
199 struct omap_mmc_platform_data *mmc = dev->platform_data;
200
201 /* NOTE: assumes card detect signal is active-low */
202 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
203}
204
205#ifdef CONFIG_PM
206
207static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
208{
209 struct omap_mmc_platform_data *mmc = dev->platform_data;
210
211 disable_irq(mmc->slots[0].card_detect_irq);
212 return 0;
213}
214
215static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
216{
217 struct omap_mmc_platform_data *mmc = dev->platform_data;
218
219 enable_irq(mmc->slots[0].card_detect_irq);
220 return 0;
221}
222
223#else
224
225#define omap_hsmmc_suspend_cdirq NULL
226#define omap_hsmmc_resume_cdirq NULL
227
228#endif
229
Adrian Hunterb702b102010-02-15 10:03:35 -0800230#ifdef CONFIG_REGULATOR
231
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500232static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800233 int vdd)
234{
235 struct omap_hsmmc_host *host =
236 platform_get_drvdata(to_platform_device(dev));
237 int ret = 0;
238
239 /*
240 * If we don't see a Vcc regulator, assume it's a fixed
241 * voltage always-on regulator.
242 */
243 if (!host->vcc)
244 return 0;
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530245 /*
246 * With DT, never turn OFF the regulator. This is because
247 * the pbias cell programming support is still missing when
248 * booting with Device tree
249 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +0530250 if (dev->of_node && !vdd)
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530251 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800252
253 if (mmc_slot(host).before_set_reg)
254 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
255
256 /*
257 * Assume Vcc regulator is used only to power the card ... OMAP
258 * VDDS is used to power the pins, optionally with a transceiver to
259 * support cards using voltages other than VDDS (1.8V nominal). When a
260 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
261 *
262 * In some cases this regulator won't support enable/disable;
263 * e.g. it's a fixed rail for a WLAN chip.
264 *
265 * In other cases vcc_aux switches interface power. Example, for
266 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
267 * chips/cards need an interface voltage rail too.
268 */
269 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400270 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800271 /* Enable interface voltage rail, if needed */
272 if (ret == 0 && host->vcc_aux) {
273 ret = regulator_enable(host->vcc_aux);
274 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400275 ret = mmc_regulator_set_ocr(host->mmc,
276 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800277 }
278 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400279 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800280 if (host->vcc_aux)
281 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400282 if (!ret) {
283 /* Then proceed to shut down the local regulator */
284 ret = mmc_regulator_set_ocr(host->mmc,
285 host->vcc, 0);
286 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800287 }
288
289 if (mmc_slot(host).after_set_reg)
290 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
291
292 return ret;
293}
294
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800295static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
296{
297 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700298 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800299
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800300 reg = regulator_get(host->dev, "vmmc");
301 if (IS_ERR(reg)) {
302 dev_dbg(host->dev, "vmmc regulator missing\n");
NeilBrown1fdc90f2012-08-08 00:06:00 -0400303 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800304 } else {
NeilBrown1fdc90f2012-08-08 00:06:00 -0400305 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800306 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700307 ocr_value = mmc_regulator_get_ocrmask(reg);
308 if (!mmc_slot(host).ocr_mask) {
309 mmc_slot(host).ocr_mask = ocr_value;
310 } else {
311 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530312 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500313 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700314 mmc_slot(host).ocr_mask = 0;
315 return -EINVAL;
316 }
317 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800318
319 /* Allow an aux regulator */
320 reg = regulator_get(host->dev, "vmmc_aux");
321 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
322
Balaji T Kb1c1df72011-05-30 19:55:34 +0530323 /* For eMMC do not power off when not in sleep state */
324 if (mmc_slot(host).no_regulator_off_init)
325 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800326 /*
327 * UGLY HACK: workaround regulator framework bugs.
328 * When the bootloader leaves a supply active, it's
329 * initialized with zero usecount ... and we can't
330 * disable it without first enabling it. Until the
331 * framework is fixed, we need a workaround like this
332 * (which is safe for MMC, but not in general).
333 */
Adrian Huntere840ce12011-05-06 12:14:10 +0300334 if (regulator_is_enabled(host->vcc) > 0 ||
335 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
336 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
337
338 mmc_slot(host).set_power(host->dev, host->slot_id,
339 1, vdd);
340 mmc_slot(host).set_power(host->dev, host->slot_id,
341 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800342 }
343 }
344
345 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800346}
347
348static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
349{
350 regulator_put(host->vcc);
351 regulator_put(host->vcc_aux);
352 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800353}
354
Adrian Hunterb702b102010-02-15 10:03:35 -0800355static inline int omap_hsmmc_have_reg(void)
356{
357 return 1;
358}
359
360#else
361
362static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
363{
364 return -EINVAL;
365}
366
367static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
368{
369}
370
371static inline int omap_hsmmc_have_reg(void)
372{
373 return 0;
374}
375
376#endif
377
378static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
379{
380 int ret;
381
382 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800383 if (pdata->slots[0].cover)
384 pdata->slots[0].get_cover_state =
385 omap_hsmmc_get_cover_state;
386 else
387 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
388 pdata->slots[0].card_detect_irq =
389 gpio_to_irq(pdata->slots[0].switch_pin);
390 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
391 if (ret)
392 return ret;
393 ret = gpio_direction_input(pdata->slots[0].switch_pin);
394 if (ret)
395 goto err_free_sp;
396 } else
397 pdata->slots[0].switch_pin = -EINVAL;
398
399 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
400 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
401 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
402 if (ret)
403 goto err_free_cd;
404 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
405 if (ret)
406 goto err_free_wp;
407 } else
408 pdata->slots[0].gpio_wp = -EINVAL;
409
410 return 0;
411
412err_free_wp:
413 gpio_free(pdata->slots[0].gpio_wp);
414err_free_cd:
415 if (gpio_is_valid(pdata->slots[0].switch_pin))
416err_free_sp:
417 gpio_free(pdata->slots[0].switch_pin);
418 return ret;
419}
420
421static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
422{
423 if (gpio_is_valid(pdata->slots[0].gpio_wp))
424 gpio_free(pdata->slots[0].gpio_wp);
425 if (gpio_is_valid(pdata->slots[0].switch_pin))
426 gpio_free(pdata->slots[0].switch_pin);
427}
428
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100429/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300430 * Start clock to the card
431 */
432static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
433{
434 OMAP_HSMMC_WRITE(host->base, SYSCTL,
435 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
436}
437
438/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100439 * Stop clock to the card
440 */
Denis Karpov70a33412009-09-22 16:44:59 -0700441static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100442{
443 OMAP_HSMMC_WRITE(host->base, SYSCTL,
444 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
445 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
446 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
447}
448
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700449static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
450 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700451{
452 unsigned int irq_mask;
453
454 if (host->use_dma)
455 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
456 else
457 irq_mask = INT_EN_MASK;
458
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700459 /* Disable timeout for erases */
460 if (cmd->opcode == MMC_ERASE)
461 irq_mask &= ~DTO_ENABLE;
462
Adrian Hunterb4175772010-05-26 14:42:06 -0700463 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
464 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
465 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
466}
467
468static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
469{
470 OMAP_HSMMC_WRITE(host->base, ISE, 0);
471 OMAP_HSMMC_WRITE(host->base, IE, 0);
472 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
473}
474
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300475/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530476static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300477{
478 u16 dsor = 0;
479
480 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530481 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300482 if (dsor > 250)
483 dsor = 250;
484 }
485
486 return dsor;
487}
488
Andy Shevchenko5934df22011-05-06 12:14:06 +0300489static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
490{
491 struct mmc_ios *ios = &host->mmc->ios;
492 unsigned long regval;
493 unsigned long timeout;
494
Venkatraman S8986d312012-08-07 19:10:38 +0530495 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300496
497 omap_hsmmc_stop_clock(host);
498
499 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
500 regval = regval & ~(CLKD_MASK | DTO_MASK);
Balaji TKd83b6e02011-12-20 15:12:00 +0530501 regval = regval | (calc_divisor(host, ios) << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300502 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
503 OMAP_HSMMC_WRITE(host->base, SYSCTL,
504 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
505
506 /* Wait till the ICS bit is set */
507 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
508 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
509 && time_before(jiffies, timeout))
510 cpu_relax();
511
512 omap_hsmmc_start_clock(host);
513}
514
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400515static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
516{
517 struct mmc_ios *ios = &host->mmc->ios;
518 u32 con;
519
520 con = OMAP_HSMMC_READ(host->base, CON);
Balaji T K03b5d922012-04-09 12:08:33 +0530521 if (ios->timing == MMC_TIMING_UHS_DDR50)
522 con |= DDR; /* configure in DDR mode */
523 else
524 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400525 switch (ios->bus_width) {
526 case MMC_BUS_WIDTH_8:
527 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
528 break;
529 case MMC_BUS_WIDTH_4:
530 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
531 OMAP_HSMMC_WRITE(host->base, HCTL,
532 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
533 break;
534 case MMC_BUS_WIDTH_1:
535 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
536 OMAP_HSMMC_WRITE(host->base, HCTL,
537 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
538 break;
539 }
540}
541
542static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
543{
544 struct mmc_ios *ios = &host->mmc->ios;
545 u32 con;
546
547 con = OMAP_HSMMC_READ(host->base, CON);
548 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
549 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
550 else
551 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
552}
553
Denis Karpov11dd62a2009-09-22 16:44:43 -0700554#ifdef CONFIG_PM
555
556/*
557 * Restore the MMC host context, if it was lost as result of a
558 * power state change.
559 */
Denis Karpov70a33412009-09-22 16:44:59 -0700560static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700561{
562 struct mmc_ios *ios = &host->mmc->ios;
563 struct omap_mmc_platform_data *pdata = host->pdata;
564 int context_loss = 0;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400565 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700566 unsigned long timeout;
567
568 if (pdata->get_context_loss_count) {
569 context_loss = pdata->get_context_loss_count(host->dev);
570 if (context_loss < 0)
571 return 1;
572 }
573
574 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
575 context_loss == host->context_loss ? "not " : "");
576 if (host->context_loss == context_loss)
577 return 1;
578
579 /* Wait for hardware reset */
580 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
581 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
582 && time_before(jiffies, timeout))
583 ;
584
585 /* Do software reset */
586 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
587 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
588 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
589 && time_before(jiffies, timeout))
590 ;
591
592 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
593 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
594
Balaji T Kc2200ef2012-03-07 09:55:30 -0500595 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700596 if (host->power_mode != MMC_POWER_OFF &&
597 (1 << ios->vdd) <= MMC_VDD_23_24)
598 hctl = SDVS18;
599 else
600 hctl = SDVS30;
601 capa = VS30 | VS18;
602 } else {
603 hctl = SDVS18;
604 capa = VS18;
605 }
606
607 OMAP_HSMMC_WRITE(host->base, HCTL,
608 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
609
610 OMAP_HSMMC_WRITE(host->base, CAPA,
611 OMAP_HSMMC_READ(host->base, CAPA) | capa);
612
613 OMAP_HSMMC_WRITE(host->base, HCTL,
614 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
615
616 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
617 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
618 && time_before(jiffies, timeout))
619 ;
620
Adrian Hunterb4175772010-05-26 14:42:06 -0700621 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700622
623 /* Do not initialize card-specific things if the power is off */
624 if (host->power_mode == MMC_POWER_OFF)
625 goto out;
626
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400627 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700628
Andy Shevchenko5934df22011-05-06 12:14:06 +0300629 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700630
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400631 omap_hsmmc_set_bus_mode(host);
632
Denis Karpov11dd62a2009-09-22 16:44:43 -0700633out:
634 host->context_loss = context_loss;
635
636 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
637 return 0;
638}
639
640/*
641 * Save the MMC host context (store the number of power state changes so far).
642 */
Denis Karpov70a33412009-09-22 16:44:59 -0700643static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700644{
645 struct omap_mmc_platform_data *pdata = host->pdata;
646 int context_loss;
647
648 if (pdata->get_context_loss_count) {
649 context_loss = pdata->get_context_loss_count(host->dev);
650 if (context_loss < 0)
651 return;
652 host->context_loss = context_loss;
653 }
654}
655
656#else
657
Denis Karpov70a33412009-09-22 16:44:59 -0700658static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700659{
660 return 0;
661}
662
Denis Karpov70a33412009-09-22 16:44:59 -0700663static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700664{
665}
666
667#endif
668
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100669/*
670 * Send init stream sequence to card
671 * before sending IDLE command
672 */
Denis Karpov70a33412009-09-22 16:44:59 -0700673static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100674{
675 int reg = 0;
676 unsigned long timeout;
677
Adrian Hunterb62f6222009-09-22 16:45:01 -0700678 if (host->protect_card)
679 return;
680
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100681 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700682
683 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100684 OMAP_HSMMC_WRITE(host->base, CON,
685 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
686 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
687
688 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
689 while ((reg != CC) && time_before(jiffies, timeout))
690 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
691
692 OMAP_HSMMC_WRITE(host->base, CON,
693 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700694
695 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
696 OMAP_HSMMC_READ(host->base, STAT);
697
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100698 enable_irq(host->irq);
699}
700
701static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700702int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100703{
704 int r = 1;
705
Denis Karpov191d1f12009-09-22 16:44:55 -0700706 if (mmc_slot(host).get_cover_state)
707 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100708 return r;
709}
710
711static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700712omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100713 char *buf)
714{
715 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700716 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100717
Denis Karpov70a33412009-09-22 16:44:59 -0700718 return sprintf(buf, "%s\n",
719 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100720}
721
Denis Karpov70a33412009-09-22 16:44:59 -0700722static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100723
724static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700725omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100726 char *buf)
727{
728 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700729 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100730
Denis Karpov191d1f12009-09-22 16:44:55 -0700731 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100732}
733
Denis Karpov70a33412009-09-22 16:44:59 -0700734static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100735
736/*
737 * Configure the response type and send the cmd.
738 */
739static void
Denis Karpov70a33412009-09-22 16:44:59 -0700740omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100741 struct mmc_data *data)
742{
743 int cmdreg = 0, resptype = 0, cmdtype = 0;
744
Venkatraman S8986d312012-08-07 19:10:38 +0530745 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100746 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
747 host->cmd = cmd;
748
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700749 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100750
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200751 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100752 if (cmd->flags & MMC_RSP_PRESENT) {
753 if (cmd->flags & MMC_RSP_136)
754 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200755 else if (cmd->flags & MMC_RSP_BUSY) {
756 resptype = 3;
757 host->response_busy = 1;
758 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100759 resptype = 2;
760 }
761
762 /*
763 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
764 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
765 * a val of 0x3, rest 0x0.
766 */
767 if (cmd == host->mrq->stop)
768 cmdtype = 0x3;
769
770 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
771
772 if (data) {
773 cmdreg |= DP_SELECT | MSBS | BCE;
774 if (data->flags & MMC_DATA_READ)
775 cmdreg |= DDIR;
776 else
777 cmdreg &= ~(DDIR);
778 }
779
780 if (host->use_dma)
781 cmdreg |= DMA_EN;
782
Adrian Hunterb4175772010-05-26 14:42:06 -0700783 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700784
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100785 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
786 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
787}
788
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200789static int
Denis Karpov70a33412009-09-22 16:44:59 -0700790omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200791{
792 if (data->flags & MMC_DATA_WRITE)
793 return DMA_TO_DEVICE;
794 else
795 return DMA_FROM_DEVICE;
796}
797
Russell Kingc5c98922012-04-13 12:14:39 +0100798static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
799 struct mmc_data *data)
800{
801 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
802}
803
Adrian Hunterb4175772010-05-26 14:42:06 -0700804static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
805{
806 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530807 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700808
Venkatraman S31463b12012-04-09 12:08:34 +0530809 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700810 host->req_in_progress = 0;
811 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530812 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700813
814 omap_hsmmc_disable_irq(host);
815 /* Do not complete the request if DMA is still in progress */
816 if (mrq->data && host->use_dma && dma_ch != -1)
817 return;
818 host->mrq = NULL;
819 mmc_request_done(host->mmc, mrq);
820}
821
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100822/*
823 * Notify the transfer complete to MMC core
824 */
825static void
Denis Karpov70a33412009-09-22 16:44:59 -0700826omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100827{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200828 if (!data) {
829 struct mmc_request *mrq = host->mrq;
830
Adrian Hunter23050102009-09-22 16:44:57 -0700831 /* TC before CC from CMD6 - don't know why, but it happens */
832 if (host->cmd && host->cmd->opcode == 6 &&
833 host->response_busy) {
834 host->response_busy = 0;
835 return;
836 }
837
Adrian Hunterb4175772010-05-26 14:42:06 -0700838 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200839 return;
840 }
841
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100842 host->data = NULL;
843
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100844 if (!data->error)
845 data->bytes_xfered += data->blocks * (data->blksz);
846 else
847 data->bytes_xfered = 0;
848
Ming Leife852272012-06-22 18:49:35 +0800849 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700850 omap_hsmmc_request_done(host, data->mrq);
Ming Leife852272012-06-22 18:49:35 +0800851 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100852 }
Ming Leife852272012-06-22 18:49:35 +0800853 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100854}
855
856/*
857 * Notify the core about command completion
858 */
859static void
Denis Karpov70a33412009-09-22 16:44:59 -0700860omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100861{
862 host->cmd = NULL;
863
864 if (cmd->flags & MMC_RSP_PRESENT) {
865 if (cmd->flags & MMC_RSP_136) {
866 /* response type 2 */
867 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
868 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
869 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
870 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
871 } else {
872 /* response types 1, 1b, 3, 4, 5, 6 */
873 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
874 }
875 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700876 if ((host->data == NULL && !host->response_busy) || cmd->error)
877 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100878}
879
880/*
881 * DMA clean up for command errors
882 */
Denis Karpov70a33412009-09-22 16:44:59 -0700883static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100884{
Adrian Hunterb4175772010-05-26 14:42:06 -0700885 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530886 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700887
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200888 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100889
Venkatraman S31463b12012-04-09 12:08:34 +0530890 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700891 dma_ch = host->dma_ch;
892 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530893 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700894
895 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100896 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
897
898 dmaengine_terminate_all(chan);
899 dma_unmap_sg(chan->device->dev,
900 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700901 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100902
Per Forlin053bf342011-11-07 21:55:11 +0530903 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100904 }
905 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100906}
907
908/*
909 * Readable error output
910 */
911#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300912static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100913{
914 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700915 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300916 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
917 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
918 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
919 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100920 };
921 char res[256];
922 char *buf = res;
923 int len, i;
924
925 len = sprintf(buf, "MMC IRQ 0x%x :", status);
926 buf += len;
927
Denis Karpov70a33412009-09-22 16:44:59 -0700928 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100929 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700930 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100931 buf += len;
932 }
933
Venkatraman S8986d312012-08-07 19:10:38 +0530934 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100935}
Adrian Hunter699b9582011-05-06 12:14:01 +0300936#else
937static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
938 u32 status)
939{
940}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100941#endif /* CONFIG_MMC_DEBUG */
942
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100943/*
944 * MMC controller internal state machines reset
945 *
946 * Used to reset command or data internal state machines, using respectively
947 * SRC or SRD bit of SYSCTL register
948 * Can be called from interrupt context
949 */
Denis Karpov70a33412009-09-22 16:44:59 -0700950static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
951 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100952{
953 unsigned long i = 0;
954 unsigned long limit = (loops_per_jiffy *
955 msecs_to_jiffies(MMC_TIMEOUT_MS));
956
957 OMAP_HSMMC_WRITE(host->base, SYSCTL,
958 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
959
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700960 /*
961 * OMAP4 ES2 and greater has an updated reset logic.
962 * Monitor a 0->1 transition first
963 */
964 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -0500965 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700966 && (i++ < limit))
967 cpu_relax();
968 }
969 i = 0;
970
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100971 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
972 (i++ < limit))
973 cpu_relax();
974
975 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
976 dev_err(mmc_dev(host->mmc),
977 "Timeout waiting on controller reset in %s\n",
978 __func__);
979}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100980
Adrian Hunterb4175772010-05-26 14:42:06 -0700981static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100982{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100983 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -0700984 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100985
Adrian Hunterb4175772010-05-26 14:42:06 -0700986 if (!host->req_in_progress) {
987 do {
988 OMAP_HSMMC_WRITE(host->base, STAT, status);
989 /* Flush posted write */
990 status = OMAP_HSMMC_READ(host->base, STAT);
991 } while (status & INT_EN_MASK);
992 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100993 }
994
995 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +0530996 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100997
998 if (status & ERR) {
Adrian Hunter699b9582011-05-06 12:14:01 +0300999 omap_hsmmc_dbg_report_irq(host, status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001000 if ((status & CMD_TIMEOUT) ||
1001 (status & CMD_CRC)) {
1002 if (host->cmd) {
1003 if (status & CMD_TIMEOUT) {
Denis Karpov70a33412009-09-22 16:44:59 -07001004 omap_hsmmc_reset_controller_fsm(host,
1005 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001006 host->cmd->error = -ETIMEDOUT;
1007 } else {
1008 host->cmd->error = -EILSEQ;
1009 }
1010 end_cmd = 1;
1011 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001012 if (host->data || host->response_busy) {
1013 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001014 omap_hsmmc_dma_cleanup(host,
1015 -ETIMEDOUT);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001016 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001017 omap_hsmmc_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -08001018 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001019 }
1020 if ((status & DATA_TIMEOUT) ||
1021 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001022 if (host->data || host->response_busy) {
1023 int err = (status & DATA_TIMEOUT) ?
1024 -ETIMEDOUT : -EILSEQ;
1025
1026 if (host->data)
Denis Karpov70a33412009-09-22 16:44:59 -07001027 omap_hsmmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001028 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001029 host->mrq->cmd->error = err;
1030 host->response_busy = 0;
Denis Karpov70a33412009-09-22 16:44:59 -07001031 omap_hsmmc_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001032 end_trans = 1;
1033 }
1034 }
1035 if (status & CARD_ERR) {
1036 dev_dbg(mmc_dev(host->mmc),
1037 "Ignoring card err CMD%d\n", host->cmd->opcode);
1038 if (host->cmd)
1039 end_cmd = 1;
1040 if (host->data)
1041 end_trans = 1;
1042 }
1043 }
1044
1045 OMAP_HSMMC_WRITE(host->base, STAT, status);
1046
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001047 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001048 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001049 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001050 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001051}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001052
Adrian Hunterb4175772010-05-26 14:42:06 -07001053/*
1054 * MMC controller IRQ handler
1055 */
1056static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1057{
1058 struct omap_hsmmc_host *host = dev_id;
1059 int status;
1060
1061 status = OMAP_HSMMC_READ(host->base, STAT);
1062 do {
1063 omap_hsmmc_do_irq(host, status);
1064 /* Flush posted write */
1065 status = OMAP_HSMMC_READ(host->base, STAT);
1066 } while (status & INT_EN_MASK);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001067
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001068 return IRQ_HANDLED;
1069}
1070
Denis Karpov70a33412009-09-22 16:44:59 -07001071static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001072{
1073 unsigned long i;
1074
1075 OMAP_HSMMC_WRITE(host->base, HCTL,
1076 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1077 for (i = 0; i < loops_per_jiffy; i++) {
1078 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1079 break;
1080 cpu_relax();
1081 }
1082}
1083
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001084/*
David Brownelleb250822009-02-17 14:49:01 -08001085 * Switch MMC interface voltage ... only relevant for MMC1.
1086 *
1087 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1088 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1089 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001090 */
Denis Karpov70a33412009-09-22 16:44:59 -07001091static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001092{
1093 u32 reg_val = 0;
1094 int ret;
1095
1096 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301097 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301098 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301099 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001100
1101 /* Turn the power off */
1102 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001103
1104 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001105 if (!ret)
1106 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1107 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301108 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301109 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301110 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001111
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001112 if (ret != 0)
1113 goto err;
1114
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001115 OMAP_HSMMC_WRITE(host->base, HCTL,
1116 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1117 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001118
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001119 /*
1120 * If a MMC dual voltage card is detected, the set_ios fn calls
1121 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001122 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001123 *
David Brownelleb250822009-02-17 14:49:01 -08001124 * Cope with a bit of slop in the range ... per data sheets:
1125 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1126 * but recommended values are 1.71V to 1.89V
1127 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1128 * but recommended values are 2.7V to 3.3V
1129 *
1130 * Board setup code shouldn't permit anything very out-of-range.
1131 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1132 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001133 */
David Brownelleb250822009-02-17 14:49:01 -08001134 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001135 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001136 else
1137 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001138
1139 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001140 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001141
1142 return 0;
1143err:
1144 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1145 return ret;
1146}
1147
Adrian Hunterb62f6222009-09-22 16:45:01 -07001148/* Protect the card while the cover is open */
1149static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1150{
1151 if (!mmc_slot(host).get_cover_state)
1152 return;
1153
1154 host->reqs_blocked = 0;
1155 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1156 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301157 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001158 "card is now accessible\n",
1159 mmc_hostname(host->mmc));
1160 host->protect_card = 0;
1161 }
1162 } else {
1163 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301164 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001165 "card is now inaccessible\n",
1166 mmc_hostname(host->mmc));
1167 host->protect_card = 1;
1168 }
1169 }
1170}
1171
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001172/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001173 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001174 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001175static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001176{
NeilBrown7efab4f2011-12-30 12:35:13 +11001177 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001178 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001179 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001180
Adrian Huntera6b22402009-09-22 16:44:45 -07001181 if (host->suspended)
NeilBrown7efab4f2011-12-30 12:35:13 +11001182 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001183
1184 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001185
Denis Karpov191d1f12009-09-22 16:44:55 -07001186 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001187 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001188 else {
1189 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001190 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001191 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001192
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001193 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001194 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001195 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001196 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001197 return IRQ_HANDLED;
1198}
1199
Russell Kingc5c98922012-04-13 12:14:39 +01001200static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001201{
Russell Kingc5c98922012-04-13 12:14:39 +01001202 struct omap_hsmmc_host *host = param;
1203 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001204 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001205 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001206
Russell Kingc5c98922012-04-13 12:14:39 +01001207 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001208 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001209 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001210 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001211 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001212
Adrian Hunter770d7432011-05-06 12:14:11 +03001213 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001214 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001215 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001216 dma_unmap_sg(chan->device->dev,
1217 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001218 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001219
1220 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001221 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001222 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001223
1224 /* If DMA has finished after TC, complete the request */
1225 if (!req_in_progress) {
1226 struct mmc_request *mrq = host->mrq;
1227
1228 host->mrq = NULL;
1229 mmc_request_done(host->mmc, mrq);
1230 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001231}
1232
Per Forlin9782aff2011-07-01 18:55:23 +02001233static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1234 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001235 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001236 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001237{
1238 int dma_len;
1239
1240 if (!next && data->host_cookie &&
1241 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301242 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001243 " host->next_data.cookie %d\n",
1244 __func__, data->host_cookie, host->next_data.cookie);
1245 data->host_cookie = 0;
1246 }
1247
1248 /* Check if next job is already prepared */
1249 if (next ||
1250 (!next && data->host_cookie != host->next_data.cookie)) {
Russell King26b88522012-04-13 12:27:37 +01001251 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001252 omap_hsmmc_get_dma_dir(host, data));
1253
1254 } else {
1255 dma_len = host->next_data.dma_len;
1256 host->next_data.dma_len = 0;
1257 }
1258
1259
1260 if (dma_len == 0)
1261 return -EINVAL;
1262
1263 if (next) {
1264 next->dma_len = dma_len;
1265 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1266 } else
1267 host->dma_len = dma_len;
1268
1269 return 0;
1270}
1271
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001272/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001273 * Routine to configure and start DMA for the MMC card
1274 */
Denis Karpov70a33412009-09-22 16:44:59 -07001275static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1276 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001277{
Russell King26b88522012-04-13 12:27:37 +01001278 struct dma_slave_config cfg;
1279 struct dma_async_tx_descriptor *tx;
1280 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001281 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001282 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001283
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001284 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001285 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001286 struct scatterlist *sgl;
1287
1288 sgl = data->sg + i;
1289 if (sgl->length % data->blksz)
1290 return -EINVAL;
1291 }
1292 if ((data->blksz % 4) != 0)
1293 /* REVISIT: The MMC buffer increments only when MSB is written.
1294 * Return error for blksz which is non multiple of four.
1295 */
1296 return -EINVAL;
1297
Adrian Hunterb4175772010-05-26 14:42:06 -07001298 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001299
Russell Kingc5c98922012-04-13 12:14:39 +01001300 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001301
Russell King26b88522012-04-13 12:27:37 +01001302 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1303 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1304 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1305 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1306 cfg.src_maxburst = data->blksz / 4;
1307 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001308
Russell King26b88522012-04-13 12:27:37 +01001309 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001310 if (ret)
1311 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001312
Russell King26b88522012-04-13 12:27:37 +01001313 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1314 if (ret)
1315 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001316
Russell King26b88522012-04-13 12:27:37 +01001317 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1318 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1319 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1320 if (!tx) {
1321 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1322 /* FIXME: cleanup */
1323 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001324 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001325
Russell King26b88522012-04-13 12:27:37 +01001326 tx->callback = omap_hsmmc_dma_callback;
1327 tx->callback_param = host;
1328
1329 /* Does not fail */
1330 dmaengine_submit(tx);
1331
1332 host->dma_ch = 1;
1333
1334 dma_async_issue_pending(chan);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001335
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001336 return 0;
1337}
1338
Denis Karpov70a33412009-09-22 16:44:59 -07001339static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001340 unsigned int timeout_ns,
1341 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001342{
1343 unsigned int timeout, cycle_ns;
1344 uint32_t reg, clkd, dto = 0;
1345
1346 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1347 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1348 if (clkd == 0)
1349 clkd = 1;
1350
1351 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001352 timeout = timeout_ns / cycle_ns;
1353 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001354 if (timeout) {
1355 while ((timeout & 0x80000000) == 0) {
1356 dto += 1;
1357 timeout <<= 1;
1358 }
1359 dto = 31 - dto;
1360 timeout <<= 1;
1361 if (timeout && dto)
1362 dto += 1;
1363 if (dto >= 13)
1364 dto -= 13;
1365 else
1366 dto = 0;
1367 if (dto > 14)
1368 dto = 14;
1369 }
1370
1371 reg &= ~DTO_MASK;
1372 reg |= dto << DTO_SHIFT;
1373 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1374}
1375
1376/*
1377 * Configure block length for MMC/SD cards and initiate the transfer.
1378 */
1379static int
Denis Karpov70a33412009-09-22 16:44:59 -07001380omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001381{
1382 int ret;
1383 host->data = req->data;
1384
1385 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001386 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001387 /*
1388 * Set an arbitrary 100ms data timeout for commands with
1389 * busy signal.
1390 */
1391 if (req->cmd->flags & MMC_RSP_BUSY)
1392 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001393 return 0;
1394 }
1395
1396 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1397 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001398 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001399
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001400 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001401 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001402 if (ret != 0) {
1403 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1404 return ret;
1405 }
1406 }
1407 return 0;
1408}
1409
Per Forlin9782aff2011-07-01 18:55:23 +02001410static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1411 int err)
1412{
1413 struct omap_hsmmc_host *host = mmc_priv(mmc);
1414 struct mmc_data *data = mrq->data;
1415
Russell King26b88522012-04-13 12:27:37 +01001416 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001417 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001418
Russell King26b88522012-04-13 12:27:37 +01001419 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1420 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001421 data->host_cookie = 0;
1422 }
1423}
1424
1425static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1426 bool is_first_req)
1427{
1428 struct omap_hsmmc_host *host = mmc_priv(mmc);
1429
1430 if (mrq->data->host_cookie) {
1431 mrq->data->host_cookie = 0;
1432 return ;
1433 }
1434
Russell Kingc5c98922012-04-13 12:14:39 +01001435 if (host->use_dma) {
1436 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001437
Per Forlin9782aff2011-07-01 18:55:23 +02001438 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001439 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001440 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001441 }
Per Forlin9782aff2011-07-01 18:55:23 +02001442}
1443
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001444/*
1445 * Request function. for read/write operation
1446 */
Denis Karpov70a33412009-09-22 16:44:59 -07001447static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001448{
Denis Karpov70a33412009-09-22 16:44:59 -07001449 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001450 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001451
Adrian Hunterb4175772010-05-26 14:42:06 -07001452 BUG_ON(host->req_in_progress);
1453 BUG_ON(host->dma_ch != -1);
1454 if (host->protect_card) {
1455 if (host->reqs_blocked < 3) {
1456 /*
1457 * Ensure the controller is left in a consistent
1458 * state by resetting the command and data state
1459 * machines.
1460 */
1461 omap_hsmmc_reset_controller_fsm(host, SRD);
1462 omap_hsmmc_reset_controller_fsm(host, SRC);
1463 host->reqs_blocked += 1;
1464 }
1465 req->cmd->error = -EBADF;
1466 if (req->data)
1467 req->data->error = -EBADF;
1468 req->cmd->retries = 0;
1469 mmc_request_done(mmc, req);
1470 return;
1471 } else if (host->reqs_blocked)
1472 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001473 WARN_ON(host->mrq != NULL);
1474 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001475 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001476 if (err) {
1477 req->cmd->error = err;
1478 if (req->data)
1479 req->data->error = err;
1480 host->mrq = NULL;
1481 mmc_request_done(mmc, req);
1482 return;
1483 }
1484
Denis Karpov70a33412009-09-22 16:44:59 -07001485 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001486}
1487
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001488/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001489static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001490{
Denis Karpov70a33412009-09-22 16:44:59 -07001491 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001492 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001493
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301494 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001495
Adrian Huntera3621462009-09-22 16:44:42 -07001496 if (ios->power_mode != host->power_mode) {
1497 switch (ios->power_mode) {
1498 case MMC_POWER_OFF:
1499 mmc_slot(host).set_power(host->dev, host->slot_id,
1500 0, 0);
1501 break;
1502 case MMC_POWER_UP:
1503 mmc_slot(host).set_power(host->dev, host->slot_id,
1504 1, ios->vdd);
1505 break;
1506 case MMC_POWER_ON:
1507 do_send_init_stream = 1;
1508 break;
1509 }
1510 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001511 }
1512
Denis Karpovdd498ef2009-09-22 16:44:49 -07001513 /* FIXME: set registers based only on changes to ios */
1514
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001515 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001516
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301517 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001518 /* Only MMC1 can interface at 3V without some flavor
1519 * of external transceiver; but they all handle 1.8V.
1520 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001521 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Rajendra Nayak1f84b712012-03-12 20:32:38 +05301522 (ios->vdd == DUAL_VOLT_OCR_BIT) &&
1523 /*
1524 * With pbias cell programming missing, this
1525 * can't be allowed when booting with device
1526 * tree.
1527 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +05301528 !host->dev->of_node) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001529 /*
1530 * The mmc_select_voltage fn of the core does
1531 * not seem to set the power_mode to
1532 * MMC_POWER_UP upon recalculating the voltage.
1533 * vdd 1.8v.
1534 */
Denis Karpov70a33412009-09-22 16:44:59 -07001535 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1536 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001537 "Switch operation failed\n");
1538 }
1539 }
1540
Andy Shevchenko5934df22011-05-06 12:14:06 +03001541 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001542
Adrian Huntera3621462009-09-22 16:44:42 -07001543 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001544 send_init_stream(host);
1545
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001546 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001547
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301548 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001549}
1550
1551static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1552{
Denis Karpov70a33412009-09-22 16:44:59 -07001553 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001554
Denis Karpov191d1f12009-09-22 16:44:55 -07001555 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001556 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001557 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001558}
1559
1560static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1561{
Denis Karpov70a33412009-09-22 16:44:59 -07001562 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001563
Denis Karpov191d1f12009-09-22 16:44:55 -07001564 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001565 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001566 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001567}
1568
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001569static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1570{
1571 struct omap_hsmmc_host *host = mmc_priv(mmc);
1572
1573 if (mmc_slot(host).init_card)
1574 mmc_slot(host).init_card(card);
1575}
1576
Denis Karpov70a33412009-09-22 16:44:59 -07001577static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001578{
1579 u32 hctl, capa, value;
1580
1581 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301582 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001583 hctl = SDVS30;
1584 capa = VS30 | VS18;
1585 } else {
1586 hctl = SDVS18;
1587 capa = VS18;
1588 }
1589
1590 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1591 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1592
1593 value = OMAP_HSMMC_READ(host->base, CAPA);
1594 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1595
1596 /* Set the controller to AUTO IDLE mode */
1597 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1598 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1599
1600 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001601 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001602}
1603
Denis Karpov70a33412009-09-22 16:44:59 -07001604static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001605{
Denis Karpov70a33412009-09-22 16:44:59 -07001606 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001607
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301608 pm_runtime_get_sync(host->dev);
1609
Denis Karpovdd498ef2009-09-22 16:44:49 -07001610 return 0;
1611}
1612
Adrian Hunter907d2e72012-02-29 09:17:21 +02001613static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001614{
Denis Karpov70a33412009-09-22 16:44:59 -07001615 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001616
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301617 pm_runtime_mark_last_busy(host->dev);
1618 pm_runtime_put_autosuspend(host->dev);
1619
Denis Karpovdd498ef2009-09-22 16:44:49 -07001620 return 0;
1621}
1622
Denis Karpov70a33412009-09-22 16:44:59 -07001623static const struct mmc_host_ops omap_hsmmc_ops = {
1624 .enable = omap_hsmmc_enable_fclk,
1625 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001626 .post_req = omap_hsmmc_post_req,
1627 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001628 .request = omap_hsmmc_request,
1629 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001630 .get_cd = omap_hsmmc_get_cd,
1631 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001632 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001633 /* NYET -- enable_sdio_irq */
1634};
1635
Denis Karpovd900f712009-09-22 16:44:38 -07001636#ifdef CONFIG_DEBUG_FS
1637
Denis Karpov70a33412009-09-22 16:44:59 -07001638static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001639{
1640 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001641 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001642 int context_loss = 0;
1643
Denis Karpov70a33412009-09-22 16:44:59 -07001644 if (host->pdata->get_context_loss_count)
1645 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001646
Adrian Hunter907d2e72012-02-29 09:17:21 +02001647 seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1648 mmc->index, host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001649
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301650 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001651 seq_printf(s, "host suspended, can't read registers\n");
1652 return 0;
1653 }
1654
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301655 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001656
1657 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1658 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1659 seq_printf(s, "CON:\t\t0x%08x\n",
1660 OMAP_HSMMC_READ(host->base, CON));
1661 seq_printf(s, "HCTL:\t\t0x%08x\n",
1662 OMAP_HSMMC_READ(host->base, HCTL));
1663 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1664 OMAP_HSMMC_READ(host->base, SYSCTL));
1665 seq_printf(s, "IE:\t\t0x%08x\n",
1666 OMAP_HSMMC_READ(host->base, IE));
1667 seq_printf(s, "ISE:\t\t0x%08x\n",
1668 OMAP_HSMMC_READ(host->base, ISE));
1669 seq_printf(s, "CAPA:\t\t0x%08x\n",
1670 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001671
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301672 pm_runtime_mark_last_busy(host->dev);
1673 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001674
Denis Karpovd900f712009-09-22 16:44:38 -07001675 return 0;
1676}
1677
Denis Karpov70a33412009-09-22 16:44:59 -07001678static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001679{
Denis Karpov70a33412009-09-22 16:44:59 -07001680 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001681}
1682
1683static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001684 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001685 .read = seq_read,
1686 .llseek = seq_lseek,
1687 .release = single_release,
1688};
1689
Denis Karpov70a33412009-09-22 16:44:59 -07001690static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001691{
1692 if (mmc->debugfs_root)
1693 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1694 mmc, &mmc_regs_fops);
1695}
1696
1697#else
1698
Denis Karpov70a33412009-09-22 16:44:59 -07001699static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001700{
1701}
1702
1703#endif
1704
Rajendra Nayak46856a62012-03-12 20:32:37 +05301705#ifdef CONFIG_OF
1706static u16 omap4_reg_offset = 0x100;
1707
1708static const struct of_device_id omap_mmc_of_match[] = {
1709 {
1710 .compatible = "ti,omap2-hsmmc",
1711 },
1712 {
1713 .compatible = "ti,omap3-hsmmc",
1714 },
1715 {
1716 .compatible = "ti,omap4-hsmmc",
1717 .data = &omap4_reg_offset,
1718 },
1719 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001720};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301721MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1722
1723static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1724{
1725 struct omap_mmc_platform_data *pdata;
1726 struct device_node *np = dev->of_node;
1727 u32 bus_width;
1728
1729 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1730 if (!pdata)
1731 return NULL; /* out of memory */
1732
1733 if (of_find_property(np, "ti,dual-volt", NULL))
1734 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1735
1736 /* This driver only supports 1 slot */
1737 pdata->nr_slots = 1;
1738 pdata->slots[0].switch_pin = of_get_named_gpio(np, "cd-gpios", 0);
1739 pdata->slots[0].gpio_wp = of_get_named_gpio(np, "wp-gpios", 0);
1740
1741 if (of_find_property(np, "ti,non-removable", NULL)) {
1742 pdata->slots[0].nonremovable = true;
1743 pdata->slots[0].no_regulator_off_init = true;
1744 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001745 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301746 if (bus_width == 4)
1747 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1748 else if (bus_width == 8)
1749 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1750
1751 if (of_find_property(np, "ti,needs-special-reset", NULL))
1752 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1753
1754 return pdata;
1755}
1756#else
1757static inline struct omap_mmc_platform_data
1758 *of_get_hsmmc_pdata(struct device *dev)
1759{
1760 return NULL;
1761}
1762#endif
1763
Felipe Balbiefa25fd2012-03-14 11:18:28 +02001764static int __devinit omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001765{
1766 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1767 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001768 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001769 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001770 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301771 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001772 dma_cap_mask_t mask;
1773 unsigned tx_req, rx_req;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301774
1775 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1776 if (match) {
1777 pdata = of_get_hsmmc_pdata(&pdev->dev);
1778 if (match->data) {
1779 u16 *offsetp = match->data;
1780 pdata->reg_offset = *offsetp;
1781 }
1782 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001783
1784 if (pdata == NULL) {
1785 dev_err(&pdev->dev, "Platform Data is missing\n");
1786 return -ENXIO;
1787 }
1788
1789 if (pdata->nr_slots == 0) {
1790 dev_err(&pdev->dev, "No Slots\n");
1791 return -ENXIO;
1792 }
1793
1794 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1795 irq = platform_get_irq(pdev, 0);
1796 if (res == NULL || irq < 0)
1797 return -ENXIO;
1798
Chris Ball984b2032011-03-22 16:34:42 -07001799 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001800 if (res == NULL)
1801 return -EBUSY;
1802
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001803 ret = omap_hsmmc_gpio_init(pdata);
1804 if (ret)
1805 goto err;
1806
Denis Karpov70a33412009-09-22 16:44:59 -07001807 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001808 if (!mmc) {
1809 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001810 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001811 }
1812
1813 host = mmc_priv(mmc);
1814 host->mmc = mmc;
1815 host->pdata = pdata;
1816 host->dev = &pdev->dev;
1817 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001818 host->dma_ch = -1;
1819 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001820 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05301821 host->mapbase = res->start + pdata->reg_offset;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001822 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001823 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001824 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001825
1826 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001827
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301828 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001829
Adrian Huntere0eb2422010-02-15 10:03:34 -08001830 /*
1831 * If regulator_disable can only put vcc_aux to sleep then there is
1832 * no off state.
1833 */
1834 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1835 mmc_slot(host).no_off = 1;
1836
Daniel Mackd418ed82012-02-19 13:20:33 +01001837 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1838
1839 if (pdata->max_freq > 0)
1840 mmc->f_max = pdata->max_freq;
1841 else
1842 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001843
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001844 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001845
Russell King6f7607c2009-01-28 10:22:50 +00001846 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001847 if (IS_ERR(host->fclk)) {
1848 ret = PTR_ERR(host->fclk);
1849 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001850 goto err1;
1851 }
1852
Paul Walmsley9b682562011-10-06 14:50:35 -06001853 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1854 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1855 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1856 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001857
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301858 pm_runtime_enable(host->dev);
1859 pm_runtime_get_sync(host->dev);
1860 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1861 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001862
Balaji T K92a3aeb2012-02-24 21:14:34 +05301863 omap_hsmmc_context_save(host);
1864
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301865 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1866 /*
1867 * MMC can still work without debounce clock.
1868 */
1869 if (IS_ERR(host->dbclk)) {
1870 dev_warn(mmc_dev(host->mmc), "Failed to get debounce clk\n");
1871 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05301872 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301873 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
1874 clk_put(host->dbclk);
1875 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07001876 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001877
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001878 /* Since we do only SG emulation, we can have as many segs
1879 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001880 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001881
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001882 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1883 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1884 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1885 mmc->max_seg_size = mmc->max_req_size;
1886
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001887 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001888 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001889
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001890 mmc->caps |= mmc_slot(host).caps;
1891 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001892 mmc->caps |= MMC_CAP_4_BIT_DATA;
1893
Denis Karpov191d1f12009-09-22 16:44:55 -07001894 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001895 mmc->caps |= MMC_CAP_NONREMOVABLE;
1896
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001897 mmc->pm_caps = mmc_slot(host).pm_caps;
1898
Denis Karpov70a33412009-09-22 16:44:59 -07001899 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001900
Balaji T Kb7bf7732012-03-07 09:55:30 -05001901 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1902 if (!res) {
1903 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
Kevin Hilman9c17d082012-07-10 16:40:56 -07001904 ret = -ENXIO;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001905 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001906 }
Russell King26b88522012-04-13 12:27:37 +01001907 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001908
1909 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1910 if (!res) {
1911 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
Kevin Hilman9c17d082012-07-10 16:40:56 -07001912 ret = -ENXIO;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001913 goto err_irq;
1914 }
Russell King26b88522012-04-13 12:27:37 +01001915 rx_req = res->start;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001916
Russell King26b88522012-04-13 12:27:37 +01001917 dma_cap_zero(mask);
1918 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01001919
Russell King26b88522012-04-13 12:27:37 +01001920 host->rx_chan = dma_request_channel(mask, omap_dma_filter_fn, &rx_req);
1921 if (!host->rx_chan) {
1922 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001923 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001924 goto err_irq;
1925 }
1926
1927 host->tx_chan = dma_request_channel(mask, omap_dma_filter_fn, &tx_req);
1928 if (!host->tx_chan) {
1929 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001930 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001931 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01001932 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001933
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001934 /* Request IRQ for MMC operations */
Yong Zhangd9618e92011-09-22 16:59:04 +08001935 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001936 mmc_hostname(mmc), host);
1937 if (ret) {
1938 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1939 goto err_irq;
1940 }
1941
1942 if (pdata->init != NULL) {
1943 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07001944 dev_dbg(mmc_dev(host->mmc),
1945 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001946 goto err_irq_cd_init;
1947 }
1948 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001949
Adrian Hunterb702b102010-02-15 10:03:35 -08001950 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001951 ret = omap_hsmmc_reg_get(host);
1952 if (ret)
1953 goto err_reg;
1954 host->use_reg = 1;
1955 }
1956
David Brownellb583f262009-05-28 14:04:03 -07001957 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001958
1959 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001960 if ((mmc_slot(host).card_detect_irq)) {
NeilBrown7efab4f2011-12-30 12:35:13 +11001961 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1962 NULL,
1963 omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08001964 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11001965 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001966 if (ret) {
1967 dev_dbg(mmc_dev(host->mmc),
1968 "Unable to grab MMC CD IRQ\n");
1969 goto err_irq_cd;
1970 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00001971 pdata->suspend = omap_hsmmc_suspend_cdirq;
1972 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001973 }
1974
Adrian Hunterb4175772010-05-26 14:42:06 -07001975 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001976
Adrian Hunterb62f6222009-09-22 16:45:01 -07001977 omap_hsmmc_protect_card(host);
1978
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001979 mmc_add_host(mmc);
1980
Denis Karpov191d1f12009-09-22 16:44:55 -07001981 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001982 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1983 if (ret < 0)
1984 goto err_slot_name;
1985 }
Denis Karpov191d1f12009-09-22 16:44:55 -07001986 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001987 ret = device_create_file(&mmc->class_dev,
1988 &dev_attr_cover_switch);
1989 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001990 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001991 }
1992
Denis Karpov70a33412009-09-22 16:44:59 -07001993 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301994 pm_runtime_mark_last_busy(host->dev);
1995 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001996
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001997 return 0;
1998
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001999err_slot_name:
2000 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002001 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002002err_irq_cd:
2003 if (host->use_reg)
2004 omap_hsmmc_reg_put(host);
2005err_reg:
2006 if (host->pdata->cleanup)
2007 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002008err_irq_cd_init:
2009 free_irq(host->irq, host);
2010err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002011 if (host->tx_chan)
2012 dma_release_channel(host->tx_chan);
2013 if (host->rx_chan)
2014 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302015 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002016 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002017 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302018 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302019 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002020 clk_put(host->dbclk);
2021 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002022err1:
2023 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002024 platform_set_drvdata(pdev, NULL);
2025 mmc_free_host(mmc);
2026err_alloc:
2027 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002028err:
Russell King48b332f2012-04-18 11:11:57 +01002029 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2030 if (res)
2031 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002032 return ret;
2033}
2034
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002035static int __devexit omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002036{
Denis Karpov70a33412009-09-22 16:44:59 -07002037 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002038 struct resource *res;
2039
Felipe Balbi927ce942012-03-14 11:18:27 +02002040 pm_runtime_get_sync(host->dev);
2041 mmc_remove_host(host->mmc);
2042 if (host->use_reg)
2043 omap_hsmmc_reg_put(host);
2044 if (host->pdata->cleanup)
2045 host->pdata->cleanup(&pdev->dev);
2046 free_irq(host->irq, host);
2047 if (mmc_slot(host).card_detect_irq)
2048 free_irq(mmc_slot(host).card_detect_irq, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002049
Russell Kingc5c98922012-04-13 12:14:39 +01002050 if (host->tx_chan)
2051 dma_release_channel(host->tx_chan);
2052 if (host->rx_chan)
2053 dma_release_channel(host->rx_chan);
2054
Felipe Balbi927ce942012-03-14 11:18:27 +02002055 pm_runtime_put_sync(host->dev);
2056 pm_runtime_disable(host->dev);
2057 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302058 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302059 clk_disable_unprepare(host->dbclk);
Felipe Balbi927ce942012-03-14 11:18:27 +02002060 clk_put(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002061 }
2062
Felipe Balbi927ce942012-03-14 11:18:27 +02002063 mmc_free_host(host->mmc);
2064 iounmap(host->base);
2065 omap_hsmmc_gpio_free(pdev->dev.platform_data);
2066
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002067 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2068 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002069 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002070 platform_set_drvdata(pdev, NULL);
2071
2072 return 0;
2073}
2074
2075#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002076static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002077{
2078 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002079 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2080
2081 if (!host)
2082 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002083
2084 if (host && host->suspended)
2085 return 0;
2086
Felipe Balbi927ce942012-03-14 11:18:27 +02002087 pm_runtime_get_sync(host->dev);
2088 host->suspended = 1;
2089 if (host->pdata->suspend) {
2090 ret = host->pdata->suspend(dev, host->slot_id);
Eliad Peller31f9d462011-11-22 16:02:17 +02002091 if (ret) {
Felipe Balbi927ce942012-03-14 11:18:27 +02002092 dev_dbg(dev, "Unable to handle MMC board"
2093 " level suspend\n");
Adrian Huntera6b22402009-09-22 16:44:45 -07002094 host->suspended = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002095 return ret;
Adrian Huntera6b22402009-09-22 16:44:45 -07002096 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002097 }
Felipe Balbi927ce942012-03-14 11:18:27 +02002098 ret = mmc_suspend_host(host->mmc);
2099
2100 if (ret) {
2101 host->suspended = 0;
2102 if (host->pdata->resume) {
2103 ret = host->pdata->resume(dev, host->slot_id);
2104 if (ret)
2105 dev_dbg(dev, "Unmask interrupt failed\n");
2106 }
2107 goto err;
2108 }
2109
2110 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2111 omap_hsmmc_disable_irq(host);
2112 OMAP_HSMMC_WRITE(host->base, HCTL,
2113 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2114 }
2115
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302116 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302117 clk_disable_unprepare(host->dbclk);
Eliad Peller31f9d462011-11-22 16:02:17 +02002118err:
2119 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002120 return ret;
2121}
2122
2123/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002124static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002125{
2126 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002127 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2128
2129 if (!host)
2130 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002131
2132 if (host && !host->suspended)
2133 return 0;
2134
Felipe Balbi927ce942012-03-14 11:18:27 +02002135 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002136
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302137 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302138 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002139
Felipe Balbi927ce942012-03-14 11:18:27 +02002140 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2141 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002142
Felipe Balbi927ce942012-03-14 11:18:27 +02002143 if (host->pdata->resume) {
2144 ret = host->pdata->resume(dev, host->slot_id);
2145 if (ret)
2146 dev_dbg(dev, "Unmask interrupt failed\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002147 }
2148
Felipe Balbi927ce942012-03-14 11:18:27 +02002149 omap_hsmmc_protect_card(host);
2150
2151 /* Notify the core to resume the host */
2152 ret = mmc_resume_host(host->mmc);
2153 if (ret == 0)
2154 host->suspended = 0;
2155
2156 pm_runtime_mark_last_busy(host->dev);
2157 pm_runtime_put_autosuspend(host->dev);
2158
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002159 return ret;
2160
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002161}
2162
2163#else
Denis Karpov70a33412009-09-22 16:44:59 -07002164#define omap_hsmmc_suspend NULL
2165#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002166#endif
2167
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302168static int omap_hsmmc_runtime_suspend(struct device *dev)
2169{
2170 struct omap_hsmmc_host *host;
2171
2172 host = platform_get_drvdata(to_platform_device(dev));
2173 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002174 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302175
2176 return 0;
2177}
2178
2179static int omap_hsmmc_runtime_resume(struct device *dev)
2180{
2181 struct omap_hsmmc_host *host;
2182
2183 host = platform_get_drvdata(to_platform_device(dev));
2184 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002185 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302186
2187 return 0;
2188}
2189
Kevin Hilmana791daa2010-05-26 14:42:07 -07002190static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002191 .suspend = omap_hsmmc_suspend,
2192 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302193 .runtime_suspend = omap_hsmmc_runtime_suspend,
2194 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002195};
2196
2197static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002198 .probe = omap_hsmmc_probe,
2199 .remove = __devexit_p(omap_hsmmc_remove),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002200 .driver = {
2201 .name = DRIVER_NAME,
2202 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002203 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302204 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002205 },
2206};
2207
Felipe Balbib7964502012-03-14 11:18:32 +02002208module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002209MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2210MODULE_LICENSE("GPL");
2211MODULE_ALIAS("platform:" DRIVER_NAME);
2212MODULE_AUTHOR("Texas Instruments Inc");