blob: 813ae0ac62868b516f643d89c32b4980f8c27231 [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Andy Shevchenkoac330f42011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
Russell Kingc5c98922012-04-13 12:14:39 +010022#include <linux/dmaengine.h>
Denis Karpovd900f712009-09-22 16:44:38 -070023#include <linux/seq_file.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010024#include <linux/interrupt.h>
25#include <linux/delay.h>
26#include <linux/dma-mapping.h>
27#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010028#include <linux/timer.h>
29#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053030#include <linux/of.h>
31#include <linux/of_gpio.h>
32#include <linux/of_device.h>
Russell King3451c062012-04-21 22:35:42 +010033#include <linux/omap-dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010034#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070035#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070036#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010037#include <linux/io.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080038#include <linux/gpio.h>
39#include <linux/regulator/consumer.h>
Daniel Mack46b76032012-10-15 21:35:05 +053040#include <linux/pinctrl/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053041#include <linux/pm_runtime.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010042#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070043#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{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530182 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
183 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800184
185 /* NOTE: assumes card detect signal is active-low */
186 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
187}
188
189static int omap_hsmmc_get_wp(struct device *dev, int slot)
190{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530191 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
192 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800193
194 /* NOTE: assumes write protect signal is active-high */
195 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
196}
197
198static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
199{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530200 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
201 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800202
203 /* NOTE: assumes card detect signal is active-low */
204 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
205}
206
207#ifdef CONFIG_PM
208
209static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
210{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530211 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
212 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800213
214 disable_irq(mmc->slots[0].card_detect_irq);
215 return 0;
216}
217
218static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
219{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530220 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
221 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800222
223 enable_irq(mmc->slots[0].card_detect_irq);
224 return 0;
225}
226
227#else
228
229#define omap_hsmmc_suspend_cdirq NULL
230#define omap_hsmmc_resume_cdirq NULL
231
232#endif
233
Adrian Hunterb702b102010-02-15 10:03:35 -0800234#ifdef CONFIG_REGULATOR
235
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500236static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800237 int vdd)
238{
239 struct omap_hsmmc_host *host =
240 platform_get_drvdata(to_platform_device(dev));
241 int ret = 0;
242
243 /*
244 * If we don't see a Vcc regulator, assume it's a fixed
245 * voltage always-on regulator.
246 */
247 if (!host->vcc)
248 return 0;
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530249 /*
250 * With DT, never turn OFF the regulator. This is because
251 * the pbias cell programming support is still missing when
252 * booting with Device tree
253 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +0530254 if (dev->of_node && !vdd)
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530255 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800256
257 if (mmc_slot(host).before_set_reg)
258 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
259
260 /*
261 * Assume Vcc regulator is used only to power the card ... OMAP
262 * VDDS is used to power the pins, optionally with a transceiver to
263 * support cards using voltages other than VDDS (1.8V nominal). When a
264 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
265 *
266 * In some cases this regulator won't support enable/disable;
267 * e.g. it's a fixed rail for a WLAN chip.
268 *
269 * In other cases vcc_aux switches interface power. Example, for
270 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
271 * chips/cards need an interface voltage rail too.
272 */
273 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400274 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800275 /* Enable interface voltage rail, if needed */
276 if (ret == 0 && host->vcc_aux) {
277 ret = regulator_enable(host->vcc_aux);
278 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400279 ret = mmc_regulator_set_ocr(host->mmc,
280 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800281 }
282 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400283 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800284 if (host->vcc_aux)
285 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400286 if (!ret) {
287 /* Then proceed to shut down the local regulator */
288 ret = mmc_regulator_set_ocr(host->mmc,
289 host->vcc, 0);
290 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800291 }
292
293 if (mmc_slot(host).after_set_reg)
294 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
295
296 return ret;
297}
298
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800299static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
300{
301 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700302 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800303
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800304 reg = regulator_get(host->dev, "vmmc");
305 if (IS_ERR(reg)) {
306 dev_dbg(host->dev, "vmmc regulator missing\n");
NeilBrown1fdc90f2012-08-08 00:06:00 -0400307 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800308 } else {
NeilBrown1fdc90f2012-08-08 00:06:00 -0400309 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800310 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700311 ocr_value = mmc_regulator_get_ocrmask(reg);
312 if (!mmc_slot(host).ocr_mask) {
313 mmc_slot(host).ocr_mask = ocr_value;
314 } else {
315 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530316 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500317 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700318 mmc_slot(host).ocr_mask = 0;
319 return -EINVAL;
320 }
321 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800322
323 /* Allow an aux regulator */
324 reg = regulator_get(host->dev, "vmmc_aux");
325 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
326
Balaji T Kb1c1df72011-05-30 19:55:34 +0530327 /* For eMMC do not power off when not in sleep state */
328 if (mmc_slot(host).no_regulator_off_init)
329 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800330 /*
331 * UGLY HACK: workaround regulator framework bugs.
332 * When the bootloader leaves a supply active, it's
333 * initialized with zero usecount ... and we can't
334 * disable it without first enabling it. Until the
335 * framework is fixed, we need a workaround like this
336 * (which is safe for MMC, but not in general).
337 */
Adrian Huntere840ce12011-05-06 12:14:10 +0300338 if (regulator_is_enabled(host->vcc) > 0 ||
339 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
340 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
341
342 mmc_slot(host).set_power(host->dev, host->slot_id,
343 1, vdd);
344 mmc_slot(host).set_power(host->dev, host->slot_id,
345 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800346 }
347 }
348
349 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800350}
351
352static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
353{
354 regulator_put(host->vcc);
355 regulator_put(host->vcc_aux);
356 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800357}
358
Adrian Hunterb702b102010-02-15 10:03:35 -0800359static inline int omap_hsmmc_have_reg(void)
360{
361 return 1;
362}
363
364#else
365
366static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
367{
368 return -EINVAL;
369}
370
371static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
372{
373}
374
375static inline int omap_hsmmc_have_reg(void)
376{
377 return 0;
378}
379
380#endif
381
382static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
383{
384 int ret;
385
386 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800387 if (pdata->slots[0].cover)
388 pdata->slots[0].get_cover_state =
389 omap_hsmmc_get_cover_state;
390 else
391 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
392 pdata->slots[0].card_detect_irq =
393 gpio_to_irq(pdata->slots[0].switch_pin);
394 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
395 if (ret)
396 return ret;
397 ret = gpio_direction_input(pdata->slots[0].switch_pin);
398 if (ret)
399 goto err_free_sp;
400 } else
401 pdata->slots[0].switch_pin = -EINVAL;
402
403 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
404 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
405 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
406 if (ret)
407 goto err_free_cd;
408 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
409 if (ret)
410 goto err_free_wp;
411 } else
412 pdata->slots[0].gpio_wp = -EINVAL;
413
414 return 0;
415
416err_free_wp:
417 gpio_free(pdata->slots[0].gpio_wp);
418err_free_cd:
419 if (gpio_is_valid(pdata->slots[0].switch_pin))
420err_free_sp:
421 gpio_free(pdata->slots[0].switch_pin);
422 return ret;
423}
424
425static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
426{
427 if (gpio_is_valid(pdata->slots[0].gpio_wp))
428 gpio_free(pdata->slots[0].gpio_wp);
429 if (gpio_is_valid(pdata->slots[0].switch_pin))
430 gpio_free(pdata->slots[0].switch_pin);
431}
432
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100433/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300434 * Start clock to the card
435 */
436static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
437{
438 OMAP_HSMMC_WRITE(host->base, SYSCTL,
439 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
440}
441
442/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100443 * Stop clock to the card
444 */
Denis Karpov70a33412009-09-22 16:44:59 -0700445static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100446{
447 OMAP_HSMMC_WRITE(host->base, SYSCTL,
448 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
449 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900450 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100451}
452
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700453static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
454 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700455{
456 unsigned int irq_mask;
457
458 if (host->use_dma)
459 irq_mask = INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE);
460 else
461 irq_mask = INT_EN_MASK;
462
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700463 /* Disable timeout for erases */
464 if (cmd->opcode == MMC_ERASE)
465 irq_mask &= ~DTO_ENABLE;
466
Adrian Hunterb4175772010-05-26 14:42:06 -0700467 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
468 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
469 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
470}
471
472static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
473{
474 OMAP_HSMMC_WRITE(host->base, ISE, 0);
475 OMAP_HSMMC_WRITE(host->base, IE, 0);
476 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
477}
478
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300479/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530480static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300481{
482 u16 dsor = 0;
483
484 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530485 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300486 if (dsor > 250)
487 dsor = 250;
488 }
489
490 return dsor;
491}
492
Andy Shevchenko5934df22011-05-06 12:14:06 +0300493static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
494{
495 struct mmc_ios *ios = &host->mmc->ios;
496 unsigned long regval;
497 unsigned long timeout;
498
Venkatraman S8986d312012-08-07 19:10:38 +0530499 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300500
501 omap_hsmmc_stop_clock(host);
502
503 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
504 regval = regval & ~(CLKD_MASK | DTO_MASK);
Balaji TKd83b6e02011-12-20 15:12:00 +0530505 regval = regval | (calc_divisor(host, ios) << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300506 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
507 OMAP_HSMMC_WRITE(host->base, SYSCTL,
508 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
509
510 /* Wait till the ICS bit is set */
511 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
512 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
513 && time_before(jiffies, timeout))
514 cpu_relax();
515
516 omap_hsmmc_start_clock(host);
517}
518
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400519static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
520{
521 struct mmc_ios *ios = &host->mmc->ios;
522 u32 con;
523
524 con = OMAP_HSMMC_READ(host->base, CON);
Balaji T K03b5d922012-04-09 12:08:33 +0530525 if (ios->timing == MMC_TIMING_UHS_DDR50)
526 con |= DDR; /* configure in DDR mode */
527 else
528 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400529 switch (ios->bus_width) {
530 case MMC_BUS_WIDTH_8:
531 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
532 break;
533 case MMC_BUS_WIDTH_4:
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 case MMC_BUS_WIDTH_1:
539 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
540 OMAP_HSMMC_WRITE(host->base, HCTL,
541 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
542 break;
543 }
544}
545
546static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
547{
548 struct mmc_ios *ios = &host->mmc->ios;
549 u32 con;
550
551 con = OMAP_HSMMC_READ(host->base, CON);
552 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
553 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
554 else
555 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
556}
557
Denis Karpov11dd62a2009-09-22 16:44:43 -0700558#ifdef CONFIG_PM
559
560/*
561 * Restore the MMC host context, if it was lost as result of a
562 * power state change.
563 */
Denis Karpov70a33412009-09-22 16:44:59 -0700564static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700565{
566 struct mmc_ios *ios = &host->mmc->ios;
567 struct omap_mmc_platform_data *pdata = host->pdata;
568 int context_loss = 0;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400569 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700570 unsigned long timeout;
571
572 if (pdata->get_context_loss_count) {
573 context_loss = pdata->get_context_loss_count(host->dev);
574 if (context_loss < 0)
575 return 1;
576 }
577
578 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
579 context_loss == host->context_loss ? "not " : "");
580 if (host->context_loss == context_loss)
581 return 1;
582
Venkatraman S6c31b212012-08-08 14:26:52 +0530583 if (!OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE)
584 return 1;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700585
Balaji T Kc2200ef2012-03-07 09:55:30 -0500586 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700587 if (host->power_mode != MMC_POWER_OFF &&
588 (1 << ios->vdd) <= MMC_VDD_23_24)
589 hctl = SDVS18;
590 else
591 hctl = SDVS30;
592 capa = VS30 | VS18;
593 } else {
594 hctl = SDVS18;
595 capa = VS18;
596 }
597
598 OMAP_HSMMC_WRITE(host->base, HCTL,
599 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
600
601 OMAP_HSMMC_WRITE(host->base, CAPA,
602 OMAP_HSMMC_READ(host->base, CAPA) | capa);
603
604 OMAP_HSMMC_WRITE(host->base, HCTL,
605 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
606
607 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
608 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
609 && time_before(jiffies, timeout))
610 ;
611
Adrian Hunterb4175772010-05-26 14:42:06 -0700612 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700613
614 /* Do not initialize card-specific things if the power is off */
615 if (host->power_mode == MMC_POWER_OFF)
616 goto out;
617
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400618 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700619
Andy Shevchenko5934df22011-05-06 12:14:06 +0300620 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700621
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400622 omap_hsmmc_set_bus_mode(host);
623
Denis Karpov11dd62a2009-09-22 16:44:43 -0700624out:
625 host->context_loss = context_loss;
626
627 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
628 return 0;
629}
630
631/*
632 * Save the MMC host context (store the number of power state changes so far).
633 */
Denis Karpov70a33412009-09-22 16:44:59 -0700634static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700635{
636 struct omap_mmc_platform_data *pdata = host->pdata;
637 int context_loss;
638
639 if (pdata->get_context_loss_count) {
640 context_loss = pdata->get_context_loss_count(host->dev);
641 if (context_loss < 0)
642 return;
643 host->context_loss = context_loss;
644 }
645}
646
647#else
648
Denis Karpov70a33412009-09-22 16:44:59 -0700649static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700650{
651 return 0;
652}
653
Denis Karpov70a33412009-09-22 16:44:59 -0700654static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700655{
656}
657
658#endif
659
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100660/*
661 * Send init stream sequence to card
662 * before sending IDLE command
663 */
Denis Karpov70a33412009-09-22 16:44:59 -0700664static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100665{
666 int reg = 0;
667 unsigned long timeout;
668
Adrian Hunterb62f6222009-09-22 16:45:01 -0700669 if (host->protect_card)
670 return;
671
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100672 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700673
674 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100675 OMAP_HSMMC_WRITE(host->base, CON,
676 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
677 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
678
679 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
680 while ((reg != CC) && time_before(jiffies, timeout))
681 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
682
683 OMAP_HSMMC_WRITE(host->base, CON,
684 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700685
686 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
687 OMAP_HSMMC_READ(host->base, STAT);
688
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100689 enable_irq(host->irq);
690}
691
692static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700693int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100694{
695 int r = 1;
696
Denis Karpov191d1f12009-09-22 16:44:55 -0700697 if (mmc_slot(host).get_cover_state)
698 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100699 return r;
700}
701
702static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700703omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100704 char *buf)
705{
706 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700707 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100708
Denis Karpov70a33412009-09-22 16:44:59 -0700709 return sprintf(buf, "%s\n",
710 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100711}
712
Denis Karpov70a33412009-09-22 16:44:59 -0700713static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100714
715static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700716omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100717 char *buf)
718{
719 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700720 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100721
Denis Karpov191d1f12009-09-22 16:44:55 -0700722 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100723}
724
Denis Karpov70a33412009-09-22 16:44:59 -0700725static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100726
727/*
728 * Configure the response type and send the cmd.
729 */
730static void
Denis Karpov70a33412009-09-22 16:44:59 -0700731omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100732 struct mmc_data *data)
733{
734 int cmdreg = 0, resptype = 0, cmdtype = 0;
735
Venkatraman S8986d312012-08-07 19:10:38 +0530736 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100737 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
738 host->cmd = cmd;
739
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700740 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100741
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200742 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100743 if (cmd->flags & MMC_RSP_PRESENT) {
744 if (cmd->flags & MMC_RSP_136)
745 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200746 else if (cmd->flags & MMC_RSP_BUSY) {
747 resptype = 3;
748 host->response_busy = 1;
749 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100750 resptype = 2;
751 }
752
753 /*
754 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
755 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
756 * a val of 0x3, rest 0x0.
757 */
758 if (cmd == host->mrq->stop)
759 cmdtype = 0x3;
760
761 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
762
763 if (data) {
764 cmdreg |= DP_SELECT | MSBS | BCE;
765 if (data->flags & MMC_DATA_READ)
766 cmdreg |= DDIR;
767 else
768 cmdreg &= ~(DDIR);
769 }
770
771 if (host->use_dma)
772 cmdreg |= DMA_EN;
773
Adrian Hunterb4175772010-05-26 14:42:06 -0700774 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700775
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100776 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
777 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
778}
779
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200780static int
Denis Karpov70a33412009-09-22 16:44:59 -0700781omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200782{
783 if (data->flags & MMC_DATA_WRITE)
784 return DMA_TO_DEVICE;
785 else
786 return DMA_FROM_DEVICE;
787}
788
Russell Kingc5c98922012-04-13 12:14:39 +0100789static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
790 struct mmc_data *data)
791{
792 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
793}
794
Adrian Hunterb4175772010-05-26 14:42:06 -0700795static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
796{
797 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530798 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700799
Venkatraman S31463b12012-04-09 12:08:34 +0530800 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700801 host->req_in_progress = 0;
802 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530803 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700804
805 omap_hsmmc_disable_irq(host);
806 /* Do not complete the request if DMA is still in progress */
807 if (mrq->data && host->use_dma && dma_ch != -1)
808 return;
809 host->mrq = NULL;
810 mmc_request_done(host->mmc, mrq);
811}
812
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100813/*
814 * Notify the transfer complete to MMC core
815 */
816static void
Denis Karpov70a33412009-09-22 16:44:59 -0700817omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100818{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200819 if (!data) {
820 struct mmc_request *mrq = host->mrq;
821
Adrian Hunter23050102009-09-22 16:44:57 -0700822 /* TC before CC from CMD6 - don't know why, but it happens */
823 if (host->cmd && host->cmd->opcode == 6 &&
824 host->response_busy) {
825 host->response_busy = 0;
826 return;
827 }
828
Adrian Hunterb4175772010-05-26 14:42:06 -0700829 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200830 return;
831 }
832
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100833 host->data = NULL;
834
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100835 if (!data->error)
836 data->bytes_xfered += data->blocks * (data->blksz);
837 else
838 data->bytes_xfered = 0;
839
Ming Leife852272012-06-22 18:49:35 +0800840 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700841 omap_hsmmc_request_done(host, data->mrq);
Ming Leife852272012-06-22 18:49:35 +0800842 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100843 }
Ming Leife852272012-06-22 18:49:35 +0800844 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100845}
846
847/*
848 * Notify the core about command completion
849 */
850static void
Denis Karpov70a33412009-09-22 16:44:59 -0700851omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100852{
853 host->cmd = NULL;
854
855 if (cmd->flags & MMC_RSP_PRESENT) {
856 if (cmd->flags & MMC_RSP_136) {
857 /* response type 2 */
858 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
859 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
860 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
861 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
862 } else {
863 /* response types 1, 1b, 3, 4, 5, 6 */
864 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
865 }
866 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700867 if ((host->data == NULL && !host->response_busy) || cmd->error)
868 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100869}
870
871/*
872 * DMA clean up for command errors
873 */
Denis Karpov70a33412009-09-22 16:44:59 -0700874static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100875{
Adrian Hunterb4175772010-05-26 14:42:06 -0700876 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530877 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700878
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200879 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100880
Venkatraman S31463b12012-04-09 12:08:34 +0530881 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700882 dma_ch = host->dma_ch;
883 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530884 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700885
886 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100887 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
888
889 dmaengine_terminate_all(chan);
890 dma_unmap_sg(chan->device->dev,
891 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700892 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100893
Per Forlin053bf342011-11-07 21:55:11 +0530894 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100895 }
896 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100897}
898
899/*
900 * Readable error output
901 */
902#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300903static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100904{
905 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700906 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300907 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
908 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
909 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
910 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100911 };
912 char res[256];
913 char *buf = res;
914 int len, i;
915
916 len = sprintf(buf, "MMC IRQ 0x%x :", status);
917 buf += len;
918
Denis Karpov70a33412009-09-22 16:44:59 -0700919 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100920 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700921 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100922 buf += len;
923 }
924
Venkatraman S8986d312012-08-07 19:10:38 +0530925 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100926}
Adrian Hunter699b9582011-05-06 12:14:01 +0300927#else
928static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
929 u32 status)
930{
931}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100932#endif /* CONFIG_MMC_DEBUG */
933
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100934/*
935 * MMC controller internal state machines reset
936 *
937 * Used to reset command or data internal state machines, using respectively
938 * SRC or SRD bit of SYSCTL register
939 * Can be called from interrupt context
940 */
Denis Karpov70a33412009-09-22 16:44:59 -0700941static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
942 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100943{
944 unsigned long i = 0;
945 unsigned long limit = (loops_per_jiffy *
946 msecs_to_jiffies(MMC_TIMEOUT_MS));
947
948 OMAP_HSMMC_WRITE(host->base, SYSCTL,
949 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
950
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700951 /*
952 * OMAP4 ES2 and greater has an updated reset logic.
953 * Monitor a 0->1 transition first
954 */
955 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -0500956 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700957 && (i++ < limit))
958 cpu_relax();
959 }
960 i = 0;
961
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100962 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
963 (i++ < limit))
964 cpu_relax();
965
966 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
967 dev_err(mmc_dev(host->mmc),
968 "Timeout waiting on controller reset in %s\n",
969 __func__);
970}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100971
Venkatraman Sae4bf782012-08-09 20:36:07 +0530972static void hsmmc_command_incomplete(struct omap_hsmmc_host *host, int err)
973{
974 omap_hsmmc_reset_controller_fsm(host, SRC);
975 host->cmd->error = err;
976
977 if (host->data) {
978 omap_hsmmc_reset_controller_fsm(host, SRD);
979 omap_hsmmc_dma_cleanup(host, err);
980 }
981
982}
983
Adrian Hunterb4175772010-05-26 14:42:06 -0700984static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100985{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100986 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -0700987 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100988
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100989 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +0530990 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100991
992 if (status & ERR) {
Adrian Hunter699b9582011-05-06 12:14:01 +0300993 omap_hsmmc_dbg_report_irq(host, status);
Venkatraman Sae4bf782012-08-09 20:36:07 +0530994 if (status & (CMD_TIMEOUT | DATA_TIMEOUT))
995 hsmmc_command_incomplete(host, -ETIMEDOUT);
996 else if (status & (CMD_CRC | DATA_CRC))
997 hsmmc_command_incomplete(host, -EILSEQ);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200998
Venkatraman Sae4bf782012-08-09 20:36:07 +0530999 end_cmd = 1;
1000 if (host->data || host->response_busy) {
1001 end_trans = 1;
1002 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001003 }
1004 }
1005
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +03001006 if (end_cmd || ((status & CC) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001007 omap_hsmmc_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -07001008 if ((end_trans || (status & TC)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001009 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001010}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001011
Adrian Hunterb4175772010-05-26 14:42:06 -07001012/*
1013 * MMC controller IRQ handler
1014 */
1015static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1016{
1017 struct omap_hsmmc_host *host = dev_id;
1018 int status;
1019
1020 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301021 while (status & INT_EN_MASK && host->req_in_progress) {
Adrian Hunterb4175772010-05-26 14:42:06 -07001022 omap_hsmmc_do_irq(host, status);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301023
Adrian Hunterb4175772010-05-26 14:42:06 -07001024 /* Flush posted write */
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301025 OMAP_HSMMC_WRITE(host->base, STAT, status);
Adrian Hunterb4175772010-05-26 14:42:06 -07001026 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301027 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001028
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001029 return IRQ_HANDLED;
1030}
1031
Denis Karpov70a33412009-09-22 16:44:59 -07001032static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001033{
1034 unsigned long i;
1035
1036 OMAP_HSMMC_WRITE(host->base, HCTL,
1037 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1038 for (i = 0; i < loops_per_jiffy; i++) {
1039 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1040 break;
1041 cpu_relax();
1042 }
1043}
1044
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001045/*
David Brownelleb250822009-02-17 14:49:01 -08001046 * Switch MMC interface voltage ... only relevant for MMC1.
1047 *
1048 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1049 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1050 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001051 */
Denis Karpov70a33412009-09-22 16:44:59 -07001052static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001053{
1054 u32 reg_val = 0;
1055 int ret;
1056
1057 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301058 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301059 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301060 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001061
1062 /* Turn the power off */
1063 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001064
1065 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001066 if (!ret)
1067 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1068 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301069 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301070 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301071 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001072
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001073 if (ret != 0)
1074 goto err;
1075
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001076 OMAP_HSMMC_WRITE(host->base, HCTL,
1077 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1078 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001079
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001080 /*
1081 * If a MMC dual voltage card is detected, the set_ios fn calls
1082 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001083 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001084 *
David Brownelleb250822009-02-17 14:49:01 -08001085 * Cope with a bit of slop in the range ... per data sheets:
1086 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1087 * but recommended values are 1.71V to 1.89V
1088 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1089 * but recommended values are 2.7V to 3.3V
1090 *
1091 * Board setup code shouldn't permit anything very out-of-range.
1092 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1093 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001094 */
David Brownelleb250822009-02-17 14:49:01 -08001095 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001096 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001097 else
1098 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001099
1100 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001101 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001102
1103 return 0;
1104err:
1105 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
1106 return ret;
1107}
1108
Adrian Hunterb62f6222009-09-22 16:45:01 -07001109/* Protect the card while the cover is open */
1110static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1111{
1112 if (!mmc_slot(host).get_cover_state)
1113 return;
1114
1115 host->reqs_blocked = 0;
1116 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1117 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301118 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001119 "card is now accessible\n",
1120 mmc_hostname(host->mmc));
1121 host->protect_card = 0;
1122 }
1123 } else {
1124 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301125 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001126 "card is now inaccessible\n",
1127 mmc_hostname(host->mmc));
1128 host->protect_card = 1;
1129 }
1130 }
1131}
1132
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001133/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001134 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001135 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001136static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001137{
NeilBrown7efab4f2011-12-30 12:35:13 +11001138 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001139 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001140 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001141
Adrian Huntera6b22402009-09-22 16:44:45 -07001142 if (host->suspended)
NeilBrown7efab4f2011-12-30 12:35:13 +11001143 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001144
1145 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001146
Denis Karpov191d1f12009-09-22 16:44:55 -07001147 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001148 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001149 else {
1150 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001151 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001152 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001153
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001154 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001155 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001156 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001157 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001158 return IRQ_HANDLED;
1159}
1160
Russell Kingc5c98922012-04-13 12:14:39 +01001161static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001162{
Russell Kingc5c98922012-04-13 12:14:39 +01001163 struct omap_hsmmc_host *host = param;
1164 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001165 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001166 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001167
Russell Kingc5c98922012-04-13 12:14:39 +01001168 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001169 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001170 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001171 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001172 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001173
Adrian Hunter770d7432011-05-06 12:14:11 +03001174 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001175 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001176 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001177 dma_unmap_sg(chan->device->dev,
1178 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001179 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001180
1181 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001182 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001183 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001184
1185 /* If DMA has finished after TC, complete the request */
1186 if (!req_in_progress) {
1187 struct mmc_request *mrq = host->mrq;
1188
1189 host->mrq = NULL;
1190 mmc_request_done(host->mmc, mrq);
1191 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001192}
1193
Per Forlin9782aff2011-07-01 18:55:23 +02001194static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1195 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001196 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001197 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001198{
1199 int dma_len;
1200
1201 if (!next && data->host_cookie &&
1202 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301203 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001204 " host->next_data.cookie %d\n",
1205 __func__, data->host_cookie, host->next_data.cookie);
1206 data->host_cookie = 0;
1207 }
1208
1209 /* Check if next job is already prepared */
1210 if (next ||
1211 (!next && data->host_cookie != host->next_data.cookie)) {
Russell King26b88522012-04-13 12:27:37 +01001212 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001213 omap_hsmmc_get_dma_dir(host, data));
1214
1215 } else {
1216 dma_len = host->next_data.dma_len;
1217 host->next_data.dma_len = 0;
1218 }
1219
1220
1221 if (dma_len == 0)
1222 return -EINVAL;
1223
1224 if (next) {
1225 next->dma_len = dma_len;
1226 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1227 } else
1228 host->dma_len = dma_len;
1229
1230 return 0;
1231}
1232
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001233/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001234 * Routine to configure and start DMA for the MMC card
1235 */
Denis Karpov70a33412009-09-22 16:44:59 -07001236static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1237 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001238{
Russell King26b88522012-04-13 12:27:37 +01001239 struct dma_slave_config cfg;
1240 struct dma_async_tx_descriptor *tx;
1241 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001242 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001243 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001244
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001245 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001246 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001247 struct scatterlist *sgl;
1248
1249 sgl = data->sg + i;
1250 if (sgl->length % data->blksz)
1251 return -EINVAL;
1252 }
1253 if ((data->blksz % 4) != 0)
1254 /* REVISIT: The MMC buffer increments only when MSB is written.
1255 * Return error for blksz which is non multiple of four.
1256 */
1257 return -EINVAL;
1258
Adrian Hunterb4175772010-05-26 14:42:06 -07001259 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001260
Russell Kingc5c98922012-04-13 12:14:39 +01001261 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001262
Russell King26b88522012-04-13 12:27:37 +01001263 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1264 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1265 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1266 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1267 cfg.src_maxburst = data->blksz / 4;
1268 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001269
Russell King26b88522012-04-13 12:27:37 +01001270 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001271 if (ret)
1272 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001273
Russell King26b88522012-04-13 12:27:37 +01001274 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1275 if (ret)
1276 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001277
Russell King26b88522012-04-13 12:27:37 +01001278 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1279 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1280 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1281 if (!tx) {
1282 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1283 /* FIXME: cleanup */
1284 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001285 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001286
Russell King26b88522012-04-13 12:27:37 +01001287 tx->callback = omap_hsmmc_dma_callback;
1288 tx->callback_param = host;
1289
1290 /* Does not fail */
1291 dmaengine_submit(tx);
1292
1293 host->dma_ch = 1;
1294
1295 dma_async_issue_pending(chan);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001296
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001297 return 0;
1298}
1299
Denis Karpov70a33412009-09-22 16:44:59 -07001300static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001301 unsigned int timeout_ns,
1302 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001303{
1304 unsigned int timeout, cycle_ns;
1305 uint32_t reg, clkd, dto = 0;
1306
1307 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1308 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1309 if (clkd == 0)
1310 clkd = 1;
1311
1312 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001313 timeout = timeout_ns / cycle_ns;
1314 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001315 if (timeout) {
1316 while ((timeout & 0x80000000) == 0) {
1317 dto += 1;
1318 timeout <<= 1;
1319 }
1320 dto = 31 - dto;
1321 timeout <<= 1;
1322 if (timeout && dto)
1323 dto += 1;
1324 if (dto >= 13)
1325 dto -= 13;
1326 else
1327 dto = 0;
1328 if (dto > 14)
1329 dto = 14;
1330 }
1331
1332 reg &= ~DTO_MASK;
1333 reg |= dto << DTO_SHIFT;
1334 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1335}
1336
1337/*
1338 * Configure block length for MMC/SD cards and initiate the transfer.
1339 */
1340static int
Denis Karpov70a33412009-09-22 16:44:59 -07001341omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001342{
1343 int ret;
1344 host->data = req->data;
1345
1346 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001347 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001348 /*
1349 * Set an arbitrary 100ms data timeout for commands with
1350 * busy signal.
1351 */
1352 if (req->cmd->flags & MMC_RSP_BUSY)
1353 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001354 return 0;
1355 }
1356
1357 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1358 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001359 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001360
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001361 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001362 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001363 if (ret != 0) {
1364 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1365 return ret;
1366 }
1367 }
1368 return 0;
1369}
1370
Per Forlin9782aff2011-07-01 18:55:23 +02001371static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1372 int err)
1373{
1374 struct omap_hsmmc_host *host = mmc_priv(mmc);
1375 struct mmc_data *data = mrq->data;
1376
Russell King26b88522012-04-13 12:27:37 +01001377 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001378 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001379
Russell King26b88522012-04-13 12:27:37 +01001380 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1381 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001382 data->host_cookie = 0;
1383 }
1384}
1385
1386static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1387 bool is_first_req)
1388{
1389 struct omap_hsmmc_host *host = mmc_priv(mmc);
1390
1391 if (mrq->data->host_cookie) {
1392 mrq->data->host_cookie = 0;
1393 return ;
1394 }
1395
Russell Kingc5c98922012-04-13 12:14:39 +01001396 if (host->use_dma) {
1397 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001398
Per Forlin9782aff2011-07-01 18:55:23 +02001399 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001400 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001401 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001402 }
Per Forlin9782aff2011-07-01 18:55:23 +02001403}
1404
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001405/*
1406 * Request function. for read/write operation
1407 */
Denis Karpov70a33412009-09-22 16:44:59 -07001408static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001409{
Denis Karpov70a33412009-09-22 16:44:59 -07001410 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001411 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001412
Adrian Hunterb4175772010-05-26 14:42:06 -07001413 BUG_ON(host->req_in_progress);
1414 BUG_ON(host->dma_ch != -1);
1415 if (host->protect_card) {
1416 if (host->reqs_blocked < 3) {
1417 /*
1418 * Ensure the controller is left in a consistent
1419 * state by resetting the command and data state
1420 * machines.
1421 */
1422 omap_hsmmc_reset_controller_fsm(host, SRD);
1423 omap_hsmmc_reset_controller_fsm(host, SRC);
1424 host->reqs_blocked += 1;
1425 }
1426 req->cmd->error = -EBADF;
1427 if (req->data)
1428 req->data->error = -EBADF;
1429 req->cmd->retries = 0;
1430 mmc_request_done(mmc, req);
1431 return;
1432 } else if (host->reqs_blocked)
1433 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001434 WARN_ON(host->mrq != NULL);
1435 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001436 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001437 if (err) {
1438 req->cmd->error = err;
1439 if (req->data)
1440 req->data->error = err;
1441 host->mrq = NULL;
1442 mmc_request_done(mmc, req);
1443 return;
1444 }
1445
Denis Karpov70a33412009-09-22 16:44:59 -07001446 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001447}
1448
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001449/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001450static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001451{
Denis Karpov70a33412009-09-22 16:44:59 -07001452 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001453 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001454
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301455 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001456
Adrian Huntera3621462009-09-22 16:44:42 -07001457 if (ios->power_mode != host->power_mode) {
1458 switch (ios->power_mode) {
1459 case MMC_POWER_OFF:
1460 mmc_slot(host).set_power(host->dev, host->slot_id,
1461 0, 0);
1462 break;
1463 case MMC_POWER_UP:
1464 mmc_slot(host).set_power(host->dev, host->slot_id,
1465 1, ios->vdd);
1466 break;
1467 case MMC_POWER_ON:
1468 do_send_init_stream = 1;
1469 break;
1470 }
1471 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001472 }
1473
Denis Karpovdd498ef2009-09-22 16:44:49 -07001474 /* FIXME: set registers based only on changes to ios */
1475
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001476 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001477
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301478 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001479 /* Only MMC1 can interface at 3V without some flavor
1480 * of external transceiver; but they all handle 1.8V.
1481 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001482 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Rajendra Nayak1f84b712012-03-12 20:32:38 +05301483 (ios->vdd == DUAL_VOLT_OCR_BIT) &&
1484 /*
1485 * With pbias cell programming missing, this
1486 * can't be allowed when booting with device
1487 * tree.
1488 */
Rajendra Nayak4d048f92012-04-11 15:33:13 +05301489 !host->dev->of_node) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001490 /*
1491 * The mmc_select_voltage fn of the core does
1492 * not seem to set the power_mode to
1493 * MMC_POWER_UP upon recalculating the voltage.
1494 * vdd 1.8v.
1495 */
Denis Karpov70a33412009-09-22 16:44:59 -07001496 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1497 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001498 "Switch operation failed\n");
1499 }
1500 }
1501
Andy Shevchenko5934df22011-05-06 12:14:06 +03001502 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001503
Adrian Huntera3621462009-09-22 16:44:42 -07001504 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001505 send_init_stream(host);
1506
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001507 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001508
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301509 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001510}
1511
1512static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1513{
Denis Karpov70a33412009-09-22 16:44:59 -07001514 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001515
Denis Karpov191d1f12009-09-22 16:44:55 -07001516 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001517 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001518 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001519}
1520
1521static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1522{
Denis Karpov70a33412009-09-22 16:44:59 -07001523 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001524
Denis Karpov191d1f12009-09-22 16:44:55 -07001525 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001526 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001527 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001528}
1529
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001530static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1531{
1532 struct omap_hsmmc_host *host = mmc_priv(mmc);
1533
1534 if (mmc_slot(host).init_card)
1535 mmc_slot(host).init_card(card);
1536}
1537
Denis Karpov70a33412009-09-22 16:44:59 -07001538static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001539{
1540 u32 hctl, capa, value;
1541
1542 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301543 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001544 hctl = SDVS30;
1545 capa = VS30 | VS18;
1546 } else {
1547 hctl = SDVS18;
1548 capa = VS18;
1549 }
1550
1551 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1552 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1553
1554 value = OMAP_HSMMC_READ(host->base, CAPA);
1555 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1556
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001557 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001558 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001559}
1560
Denis Karpov70a33412009-09-22 16:44:59 -07001561static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001562{
Denis Karpov70a33412009-09-22 16:44:59 -07001563 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001564
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301565 pm_runtime_get_sync(host->dev);
1566
Denis Karpovdd498ef2009-09-22 16:44:49 -07001567 return 0;
1568}
1569
Adrian Hunter907d2e72012-02-29 09:17:21 +02001570static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001571{
Denis Karpov70a33412009-09-22 16:44:59 -07001572 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001573
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301574 pm_runtime_mark_last_busy(host->dev);
1575 pm_runtime_put_autosuspend(host->dev);
1576
Denis Karpovdd498ef2009-09-22 16:44:49 -07001577 return 0;
1578}
1579
Denis Karpov70a33412009-09-22 16:44:59 -07001580static const struct mmc_host_ops omap_hsmmc_ops = {
1581 .enable = omap_hsmmc_enable_fclk,
1582 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001583 .post_req = omap_hsmmc_post_req,
1584 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001585 .request = omap_hsmmc_request,
1586 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001587 .get_cd = omap_hsmmc_get_cd,
1588 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001589 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001590 /* NYET -- enable_sdio_irq */
1591};
1592
Denis Karpovd900f712009-09-22 16:44:38 -07001593#ifdef CONFIG_DEBUG_FS
1594
Denis Karpov70a33412009-09-22 16:44:59 -07001595static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001596{
1597 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001598 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001599 int context_loss = 0;
1600
Denis Karpov70a33412009-09-22 16:44:59 -07001601 if (host->pdata->get_context_loss_count)
1602 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001603
Adrian Hunter907d2e72012-02-29 09:17:21 +02001604 seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1605 mmc->index, host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001606
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301607 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001608 seq_printf(s, "host suspended, can't read registers\n");
1609 return 0;
1610 }
1611
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301612 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001613
Denis Karpovd900f712009-09-22 16:44:38 -07001614 seq_printf(s, "CON:\t\t0x%08x\n",
1615 OMAP_HSMMC_READ(host->base, CON));
1616 seq_printf(s, "HCTL:\t\t0x%08x\n",
1617 OMAP_HSMMC_READ(host->base, HCTL));
1618 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1619 OMAP_HSMMC_READ(host->base, SYSCTL));
1620 seq_printf(s, "IE:\t\t0x%08x\n",
1621 OMAP_HSMMC_READ(host->base, IE));
1622 seq_printf(s, "ISE:\t\t0x%08x\n",
1623 OMAP_HSMMC_READ(host->base, ISE));
1624 seq_printf(s, "CAPA:\t\t0x%08x\n",
1625 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001626
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301627 pm_runtime_mark_last_busy(host->dev);
1628 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001629
Denis Karpovd900f712009-09-22 16:44:38 -07001630 return 0;
1631}
1632
Denis Karpov70a33412009-09-22 16:44:59 -07001633static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001634{
Denis Karpov70a33412009-09-22 16:44:59 -07001635 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001636}
1637
1638static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001639 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001640 .read = seq_read,
1641 .llseek = seq_lseek,
1642 .release = single_release,
1643};
1644
Denis Karpov70a33412009-09-22 16:44:59 -07001645static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001646{
1647 if (mmc->debugfs_root)
1648 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1649 mmc, &mmc_regs_fops);
1650}
1651
1652#else
1653
Denis Karpov70a33412009-09-22 16:44:59 -07001654static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001655{
1656}
1657
1658#endif
1659
Rajendra Nayak46856a62012-03-12 20:32:37 +05301660#ifdef CONFIG_OF
1661static u16 omap4_reg_offset = 0x100;
1662
1663static const struct of_device_id omap_mmc_of_match[] = {
1664 {
1665 .compatible = "ti,omap2-hsmmc",
1666 },
1667 {
1668 .compatible = "ti,omap3-hsmmc",
1669 },
1670 {
1671 .compatible = "ti,omap4-hsmmc",
1672 .data = &omap4_reg_offset,
1673 },
1674 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001675};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301676MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1677
1678static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1679{
1680 struct omap_mmc_platform_data *pdata;
1681 struct device_node *np = dev->of_node;
1682 u32 bus_width;
1683
1684 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1685 if (!pdata)
1686 return NULL; /* out of memory */
1687
1688 if (of_find_property(np, "ti,dual-volt", NULL))
1689 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1690
1691 /* This driver only supports 1 slot */
1692 pdata->nr_slots = 1;
1693 pdata->slots[0].switch_pin = of_get_named_gpio(np, "cd-gpios", 0);
1694 pdata->slots[0].gpio_wp = of_get_named_gpio(np, "wp-gpios", 0);
1695
1696 if (of_find_property(np, "ti,non-removable", NULL)) {
1697 pdata->slots[0].nonremovable = true;
1698 pdata->slots[0].no_regulator_off_init = true;
1699 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001700 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301701 if (bus_width == 4)
1702 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1703 else if (bus_width == 8)
1704 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1705
1706 if (of_find_property(np, "ti,needs-special-reset", NULL))
1707 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1708
1709 return pdata;
1710}
1711#else
1712static inline struct omap_mmc_platform_data
1713 *of_get_hsmmc_pdata(struct device *dev)
1714{
1715 return NULL;
1716}
1717#endif
1718
Felipe Balbiefa25fd2012-03-14 11:18:28 +02001719static int __devinit omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001720{
1721 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1722 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001723 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001724 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001725 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301726 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001727 dma_cap_mask_t mask;
1728 unsigned tx_req, rx_req;
Daniel Mack46b76032012-10-15 21:35:05 +05301729 struct pinctrl *pinctrl;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301730
1731 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1732 if (match) {
1733 pdata = of_get_hsmmc_pdata(&pdev->dev);
1734 if (match->data) {
Uwe Kleine-Königefc9b732012-05-21 21:57:39 +02001735 const u16 *offsetp = match->data;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301736 pdata->reg_offset = *offsetp;
1737 }
1738 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001739
1740 if (pdata == NULL) {
1741 dev_err(&pdev->dev, "Platform Data is missing\n");
1742 return -ENXIO;
1743 }
1744
1745 if (pdata->nr_slots == 0) {
1746 dev_err(&pdev->dev, "No Slots\n");
1747 return -ENXIO;
1748 }
1749
1750 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1751 irq = platform_get_irq(pdev, 0);
1752 if (res == NULL || irq < 0)
1753 return -ENXIO;
1754
Chris Ball984b2032011-03-22 16:34:42 -07001755 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001756 if (res == NULL)
1757 return -EBUSY;
1758
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001759 ret = omap_hsmmc_gpio_init(pdata);
1760 if (ret)
1761 goto err;
1762
Denis Karpov70a33412009-09-22 16:44:59 -07001763 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001764 if (!mmc) {
1765 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001766 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001767 }
1768
1769 host = mmc_priv(mmc);
1770 host->mmc = mmc;
1771 host->pdata = pdata;
1772 host->dev = &pdev->dev;
1773 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001774 host->dma_ch = -1;
1775 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001776 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05301777 host->mapbase = res->start + pdata->reg_offset;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001778 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001779 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001780 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001781
1782 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001783
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301784 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001785
Adrian Huntere0eb2422010-02-15 10:03:34 -08001786 /*
1787 * If regulator_disable can only put vcc_aux to sleep then there is
1788 * no off state.
1789 */
1790 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1791 mmc_slot(host).no_off = 1;
1792
Daniel Mackd418ed82012-02-19 13:20:33 +01001793 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1794
1795 if (pdata->max_freq > 0)
1796 mmc->f_max = pdata->max_freq;
1797 else
1798 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001799
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001800 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001801
Russell King6f7607c2009-01-28 10:22:50 +00001802 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001803 if (IS_ERR(host->fclk)) {
1804 ret = PTR_ERR(host->fclk);
1805 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001806 goto err1;
1807 }
1808
Paul Walmsley9b682562011-10-06 14:50:35 -06001809 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1810 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1811 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1812 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001813
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301814 pm_runtime_enable(host->dev);
1815 pm_runtime_get_sync(host->dev);
1816 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1817 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001818
Balaji T K92a3aeb2012-02-24 21:14:34 +05301819 omap_hsmmc_context_save(host);
1820
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301821 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1822 /*
1823 * MMC can still work without debounce clock.
1824 */
1825 if (IS_ERR(host->dbclk)) {
1826 dev_warn(mmc_dev(host->mmc), "Failed to get debounce clk\n");
1827 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05301828 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301829 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
1830 clk_put(host->dbclk);
1831 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07001832 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001833
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001834 /* Since we do only SG emulation, we can have as many segs
1835 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001836 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001837
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001838 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1839 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1840 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1841 mmc->max_seg_size = mmc->max_req_size;
1842
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001843 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001844 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001845
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001846 mmc->caps |= mmc_slot(host).caps;
1847 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001848 mmc->caps |= MMC_CAP_4_BIT_DATA;
1849
Denis Karpov191d1f12009-09-22 16:44:55 -07001850 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001851 mmc->caps |= MMC_CAP_NONREMOVABLE;
1852
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001853 mmc->pm_caps = mmc_slot(host).pm_caps;
1854
Denis Karpov70a33412009-09-22 16:44:59 -07001855 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001856
Balaji T Kb7bf7732012-03-07 09:55:30 -05001857 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1858 if (!res) {
1859 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
Kevin Hilman9c17d082012-07-10 16:40:56 -07001860 ret = -ENXIO;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001861 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001862 }
Russell King26b88522012-04-13 12:27:37 +01001863 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001864
1865 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1866 if (!res) {
1867 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
Kevin Hilman9c17d082012-07-10 16:40:56 -07001868 ret = -ENXIO;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001869 goto err_irq;
1870 }
Russell King26b88522012-04-13 12:27:37 +01001871 rx_req = res->start;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001872
Russell King26b88522012-04-13 12:27:37 +01001873 dma_cap_zero(mask);
1874 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01001875
Russell King26b88522012-04-13 12:27:37 +01001876 host->rx_chan = dma_request_channel(mask, omap_dma_filter_fn, &rx_req);
1877 if (!host->rx_chan) {
1878 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001879 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001880 goto err_irq;
1881 }
1882
1883 host->tx_chan = dma_request_channel(mask, omap_dma_filter_fn, &tx_req);
1884 if (!host->tx_chan) {
1885 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001886 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001887 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01001888 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001889
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001890 /* Request IRQ for MMC operations */
Yong Zhangd9618e92011-09-22 16:59:04 +08001891 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001892 mmc_hostname(mmc), host);
1893 if (ret) {
1894 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1895 goto err_irq;
1896 }
1897
1898 if (pdata->init != NULL) {
1899 if (pdata->init(&pdev->dev) != 0) {
Denis Karpov70a33412009-09-22 16:44:59 -07001900 dev_dbg(mmc_dev(host->mmc),
1901 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001902 goto err_irq_cd_init;
1903 }
1904 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001905
Adrian Hunterb702b102010-02-15 10:03:35 -08001906 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001907 ret = omap_hsmmc_reg_get(host);
1908 if (ret)
1909 goto err_reg;
1910 host->use_reg = 1;
1911 }
1912
David Brownellb583f262009-05-28 14:04:03 -07001913 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001914
1915 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001916 if ((mmc_slot(host).card_detect_irq)) {
NeilBrown7efab4f2011-12-30 12:35:13 +11001917 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1918 NULL,
1919 omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08001920 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11001921 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001922 if (ret) {
1923 dev_dbg(mmc_dev(host->mmc),
1924 "Unable to grab MMC CD IRQ\n");
1925 goto err_irq_cd;
1926 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00001927 pdata->suspend = omap_hsmmc_suspend_cdirq;
1928 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001929 }
1930
Adrian Hunterb4175772010-05-26 14:42:06 -07001931 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001932
Daniel Mack46b76032012-10-15 21:35:05 +05301933 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1934 if (IS_ERR(pinctrl))
1935 dev_warn(&pdev->dev,
1936 "pins are not configured from the driver\n");
1937
Adrian Hunterb62f6222009-09-22 16:45:01 -07001938 omap_hsmmc_protect_card(host);
1939
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001940 mmc_add_host(mmc);
1941
Denis Karpov191d1f12009-09-22 16:44:55 -07001942 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001943 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1944 if (ret < 0)
1945 goto err_slot_name;
1946 }
Denis Karpov191d1f12009-09-22 16:44:55 -07001947 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001948 ret = device_create_file(&mmc->class_dev,
1949 &dev_attr_cover_switch);
1950 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001951 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001952 }
1953
Denis Karpov70a33412009-09-22 16:44:59 -07001954 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301955 pm_runtime_mark_last_busy(host->dev);
1956 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001957
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001958 return 0;
1959
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001960err_slot_name:
1961 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001962 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001963err_irq_cd:
1964 if (host->use_reg)
1965 omap_hsmmc_reg_put(host);
1966err_reg:
1967 if (host->pdata->cleanup)
1968 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001969err_irq_cd_init:
1970 free_irq(host->irq, host);
1971err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01001972 if (host->tx_chan)
1973 dma_release_channel(host->tx_chan);
1974 if (host->rx_chan)
1975 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05301976 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05001977 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001978 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301979 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05301980 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001981 clk_put(host->dbclk);
1982 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001983err1:
1984 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001985 platform_set_drvdata(pdev, NULL);
1986 mmc_free_host(mmc);
1987err_alloc:
1988 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001989err:
Russell King48b332f2012-04-18 11:11:57 +01001990 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1991 if (res)
1992 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001993 return ret;
1994}
1995
Felipe Balbiefa25fd2012-03-14 11:18:28 +02001996static int __devexit omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001997{
Denis Karpov70a33412009-09-22 16:44:59 -07001998 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001999 struct resource *res;
2000
Felipe Balbi927ce942012-03-14 11:18:27 +02002001 pm_runtime_get_sync(host->dev);
2002 mmc_remove_host(host->mmc);
2003 if (host->use_reg)
2004 omap_hsmmc_reg_put(host);
2005 if (host->pdata->cleanup)
2006 host->pdata->cleanup(&pdev->dev);
2007 free_irq(host->irq, host);
2008 if (mmc_slot(host).card_detect_irq)
2009 free_irq(mmc_slot(host).card_detect_irq, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002010
Russell Kingc5c98922012-04-13 12:14:39 +01002011 if (host->tx_chan)
2012 dma_release_channel(host->tx_chan);
2013 if (host->rx_chan)
2014 dma_release_channel(host->rx_chan);
2015
Felipe Balbi927ce942012-03-14 11:18:27 +02002016 pm_runtime_put_sync(host->dev);
2017 pm_runtime_disable(host->dev);
2018 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302019 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302020 clk_disable_unprepare(host->dbclk);
Felipe Balbi927ce942012-03-14 11:18:27 +02002021 clk_put(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002022 }
2023
Balaji T K9ea28ec2012-10-15 21:35:08 +05302024 omap_hsmmc_gpio_free(host->pdata);
Felipe Balbi927ce942012-03-14 11:18:27 +02002025 iounmap(host->base);
Balaji T K9d1f0282012-10-15 21:35:07 +05302026 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002027
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002028 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2029 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002030 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002031 platform_set_drvdata(pdev, NULL);
2032
2033 return 0;
2034}
2035
2036#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002037static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002038{
2039 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002040 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2041
2042 if (!host)
2043 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002044
2045 if (host && host->suspended)
2046 return 0;
2047
Felipe Balbi927ce942012-03-14 11:18:27 +02002048 pm_runtime_get_sync(host->dev);
2049 host->suspended = 1;
2050 if (host->pdata->suspend) {
2051 ret = host->pdata->suspend(dev, host->slot_id);
Eliad Peller31f9d462011-11-22 16:02:17 +02002052 if (ret) {
Felipe Balbi927ce942012-03-14 11:18:27 +02002053 dev_dbg(dev, "Unable to handle MMC board"
2054 " level suspend\n");
Adrian Huntera6b22402009-09-22 16:44:45 -07002055 host->suspended = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002056 return ret;
Adrian Huntera6b22402009-09-22 16:44:45 -07002057 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002058 }
Felipe Balbi927ce942012-03-14 11:18:27 +02002059 ret = mmc_suspend_host(host->mmc);
2060
2061 if (ret) {
2062 host->suspended = 0;
2063 if (host->pdata->resume) {
Vaibhav Bediac4c8eeb2012-09-13 06:31:03 +00002064 if (host->pdata->resume(dev, host->slot_id))
Felipe Balbi927ce942012-03-14 11:18:27 +02002065 dev_dbg(dev, "Unmask interrupt failed\n");
2066 }
2067 goto err;
2068 }
2069
2070 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2071 omap_hsmmc_disable_irq(host);
2072 OMAP_HSMMC_WRITE(host->base, HCTL,
2073 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2074 }
2075
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302076 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302077 clk_disable_unprepare(host->dbclk);
Eliad Peller31f9d462011-11-22 16:02:17 +02002078err:
2079 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002080 return ret;
2081}
2082
2083/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002084static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002085{
2086 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002087 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2088
2089 if (!host)
2090 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002091
2092 if (host && !host->suspended)
2093 return 0;
2094
Felipe Balbi927ce942012-03-14 11:18:27 +02002095 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002096
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302097 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302098 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002099
Felipe Balbi927ce942012-03-14 11:18:27 +02002100 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2101 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002102
Felipe Balbi927ce942012-03-14 11:18:27 +02002103 if (host->pdata->resume) {
2104 ret = host->pdata->resume(dev, host->slot_id);
2105 if (ret)
2106 dev_dbg(dev, "Unmask interrupt failed\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002107 }
2108
Felipe Balbi927ce942012-03-14 11:18:27 +02002109 omap_hsmmc_protect_card(host);
2110
2111 /* Notify the core to resume the host */
2112 ret = mmc_resume_host(host->mmc);
2113 if (ret == 0)
2114 host->suspended = 0;
2115
2116 pm_runtime_mark_last_busy(host->dev);
2117 pm_runtime_put_autosuspend(host->dev);
2118
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002119 return ret;
2120
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002121}
2122
2123#else
Denis Karpov70a33412009-09-22 16:44:59 -07002124#define omap_hsmmc_suspend NULL
2125#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002126#endif
2127
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302128static int omap_hsmmc_runtime_suspend(struct device *dev)
2129{
2130 struct omap_hsmmc_host *host;
2131
2132 host = platform_get_drvdata(to_platform_device(dev));
2133 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002134 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302135
2136 return 0;
2137}
2138
2139static int omap_hsmmc_runtime_resume(struct device *dev)
2140{
2141 struct omap_hsmmc_host *host;
2142
2143 host = platform_get_drvdata(to_platform_device(dev));
2144 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002145 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302146
2147 return 0;
2148}
2149
Kevin Hilmana791daa2010-05-26 14:42:07 -07002150static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002151 .suspend = omap_hsmmc_suspend,
2152 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302153 .runtime_suspend = omap_hsmmc_runtime_suspend,
2154 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002155};
2156
2157static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002158 .probe = omap_hsmmc_probe,
2159 .remove = __devexit_p(omap_hsmmc_remove),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002160 .driver = {
2161 .name = DRIVER_NAME,
2162 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002163 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302164 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002165 },
2166};
2167
Felipe Balbib7964502012-03-14 11:18:32 +02002168module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002169MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2170MODULE_LICENSE("GPL");
2171MODULE_ALIAS("platform:" DRIVER_NAME);
2172MODULE_AUTHOR("Texas Instruments Inc");