blob: 6b7b75585926c44d2fc39fdda4f0d58e73ec392a [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>
Balaji T Kee526d52014-05-09 22:16:53 +053034#include <linux/omap-dmaengine.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
Balaji T Ka2e77152014-01-21 19:54:42 +053048#define OMAP_HSMMC_SDMASA 0x0100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010049#define OMAP_HSMMC_BLK 0x0104
50#define OMAP_HSMMC_ARG 0x0108
51#define OMAP_HSMMC_CMD 0x010C
52#define OMAP_HSMMC_RSP10 0x0110
53#define OMAP_HSMMC_RSP32 0x0114
54#define OMAP_HSMMC_RSP54 0x0118
55#define OMAP_HSMMC_RSP76 0x011C
56#define OMAP_HSMMC_DATA 0x0120
57#define OMAP_HSMMC_HCTL 0x0128
58#define OMAP_HSMMC_SYSCTL 0x012C
59#define OMAP_HSMMC_STAT 0x0130
60#define OMAP_HSMMC_IE 0x0134
61#define OMAP_HSMMC_ISE 0x0138
Balaji T Ka2e77152014-01-21 19:54:42 +053062#define OMAP_HSMMC_AC12 0x013C
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010063#define OMAP_HSMMC_CAPA 0x0140
64
65#define VS18 (1 << 26)
66#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053067#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010068#define SDVS18 (0x5 << 9)
69#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080070#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010071#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010072#define SDVSCLR 0xFFFFF1FF
73#define SDVSDET 0x00000400
74#define AUTOIDLE 0x1
75#define SDBP (1 << 8)
76#define DTO 0xe
77#define ICE 0x1
78#define ICS 0x2
79#define CEN (1 << 2)
Balaji T Ked164182013-10-21 00:25:21 +053080#define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010081#define CLKD_MASK 0x0000FFC0
82#define CLKD_SHIFT 6
83#define DTO_MASK 0x000F0000
84#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010085#define INIT_STREAM (1 << 1)
Balaji T Ka2e77152014-01-21 19:54:42 +053086#define ACEN_ACMD23 (2 << 2)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010087#define DP_SELECT (1 << 21)
88#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053089#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010090#define MSBS (1 << 5)
91#define BCE (1 << 1)
92#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053093#define HSPE (1 << 2)
Balaji T K03b5d922012-04-09 12:08:33 +053094#define DDR (1 << 19)
Jarkko Lavinen73153012008-11-21 16:49:54 +020095#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010096#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010097#define STAT_CLEAR 0xFFFFFFFF
98#define INIT_STREAM_CMD 0x00000000
99#define DUAL_VOLT_OCR_BIT 7
100#define SRC (1 << 25)
101#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700102#define SOFTRESET (1 << 1)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100103
Venkatraman Sa7e96872012-11-19 22:00:01 +0530104/* Interrupt masks for IE and ISE register */
105#define CC_EN (1 << 0)
106#define TC_EN (1 << 1)
107#define BWR_EN (1 << 4)
108#define BRR_EN (1 << 5)
109#define ERR_EN (1 << 15)
110#define CTO_EN (1 << 16)
111#define CCRC_EN (1 << 17)
112#define CEB_EN (1 << 18)
113#define CIE_EN (1 << 19)
114#define DTO_EN (1 << 20)
115#define DCRC_EN (1 << 21)
116#define DEB_EN (1 << 22)
Balaji T Ka2e77152014-01-21 19:54:42 +0530117#define ACE_EN (1 << 24)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530118#define CERR_EN (1 << 28)
119#define BADA_EN (1 << 29)
120
Balaji T Ka2e77152014-01-21 19:54:42 +0530121#define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
Venkatraman Sa7e96872012-11-19 22:00:01 +0530122 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
123 BRR_EN | BWR_EN | TC_EN | CC_EN)
124
Balaji T Ka2e77152014-01-21 19:54:42 +0530125#define CNI (1 << 7)
126#define ACIE (1 << 4)
127#define ACEB (1 << 3)
128#define ACCE (1 << 2)
129#define ACTO (1 << 1)
130#define ACNE (1 << 0)
131
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530132#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530133#define MMC_TIMEOUT_MS 20 /* 20 mSec */
134#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400135#define OMAP_MMC_MIN_CLOCK 400000
136#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530137#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100138
Balaji T Ke99448f2014-02-19 20:26:40 +0530139#define VDD_1V8 1800000 /* 180000 uV */
140#define VDD_3V0 3000000 /* 300000 uV */
141#define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
142
Balaji T Ka2e77152014-01-21 19:54:42 +0530143#define AUTO_CMD23 (1 << 1) /* Auto CMD23 support */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100144/*
145 * One controller can have multiple slots, like on some omap boards using
146 * omap.c controller driver. Luckily this is not currently done on any known
147 * omap_hsmmc.c device.
148 */
149#define mmc_slot(host) (host->pdata->slots[host->slot_id])
150
151/*
152 * MMC Host controller read/write API's
153 */
154#define OMAP_HSMMC_READ(base, reg) \
155 __raw_readl((base) + OMAP_HSMMC_##reg)
156
157#define OMAP_HSMMC_WRITE(base, reg, val) \
158 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
159
Per Forlin9782aff2011-07-01 18:55:23 +0200160struct omap_hsmmc_next {
161 unsigned int dma_len;
162 s32 cookie;
163};
164
Denis Karpov70a33412009-09-22 16:44:59 -0700165struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100166 struct device *dev;
167 struct mmc_host *mmc;
168 struct mmc_request *mrq;
169 struct mmc_command *cmd;
170 struct mmc_data *data;
171 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100172 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800173 /*
174 * vcc == configured supply
175 * vcc_aux == optional
176 * - MMC1, supply for DAT4..DAT7
177 * - MMC2/MMC2, external level shifter voltage supply, for
178 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
179 */
180 struct regulator *vcc;
181 struct regulator *vcc_aux;
Balaji T Ke99448f2014-02-19 20:26:40 +0530182 struct regulator *pbias;
183 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100184 void __iomem *base;
185 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700186 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100187 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200188 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100189 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700190 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100191 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530192 u32 con;
193 u32 hctl;
194 u32 sysctl;
195 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100196 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100197 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100198 struct dma_chan *tx_chan;
199 struct dma_chan *rx_chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100200 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200201 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700202 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700203 int protect_card;
204 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800205 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700206 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530207 unsigned long clk_rate;
Balaji T Ka2e77152014-01-21 19:54:42 +0530208 unsigned int flags;
Per Forlin9782aff2011-07-01 18:55:23 +0200209 struct omap_hsmmc_next next_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100210 struct omap_mmc_platform_data *pdata;
211};
212
Nishanth Menon59445b12014-02-13 23:45:48 -0600213struct omap_mmc_of_data {
214 u32 reg_offset;
215 u8 controller_flags;
216};
217
Balaji T Kbf129e12014-01-21 19:54:42 +0530218static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
219
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800220static int omap_hsmmc_card_detect(struct device *dev, int slot)
221{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530222 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
223 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800224
225 /* NOTE: assumes card detect signal is active-low */
226 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
227}
228
229static int omap_hsmmc_get_wp(struct device *dev, int slot)
230{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530231 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
232 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800233
234 /* NOTE: assumes write protect signal is active-high */
235 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
236}
237
238static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
239{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530240 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
241 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800242
243 /* NOTE: assumes card detect signal is active-low */
244 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
245}
246
247#ifdef CONFIG_PM
248
249static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
250{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530251 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
252 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800253
254 disable_irq(mmc->slots[0].card_detect_irq);
255 return 0;
256}
257
258static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
259{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530260 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
261 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800262
263 enable_irq(mmc->slots[0].card_detect_irq);
264 return 0;
265}
266
267#else
268
269#define omap_hsmmc_suspend_cdirq NULL
270#define omap_hsmmc_resume_cdirq NULL
271
272#endif
273
Adrian Hunterb702b102010-02-15 10:03:35 -0800274#ifdef CONFIG_REGULATOR
275
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500276static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800277 int vdd)
278{
279 struct omap_hsmmc_host *host =
280 platform_get_drvdata(to_platform_device(dev));
281 int ret = 0;
282
283 /*
284 * If we don't see a Vcc regulator, assume it's a fixed
285 * voltage always-on regulator.
286 */
287 if (!host->vcc)
288 return 0;
289
290 if (mmc_slot(host).before_set_reg)
291 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
292
Balaji T Ke99448f2014-02-19 20:26:40 +0530293 if (host->pbias) {
294 if (host->pbias_enabled == 1) {
295 ret = regulator_disable(host->pbias);
296 if (!ret)
297 host->pbias_enabled = 0;
298 }
299 regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
300 }
301
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800302 /*
303 * Assume Vcc regulator is used only to power the card ... OMAP
304 * VDDS is used to power the pins, optionally with a transceiver to
305 * support cards using voltages other than VDDS (1.8V nominal). When a
306 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
307 *
308 * In some cases this regulator won't support enable/disable;
309 * e.g. it's a fixed rail for a WLAN chip.
310 *
311 * In other cases vcc_aux switches interface power. Example, for
312 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
313 * chips/cards need an interface voltage rail too.
314 */
315 if (power_on) {
Balaji T K987fd492014-02-19 20:26:40 +0530316 if (host->vcc)
317 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800318 /* Enable interface voltage rail, if needed */
319 if (ret == 0 && host->vcc_aux) {
320 ret = regulator_enable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530321 if (ret < 0 && host->vcc)
Linus Walleij99fc5132010-09-29 01:08:27 -0400322 ret = mmc_regulator_set_ocr(host->mmc,
323 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800324 }
325 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400326 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800327 if (host->vcc_aux)
328 ret = regulator_disable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530329 if (host->vcc) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400330 /* Then proceed to shut down the local regulator */
331 ret = mmc_regulator_set_ocr(host->mmc,
332 host->vcc, 0);
333 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800334 }
335
Balaji T Ke99448f2014-02-19 20:26:40 +0530336 if (host->pbias) {
337 if (vdd <= VDD_165_195)
338 ret = regulator_set_voltage(host->pbias, VDD_1V8,
339 VDD_1V8);
340 else
341 ret = regulator_set_voltage(host->pbias, VDD_3V0,
342 VDD_3V0);
343 if (ret < 0)
344 goto error_set_power;
345
346 if (host->pbias_enabled == 0) {
347 ret = regulator_enable(host->pbias);
348 if (!ret)
349 host->pbias_enabled = 1;
350 }
351 }
352
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800353 if (mmc_slot(host).after_set_reg)
354 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
355
Balaji T Ke99448f2014-02-19 20:26:40 +0530356error_set_power:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800357 return ret;
358}
359
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800360static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
361{
362 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700363 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800364
Balaji T Kf2ddc1d2014-02-19 20:26:40 +0530365 reg = devm_regulator_get(host->dev, "vmmc");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800366 if (IS_ERR(reg)) {
Balaji T K987fd492014-02-19 20:26:40 +0530367 dev_err(host->dev, "unable to get vmmc regulator %ld\n",
368 PTR_ERR(reg));
NeilBrown1fdc90f2012-08-08 00:06:00 -0400369 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800370 } else {
371 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700372 ocr_value = mmc_regulator_get_ocrmask(reg);
373 if (!mmc_slot(host).ocr_mask) {
374 mmc_slot(host).ocr_mask = ocr_value;
375 } else {
376 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530377 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500378 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700379 mmc_slot(host).ocr_mask = 0;
380 return -EINVAL;
381 }
382 }
Balaji T K987fd492014-02-19 20:26:40 +0530383 }
384 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800385
Balaji T K987fd492014-02-19 20:26:40 +0530386 /* Allow an aux regulator */
387 reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
388 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800389
Balaji T Ke99448f2014-02-19 20:26:40 +0530390 reg = devm_regulator_get_optional(host->dev, "pbias");
391 host->pbias = IS_ERR(reg) ? NULL : reg;
392
Balaji T K987fd492014-02-19 20:26:40 +0530393 /* For eMMC do not power off when not in sleep state */
394 if (mmc_slot(host).no_regulator_off_init)
395 return 0;
396 /*
397 * To disable boot_on regulator, enable regulator
398 * to increase usecount and then disable it.
399 */
400 if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
401 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
402 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
Adrian Huntere840ce12011-05-06 12:14:10 +0300403
Balaji T K987fd492014-02-19 20:26:40 +0530404 mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
405 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800406 }
407
408 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800409}
410
411static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
412{
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800413 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800414}
415
Adrian Hunterb702b102010-02-15 10:03:35 -0800416static inline int omap_hsmmc_have_reg(void)
417{
418 return 1;
419}
420
421#else
422
423static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
424{
425 return -EINVAL;
426}
427
428static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
429{
430}
431
432static inline int omap_hsmmc_have_reg(void)
433{
434 return 0;
435}
436
437#endif
438
439static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
440{
441 int ret;
442
443 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800444 if (pdata->slots[0].cover)
445 pdata->slots[0].get_cover_state =
446 omap_hsmmc_get_cover_state;
447 else
448 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
449 pdata->slots[0].card_detect_irq =
450 gpio_to_irq(pdata->slots[0].switch_pin);
451 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
452 if (ret)
453 return ret;
454 ret = gpio_direction_input(pdata->slots[0].switch_pin);
455 if (ret)
456 goto err_free_sp;
457 } else
458 pdata->slots[0].switch_pin = -EINVAL;
459
460 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
461 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
462 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
463 if (ret)
464 goto err_free_cd;
465 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
466 if (ret)
467 goto err_free_wp;
468 } else
469 pdata->slots[0].gpio_wp = -EINVAL;
470
471 return 0;
472
473err_free_wp:
474 gpio_free(pdata->slots[0].gpio_wp);
475err_free_cd:
476 if (gpio_is_valid(pdata->slots[0].switch_pin))
477err_free_sp:
478 gpio_free(pdata->slots[0].switch_pin);
479 return ret;
480}
481
482static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
483{
484 if (gpio_is_valid(pdata->slots[0].gpio_wp))
485 gpio_free(pdata->slots[0].gpio_wp);
486 if (gpio_is_valid(pdata->slots[0].switch_pin))
487 gpio_free(pdata->slots[0].switch_pin);
488}
489
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100490/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300491 * Start clock to the card
492 */
493static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
494{
495 OMAP_HSMMC_WRITE(host->base, SYSCTL,
496 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
497}
498
499/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100500 * Stop clock to the card
501 */
Denis Karpov70a33412009-09-22 16:44:59 -0700502static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100503{
504 OMAP_HSMMC_WRITE(host->base, SYSCTL,
505 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
506 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900507 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100508}
509
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700510static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
511 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700512{
513 unsigned int irq_mask;
514
515 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530516 irq_mask = INT_EN_MASK & ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700517 else
518 irq_mask = INT_EN_MASK;
519
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700520 /* Disable timeout for erases */
521 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530522 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700523
Adrian Hunterb4175772010-05-26 14:42:06 -0700524 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
525 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
526 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
527}
528
529static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
530{
531 OMAP_HSMMC_WRITE(host->base, ISE, 0);
532 OMAP_HSMMC_WRITE(host->base, IE, 0);
533 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
534}
535
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300536/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530537static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300538{
539 u16 dsor = 0;
540
541 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530542 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530543 if (dsor > CLKD_MAX)
544 dsor = CLKD_MAX;
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300545 }
546
547 return dsor;
548}
549
Andy Shevchenko5934df22011-05-06 12:14:06 +0300550static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
551{
552 struct mmc_ios *ios = &host->mmc->ios;
553 unsigned long regval;
554 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530555 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300556
Venkatraman S8986d312012-08-07 19:10:38 +0530557 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300558
559 omap_hsmmc_stop_clock(host);
560
561 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
562 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530563 clkdiv = calc_divisor(host, ios);
564 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300565 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
566 OMAP_HSMMC_WRITE(host->base, SYSCTL,
567 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
568
569 /* Wait till the ICS bit is set */
570 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
571 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
572 && time_before(jiffies, timeout))
573 cpu_relax();
574
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530575 /*
576 * Enable High-Speed Support
577 * Pre-Requisites
578 * - Controller should support High-Speed-Enable Bit
579 * - Controller should not be using DDR Mode
580 * - Controller should advertise that it supports High Speed
581 * in capabilities register
582 * - MMC/SD clock coming out of controller > 25MHz
583 */
584 if ((mmc_slot(host).features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900585 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530586 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
587 regval = OMAP_HSMMC_READ(host->base, HCTL);
588 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
589 regval |= HSPE;
590 else
591 regval &= ~HSPE;
592
593 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
594 }
595
Andy Shevchenko5934df22011-05-06 12:14:06 +0300596 omap_hsmmc_start_clock(host);
597}
598
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400599static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
600{
601 struct mmc_ios *ios = &host->mmc->ios;
602 u32 con;
603
604 con = OMAP_HSMMC_READ(host->base, CON);
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900605 if (ios->timing == MMC_TIMING_MMC_DDR52)
Balaji T K03b5d922012-04-09 12:08:33 +0530606 con |= DDR; /* configure in DDR mode */
607 else
608 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400609 switch (ios->bus_width) {
610 case MMC_BUS_WIDTH_8:
611 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
612 break;
613 case MMC_BUS_WIDTH_4:
614 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
615 OMAP_HSMMC_WRITE(host->base, HCTL,
616 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
617 break;
618 case MMC_BUS_WIDTH_1:
619 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
620 OMAP_HSMMC_WRITE(host->base, HCTL,
621 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
622 break;
623 }
624}
625
626static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
627{
628 struct mmc_ios *ios = &host->mmc->ios;
629 u32 con;
630
631 con = OMAP_HSMMC_READ(host->base, CON);
632 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
633 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
634 else
635 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
636}
637
Denis Karpov11dd62a2009-09-22 16:44:43 -0700638#ifdef CONFIG_PM
639
640/*
641 * Restore the MMC host context, if it was lost as result of a
642 * power state change.
643 */
Denis Karpov70a33412009-09-22 16:44:59 -0700644static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700645{
646 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400647 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700648 unsigned long timeout;
649
Tony Lindgren0a82e062013-10-21 00:25:19 +0530650 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
651 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
652 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
653 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
654 return 0;
655
656 host->context_loss++;
657
Balaji T Kc2200ef2012-03-07 09:55:30 -0500658 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700659 if (host->power_mode != MMC_POWER_OFF &&
660 (1 << ios->vdd) <= MMC_VDD_23_24)
661 hctl = SDVS18;
662 else
663 hctl = SDVS30;
664 capa = VS30 | VS18;
665 } else {
666 hctl = SDVS18;
667 capa = VS18;
668 }
669
670 OMAP_HSMMC_WRITE(host->base, HCTL,
671 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
672
673 OMAP_HSMMC_WRITE(host->base, CAPA,
674 OMAP_HSMMC_READ(host->base, CAPA) | capa);
675
676 OMAP_HSMMC_WRITE(host->base, HCTL,
677 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
678
679 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
680 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
681 && time_before(jiffies, timeout))
682 ;
683
Adrian Hunterb4175772010-05-26 14:42:06 -0700684 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700685
686 /* Do not initialize card-specific things if the power is off */
687 if (host->power_mode == MMC_POWER_OFF)
688 goto out;
689
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400690 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700691
Andy Shevchenko5934df22011-05-06 12:14:06 +0300692 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700693
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400694 omap_hsmmc_set_bus_mode(host);
695
Denis Karpov11dd62a2009-09-22 16:44:43 -0700696out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530697 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
698 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700699 return 0;
700}
701
702/*
703 * Save the MMC host context (store the number of power state changes so far).
704 */
Denis Karpov70a33412009-09-22 16:44:59 -0700705static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700706{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530707 host->con = OMAP_HSMMC_READ(host->base, CON);
708 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
709 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
710 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700711}
712
713#else
714
Denis Karpov70a33412009-09-22 16:44:59 -0700715static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700716{
717 return 0;
718}
719
Denis Karpov70a33412009-09-22 16:44:59 -0700720static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700721{
722}
723
724#endif
725
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100726/*
727 * Send init stream sequence to card
728 * before sending IDLE command
729 */
Denis Karpov70a33412009-09-22 16:44:59 -0700730static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100731{
732 int reg = 0;
733 unsigned long timeout;
734
Adrian Hunterb62f6222009-09-22 16:45:01 -0700735 if (host->protect_card)
736 return;
737
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100738 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700739
740 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100741 OMAP_HSMMC_WRITE(host->base, CON,
742 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
743 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
744
745 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530746 while ((reg != CC_EN) && time_before(jiffies, timeout))
747 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100748
749 OMAP_HSMMC_WRITE(host->base, CON,
750 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700751
752 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
753 OMAP_HSMMC_READ(host->base, STAT);
754
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100755 enable_irq(host->irq);
756}
757
758static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700759int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100760{
761 int r = 1;
762
Denis Karpov191d1f12009-09-22 16:44:55 -0700763 if (mmc_slot(host).get_cover_state)
764 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100765 return r;
766}
767
768static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700769omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100770 char *buf)
771{
772 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700773 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100774
Denis Karpov70a33412009-09-22 16:44:59 -0700775 return sprintf(buf, "%s\n",
776 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100777}
778
Denis Karpov70a33412009-09-22 16:44:59 -0700779static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100780
781static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700782omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100783 char *buf)
784{
785 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700786 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100787
Denis Karpov191d1f12009-09-22 16:44:55 -0700788 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100789}
790
Denis Karpov70a33412009-09-22 16:44:59 -0700791static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100792
793/*
794 * Configure the response type and send the cmd.
795 */
796static void
Denis Karpov70a33412009-09-22 16:44:59 -0700797omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100798 struct mmc_data *data)
799{
800 int cmdreg = 0, resptype = 0, cmdtype = 0;
801
Venkatraman S8986d312012-08-07 19:10:38 +0530802 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100803 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
804 host->cmd = cmd;
805
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700806 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100807
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200808 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100809 if (cmd->flags & MMC_RSP_PRESENT) {
810 if (cmd->flags & MMC_RSP_136)
811 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200812 else if (cmd->flags & MMC_RSP_BUSY) {
813 resptype = 3;
814 host->response_busy = 1;
815 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100816 resptype = 2;
817 }
818
819 /*
820 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
821 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
822 * a val of 0x3, rest 0x0.
823 */
824 if (cmd == host->mrq->stop)
825 cmdtype = 0x3;
826
827 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
828
Balaji T Ka2e77152014-01-21 19:54:42 +0530829 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
830 host->mrq->sbc) {
831 cmdreg |= ACEN_ACMD23;
832 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
833 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100834 if (data) {
835 cmdreg |= DP_SELECT | MSBS | BCE;
836 if (data->flags & MMC_DATA_READ)
837 cmdreg |= DDIR;
838 else
839 cmdreg &= ~(DDIR);
840 }
841
842 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530843 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100844
Adrian Hunterb4175772010-05-26 14:42:06 -0700845 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700846
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100847 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
848 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
849}
850
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200851static int
Denis Karpov70a33412009-09-22 16:44:59 -0700852omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200853{
854 if (data->flags & MMC_DATA_WRITE)
855 return DMA_TO_DEVICE;
856 else
857 return DMA_FROM_DEVICE;
858}
859
Russell Kingc5c98922012-04-13 12:14:39 +0100860static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
861 struct mmc_data *data)
862{
863 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
864}
865
Adrian Hunterb4175772010-05-26 14:42:06 -0700866static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
867{
868 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530869 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700870
Venkatraman S31463b12012-04-09 12:08:34 +0530871 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700872 host->req_in_progress = 0;
873 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530874 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700875
876 omap_hsmmc_disable_irq(host);
877 /* Do not complete the request if DMA is still in progress */
878 if (mrq->data && host->use_dma && dma_ch != -1)
879 return;
880 host->mrq = NULL;
881 mmc_request_done(host->mmc, mrq);
882}
883
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100884/*
885 * Notify the transfer complete to MMC core
886 */
887static void
Denis Karpov70a33412009-09-22 16:44:59 -0700888omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100889{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200890 if (!data) {
891 struct mmc_request *mrq = host->mrq;
892
Adrian Hunter23050102009-09-22 16:44:57 -0700893 /* TC before CC from CMD6 - don't know why, but it happens */
894 if (host->cmd && host->cmd->opcode == 6 &&
895 host->response_busy) {
896 host->response_busy = 0;
897 return;
898 }
899
Adrian Hunterb4175772010-05-26 14:42:06 -0700900 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200901 return;
902 }
903
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100904 host->data = NULL;
905
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100906 if (!data->error)
907 data->bytes_xfered += data->blocks * (data->blksz);
908 else
909 data->bytes_xfered = 0;
910
Balaji T Kbf129e12014-01-21 19:54:42 +0530911 if (data->stop && (data->error || !host->mrq->sbc))
912 omap_hsmmc_start_command(host, data->stop, NULL);
913 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700914 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100915}
916
917/*
918 * Notify the core about command completion
919 */
920static void
Denis Karpov70a33412009-09-22 16:44:59 -0700921omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100922{
Balaji T Kbf129e12014-01-21 19:54:42 +0530923 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +0530924 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +0530925 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +0530926 omap_hsmmc_start_dma_transfer(host);
927 omap_hsmmc_start_command(host, host->mrq->cmd,
928 host->mrq->data);
929 return;
930 }
931
Balaji T K2177fa92014-05-09 22:16:52 +0530932 host->cmd = NULL;
933
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100934 if (cmd->flags & MMC_RSP_PRESENT) {
935 if (cmd->flags & MMC_RSP_136) {
936 /* response type 2 */
937 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
938 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
939 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
940 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
941 } else {
942 /* response types 1, 1b, 3, 4, 5, 6 */
943 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
944 }
945 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700946 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530947 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100948}
949
950/*
951 * DMA clean up for command errors
952 */
Denis Karpov70a33412009-09-22 16:44:59 -0700953static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100954{
Adrian Hunterb4175772010-05-26 14:42:06 -0700955 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530956 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700957
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200958 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100959
Venkatraman S31463b12012-04-09 12:08:34 +0530960 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700961 dma_ch = host->dma_ch;
962 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530963 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700964
965 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100966 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
967
968 dmaengine_terminate_all(chan);
969 dma_unmap_sg(chan->device->dev,
970 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700971 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100972
Per Forlin053bf342011-11-07 21:55:11 +0530973 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100974 }
975 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100976}
977
978/*
979 * Readable error output
980 */
981#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300982static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100983{
984 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700985 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300986 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
987 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
988 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
989 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100990 };
991 char res[256];
992 char *buf = res;
993 int len, i;
994
995 len = sprintf(buf, "MMC IRQ 0x%x :", status);
996 buf += len;
997
Denis Karpov70a33412009-09-22 16:44:59 -0700998 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100999 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -07001000 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001001 buf += len;
1002 }
1003
Venkatraman S8986d312012-08-07 19:10:38 +05301004 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001005}
Adrian Hunter699b9582011-05-06 12:14:01 +03001006#else
1007static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1008 u32 status)
1009{
1010}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001011#endif /* CONFIG_MMC_DEBUG */
1012
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001013/*
1014 * MMC controller internal state machines reset
1015 *
1016 * Used to reset command or data internal state machines, using respectively
1017 * SRC or SRD bit of SYSCTL register
1018 * Can be called from interrupt context
1019 */
Denis Karpov70a33412009-09-22 16:44:59 -07001020static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1021 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001022{
1023 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301024 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001025
1026 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1027 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1028
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001029 /*
1030 * OMAP4 ES2 and greater has an updated reset logic.
1031 * Monitor a 0->1 transition first
1032 */
1033 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001034 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001035 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301036 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001037 }
1038 i = 0;
1039
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001040 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1041 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301042 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001043
1044 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1045 dev_err(mmc_dev(host->mmc),
1046 "Timeout waiting on controller reset in %s\n",
1047 __func__);
1048}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001049
Balaji T K25e18972012-11-19 21:59:55 +05301050static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1051 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301052{
Balaji T K25e18972012-11-19 21:59:55 +05301053 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301054 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301055 if (host->cmd)
1056 host->cmd->error = err;
1057 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301058
1059 if (host->data) {
1060 omap_hsmmc_reset_controller_fsm(host, SRD);
1061 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301062 } else if (host->mrq && host->mrq->cmd)
1063 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301064}
1065
Adrian Hunterb4175772010-05-26 14:42:06 -07001066static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001067{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001068 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001069 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301070 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001071
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001072 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301073 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001074
Venkatraman Sa7e96872012-11-19 22:00:01 +05301075 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001076 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001077
Venkatraman Sa7e96872012-11-19 22:00:01 +05301078 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301079 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301080 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301081 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301082 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301083 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1084
Balaji T Ka2e77152014-01-21 19:54:42 +05301085 if (status & ACE_EN) {
1086 u32 ac12;
1087 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1088 if (!(ac12 & ACNE) && host->mrq->sbc) {
1089 end_cmd = 1;
1090 if (ac12 & ACTO)
1091 error = -ETIMEDOUT;
1092 else if (ac12 & (ACCE | ACEB | ACIE))
1093 error = -EILSEQ;
1094 host->mrq->sbc->error = error;
1095 hsmmc_command_incomplete(host, error, end_cmd);
1096 }
1097 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1098 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301099 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301100 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301101 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001102 }
1103 }
1104
Francesco Lavra7472bab2013-06-29 08:25:12 +02001105 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301106 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001107 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301108 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001109 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001110}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001111
Adrian Hunterb4175772010-05-26 14:42:06 -07001112/*
1113 * MMC controller IRQ handler
1114 */
1115static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1116{
1117 struct omap_hsmmc_host *host = dev_id;
1118 int status;
1119
1120 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301121 while (status & INT_EN_MASK && host->req_in_progress) {
Adrian Hunterb4175772010-05-26 14:42:06 -07001122 omap_hsmmc_do_irq(host, status);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301123
Adrian Hunterb4175772010-05-26 14:42:06 -07001124 /* Flush posted write */
1125 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301126 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001127
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001128 return IRQ_HANDLED;
1129}
1130
Denis Karpov70a33412009-09-22 16:44:59 -07001131static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001132{
1133 unsigned long i;
1134
1135 OMAP_HSMMC_WRITE(host->base, HCTL,
1136 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1137 for (i = 0; i < loops_per_jiffy; i++) {
1138 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1139 break;
1140 cpu_relax();
1141 }
1142}
1143
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001144/*
David Brownelleb250822009-02-17 14:49:01 -08001145 * Switch MMC interface voltage ... only relevant for MMC1.
1146 *
1147 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1148 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1149 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001150 */
Denis Karpov70a33412009-09-22 16:44:59 -07001151static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001152{
1153 u32 reg_val = 0;
1154 int ret;
1155
1156 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301157 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301158 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301159 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001160
1161 /* Turn the power off */
1162 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001163
1164 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001165 if (!ret)
1166 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1167 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301168 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301169 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301170 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001171
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001172 if (ret != 0)
1173 goto err;
1174
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001175 OMAP_HSMMC_WRITE(host->base, HCTL,
1176 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1177 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001178
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001179 /*
1180 * If a MMC dual voltage card is detected, the set_ios fn calls
1181 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001182 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001183 *
David Brownelleb250822009-02-17 14:49:01 -08001184 * Cope with a bit of slop in the range ... per data sheets:
1185 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1186 * but recommended values are 1.71V to 1.89V
1187 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1188 * but recommended values are 2.7V to 3.3V
1189 *
1190 * Board setup code shouldn't permit anything very out-of-range.
1191 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1192 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001193 */
David Brownelleb250822009-02-17 14:49:01 -08001194 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001195 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001196 else
1197 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001198
1199 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001200 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001201
1202 return 0;
1203err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301204 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001205 return ret;
1206}
1207
Adrian Hunterb62f6222009-09-22 16:45:01 -07001208/* Protect the card while the cover is open */
1209static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1210{
1211 if (!mmc_slot(host).get_cover_state)
1212 return;
1213
1214 host->reqs_blocked = 0;
1215 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1216 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301217 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001218 "card is now accessible\n",
1219 mmc_hostname(host->mmc));
1220 host->protect_card = 0;
1221 }
1222 } else {
1223 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301224 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001225 "card is now inaccessible\n",
1226 mmc_hostname(host->mmc));
1227 host->protect_card = 1;
1228 }
1229 }
1230}
1231
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001232/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001233 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001234 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001235static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001236{
NeilBrown7efab4f2011-12-30 12:35:13 +11001237 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001238 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001239 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001240
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001241 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001242
Denis Karpov191d1f12009-09-22 16:44:55 -07001243 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001244 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001245 else {
1246 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001247 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001248 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001249
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001250 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001251 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001252 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001253 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001254 return IRQ_HANDLED;
1255}
1256
Russell Kingc5c98922012-04-13 12:14:39 +01001257static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001258{
Russell Kingc5c98922012-04-13 12:14:39 +01001259 struct omap_hsmmc_host *host = param;
1260 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001261 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001262 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001263
Russell Kingc5c98922012-04-13 12:14:39 +01001264 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001265 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001266 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001267 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001268 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001269
Adrian Hunter770d7432011-05-06 12:14:11 +03001270 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001271 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001272 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001273 dma_unmap_sg(chan->device->dev,
1274 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001275 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001276
1277 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001278 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001279 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001280
1281 /* If DMA has finished after TC, complete the request */
1282 if (!req_in_progress) {
1283 struct mmc_request *mrq = host->mrq;
1284
1285 host->mrq = NULL;
1286 mmc_request_done(host->mmc, mrq);
1287 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001288}
1289
Per Forlin9782aff2011-07-01 18:55:23 +02001290static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1291 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001292 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001293 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001294{
1295 int dma_len;
1296
1297 if (!next && data->host_cookie &&
1298 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301299 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001300 " host->next_data.cookie %d\n",
1301 __func__, data->host_cookie, host->next_data.cookie);
1302 data->host_cookie = 0;
1303 }
1304
1305 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001306 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001307 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001308 omap_hsmmc_get_dma_dir(host, data));
1309
1310 } else {
1311 dma_len = host->next_data.dma_len;
1312 host->next_data.dma_len = 0;
1313 }
1314
1315
1316 if (dma_len == 0)
1317 return -EINVAL;
1318
1319 if (next) {
1320 next->dma_len = dma_len;
1321 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1322 } else
1323 host->dma_len = dma_len;
1324
1325 return 0;
1326}
1327
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001328/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001329 * Routine to configure and start DMA for the MMC card
1330 */
Balaji T K9d025332014-01-21 19:54:42 +05301331static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001332 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001333{
Russell King26b88522012-04-13 12:27:37 +01001334 struct dma_slave_config cfg;
1335 struct dma_async_tx_descriptor *tx;
1336 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001337 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001338 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001339
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001340 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001341 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001342 struct scatterlist *sgl;
1343
1344 sgl = data->sg + i;
1345 if (sgl->length % data->blksz)
1346 return -EINVAL;
1347 }
1348 if ((data->blksz % 4) != 0)
1349 /* REVISIT: The MMC buffer increments only when MSB is written.
1350 * Return error for blksz which is non multiple of four.
1351 */
1352 return -EINVAL;
1353
Adrian Hunterb4175772010-05-26 14:42:06 -07001354 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001355
Russell Kingc5c98922012-04-13 12:14:39 +01001356 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001357
Russell King26b88522012-04-13 12:27:37 +01001358 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1359 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1360 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1361 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1362 cfg.src_maxburst = data->blksz / 4;
1363 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001364
Russell King26b88522012-04-13 12:27:37 +01001365 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001366 if (ret)
1367 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001368
Russell King26b88522012-04-13 12:27:37 +01001369 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1370 if (ret)
1371 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001372
Russell King26b88522012-04-13 12:27:37 +01001373 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1374 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1375 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1376 if (!tx) {
1377 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1378 /* FIXME: cleanup */
1379 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001380 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001381
Russell King26b88522012-04-13 12:27:37 +01001382 tx->callback = omap_hsmmc_dma_callback;
1383 tx->callback_param = host;
1384
1385 /* Does not fail */
1386 dmaengine_submit(tx);
1387
1388 host->dma_ch = 1;
1389
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001390 return 0;
1391}
1392
Denis Karpov70a33412009-09-22 16:44:59 -07001393static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001394 unsigned int timeout_ns,
1395 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001396{
1397 unsigned int timeout, cycle_ns;
1398 uint32_t reg, clkd, dto = 0;
1399
1400 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1401 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1402 if (clkd == 0)
1403 clkd = 1;
1404
Balaji T K6e3076c2014-01-21 19:54:42 +05301405 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001406 timeout = timeout_ns / cycle_ns;
1407 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001408 if (timeout) {
1409 while ((timeout & 0x80000000) == 0) {
1410 dto += 1;
1411 timeout <<= 1;
1412 }
1413 dto = 31 - dto;
1414 timeout <<= 1;
1415 if (timeout && dto)
1416 dto += 1;
1417 if (dto >= 13)
1418 dto -= 13;
1419 else
1420 dto = 0;
1421 if (dto > 14)
1422 dto = 14;
1423 }
1424
1425 reg &= ~DTO_MASK;
1426 reg |= dto << DTO_SHIFT;
1427 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1428}
1429
Balaji T K9d025332014-01-21 19:54:42 +05301430static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1431{
1432 struct mmc_request *req = host->mrq;
1433 struct dma_chan *chan;
1434
1435 if (!req->data)
1436 return;
1437 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1438 | (req->data->blocks << 16));
1439 set_data_timeout(host, req->data->timeout_ns,
1440 req->data->timeout_clks);
1441 chan = omap_hsmmc_get_dma_chan(host, req->data);
1442 dma_async_issue_pending(chan);
1443}
1444
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001445/*
1446 * Configure block length for MMC/SD cards and initiate the transfer.
1447 */
1448static int
Denis Karpov70a33412009-09-22 16:44:59 -07001449omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001450{
1451 int ret;
1452 host->data = req->data;
1453
1454 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001455 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001456 /*
1457 * Set an arbitrary 100ms data timeout for commands with
1458 * busy signal.
1459 */
1460 if (req->cmd->flags & MMC_RSP_BUSY)
1461 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001462 return 0;
1463 }
1464
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001465 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301466 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001467 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301468 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001469 return ret;
1470 }
1471 }
1472 return 0;
1473}
1474
Per Forlin9782aff2011-07-01 18:55:23 +02001475static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1476 int err)
1477{
1478 struct omap_hsmmc_host *host = mmc_priv(mmc);
1479 struct mmc_data *data = mrq->data;
1480
Russell King26b88522012-04-13 12:27:37 +01001481 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001482 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001483
Russell King26b88522012-04-13 12:27:37 +01001484 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1485 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001486 data->host_cookie = 0;
1487 }
1488}
1489
1490static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1491 bool is_first_req)
1492{
1493 struct omap_hsmmc_host *host = mmc_priv(mmc);
1494
1495 if (mrq->data->host_cookie) {
1496 mrq->data->host_cookie = 0;
1497 return ;
1498 }
1499
Russell Kingc5c98922012-04-13 12:14:39 +01001500 if (host->use_dma) {
1501 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001502
Per Forlin9782aff2011-07-01 18:55:23 +02001503 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001504 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001505 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001506 }
Per Forlin9782aff2011-07-01 18:55:23 +02001507}
1508
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001509/*
1510 * Request function. for read/write operation
1511 */
Denis Karpov70a33412009-09-22 16:44:59 -07001512static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001513{
Denis Karpov70a33412009-09-22 16:44:59 -07001514 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001515 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001516
Adrian Hunterb4175772010-05-26 14:42:06 -07001517 BUG_ON(host->req_in_progress);
1518 BUG_ON(host->dma_ch != -1);
1519 if (host->protect_card) {
1520 if (host->reqs_blocked < 3) {
1521 /*
1522 * Ensure the controller is left in a consistent
1523 * state by resetting the command and data state
1524 * machines.
1525 */
1526 omap_hsmmc_reset_controller_fsm(host, SRD);
1527 omap_hsmmc_reset_controller_fsm(host, SRC);
1528 host->reqs_blocked += 1;
1529 }
1530 req->cmd->error = -EBADF;
1531 if (req->data)
1532 req->data->error = -EBADF;
1533 req->cmd->retries = 0;
1534 mmc_request_done(mmc, req);
1535 return;
1536 } else if (host->reqs_blocked)
1537 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001538 WARN_ON(host->mrq != NULL);
1539 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301540 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001541 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001542 if (err) {
1543 req->cmd->error = err;
1544 if (req->data)
1545 req->data->error = err;
1546 host->mrq = NULL;
1547 mmc_request_done(mmc, req);
1548 return;
1549 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301550 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301551 omap_hsmmc_start_command(host, req->sbc, NULL);
1552 return;
1553 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001554
Balaji T K9d025332014-01-21 19:54:42 +05301555 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001556 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001557}
1558
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001559/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001560static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001561{
Denis Karpov70a33412009-09-22 16:44:59 -07001562 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001563 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001564
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301565 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001566
Adrian Huntera3621462009-09-22 16:44:42 -07001567 if (ios->power_mode != host->power_mode) {
1568 switch (ios->power_mode) {
1569 case MMC_POWER_OFF:
1570 mmc_slot(host).set_power(host->dev, host->slot_id,
1571 0, 0);
1572 break;
1573 case MMC_POWER_UP:
1574 mmc_slot(host).set_power(host->dev, host->slot_id,
1575 1, ios->vdd);
1576 break;
1577 case MMC_POWER_ON:
1578 do_send_init_stream = 1;
1579 break;
1580 }
1581 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001582 }
1583
Denis Karpovdd498ef2009-09-22 16:44:49 -07001584 /* FIXME: set registers based only on changes to ios */
1585
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001586 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001587
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301588 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001589 /* Only MMC1 can interface at 3V without some flavor
1590 * of external transceiver; but they all handle 1.8V.
1591 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001592 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301593 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001594 /*
1595 * The mmc_select_voltage fn of the core does
1596 * not seem to set the power_mode to
1597 * MMC_POWER_UP upon recalculating the voltage.
1598 * vdd 1.8v.
1599 */
Denis Karpov70a33412009-09-22 16:44:59 -07001600 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1601 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001602 "Switch operation failed\n");
1603 }
1604 }
1605
Andy Shevchenko5934df22011-05-06 12:14:06 +03001606 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001607
Adrian Huntera3621462009-09-22 16:44:42 -07001608 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001609 send_init_stream(host);
1610
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001611 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001612
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301613 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001614}
1615
1616static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1617{
Denis Karpov70a33412009-09-22 16:44:59 -07001618 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001619
Denis Karpov191d1f12009-09-22 16:44:55 -07001620 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001621 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001622 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001623}
1624
1625static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1626{
Denis Karpov70a33412009-09-22 16:44:59 -07001627 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001628
Denis Karpov191d1f12009-09-22 16:44:55 -07001629 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001630 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001631 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001632}
1633
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001634static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1635{
1636 struct omap_hsmmc_host *host = mmc_priv(mmc);
1637
1638 if (mmc_slot(host).init_card)
1639 mmc_slot(host).init_card(card);
1640}
1641
Denis Karpov70a33412009-09-22 16:44:59 -07001642static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001643{
1644 u32 hctl, capa, value;
1645
1646 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301647 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001648 hctl = SDVS30;
1649 capa = VS30 | VS18;
1650 } else {
1651 hctl = SDVS18;
1652 capa = VS18;
1653 }
1654
1655 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1656 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1657
1658 value = OMAP_HSMMC_READ(host->base, CAPA);
1659 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1660
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001661 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001662 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001663}
1664
Denis Karpov70a33412009-09-22 16:44:59 -07001665static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001666{
Denis Karpov70a33412009-09-22 16:44:59 -07001667 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001668
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301669 pm_runtime_get_sync(host->dev);
1670
Denis Karpovdd498ef2009-09-22 16:44:49 -07001671 return 0;
1672}
1673
Adrian Hunter907d2e72012-02-29 09:17:21 +02001674static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001675{
Denis Karpov70a33412009-09-22 16:44:59 -07001676 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001677
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301678 pm_runtime_mark_last_busy(host->dev);
1679 pm_runtime_put_autosuspend(host->dev);
1680
Denis Karpovdd498ef2009-09-22 16:44:49 -07001681 return 0;
1682}
1683
Denis Karpov70a33412009-09-22 16:44:59 -07001684static const struct mmc_host_ops omap_hsmmc_ops = {
1685 .enable = omap_hsmmc_enable_fclk,
1686 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001687 .post_req = omap_hsmmc_post_req,
1688 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001689 .request = omap_hsmmc_request,
1690 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001691 .get_cd = omap_hsmmc_get_cd,
1692 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001693 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001694 /* NYET -- enable_sdio_irq */
1695};
1696
Denis Karpovd900f712009-09-22 16:44:38 -07001697#ifdef CONFIG_DEBUG_FS
1698
Denis Karpov70a33412009-09-22 16:44:59 -07001699static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001700{
1701 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001702 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001703
Tony Lindgren0a82e062013-10-21 00:25:19 +05301704 seq_printf(s, "mmc%d:\n ctx_loss:\t%d\n\nregs:\n",
1705 mmc->index, host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001706
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301707 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001708
Denis Karpovd900f712009-09-22 16:44:38 -07001709 seq_printf(s, "CON:\t\t0x%08x\n",
1710 OMAP_HSMMC_READ(host->base, CON));
1711 seq_printf(s, "HCTL:\t\t0x%08x\n",
1712 OMAP_HSMMC_READ(host->base, HCTL));
1713 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1714 OMAP_HSMMC_READ(host->base, SYSCTL));
1715 seq_printf(s, "IE:\t\t0x%08x\n",
1716 OMAP_HSMMC_READ(host->base, IE));
1717 seq_printf(s, "ISE:\t\t0x%08x\n",
1718 OMAP_HSMMC_READ(host->base, ISE));
1719 seq_printf(s, "CAPA:\t\t0x%08x\n",
1720 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001721
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301722 pm_runtime_mark_last_busy(host->dev);
1723 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001724
Denis Karpovd900f712009-09-22 16:44:38 -07001725 return 0;
1726}
1727
Denis Karpov70a33412009-09-22 16:44:59 -07001728static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001729{
Denis Karpov70a33412009-09-22 16:44:59 -07001730 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001731}
1732
1733static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001734 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001735 .read = seq_read,
1736 .llseek = seq_lseek,
1737 .release = single_release,
1738};
1739
Denis Karpov70a33412009-09-22 16:44:59 -07001740static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001741{
1742 if (mmc->debugfs_root)
1743 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1744 mmc, &mmc_regs_fops);
1745}
1746
1747#else
1748
Denis Karpov70a33412009-09-22 16:44:59 -07001749static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001750{
1751}
1752
1753#endif
1754
Rajendra Nayak46856a62012-03-12 20:32:37 +05301755#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001756static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1757 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1758 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1759};
1760
1761static const struct omap_mmc_of_data omap4_mmc_of_data = {
1762 .reg_offset = 0x100,
1763};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301764
1765static const struct of_device_id omap_mmc_of_match[] = {
1766 {
1767 .compatible = "ti,omap2-hsmmc",
1768 },
1769 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001770 .compatible = "ti,omap3-pre-es3-hsmmc",
1771 .data = &omap3_pre_es3_mmc_of_data,
1772 },
1773 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301774 .compatible = "ti,omap3-hsmmc",
1775 },
1776 {
1777 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001778 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301779 },
1780 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001781};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301782MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1783
1784static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1785{
1786 struct omap_mmc_platform_data *pdata;
1787 struct device_node *np = dev->of_node;
Daniel Mackd8714e82012-10-15 21:35:06 +05301788 u32 bus_width, max_freq;
Jan Luebbedc642c22013-01-30 10:07:17 +01001789 int cd_gpio, wp_gpio;
1790
1791 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
1792 wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1793 if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
1794 return ERR_PTR(-EPROBE_DEFER);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301795
1796 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1797 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301798 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301799
1800 if (of_find_property(np, "ti,dual-volt", NULL))
1801 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1802
1803 /* This driver only supports 1 slot */
1804 pdata->nr_slots = 1;
Jan Luebbedc642c22013-01-30 10:07:17 +01001805 pdata->slots[0].switch_pin = cd_gpio;
1806 pdata->slots[0].gpio_wp = wp_gpio;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301807
1808 if (of_find_property(np, "ti,non-removable", NULL)) {
1809 pdata->slots[0].nonremovable = true;
1810 pdata->slots[0].no_regulator_off_init = true;
1811 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001812 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301813 if (bus_width == 4)
1814 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1815 else if (bus_width == 8)
1816 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1817
1818 if (of_find_property(np, "ti,needs-special-reset", NULL))
1819 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1820
Daniel Mackd8714e82012-10-15 21:35:06 +05301821 if (!of_property_read_u32(np, "max-frequency", &max_freq))
1822 pdata->max_freq = max_freq;
1823
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301824 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
1825 pdata->slots[0].features |= HSMMC_HAS_HSPE_SUPPORT;
1826
Daniel Mackc9ae64d2014-02-17 12:36:33 +01001827 if (of_find_property(np, "keep-power-in-suspend", NULL))
1828 pdata->slots[0].pm_caps |= MMC_PM_KEEP_POWER;
1829
1830 if (of_find_property(np, "enable-sdio-wakeup", NULL))
1831 pdata->slots[0].pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
1832
Rajendra Nayak46856a62012-03-12 20:32:37 +05301833 return pdata;
1834}
1835#else
1836static inline struct omap_mmc_platform_data
1837 *of_get_hsmmc_pdata(struct device *dev)
1838{
Balaji T K19df45b2014-02-28 19:08:18 +05301839 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301840}
1841#endif
1842
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001843static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001844{
1845 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1846 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001847 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001848 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001849 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301850 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001851 dma_cap_mask_t mask;
1852 unsigned tx_req, rx_req;
Daniel Mack46b76032012-10-15 21:35:05 +05301853 struct pinctrl *pinctrl;
Nishanth Menon59445b12014-02-13 23:45:48 -06001854 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05301855 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301856
1857 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1858 if (match) {
1859 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01001860
1861 if (IS_ERR(pdata))
1862 return PTR_ERR(pdata);
1863
Rajendra Nayak46856a62012-03-12 20:32:37 +05301864 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06001865 data = match->data;
1866 pdata->reg_offset = data->reg_offset;
1867 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301868 }
1869 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001870
1871 if (pdata == NULL) {
1872 dev_err(&pdev->dev, "Platform Data is missing\n");
1873 return -ENXIO;
1874 }
1875
1876 if (pdata->nr_slots == 0) {
1877 dev_err(&pdev->dev, "No Slots\n");
1878 return -ENXIO;
1879 }
1880
1881 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1882 irq = platform_get_irq(pdev, 0);
1883 if (res == NULL || irq < 0)
1884 return -ENXIO;
1885
Balaji T K77fae212014-05-09 22:16:51 +05301886 base = devm_ioremap_resource(&pdev->dev, res);
1887 if (IS_ERR(base))
1888 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001889
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001890 ret = omap_hsmmc_gpio_init(pdata);
1891 if (ret)
1892 goto err;
1893
Denis Karpov70a33412009-09-22 16:44:59 -07001894 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001895 if (!mmc) {
1896 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001897 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001898 }
1899
1900 host = mmc_priv(mmc);
1901 host->mmc = mmc;
1902 host->pdata = pdata;
1903 host->dev = &pdev->dev;
1904 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001905 host->dma_ch = -1;
1906 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001907 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05301908 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05301909 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08001910 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001911 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05301912 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001913
1914 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001915
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301916 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001917
Daniel Mackd418ed82012-02-19 13:20:33 +01001918 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1919
1920 if (pdata->max_freq > 0)
1921 mmc->f_max = pdata->max_freq;
1922 else
1923 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001924
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001925 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001926
Balaji T K96181952014-05-09 22:16:48 +05301927 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001928 if (IS_ERR(host->fclk)) {
1929 ret = PTR_ERR(host->fclk);
1930 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001931 goto err1;
1932 }
1933
Paul Walmsley9b682562011-10-06 14:50:35 -06001934 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1935 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1936 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1937 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001938
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301939 pm_runtime_enable(host->dev);
1940 pm_runtime_get_sync(host->dev);
1941 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1942 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001943
Balaji T K92a3aeb2012-02-24 21:14:34 +05301944 omap_hsmmc_context_save(host);
1945
Balaji T K96181952014-05-09 22:16:48 +05301946 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301947 /*
1948 * MMC can still work without debounce clock.
1949 */
1950 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301951 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05301952 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301953 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301954 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07001955 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001956
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001957 /* Since we do only SG emulation, we can have as many segs
1958 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001959 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001960
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001961 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1962 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1963 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1964 mmc->max_seg_size = mmc->max_req_size;
1965
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001966 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001967 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001968
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001969 mmc->caps |= mmc_slot(host).caps;
1970 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001971 mmc->caps |= MMC_CAP_4_BIT_DATA;
1972
Denis Karpov191d1f12009-09-22 16:44:55 -07001973 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001974 mmc->caps |= MMC_CAP_NONREMOVABLE;
1975
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001976 mmc->pm_caps = mmc_slot(host).pm_caps;
1977
Denis Karpov70a33412009-09-22 16:44:59 -07001978 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001979
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05301980 if (!pdev->dev.of_node) {
1981 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1982 if (!res) {
1983 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
1984 ret = -ENXIO;
1985 goto err_irq;
1986 }
1987 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001988
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05301989 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1990 if (!res) {
1991 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
1992 ret = -ENXIO;
1993 goto err_irq;
1994 }
1995 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001996 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001997
Russell King26b88522012-04-13 12:27:37 +01001998 dma_cap_zero(mask);
1999 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002000
Matt Porterd272fbf2013-05-10 17:42:34 +05302001 host->rx_chan =
2002 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2003 &rx_req, &pdev->dev, "rx");
2004
Russell King26b88522012-04-13 12:27:37 +01002005 if (!host->rx_chan) {
2006 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002007 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002008 goto err_irq;
2009 }
2010
Matt Porterd272fbf2013-05-10 17:42:34 +05302011 host->tx_chan =
2012 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2013 &tx_req, &pdev->dev, "tx");
2014
Russell King26b88522012-04-13 12:27:37 +01002015 if (!host->tx_chan) {
2016 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002017 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002018 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002019 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002020
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002021 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302022 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002023 mmc_hostname(mmc), host);
2024 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302025 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002026 goto err_irq;
2027 }
2028
2029 if (pdata->init != NULL) {
2030 if (pdata->init(&pdev->dev) != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302031 dev_err(mmc_dev(host->mmc),
Denis Karpov70a33412009-09-22 16:44:59 -07002032 "Unable to configure MMC IRQs\n");
Balaji T Ke1538ed2014-05-09 22:16:49 +05302033 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002034 }
2035 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002036
Adrian Hunterb702b102010-02-15 10:03:35 -08002037 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002038 ret = omap_hsmmc_reg_get(host);
2039 if (ret)
2040 goto err_reg;
2041 host->use_reg = 1;
2042 }
2043
David Brownellb583f262009-05-28 14:04:03 -07002044 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002045
2046 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002047 if ((mmc_slot(host).card_detect_irq)) {
Balaji T K9fa0e052014-05-09 22:16:50 +05302048 ret = devm_request_threaded_irq(&pdev->dev,
2049 mmc_slot(host).card_detect_irq,
2050 NULL, omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08002051 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11002052 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002053 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302054 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002055 "Unable to grab MMC CD IRQ\n");
2056 goto err_irq_cd;
2057 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00002058 pdata->suspend = omap_hsmmc_suspend_cdirq;
2059 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002060 }
2061
Adrian Hunterb4175772010-05-26 14:42:06 -07002062 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002063
Daniel Mack46b76032012-10-15 21:35:05 +05302064 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
2065 if (IS_ERR(pinctrl))
2066 dev_warn(&pdev->dev,
2067 "pins are not configured from the driver\n");
2068
Adrian Hunterb62f6222009-09-22 16:45:01 -07002069 omap_hsmmc_protect_card(host);
2070
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002071 mmc_add_host(mmc);
2072
Denis Karpov191d1f12009-09-22 16:44:55 -07002073 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002074 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2075 if (ret < 0)
2076 goto err_slot_name;
2077 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002078 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002079 ret = device_create_file(&mmc->class_dev,
2080 &dev_attr_cover_switch);
2081 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002082 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002083 }
2084
Denis Karpov70a33412009-09-22 16:44:59 -07002085 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302086 pm_runtime_mark_last_busy(host->dev);
2087 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002088
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002089 return 0;
2090
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002091err_slot_name:
2092 mmc_remove_host(mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002093err_irq_cd:
2094 if (host->use_reg)
2095 omap_hsmmc_reg_put(host);
2096err_reg:
2097 if (host->pdata->cleanup)
2098 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002099err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002100 if (host->tx_chan)
2101 dma_release_channel(host->tx_chan);
2102 if (host->rx_chan)
2103 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302104 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002105 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302106 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302107 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002108err1:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002109 mmc_free_host(mmc);
2110err_alloc:
2111 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002112err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002113 return ret;
2114}
2115
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002116static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002117{
Denis Karpov70a33412009-09-22 16:44:59 -07002118 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002119
Felipe Balbi927ce942012-03-14 11:18:27 +02002120 pm_runtime_get_sync(host->dev);
2121 mmc_remove_host(host->mmc);
2122 if (host->use_reg)
2123 omap_hsmmc_reg_put(host);
2124 if (host->pdata->cleanup)
2125 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002126
Russell Kingc5c98922012-04-13 12:14:39 +01002127 if (host->tx_chan)
2128 dma_release_channel(host->tx_chan);
2129 if (host->rx_chan)
2130 dma_release_channel(host->rx_chan);
2131
Felipe Balbi927ce942012-03-14 11:18:27 +02002132 pm_runtime_put_sync(host->dev);
2133 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302134 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302135 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002136
Balaji T K9ea28ec2012-10-15 21:35:08 +05302137 omap_hsmmc_gpio_free(host->pdata);
Balaji T K9d1f0282012-10-15 21:35:07 +05302138 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002139
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002140 return 0;
2141}
2142
2143#ifdef CONFIG_PM
Felipe Balbia48ce882012-11-19 21:59:59 +05302144static int omap_hsmmc_prepare(struct device *dev)
2145{
2146 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2147
2148 if (host->pdata->suspend)
2149 return host->pdata->suspend(dev, host->slot_id);
2150
2151 return 0;
2152}
2153
2154static void omap_hsmmc_complete(struct device *dev)
2155{
2156 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2157
2158 if (host->pdata->resume)
2159 host->pdata->resume(dev, host->slot_id);
2160
2161}
2162
Kevin Hilmana791daa2010-05-26 14:42:07 -07002163static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002164{
Felipe Balbi927ce942012-03-14 11:18:27 +02002165 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2166
2167 if (!host)
2168 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002169
Felipe Balbi927ce942012-03-14 11:18:27 +02002170 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002171
2172 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2173 omap_hsmmc_disable_irq(host);
2174 OMAP_HSMMC_WRITE(host->base, HCTL,
2175 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2176 }
2177
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302178 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302179 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002180
Eliad Peller31f9d462011-11-22 16:02:17 +02002181 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002182 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002183}
2184
2185/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002186static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002187{
Felipe Balbi927ce942012-03-14 11:18:27 +02002188 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2189
2190 if (!host)
2191 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002192
Felipe Balbi927ce942012-03-14 11:18:27 +02002193 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002194
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302195 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302196 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002197
Felipe Balbi927ce942012-03-14 11:18:27 +02002198 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2199 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002200
Felipe Balbi927ce942012-03-14 11:18:27 +02002201 omap_hsmmc_protect_card(host);
2202
Felipe Balbi927ce942012-03-14 11:18:27 +02002203 pm_runtime_mark_last_busy(host->dev);
2204 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002205 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002206}
2207
2208#else
Felipe Balbia48ce882012-11-19 21:59:59 +05302209#define omap_hsmmc_prepare NULL
2210#define omap_hsmmc_complete NULL
Denis Karpov70a33412009-09-22 16:44:59 -07002211#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302212#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002213#endif
2214
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302215static int omap_hsmmc_runtime_suspend(struct device *dev)
2216{
2217 struct omap_hsmmc_host *host;
2218
2219 host = platform_get_drvdata(to_platform_device(dev));
2220 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002221 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302222
2223 return 0;
2224}
2225
2226static int omap_hsmmc_runtime_resume(struct device *dev)
2227{
2228 struct omap_hsmmc_host *host;
2229
2230 host = platform_get_drvdata(to_platform_device(dev));
2231 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002232 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302233
2234 return 0;
2235}
2236
Kevin Hilmana791daa2010-05-26 14:42:07 -07002237static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002238 .suspend = omap_hsmmc_suspend,
2239 .resume = omap_hsmmc_resume,
Felipe Balbia48ce882012-11-19 21:59:59 +05302240 .prepare = omap_hsmmc_prepare,
2241 .complete = omap_hsmmc_complete,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302242 .runtime_suspend = omap_hsmmc_runtime_suspend,
2243 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002244};
2245
2246static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002247 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002248 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002249 .driver = {
2250 .name = DRIVER_NAME,
2251 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002252 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302253 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002254 },
2255};
2256
Felipe Balbib7964502012-03-14 11:18:32 +02002257module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002258MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2259MODULE_LICENSE("GPL");
2260MODULE_ALIAS("platform:" DRIVER_NAME);
2261MODULE_AUTHOR("Texas Instruments Inc");