blob: f5de0b7052a527c5d0867b497e14340ff40f5791 [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>
Felipe Balbi031cd032013-07-11 16:09:05 +030024#include <linux/sizes.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010025#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/dma-mapping.h>
28#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010029#include <linux/timer.h>
30#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053031#include <linux/of.h>
32#include <linux/of_gpio.h>
33#include <linux/of_device.h>
Russell King3451c062012-04-21 22:35:42 +010034#include <linux/omap-dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010035#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070036#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070037#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010038#include <linux/io.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080039#include <linux/gpio.h>
40#include <linux/regulator/consumer.h>
Daniel Mack46b76032012-10-15 21:35:05 +053041#include <linux/pinctrl/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053042#include <linux/pm_runtime.h>
Tony Lindgren68f39e72012-10-15 12:09:43 -070043#include <linux/platform_data/mmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010044
45/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070046#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010047#define OMAP_HSMMC_CON 0x002C
48#define OMAP_HSMMC_BLK 0x0104
49#define OMAP_HSMMC_ARG 0x0108
50#define OMAP_HSMMC_CMD 0x010C
51#define OMAP_HSMMC_RSP10 0x0110
52#define OMAP_HSMMC_RSP32 0x0114
53#define OMAP_HSMMC_RSP54 0x0118
54#define OMAP_HSMMC_RSP76 0x011C
55#define OMAP_HSMMC_DATA 0x0120
56#define OMAP_HSMMC_HCTL 0x0128
57#define OMAP_HSMMC_SYSCTL 0x012C
58#define OMAP_HSMMC_STAT 0x0130
59#define OMAP_HSMMC_IE 0x0134
60#define OMAP_HSMMC_ISE 0x0138
61#define OMAP_HSMMC_CAPA 0x0140
62
63#define VS18 (1 << 26)
64#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053065#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010066#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
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010082#define INIT_STREAM (1 << 1)
83#define DP_SELECT (1 << 21)
84#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053085#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010086#define MSBS (1 << 5)
87#define BCE (1 << 1)
88#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053089#define HSPE (1 << 2)
Balaji T K03b5d922012-04-09 12:08:33 +053090#define DDR (1 << 19)
Jarkko Lavinen73153012008-11-21 16:49:54 +020091#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010092#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010093#define STAT_CLEAR 0xFFFFFFFF
94#define INIT_STREAM_CMD 0x00000000
95#define DUAL_VOLT_OCR_BIT 7
96#define SRC (1 << 25)
97#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -070098#define SOFTRESET (1 << 1)
99#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100100
Venkatraman Sa7e96872012-11-19 22:00:01 +0530101/* Interrupt masks for IE and ISE register */
102#define CC_EN (1 << 0)
103#define TC_EN (1 << 1)
104#define BWR_EN (1 << 4)
105#define BRR_EN (1 << 5)
106#define ERR_EN (1 << 15)
107#define CTO_EN (1 << 16)
108#define CCRC_EN (1 << 17)
109#define CEB_EN (1 << 18)
110#define CIE_EN (1 << 19)
111#define DTO_EN (1 << 20)
112#define DCRC_EN (1 << 21)
113#define DEB_EN (1 << 22)
114#define CERR_EN (1 << 28)
115#define BADA_EN (1 << 29)
116
117#define INT_EN_MASK (BADA_EN | CERR_EN | DEB_EN | DCRC_EN |\
118 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
119 BRR_EN | BWR_EN | TC_EN | CC_EN)
120
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530121#define MMC_AUTOSUSPEND_DELAY 100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100122#define MMC_TIMEOUT_MS 20
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400123#define OMAP_MMC_MIN_CLOCK 400000
124#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530125#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100126
127/*
128 * One controller can have multiple slots, like on some omap boards using
129 * omap.c controller driver. Luckily this is not currently done on any known
130 * omap_hsmmc.c device.
131 */
132#define mmc_slot(host) (host->pdata->slots[host->slot_id])
133
134/*
135 * MMC Host controller read/write API's
136 */
137#define OMAP_HSMMC_READ(base, reg) \
138 __raw_readl((base) + OMAP_HSMMC_##reg)
139
140#define OMAP_HSMMC_WRITE(base, reg, val) \
141 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
142
Per Forlin9782aff2011-07-01 18:55:23 +0200143struct omap_hsmmc_next {
144 unsigned int dma_len;
145 s32 cookie;
146};
147
Denis Karpov70a33412009-09-22 16:44:59 -0700148struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100149 struct device *dev;
150 struct mmc_host *mmc;
151 struct mmc_request *mrq;
152 struct mmc_command *cmd;
153 struct mmc_data *data;
154 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100155 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800156 /*
157 * vcc == configured supply
158 * vcc_aux == optional
159 * - MMC1, supply for DAT4..DAT7
160 * - MMC2/MMC2, external level shifter voltage supply, for
161 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
162 */
163 struct regulator *vcc;
164 struct regulator *vcc_aux;
Tony Lindgrencf5ae402013-05-10 17:42:33 +0530165 int pbias_disable;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100166 void __iomem *base;
167 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700168 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100169 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200170 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100171 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700172 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100173 int suspended;
174 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100175 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100176 struct dma_chan *tx_chan;
177 struct dma_chan *rx_chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100178 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200179 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700180 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700181 int protect_card;
182 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800183 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700184 int req_in_progress;
Per Forlin9782aff2011-07-01 18:55:23 +0200185 struct omap_hsmmc_next next_data;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700186
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100187 struct omap_mmc_platform_data *pdata;
188};
189
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800190static int omap_hsmmc_card_detect(struct device *dev, int slot)
191{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530192 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
193 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800194
195 /* NOTE: assumes card detect signal is active-low */
196 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
197}
198
199static int omap_hsmmc_get_wp(struct device *dev, int slot)
200{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530201 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
202 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800203
204 /* NOTE: assumes write protect signal is active-high */
205 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
206}
207
208static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
209{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530210 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
211 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800212
213 /* NOTE: assumes card detect signal is active-low */
214 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
215}
216
217#ifdef CONFIG_PM
218
219static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
220{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530221 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
222 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800223
224 disable_irq(mmc->slots[0].card_detect_irq);
225 return 0;
226}
227
228static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
229{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530230 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
231 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800232
233 enable_irq(mmc->slots[0].card_detect_irq);
234 return 0;
235}
236
237#else
238
239#define omap_hsmmc_suspend_cdirq NULL
240#define omap_hsmmc_resume_cdirq NULL
241
242#endif
243
Adrian Hunterb702b102010-02-15 10:03:35 -0800244#ifdef CONFIG_REGULATOR
245
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500246static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800247 int vdd)
248{
249 struct omap_hsmmc_host *host =
250 platform_get_drvdata(to_platform_device(dev));
251 int ret = 0;
252
253 /*
254 * If we don't see a Vcc regulator, assume it's a fixed
255 * voltage always-on regulator.
256 */
257 if (!host->vcc)
258 return 0;
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530259 /*
Tony Lindgrencf5ae402013-05-10 17:42:33 +0530260 * With DT, never turn OFF the regulator for MMC1. This is because
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530261 * the pbias cell programming support is still missing when
262 * booting with Device tree
263 */
Tony Lindgrencf5ae402013-05-10 17:42:33 +0530264 if (host->pbias_disable && !vdd)
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530265 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800266
267 if (mmc_slot(host).before_set_reg)
268 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
269
270 /*
271 * Assume Vcc regulator is used only to power the card ... OMAP
272 * VDDS is used to power the pins, optionally with a transceiver to
273 * support cards using voltages other than VDDS (1.8V nominal). When a
274 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
275 *
276 * In some cases this regulator won't support enable/disable;
277 * e.g. it's a fixed rail for a WLAN chip.
278 *
279 * In other cases vcc_aux switches interface power. Example, for
280 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
281 * chips/cards need an interface voltage rail too.
282 */
283 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400284 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800285 /* Enable interface voltage rail, if needed */
286 if (ret == 0 && host->vcc_aux) {
287 ret = regulator_enable(host->vcc_aux);
288 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400289 ret = mmc_regulator_set_ocr(host->mmc,
290 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800291 }
292 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400293 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800294 if (host->vcc_aux)
295 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400296 if (!ret) {
297 /* Then proceed to shut down the local regulator */
298 ret = mmc_regulator_set_ocr(host->mmc,
299 host->vcc, 0);
300 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800301 }
302
303 if (mmc_slot(host).after_set_reg)
304 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
305
306 return ret;
307}
308
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800309static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
310{
311 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700312 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800313
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800314 reg = regulator_get(host->dev, "vmmc");
315 if (IS_ERR(reg)) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +0530316 dev_err(host->dev, "vmmc regulator missing\n");
NeilBrown1fdc90f2012-08-08 00:06:00 -0400317 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800318 } else {
NeilBrown1fdc90f2012-08-08 00:06:00 -0400319 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800320 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700321 ocr_value = mmc_regulator_get_ocrmask(reg);
322 if (!mmc_slot(host).ocr_mask) {
323 mmc_slot(host).ocr_mask = ocr_value;
324 } else {
325 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530326 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500327 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700328 mmc_slot(host).ocr_mask = 0;
329 return -EINVAL;
330 }
331 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800332
333 /* Allow an aux regulator */
334 reg = regulator_get(host->dev, "vmmc_aux");
335 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
336
Balaji T Kb1c1df72011-05-30 19:55:34 +0530337 /* For eMMC do not power off when not in sleep state */
338 if (mmc_slot(host).no_regulator_off_init)
339 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800340 /*
341 * UGLY HACK: workaround regulator framework bugs.
342 * When the bootloader leaves a supply active, it's
343 * initialized with zero usecount ... and we can't
344 * disable it without first enabling it. Until the
345 * framework is fixed, we need a workaround like this
346 * (which is safe for MMC, but not in general).
347 */
Adrian Huntere840ce12011-05-06 12:14:10 +0300348 if (regulator_is_enabled(host->vcc) > 0 ||
349 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
350 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
351
352 mmc_slot(host).set_power(host->dev, host->slot_id,
353 1, vdd);
354 mmc_slot(host).set_power(host->dev, host->slot_id,
355 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800356 }
357 }
358
359 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800360}
361
362static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
363{
364 regulator_put(host->vcc);
365 regulator_put(host->vcc_aux);
366 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800367}
368
Adrian Hunterb702b102010-02-15 10:03:35 -0800369static inline int omap_hsmmc_have_reg(void)
370{
371 return 1;
372}
373
374#else
375
376static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
377{
378 return -EINVAL;
379}
380
381static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
382{
383}
384
385static inline int omap_hsmmc_have_reg(void)
386{
387 return 0;
388}
389
390#endif
391
392static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
393{
394 int ret;
395
396 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800397 if (pdata->slots[0].cover)
398 pdata->slots[0].get_cover_state =
399 omap_hsmmc_get_cover_state;
400 else
401 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
402 pdata->slots[0].card_detect_irq =
403 gpio_to_irq(pdata->slots[0].switch_pin);
404 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
405 if (ret)
406 return ret;
407 ret = gpio_direction_input(pdata->slots[0].switch_pin);
408 if (ret)
409 goto err_free_sp;
410 } else
411 pdata->slots[0].switch_pin = -EINVAL;
412
413 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
414 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
415 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
416 if (ret)
417 goto err_free_cd;
418 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
419 if (ret)
420 goto err_free_wp;
421 } else
422 pdata->slots[0].gpio_wp = -EINVAL;
423
424 return 0;
425
426err_free_wp:
427 gpio_free(pdata->slots[0].gpio_wp);
428err_free_cd:
429 if (gpio_is_valid(pdata->slots[0].switch_pin))
430err_free_sp:
431 gpio_free(pdata->slots[0].switch_pin);
432 return ret;
433}
434
435static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
436{
437 if (gpio_is_valid(pdata->slots[0].gpio_wp))
438 gpio_free(pdata->slots[0].gpio_wp);
439 if (gpio_is_valid(pdata->slots[0].switch_pin))
440 gpio_free(pdata->slots[0].switch_pin);
441}
442
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100443/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300444 * Start clock to the card
445 */
446static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
447{
448 OMAP_HSMMC_WRITE(host->base, SYSCTL,
449 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
450}
451
452/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100453 * Stop clock to the card
454 */
Denis Karpov70a33412009-09-22 16:44:59 -0700455static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100456{
457 OMAP_HSMMC_WRITE(host->base, SYSCTL,
458 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
459 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900460 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100461}
462
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700463static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
464 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700465{
466 unsigned int irq_mask;
467
468 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530469 irq_mask = INT_EN_MASK & ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700470 else
471 irq_mask = INT_EN_MASK;
472
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700473 /* Disable timeout for erases */
474 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530475 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700476
Adrian Hunterb4175772010-05-26 14:42:06 -0700477 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
478 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
479 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
480}
481
482static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
483{
484 OMAP_HSMMC_WRITE(host->base, ISE, 0);
485 OMAP_HSMMC_WRITE(host->base, IE, 0);
486 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
487}
488
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300489/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530490static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300491{
492 u16 dsor = 0;
493
494 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530495 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300496 if (dsor > 250)
497 dsor = 250;
498 }
499
500 return dsor;
501}
502
Andy Shevchenko5934df22011-05-06 12:14:06 +0300503static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
504{
505 struct mmc_ios *ios = &host->mmc->ios;
506 unsigned long regval;
507 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530508 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300509
Venkatraman S8986d312012-08-07 19:10:38 +0530510 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300511
512 omap_hsmmc_stop_clock(host);
513
514 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
515 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530516 clkdiv = calc_divisor(host, ios);
517 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300518 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
519 OMAP_HSMMC_WRITE(host->base, SYSCTL,
520 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
521
522 /* Wait till the ICS bit is set */
523 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
524 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
525 && time_before(jiffies, timeout))
526 cpu_relax();
527
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530528 /*
529 * Enable High-Speed Support
530 * Pre-Requisites
531 * - Controller should support High-Speed-Enable Bit
532 * - Controller should not be using DDR Mode
533 * - Controller should advertise that it supports High Speed
534 * in capabilities register
535 * - MMC/SD clock coming out of controller > 25MHz
536 */
537 if ((mmc_slot(host).features & HSMMC_HAS_HSPE_SUPPORT) &&
538 (ios->timing != MMC_TIMING_UHS_DDR50) &&
539 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
540 regval = OMAP_HSMMC_READ(host->base, HCTL);
541 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
542 regval |= HSPE;
543 else
544 regval &= ~HSPE;
545
546 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
547 }
548
Andy Shevchenko5934df22011-05-06 12:14:06 +0300549 omap_hsmmc_start_clock(host);
550}
551
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400552static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
553{
554 struct mmc_ios *ios = &host->mmc->ios;
555 u32 con;
556
557 con = OMAP_HSMMC_READ(host->base, CON);
Balaji T K03b5d922012-04-09 12:08:33 +0530558 if (ios->timing == MMC_TIMING_UHS_DDR50)
559 con |= DDR; /* configure in DDR mode */
560 else
561 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400562 switch (ios->bus_width) {
563 case MMC_BUS_WIDTH_8:
564 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
565 break;
566 case MMC_BUS_WIDTH_4:
567 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
568 OMAP_HSMMC_WRITE(host->base, HCTL,
569 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
570 break;
571 case MMC_BUS_WIDTH_1:
572 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
573 OMAP_HSMMC_WRITE(host->base, HCTL,
574 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
575 break;
576 }
577}
578
579static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
580{
581 struct mmc_ios *ios = &host->mmc->ios;
582 u32 con;
583
584 con = OMAP_HSMMC_READ(host->base, CON);
585 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
586 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
587 else
588 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
589}
590
Denis Karpov11dd62a2009-09-22 16:44:43 -0700591#ifdef CONFIG_PM
592
593/*
594 * Restore the MMC host context, if it was lost as result of a
595 * power state change.
596 */
Denis Karpov70a33412009-09-22 16:44:59 -0700597static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700598{
599 struct mmc_ios *ios = &host->mmc->ios;
600 struct omap_mmc_platform_data *pdata = host->pdata;
601 int context_loss = 0;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400602 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700603 unsigned long timeout;
604
605 if (pdata->get_context_loss_count) {
606 context_loss = pdata->get_context_loss_count(host->dev);
607 if (context_loss < 0)
608 return 1;
609 }
610
611 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
612 context_loss == host->context_loss ? "not " : "");
613 if (host->context_loss == context_loss)
614 return 1;
615
Venkatraman S6c31b212012-08-08 14:26:52 +0530616 if (!OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE)
617 return 1;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700618
Balaji T Kc2200ef2012-03-07 09:55:30 -0500619 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700620 if (host->power_mode != MMC_POWER_OFF &&
621 (1 << ios->vdd) <= MMC_VDD_23_24)
622 hctl = SDVS18;
623 else
624 hctl = SDVS30;
625 capa = VS30 | VS18;
626 } else {
627 hctl = SDVS18;
628 capa = VS18;
629 }
630
631 OMAP_HSMMC_WRITE(host->base, HCTL,
632 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
633
634 OMAP_HSMMC_WRITE(host->base, CAPA,
635 OMAP_HSMMC_READ(host->base, CAPA) | capa);
636
637 OMAP_HSMMC_WRITE(host->base, HCTL,
638 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
639
640 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
641 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
642 && time_before(jiffies, timeout))
643 ;
644
Adrian Hunterb4175772010-05-26 14:42:06 -0700645 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700646
647 /* Do not initialize card-specific things if the power is off */
648 if (host->power_mode == MMC_POWER_OFF)
649 goto out;
650
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400651 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700652
Andy Shevchenko5934df22011-05-06 12:14:06 +0300653 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700654
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400655 omap_hsmmc_set_bus_mode(host);
656
Denis Karpov11dd62a2009-09-22 16:44:43 -0700657out:
658 host->context_loss = context_loss;
659
660 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
661 return 0;
662}
663
664/*
665 * Save the MMC host context (store the number of power state changes so far).
666 */
Denis Karpov70a33412009-09-22 16:44:59 -0700667static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700668{
669 struct omap_mmc_platform_data *pdata = host->pdata;
670 int context_loss;
671
672 if (pdata->get_context_loss_count) {
673 context_loss = pdata->get_context_loss_count(host->dev);
674 if (context_loss < 0)
675 return;
676 host->context_loss = context_loss;
677 }
678}
679
680#else
681
Denis Karpov70a33412009-09-22 16:44:59 -0700682static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700683{
684 return 0;
685}
686
Denis Karpov70a33412009-09-22 16:44:59 -0700687static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700688{
689}
690
691#endif
692
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100693/*
694 * Send init stream sequence to card
695 * before sending IDLE command
696 */
Denis Karpov70a33412009-09-22 16:44:59 -0700697static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100698{
699 int reg = 0;
700 unsigned long timeout;
701
Adrian Hunterb62f6222009-09-22 16:45:01 -0700702 if (host->protect_card)
703 return;
704
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100705 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700706
707 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100708 OMAP_HSMMC_WRITE(host->base, CON,
709 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
710 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
711
712 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530713 while ((reg != CC_EN) && time_before(jiffies, timeout))
714 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100715
716 OMAP_HSMMC_WRITE(host->base, CON,
717 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700718
719 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
720 OMAP_HSMMC_READ(host->base, STAT);
721
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100722 enable_irq(host->irq);
723}
724
725static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700726int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100727{
728 int r = 1;
729
Denis Karpov191d1f12009-09-22 16:44:55 -0700730 if (mmc_slot(host).get_cover_state)
731 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100732 return r;
733}
734
735static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700736omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100737 char *buf)
738{
739 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700740 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100741
Denis Karpov70a33412009-09-22 16:44:59 -0700742 return sprintf(buf, "%s\n",
743 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100744}
745
Denis Karpov70a33412009-09-22 16:44:59 -0700746static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100747
748static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700749omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100750 char *buf)
751{
752 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700753 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100754
Denis Karpov191d1f12009-09-22 16:44:55 -0700755 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100756}
757
Denis Karpov70a33412009-09-22 16:44:59 -0700758static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100759
760/*
761 * Configure the response type and send the cmd.
762 */
763static void
Denis Karpov70a33412009-09-22 16:44:59 -0700764omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100765 struct mmc_data *data)
766{
767 int cmdreg = 0, resptype = 0, cmdtype = 0;
768
Venkatraman S8986d312012-08-07 19:10:38 +0530769 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100770 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
771 host->cmd = cmd;
772
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700773 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100774
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200775 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100776 if (cmd->flags & MMC_RSP_PRESENT) {
777 if (cmd->flags & MMC_RSP_136)
778 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200779 else if (cmd->flags & MMC_RSP_BUSY) {
780 resptype = 3;
781 host->response_busy = 1;
782 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100783 resptype = 2;
784 }
785
786 /*
787 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
788 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
789 * a val of 0x3, rest 0x0.
790 */
791 if (cmd == host->mrq->stop)
792 cmdtype = 0x3;
793
794 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
795
796 if (data) {
797 cmdreg |= DP_SELECT | MSBS | BCE;
798 if (data->flags & MMC_DATA_READ)
799 cmdreg |= DDIR;
800 else
801 cmdreg &= ~(DDIR);
802 }
803
804 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530805 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100806
Adrian Hunterb4175772010-05-26 14:42:06 -0700807 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700808
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100809 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
810 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
811}
812
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200813static int
Denis Karpov70a33412009-09-22 16:44:59 -0700814omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200815{
816 if (data->flags & MMC_DATA_WRITE)
817 return DMA_TO_DEVICE;
818 else
819 return DMA_FROM_DEVICE;
820}
821
Russell Kingc5c98922012-04-13 12:14:39 +0100822static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
823 struct mmc_data *data)
824{
825 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
826}
827
Adrian Hunterb4175772010-05-26 14:42:06 -0700828static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
829{
830 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530831 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700832
Venkatraman S31463b12012-04-09 12:08:34 +0530833 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700834 host->req_in_progress = 0;
835 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530836 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700837
838 omap_hsmmc_disable_irq(host);
839 /* Do not complete the request if DMA is still in progress */
840 if (mrq->data && host->use_dma && dma_ch != -1)
841 return;
842 host->mrq = NULL;
843 mmc_request_done(host->mmc, mrq);
844}
845
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100846/*
847 * Notify the transfer complete to MMC core
848 */
849static void
Denis Karpov70a33412009-09-22 16:44:59 -0700850omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100851{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200852 if (!data) {
853 struct mmc_request *mrq = host->mrq;
854
Adrian Hunter23050102009-09-22 16:44:57 -0700855 /* TC before CC from CMD6 - don't know why, but it happens */
856 if (host->cmd && host->cmd->opcode == 6 &&
857 host->response_busy) {
858 host->response_busy = 0;
859 return;
860 }
861
Adrian Hunterb4175772010-05-26 14:42:06 -0700862 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200863 return;
864 }
865
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100866 host->data = NULL;
867
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100868 if (!data->error)
869 data->bytes_xfered += data->blocks * (data->blksz);
870 else
871 data->bytes_xfered = 0;
872
Ming Leife852272012-06-22 18:49:35 +0800873 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700874 omap_hsmmc_request_done(host, data->mrq);
Ming Leife852272012-06-22 18:49:35 +0800875 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100876 }
Ming Leife852272012-06-22 18:49:35 +0800877 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100878}
879
880/*
881 * Notify the core about command completion
882 */
883static void
Denis Karpov70a33412009-09-22 16:44:59 -0700884omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100885{
886 host->cmd = NULL;
887
888 if (cmd->flags & MMC_RSP_PRESENT) {
889 if (cmd->flags & MMC_RSP_136) {
890 /* response type 2 */
891 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
892 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
893 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
894 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
895 } else {
896 /* response types 1, 1b, 3, 4, 5, 6 */
897 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
898 }
899 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700900 if ((host->data == NULL && !host->response_busy) || cmd->error)
901 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100902}
903
904/*
905 * DMA clean up for command errors
906 */
Denis Karpov70a33412009-09-22 16:44:59 -0700907static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100908{
Adrian Hunterb4175772010-05-26 14:42:06 -0700909 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530910 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700911
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200912 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100913
Venkatraman S31463b12012-04-09 12:08:34 +0530914 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700915 dma_ch = host->dma_ch;
916 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530917 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700918
919 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100920 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
921
922 dmaengine_terminate_all(chan);
923 dma_unmap_sg(chan->device->dev,
924 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700925 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100926
Per Forlin053bf342011-11-07 21:55:11 +0530927 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100928 }
929 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100930}
931
932/*
933 * Readable error output
934 */
935#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300936static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100937{
938 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700939 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300940 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
941 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
942 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
943 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100944 };
945 char res[256];
946 char *buf = res;
947 int len, i;
948
949 len = sprintf(buf, "MMC IRQ 0x%x :", status);
950 buf += len;
951
Denis Karpov70a33412009-09-22 16:44:59 -0700952 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100953 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700954 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100955 buf += len;
956 }
957
Venkatraman S8986d312012-08-07 19:10:38 +0530958 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100959}
Adrian Hunter699b9582011-05-06 12:14:01 +0300960#else
961static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
962 u32 status)
963{
964}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100965#endif /* CONFIG_MMC_DEBUG */
966
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100967/*
968 * MMC controller internal state machines reset
969 *
970 * Used to reset command or data internal state machines, using respectively
971 * SRC or SRD bit of SYSCTL register
972 * Can be called from interrupt context
973 */
Denis Karpov70a33412009-09-22 16:44:59 -0700974static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
975 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100976{
977 unsigned long i = 0;
978 unsigned long limit = (loops_per_jiffy *
979 msecs_to_jiffies(MMC_TIMEOUT_MS));
980
981 OMAP_HSMMC_WRITE(host->base, SYSCTL,
982 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
983
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700984 /*
985 * OMAP4 ES2 and greater has an updated reset logic.
986 * Monitor a 0->1 transition first
987 */
988 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -0500989 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700990 && (i++ < limit))
991 cpu_relax();
992 }
993 i = 0;
994
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100995 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
996 (i++ < limit))
997 cpu_relax();
998
999 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1000 dev_err(mmc_dev(host->mmc),
1001 "Timeout waiting on controller reset in %s\n",
1002 __func__);
1003}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001004
Balaji T K25e18972012-11-19 21:59:55 +05301005static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1006 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301007{
Balaji T K25e18972012-11-19 21:59:55 +05301008 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301009 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301010 if (host->cmd)
1011 host->cmd->error = err;
1012 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301013
1014 if (host->data) {
1015 omap_hsmmc_reset_controller_fsm(host, SRD);
1016 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301017 } else if (host->mrq && host->mrq->cmd)
1018 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301019}
1020
Adrian Hunterb4175772010-05-26 14:42:06 -07001021static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001022{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001023 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001024 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001025
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001026 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301027 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001028
Venkatraman Sa7e96872012-11-19 22:00:01 +05301029 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001030 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001031
Venkatraman Sa7e96872012-11-19 22:00:01 +05301032 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301033 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301034 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301035 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301036 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301037 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1038
Venkatraman Sae4bf782012-08-09 20:36:07 +05301039 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301040 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301041 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001042 }
1043 }
1044
Francesco Lavra7472bab2013-06-29 08:25:12 +02001045 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301046 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001047 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301048 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001049 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001050}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001051
Adrian Hunterb4175772010-05-26 14:42:06 -07001052/*
1053 * MMC controller IRQ handler
1054 */
1055static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1056{
1057 struct omap_hsmmc_host *host = dev_id;
1058 int status;
1059
1060 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301061 while (status & INT_EN_MASK && host->req_in_progress) {
Adrian Hunterb4175772010-05-26 14:42:06 -07001062 omap_hsmmc_do_irq(host, status);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301063
Adrian Hunterb4175772010-05-26 14:42:06 -07001064 /* Flush posted write */
1065 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301066 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001067
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001068 return IRQ_HANDLED;
1069}
1070
Denis Karpov70a33412009-09-22 16:44:59 -07001071static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001072{
1073 unsigned long i;
1074
1075 OMAP_HSMMC_WRITE(host->base, HCTL,
1076 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1077 for (i = 0; i < loops_per_jiffy; i++) {
1078 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1079 break;
1080 cpu_relax();
1081 }
1082}
1083
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001084/*
David Brownelleb250822009-02-17 14:49:01 -08001085 * Switch MMC interface voltage ... only relevant for MMC1.
1086 *
1087 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1088 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1089 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001090 */
Denis Karpov70a33412009-09-22 16:44:59 -07001091static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001092{
1093 u32 reg_val = 0;
1094 int ret;
1095
1096 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301097 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301098 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301099 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001100
1101 /* Turn the power off */
1102 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001103
1104 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001105 if (!ret)
1106 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1107 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301108 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301109 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301110 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001111
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001112 if (ret != 0)
1113 goto err;
1114
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001115 OMAP_HSMMC_WRITE(host->base, HCTL,
1116 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1117 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001118
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001119 /*
1120 * If a MMC dual voltage card is detected, the set_ios fn calls
1121 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001122 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001123 *
David Brownelleb250822009-02-17 14:49:01 -08001124 * Cope with a bit of slop in the range ... per data sheets:
1125 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1126 * but recommended values are 1.71V to 1.89V
1127 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1128 * but recommended values are 2.7V to 3.3V
1129 *
1130 * Board setup code shouldn't permit anything very out-of-range.
1131 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1132 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001133 */
David Brownelleb250822009-02-17 14:49:01 -08001134 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001135 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001136 else
1137 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001138
1139 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001140 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001141
1142 return 0;
1143err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301144 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001145 return ret;
1146}
1147
Adrian Hunterb62f6222009-09-22 16:45:01 -07001148/* Protect the card while the cover is open */
1149static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1150{
1151 if (!mmc_slot(host).get_cover_state)
1152 return;
1153
1154 host->reqs_blocked = 0;
1155 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1156 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301157 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001158 "card is now accessible\n",
1159 mmc_hostname(host->mmc));
1160 host->protect_card = 0;
1161 }
1162 } else {
1163 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301164 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001165 "card is now inaccessible\n",
1166 mmc_hostname(host->mmc));
1167 host->protect_card = 1;
1168 }
1169 }
1170}
1171
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001172/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001173 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001174 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001175static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001176{
NeilBrown7efab4f2011-12-30 12:35:13 +11001177 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001178 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001179 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001180
Adrian Huntera6b22402009-09-22 16:44:45 -07001181 if (host->suspended)
NeilBrown7efab4f2011-12-30 12:35:13 +11001182 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001183
1184 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001185
Denis Karpov191d1f12009-09-22 16:44:55 -07001186 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001187 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001188 else {
1189 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001190 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001191 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001192
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001193 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001194 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001195 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001196 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001197 return IRQ_HANDLED;
1198}
1199
Russell Kingc5c98922012-04-13 12:14:39 +01001200static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001201{
Russell Kingc5c98922012-04-13 12:14:39 +01001202 struct omap_hsmmc_host *host = param;
1203 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001204 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001205 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001206
Russell Kingc5c98922012-04-13 12:14:39 +01001207 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001208 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001209 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001210 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001211 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001212
Adrian Hunter770d7432011-05-06 12:14:11 +03001213 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001214 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001215 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001216 dma_unmap_sg(chan->device->dev,
1217 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001218 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001219
1220 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001221 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001222 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001223
1224 /* If DMA has finished after TC, complete the request */
1225 if (!req_in_progress) {
1226 struct mmc_request *mrq = host->mrq;
1227
1228 host->mrq = NULL;
1229 mmc_request_done(host->mmc, mrq);
1230 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001231}
1232
Per Forlin9782aff2011-07-01 18:55:23 +02001233static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1234 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001235 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001236 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001237{
1238 int dma_len;
1239
1240 if (!next && data->host_cookie &&
1241 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301242 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001243 " host->next_data.cookie %d\n",
1244 __func__, data->host_cookie, host->next_data.cookie);
1245 data->host_cookie = 0;
1246 }
1247
1248 /* Check if next job is already prepared */
1249 if (next ||
1250 (!next && data->host_cookie != host->next_data.cookie)) {
Russell King26b88522012-04-13 12:27:37 +01001251 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001252 omap_hsmmc_get_dma_dir(host, data));
1253
1254 } else {
1255 dma_len = host->next_data.dma_len;
1256 host->next_data.dma_len = 0;
1257 }
1258
1259
1260 if (dma_len == 0)
1261 return -EINVAL;
1262
1263 if (next) {
1264 next->dma_len = dma_len;
1265 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1266 } else
1267 host->dma_len = dma_len;
1268
1269 return 0;
1270}
1271
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001272/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001273 * Routine to configure and start DMA for the MMC card
1274 */
Denis Karpov70a33412009-09-22 16:44:59 -07001275static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1276 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001277{
Russell King26b88522012-04-13 12:27:37 +01001278 struct dma_slave_config cfg;
1279 struct dma_async_tx_descriptor *tx;
1280 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001281 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001282 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001283
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001284 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001285 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001286 struct scatterlist *sgl;
1287
1288 sgl = data->sg + i;
1289 if (sgl->length % data->blksz)
1290 return -EINVAL;
1291 }
1292 if ((data->blksz % 4) != 0)
1293 /* REVISIT: The MMC buffer increments only when MSB is written.
1294 * Return error for blksz which is non multiple of four.
1295 */
1296 return -EINVAL;
1297
Adrian Hunterb4175772010-05-26 14:42:06 -07001298 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001299
Russell Kingc5c98922012-04-13 12:14:39 +01001300 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001301
Russell King26b88522012-04-13 12:27:37 +01001302 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1303 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1304 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1305 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1306 cfg.src_maxburst = data->blksz / 4;
1307 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001308
Russell King26b88522012-04-13 12:27:37 +01001309 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001310 if (ret)
1311 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001312
Russell King26b88522012-04-13 12:27:37 +01001313 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1314 if (ret)
1315 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001316
Russell King26b88522012-04-13 12:27:37 +01001317 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1318 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1319 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1320 if (!tx) {
1321 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1322 /* FIXME: cleanup */
1323 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001324 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001325
Russell King26b88522012-04-13 12:27:37 +01001326 tx->callback = omap_hsmmc_dma_callback;
1327 tx->callback_param = host;
1328
1329 /* Does not fail */
1330 dmaengine_submit(tx);
1331
1332 host->dma_ch = 1;
1333
1334 dma_async_issue_pending(chan);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001335
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001336 return 0;
1337}
1338
Denis Karpov70a33412009-09-22 16:44:59 -07001339static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001340 unsigned int timeout_ns,
1341 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001342{
1343 unsigned int timeout, cycle_ns;
1344 uint32_t reg, clkd, dto = 0;
1345
1346 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1347 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1348 if (clkd == 0)
1349 clkd = 1;
1350
1351 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001352 timeout = timeout_ns / cycle_ns;
1353 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001354 if (timeout) {
1355 while ((timeout & 0x80000000) == 0) {
1356 dto += 1;
1357 timeout <<= 1;
1358 }
1359 dto = 31 - dto;
1360 timeout <<= 1;
1361 if (timeout && dto)
1362 dto += 1;
1363 if (dto >= 13)
1364 dto -= 13;
1365 else
1366 dto = 0;
1367 if (dto > 14)
1368 dto = 14;
1369 }
1370
1371 reg &= ~DTO_MASK;
1372 reg |= dto << DTO_SHIFT;
1373 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1374}
1375
1376/*
1377 * Configure block length for MMC/SD cards and initiate the transfer.
1378 */
1379static int
Denis Karpov70a33412009-09-22 16:44:59 -07001380omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001381{
1382 int ret;
1383 host->data = req->data;
1384
1385 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001386 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001387 /*
1388 * Set an arbitrary 100ms data timeout for commands with
1389 * busy signal.
1390 */
1391 if (req->cmd->flags & MMC_RSP_BUSY)
1392 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001393 return 0;
1394 }
1395
1396 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1397 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001398 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001399
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001400 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001401 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001402 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301403 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001404 return ret;
1405 }
1406 }
1407 return 0;
1408}
1409
Per Forlin9782aff2011-07-01 18:55:23 +02001410static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1411 int err)
1412{
1413 struct omap_hsmmc_host *host = mmc_priv(mmc);
1414 struct mmc_data *data = mrq->data;
1415
Russell King26b88522012-04-13 12:27:37 +01001416 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001417 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001418
Russell King26b88522012-04-13 12:27:37 +01001419 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1420 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001421 data->host_cookie = 0;
1422 }
1423}
1424
1425static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1426 bool is_first_req)
1427{
1428 struct omap_hsmmc_host *host = mmc_priv(mmc);
1429
1430 if (mrq->data->host_cookie) {
1431 mrq->data->host_cookie = 0;
1432 return ;
1433 }
1434
Russell Kingc5c98922012-04-13 12:14:39 +01001435 if (host->use_dma) {
1436 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001437
Per Forlin9782aff2011-07-01 18:55:23 +02001438 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001439 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001440 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001441 }
Per Forlin9782aff2011-07-01 18:55:23 +02001442}
1443
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001444/*
1445 * Request function. for read/write operation
1446 */
Denis Karpov70a33412009-09-22 16:44:59 -07001447static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001448{
Denis Karpov70a33412009-09-22 16:44:59 -07001449 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001450 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001451
Adrian Hunterb4175772010-05-26 14:42:06 -07001452 BUG_ON(host->req_in_progress);
1453 BUG_ON(host->dma_ch != -1);
1454 if (host->protect_card) {
1455 if (host->reqs_blocked < 3) {
1456 /*
1457 * Ensure the controller is left in a consistent
1458 * state by resetting the command and data state
1459 * machines.
1460 */
1461 omap_hsmmc_reset_controller_fsm(host, SRD);
1462 omap_hsmmc_reset_controller_fsm(host, SRC);
1463 host->reqs_blocked += 1;
1464 }
1465 req->cmd->error = -EBADF;
1466 if (req->data)
1467 req->data->error = -EBADF;
1468 req->cmd->retries = 0;
1469 mmc_request_done(mmc, req);
1470 return;
1471 } else if (host->reqs_blocked)
1472 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001473 WARN_ON(host->mrq != NULL);
1474 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001475 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001476 if (err) {
1477 req->cmd->error = err;
1478 if (req->data)
1479 req->data->error = err;
1480 host->mrq = NULL;
1481 mmc_request_done(mmc, req);
1482 return;
1483 }
1484
Denis Karpov70a33412009-09-22 16:44:59 -07001485 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001486}
1487
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001488/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001489static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001490{
Denis Karpov70a33412009-09-22 16:44:59 -07001491 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001492 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001493
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301494 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001495
Adrian Huntera3621462009-09-22 16:44:42 -07001496 if (ios->power_mode != host->power_mode) {
1497 switch (ios->power_mode) {
1498 case MMC_POWER_OFF:
1499 mmc_slot(host).set_power(host->dev, host->slot_id,
1500 0, 0);
1501 break;
1502 case MMC_POWER_UP:
1503 mmc_slot(host).set_power(host->dev, host->slot_id,
1504 1, ios->vdd);
1505 break;
1506 case MMC_POWER_ON:
1507 do_send_init_stream = 1;
1508 break;
1509 }
1510 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001511 }
1512
Denis Karpovdd498ef2009-09-22 16:44:49 -07001513 /* FIXME: set registers based only on changes to ios */
1514
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001515 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001516
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301517 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001518 /* Only MMC1 can interface at 3V without some flavor
1519 * of external transceiver; but they all handle 1.8V.
1520 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001521 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Rajendra Nayak1f84b712012-03-12 20:32:38 +05301522 (ios->vdd == DUAL_VOLT_OCR_BIT) &&
1523 /*
1524 * With pbias cell programming missing, this
Tony Lindgrencf5ae402013-05-10 17:42:33 +05301525 * can't be allowed on MMC1 when booting with device
Rajendra Nayak1f84b712012-03-12 20:32:38 +05301526 * tree.
1527 */
Tony Lindgrencf5ae402013-05-10 17:42:33 +05301528 !host->pbias_disable) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001529 /*
1530 * The mmc_select_voltage fn of the core does
1531 * not seem to set the power_mode to
1532 * MMC_POWER_UP upon recalculating the voltage.
1533 * vdd 1.8v.
1534 */
Denis Karpov70a33412009-09-22 16:44:59 -07001535 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1536 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001537 "Switch operation failed\n");
1538 }
1539 }
1540
Andy Shevchenko5934df22011-05-06 12:14:06 +03001541 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001542
Adrian Huntera3621462009-09-22 16:44:42 -07001543 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001544 send_init_stream(host);
1545
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001546 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001547
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301548 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001549}
1550
1551static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1552{
Denis Karpov70a33412009-09-22 16:44:59 -07001553 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001554
Denis Karpov191d1f12009-09-22 16:44:55 -07001555 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001556 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001557 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001558}
1559
1560static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1561{
Denis Karpov70a33412009-09-22 16:44:59 -07001562 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001563
Denis Karpov191d1f12009-09-22 16:44:55 -07001564 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001565 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001566 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001567}
1568
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001569static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1570{
1571 struct omap_hsmmc_host *host = mmc_priv(mmc);
1572
1573 if (mmc_slot(host).init_card)
1574 mmc_slot(host).init_card(card);
1575}
1576
Denis Karpov70a33412009-09-22 16:44:59 -07001577static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001578{
1579 u32 hctl, capa, value;
1580
1581 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301582 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001583 hctl = SDVS30;
1584 capa = VS30 | VS18;
1585 } else {
1586 hctl = SDVS18;
1587 capa = VS18;
1588 }
1589
1590 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1591 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1592
1593 value = OMAP_HSMMC_READ(host->base, CAPA);
1594 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1595
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001596 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001597 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001598}
1599
Denis Karpov70a33412009-09-22 16:44:59 -07001600static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001601{
Denis Karpov70a33412009-09-22 16:44:59 -07001602 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001603
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301604 pm_runtime_get_sync(host->dev);
1605
Denis Karpovdd498ef2009-09-22 16:44:49 -07001606 return 0;
1607}
1608
Adrian Hunter907d2e72012-02-29 09:17:21 +02001609static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001610{
Denis Karpov70a33412009-09-22 16:44:59 -07001611 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001612
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301613 pm_runtime_mark_last_busy(host->dev);
1614 pm_runtime_put_autosuspend(host->dev);
1615
Denis Karpovdd498ef2009-09-22 16:44:49 -07001616 return 0;
1617}
1618
Denis Karpov70a33412009-09-22 16:44:59 -07001619static const struct mmc_host_ops omap_hsmmc_ops = {
1620 .enable = omap_hsmmc_enable_fclk,
1621 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001622 .post_req = omap_hsmmc_post_req,
1623 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001624 .request = omap_hsmmc_request,
1625 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001626 .get_cd = omap_hsmmc_get_cd,
1627 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001628 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001629 /* NYET -- enable_sdio_irq */
1630};
1631
Denis Karpovd900f712009-09-22 16:44:38 -07001632#ifdef CONFIG_DEBUG_FS
1633
Denis Karpov70a33412009-09-22 16:44:59 -07001634static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001635{
1636 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001637 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001638 int context_loss = 0;
1639
Denis Karpov70a33412009-09-22 16:44:59 -07001640 if (host->pdata->get_context_loss_count)
1641 context_loss = host->pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001642
Adrian Hunter907d2e72012-02-29 09:17:21 +02001643 seq_printf(s, "mmc%d:\n ctx_loss:\t%d:%d\n\nregs:\n",
1644 mmc->index, host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001645
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301646 if (host->suspended) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001647 seq_printf(s, "host suspended, can't read registers\n");
1648 return 0;
1649 }
1650
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301651 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001652
Denis Karpovd900f712009-09-22 16:44:38 -07001653 seq_printf(s, "CON:\t\t0x%08x\n",
1654 OMAP_HSMMC_READ(host->base, CON));
1655 seq_printf(s, "HCTL:\t\t0x%08x\n",
1656 OMAP_HSMMC_READ(host->base, HCTL));
1657 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1658 OMAP_HSMMC_READ(host->base, SYSCTL));
1659 seq_printf(s, "IE:\t\t0x%08x\n",
1660 OMAP_HSMMC_READ(host->base, IE));
1661 seq_printf(s, "ISE:\t\t0x%08x\n",
1662 OMAP_HSMMC_READ(host->base, ISE));
1663 seq_printf(s, "CAPA:\t\t0x%08x\n",
1664 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001665
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301666 pm_runtime_mark_last_busy(host->dev);
1667 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001668
Denis Karpovd900f712009-09-22 16:44:38 -07001669 return 0;
1670}
1671
Denis Karpov70a33412009-09-22 16:44:59 -07001672static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001673{
Denis Karpov70a33412009-09-22 16:44:59 -07001674 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001675}
1676
1677static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001678 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001679 .read = seq_read,
1680 .llseek = seq_lseek,
1681 .release = single_release,
1682};
1683
Denis Karpov70a33412009-09-22 16:44:59 -07001684static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001685{
1686 if (mmc->debugfs_root)
1687 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1688 mmc, &mmc_regs_fops);
1689}
1690
1691#else
1692
Denis Karpov70a33412009-09-22 16:44:59 -07001693static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001694{
1695}
1696
1697#endif
1698
Rajendra Nayak46856a62012-03-12 20:32:37 +05301699#ifdef CONFIG_OF
1700static u16 omap4_reg_offset = 0x100;
1701
1702static const struct of_device_id omap_mmc_of_match[] = {
1703 {
1704 .compatible = "ti,omap2-hsmmc",
1705 },
1706 {
1707 .compatible = "ti,omap3-hsmmc",
1708 },
1709 {
1710 .compatible = "ti,omap4-hsmmc",
1711 .data = &omap4_reg_offset,
1712 },
1713 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001714};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301715MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1716
1717static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1718{
1719 struct omap_mmc_platform_data *pdata;
1720 struct device_node *np = dev->of_node;
Daniel Mackd8714e82012-10-15 21:35:06 +05301721 u32 bus_width, max_freq;
Jan Luebbedc642c22013-01-30 10:07:17 +01001722 int cd_gpio, wp_gpio;
1723
1724 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
1725 wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1726 if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
1727 return ERR_PTR(-EPROBE_DEFER);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301728
1729 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1730 if (!pdata)
1731 return NULL; /* out of memory */
1732
1733 if (of_find_property(np, "ti,dual-volt", NULL))
1734 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1735
1736 /* This driver only supports 1 slot */
1737 pdata->nr_slots = 1;
Jan Luebbedc642c22013-01-30 10:07:17 +01001738 pdata->slots[0].switch_pin = cd_gpio;
1739 pdata->slots[0].gpio_wp = wp_gpio;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301740
1741 if (of_find_property(np, "ti,non-removable", NULL)) {
1742 pdata->slots[0].nonremovable = true;
1743 pdata->slots[0].no_regulator_off_init = true;
1744 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001745 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301746 if (bus_width == 4)
1747 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1748 else if (bus_width == 8)
1749 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1750
1751 if (of_find_property(np, "ti,needs-special-reset", NULL))
1752 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1753
Daniel Mackd8714e82012-10-15 21:35:06 +05301754 if (!of_property_read_u32(np, "max-frequency", &max_freq))
1755 pdata->max_freq = max_freq;
1756
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301757 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
1758 pdata->slots[0].features |= HSMMC_HAS_HSPE_SUPPORT;
1759
Rajendra Nayak46856a62012-03-12 20:32:37 +05301760 return pdata;
1761}
1762#else
1763static inline struct omap_mmc_platform_data
1764 *of_get_hsmmc_pdata(struct device *dev)
1765{
1766 return NULL;
1767}
1768#endif
1769
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001770static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001771{
1772 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1773 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001774 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001775 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001776 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301777 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001778 dma_cap_mask_t mask;
1779 unsigned tx_req, rx_req;
Daniel Mack46b76032012-10-15 21:35:05 +05301780 struct pinctrl *pinctrl;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301781
1782 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1783 if (match) {
1784 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01001785
1786 if (IS_ERR(pdata))
1787 return PTR_ERR(pdata);
1788
Rajendra Nayak46856a62012-03-12 20:32:37 +05301789 if (match->data) {
Uwe Kleine-Königefc9b732012-05-21 21:57:39 +02001790 const u16 *offsetp = match->data;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301791 pdata->reg_offset = *offsetp;
1792 }
1793 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001794
1795 if (pdata == NULL) {
1796 dev_err(&pdev->dev, "Platform Data is missing\n");
1797 return -ENXIO;
1798 }
1799
1800 if (pdata->nr_slots == 0) {
1801 dev_err(&pdev->dev, "No Slots\n");
1802 return -ENXIO;
1803 }
1804
1805 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1806 irq = platform_get_irq(pdev, 0);
1807 if (res == NULL || irq < 0)
1808 return -ENXIO;
1809
Chris Ball984b2032011-03-22 16:34:42 -07001810 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001811 if (res == NULL)
1812 return -EBUSY;
1813
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001814 ret = omap_hsmmc_gpio_init(pdata);
1815 if (ret)
1816 goto err;
1817
Denis Karpov70a33412009-09-22 16:44:59 -07001818 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001819 if (!mmc) {
1820 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001821 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001822 }
1823
1824 host = mmc_priv(mmc);
1825 host->mmc = mmc;
1826 host->pdata = pdata;
1827 host->dev = &pdev->dev;
1828 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001829 host->dma_ch = -1;
1830 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001831 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05301832 host->mapbase = res->start + pdata->reg_offset;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001833 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001834 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001835 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001836
1837 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001838
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301839 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001840
Adrian Huntere0eb2422010-02-15 10:03:34 -08001841 /*
1842 * If regulator_disable can only put vcc_aux to sleep then there is
1843 * no off state.
1844 */
1845 if (mmc_slot(host).vcc_aux_disable_is_sleep)
1846 mmc_slot(host).no_off = 1;
1847
Daniel Mackd418ed82012-02-19 13:20:33 +01001848 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1849
1850 if (pdata->max_freq > 0)
1851 mmc->f_max = pdata->max_freq;
1852 else
1853 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001854
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001855 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001856
Russell King6f7607c2009-01-28 10:22:50 +00001857 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001858 if (IS_ERR(host->fclk)) {
1859 ret = PTR_ERR(host->fclk);
1860 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001861 goto err1;
1862 }
1863
Paul Walmsley9b682562011-10-06 14:50:35 -06001864 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1865 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1866 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1867 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001868
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301869 pm_runtime_enable(host->dev);
1870 pm_runtime_get_sync(host->dev);
1871 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1872 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001873
Balaji T K92a3aeb2012-02-24 21:14:34 +05301874 omap_hsmmc_context_save(host);
1875
Tony Lindgrencf5ae402013-05-10 17:42:33 +05301876 /* This can be removed once we support PBIAS with DT */
Balaji T Ke0022642013-10-21 00:25:18 +05301877 if (host->dev->of_node && res->start == 0x4809c000)
Tony Lindgrencf5ae402013-05-10 17:42:33 +05301878 host->pbias_disable = 1;
1879
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301880 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1881 /*
1882 * MMC can still work without debounce clock.
1883 */
1884 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301885 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05301886 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301887 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
1888 clk_put(host->dbclk);
1889 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07001890 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001891
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001892 /* Since we do only SG emulation, we can have as many segs
1893 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001894 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001895
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001896 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1897 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1898 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1899 mmc->max_seg_size = mmc->max_req_size;
1900
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001901 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001902 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001903
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001904 mmc->caps |= mmc_slot(host).caps;
1905 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001906 mmc->caps |= MMC_CAP_4_BIT_DATA;
1907
Denis Karpov191d1f12009-09-22 16:44:55 -07001908 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001909 mmc->caps |= MMC_CAP_NONREMOVABLE;
1910
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001911 mmc->pm_caps = mmc_slot(host).pm_caps;
1912
Denis Karpov70a33412009-09-22 16:44:59 -07001913 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001914
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05301915 if (!pdev->dev.of_node) {
1916 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1917 if (!res) {
1918 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
1919 ret = -ENXIO;
1920 goto err_irq;
1921 }
1922 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001923
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05301924 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1925 if (!res) {
1926 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
1927 ret = -ENXIO;
1928 goto err_irq;
1929 }
1930 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001931 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001932
Russell King26b88522012-04-13 12:27:37 +01001933 dma_cap_zero(mask);
1934 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01001935
Matt Porterd272fbf2013-05-10 17:42:34 +05301936 host->rx_chan =
1937 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
1938 &rx_req, &pdev->dev, "rx");
1939
Russell King26b88522012-04-13 12:27:37 +01001940 if (!host->rx_chan) {
1941 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001942 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001943 goto err_irq;
1944 }
1945
Matt Porterd272fbf2013-05-10 17:42:34 +05301946 host->tx_chan =
1947 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
1948 &tx_req, &pdev->dev, "tx");
1949
Russell King26b88522012-04-13 12:27:37 +01001950 if (!host->tx_chan) {
1951 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001952 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001953 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01001954 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001955
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001956 /* Request IRQ for MMC operations */
Yong Zhangd9618e92011-09-22 16:59:04 +08001957 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001958 mmc_hostname(mmc), host);
1959 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301960 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001961 goto err_irq;
1962 }
1963
1964 if (pdata->init != NULL) {
1965 if (pdata->init(&pdev->dev) != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301966 dev_err(mmc_dev(host->mmc),
Denis Karpov70a33412009-09-22 16:44:59 -07001967 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001968 goto err_irq_cd_init;
1969 }
1970 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001971
Adrian Hunterb702b102010-02-15 10:03:35 -08001972 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001973 ret = omap_hsmmc_reg_get(host);
1974 if (ret)
1975 goto err_reg;
1976 host->use_reg = 1;
1977 }
1978
David Brownellb583f262009-05-28 14:04:03 -07001979 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001980
1981 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001982 if ((mmc_slot(host).card_detect_irq)) {
NeilBrown7efab4f2011-12-30 12:35:13 +11001983 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1984 NULL,
1985 omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08001986 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11001987 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001988 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301989 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001990 "Unable to grab MMC CD IRQ\n");
1991 goto err_irq_cd;
1992 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00001993 pdata->suspend = omap_hsmmc_suspend_cdirq;
1994 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001995 }
1996
Adrian Hunterb4175772010-05-26 14:42:06 -07001997 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001998
Daniel Mack46b76032012-10-15 21:35:05 +05301999 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
2000 if (IS_ERR(pinctrl))
2001 dev_warn(&pdev->dev,
2002 "pins are not configured from the driver\n");
2003
Adrian Hunterb62f6222009-09-22 16:45:01 -07002004 omap_hsmmc_protect_card(host);
2005
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002006 mmc_add_host(mmc);
2007
Denis Karpov191d1f12009-09-22 16:44:55 -07002008 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002009 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2010 if (ret < 0)
2011 goto err_slot_name;
2012 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002013 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002014 ret = device_create_file(&mmc->class_dev,
2015 &dev_attr_cover_switch);
2016 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002017 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002018 }
2019
Denis Karpov70a33412009-09-22 16:44:59 -07002020 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302021 pm_runtime_mark_last_busy(host->dev);
2022 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002023
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002024 return 0;
2025
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002026err_slot_name:
2027 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002028 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002029err_irq_cd:
2030 if (host->use_reg)
2031 omap_hsmmc_reg_put(host);
2032err_reg:
2033 if (host->pdata->cleanup)
2034 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002035err_irq_cd_init:
2036 free_irq(host->irq, host);
2037err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002038 if (host->tx_chan)
2039 dma_release_channel(host->tx_chan);
2040 if (host->rx_chan)
2041 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302042 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002043 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002044 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302045 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302046 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002047 clk_put(host->dbclk);
2048 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002049err1:
2050 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002051 mmc_free_host(mmc);
2052err_alloc:
2053 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002054err:
Russell King48b332f2012-04-18 11:11:57 +01002055 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2056 if (res)
2057 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002058 return ret;
2059}
2060
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002061static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002062{
Denis Karpov70a33412009-09-22 16:44:59 -07002063 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002064 struct resource *res;
2065
Felipe Balbi927ce942012-03-14 11:18:27 +02002066 pm_runtime_get_sync(host->dev);
2067 mmc_remove_host(host->mmc);
2068 if (host->use_reg)
2069 omap_hsmmc_reg_put(host);
2070 if (host->pdata->cleanup)
2071 host->pdata->cleanup(&pdev->dev);
2072 free_irq(host->irq, host);
2073 if (mmc_slot(host).card_detect_irq)
2074 free_irq(mmc_slot(host).card_detect_irq, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002075
Russell Kingc5c98922012-04-13 12:14:39 +01002076 if (host->tx_chan)
2077 dma_release_channel(host->tx_chan);
2078 if (host->rx_chan)
2079 dma_release_channel(host->rx_chan);
2080
Felipe Balbi927ce942012-03-14 11:18:27 +02002081 pm_runtime_put_sync(host->dev);
2082 pm_runtime_disable(host->dev);
2083 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302084 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302085 clk_disable_unprepare(host->dbclk);
Felipe Balbi927ce942012-03-14 11:18:27 +02002086 clk_put(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002087 }
2088
Balaji T K9ea28ec2012-10-15 21:35:08 +05302089 omap_hsmmc_gpio_free(host->pdata);
Felipe Balbi927ce942012-03-14 11:18:27 +02002090 iounmap(host->base);
Balaji T K9d1f0282012-10-15 21:35:07 +05302091 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002092
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002093 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2094 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002095 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002096
2097 return 0;
2098}
2099
2100#ifdef CONFIG_PM
Felipe Balbia48ce882012-11-19 21:59:59 +05302101static int omap_hsmmc_prepare(struct device *dev)
2102{
2103 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2104
2105 if (host->pdata->suspend)
2106 return host->pdata->suspend(dev, host->slot_id);
2107
2108 return 0;
2109}
2110
2111static void omap_hsmmc_complete(struct device *dev)
2112{
2113 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2114
2115 if (host->pdata->resume)
2116 host->pdata->resume(dev, host->slot_id);
2117
2118}
2119
Kevin Hilmana791daa2010-05-26 14:42:07 -07002120static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002121{
2122 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002123 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2124
2125 if (!host)
2126 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002127
2128 if (host && host->suspended)
2129 return 0;
2130
Felipe Balbi927ce942012-03-14 11:18:27 +02002131 pm_runtime_get_sync(host->dev);
2132 host->suspended = 1;
Felipe Balbi927ce942012-03-14 11:18:27 +02002133 ret = mmc_suspend_host(host->mmc);
2134
2135 if (ret) {
2136 host->suspended = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002137 goto err;
2138 }
2139
2140 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2141 omap_hsmmc_disable_irq(host);
2142 OMAP_HSMMC_WRITE(host->base, HCTL,
2143 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2144 }
2145
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302146 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302147 clk_disable_unprepare(host->dbclk);
Eliad Peller31f9d462011-11-22 16:02:17 +02002148err:
2149 pm_runtime_put_sync(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002150 return ret;
2151}
2152
2153/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002154static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002155{
2156 int ret = 0;
Felipe Balbi927ce942012-03-14 11:18:27 +02002157 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2158
2159 if (!host)
2160 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002161
2162 if (host && !host->suspended)
2163 return 0;
2164
Felipe Balbi927ce942012-03-14 11:18:27 +02002165 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002166
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302167 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302168 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002169
Felipe Balbi927ce942012-03-14 11:18:27 +02002170 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2171 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002172
Felipe Balbi927ce942012-03-14 11:18:27 +02002173 omap_hsmmc_protect_card(host);
2174
2175 /* Notify the core to resume the host */
2176 ret = mmc_resume_host(host->mmc);
2177 if (ret == 0)
2178 host->suspended = 0;
2179
2180 pm_runtime_mark_last_busy(host->dev);
2181 pm_runtime_put_autosuspend(host->dev);
2182
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002183 return ret;
2184
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002185}
2186
2187#else
Felipe Balbia48ce882012-11-19 21:59:59 +05302188#define omap_hsmmc_prepare NULL
2189#define omap_hsmmc_complete NULL
Denis Karpov70a33412009-09-22 16:44:59 -07002190#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302191#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002192#endif
2193
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302194static int omap_hsmmc_runtime_suspend(struct device *dev)
2195{
2196 struct omap_hsmmc_host *host;
2197
2198 host = platform_get_drvdata(to_platform_device(dev));
2199 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002200 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302201
2202 return 0;
2203}
2204
2205static int omap_hsmmc_runtime_resume(struct device *dev)
2206{
2207 struct omap_hsmmc_host *host;
2208
2209 host = platform_get_drvdata(to_platform_device(dev));
2210 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002211 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302212
2213 return 0;
2214}
2215
Kevin Hilmana791daa2010-05-26 14:42:07 -07002216static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002217 .suspend = omap_hsmmc_suspend,
2218 .resume = omap_hsmmc_resume,
Felipe Balbia48ce882012-11-19 21:59:59 +05302219 .prepare = omap_hsmmc_prepare,
2220 .complete = omap_hsmmc_complete,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302221 .runtime_suspend = omap_hsmmc_runtime_suspend,
2222 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002223};
2224
2225static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002226 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002227 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002228 .driver = {
2229 .name = DRIVER_NAME,
2230 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002231 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302232 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002233 },
2234};
2235
Felipe Balbib7964502012-03-14 11:18:32 +02002236module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002237MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2238MODULE_LICENSE("GPL");
2239MODULE_ALIAS("platform:" DRIVER_NAME);
2240MODULE_AUTHOR("Texas Instruments Inc");