blob: 476c6a632bfc6d2a903860a608a2d0f506404eb7 [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Andy Shevchenkoac330f442011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
Russell Kingc5c98922012-04-13 12:14:39 +010022#include <linux/dmaengine.h>
Denis Karpovd900f712009-09-22 16:44:38 -070023#include <linux/seq_file.h>
Felipe Balbi031cd032013-07-11 16:09:05 +030024#include <linux/sizes.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010025#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/dma-mapping.h>
28#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010029#include <linux/timer.h>
30#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053031#include <linux/of.h>
32#include <linux/of_gpio.h>
33#include <linux/of_device.h>
Russell King3451c062012-04-21 22:35:42 +010034#include <linux/omap-dma.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010035#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070036#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070037#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010038#include <linux/io.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080039#include <linux/gpio.h>
40#include <linux/regulator/consumer.h>
Daniel Mack46b76032012-10-15 21:35:05 +053041#include <linux/pinctrl/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053042#include <linux/pm_runtime.h>
Tony Lindgren68f39e72012-10-15 12:09:43 -070043#include <linux/platform_data/mmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010044
45/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070046#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010047#define OMAP_HSMMC_CON 0x002C
48#define OMAP_HSMMC_BLK 0x0104
49#define OMAP_HSMMC_ARG 0x0108
50#define OMAP_HSMMC_CMD 0x010C
51#define OMAP_HSMMC_RSP10 0x0110
52#define OMAP_HSMMC_RSP32 0x0114
53#define OMAP_HSMMC_RSP54 0x0118
54#define OMAP_HSMMC_RSP76 0x011C
55#define OMAP_HSMMC_DATA 0x0120
56#define OMAP_HSMMC_HCTL 0x0128
57#define OMAP_HSMMC_SYSCTL 0x012C
58#define OMAP_HSMMC_STAT 0x0130
59#define OMAP_HSMMC_IE 0x0134
60#define OMAP_HSMMC_ISE 0x0138
61#define OMAP_HSMMC_CAPA 0x0140
62
63#define VS18 (1 << 26)
64#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053065#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010066#define SDVS18 (0x5 << 9)
67#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080068#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010069#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010070#define SDVSCLR 0xFFFFF1FF
71#define SDVSDET 0x00000400
72#define AUTOIDLE 0x1
73#define SDBP (1 << 8)
74#define DTO 0xe
75#define ICE 0x1
76#define ICS 0x2
77#define CEN (1 << 2)
Balaji T Ked164182013-10-21 00:25:21 +053078#define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010079#define CLKD_MASK 0x0000FFC0
80#define CLKD_SHIFT 6
81#define DTO_MASK 0x000F0000
82#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010083#define INIT_STREAM (1 << 1)
84#define DP_SELECT (1 << 21)
85#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053086#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010087#define MSBS (1 << 5)
88#define BCE (1 << 1)
89#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053090#define HSPE (1 << 2)
Balaji T K03b5d922012-04-09 12:08:33 +053091#define DDR (1 << 19)
Jarkko Lavinen73153012008-11-21 16:49:54 +020092#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010093#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010094#define STAT_CLEAR 0xFFFFFFFF
95#define INIT_STREAM_CMD 0x00000000
96#define DUAL_VOLT_OCR_BIT 7
97#define SRC (1 << 25)
98#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -070099#define SOFTRESET (1 << 1)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100100
Venkatraman Sa7e96872012-11-19 22:00:01 +0530101/* Interrupt masks for IE and ISE register */
102#define CC_EN (1 << 0)
103#define TC_EN (1 << 1)
104#define BWR_EN (1 << 4)
105#define BRR_EN (1 << 5)
106#define ERR_EN (1 << 15)
107#define CTO_EN (1 << 16)
108#define CCRC_EN (1 << 17)
109#define CEB_EN (1 << 18)
110#define CIE_EN (1 << 19)
111#define DTO_EN (1 << 20)
112#define DCRC_EN (1 << 21)
113#define DEB_EN (1 << 22)
114#define CERR_EN (1 << 28)
115#define BADA_EN (1 << 29)
116
117#define INT_EN_MASK (BADA_EN | CERR_EN | DEB_EN | DCRC_EN |\
118 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
119 BRR_EN | BWR_EN | TC_EN | CC_EN)
120
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530121#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530122#define MMC_TIMEOUT_MS 20 /* 20 mSec */
123#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400124#define OMAP_MMC_MIN_CLOCK 400000
125#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530126#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100127
Balaji T Ke99448f2014-02-19 20:26:40 +0530128#define VDD_1V8 1800000 /* 180000 uV */
129#define VDD_3V0 3000000 /* 300000 uV */
130#define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
131
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100132/*
133 * One controller can have multiple slots, like on some omap boards using
134 * omap.c controller driver. Luckily this is not currently done on any known
135 * omap_hsmmc.c device.
136 */
137#define mmc_slot(host) (host->pdata->slots[host->slot_id])
138
139/*
140 * MMC Host controller read/write API's
141 */
142#define OMAP_HSMMC_READ(base, reg) \
143 __raw_readl((base) + OMAP_HSMMC_##reg)
144
145#define OMAP_HSMMC_WRITE(base, reg, val) \
146 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
147
Per Forlin9782aff2011-07-01 18:55:23 +0200148struct omap_hsmmc_next {
149 unsigned int dma_len;
150 s32 cookie;
151};
152
Denis Karpov70a33412009-09-22 16:44:59 -0700153struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100154 struct device *dev;
155 struct mmc_host *mmc;
156 struct mmc_request *mrq;
157 struct mmc_command *cmd;
158 struct mmc_data *data;
159 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100160 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800161 /*
162 * vcc == configured supply
163 * vcc_aux == optional
164 * - MMC1, supply for DAT4..DAT7
165 * - MMC2/MMC2, external level shifter voltage supply, for
166 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
167 */
168 struct regulator *vcc;
169 struct regulator *vcc_aux;
Balaji T Ke99448f2014-02-19 20:26:40 +0530170 struct regulator *pbias;
171 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100172 void __iomem *base;
173 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700174 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100175 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200176 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100177 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700178 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100179 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530180 u32 con;
181 u32 hctl;
182 u32 sysctl;
183 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100184 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100185 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100186 struct dma_chan *tx_chan;
187 struct dma_chan *rx_chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100188 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200189 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700190 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700191 int protect_card;
192 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800193 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700194 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530195 unsigned long clk_rate;
Per Forlin9782aff2011-07-01 18:55:23 +0200196 struct omap_hsmmc_next next_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100197 struct omap_mmc_platform_data *pdata;
198};
199
Nishanth Menon59445b12014-02-13 23:45:48 -0600200struct omap_mmc_of_data {
201 u32 reg_offset;
202 u8 controller_flags;
203};
204
Balaji T Kbf129e12014-01-21 19:54:42 +0530205static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
206
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800207static int omap_hsmmc_card_detect(struct device *dev, int slot)
208{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530209 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
210 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800211
212 /* NOTE: assumes card detect signal is active-low */
213 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
214}
215
216static int omap_hsmmc_get_wp(struct device *dev, int slot)
217{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530218 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
219 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800220
221 /* NOTE: assumes write protect signal is active-high */
222 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
223}
224
225static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
226{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530227 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
228 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800229
230 /* NOTE: assumes card detect signal is active-low */
231 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
232}
233
234#ifdef CONFIG_PM
235
236static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
237{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530238 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
239 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800240
241 disable_irq(mmc->slots[0].card_detect_irq);
242 return 0;
243}
244
245static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
246{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530247 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
248 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800249
250 enable_irq(mmc->slots[0].card_detect_irq);
251 return 0;
252}
253
254#else
255
256#define omap_hsmmc_suspend_cdirq NULL
257#define omap_hsmmc_resume_cdirq NULL
258
259#endif
260
Adrian Hunterb702b102010-02-15 10:03:35 -0800261#ifdef CONFIG_REGULATOR
262
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500263static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800264 int vdd)
265{
266 struct omap_hsmmc_host *host =
267 platform_get_drvdata(to_platform_device(dev));
268 int ret = 0;
269
270 /*
271 * If we don't see a Vcc regulator, assume it's a fixed
272 * voltage always-on regulator.
273 */
274 if (!host->vcc)
275 return 0;
276
277 if (mmc_slot(host).before_set_reg)
278 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
279
Balaji T Ke99448f2014-02-19 20:26:40 +0530280 if (host->pbias) {
281 if (host->pbias_enabled == 1) {
282 ret = regulator_disable(host->pbias);
283 if (!ret)
284 host->pbias_enabled = 0;
285 }
286 regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
287 }
288
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800289 /*
290 * Assume Vcc regulator is used only to power the card ... OMAP
291 * VDDS is used to power the pins, optionally with a transceiver to
292 * support cards using voltages other than VDDS (1.8V nominal). When a
293 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
294 *
295 * In some cases this regulator won't support enable/disable;
296 * e.g. it's a fixed rail for a WLAN chip.
297 *
298 * In other cases vcc_aux switches interface power. Example, for
299 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
300 * chips/cards need an interface voltage rail too.
301 */
302 if (power_on) {
Balaji T K987fd492014-02-19 20:26:40 +0530303 if (host->vcc)
304 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800305 /* Enable interface voltage rail, if needed */
306 if (ret == 0 && host->vcc_aux) {
307 ret = regulator_enable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530308 if (ret < 0 && host->vcc)
Linus Walleij99fc5132010-09-29 01:08:27 -0400309 ret = mmc_regulator_set_ocr(host->mmc,
310 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800311 }
312 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400313 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800314 if (host->vcc_aux)
315 ret = regulator_disable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530316 if (host->vcc) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400317 /* Then proceed to shut down the local regulator */
318 ret = mmc_regulator_set_ocr(host->mmc,
319 host->vcc, 0);
320 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800321 }
322
Balaji T Ke99448f2014-02-19 20:26:40 +0530323 if (host->pbias) {
324 if (vdd <= VDD_165_195)
325 ret = regulator_set_voltage(host->pbias, VDD_1V8,
326 VDD_1V8);
327 else
328 ret = regulator_set_voltage(host->pbias, VDD_3V0,
329 VDD_3V0);
330 if (ret < 0)
331 goto error_set_power;
332
333 if (host->pbias_enabled == 0) {
334 ret = regulator_enable(host->pbias);
335 if (!ret)
336 host->pbias_enabled = 1;
337 }
338 }
339
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800340 if (mmc_slot(host).after_set_reg)
341 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
342
Balaji T Ke99448f2014-02-19 20:26:40 +0530343error_set_power:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800344 return ret;
345}
346
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800347static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
348{
349 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700350 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800351
Balaji T Kf2ddc1d2014-02-19 20:26:40 +0530352 reg = devm_regulator_get(host->dev, "vmmc");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800353 if (IS_ERR(reg)) {
Balaji T K987fd492014-02-19 20:26:40 +0530354 dev_err(host->dev, "unable to get vmmc regulator %ld\n",
355 PTR_ERR(reg));
NeilBrown1fdc90f2012-08-08 00:06:00 -0400356 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800357 } else {
358 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700359 ocr_value = mmc_regulator_get_ocrmask(reg);
360 if (!mmc_slot(host).ocr_mask) {
361 mmc_slot(host).ocr_mask = ocr_value;
362 } else {
363 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530364 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500365 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700366 mmc_slot(host).ocr_mask = 0;
367 return -EINVAL;
368 }
369 }
Balaji T K987fd492014-02-19 20:26:40 +0530370 }
371 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800372
Balaji T K987fd492014-02-19 20:26:40 +0530373 /* Allow an aux regulator */
374 reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
375 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800376
Balaji T Ke99448f2014-02-19 20:26:40 +0530377 reg = devm_regulator_get_optional(host->dev, "pbias");
378 host->pbias = IS_ERR(reg) ? NULL : reg;
379
Balaji T K987fd492014-02-19 20:26:40 +0530380 /* For eMMC do not power off when not in sleep state */
381 if (mmc_slot(host).no_regulator_off_init)
382 return 0;
383 /*
384 * To disable boot_on regulator, enable regulator
385 * to increase usecount and then disable it.
386 */
387 if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
388 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
389 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
Adrian Huntere840ce12011-05-06 12:14:10 +0300390
Balaji T K987fd492014-02-19 20:26:40 +0530391 mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
392 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800393 }
394
395 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800396}
397
398static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
399{
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800400 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800401}
402
Adrian Hunterb702b102010-02-15 10:03:35 -0800403static inline int omap_hsmmc_have_reg(void)
404{
405 return 1;
406}
407
408#else
409
410static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
411{
412 return -EINVAL;
413}
414
415static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
416{
417}
418
419static inline int omap_hsmmc_have_reg(void)
420{
421 return 0;
422}
423
424#endif
425
426static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
427{
428 int ret;
429
430 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800431 if (pdata->slots[0].cover)
432 pdata->slots[0].get_cover_state =
433 omap_hsmmc_get_cover_state;
434 else
435 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
436 pdata->slots[0].card_detect_irq =
437 gpio_to_irq(pdata->slots[0].switch_pin);
438 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
439 if (ret)
440 return ret;
441 ret = gpio_direction_input(pdata->slots[0].switch_pin);
442 if (ret)
443 goto err_free_sp;
444 } else
445 pdata->slots[0].switch_pin = -EINVAL;
446
447 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
448 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
449 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
450 if (ret)
451 goto err_free_cd;
452 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
453 if (ret)
454 goto err_free_wp;
455 } else
456 pdata->slots[0].gpio_wp = -EINVAL;
457
458 return 0;
459
460err_free_wp:
461 gpio_free(pdata->slots[0].gpio_wp);
462err_free_cd:
463 if (gpio_is_valid(pdata->slots[0].switch_pin))
464err_free_sp:
465 gpio_free(pdata->slots[0].switch_pin);
466 return ret;
467}
468
469static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
470{
471 if (gpio_is_valid(pdata->slots[0].gpio_wp))
472 gpio_free(pdata->slots[0].gpio_wp);
473 if (gpio_is_valid(pdata->slots[0].switch_pin))
474 gpio_free(pdata->slots[0].switch_pin);
475}
476
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100477/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300478 * Start clock to the card
479 */
480static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
481{
482 OMAP_HSMMC_WRITE(host->base, SYSCTL,
483 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
484}
485
486/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100487 * Stop clock to the card
488 */
Denis Karpov70a33412009-09-22 16:44:59 -0700489static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100490{
491 OMAP_HSMMC_WRITE(host->base, SYSCTL,
492 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
493 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900494 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100495}
496
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700497static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
498 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700499{
500 unsigned int irq_mask;
501
502 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530503 irq_mask = INT_EN_MASK & ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700504 else
505 irq_mask = INT_EN_MASK;
506
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700507 /* Disable timeout for erases */
508 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530509 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700510
Adrian Hunterb4175772010-05-26 14:42:06 -0700511 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
512 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
513 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
514}
515
516static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
517{
518 OMAP_HSMMC_WRITE(host->base, ISE, 0);
519 OMAP_HSMMC_WRITE(host->base, IE, 0);
520 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
521}
522
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300523/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530524static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300525{
526 u16 dsor = 0;
527
528 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530529 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530530 if (dsor > CLKD_MAX)
531 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300532 }
533
534 return dsor;
535}
536
Andy Shevchenko5934df22011-05-06 12:14:06 +0300537static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
538{
539 struct mmc_ios *ios = &host->mmc->ios;
540 unsigned long regval;
541 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530542 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300543
Venkatraman S8986d312012-08-07 19:10:38 +0530544 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300545
546 omap_hsmmc_stop_clock(host);
547
548 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
549 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530550 clkdiv = calc_divisor(host, ios);
551 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300552 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
553 OMAP_HSMMC_WRITE(host->base, SYSCTL,
554 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
555
556 /* Wait till the ICS bit is set */
557 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
558 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
559 && time_before(jiffies, timeout))
560 cpu_relax();
561
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530562 /*
563 * Enable High-Speed Support
564 * Pre-Requisites
565 * - Controller should support High-Speed-Enable Bit
566 * - Controller should not be using DDR Mode
567 * - Controller should advertise that it supports High Speed
568 * in capabilities register
569 * - MMC/SD clock coming out of controller > 25MHz
570 */
571 if ((mmc_slot(host).features & HSMMC_HAS_HSPE_SUPPORT) &&
572 (ios->timing != MMC_TIMING_UHS_DDR50) &&
573 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
574 regval = OMAP_HSMMC_READ(host->base, HCTL);
575 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
576 regval |= HSPE;
577 else
578 regval &= ~HSPE;
579
580 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
581 }
582
Andy Shevchenko5934df22011-05-06 12:14:06 +0300583 omap_hsmmc_start_clock(host);
584}
585
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400586static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
587{
588 struct mmc_ios *ios = &host->mmc->ios;
589 u32 con;
590
591 con = OMAP_HSMMC_READ(host->base, CON);
Balaji T K03b5d922012-04-09 12:08:33 +0530592 if (ios->timing == MMC_TIMING_UHS_DDR50)
593 con |= DDR; /* configure in DDR mode */
594 else
595 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400596 switch (ios->bus_width) {
597 case MMC_BUS_WIDTH_8:
598 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
599 break;
600 case MMC_BUS_WIDTH_4:
601 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
602 OMAP_HSMMC_WRITE(host->base, HCTL,
603 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
604 break;
605 case MMC_BUS_WIDTH_1:
606 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
607 OMAP_HSMMC_WRITE(host->base, HCTL,
608 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
609 break;
610 }
611}
612
613static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
614{
615 struct mmc_ios *ios = &host->mmc->ios;
616 u32 con;
617
618 con = OMAP_HSMMC_READ(host->base, CON);
619 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
620 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
621 else
622 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
623}
624
Denis Karpov11dd62a2009-09-22 16:44:43 -0700625#ifdef CONFIG_PM
626
627/*
628 * Restore the MMC host context, if it was lost as result of a
629 * power state change.
630 */
Denis Karpov70a33412009-09-22 16:44:59 -0700631static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700632{
633 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400634 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700635 unsigned long timeout;
636
Tony Lindgren0a82e062013-10-21 00:25:19 +0530637 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
638 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
639 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
640 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
641 return 0;
642
643 host->context_loss++;
644
Balaji T Kc2200ef2012-03-07 09:55:30 -0500645 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700646 if (host->power_mode != MMC_POWER_OFF &&
647 (1 << ios->vdd) <= MMC_VDD_23_24)
648 hctl = SDVS18;
649 else
650 hctl = SDVS30;
651 capa = VS30 | VS18;
652 } else {
653 hctl = SDVS18;
654 capa = VS18;
655 }
656
657 OMAP_HSMMC_WRITE(host->base, HCTL,
658 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
659
660 OMAP_HSMMC_WRITE(host->base, CAPA,
661 OMAP_HSMMC_READ(host->base, CAPA) | capa);
662
663 OMAP_HSMMC_WRITE(host->base, HCTL,
664 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
665
666 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
667 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
668 && time_before(jiffies, timeout))
669 ;
670
Adrian Hunterb4175772010-05-26 14:42:06 -0700671 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700672
673 /* Do not initialize card-specific things if the power is off */
674 if (host->power_mode == MMC_POWER_OFF)
675 goto out;
676
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400677 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700678
Andy Shevchenko5934df22011-05-06 12:14:06 +0300679 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700680
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400681 omap_hsmmc_set_bus_mode(host);
682
Denis Karpov11dd62a2009-09-22 16:44:43 -0700683out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530684 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
685 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700686 return 0;
687}
688
689/*
690 * Save the MMC host context (store the number of power state changes so far).
691 */
Denis Karpov70a33412009-09-22 16:44:59 -0700692static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700693{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530694 host->con = OMAP_HSMMC_READ(host->base, CON);
695 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
696 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
697 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700698}
699
700#else
701
Denis Karpov70a33412009-09-22 16:44:59 -0700702static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700703{
704 return 0;
705}
706
Denis Karpov70a33412009-09-22 16:44:59 -0700707static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700708{
709}
710
711#endif
712
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100713/*
714 * Send init stream sequence to card
715 * before sending IDLE command
716 */
Denis Karpov70a33412009-09-22 16:44:59 -0700717static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100718{
719 int reg = 0;
720 unsigned long timeout;
721
Adrian Hunterb62f6222009-09-22 16:45:01 -0700722 if (host->protect_card)
723 return;
724
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100725 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700726
727 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100728 OMAP_HSMMC_WRITE(host->base, CON,
729 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
730 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
731
732 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530733 while ((reg != CC_EN) && time_before(jiffies, timeout))
734 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100735
736 OMAP_HSMMC_WRITE(host->base, CON,
737 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700738
739 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
740 OMAP_HSMMC_READ(host->base, STAT);
741
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100742 enable_irq(host->irq);
743}
744
745static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700746int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100747{
748 int r = 1;
749
Denis Karpov191d1f12009-09-22 16:44:55 -0700750 if (mmc_slot(host).get_cover_state)
751 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100752 return r;
753}
754
755static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700756omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100757 char *buf)
758{
759 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700760 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100761
Denis Karpov70a33412009-09-22 16:44:59 -0700762 return sprintf(buf, "%s\n",
763 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100764}
765
Denis Karpov70a33412009-09-22 16:44:59 -0700766static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100767
768static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700769omap_hsmmc_show_slot_name(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 Karpov191d1f12009-09-22 16:44:55 -0700775 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100776}
777
Denis Karpov70a33412009-09-22 16:44:59 -0700778static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100779
780/*
781 * Configure the response type and send the cmd.
782 */
783static void
Denis Karpov70a33412009-09-22 16:44:59 -0700784omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100785 struct mmc_data *data)
786{
787 int cmdreg = 0, resptype = 0, cmdtype = 0;
788
Venkatraman S8986d312012-08-07 19:10:38 +0530789 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100790 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
791 host->cmd = cmd;
792
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700793 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100794
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200795 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100796 if (cmd->flags & MMC_RSP_PRESENT) {
797 if (cmd->flags & MMC_RSP_136)
798 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200799 else if (cmd->flags & MMC_RSP_BUSY) {
800 resptype = 3;
801 host->response_busy = 1;
802 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100803 resptype = 2;
804 }
805
806 /*
807 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
808 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
809 * a val of 0x3, rest 0x0.
810 */
811 if (cmd == host->mrq->stop)
812 cmdtype = 0x3;
813
814 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
815
816 if (data) {
817 cmdreg |= DP_SELECT | MSBS | BCE;
818 if (data->flags & MMC_DATA_READ)
819 cmdreg |= DDIR;
820 else
821 cmdreg &= ~(DDIR);
822 }
823
824 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530825 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100826
Adrian Hunterb4175772010-05-26 14:42:06 -0700827 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700828
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100829 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
830 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
831}
832
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200833static int
Denis Karpov70a33412009-09-22 16:44:59 -0700834omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200835{
836 if (data->flags & MMC_DATA_WRITE)
837 return DMA_TO_DEVICE;
838 else
839 return DMA_FROM_DEVICE;
840}
841
Russell Kingc5c98922012-04-13 12:14:39 +0100842static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
843 struct mmc_data *data)
844{
845 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
846}
847
Adrian Hunterb4175772010-05-26 14:42:06 -0700848static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
849{
850 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530851 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700852
Venkatraman S31463b12012-04-09 12:08:34 +0530853 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700854 host->req_in_progress = 0;
855 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530856 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700857
858 omap_hsmmc_disable_irq(host);
859 /* Do not complete the request if DMA is still in progress */
860 if (mrq->data && host->use_dma && dma_ch != -1)
861 return;
862 host->mrq = NULL;
863 mmc_request_done(host->mmc, mrq);
864}
865
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100866/*
867 * Notify the transfer complete to MMC core
868 */
869static void
Denis Karpov70a33412009-09-22 16:44:59 -0700870omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100871{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200872 if (!data) {
873 struct mmc_request *mrq = host->mrq;
874
Adrian Hunter23050102009-09-22 16:44:57 -0700875 /* TC before CC from CMD6 - don't know why, but it happens */
876 if (host->cmd && host->cmd->opcode == 6 &&
877 host->response_busy) {
878 host->response_busy = 0;
879 return;
880 }
881
Adrian Hunterb4175772010-05-26 14:42:06 -0700882 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200883 return;
884 }
885
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100886 host->data = NULL;
887
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100888 if (!data->error)
889 data->bytes_xfered += data->blocks * (data->blksz);
890 else
891 data->bytes_xfered = 0;
892
Balaji T Kbf129e12014-01-21 19:54:42 +0530893 if (data->stop && (data->error || !host->mrq->sbc))
894 omap_hsmmc_start_command(host, data->stop, NULL);
895 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700896 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100897}
898
899/*
900 * Notify the core about command completion
901 */
902static void
Denis Karpov70a33412009-09-22 16:44:59 -0700903omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100904{
905 host->cmd = NULL;
906
Balaji T Kbf129e12014-01-21 19:54:42 +0530907 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
908 !host->mrq->sbc->error) {
909 omap_hsmmc_start_dma_transfer(host);
910 omap_hsmmc_start_command(host, host->mrq->cmd,
911 host->mrq->data);
912 return;
913 }
914
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100915 if (cmd->flags & MMC_RSP_PRESENT) {
916 if (cmd->flags & MMC_RSP_136) {
917 /* response type 2 */
918 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
919 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
920 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
921 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
922 } else {
923 /* response types 1, 1b, 3, 4, 5, 6 */
924 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
925 }
926 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700927 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530928 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100929}
930
931/*
932 * DMA clean up for command errors
933 */
Denis Karpov70a33412009-09-22 16:44:59 -0700934static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100935{
Adrian Hunterb4175772010-05-26 14:42:06 -0700936 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530937 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700938
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200939 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100940
Venkatraman S31463b12012-04-09 12:08:34 +0530941 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700942 dma_ch = host->dma_ch;
943 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530944 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700945
946 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100947 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
948
949 dmaengine_terminate_all(chan);
950 dma_unmap_sg(chan->device->dev,
951 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700952 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100953
Per Forlin053bf342011-11-07 21:55:11 +0530954 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100955 }
956 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100957}
958
959/*
960 * Readable error output
961 */
962#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300963static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100964{
965 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700966 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300967 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
968 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
969 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
970 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100971 };
972 char res[256];
973 char *buf = res;
974 int len, i;
975
976 len = sprintf(buf, "MMC IRQ 0x%x :", status);
977 buf += len;
978
Denis Karpov70a33412009-09-22 16:44:59 -0700979 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100980 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700981 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100982 buf += len;
983 }
984
Venkatraman S8986d312012-08-07 19:10:38 +0530985 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100986}
Adrian Hunter699b9582011-05-06 12:14:01 +0300987#else
988static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
989 u32 status)
990{
991}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100992#endif /* CONFIG_MMC_DEBUG */
993
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100994/*
995 * MMC controller internal state machines reset
996 *
997 * Used to reset command or data internal state machines, using respectively
998 * SRC or SRD bit of SYSCTL register
999 * Can be called from interrupt context
1000 */
Denis Karpov70a33412009-09-22 16:44:59 -07001001static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1002 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001003{
1004 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301005 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001006
1007 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1008 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1009
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001010 /*
1011 * OMAP4 ES2 and greater has an updated reset logic.
1012 * Monitor a 0->1 transition first
1013 */
1014 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001015 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001016 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301017 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001018 }
1019 i = 0;
1020
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001021 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1022 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301023 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001024
1025 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1026 dev_err(mmc_dev(host->mmc),
1027 "Timeout waiting on controller reset in %s\n",
1028 __func__);
1029}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001030
Balaji T K25e18972012-11-19 21:59:55 +05301031static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1032 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301033{
Balaji T K25e18972012-11-19 21:59:55 +05301034 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301035 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301036 if (host->cmd)
1037 host->cmd->error = err;
1038 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301039
1040 if (host->data) {
1041 omap_hsmmc_reset_controller_fsm(host, SRD);
1042 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301043 } else if (host->mrq && host->mrq->cmd)
1044 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301045}
1046
Adrian Hunterb4175772010-05-26 14:42:06 -07001047static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001048{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001049 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001050 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001051
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001052 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301053 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001054
Venkatraman Sa7e96872012-11-19 22:00:01 +05301055 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001056 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001057
Venkatraman Sa7e96872012-11-19 22:00:01 +05301058 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301059 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301060 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301061 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301062 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301063 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1064
Venkatraman Sae4bf782012-08-09 20:36:07 +05301065 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301066 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301067 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001068 }
1069 }
1070
Francesco Lavra7472bab2013-06-29 08:25:12 +02001071 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301072 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001073 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301074 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001075 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001076}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001077
Adrian Hunterb4175772010-05-26 14:42:06 -07001078/*
1079 * MMC controller IRQ handler
1080 */
1081static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1082{
1083 struct omap_hsmmc_host *host = dev_id;
1084 int status;
1085
1086 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301087 while (status & INT_EN_MASK && host->req_in_progress) {
Adrian Hunterb4175772010-05-26 14:42:06 -07001088 omap_hsmmc_do_irq(host, status);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301089
Adrian Hunterb4175772010-05-26 14:42:06 -07001090 /* Flush posted write */
1091 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301092 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001093
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001094 return IRQ_HANDLED;
1095}
1096
Denis Karpov70a33412009-09-22 16:44:59 -07001097static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001098{
1099 unsigned long i;
1100
1101 OMAP_HSMMC_WRITE(host->base, HCTL,
1102 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1103 for (i = 0; i < loops_per_jiffy; i++) {
1104 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1105 break;
1106 cpu_relax();
1107 }
1108}
1109
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001110/*
David Brownelleb250822009-02-17 14:49:01 -08001111 * Switch MMC interface voltage ... only relevant for MMC1.
1112 *
1113 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1114 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1115 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001116 */
Denis Karpov70a33412009-09-22 16:44:59 -07001117static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001118{
1119 u32 reg_val = 0;
1120 int ret;
1121
1122 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301123 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301124 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301125 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001126
1127 /* Turn the power off */
1128 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001129
1130 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001131 if (!ret)
1132 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1133 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301134 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301135 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301136 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001137
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001138 if (ret != 0)
1139 goto err;
1140
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001141 OMAP_HSMMC_WRITE(host->base, HCTL,
1142 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1143 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001144
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001145 /*
1146 * If a MMC dual voltage card is detected, the set_ios fn calls
1147 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001148 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001149 *
David Brownelleb250822009-02-17 14:49:01 -08001150 * Cope with a bit of slop in the range ... per data sheets:
1151 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1152 * but recommended values are 1.71V to 1.89V
1153 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1154 * but recommended values are 2.7V to 3.3V
1155 *
1156 * Board setup code shouldn't permit anything very out-of-range.
1157 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1158 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001159 */
David Brownelleb250822009-02-17 14:49:01 -08001160 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001161 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001162 else
1163 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001164
1165 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001166 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001167
1168 return 0;
1169err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301170 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001171 return ret;
1172}
1173
Adrian Hunterb62f6222009-09-22 16:45:01 -07001174/* Protect the card while the cover is open */
1175static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1176{
1177 if (!mmc_slot(host).get_cover_state)
1178 return;
1179
1180 host->reqs_blocked = 0;
1181 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1182 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301183 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001184 "card is now accessible\n",
1185 mmc_hostname(host->mmc));
1186 host->protect_card = 0;
1187 }
1188 } else {
1189 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301190 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001191 "card is now inaccessible\n",
1192 mmc_hostname(host->mmc));
1193 host->protect_card = 1;
1194 }
1195 }
1196}
1197
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001198/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001199 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001200 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001201static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001202{
NeilBrown7efab4f2011-12-30 12:35:13 +11001203 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001204 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001205 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001206
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001207 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001208
Denis Karpov191d1f12009-09-22 16:44:55 -07001209 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001210 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001211 else {
1212 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001213 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001214 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001215
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001216 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001217 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001218 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001219 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001220 return IRQ_HANDLED;
1221}
1222
Russell Kingc5c98922012-04-13 12:14:39 +01001223static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001224{
Russell Kingc5c98922012-04-13 12:14:39 +01001225 struct omap_hsmmc_host *host = param;
1226 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001227 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001228 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001229
Russell Kingc5c98922012-04-13 12:14:39 +01001230 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001231 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001232 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001233 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001234 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001235
Adrian Hunter770d7432011-05-06 12:14:11 +03001236 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001237 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001238 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001239 dma_unmap_sg(chan->device->dev,
1240 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001241 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001242
1243 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001244 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001245 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001246
1247 /* If DMA has finished after TC, complete the request */
1248 if (!req_in_progress) {
1249 struct mmc_request *mrq = host->mrq;
1250
1251 host->mrq = NULL;
1252 mmc_request_done(host->mmc, mrq);
1253 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001254}
1255
Per Forlin9782aff2011-07-01 18:55:23 +02001256static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1257 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001258 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001259 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001260{
1261 int dma_len;
1262
1263 if (!next && data->host_cookie &&
1264 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301265 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001266 " host->next_data.cookie %d\n",
1267 __func__, data->host_cookie, host->next_data.cookie);
1268 data->host_cookie = 0;
1269 }
1270
1271 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001272 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001273 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001274 omap_hsmmc_get_dma_dir(host, data));
1275
1276 } else {
1277 dma_len = host->next_data.dma_len;
1278 host->next_data.dma_len = 0;
1279 }
1280
1281
1282 if (dma_len == 0)
1283 return -EINVAL;
1284
1285 if (next) {
1286 next->dma_len = dma_len;
1287 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1288 } else
1289 host->dma_len = dma_len;
1290
1291 return 0;
1292}
1293
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001294/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001295 * Routine to configure and start DMA for the MMC card
1296 */
Balaji T K9d025332014-01-21 19:54:42 +05301297static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001298 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001299{
Russell King26b88522012-04-13 12:27:37 +01001300 struct dma_slave_config cfg;
1301 struct dma_async_tx_descriptor *tx;
1302 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001303 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001304 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001305
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001306 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001307 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001308 struct scatterlist *sgl;
1309
1310 sgl = data->sg + i;
1311 if (sgl->length % data->blksz)
1312 return -EINVAL;
1313 }
1314 if ((data->blksz % 4) != 0)
1315 /* REVISIT: The MMC buffer increments only when MSB is written.
1316 * Return error for blksz which is non multiple of four.
1317 */
1318 return -EINVAL;
1319
Adrian Hunterb4175772010-05-26 14:42:06 -07001320 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001321
Russell Kingc5c98922012-04-13 12:14:39 +01001322 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001323
Russell King26b88522012-04-13 12:27:37 +01001324 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1325 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1326 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1327 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1328 cfg.src_maxburst = data->blksz / 4;
1329 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001330
Russell King26b88522012-04-13 12:27:37 +01001331 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001332 if (ret)
1333 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001334
Russell King26b88522012-04-13 12:27:37 +01001335 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1336 if (ret)
1337 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001338
Russell King26b88522012-04-13 12:27:37 +01001339 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1340 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1341 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1342 if (!tx) {
1343 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1344 /* FIXME: cleanup */
1345 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001346 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001347
Russell King26b88522012-04-13 12:27:37 +01001348 tx->callback = omap_hsmmc_dma_callback;
1349 tx->callback_param = host;
1350
1351 /* Does not fail */
1352 dmaengine_submit(tx);
1353
1354 host->dma_ch = 1;
1355
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001356 return 0;
1357}
1358
Denis Karpov70a33412009-09-22 16:44:59 -07001359static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001360 unsigned int timeout_ns,
1361 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001362{
1363 unsigned int timeout, cycle_ns;
1364 uint32_t reg, clkd, dto = 0;
1365
1366 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1367 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1368 if (clkd == 0)
1369 clkd = 1;
1370
Balaji T K6e3076c2014-01-21 19:54:42 +05301371 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001372 timeout = timeout_ns / cycle_ns;
1373 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001374 if (timeout) {
1375 while ((timeout & 0x80000000) == 0) {
1376 dto += 1;
1377 timeout <<= 1;
1378 }
1379 dto = 31 - dto;
1380 timeout <<= 1;
1381 if (timeout && dto)
1382 dto += 1;
1383 if (dto >= 13)
1384 dto -= 13;
1385 else
1386 dto = 0;
1387 if (dto > 14)
1388 dto = 14;
1389 }
1390
1391 reg &= ~DTO_MASK;
1392 reg |= dto << DTO_SHIFT;
1393 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1394}
1395
Balaji T K9d025332014-01-21 19:54:42 +05301396static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1397{
1398 struct mmc_request *req = host->mrq;
1399 struct dma_chan *chan;
1400
1401 if (!req->data)
1402 return;
1403 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1404 | (req->data->blocks << 16));
1405 set_data_timeout(host, req->data->timeout_ns,
1406 req->data->timeout_clks);
1407 chan = omap_hsmmc_get_dma_chan(host, req->data);
1408 dma_async_issue_pending(chan);
1409}
1410
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001411/*
1412 * Configure block length for MMC/SD cards and initiate the transfer.
1413 */
1414static int
Denis Karpov70a33412009-09-22 16:44:59 -07001415omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001416{
1417 int ret;
1418 host->data = req->data;
1419
1420 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001421 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001422 /*
1423 * Set an arbitrary 100ms data timeout for commands with
1424 * busy signal.
1425 */
1426 if (req->cmd->flags & MMC_RSP_BUSY)
1427 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001428 return 0;
1429 }
1430
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001431 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301432 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001433 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301434 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001435 return ret;
1436 }
1437 }
1438 return 0;
1439}
1440
Per Forlin9782aff2011-07-01 18:55:23 +02001441static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1442 int err)
1443{
1444 struct omap_hsmmc_host *host = mmc_priv(mmc);
1445 struct mmc_data *data = mrq->data;
1446
Russell King26b88522012-04-13 12:27:37 +01001447 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001448 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001449
Russell King26b88522012-04-13 12:27:37 +01001450 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1451 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001452 data->host_cookie = 0;
1453 }
1454}
1455
1456static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1457 bool is_first_req)
1458{
1459 struct omap_hsmmc_host *host = mmc_priv(mmc);
1460
1461 if (mrq->data->host_cookie) {
1462 mrq->data->host_cookie = 0;
1463 return ;
1464 }
1465
Russell Kingc5c98922012-04-13 12:14:39 +01001466 if (host->use_dma) {
1467 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001468
Per Forlin9782aff2011-07-01 18:55:23 +02001469 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001470 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001471 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001472 }
Per Forlin9782aff2011-07-01 18:55:23 +02001473}
1474
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001475/*
1476 * Request function. for read/write operation
1477 */
Denis Karpov70a33412009-09-22 16:44:59 -07001478static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001479{
Denis Karpov70a33412009-09-22 16:44:59 -07001480 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001481 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001482
Adrian Hunterb4175772010-05-26 14:42:06 -07001483 BUG_ON(host->req_in_progress);
1484 BUG_ON(host->dma_ch != -1);
1485 if (host->protect_card) {
1486 if (host->reqs_blocked < 3) {
1487 /*
1488 * Ensure the controller is left in a consistent
1489 * state by resetting the command and data state
1490 * machines.
1491 */
1492 omap_hsmmc_reset_controller_fsm(host, SRD);
1493 omap_hsmmc_reset_controller_fsm(host, SRC);
1494 host->reqs_blocked += 1;
1495 }
1496 req->cmd->error = -EBADF;
1497 if (req->data)
1498 req->data->error = -EBADF;
1499 req->cmd->retries = 0;
1500 mmc_request_done(mmc, req);
1501 return;
1502 } else if (host->reqs_blocked)
1503 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001504 WARN_ON(host->mrq != NULL);
1505 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301506 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001507 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001508 if (err) {
1509 req->cmd->error = err;
1510 if (req->data)
1511 req->data->error = err;
1512 host->mrq = NULL;
1513 mmc_request_done(mmc, req);
1514 return;
1515 }
Balaji T Kbf129e12014-01-21 19:54:42 +05301516 if (req->sbc) {
1517 omap_hsmmc_start_command(host, req->sbc, NULL);
1518 return;
1519 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001520
Balaji T K9d025332014-01-21 19:54:42 +05301521 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001522 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001523}
1524
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001525/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001526static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001527{
Denis Karpov70a33412009-09-22 16:44:59 -07001528 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001529 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001530
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301531 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001532
Adrian Huntera3621462009-09-22 16:44:42 -07001533 if (ios->power_mode != host->power_mode) {
1534 switch (ios->power_mode) {
1535 case MMC_POWER_OFF:
1536 mmc_slot(host).set_power(host->dev, host->slot_id,
1537 0, 0);
1538 break;
1539 case MMC_POWER_UP:
1540 mmc_slot(host).set_power(host->dev, host->slot_id,
1541 1, ios->vdd);
1542 break;
1543 case MMC_POWER_ON:
1544 do_send_init_stream = 1;
1545 break;
1546 }
1547 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001548 }
1549
Denis Karpovdd498ef2009-09-22 16:44:49 -07001550 /* FIXME: set registers based only on changes to ios */
1551
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001552 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001553
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301554 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001555 /* Only MMC1 can interface at 3V without some flavor
1556 * of external transceiver; but they all handle 1.8V.
1557 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001558 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301559 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001560 /*
1561 * The mmc_select_voltage fn of the core does
1562 * not seem to set the power_mode to
1563 * MMC_POWER_UP upon recalculating the voltage.
1564 * vdd 1.8v.
1565 */
Denis Karpov70a33412009-09-22 16:44:59 -07001566 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1567 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001568 "Switch operation failed\n");
1569 }
1570 }
1571
Andy Shevchenko5934df22011-05-06 12:14:06 +03001572 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001573
Adrian Huntera3621462009-09-22 16:44:42 -07001574 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001575 send_init_stream(host);
1576
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001577 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001578
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301579 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001580}
1581
1582static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1583{
Denis Karpov70a33412009-09-22 16:44:59 -07001584 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001585
Denis Karpov191d1f12009-09-22 16:44:55 -07001586 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001587 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001588 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001589}
1590
1591static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1592{
Denis Karpov70a33412009-09-22 16:44:59 -07001593 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001594
Denis Karpov191d1f12009-09-22 16:44:55 -07001595 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001596 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001597 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001598}
1599
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001600static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1601{
1602 struct omap_hsmmc_host *host = mmc_priv(mmc);
1603
1604 if (mmc_slot(host).init_card)
1605 mmc_slot(host).init_card(card);
1606}
1607
Denis Karpov70a33412009-09-22 16:44:59 -07001608static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001609{
1610 u32 hctl, capa, value;
1611
1612 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301613 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001614 hctl = SDVS30;
1615 capa = VS30 | VS18;
1616 } else {
1617 hctl = SDVS18;
1618 capa = VS18;
1619 }
1620
1621 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1622 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1623
1624 value = OMAP_HSMMC_READ(host->base, CAPA);
1625 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1626
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001627 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001628 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001629}
1630
Denis Karpov70a33412009-09-22 16:44:59 -07001631static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001632{
Denis Karpov70a33412009-09-22 16:44:59 -07001633 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001634
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301635 pm_runtime_get_sync(host->dev);
1636
Denis Karpovdd498ef2009-09-22 16:44:49 -07001637 return 0;
1638}
1639
Adrian Hunter907d2e72012-02-29 09:17:21 +02001640static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001641{
Denis Karpov70a33412009-09-22 16:44:59 -07001642 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001643
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301644 pm_runtime_mark_last_busy(host->dev);
1645 pm_runtime_put_autosuspend(host->dev);
1646
Denis Karpovdd498ef2009-09-22 16:44:49 -07001647 return 0;
1648}
1649
Denis Karpov70a33412009-09-22 16:44:59 -07001650static const struct mmc_host_ops omap_hsmmc_ops = {
1651 .enable = omap_hsmmc_enable_fclk,
1652 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001653 .post_req = omap_hsmmc_post_req,
1654 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001655 .request = omap_hsmmc_request,
1656 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001657 .get_cd = omap_hsmmc_get_cd,
1658 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001659 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001660 /* NYET -- enable_sdio_irq */
1661};
1662
Denis Karpovd900f712009-09-22 16:44:38 -07001663#ifdef CONFIG_DEBUG_FS
1664
Denis Karpov70a33412009-09-22 16:44:59 -07001665static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001666{
1667 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001668 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001669
Tony Lindgren0a82e062013-10-21 00:25:19 +05301670 seq_printf(s, "mmc%d:\n ctx_loss:\t%d\n\nregs:\n",
1671 mmc->index, host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001672
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301673 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001674
Denis Karpovd900f712009-09-22 16:44:38 -07001675 seq_printf(s, "CON:\t\t0x%08x\n",
1676 OMAP_HSMMC_READ(host->base, CON));
1677 seq_printf(s, "HCTL:\t\t0x%08x\n",
1678 OMAP_HSMMC_READ(host->base, HCTL));
1679 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1680 OMAP_HSMMC_READ(host->base, SYSCTL));
1681 seq_printf(s, "IE:\t\t0x%08x\n",
1682 OMAP_HSMMC_READ(host->base, IE));
1683 seq_printf(s, "ISE:\t\t0x%08x\n",
1684 OMAP_HSMMC_READ(host->base, ISE));
1685 seq_printf(s, "CAPA:\t\t0x%08x\n",
1686 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001687
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301688 pm_runtime_mark_last_busy(host->dev);
1689 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001690
Denis Karpovd900f712009-09-22 16:44:38 -07001691 return 0;
1692}
1693
Denis Karpov70a33412009-09-22 16:44:59 -07001694static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001695{
Denis Karpov70a33412009-09-22 16:44:59 -07001696 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001697}
1698
1699static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001700 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001701 .read = seq_read,
1702 .llseek = seq_lseek,
1703 .release = single_release,
1704};
1705
Denis Karpov70a33412009-09-22 16:44:59 -07001706static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001707{
1708 if (mmc->debugfs_root)
1709 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1710 mmc, &mmc_regs_fops);
1711}
1712
1713#else
1714
Denis Karpov70a33412009-09-22 16:44:59 -07001715static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001716{
1717}
1718
1719#endif
1720
Rajendra Nayak46856a62012-03-12 20:32:37 +05301721#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001722static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1723 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1724 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1725};
1726
1727static const struct omap_mmc_of_data omap4_mmc_of_data = {
1728 .reg_offset = 0x100,
1729};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301730
1731static const struct of_device_id omap_mmc_of_match[] = {
1732 {
1733 .compatible = "ti,omap2-hsmmc",
1734 },
1735 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001736 .compatible = "ti,omap3-pre-es3-hsmmc",
1737 .data = &omap3_pre_es3_mmc_of_data,
1738 },
1739 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301740 .compatible = "ti,omap3-hsmmc",
1741 },
1742 {
1743 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001744 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301745 },
1746 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001747};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301748MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1749
1750static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1751{
1752 struct omap_mmc_platform_data *pdata;
1753 struct device_node *np = dev->of_node;
Daniel Mackd8714e82012-10-15 21:35:06 +05301754 u32 bus_width, max_freq;
Jan Luebbedc642c22013-01-30 10:07:17 +01001755 int cd_gpio, wp_gpio;
1756
1757 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
1758 wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1759 if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
1760 return ERR_PTR(-EPROBE_DEFER);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301761
1762 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1763 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301764 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301765
1766 if (of_find_property(np, "ti,dual-volt", NULL))
1767 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1768
1769 /* This driver only supports 1 slot */
1770 pdata->nr_slots = 1;
Jan Luebbedc642c22013-01-30 10:07:17 +01001771 pdata->slots[0].switch_pin = cd_gpio;
1772 pdata->slots[0].gpio_wp = wp_gpio;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301773
1774 if (of_find_property(np, "ti,non-removable", NULL)) {
1775 pdata->slots[0].nonremovable = true;
1776 pdata->slots[0].no_regulator_off_init = true;
1777 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001778 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301779 if (bus_width == 4)
1780 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1781 else if (bus_width == 8)
1782 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1783
1784 if (of_find_property(np, "ti,needs-special-reset", NULL))
1785 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1786
Daniel Mackd8714e82012-10-15 21:35:06 +05301787 if (!of_property_read_u32(np, "max-frequency", &max_freq))
1788 pdata->max_freq = max_freq;
1789
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301790 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
1791 pdata->slots[0].features |= HSMMC_HAS_HSPE_SUPPORT;
1792
Daniel Mackc9ae64d2014-02-17 12:36:33 +01001793 if (of_find_property(np, "keep-power-in-suspend", NULL))
1794 pdata->slots[0].pm_caps |= MMC_PM_KEEP_POWER;
1795
1796 if (of_find_property(np, "enable-sdio-wakeup", NULL))
1797 pdata->slots[0].pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
1798
Rajendra Nayak46856a62012-03-12 20:32:37 +05301799 return pdata;
1800}
1801#else
1802static inline struct omap_mmc_platform_data
1803 *of_get_hsmmc_pdata(struct device *dev)
1804{
Balaji T K19df45b2014-02-28 19:08:18 +05301805 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301806}
1807#endif
1808
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001809static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001810{
1811 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1812 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001813 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001814 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001815 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301816 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001817 dma_cap_mask_t mask;
1818 unsigned tx_req, rx_req;
Daniel Mack46b76032012-10-15 21:35:05 +05301819 struct pinctrl *pinctrl;
Nishanth Menon59445b12014-02-13 23:45:48 -06001820 const struct omap_mmc_of_data *data;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301821
1822 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1823 if (match) {
1824 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01001825
1826 if (IS_ERR(pdata))
1827 return PTR_ERR(pdata);
1828
Rajendra Nayak46856a62012-03-12 20:32:37 +05301829 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06001830 data = match->data;
1831 pdata->reg_offset = data->reg_offset;
1832 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301833 }
1834 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001835
1836 if (pdata == NULL) {
1837 dev_err(&pdev->dev, "Platform Data is missing\n");
1838 return -ENXIO;
1839 }
1840
1841 if (pdata->nr_slots == 0) {
1842 dev_err(&pdev->dev, "No Slots\n");
1843 return -ENXIO;
1844 }
1845
1846 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1847 irq = platform_get_irq(pdev, 0);
1848 if (res == NULL || irq < 0)
1849 return -ENXIO;
1850
Chris Ball984b2032011-03-22 16:34:42 -07001851 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001852 if (res == NULL)
1853 return -EBUSY;
1854
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001855 ret = omap_hsmmc_gpio_init(pdata);
1856 if (ret)
1857 goto err;
1858
Denis Karpov70a33412009-09-22 16:44:59 -07001859 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001860 if (!mmc) {
1861 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001862 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001863 }
1864
1865 host = mmc_priv(mmc);
1866 host->mmc = mmc;
1867 host->pdata = pdata;
1868 host->dev = &pdev->dev;
1869 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001870 host->dma_ch = -1;
1871 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001872 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05301873 host->mapbase = res->start + pdata->reg_offset;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001874 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001875 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001876 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05301877 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001878
1879 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001880
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301881 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001882
Daniel Mackd418ed82012-02-19 13:20:33 +01001883 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1884
1885 if (pdata->max_freq > 0)
1886 mmc->f_max = pdata->max_freq;
1887 else
1888 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001889
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001890 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001891
Russell King6f7607c2009-01-28 10:22:50 +00001892 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001893 if (IS_ERR(host->fclk)) {
1894 ret = PTR_ERR(host->fclk);
1895 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001896 goto err1;
1897 }
1898
Paul Walmsley9b682562011-10-06 14:50:35 -06001899 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1900 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1901 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1902 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001903
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301904 pm_runtime_enable(host->dev);
1905 pm_runtime_get_sync(host->dev);
1906 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1907 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001908
Balaji T K92a3aeb2012-02-24 21:14:34 +05301909 omap_hsmmc_context_save(host);
1910
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301911 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1912 /*
1913 * MMC can still work without debounce clock.
1914 */
1915 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301916 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05301917 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301918 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
1919 clk_put(host->dbclk);
1920 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07001921 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001922
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001923 /* Since we do only SG emulation, we can have as many segs
1924 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001925 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001926
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001927 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1928 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1929 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1930 mmc->max_seg_size = mmc->max_req_size;
1931
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001932 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001933 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001934
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001935 mmc->caps |= mmc_slot(host).caps;
1936 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001937 mmc->caps |= MMC_CAP_4_BIT_DATA;
1938
Denis Karpov191d1f12009-09-22 16:44:55 -07001939 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001940 mmc->caps |= MMC_CAP_NONREMOVABLE;
1941
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001942 mmc->pm_caps = mmc_slot(host).pm_caps;
1943
Denis Karpov70a33412009-09-22 16:44:59 -07001944 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001945
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05301946 if (!pdev->dev.of_node) {
1947 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1948 if (!res) {
1949 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
1950 ret = -ENXIO;
1951 goto err_irq;
1952 }
1953 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001954
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05301955 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1956 if (!res) {
1957 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
1958 ret = -ENXIO;
1959 goto err_irq;
1960 }
1961 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001962 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001963
Russell King26b88522012-04-13 12:27:37 +01001964 dma_cap_zero(mask);
1965 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01001966
Matt Porterd272fbf2013-05-10 17:42:34 +05301967 host->rx_chan =
1968 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
1969 &rx_req, &pdev->dev, "rx");
1970
Russell King26b88522012-04-13 12:27:37 +01001971 if (!host->rx_chan) {
1972 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001973 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001974 goto err_irq;
1975 }
1976
Matt Porterd272fbf2013-05-10 17:42:34 +05301977 host->tx_chan =
1978 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
1979 &tx_req, &pdev->dev, "tx");
1980
Russell King26b88522012-04-13 12:27:37 +01001981 if (!host->tx_chan) {
1982 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001983 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001984 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01001985 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001986
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001987 /* Request IRQ for MMC operations */
Yong Zhangd9618e92011-09-22 16:59:04 +08001988 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001989 mmc_hostname(mmc), host);
1990 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301991 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001992 goto err_irq;
1993 }
1994
1995 if (pdata->init != NULL) {
1996 if (pdata->init(&pdev->dev) != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301997 dev_err(mmc_dev(host->mmc),
Denis Karpov70a33412009-09-22 16:44:59 -07001998 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001999 goto err_irq_cd_init;
2000 }
2001 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002002
Adrian Hunterb702b102010-02-15 10:03:35 -08002003 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002004 ret = omap_hsmmc_reg_get(host);
2005 if (ret)
2006 goto err_reg;
2007 host->use_reg = 1;
2008 }
2009
David Brownellb583f262009-05-28 14:04:03 -07002010 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002011
2012 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02002013 if ((mmc_slot(host).card_detect_irq)) {
NeilBrown7efab4f2011-12-30 12:35:13 +11002014 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
2015 NULL,
2016 omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08002017 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11002018 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002019 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302020 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002021 "Unable to grab MMC CD IRQ\n");
2022 goto err_irq_cd;
2023 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00002024 pdata->suspend = omap_hsmmc_suspend_cdirq;
2025 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002026 }
2027
Adrian Hunterb4175772010-05-26 14:42:06 -07002028 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002029
Daniel Mack46b76032012-10-15 21:35:05 +05302030 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
2031 if (IS_ERR(pinctrl))
2032 dev_warn(&pdev->dev,
2033 "pins are not configured from the driver\n");
2034
Adrian Hunterb62f6222009-09-22 16:45:01 -07002035 omap_hsmmc_protect_card(host);
2036
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002037 mmc_add_host(mmc);
2038
Denis Karpov191d1f12009-09-22 16:44:55 -07002039 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002040 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2041 if (ret < 0)
2042 goto err_slot_name;
2043 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002044 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002045 ret = device_create_file(&mmc->class_dev,
2046 &dev_attr_cover_switch);
2047 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002048 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002049 }
2050
Denis Karpov70a33412009-09-22 16:44:59 -07002051 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302052 pm_runtime_mark_last_busy(host->dev);
2053 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002054
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002055 return 0;
2056
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002057err_slot_name:
2058 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002059 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002060err_irq_cd:
2061 if (host->use_reg)
2062 omap_hsmmc_reg_put(host);
2063err_reg:
2064 if (host->pdata->cleanup)
2065 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002066err_irq_cd_init:
2067 free_irq(host->irq, host);
2068err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002069 if (host->tx_chan)
2070 dma_release_channel(host->tx_chan);
2071 if (host->rx_chan)
2072 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302073 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002074 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002075 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302076 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302077 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002078 clk_put(host->dbclk);
2079 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002080err1:
2081 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002082 mmc_free_host(mmc);
2083err_alloc:
2084 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002085err:
Russell King48b332f2012-04-18 11:11:57 +01002086 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2087 if (res)
2088 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002089 return ret;
2090}
2091
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002092static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002093{
Denis Karpov70a33412009-09-22 16:44:59 -07002094 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002095 struct resource *res;
2096
Felipe Balbi927ce942012-03-14 11:18:27 +02002097 pm_runtime_get_sync(host->dev);
2098 mmc_remove_host(host->mmc);
2099 if (host->use_reg)
2100 omap_hsmmc_reg_put(host);
2101 if (host->pdata->cleanup)
2102 host->pdata->cleanup(&pdev->dev);
2103 free_irq(host->irq, host);
2104 if (mmc_slot(host).card_detect_irq)
2105 free_irq(mmc_slot(host).card_detect_irq, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002106
Russell Kingc5c98922012-04-13 12:14:39 +01002107 if (host->tx_chan)
2108 dma_release_channel(host->tx_chan);
2109 if (host->rx_chan)
2110 dma_release_channel(host->rx_chan);
2111
Felipe Balbi927ce942012-03-14 11:18:27 +02002112 pm_runtime_put_sync(host->dev);
2113 pm_runtime_disable(host->dev);
2114 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302115 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302116 clk_disable_unprepare(host->dbclk);
Felipe Balbi927ce942012-03-14 11:18:27 +02002117 clk_put(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002118 }
2119
Balaji T K9ea28ec2012-10-15 21:35:08 +05302120 omap_hsmmc_gpio_free(host->pdata);
Felipe Balbi927ce942012-03-14 11:18:27 +02002121 iounmap(host->base);
Balaji T K9d1f0282012-10-15 21:35:07 +05302122 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002123
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002124 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2125 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002126 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002127
2128 return 0;
2129}
2130
2131#ifdef CONFIG_PM
Felipe Balbia48ce882012-11-19 21:59:59 +05302132static int omap_hsmmc_prepare(struct device *dev)
2133{
2134 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2135
2136 if (host->pdata->suspend)
2137 return host->pdata->suspend(dev, host->slot_id);
2138
2139 return 0;
2140}
2141
2142static void omap_hsmmc_complete(struct device *dev)
2143{
2144 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2145
2146 if (host->pdata->resume)
2147 host->pdata->resume(dev, host->slot_id);
2148
2149}
2150
Kevin Hilmana791daa2010-05-26 14:42:07 -07002151static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002152{
Felipe Balbi927ce942012-03-14 11:18:27 +02002153 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2154
2155 if (!host)
2156 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002157
Felipe Balbi927ce942012-03-14 11:18:27 +02002158 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002159
2160 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2161 omap_hsmmc_disable_irq(host);
2162 OMAP_HSMMC_WRITE(host->base, HCTL,
2163 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2164 }
2165
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302166 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302167 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002168
Eliad Peller31f9d462011-11-22 16:02:17 +02002169 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002170 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002171}
2172
2173/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002174static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002175{
Felipe Balbi927ce942012-03-14 11:18:27 +02002176 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2177
2178 if (!host)
2179 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002180
Felipe Balbi927ce942012-03-14 11:18:27 +02002181 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002182
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302183 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302184 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002185
Felipe Balbi927ce942012-03-14 11:18:27 +02002186 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2187 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002188
Felipe Balbi927ce942012-03-14 11:18:27 +02002189 omap_hsmmc_protect_card(host);
2190
Felipe Balbi927ce942012-03-14 11:18:27 +02002191 pm_runtime_mark_last_busy(host->dev);
2192 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002193 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002194}
2195
2196#else
Felipe Balbia48ce882012-11-19 21:59:59 +05302197#define omap_hsmmc_prepare NULL
2198#define omap_hsmmc_complete NULL
Denis Karpov70a33412009-09-22 16:44:59 -07002199#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302200#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002201#endif
2202
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302203static int omap_hsmmc_runtime_suspend(struct device *dev)
2204{
2205 struct omap_hsmmc_host *host;
2206
2207 host = platform_get_drvdata(to_platform_device(dev));
2208 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002209 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302210
2211 return 0;
2212}
2213
2214static int omap_hsmmc_runtime_resume(struct device *dev)
2215{
2216 struct omap_hsmmc_host *host;
2217
2218 host = platform_get_drvdata(to_platform_device(dev));
2219 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002220 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302221
2222 return 0;
2223}
2224
Kevin Hilmana791daa2010-05-26 14:42:07 -07002225static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002226 .suspend = omap_hsmmc_suspend,
2227 .resume = omap_hsmmc_resume,
Felipe Balbia48ce882012-11-19 21:59:59 +05302228 .prepare = omap_hsmmc_prepare,
2229 .complete = omap_hsmmc_complete,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302230 .runtime_suspend = omap_hsmmc_runtime_suspend,
2231 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002232};
2233
2234static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002235 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002236 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002237 .driver = {
2238 .name = DRIVER_NAME,
2239 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002240 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302241 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002242 },
2243};
2244
Felipe Balbib7964502012-03-14 11:18:32 +02002245module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002246MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2247MODULE_LICENSE("GPL");
2248MODULE_ALIAS("platform:" DRIVER_NAME);
2249MODULE_AUTHOR("Texas Instruments Inc");