blob: 9afdd202b8735bf5e36509c1952f9f0e8a86d7be [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 */
Denis Karpov11dd62a2009-09-22 16:44:43 -070047#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010048#define OMAP_HSMMC_CON 0x002C
49#define OMAP_HSMMC_BLK 0x0104
50#define OMAP_HSMMC_ARG 0x0108
51#define OMAP_HSMMC_CMD 0x010C
52#define OMAP_HSMMC_RSP10 0x0110
53#define OMAP_HSMMC_RSP32 0x0114
54#define OMAP_HSMMC_RSP54 0x0118
55#define OMAP_HSMMC_RSP76 0x011C
56#define OMAP_HSMMC_DATA 0x0120
57#define OMAP_HSMMC_HCTL 0x0128
58#define OMAP_HSMMC_SYSCTL 0x012C
59#define OMAP_HSMMC_STAT 0x0130
60#define OMAP_HSMMC_IE 0x0134
61#define OMAP_HSMMC_ISE 0x0138
62#define OMAP_HSMMC_CAPA 0x0140
63
64#define VS18 (1 << 26)
65#define VS30 (1 << 25)
66#define SDVS18 (0x5 << 9)
67#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080068#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010069#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010070#define SDVSCLR 0xFFFFF1FF
71#define SDVSDET 0x00000400
72#define AUTOIDLE 0x1
73#define SDBP (1 << 8)
74#define DTO 0xe
75#define ICE 0x1
76#define ICS 0x2
77#define CEN (1 << 2)
78#define CLKD_MASK 0x0000FFC0
79#define CLKD_SHIFT 6
80#define DTO_MASK 0x000F0000
81#define DTO_SHIFT 16
82#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070083#define BWR_ENABLE (1 << 4)
84#define BRR_ENABLE (1 << 5)
Adrian Hunter93caf8e692010-08-11 14:17:48 -070085#define DTO_ENABLE (1 << 20)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010086#define INIT_STREAM (1 << 1)
87#define DP_SELECT (1 << 21)
88#define DDIR (1 << 4)
89#define DMA_EN 0x1
90#define MSBS (1 << 5)
91#define BCE (1 << 1)
92#define FOUR_BIT (1 << 1)
Balaji T K03b5d922012-04-09 12:08:33 +053093#define DDR (1 << 19)
Jarkko Lavinen73153012008-11-21 16:49:54 +020094#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010095#define CC 0x1
96#define TC 0x02
97#define OD 0x1
98#define ERR (1 << 15)
99#define CMD_TIMEOUT (1 << 16)
100#define DATA_TIMEOUT (1 << 20)
101#define CMD_CRC (1 << 17)
102#define DATA_CRC (1 << 21)
103#define CARD_ERR (1 << 28)
104#define STAT_CLEAR 0xFFFFFFFF
105#define INIT_STREAM_CMD 0x00000000
106#define DUAL_VOLT_OCR_BIT 7
107#define SRC (1 << 25)
108#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700109#define SOFTRESET (1 << 1)
110#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100111
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530112#define MMC_AUTOSUSPEND_DELAY 100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100113#define MMC_TIMEOUT_MS 20
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400114#define OMAP_MMC_MIN_CLOCK 400000
115#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530116#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100117
118/*
119 * One controller can have multiple slots, like on some omap boards using
120 * omap.c controller driver. Luckily this is not currently done on any known
121 * omap_hsmmc.c device.
122 */
123#define mmc_slot(host) (host->pdata->slots[host->slot_id])
124
125/*
126 * MMC Host controller read/write API's
127 */
128#define OMAP_HSMMC_READ(base, reg) \
129 __raw_readl((base) + OMAP_HSMMC_##reg)
130
131#define OMAP_HSMMC_WRITE(base, reg, val) \
132 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
133
Per Forlin9782aff2011-07-01 18:55:23 +0200134struct omap_hsmmc_next {
135 unsigned int dma_len;
136 s32 cookie;
137};
138
Denis Karpov70a33412009-09-22 16:44:59 -0700139struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100140 struct device *dev;
141 struct mmc_host *mmc;
142 struct mmc_request *mrq;
143 struct mmc_command *cmd;
144 struct mmc_data *data;
145 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100146 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800147 /*
148 * vcc == configured supply
149 * vcc_aux == optional
150 * - MMC1, supply for DAT4..DAT7
151 * - MMC2/MMC2, external level shifter voltage supply, for
152 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
153 */
154 struct regulator *vcc;
155 struct regulator *vcc_aux;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100156 void __iomem *base;
157 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700158 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100159 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200160 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100161 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700162 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100163 int suspended;
164 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100165 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100166 struct dma_chan *tx_chan;
167 struct dma_chan *rx_chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100168 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200169 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700170 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700171 int protect_card;
172 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800173 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700174 int req_in_progress;
Per Forlin9782aff2011-07-01 18:55:23 +0200175 struct omap_hsmmc_next next_data;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700176
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100177 struct omap_mmc_platform_data *pdata;
178};
179
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800180static int omap_hsmmc_card_detect(struct device *dev, int slot)
181{
182 struct omap_mmc_platform_data *mmc = dev->platform_data;
183
184 /* NOTE: assumes card detect signal is active-low */
185 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
186}
187
188static int omap_hsmmc_get_wp(struct device *dev, int slot)
189{
190 struct omap_mmc_platform_data *mmc = dev->platform_data;
191
192 /* NOTE: assumes write protect signal is active-high */
193 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
194}
195
196static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
197{
198 struct omap_mmc_platform_data *mmc = dev->platform_data;
199
200 /* NOTE: assumes card detect signal is active-low */
201 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
202}
203
204#ifdef CONFIG_PM
205
206static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
207{
208 struct omap_mmc_platform_data *mmc = dev->platform_data;
209
210 disable_irq(mmc->slots[0].card_detect_irq);
211 return 0;
212}
213
214static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
215{
216 struct omap_mmc_platform_data *mmc = dev->platform_data;
217
218 enable_irq(mmc->slots[0].card_detect_irq);
219 return 0;
220}
221
222#else
223
224#define omap_hsmmc_suspend_cdirq NULL
225#define omap_hsmmc_resume_cdirq NULL
226
227#endif
228
Adrian Hunterb702b102010-02-15 10:03:35 -0800229#ifdef CONFIG_REGULATOR
230
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500231static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800232 int vdd)
233{
234 struct omap_hsmmc_host *host =
235 platform_get_drvdata(to_platform_device(dev));
236 int ret = 0;
237
238 /*
239 * If we don't see a Vcc regulator, assume it's a fixed
240 * voltage always-on regulator.
241 */
242 if (!host->vcc)
243 return 0;
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530244 /*
245 * With DT, never turn OFF the regulator. This is because
246 * the pbias cell programming support is still missing when
247 * booting with Device tree
248 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +0530249 if (dev->of_node && !vdd)
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530250 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800251
252 if (mmc_slot(host).before_set_reg)
253 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
254
255 /*
256 * Assume Vcc regulator is used only to power the card ... OMAP
257 * VDDS is used to power the pins, optionally with a transceiver to
258 * support cards using voltages other than VDDS (1.8V nominal). When a
259 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
260 *
261 * In some cases this regulator won't support enable/disable;
262 * e.g. it's a fixed rail for a WLAN chip.
263 *
264 * In other cases vcc_aux switches interface power. Example, for
265 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
266 * chips/cards need an interface voltage rail too.
267 */
268 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400269 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800270 /* Enable interface voltage rail, if needed */
271 if (ret == 0 && host->vcc_aux) {
272 ret = regulator_enable(host->vcc_aux);
273 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400274 ret = mmc_regulator_set_ocr(host->mmc,
275 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800276 }
277 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400278 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800279 if (host->vcc_aux)
280 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400281 if (!ret) {
282 /* Then proceed to shut down the local regulator */
283 ret = mmc_regulator_set_ocr(host->mmc,
284 host->vcc, 0);
285 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800286 }
287
288 if (mmc_slot(host).after_set_reg)
289 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
290
291 return ret;
292}
293
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800294static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
295{
296 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700297 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800298
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800299 reg = regulator_get(host->dev, "vmmc");
300 if (IS_ERR(reg)) {
301 dev_dbg(host->dev, "vmmc regulator missing\n");
NeilBrown1fdc90f2012-08-08 00:06:00 -0400302 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800303 } else {
NeilBrown1fdc90f2012-08-08 00:06:00 -0400304 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800305 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700306 ocr_value = mmc_regulator_get_ocrmask(reg);
307 if (!mmc_slot(host).ocr_mask) {
308 mmc_slot(host).ocr_mask = ocr_value;
309 } else {
310 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530311 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500312 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700313 mmc_slot(host).ocr_mask = 0;
314 return -EINVAL;
315 }
316 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800317
318 /* Allow an aux regulator */
319 reg = regulator_get(host->dev, "vmmc_aux");
320 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
321
Balaji T Kb1c1df72011-05-30 19:55:34 +0530322 /* For eMMC do not power off when not in sleep state */
323 if (mmc_slot(host).no_regulator_off_init)
324 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800325 /*
326 * UGLY HACK: workaround regulator framework bugs.
327 * When the bootloader leaves a supply active, it's
328 * initialized with zero usecount ... and we can't
329 * disable it without first enabling it. Until the
330 * framework is fixed, we need a workaround like this
331 * (which is safe for MMC, but not in general).
332 */
Adrian Huntere840ce12011-05-06 12:14:10 +0300333 if (regulator_is_enabled(host->vcc) > 0 ||
334 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
335 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
336
337 mmc_slot(host).set_power(host->dev, host->slot_id,
338 1, vdd);
339 mmc_slot(host).set_power(host->dev, host->slot_id,
340 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800341 }
342 }
343
344 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800345}
346
347static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
348{
349 regulator_put(host->vcc);
350 regulator_put(host->vcc_aux);
351 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800352}
353
Adrian Hunterb702b102010-02-15 10:03:35 -0800354static inline int omap_hsmmc_have_reg(void)
355{
356 return 1;
357}
358
359#else
360
361static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
362{
363 return -EINVAL;
364}
365
366static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
367{
368}
369
370static inline int omap_hsmmc_have_reg(void)
371{
372 return 0;
373}
374
375#endif
376
377static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
378{
379 int ret;
380
381 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800382 if (pdata->slots[0].cover)
383 pdata->slots[0].get_cover_state =
384 omap_hsmmc_get_cover_state;
385 else
386 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
387 pdata->slots[0].card_detect_irq =
388 gpio_to_irq(pdata->slots[0].switch_pin);
389 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
390 if (ret)
391 return ret;
392 ret = gpio_direction_input(pdata->slots[0].switch_pin);
393 if (ret)
394 goto err_free_sp;
395 } else
396 pdata->slots[0].switch_pin = -EINVAL;
397
398 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
399 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
400 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
401 if (ret)
402 goto err_free_cd;
403 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
404 if (ret)
405 goto err_free_wp;
406 } else
407 pdata->slots[0].gpio_wp = -EINVAL;
408
409 return 0;
410
411err_free_wp:
412 gpio_free(pdata->slots[0].gpio_wp);
413err_free_cd:
414 if (gpio_is_valid(pdata->slots[0].switch_pin))
415err_free_sp:
416 gpio_free(pdata->slots[0].switch_pin);
417 return ret;
418}
419
420static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
421{
422 if (gpio_is_valid(pdata->slots[0].gpio_wp))
423 gpio_free(pdata->slots[0].gpio_wp);
424 if (gpio_is_valid(pdata->slots[0].switch_pin))
425 gpio_free(pdata->slots[0].switch_pin);
426}
427
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100428/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300429 * Start clock to the card
430 */
431static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
432{
433 OMAP_HSMMC_WRITE(host->base, SYSCTL,
434 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
435}
436
437/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100438 * Stop clock to the card
439 */
Denis Karpov70a33412009-09-22 16:44:59 -0700440static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100441{
442 OMAP_HSMMC_WRITE(host->base, SYSCTL,
443 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
444 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
445 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
446}
447
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700448static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
449 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700450{
451 unsigned int irq_mask;
452
453 if (host->use_dma)
454 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
455 else
456 irq_mask = INT_EN_MASK;
457
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700458 /* Disable timeout for erases */
459 if (cmd->opcode == MMC_ERASE)
460 irq_mask &= ~DTO_ENABLE;
461
Adrian Hunterb4175772010-05-26 14:42:06 -0700462 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
463 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
464 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
465}
466
467static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
468{
469 OMAP_HSMMC_WRITE(host->base, ISE, 0);
470 OMAP_HSMMC_WRITE(host->base, IE, 0);
471 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
472}
473
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300474/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530475static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300476{
477 u16 dsor = 0;
478
479 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530480 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300481 if (dsor > 250)
482 dsor = 250;
483 }
484
485 return dsor;
486}
487
Andy Shevchenko5934df22011-05-06 12:14:06 +0300488static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
489{
490 struct mmc_ios *ios = &host->mmc->ios;
491 unsigned long regval;
492 unsigned long timeout;
493
Venkatraman S8986d312012-08-07 19:10:38 +0530494 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300495
496 omap_hsmmc_stop_clock(host);
497
498 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
499 regval = regval & ~(CLKD_MASK | DTO_MASK);
Balaji TKd83b6e02011-12-20 15:12:00 +0530500 regval = regval | (calc_divisor(host, ios) << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300501 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
502 OMAP_HSMMC_WRITE(host->base, SYSCTL,
503 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
504
505 /* Wait till the ICS bit is set */
506 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
507 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
508 && time_before(jiffies, timeout))
509 cpu_relax();
510
511 omap_hsmmc_start_clock(host);
512}
513
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400514static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
515{
516 struct mmc_ios *ios = &host->mmc->ios;
517 u32 con;
518
519 con = OMAP_HSMMC_READ(host->base, CON);
Balaji T K03b5d922012-04-09 12:08:33 +0530520 if (ios->timing == MMC_TIMING_UHS_DDR50)
521 con |= DDR; /* configure in DDR mode */
522 else
523 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400524 switch (ios->bus_width) {
525 case MMC_BUS_WIDTH_8:
526 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
527 break;
528 case MMC_BUS_WIDTH_4:
529 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
530 OMAP_HSMMC_WRITE(host->base, HCTL,
531 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
532 break;
533 case MMC_BUS_WIDTH_1:
534 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
535 OMAP_HSMMC_WRITE(host->base, HCTL,
536 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
537 break;
538 }
539}
540
541static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
542{
543 struct mmc_ios *ios = &host->mmc->ios;
544 u32 con;
545
546 con = OMAP_HSMMC_READ(host->base, CON);
547 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
548 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
549 else
550 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
551}
552
Denis Karpov11dd62a2009-09-22 16:44:43 -0700553#ifdef CONFIG_PM
554
555/*
556 * Restore the MMC host context, if it was lost as result of a
557 * power state change.
558 */
Denis Karpov70a33412009-09-22 16:44:59 -0700559static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700560{
561 struct mmc_ios *ios = &host->mmc->ios;
562 struct omap_mmc_platform_data *pdata = host->pdata;
563 int context_loss = 0;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400564 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700565 unsigned long timeout;
566
567 if (pdata->get_context_loss_count) {
568 context_loss = pdata->get_context_loss_count(host->dev);
569 if (context_loss < 0)
570 return 1;
571 }
572
573 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
574 context_loss == host->context_loss ? "not " : "");
575 if (host->context_loss == context_loss)
576 return 1;
577
Venkatraman S6c31b212012-08-08 14:26:52 +0530578 if (!OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE)
579 return 1;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700580
Balaji T Kc2200ef2012-03-07 09:55:30 -0500581 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700582 if (host->power_mode != MMC_POWER_OFF &&
583 (1 << ios->vdd) <= MMC_VDD_23_24)
584 hctl = SDVS18;
585 else
586 hctl = SDVS30;
587 capa = VS30 | VS18;
588 } else {
589 hctl = SDVS18;
590 capa = VS18;
591 }
592
593 OMAP_HSMMC_WRITE(host->base, HCTL,
594 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
595
596 OMAP_HSMMC_WRITE(host->base, CAPA,
597 OMAP_HSMMC_READ(host->base, CAPA) | capa);
598
599 OMAP_HSMMC_WRITE(host->base, HCTL,
600 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
601
602 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
603 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
604 && time_before(jiffies, timeout))
605 ;
606
Adrian Hunterb4175772010-05-26 14:42:06 -0700607 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700608
609 /* Do not initialize card-specific things if the power is off */
610 if (host->power_mode == MMC_POWER_OFF)
611 goto out;
612
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400613 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700614
Andy Shevchenko5934df22011-05-06 12:14:06 +0300615 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700616
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400617 omap_hsmmc_set_bus_mode(host);
618
Denis Karpov11dd62a2009-09-22 16:44:43 -0700619out:
620 host->context_loss = context_loss;
621
622 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
623 return 0;
624}
625
626/*
627 * Save the MMC host context (store the number of power state changes so far).
628 */
Denis Karpov70a33412009-09-22 16:44:59 -0700629static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700630{
631 struct omap_mmc_platform_data *pdata = host->pdata;
632 int context_loss;
633
634 if (pdata->get_context_loss_count) {
635 context_loss = pdata->get_context_loss_count(host->dev);
636 if (context_loss < 0)
637 return;
638 host->context_loss = context_loss;
639 }
640}
641
642#else
643
Denis Karpov70a33412009-09-22 16:44:59 -0700644static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700645{
646 return 0;
647}
648
Denis Karpov70a33412009-09-22 16:44:59 -0700649static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700650{
651}
652
653#endif
654
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100655/*
656 * Send init stream sequence to card
657 * before sending IDLE command
658 */
Denis Karpov70a33412009-09-22 16:44:59 -0700659static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100660{
661 int reg = 0;
662 unsigned long timeout;
663
Adrian Hunterb62f6222009-09-22 16:45:01 -0700664 if (host->protect_card)
665 return;
666
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100667 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700668
669 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100670 OMAP_HSMMC_WRITE(host->base, CON,
671 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
672 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
673
674 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
675 while ((reg != CC) && time_before(jiffies, timeout))
676 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
677
678 OMAP_HSMMC_WRITE(host->base, CON,
679 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700680
681 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
682 OMAP_HSMMC_READ(host->base, STAT);
683
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100684 enable_irq(host->irq);
685}
686
687static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700688int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100689{
690 int r = 1;
691
Denis Karpov191d1f12009-09-22 16:44:55 -0700692 if (mmc_slot(host).get_cover_state)
693 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100694 return r;
695}
696
697static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700698omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100699 char *buf)
700{
701 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700702 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100703
Denis Karpov70a33412009-09-22 16:44:59 -0700704 return sprintf(buf, "%s\n",
705 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100706}
707
Denis Karpov70a33412009-09-22 16:44:59 -0700708static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100709
710static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700711omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100712 char *buf)
713{
714 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700715 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100716
Denis Karpov191d1f12009-09-22 16:44:55 -0700717 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100718}
719
Denis Karpov70a33412009-09-22 16:44:59 -0700720static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100721
722/*
723 * Configure the response type and send the cmd.
724 */
725static void
Denis Karpov70a33412009-09-22 16:44:59 -0700726omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100727 struct mmc_data *data)
728{
729 int cmdreg = 0, resptype = 0, cmdtype = 0;
730
Venkatraman S8986d312012-08-07 19:10:38 +0530731 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100732 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
733 host->cmd = cmd;
734
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700735 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100736
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200737 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100738 if (cmd->flags & MMC_RSP_PRESENT) {
739 if (cmd->flags & MMC_RSP_136)
740 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200741 else if (cmd->flags & MMC_RSP_BUSY) {
742 resptype = 3;
743 host->response_busy = 1;
744 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100745 resptype = 2;
746 }
747
748 /*
749 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
750 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
751 * a val of 0x3, rest 0x0.
752 */
753 if (cmd == host->mrq->stop)
754 cmdtype = 0x3;
755
756 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
757
758 if (data) {
759 cmdreg |= DP_SELECT | MSBS | BCE;
760 if (data->flags & MMC_DATA_READ)
761 cmdreg |= DDIR;
762 else
763 cmdreg &= ~(DDIR);
764 }
765
766 if (host->use_dma)
767 cmdreg |= DMA_EN;
768
Adrian Hunterb4175772010-05-26 14:42:06 -0700769 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700770
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100771 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
772 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
773}
774
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200775static int
Denis Karpov70a33412009-09-22 16:44:59 -0700776omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200777{
778 if (data->flags & MMC_DATA_WRITE)
779 return DMA_TO_DEVICE;
780 else
781 return DMA_FROM_DEVICE;
782}
783
Russell Kingc5c98922012-04-13 12:14:39 +0100784static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
785 struct mmc_data *data)
786{
787 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
788}
789
Adrian Hunterb4175772010-05-26 14:42:06 -0700790static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
791{
792 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530793 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700794
Venkatraman S31463b12012-04-09 12:08:34 +0530795 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700796 host->req_in_progress = 0;
797 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530798 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700799
800 omap_hsmmc_disable_irq(host);
801 /* Do not complete the request if DMA is still in progress */
802 if (mrq->data && host->use_dma && dma_ch != -1)
803 return;
804 host->mrq = NULL;
805 mmc_request_done(host->mmc, mrq);
806}
807
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100808/*
809 * Notify the transfer complete to MMC core
810 */
811static void
Denis Karpov70a33412009-09-22 16:44:59 -0700812omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100813{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200814 if (!data) {
815 struct mmc_request *mrq = host->mrq;
816
Adrian Hunter23050102009-09-22 16:44:57 -0700817 /* TC before CC from CMD6 - don't know why, but it happens */
818 if (host->cmd && host->cmd->opcode == 6 &&
819 host->response_busy) {
820 host->response_busy = 0;
821 return;
822 }
823
Adrian Hunterb4175772010-05-26 14:42:06 -0700824 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200825 return;
826 }
827
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100828 host->data = NULL;
829
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100830 if (!data->error)
831 data->bytes_xfered += data->blocks * (data->blksz);
832 else
833 data->bytes_xfered = 0;
834
Ming Leife852272012-06-22 18:49:35 +0800835 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700836 omap_hsmmc_request_done(host, data->mrq);
Ming Leife852272012-06-22 18:49:35 +0800837 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100838 }
Ming Leife852272012-06-22 18:49:35 +0800839 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100840}
841
842/*
843 * Notify the core about command completion
844 */
845static void
Denis Karpov70a33412009-09-22 16:44:59 -0700846omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100847{
848 host->cmd = NULL;
849
850 if (cmd->flags & MMC_RSP_PRESENT) {
851 if (cmd->flags & MMC_RSP_136) {
852 /* response type 2 */
853 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
854 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
855 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
856 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
857 } else {
858 /* response types 1, 1b, 3, 4, 5, 6 */
859 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
860 }
861 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700862 if ((host->data == NULL && !host->response_busy) || cmd->error)
863 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100864}
865
866/*
867 * DMA clean up for command errors
868 */
Denis Karpov70a33412009-09-22 16:44:59 -0700869static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100870{
Adrian Hunterb4175772010-05-26 14:42:06 -0700871 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530872 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700873
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200874 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100875
Venkatraman S31463b12012-04-09 12:08:34 +0530876 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700877 dma_ch = host->dma_ch;
878 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530879 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700880
881 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100882 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
883
884 dmaengine_terminate_all(chan);
885 dma_unmap_sg(chan->device->dev,
886 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700887 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100888
Per Forlin053bf342011-11-07 21:55:11 +0530889 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100890 }
891 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100892}
893
894/*
895 * Readable error output
896 */
897#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300898static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100899{
900 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700901 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300902 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
903 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
904 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
905 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100906 };
907 char res[256];
908 char *buf = res;
909 int len, i;
910
911 len = sprintf(buf, "MMC IRQ 0x%x :", status);
912 buf += len;
913
Denis Karpov70a33412009-09-22 16:44:59 -0700914 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100915 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700916 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100917 buf += len;
918 }
919
Venkatraman S8986d312012-08-07 19:10:38 +0530920 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100921}
Adrian Hunter699b9582011-05-06 12:14:01 +0300922#else
923static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
924 u32 status)
925{
926}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100927#endif /* CONFIG_MMC_DEBUG */
928
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100929/*
930 * MMC controller internal state machines reset
931 *
932 * Used to reset command or data internal state machines, using respectively
933 * SRC or SRD bit of SYSCTL register
934 * Can be called from interrupt context
935 */
Denis Karpov70a33412009-09-22 16:44:59 -0700936static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
937 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100938{
939 unsigned long i = 0;
940 unsigned long limit = (loops_per_jiffy *
941 msecs_to_jiffies(MMC_TIMEOUT_MS));
942
943 OMAP_HSMMC_WRITE(host->base, SYSCTL,
944 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
945
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700946 /*
947 * OMAP4 ES2 and greater has an updated reset logic.
948 * Monitor a 0->1 transition first
949 */
950 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -0500951 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700952 && (i++ < limit))
953 cpu_relax();
954 }
955 i = 0;
956
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100957 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
958 (i++ < limit))
959 cpu_relax();
960
961 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
962 dev_err(mmc_dev(host->mmc),
963 "Timeout waiting on controller reset in %s\n",
964 __func__);
965}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100966
Venkatraman Sae4bf782012-08-09 20:36:07 +0530967static void hsmmc_command_incomplete(struct omap_hsmmc_host *host, int err)
968{
969 omap_hsmmc_reset_controller_fsm(host, SRC);
970 host->cmd->error = err;
971
972 if (host->data) {
973 omap_hsmmc_reset_controller_fsm(host, SRD);
974 omap_hsmmc_dma_cleanup(host, err);
975 }
976
977}
978
Adrian Hunterb4175772010-05-26 14:42:06 -0700979static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100980{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100981 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -0700982 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100983
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100984 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +0530985 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100986
987 if (status & ERR) {
Adrian Hunter699b9582011-05-06 12:14:01 +0300988 omap_hsmmc_dbg_report_irq(host, status);
Venkatraman Sae4bf782012-08-09 20:36:07 +0530989 if (status & (CMD_TIMEOUT | DATA_TIMEOUT))
990 hsmmc_command_incomplete(host, -ETIMEDOUT);
991 else if (status & (CMD_CRC | DATA_CRC))
992 hsmmc_command_incomplete(host, -EILSEQ);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200993
Venkatraman Sae4bf782012-08-09 20:36:07 +0530994 end_cmd = 1;
995 if (host->data || host->response_busy) {
996 end_trans = 1;
997 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100998 }
999 }
1000
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001001 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001002 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001003 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001004 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001005}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001006
Adrian Hunterb4175772010-05-26 14:42:06 -07001007/*
1008 * MMC controller IRQ handler
1009 */
1010static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1011{
1012 struct omap_hsmmc_host *host = dev_id;
1013 int status;
1014
1015 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301016 while (status & INT_EN_MASK && host->req_in_progress) {
Adrian Hunterb4175772010-05-26 14:42:06 -07001017 omap_hsmmc_do_irq(host, status);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301018
Adrian Hunterb4175772010-05-26 14:42:06 -07001019 /* Flush posted write */
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301020 OMAP_HSMMC_WRITE(host->base, STAT, status);
Adrian Hunterb4175772010-05-26 14:42:06 -07001021 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301022 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001023
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001024 return IRQ_HANDLED;
1025}
1026
Denis Karpov70a33412009-09-22 16:44:59 -07001027static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001028{
1029 unsigned long i;
1030
1031 OMAP_HSMMC_WRITE(host->base, HCTL,
1032 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1033 for (i = 0; i < loops_per_jiffy; i++) {
1034 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1035 break;
1036 cpu_relax();
1037 }
1038}
1039
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001040/*
David Brownelleb250822009-02-17 14:49:01 -08001041 * Switch MMC interface voltage ... only relevant for MMC1.
1042 *
1043 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1044 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1045 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001046 */
Denis Karpov70a33412009-09-22 16:44:59 -07001047static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001048{
1049 u32 reg_val = 0;
1050 int ret;
1051
1052 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301053 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301054 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301055 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001056
1057 /* Turn the power off */
1058 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001059
1060 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001061 if (!ret)
1062 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1063 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301064 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301065 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301066 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001067
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001068 if (ret != 0)
1069 goto err;
1070
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001071 OMAP_HSMMC_WRITE(host->base, HCTL,
1072 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1073 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001074
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001075 /*
1076 * If a MMC dual voltage card is detected, the set_ios fn calls
1077 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001078 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001079 *
David Brownelleb250822009-02-17 14:49:01 -08001080 * Cope with a bit of slop in the range ... per data sheets:
1081 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1082 * but recommended values are 1.71V to 1.89V
1083 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1084 * but recommended values are 2.7V to 3.3V
1085 *
1086 * Board setup code shouldn't permit anything very out-of-range.
1087 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1088 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001089 */
David Brownelleb250822009-02-17 14:49:01 -08001090 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001091 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001092 else
1093 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001094
1095 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001096 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001097
1098 return 0;
1099err:
1100 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1101 return ret;
1102}
1103
Adrian Hunterb62f6222009-09-22 16:45:01 -07001104/* Protect the card while the cover is open */
1105static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1106{
1107 if (!mmc_slot(host).get_cover_state)
1108 return;
1109
1110 host->reqs_blocked = 0;
1111 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1112 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301113 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001114 "card is now accessible\n",
1115 mmc_hostname(host->mmc));
1116 host->protect_card = 0;
1117 }
1118 } else {
1119 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301120 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001121 "card is now inaccessible\n",
1122 mmc_hostname(host->mmc));
1123 host->protect_card = 1;
1124 }
1125 }
1126}
1127
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001128/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001129 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001130 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001131static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001132{
NeilBrown7efab4f2011-12-30 12:35:13 +11001133 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001134 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001135 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001136
Adrian Huntera6b22402009-09-22 16:44:45 -07001137 if (host->suspended)
NeilBrown7efab4f2011-12-30 12:35:13 +11001138 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001139
1140 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001141
Denis Karpov191d1f12009-09-22 16:44:55 -07001142 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001143 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001144 else {
1145 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001146 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001147 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001148
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001149 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001150 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001151 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001152 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001153 return IRQ_HANDLED;
1154}
1155
Russell Kingc5c98922012-04-13 12:14:39 +01001156static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001157{
Russell Kingc5c98922012-04-13 12:14:39 +01001158 struct omap_hsmmc_host *host = param;
1159 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001160 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001161 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001162
Russell Kingc5c98922012-04-13 12:14:39 +01001163 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001164 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001165 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001166 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001167 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001168
Adrian Hunter770d7432011-05-06 12:14:11 +03001169 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001170 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001171 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001172 dma_unmap_sg(chan->device->dev,
1173 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001174 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001175
1176 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001177 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001178 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001179
1180 /* If DMA has finished after TC, complete the request */
1181 if (!req_in_progress) {
1182 struct mmc_request *mrq = host->mrq;
1183
1184 host->mrq = NULL;
1185 mmc_request_done(host->mmc, mrq);
1186 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001187}
1188
Per Forlin9782aff2011-07-01 18:55:23 +02001189static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1190 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001191 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001192 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001193{
1194 int dma_len;
1195
1196 if (!next && data->host_cookie &&
1197 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301198 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001199 " host->next_data.cookie %d\n",
1200 __func__, data->host_cookie, host->next_data.cookie);
1201 data->host_cookie = 0;
1202 }
1203
1204 /* Check if next job is already prepared */
1205 if (next ||
1206 (!next && data->host_cookie != host->next_data.cookie)) {
Russell King26b88522012-04-13 12:27:37 +01001207 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001208 omap_hsmmc_get_dma_dir(host, data));
1209
1210 } else {
1211 dma_len = host->next_data.dma_len;
1212 host->next_data.dma_len = 0;
1213 }
1214
1215
1216 if (dma_len == 0)
1217 return -EINVAL;
1218
1219 if (next) {
1220 next->dma_len = dma_len;
1221 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1222 } else
1223 host->dma_len = dma_len;
1224
1225 return 0;
1226}
1227
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001228/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001229 * Routine to configure and start DMA for the MMC card
1230 */
Denis Karpov70a33412009-09-22 16:44:59 -07001231static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1232 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001233{
Russell King26b88522012-04-13 12:27:37 +01001234 struct dma_slave_config cfg;
1235 struct dma_async_tx_descriptor *tx;
1236 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001237 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001238 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001239
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001240 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001241 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001242 struct scatterlist *sgl;
1243
1244 sgl = data->sg + i;
1245 if (sgl->length % data->blksz)
1246 return -EINVAL;
1247 }
1248 if ((data->blksz % 4) != 0)
1249 /* REVISIT: The MMC buffer increments only when MSB is written.
1250 * Return error for blksz which is non multiple of four.
1251 */
1252 return -EINVAL;
1253
Adrian Hunterb4175772010-05-26 14:42:06 -07001254 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001255
Russell Kingc5c98922012-04-13 12:14:39 +01001256 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001257
Russell King26b88522012-04-13 12:27:37 +01001258 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1259 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1260 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1261 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1262 cfg.src_maxburst = data->blksz / 4;
1263 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001264
Russell King26b88522012-04-13 12:27:37 +01001265 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001266 if (ret)
1267 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001268
Russell King26b88522012-04-13 12:27:37 +01001269 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1270 if (ret)
1271 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001272
Russell King26b88522012-04-13 12:27:37 +01001273 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1274 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1275 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1276 if (!tx) {
1277 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1278 /* FIXME: cleanup */
1279 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001280 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001281
Russell King26b88522012-04-13 12:27:37 +01001282 tx->callback = omap_hsmmc_dma_callback;
1283 tx->callback_param = host;
1284
1285 /* Does not fail */
1286 dmaengine_submit(tx);
1287
1288 host->dma_ch = 1;
1289
1290 dma_async_issue_pending(chan);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001291
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001292 return 0;
1293}
1294
Denis Karpov70a33412009-09-22 16:44:59 -07001295static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001296 unsigned int timeout_ns,
1297 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001298{
1299 unsigned int timeout, cycle_ns;
1300 uint32_t reg, clkd, dto = 0;
1301
1302 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1303 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1304 if (clkd == 0)
1305 clkd = 1;
1306
1307 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001308 timeout = timeout_ns / cycle_ns;
1309 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001310 if (timeout) {
1311 while ((timeout & 0x80000000) == 0) {
1312 dto += 1;
1313 timeout <<= 1;
1314 }
1315 dto = 31 - dto;
1316 timeout <<= 1;
1317 if (timeout && dto)
1318 dto += 1;
1319 if (dto >= 13)
1320 dto -= 13;
1321 else
1322 dto = 0;
1323 if (dto > 14)
1324 dto = 14;
1325 }
1326
1327 reg &= ~DTO_MASK;
1328 reg |= dto << DTO_SHIFT;
1329 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1330}
1331
1332/*
1333 * Configure block length for MMC/SD cards and initiate the transfer.
1334 */
1335static int
Denis Karpov70a33412009-09-22 16:44:59 -07001336omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001337{
1338 int ret;
1339 host->data = req->data;
1340
1341 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001342 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001343 /*
1344 * Set an arbitrary 100ms data timeout for commands with
1345 * busy signal.
1346 */
1347 if (req->cmd->flags & MMC_RSP_BUSY)
1348 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001349 return 0;
1350 }
1351
1352 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1353 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001354 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001355
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001356 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001357 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001358 if (ret != 0) {
1359 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1360 return ret;
1361 }
1362 }
1363 return 0;
1364}
1365
Per Forlin9782aff2011-07-01 18:55:23 +02001366static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1367 int err)
1368{
1369 struct omap_hsmmc_host *host = mmc_priv(mmc);
1370 struct mmc_data *data = mrq->data;
1371
Russell King26b88522012-04-13 12:27:37 +01001372 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001373 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001374
Russell King26b88522012-04-13 12:27:37 +01001375 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1376 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001377 data->host_cookie = 0;
1378 }
1379}
1380
1381static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1382 bool is_first_req)
1383{
1384 struct omap_hsmmc_host *host = mmc_priv(mmc);
1385
1386 if (mrq->data->host_cookie) {
1387 mrq->data->host_cookie = 0;
1388 return ;
1389 }
1390
Russell Kingc5c98922012-04-13 12:14:39 +01001391 if (host->use_dma) {
1392 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001393
Per Forlin9782aff2011-07-01 18:55:23 +02001394 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001395 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001396 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001397 }
Per Forlin9782aff2011-07-01 18:55:23 +02001398}
1399
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001400/*
1401 * Request function. for read/write operation
1402 */
Denis Karpov70a33412009-09-22 16:44:59 -07001403static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001404{
Denis Karpov70a33412009-09-22 16:44:59 -07001405 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001406 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001407
Adrian Hunterb4175772010-05-26 14:42:06 -07001408 BUG_ON(host->req_in_progress);
1409 BUG_ON(host->dma_ch != -1);
1410 if (host->protect_card) {
1411 if (host->reqs_blocked < 3) {
1412 /*
1413 * Ensure the controller is left in a consistent
1414 * state by resetting the command and data state
1415 * machines.
1416 */
1417 omap_hsmmc_reset_controller_fsm(host, SRD);
1418 omap_hsmmc_reset_controller_fsm(host, SRC);
1419 host->reqs_blocked += 1;
1420 }
1421 req->cmd->error = -EBADF;
1422 if (req->data)
1423 req->data->error = -EBADF;
1424 req->cmd->retries = 0;
1425 mmc_request_done(mmc, req);
1426 return;
1427 } else if (host->reqs_blocked)
1428 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001429 WARN_ON(host->mrq != NULL);
1430 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001431 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001432 if (err) {
1433 req->cmd->error = err;
1434 if (req->data)
1435 req->data->error = err;
1436 host->mrq = NULL;
1437 mmc_request_done(mmc, req);
1438 return;
1439 }
1440
Denis Karpov70a33412009-09-22 16:44:59 -07001441 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001442}
1443
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001444/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001445static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001446{
Denis Karpov70a33412009-09-22 16:44:59 -07001447 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001448 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001449
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301450 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001451
Adrian Huntera3621462009-09-22 16:44:42 -07001452 if (ios->power_mode != host->power_mode) {
1453 switch (ios->power_mode) {
1454 case MMC_POWER_OFF:
1455 mmc_slot(host).set_power(host->dev, host->slot_id,
1456 0, 0);
1457 break;
1458 case MMC_POWER_UP:
1459 mmc_slot(host).set_power(host->dev, host->slot_id,
1460 1, ios->vdd);
1461 break;
1462 case MMC_POWER_ON:
1463 do_send_init_stream = 1;
1464 break;
1465 }
1466 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001467 }
1468
Denis Karpovdd498ef2009-09-22 16:44:49 -07001469 /* FIXME: set registers based only on changes to ios */
1470
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001471 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001472
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301473 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001474 /* Only MMC1 can interface at 3V without some flavor
1475 * of external transceiver; but they all handle 1.8V.
1476 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001477 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Rajendra Nayak1f84b712012-03-12 20:32:38 +05301478 (ios->vdd == DUAL_VOLT_OCR_BIT) &&
1479 /*
1480 * With pbias cell programming missing, this
1481 * can't be allowed when booting with device
1482 * tree.
1483 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +05301484 !host->dev->of_node) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001485 /*
1486 * The mmc_select_voltage fn of the core does
1487 * not seem to set the power_mode to
1488 * MMC_POWER_UP upon recalculating the voltage.
1489 * vdd 1.8v.
1490 */
Denis Karpov70a33412009-09-22 16:44:59 -07001491 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1492 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001493 "Switch operation failed\n");
1494 }
1495 }
1496
Andy Shevchenko5934df22011-05-06 12:14:06 +03001497 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001498
Adrian Huntera3621462009-09-22 16:44:42 -07001499 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001500 send_init_stream(host);
1501
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001502 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001503
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301504 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001505}
1506
1507static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1508{
Denis Karpov70a33412009-09-22 16:44:59 -07001509 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001510
Denis Karpov191d1f12009-09-22 16:44:55 -07001511 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001512 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001513 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001514}
1515
1516static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1517{
Denis Karpov70a33412009-09-22 16:44:59 -07001518 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001519
Denis Karpov191d1f12009-09-22 16:44:55 -07001520 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001521 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001522 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001523}
1524
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001525static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1526{
1527 struct omap_hsmmc_host *host = mmc_priv(mmc);
1528
1529 if (mmc_slot(host).init_card)
1530 mmc_slot(host).init_card(card);
1531}
1532
Denis Karpov70a33412009-09-22 16:44:59 -07001533static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001534{
1535 u32 hctl, capa, value;
1536
1537 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301538 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001539 hctl = SDVS30;
1540 capa = VS30 | VS18;
1541 } else {
1542 hctl = SDVS18;
1543 capa = VS18;
1544 }
1545
1546 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1547 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1548
1549 value = OMAP_HSMMC_READ(host->base, CAPA);
1550 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1551
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001552 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001553 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001554}
1555
Denis Karpov70a33412009-09-22 16:44:59 -07001556static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001557{
Denis Karpov70a33412009-09-22 16:44:59 -07001558 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001559
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301560 pm_runtime_get_sync(host->dev);
1561
Denis Karpovdd498ef2009-09-22 16:44:49 -07001562 return 0;
1563}
1564
Adrian Hunter907d2e72012-02-29 09:17:21 +02001565static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001566{
Denis Karpov70a33412009-09-22 16:44:59 -07001567 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001568
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301569 pm_runtime_mark_last_busy(host->dev);
1570 pm_runtime_put_autosuspend(host->dev);
1571
Denis Karpovdd498ef2009-09-22 16:44:49 -07001572 return 0;
1573}
1574
Denis Karpov70a33412009-09-22 16:44:59 -07001575static const struct mmc_host_ops omap_hsmmc_ops = {
1576 .enable = omap_hsmmc_enable_fclk,
1577 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001578 .post_req = omap_hsmmc_post_req,
1579 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001580 .request = omap_hsmmc_request,
1581 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001582 .get_cd = omap_hsmmc_get_cd,
1583 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001584 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001585 /* NYET -- enable_sdio_irq */
1586};
1587
Denis Karpovd900f712009-09-22 16:44:38 -07001588#ifdef CONFIG_DEBUG_FS
1589
Denis Karpov70a33412009-09-22 16:44:59 -07001590static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001591{
1592 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001593 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001594 int context_loss = 0;
1595
Denis Karpov70a33412009-09-22 16:44:59 -07001596 if (host->pdata->get_context_loss_count)
1597 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001598
Adrian Hunter907d2e72012-02-29 09:17:21 +02001599 seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1600 mmc->index, host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001601
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301602 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001603 seq_printf(s, "host suspended, can't read registers\n");
1604 return 0;
1605 }
1606
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301607 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001608
Denis Karpovd900f712009-09-22 16:44:38 -07001609 seq_printf(s, "CON:\t\t0x%08x\n",
1610 OMAP_HSMMC_READ(host->base, CON));
1611 seq_printf(s, "HCTL:\t\t0x%08x\n",
1612 OMAP_HSMMC_READ(host->base, HCTL));
1613 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1614 OMAP_HSMMC_READ(host->base, SYSCTL));
1615 seq_printf(s, "IE:\t\t0x%08x\n",
1616 OMAP_HSMMC_READ(host->base, IE));
1617 seq_printf(s, "ISE:\t\t0x%08x\n",
1618 OMAP_HSMMC_READ(host->base, ISE));
1619 seq_printf(s, "CAPA:\t\t0x%08x\n",
1620 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001621
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301622 pm_runtime_mark_last_busy(host->dev);
1623 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001624
Denis Karpovd900f712009-09-22 16:44:38 -07001625 return 0;
1626}
1627
Denis Karpov70a33412009-09-22 16:44:59 -07001628static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001629{
Denis Karpov70a33412009-09-22 16:44:59 -07001630 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001631}
1632
1633static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001634 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001635 .read = seq_read,
1636 .llseek = seq_lseek,
1637 .release = single_release,
1638};
1639
Denis Karpov70a33412009-09-22 16:44:59 -07001640static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001641{
1642 if (mmc->debugfs_root)
1643 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1644 mmc, &mmc_regs_fops);
1645}
1646
1647#else
1648
Denis Karpov70a33412009-09-22 16:44:59 -07001649static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001650{
1651}
1652
1653#endif
1654
Rajendra Nayak46856a62012-03-12 20:32:37 +05301655#ifdef CONFIG_OF
1656static u16 omap4_reg_offset = 0x100;
1657
1658static const struct of_device_id omap_mmc_of_match[] = {
1659 {
1660 .compatible = "ti,omap2-hsmmc",
1661 },
1662 {
1663 .compatible = "ti,omap3-hsmmc",
1664 },
1665 {
1666 .compatible = "ti,omap4-hsmmc",
1667 .data = &omap4_reg_offset,
1668 },
1669 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001670};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301671MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1672
1673static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1674{
1675 struct omap_mmc_platform_data *pdata;
1676 struct device_node *np = dev->of_node;
1677 u32 bus_width;
1678
1679 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1680 if (!pdata)
1681 return NULL; /* out of memory */
1682
1683 if (of_find_property(np, "ti,dual-volt", NULL))
1684 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1685
1686 /* This driver only supports 1 slot */
1687 pdata->nr_slots = 1;
1688 pdata->slots[0].switch_pin = of_get_named_gpio(np, "cd-gpios", 0);
1689 pdata->slots[0].gpio_wp = of_get_named_gpio(np, "wp-gpios", 0);
1690
1691 if (of_find_property(np, "ti,non-removable", NULL)) {
1692 pdata->slots[0].nonremovable = true;
1693 pdata->slots[0].no_regulator_off_init = true;
1694 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001695 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301696 if (bus_width == 4)
1697 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1698 else if (bus_width == 8)
1699 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1700
1701 if (of_find_property(np, "ti,needs-special-reset", NULL))
1702 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1703
1704 return pdata;
1705}
1706#else
1707static inline struct omap_mmc_platform_data
1708 *of_get_hsmmc_pdata(struct device *dev)
1709{
1710 return NULL;
1711}
1712#endif
1713
Felipe Balbiefa25fd2012-03-14 11:18:28 +02001714static int __devinit omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001715{
1716 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1717 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001718 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001719 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001720 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301721 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001722 dma_cap_mask_t mask;
1723 unsigned tx_req, rx_req;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301724
1725 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1726 if (match) {
1727 pdata = of_get_hsmmc_pdata(&pdev->dev);
1728 if (match->data) {
1729 u16 *offsetp = match->data;
1730 pdata->reg_offset = *offsetp;
1731 }
1732 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001733
1734 if (pdata == NULL) {
1735 dev_err(&pdev->dev, "Platform Data is missing\n");
1736 return -ENXIO;
1737 }
1738
1739 if (pdata->nr_slots == 0) {
1740 dev_err(&pdev->dev, "No Slots\n");
1741 return -ENXIO;
1742 }
1743
1744 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1745 irq = platform_get_irq(pdev, 0);
1746 if (res == NULL || irq < 0)
1747 return -ENXIO;
1748
Chris Ball984b2032011-03-22 16:34:42 -07001749 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001750 if (res == NULL)
1751 return -EBUSY;
1752
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001753 ret = omap_hsmmc_gpio_init(pdata);
1754 if (ret)
1755 goto err;
1756
Denis Karpov70a33412009-09-22 16:44:59 -07001757 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001758 if (!mmc) {
1759 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001760 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001761 }
1762
1763 host = mmc_priv(mmc);
1764 host->mmc = mmc;
1765 host->pdata = pdata;
1766 host->dev = &pdev->dev;
1767 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001768 host->dma_ch = -1;
1769 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001770 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05301771 host->mapbase = res->start + pdata->reg_offset;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001772 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001773 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001774 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001775
1776 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001777
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301778 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001779
Adrian Huntere0eb2422010-02-15 10:03:34 -08001780 /*
1781 * If regulator_disable can only put vcc_aux to sleep then there is
1782 * no off state.
1783 */
1784 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1785 mmc_slot(host).no_off = 1;
1786
Daniel Mackd418ed82012-02-19 13:20:33 +01001787 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1788
1789 if (pdata->max_freq > 0)
1790 mmc->f_max = pdata->max_freq;
1791 else
1792 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001793
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001794 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001795
Russell King6f7607c2009-01-28 10:22:50 +00001796 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001797 if (IS_ERR(host->fclk)) {
1798 ret = PTR_ERR(host->fclk);
1799 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001800 goto err1;
1801 }
1802
Paul Walmsley9b682562011-10-06 14:50:35 -06001803 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1804 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1805 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1806 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001807
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301808 pm_runtime_enable(host->dev);
1809 pm_runtime_get_sync(host->dev);
1810 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1811 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001812
Balaji T K92a3aeb2012-02-24 21:14:34 +05301813 omap_hsmmc_context_save(host);
1814
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301815 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1816 /*
1817 * MMC can still work without debounce clock.
1818 */
1819 if (IS_ERR(host->dbclk)) {
1820 dev_warn(mmc_dev(host->mmc), "Failed to get debounce clk\n");
1821 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05301822 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301823 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
1824 clk_put(host->dbclk);
1825 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07001826 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001827
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001828 /* Since we do only SG emulation, we can have as many segs
1829 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001830 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001831
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001832 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1833 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1834 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1835 mmc->max_seg_size = mmc->max_req_size;
1836
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001837 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001838 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001839
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001840 mmc->caps |= mmc_slot(host).caps;
1841 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001842 mmc->caps |= MMC_CAP_4_BIT_DATA;
1843
Denis Karpov191d1f12009-09-22 16:44:55 -07001844 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001845 mmc->caps |= MMC_CAP_NONREMOVABLE;
1846
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001847 mmc->pm_caps = mmc_slot(host).pm_caps;
1848
Denis Karpov70a33412009-09-22 16:44:59 -07001849 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001850
Balaji T Kb7bf7732012-03-07 09:55:30 -05001851 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1852 if (!res) {
1853 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
Kevin Hilman9c17d082012-07-10 16:40:56 -07001854 ret = -ENXIO;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001855 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001856 }
Russell King26b88522012-04-13 12:27:37 +01001857 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001858
1859 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1860 if (!res) {
1861 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
Kevin Hilman9c17d082012-07-10 16:40:56 -07001862 ret = -ENXIO;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001863 goto err_irq;
1864 }
Russell King26b88522012-04-13 12:27:37 +01001865 rx_req = res->start;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001866
Russell King26b88522012-04-13 12:27:37 +01001867 dma_cap_zero(mask);
1868 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01001869
Russell King26b88522012-04-13 12:27:37 +01001870 host->rx_chan = dma_request_channel(mask, omap_dma_filter_fn, &rx_req);
1871 if (!host->rx_chan) {
1872 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001873 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001874 goto err_irq;
1875 }
1876
1877 host->tx_chan = dma_request_channel(mask, omap_dma_filter_fn, &tx_req);
1878 if (!host->tx_chan) {
1879 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001880 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001881 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01001882 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001883
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001884 /* Request IRQ for MMC operations */
Yong Zhangd9618e92011-09-22 16:59:04 +08001885 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001886 mmc_hostname(mmc), host);
1887 if (ret) {
1888 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1889 goto err_irq;
1890 }
1891
1892 if (pdata->init != NULL) {
1893 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07001894 dev_dbg(mmc_dev(host->mmc),
1895 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001896 goto err_irq_cd_init;
1897 }
1898 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001899
Adrian Hunterb702b102010-02-15 10:03:35 -08001900 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001901 ret = omap_hsmmc_reg_get(host);
1902 if (ret)
1903 goto err_reg;
1904 host->use_reg = 1;
1905 }
1906
David Brownellb583f262009-05-28 14:04:03 -07001907 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001908
1909 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001910 if ((mmc_slot(host).card_detect_irq)) {
NeilBrown7efab4f2011-12-30 12:35:13 +11001911 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1912 NULL,
1913 omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08001914 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11001915 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001916 if (ret) {
1917 dev_dbg(mmc_dev(host->mmc),
1918 "Unable to grab MMC CD IRQ\n");
1919 goto err_irq_cd;
1920 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00001921 pdata->suspend = omap_hsmmc_suspend_cdirq;
1922 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001923 }
1924
Adrian Hunterb4175772010-05-26 14:42:06 -07001925 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001926
Adrian Hunterb62f6222009-09-22 16:45:01 -07001927 omap_hsmmc_protect_card(host);
1928
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001929 mmc_add_host(mmc);
1930
Denis Karpov191d1f12009-09-22 16:44:55 -07001931 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001932 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1933 if (ret < 0)
1934 goto err_slot_name;
1935 }
Denis Karpov191d1f12009-09-22 16:44:55 -07001936 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001937 ret = device_create_file(&mmc->class_dev,
1938 &dev_attr_cover_switch);
1939 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001940 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001941 }
1942
Denis Karpov70a33412009-09-22 16:44:59 -07001943 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301944 pm_runtime_mark_last_busy(host->dev);
1945 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001946
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001947 return 0;
1948
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001949err_slot_name:
1950 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001951 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001952err_irq_cd:
1953 if (host->use_reg)
1954 omap_hsmmc_reg_put(host);
1955err_reg:
1956 if (host->pdata->cleanup)
1957 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001958err_irq_cd_init:
1959 free_irq(host->irq, host);
1960err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01001961 if (host->tx_chan)
1962 dma_release_channel(host->tx_chan);
1963 if (host->rx_chan)
1964 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05301965 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05001966 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001967 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301968 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05301969 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001970 clk_put(host->dbclk);
1971 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001972err1:
1973 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001974 platform_set_drvdata(pdev, NULL);
1975 mmc_free_host(mmc);
1976err_alloc:
1977 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001978err:
Russell King48b332f2012-04-18 11:11:57 +01001979 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1980 if (res)
1981 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001982 return ret;
1983}
1984
Felipe Balbiefa25fd2012-03-14 11:18:28 +02001985static int __devexit omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001986{
Denis Karpov70a33412009-09-22 16:44:59 -07001987 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001988 struct resource *res;
1989
Felipe Balbi927ce942012-03-14 11:18:27 +02001990 pm_runtime_get_sync(host->dev);
1991 mmc_remove_host(host->mmc);
1992 if (host->use_reg)
1993 omap_hsmmc_reg_put(host);
1994 if (host->pdata->cleanup)
1995 host->pdata->cleanup(&pdev->dev);
1996 free_irq(host->irq, host);
1997 if (mmc_slot(host).card_detect_irq)
1998 free_irq(mmc_slot(host).card_detect_irq, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001999
Russell Kingc5c98922012-04-13 12:14:39 +01002000 if (host->tx_chan)
2001 dma_release_channel(host->tx_chan);
2002 if (host->rx_chan)
2003 dma_release_channel(host->rx_chan);
2004
Felipe Balbi927ce942012-03-14 11:18:27 +02002005 pm_runtime_put_sync(host->dev);
2006 pm_runtime_disable(host->dev);
2007 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302008 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302009 clk_disable_unprepare(host->dbclk);
Felipe Balbi927ce942012-03-14 11:18:27 +02002010 clk_put(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002011 }
2012
Felipe Balbi927ce942012-03-14 11:18:27 +02002013 mmc_free_host(host->mmc);
2014 iounmap(host->base);
2015 omap_hsmmc_gpio_free(pdev->dev.platform_data);
2016
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002017 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2018 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002019 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002020 platform_set_drvdata(pdev, NULL);
2021
2022 return 0;
2023}
2024
2025#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002026static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002027{
2028 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002029 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2030
2031 if (!host)
2032 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002033
2034 if (host && host->suspended)
2035 return 0;
2036
Felipe Balbi927ce942012-03-14 11:18:27 +02002037 pm_runtime_get_sync(host->dev);
2038 host->suspended = 1;
2039 if (host->pdata->suspend) {
2040 ret = host->pdata->suspend(dev, host->slot_id);
Eliad Peller31f9d462011-11-22 16:02:17 +02002041 if (ret) {
Felipe Balbi927ce942012-03-14 11:18:27 +02002042 dev_dbg(dev, "Unable to handle MMC board"
2043 " level suspend\n");
Adrian Huntera6b22402009-09-22 16:44:45 -07002044 host->suspended = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002045 return ret;
Adrian Huntera6b22402009-09-22 16:44:45 -07002046 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002047 }
Felipe Balbi927ce942012-03-14 11:18:27 +02002048 ret = mmc_suspend_host(host->mmc);
2049
2050 if (ret) {
2051 host->suspended = 0;
2052 if (host->pdata->resume) {
2053 ret = host->pdata->resume(dev, host->slot_id);
2054 if (ret)
2055 dev_dbg(dev, "Unmask interrupt failed\n");
2056 }
2057 goto err;
2058 }
2059
2060 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2061 omap_hsmmc_disable_irq(host);
2062 OMAP_HSMMC_WRITE(host->base, HCTL,
2063 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2064 }
2065
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302066 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302067 clk_disable_unprepare(host->dbclk);
Eliad Peller31f9d462011-11-22 16:02:17 +02002068err:
2069 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002070 return ret;
2071}
2072
2073/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002074static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002075{
2076 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002077 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2078
2079 if (!host)
2080 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002081
2082 if (host && !host->suspended)
2083 return 0;
2084
Felipe Balbi927ce942012-03-14 11:18:27 +02002085 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002086
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302087 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302088 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002089
Felipe Balbi927ce942012-03-14 11:18:27 +02002090 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2091 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002092
Felipe Balbi927ce942012-03-14 11:18:27 +02002093 if (host->pdata->resume) {
2094 ret = host->pdata->resume(dev, host->slot_id);
2095 if (ret)
2096 dev_dbg(dev, "Unmask interrupt failed\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002097 }
2098
Felipe Balbi927ce942012-03-14 11:18:27 +02002099 omap_hsmmc_protect_card(host);
2100
2101 /* Notify the core to resume the host */
2102 ret = mmc_resume_host(host->mmc);
2103 if (ret == 0)
2104 host->suspended = 0;
2105
2106 pm_runtime_mark_last_busy(host->dev);
2107 pm_runtime_put_autosuspend(host->dev);
2108
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002109 return ret;
2110
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002111}
2112
2113#else
Denis Karpov70a33412009-09-22 16:44:59 -07002114#define omap_hsmmc_suspend NULL
2115#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002116#endif
2117
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302118static int omap_hsmmc_runtime_suspend(struct device *dev)
2119{
2120 struct omap_hsmmc_host *host;
2121
2122 host = platform_get_drvdata(to_platform_device(dev));
2123 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002124 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302125
2126 return 0;
2127}
2128
2129static int omap_hsmmc_runtime_resume(struct device *dev)
2130{
2131 struct omap_hsmmc_host *host;
2132
2133 host = platform_get_drvdata(to_platform_device(dev));
2134 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002135 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302136
2137 return 0;
2138}
2139
Kevin Hilmana791daa2010-05-26 14:42:07 -07002140static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002141 .suspend = omap_hsmmc_suspend,
2142 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302143 .runtime_suspend = omap_hsmmc_runtime_suspend,
2144 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002145};
2146
2147static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002148 .probe = omap_hsmmc_probe,
2149 .remove = __devexit_p(omap_hsmmc_remove),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002150 .driver = {
2151 .name = DRIVER_NAME,
2152 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002153 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302154 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002155 },
2156};
2157
Felipe Balbib7964502012-03-14 11:18:32 +02002158module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002159MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2160MODULE_LICENSE("GPL");
2161MODULE_ALIAS("platform:" DRIVER_NAME);
2162MODULE_AUTHOR("Texas Instruments Inc");