blob: b1ac26a7630688853cdbf00275579a32840f6ec9 [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)
100#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100101
Venkatraman Sa7e96872012-11-19 22:00:01 +0530102/* Interrupt masks for IE and ISE register */
103#define CC_EN (1 << 0)
104#define TC_EN (1 << 1)
105#define BWR_EN (1 << 4)
106#define BRR_EN (1 << 5)
107#define ERR_EN (1 << 15)
108#define CTO_EN (1 << 16)
109#define CCRC_EN (1 << 17)
110#define CEB_EN (1 << 18)
111#define CIE_EN (1 << 19)
112#define DTO_EN (1 << 20)
113#define DCRC_EN (1 << 21)
114#define DEB_EN (1 << 22)
115#define CERR_EN (1 << 28)
116#define BADA_EN (1 << 29)
117
118#define INT_EN_MASK (BADA_EN | CERR_EN | DEB_EN | DCRC_EN |\
119 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
120 BRR_EN | BWR_EN | TC_EN | CC_EN)
121
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530122#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530123#define MMC_TIMEOUT_MS 20 /* 20 mSec */
124#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400125#define OMAP_MMC_MIN_CLOCK 400000
126#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530127#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100128
129/*
130 * One controller can have multiple slots, like on some omap boards using
131 * omap.c controller driver. Luckily this is not currently done on any known
132 * omap_hsmmc.c device.
133 */
134#define mmc_slot(host) (host->pdata->slots[host->slot_id])
135
136/*
137 * MMC Host controller read/write API's
138 */
139#define OMAP_HSMMC_READ(base, reg) \
140 __raw_readl((base) + OMAP_HSMMC_##reg)
141
142#define OMAP_HSMMC_WRITE(base, reg, val) \
143 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
144
Per Forlin9782aff2011-07-01 18:55:23 +0200145struct omap_hsmmc_next {
146 unsigned int dma_len;
147 s32 cookie;
148};
149
Denis Karpov70a33412009-09-22 16:44:59 -0700150struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100151 struct device *dev;
152 struct mmc_host *mmc;
153 struct mmc_request *mrq;
154 struct mmc_command *cmd;
155 struct mmc_data *data;
156 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100157 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800158 /*
159 * vcc == configured supply
160 * vcc_aux == optional
161 * - MMC1, supply for DAT4..DAT7
162 * - MMC2/MMC2, external level shifter voltage supply, for
163 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
164 */
165 struct regulator *vcc;
166 struct regulator *vcc_aux;
Tony Lindgrencf5ae402013-05-10 17:42:33 +0530167 int pbias_disable;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100168 void __iomem *base;
169 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700170 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100171 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200172 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100173 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700174 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100175 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530176 u32 con;
177 u32 hctl;
178 u32 sysctl;
179 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100180 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100181 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100182 struct dma_chan *tx_chan;
183 struct dma_chan *rx_chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100184 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200185 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700186 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700187 int protect_card;
188 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800189 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700190 int req_in_progress;
Per Forlin9782aff2011-07-01 18:55:23 +0200191 struct omap_hsmmc_next next_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100192 struct omap_mmc_platform_data *pdata;
193};
194
Nishanth Menon59445b12014-02-13 23:45:48 -0600195struct omap_mmc_of_data {
196 u32 reg_offset;
197 u8 controller_flags;
198};
199
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800200static int omap_hsmmc_card_detect(struct device *dev, int slot)
201{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530202 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
203 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800204
205 /* NOTE: assumes card detect signal is active-low */
206 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
207}
208
209static int omap_hsmmc_get_wp(struct device *dev, int slot)
210{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530211 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
212 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800213
214 /* NOTE: assumes write protect signal is active-high */
215 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
216}
217
218static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
219{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530220 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
221 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800222
223 /* NOTE: assumes card detect signal is active-low */
224 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
225}
226
227#ifdef CONFIG_PM
228
229static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
230{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530231 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
232 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800233
234 disable_irq(mmc->slots[0].card_detect_irq);
235 return 0;
236}
237
238static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
239{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530240 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
241 struct omap_mmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800242
243 enable_irq(mmc->slots[0].card_detect_irq);
244 return 0;
245}
246
247#else
248
249#define omap_hsmmc_suspend_cdirq NULL
250#define omap_hsmmc_resume_cdirq NULL
251
252#endif
253
Adrian Hunterb702b102010-02-15 10:03:35 -0800254#ifdef CONFIG_REGULATOR
255
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500256static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800257 int vdd)
258{
259 struct omap_hsmmc_host *host =
260 platform_get_drvdata(to_platform_device(dev));
261 int ret = 0;
262
263 /*
264 * If we don't see a Vcc regulator, assume it's a fixed
265 * voltage always-on regulator.
266 */
267 if (!host->vcc)
268 return 0;
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530269 /*
Tony Lindgrencf5ae402013-05-10 17:42:33 +0530270 * With DT, never turn OFF the regulator for MMC1. This is because
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530271 * the pbias cell programming support is still missing when
272 * booting with Device tree
273 */
Tony Lindgrencf5ae402013-05-10 17:42:33 +0530274 if (host->pbias_disable && !vdd)
Rajendra Nayak1f84b712012-03-12 20:32:38 +0530275 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800276
277 if (mmc_slot(host).before_set_reg)
278 mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
279
280 /*
281 * Assume Vcc regulator is used only to power the card ... OMAP
282 * VDDS is used to power the pins, optionally with a transceiver to
283 * support cards using voltages other than VDDS (1.8V nominal). When a
284 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
285 *
286 * In some cases this regulator won't support enable/disable;
287 * e.g. it's a fixed rail for a WLAN chip.
288 *
289 * In other cases vcc_aux switches interface power. Example, for
290 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
291 * chips/cards need an interface voltage rail too.
292 */
293 if (power_on) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400294 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800295 /* Enable interface voltage rail, if needed */
296 if (ret == 0 && host->vcc_aux) {
297 ret = regulator_enable(host->vcc_aux);
298 if (ret < 0)
Linus Walleij99fc5132010-09-29 01:08:27 -0400299 ret = mmc_regulator_set_ocr(host->mmc,
300 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800301 }
302 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400303 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800304 if (host->vcc_aux)
305 ret = regulator_disable(host->vcc_aux);
Linus Walleij99fc5132010-09-29 01:08:27 -0400306 if (!ret) {
307 /* Then proceed to shut down the local regulator */
308 ret = mmc_regulator_set_ocr(host->mmc,
309 host->vcc, 0);
310 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800311 }
312
313 if (mmc_slot(host).after_set_reg)
314 mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
315
316 return ret;
317}
318
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800319static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
320{
321 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700322 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800323
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800324 reg = regulator_get(host->dev, "vmmc");
325 if (IS_ERR(reg)) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +0530326 dev_err(host->dev, "vmmc regulator missing\n");
NeilBrown1fdc90f2012-08-08 00:06:00 -0400327 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800328 } else {
NeilBrown1fdc90f2012-08-08 00:06:00 -0400329 mmc_slot(host).set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800330 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700331 ocr_value = mmc_regulator_get_ocrmask(reg);
332 if (!mmc_slot(host).ocr_mask) {
333 mmc_slot(host).ocr_mask = ocr_value;
334 } else {
335 if (!(mmc_slot(host).ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530336 dev_err(host->dev, "ocrmask %x is not supported\n",
Rajendra Nayake3f1adb2012-03-07 09:55:31 -0500337 mmc_slot(host).ocr_mask);
kishore kadiyala64be9782010-10-01 16:35:28 -0700338 mmc_slot(host).ocr_mask = 0;
339 return -EINVAL;
340 }
341 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800342
343 /* Allow an aux regulator */
344 reg = regulator_get(host->dev, "vmmc_aux");
345 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
346
Balaji T Kb1c1df72011-05-30 19:55:34 +0530347 /* For eMMC do not power off when not in sleep state */
348 if (mmc_slot(host).no_regulator_off_init)
349 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800350 /*
351 * UGLY HACK: workaround regulator framework bugs.
352 * When the bootloader leaves a supply active, it's
353 * initialized with zero usecount ... and we can't
354 * disable it without first enabling it. Until the
355 * framework is fixed, we need a workaround like this
356 * (which is safe for MMC, but not in general).
357 */
Adrian Huntere840ce12011-05-06 12:14:10 +0300358 if (regulator_is_enabled(host->vcc) > 0 ||
359 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
360 int vdd = ffs(mmc_slot(host).ocr_mask) - 1;
361
362 mmc_slot(host).set_power(host->dev, host->slot_id,
363 1, vdd);
364 mmc_slot(host).set_power(host->dev, host->slot_id,
365 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800366 }
367 }
368
369 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800370}
371
372static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
373{
374 regulator_put(host->vcc);
375 regulator_put(host->vcc_aux);
376 mmc_slot(host).set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800377}
378
Adrian Hunterb702b102010-02-15 10:03:35 -0800379static inline int omap_hsmmc_have_reg(void)
380{
381 return 1;
382}
383
384#else
385
386static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
387{
388 return -EINVAL;
389}
390
391static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
392{
393}
394
395static inline int omap_hsmmc_have_reg(void)
396{
397 return 0;
398}
399
400#endif
401
402static int omap_hsmmc_gpio_init(struct omap_mmc_platform_data *pdata)
403{
404 int ret;
405
406 if (gpio_is_valid(pdata->slots[0].switch_pin)) {
Adrian Hunterb702b102010-02-15 10:03:35 -0800407 if (pdata->slots[0].cover)
408 pdata->slots[0].get_cover_state =
409 omap_hsmmc_get_cover_state;
410 else
411 pdata->slots[0].card_detect = omap_hsmmc_card_detect;
412 pdata->slots[0].card_detect_irq =
413 gpio_to_irq(pdata->slots[0].switch_pin);
414 ret = gpio_request(pdata->slots[0].switch_pin, "mmc_cd");
415 if (ret)
416 return ret;
417 ret = gpio_direction_input(pdata->slots[0].switch_pin);
418 if (ret)
419 goto err_free_sp;
420 } else
421 pdata->slots[0].switch_pin = -EINVAL;
422
423 if (gpio_is_valid(pdata->slots[0].gpio_wp)) {
424 pdata->slots[0].get_ro = omap_hsmmc_get_wp;
425 ret = gpio_request(pdata->slots[0].gpio_wp, "mmc_wp");
426 if (ret)
427 goto err_free_cd;
428 ret = gpio_direction_input(pdata->slots[0].gpio_wp);
429 if (ret)
430 goto err_free_wp;
431 } else
432 pdata->slots[0].gpio_wp = -EINVAL;
433
434 return 0;
435
436err_free_wp:
437 gpio_free(pdata->slots[0].gpio_wp);
438err_free_cd:
439 if (gpio_is_valid(pdata->slots[0].switch_pin))
440err_free_sp:
441 gpio_free(pdata->slots[0].switch_pin);
442 return ret;
443}
444
445static void omap_hsmmc_gpio_free(struct omap_mmc_platform_data *pdata)
446{
447 if (gpio_is_valid(pdata->slots[0].gpio_wp))
448 gpio_free(pdata->slots[0].gpio_wp);
449 if (gpio_is_valid(pdata->slots[0].switch_pin))
450 gpio_free(pdata->slots[0].switch_pin);
451}
452
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100453/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300454 * Start clock to the card
455 */
456static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
457{
458 OMAP_HSMMC_WRITE(host->base, SYSCTL,
459 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
460}
461
462/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100463 * Stop clock to the card
464 */
Denis Karpov70a33412009-09-22 16:44:59 -0700465static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100466{
467 OMAP_HSMMC_WRITE(host->base, SYSCTL,
468 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
469 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900470 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100471}
472
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700473static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
474 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700475{
476 unsigned int irq_mask;
477
478 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530479 irq_mask = INT_EN_MASK & ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700480 else
481 irq_mask = INT_EN_MASK;
482
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700483 /* Disable timeout for erases */
484 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530485 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700486
Adrian Hunterb4175772010-05-26 14:42:06 -0700487 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
488 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
489 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
490}
491
492static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
493{
494 OMAP_HSMMC_WRITE(host->base, ISE, 0);
495 OMAP_HSMMC_WRITE(host->base, IE, 0);
496 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
497}
498
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300499/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530500static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300501{
502 u16 dsor = 0;
503
504 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530505 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530506 if (dsor > CLKD_MAX)
507 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300508 }
509
510 return dsor;
511}
512
Andy Shevchenko5934df22011-05-06 12:14:06 +0300513static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
514{
515 struct mmc_ios *ios = &host->mmc->ios;
516 unsigned long regval;
517 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530518 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300519
Venkatraman S8986d312012-08-07 19:10:38 +0530520 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300521
522 omap_hsmmc_stop_clock(host);
523
524 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
525 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530526 clkdiv = calc_divisor(host, ios);
527 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300528 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
529 OMAP_HSMMC_WRITE(host->base, SYSCTL,
530 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
531
532 /* Wait till the ICS bit is set */
533 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
534 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
535 && time_before(jiffies, timeout))
536 cpu_relax();
537
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530538 /*
539 * Enable High-Speed Support
540 * Pre-Requisites
541 * - Controller should support High-Speed-Enable Bit
542 * - Controller should not be using DDR Mode
543 * - Controller should advertise that it supports High Speed
544 * in capabilities register
545 * - MMC/SD clock coming out of controller > 25MHz
546 */
547 if ((mmc_slot(host).features & HSMMC_HAS_HSPE_SUPPORT) &&
548 (ios->timing != MMC_TIMING_UHS_DDR50) &&
549 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
550 regval = OMAP_HSMMC_READ(host->base, HCTL);
551 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
552 regval |= HSPE;
553 else
554 regval &= ~HSPE;
555
556 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
557 }
558
Andy Shevchenko5934df22011-05-06 12:14:06 +0300559 omap_hsmmc_start_clock(host);
560}
561
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400562static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
563{
564 struct mmc_ios *ios = &host->mmc->ios;
565 u32 con;
566
567 con = OMAP_HSMMC_READ(host->base, CON);
Balaji T K03b5d922012-04-09 12:08:33 +0530568 if (ios->timing == MMC_TIMING_UHS_DDR50)
569 con |= DDR; /* configure in DDR mode */
570 else
571 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400572 switch (ios->bus_width) {
573 case MMC_BUS_WIDTH_8:
574 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
575 break;
576 case MMC_BUS_WIDTH_4:
577 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
578 OMAP_HSMMC_WRITE(host->base, HCTL,
579 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
580 break;
581 case MMC_BUS_WIDTH_1:
582 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
583 OMAP_HSMMC_WRITE(host->base, HCTL,
584 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
585 break;
586 }
587}
588
589static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
590{
591 struct mmc_ios *ios = &host->mmc->ios;
592 u32 con;
593
594 con = OMAP_HSMMC_READ(host->base, CON);
595 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
596 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
597 else
598 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
599}
600
Denis Karpov11dd62a2009-09-22 16:44:43 -0700601#ifdef CONFIG_PM
602
603/*
604 * Restore the MMC host context, if it was lost as result of a
605 * power state change.
606 */
Denis Karpov70a33412009-09-22 16:44:59 -0700607static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700608{
609 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400610 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700611 unsigned long timeout;
612
Venkatraman S6c31b212012-08-08 14:26:52 +0530613 if (!OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE)
614 return 1;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700615
Tony Lindgren0a82e062013-10-21 00:25:19 +0530616 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
617 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
618 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
619 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
620 return 0;
621
622 host->context_loss++;
623
Balaji T Kc2200ef2012-03-07 09:55:30 -0500624 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700625 if (host->power_mode != MMC_POWER_OFF &&
626 (1 << ios->vdd) <= MMC_VDD_23_24)
627 hctl = SDVS18;
628 else
629 hctl = SDVS30;
630 capa = VS30 | VS18;
631 } else {
632 hctl = SDVS18;
633 capa = VS18;
634 }
635
636 OMAP_HSMMC_WRITE(host->base, HCTL,
637 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
638
639 OMAP_HSMMC_WRITE(host->base, CAPA,
640 OMAP_HSMMC_READ(host->base, CAPA) | capa);
641
642 OMAP_HSMMC_WRITE(host->base, HCTL,
643 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
644
645 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
646 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
647 && time_before(jiffies, timeout))
648 ;
649
Adrian Hunterb4175772010-05-26 14:42:06 -0700650 omap_hsmmc_disable_irq(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700651
652 /* Do not initialize card-specific things if the power is off */
653 if (host->power_mode == MMC_POWER_OFF)
654 goto out;
655
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400656 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700657
Andy Shevchenko5934df22011-05-06 12:14:06 +0300658 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700659
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400660 omap_hsmmc_set_bus_mode(host);
661
Denis Karpov11dd62a2009-09-22 16:44:43 -0700662out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530663 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
664 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700665 return 0;
666}
667
668/*
669 * Save the MMC host context (store the number of power state changes so far).
670 */
Denis Karpov70a33412009-09-22 16:44:59 -0700671static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700672{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530673 host->con = OMAP_HSMMC_READ(host->base, CON);
674 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
675 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
676 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700677}
678
679#else
680
Denis Karpov70a33412009-09-22 16:44:59 -0700681static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700682{
683 return 0;
684}
685
Denis Karpov70a33412009-09-22 16:44:59 -0700686static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700687{
688}
689
690#endif
691
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100692/*
693 * Send init stream sequence to card
694 * before sending IDLE command
695 */
Denis Karpov70a33412009-09-22 16:44:59 -0700696static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100697{
698 int reg = 0;
699 unsigned long timeout;
700
Adrian Hunterb62f6222009-09-22 16:45:01 -0700701 if (host->protect_card)
702 return;
703
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100704 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700705
706 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100707 OMAP_HSMMC_WRITE(host->base, CON,
708 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
709 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
710
711 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530712 while ((reg != CC_EN) && time_before(jiffies, timeout))
713 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100714
715 OMAP_HSMMC_WRITE(host->base, CON,
716 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700717
718 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
719 OMAP_HSMMC_READ(host->base, STAT);
720
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100721 enable_irq(host->irq);
722}
723
724static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700725int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100726{
727 int r = 1;
728
Denis Karpov191d1f12009-09-22 16:44:55 -0700729 if (mmc_slot(host).get_cover_state)
730 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100731 return r;
732}
733
734static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700735omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100736 char *buf)
737{
738 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700739 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100740
Denis Karpov70a33412009-09-22 16:44:59 -0700741 return sprintf(buf, "%s\n",
742 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100743}
744
Denis Karpov70a33412009-09-22 16:44:59 -0700745static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100746
747static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700748omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100749 char *buf)
750{
751 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700752 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100753
Denis Karpov191d1f12009-09-22 16:44:55 -0700754 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100755}
756
Denis Karpov70a33412009-09-22 16:44:59 -0700757static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100758
759/*
760 * Configure the response type and send the cmd.
761 */
762static void
Denis Karpov70a33412009-09-22 16:44:59 -0700763omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100764 struct mmc_data *data)
765{
766 int cmdreg = 0, resptype = 0, cmdtype = 0;
767
Venkatraman S8986d312012-08-07 19:10:38 +0530768 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100769 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
770 host->cmd = cmd;
771
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700772 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100773
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200774 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100775 if (cmd->flags & MMC_RSP_PRESENT) {
776 if (cmd->flags & MMC_RSP_136)
777 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200778 else if (cmd->flags & MMC_RSP_BUSY) {
779 resptype = 3;
780 host->response_busy = 1;
781 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100782 resptype = 2;
783 }
784
785 /*
786 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
787 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
788 * a val of 0x3, rest 0x0.
789 */
790 if (cmd == host->mrq->stop)
791 cmdtype = 0x3;
792
793 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
794
795 if (data) {
796 cmdreg |= DP_SELECT | MSBS | BCE;
797 if (data->flags & MMC_DATA_READ)
798 cmdreg |= DDIR;
799 else
800 cmdreg &= ~(DDIR);
801 }
802
803 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530804 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100805
Adrian Hunterb4175772010-05-26 14:42:06 -0700806 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700807
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100808 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
809 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
810}
811
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200812static int
Denis Karpov70a33412009-09-22 16:44:59 -0700813omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200814{
815 if (data->flags & MMC_DATA_WRITE)
816 return DMA_TO_DEVICE;
817 else
818 return DMA_FROM_DEVICE;
819}
820
Russell Kingc5c98922012-04-13 12:14:39 +0100821static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
822 struct mmc_data *data)
823{
824 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
825}
826
Adrian Hunterb4175772010-05-26 14:42:06 -0700827static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
828{
829 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530830 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700831
Venkatraman S31463b12012-04-09 12:08:34 +0530832 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700833 host->req_in_progress = 0;
834 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530835 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700836
837 omap_hsmmc_disable_irq(host);
838 /* Do not complete the request if DMA is still in progress */
839 if (mrq->data && host->use_dma && dma_ch != -1)
840 return;
841 host->mrq = NULL;
842 mmc_request_done(host->mmc, mrq);
843}
844
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100845/*
846 * Notify the transfer complete to MMC core
847 */
848static void
Denis Karpov70a33412009-09-22 16:44:59 -0700849omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100850{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200851 if (!data) {
852 struct mmc_request *mrq = host->mrq;
853
Adrian Hunter23050102009-09-22 16:44:57 -0700854 /* TC before CC from CMD6 - don't know why, but it happens */
855 if (host->cmd && host->cmd->opcode == 6 &&
856 host->response_busy) {
857 host->response_busy = 0;
858 return;
859 }
860
Adrian Hunterb4175772010-05-26 14:42:06 -0700861 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200862 return;
863 }
864
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100865 host->data = NULL;
866
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100867 if (!data->error)
868 data->bytes_xfered += data->blocks * (data->blksz);
869 else
870 data->bytes_xfered = 0;
871
Ming Leife852272012-06-22 18:49:35 +0800872 if (!data->stop) {
Adrian Hunterb4175772010-05-26 14:42:06 -0700873 omap_hsmmc_request_done(host, data->mrq);
Ming Leife852272012-06-22 18:49:35 +0800874 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100875 }
Ming Leife852272012-06-22 18:49:35 +0800876 omap_hsmmc_start_command(host, data->stop, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100877}
878
879/*
880 * Notify the core about command completion
881 */
882static void
Denis Karpov70a33412009-09-22 16:44:59 -0700883omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100884{
885 host->cmd = NULL;
886
887 if (cmd->flags & MMC_RSP_PRESENT) {
888 if (cmd->flags & MMC_RSP_136) {
889 /* response type 2 */
890 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
891 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
892 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
893 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
894 } else {
895 /* response types 1, 1b, 3, 4, 5, 6 */
896 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
897 }
898 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700899 if ((host->data == NULL && !host->response_busy) || cmd->error)
900 omap_hsmmc_request_done(host, cmd->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100901}
902
903/*
904 * DMA clean up for command errors
905 */
Denis Karpov70a33412009-09-22 16:44:59 -0700906static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100907{
Adrian Hunterb4175772010-05-26 14:42:06 -0700908 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530909 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700910
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200911 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100912
Venkatraman S31463b12012-04-09 12:08:34 +0530913 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700914 dma_ch = host->dma_ch;
915 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530916 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700917
918 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100919 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
920
921 dmaengine_terminate_all(chan);
922 dma_unmap_sg(chan->device->dev,
923 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700924 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100925
Per Forlin053bf342011-11-07 21:55:11 +0530926 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100927 }
928 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100929}
930
931/*
932 * Readable error output
933 */
934#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300935static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100936{
937 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700938 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300939 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
940 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
941 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
942 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100943 };
944 char res[256];
945 char *buf = res;
946 int len, i;
947
948 len = sprintf(buf, "MMC IRQ 0x%x :", status);
949 buf += len;
950
Denis Karpov70a33412009-09-22 16:44:59 -0700951 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100952 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700953 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100954 buf += len;
955 }
956
Venkatraman S8986d312012-08-07 19:10:38 +0530957 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100958}
Adrian Hunter699b9582011-05-06 12:14:01 +0300959#else
960static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
961 u32 status)
962{
963}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100964#endif /* CONFIG_MMC_DEBUG */
965
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100966/*
967 * MMC controller internal state machines reset
968 *
969 * Used to reset command or data internal state machines, using respectively
970 * SRC or SRD bit of SYSCTL register
971 * Can be called from interrupt context
972 */
Denis Karpov70a33412009-09-22 16:44:59 -0700973static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
974 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100975{
976 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +0530977 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100978
979 OMAP_HSMMC_WRITE(host->base, SYSCTL,
980 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
981
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700982 /*
983 * OMAP4 ES2 and greater has an updated reset logic.
984 * Monitor a 0->1 transition first
985 */
986 if (mmc_slot(host).features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -0500987 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700988 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +0530989 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -0700990 }
991 i = 0;
992
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100993 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
994 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +0530995 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100996
997 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
998 dev_err(mmc_dev(host->mmc),
999 "Timeout waiting on controller reset in %s\n",
1000 __func__);
1001}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001002
Balaji T K25e18972012-11-19 21:59:55 +05301003static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1004 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301005{
Balaji T K25e18972012-11-19 21:59:55 +05301006 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301007 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301008 if (host->cmd)
1009 host->cmd->error = err;
1010 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301011
1012 if (host->data) {
1013 omap_hsmmc_reset_controller_fsm(host, SRD);
1014 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301015 } else if (host->mrq && host->mrq->cmd)
1016 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301017}
1018
Adrian Hunterb4175772010-05-26 14:42:06 -07001019static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001020{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001021 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001022 int end_cmd = 0, end_trans = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001023
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001024 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301025 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001026
Venkatraman Sa7e96872012-11-19 22:00:01 +05301027 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001028 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001029
Venkatraman Sa7e96872012-11-19 22:00:01 +05301030 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301031 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301032 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301033 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301034 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301035 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1036
Venkatraman Sae4bf782012-08-09 20:36:07 +05301037 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301038 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301039 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001040 }
1041 }
1042
Francesco Lavra7472bab2013-06-29 08:25:12 +02001043 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301044 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001045 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301046 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001047 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001048}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001049
Adrian Hunterb4175772010-05-26 14:42:06 -07001050/*
1051 * MMC controller IRQ handler
1052 */
1053static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1054{
1055 struct omap_hsmmc_host *host = dev_id;
1056 int status;
1057
1058 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301059 while (status & INT_EN_MASK && host->req_in_progress) {
Adrian Hunterb4175772010-05-26 14:42:06 -07001060 omap_hsmmc_do_irq(host, status);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301061
Adrian Hunterb4175772010-05-26 14:42:06 -07001062 /* Flush posted write */
1063 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301064 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001065
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001066 return IRQ_HANDLED;
1067}
1068
Denis Karpov70a33412009-09-22 16:44:59 -07001069static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001070{
1071 unsigned long i;
1072
1073 OMAP_HSMMC_WRITE(host->base, HCTL,
1074 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1075 for (i = 0; i < loops_per_jiffy; i++) {
1076 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1077 break;
1078 cpu_relax();
1079 }
1080}
1081
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001082/*
David Brownelleb250822009-02-17 14:49:01 -08001083 * Switch MMC interface voltage ... only relevant for MMC1.
1084 *
1085 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1086 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1087 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001088 */
Denis Karpov70a33412009-09-22 16:44:59 -07001089static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001090{
1091 u32 reg_val = 0;
1092 int ret;
1093
1094 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301095 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301096 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301097 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001098
1099 /* Turn the power off */
1100 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001101
1102 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001103 if (!ret)
1104 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1,
1105 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301106 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301107 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301108 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001109
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001110 if (ret != 0)
1111 goto err;
1112
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001113 OMAP_HSMMC_WRITE(host->base, HCTL,
1114 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1115 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001116
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001117 /*
1118 * If a MMC dual voltage card is detected, the set_ios fn calls
1119 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001120 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001121 *
David Brownelleb250822009-02-17 14:49:01 -08001122 * Cope with a bit of slop in the range ... per data sheets:
1123 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1124 * but recommended values are 1.71V to 1.89V
1125 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1126 * but recommended values are 2.7V to 3.3V
1127 *
1128 * Board setup code shouldn't permit anything very out-of-range.
1129 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1130 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001131 */
David Brownelleb250822009-02-17 14:49:01 -08001132 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001133 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001134 else
1135 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001136
1137 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001138 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001139
1140 return 0;
1141err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301142 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001143 return ret;
1144}
1145
Adrian Hunterb62f6222009-09-22 16:45:01 -07001146/* Protect the card while the cover is open */
1147static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1148{
1149 if (!mmc_slot(host).get_cover_state)
1150 return;
1151
1152 host->reqs_blocked = 0;
1153 if (mmc_slot(host).get_cover_state(host->dev, host->slot_id)) {
1154 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301155 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001156 "card is now accessible\n",
1157 mmc_hostname(host->mmc));
1158 host->protect_card = 0;
1159 }
1160 } else {
1161 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301162 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001163 "card is now inaccessible\n",
1164 mmc_hostname(host->mmc));
1165 host->protect_card = 1;
1166 }
1167 }
1168}
1169
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001170/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001171 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001172 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001173static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001174{
NeilBrown7efab4f2011-12-30 12:35:13 +11001175 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001176 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001177 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001178
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001179 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001180
Denis Karpov191d1f12009-09-22 16:44:55 -07001181 if (slot->card_detect)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001182 carddetect = slot->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001183 else {
1184 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001185 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001186 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001187
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001188 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001189 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001190 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001191 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001192 return IRQ_HANDLED;
1193}
1194
Russell Kingc5c98922012-04-13 12:14:39 +01001195static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001196{
Russell Kingc5c98922012-04-13 12:14:39 +01001197 struct omap_hsmmc_host *host = param;
1198 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001199 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001200 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001201
Russell Kingc5c98922012-04-13 12:14:39 +01001202 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001203 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001204 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001205 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001206 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001207
Adrian Hunter770d7432011-05-06 12:14:11 +03001208 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001209 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001210 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001211 dma_unmap_sg(chan->device->dev,
1212 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001213 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001214
1215 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001216 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001217 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001218
1219 /* If DMA has finished after TC, complete the request */
1220 if (!req_in_progress) {
1221 struct mmc_request *mrq = host->mrq;
1222
1223 host->mrq = NULL;
1224 mmc_request_done(host->mmc, mrq);
1225 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001226}
1227
Per Forlin9782aff2011-07-01 18:55:23 +02001228static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1229 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001230 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001231 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001232{
1233 int dma_len;
1234
1235 if (!next && data->host_cookie &&
1236 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301237 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001238 " host->next_data.cookie %d\n",
1239 __func__, data->host_cookie, host->next_data.cookie);
1240 data->host_cookie = 0;
1241 }
1242
1243 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001244 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001245 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001246 omap_hsmmc_get_dma_dir(host, data));
1247
1248 } else {
1249 dma_len = host->next_data.dma_len;
1250 host->next_data.dma_len = 0;
1251 }
1252
1253
1254 if (dma_len == 0)
1255 return -EINVAL;
1256
1257 if (next) {
1258 next->dma_len = dma_len;
1259 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1260 } else
1261 host->dma_len = dma_len;
1262
1263 return 0;
1264}
1265
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001266/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001267 * Routine to configure and start DMA for the MMC card
1268 */
Denis Karpov70a33412009-09-22 16:44:59 -07001269static int omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host,
1270 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001271{
Russell King26b88522012-04-13 12:27:37 +01001272 struct dma_slave_config cfg;
1273 struct dma_async_tx_descriptor *tx;
1274 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001275 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001276 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001277
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001278 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001279 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001280 struct scatterlist *sgl;
1281
1282 sgl = data->sg + i;
1283 if (sgl->length % data->blksz)
1284 return -EINVAL;
1285 }
1286 if ((data->blksz % 4) != 0)
1287 /* REVISIT: The MMC buffer increments only when MSB is written.
1288 * Return error for blksz which is non multiple of four.
1289 */
1290 return -EINVAL;
1291
Adrian Hunterb4175772010-05-26 14:42:06 -07001292 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001293
Russell Kingc5c98922012-04-13 12:14:39 +01001294 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001295
Russell King26b88522012-04-13 12:27:37 +01001296 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1297 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1298 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1299 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1300 cfg.src_maxburst = data->blksz / 4;
1301 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001302
Russell King26b88522012-04-13 12:27:37 +01001303 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001304 if (ret)
1305 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001306
Russell King26b88522012-04-13 12:27:37 +01001307 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1308 if (ret)
1309 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001310
Russell King26b88522012-04-13 12:27:37 +01001311 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1312 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1313 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1314 if (!tx) {
1315 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1316 /* FIXME: cleanup */
1317 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001318 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001319
Russell King26b88522012-04-13 12:27:37 +01001320 tx->callback = omap_hsmmc_dma_callback;
1321 tx->callback_param = host;
1322
1323 /* Does not fail */
1324 dmaengine_submit(tx);
1325
1326 host->dma_ch = 1;
1327
1328 dma_async_issue_pending(chan);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001329
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001330 return 0;
1331}
1332
Denis Karpov70a33412009-09-22 16:44:59 -07001333static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001334 unsigned int timeout_ns,
1335 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001336{
1337 unsigned int timeout, cycle_ns;
1338 uint32_t reg, clkd, dto = 0;
1339
1340 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1341 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1342 if (clkd == 0)
1343 clkd = 1;
1344
1345 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001346 timeout = timeout_ns / cycle_ns;
1347 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001348 if (timeout) {
1349 while ((timeout & 0x80000000) == 0) {
1350 dto += 1;
1351 timeout <<= 1;
1352 }
1353 dto = 31 - dto;
1354 timeout <<= 1;
1355 if (timeout && dto)
1356 dto += 1;
1357 if (dto >= 13)
1358 dto -= 13;
1359 else
1360 dto = 0;
1361 if (dto > 14)
1362 dto = 14;
1363 }
1364
1365 reg &= ~DTO_MASK;
1366 reg |= dto << DTO_SHIFT;
1367 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1368}
1369
1370/*
1371 * Configure block length for MMC/SD cards and initiate the transfer.
1372 */
1373static int
Denis Karpov70a33412009-09-22 16:44:59 -07001374omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001375{
1376 int ret;
1377 host->data = req->data;
1378
1379 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001380 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001381 /*
1382 * Set an arbitrary 100ms data timeout for commands with
1383 * busy signal.
1384 */
1385 if (req->cmd->flags & MMC_RSP_BUSY)
1386 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001387 return 0;
1388 }
1389
1390 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1391 | (req->data->blocks << 16));
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001392 set_data_timeout(host, req->data->timeout_ns, req->data->timeout_clks);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001393
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001394 if (host->use_dma) {
Denis Karpov70a33412009-09-22 16:44:59 -07001395 ret = omap_hsmmc_start_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001396 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301397 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001398 return ret;
1399 }
1400 }
1401 return 0;
1402}
1403
Per Forlin9782aff2011-07-01 18:55:23 +02001404static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1405 int err)
1406{
1407 struct omap_hsmmc_host *host = mmc_priv(mmc);
1408 struct mmc_data *data = mrq->data;
1409
Russell King26b88522012-04-13 12:27:37 +01001410 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001411 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001412
Russell King26b88522012-04-13 12:27:37 +01001413 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1414 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001415 data->host_cookie = 0;
1416 }
1417}
1418
1419static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1420 bool is_first_req)
1421{
1422 struct omap_hsmmc_host *host = mmc_priv(mmc);
1423
1424 if (mrq->data->host_cookie) {
1425 mrq->data->host_cookie = 0;
1426 return ;
1427 }
1428
Russell Kingc5c98922012-04-13 12:14:39 +01001429 if (host->use_dma) {
1430 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001431
Per Forlin9782aff2011-07-01 18:55:23 +02001432 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001433 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001434 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001435 }
Per Forlin9782aff2011-07-01 18:55:23 +02001436}
1437
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001438/*
1439 * Request function. for read/write operation
1440 */
Denis Karpov70a33412009-09-22 16:44:59 -07001441static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001442{
Denis Karpov70a33412009-09-22 16:44:59 -07001443 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001444 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001445
Adrian Hunterb4175772010-05-26 14:42:06 -07001446 BUG_ON(host->req_in_progress);
1447 BUG_ON(host->dma_ch != -1);
1448 if (host->protect_card) {
1449 if (host->reqs_blocked < 3) {
1450 /*
1451 * Ensure the controller is left in a consistent
1452 * state by resetting the command and data state
1453 * machines.
1454 */
1455 omap_hsmmc_reset_controller_fsm(host, SRD);
1456 omap_hsmmc_reset_controller_fsm(host, SRC);
1457 host->reqs_blocked += 1;
1458 }
1459 req->cmd->error = -EBADF;
1460 if (req->data)
1461 req->data->error = -EBADF;
1462 req->cmd->retries = 0;
1463 mmc_request_done(mmc, req);
1464 return;
1465 } else if (host->reqs_blocked)
1466 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001467 WARN_ON(host->mrq != NULL);
1468 host->mrq = req;
Denis Karpov70a33412009-09-22 16:44:59 -07001469 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001470 if (err) {
1471 req->cmd->error = err;
1472 if (req->data)
1473 req->data->error = err;
1474 host->mrq = NULL;
1475 mmc_request_done(mmc, req);
1476 return;
1477 }
1478
Denis Karpov70a33412009-09-22 16:44:59 -07001479 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001480}
1481
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001482/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001483static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001484{
Denis Karpov70a33412009-09-22 16:44:59 -07001485 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001486 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001487
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301488 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001489
Adrian Huntera3621462009-09-22 16:44:42 -07001490 if (ios->power_mode != host->power_mode) {
1491 switch (ios->power_mode) {
1492 case MMC_POWER_OFF:
1493 mmc_slot(host).set_power(host->dev, host->slot_id,
1494 0, 0);
1495 break;
1496 case MMC_POWER_UP:
1497 mmc_slot(host).set_power(host->dev, host->slot_id,
1498 1, ios->vdd);
1499 break;
1500 case MMC_POWER_ON:
1501 do_send_init_stream = 1;
1502 break;
1503 }
1504 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001505 }
1506
Denis Karpovdd498ef2009-09-22 16:44:49 -07001507 /* FIXME: set registers based only on changes to ios */
1508
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001509 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001510
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301511 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001512 /* Only MMC1 can interface at 3V without some flavor
1513 * of external transceiver; but they all handle 1.8V.
1514 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001515 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Rajendra Nayak1f84b712012-03-12 20:32:38 +05301516 (ios->vdd == DUAL_VOLT_OCR_BIT) &&
1517 /*
1518 * With pbias cell programming missing, this
Tony Lindgrencf5ae402013-05-10 17:42:33 +05301519 * can't be allowed on MMC1 when booting with device
Rajendra Nayak1f84b712012-03-12 20:32:38 +05301520 * tree.
1521 */
Tony Lindgrencf5ae402013-05-10 17:42:33 +05301522 !host->pbias_disable) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001523 /*
1524 * The mmc_select_voltage fn of the core does
1525 * not seem to set the power_mode to
1526 * MMC_POWER_UP upon recalculating the voltage.
1527 * vdd 1.8v.
1528 */
Denis Karpov70a33412009-09-22 16:44:59 -07001529 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1530 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001531 "Switch operation failed\n");
1532 }
1533 }
1534
Andy Shevchenko5934df22011-05-06 12:14:06 +03001535 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001536
Adrian Huntera3621462009-09-22 16:44:42 -07001537 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001538 send_init_stream(host);
1539
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001540 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001541
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301542 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001543}
1544
1545static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1546{
Denis Karpov70a33412009-09-22 16:44:59 -07001547 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001548
Denis Karpov191d1f12009-09-22 16:44:55 -07001549 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001550 return -ENOSYS;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001551 return mmc_slot(host).card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001552}
1553
1554static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1555{
Denis Karpov70a33412009-09-22 16:44:59 -07001556 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001557
Denis Karpov191d1f12009-09-22 16:44:55 -07001558 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001559 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001560 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001561}
1562
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001563static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1564{
1565 struct omap_hsmmc_host *host = mmc_priv(mmc);
1566
1567 if (mmc_slot(host).init_card)
1568 mmc_slot(host).init_card(card);
1569}
1570
Denis Karpov70a33412009-09-22 16:44:59 -07001571static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001572{
1573 u32 hctl, capa, value;
1574
1575 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301576 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001577 hctl = SDVS30;
1578 capa = VS30 | VS18;
1579 } else {
1580 hctl = SDVS18;
1581 capa = VS18;
1582 }
1583
1584 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1585 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1586
1587 value = OMAP_HSMMC_READ(host->base, CAPA);
1588 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1589
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001590 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001591 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001592}
1593
Denis Karpov70a33412009-09-22 16:44:59 -07001594static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001595{
Denis Karpov70a33412009-09-22 16:44:59 -07001596 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001597
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301598 pm_runtime_get_sync(host->dev);
1599
Denis Karpovdd498ef2009-09-22 16:44:49 -07001600 return 0;
1601}
1602
Adrian Hunter907d2e72012-02-29 09:17:21 +02001603static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001604{
Denis Karpov70a33412009-09-22 16:44:59 -07001605 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001606
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301607 pm_runtime_mark_last_busy(host->dev);
1608 pm_runtime_put_autosuspend(host->dev);
1609
Denis Karpovdd498ef2009-09-22 16:44:49 -07001610 return 0;
1611}
1612
Denis Karpov70a33412009-09-22 16:44:59 -07001613static const struct mmc_host_ops omap_hsmmc_ops = {
1614 .enable = omap_hsmmc_enable_fclk,
1615 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001616 .post_req = omap_hsmmc_post_req,
1617 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001618 .request = omap_hsmmc_request,
1619 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001620 .get_cd = omap_hsmmc_get_cd,
1621 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001622 .init_card = omap_hsmmc_init_card,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001623 /* NYET -- enable_sdio_irq */
1624};
1625
Denis Karpovd900f712009-09-22 16:44:38 -07001626#ifdef CONFIG_DEBUG_FS
1627
Denis Karpov70a33412009-09-22 16:44:59 -07001628static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001629{
1630 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001631 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001632
Tony Lindgren0a82e062013-10-21 00:25:19 +05301633 seq_printf(s, "mmc%d:\n ctx_loss:\t%d\n\nregs:\n",
1634 mmc->index, host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001635
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301636 pm_runtime_get_sync(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001637
Denis Karpovd900f712009-09-22 16:44:38 -07001638 seq_printf(s, "CON:\t\t0x%08x\n",
1639 OMAP_HSMMC_READ(host->base, CON));
1640 seq_printf(s, "HCTL:\t\t0x%08x\n",
1641 OMAP_HSMMC_READ(host->base, HCTL));
1642 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1643 OMAP_HSMMC_READ(host->base, SYSCTL));
1644 seq_printf(s, "IE:\t\t0x%08x\n",
1645 OMAP_HSMMC_READ(host->base, IE));
1646 seq_printf(s, "ISE:\t\t0x%08x\n",
1647 OMAP_HSMMC_READ(host->base, ISE));
1648 seq_printf(s, "CAPA:\t\t0x%08x\n",
1649 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001650
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301651 pm_runtime_mark_last_busy(host->dev);
1652 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001653
Denis Karpovd900f712009-09-22 16:44:38 -07001654 return 0;
1655}
1656
Denis Karpov70a33412009-09-22 16:44:59 -07001657static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001658{
Denis Karpov70a33412009-09-22 16:44:59 -07001659 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001660}
1661
1662static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001663 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001664 .read = seq_read,
1665 .llseek = seq_lseek,
1666 .release = single_release,
1667};
1668
Denis Karpov70a33412009-09-22 16:44:59 -07001669static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001670{
1671 if (mmc->debugfs_root)
1672 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1673 mmc, &mmc_regs_fops);
1674}
1675
1676#else
1677
Denis Karpov70a33412009-09-22 16:44:59 -07001678static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001679{
1680}
1681
1682#endif
1683
Rajendra Nayak46856a62012-03-12 20:32:37 +05301684#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001685static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1686 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1687 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1688};
1689
1690static const struct omap_mmc_of_data omap4_mmc_of_data = {
1691 .reg_offset = 0x100,
1692};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301693
1694static const struct of_device_id omap_mmc_of_match[] = {
1695 {
1696 .compatible = "ti,omap2-hsmmc",
1697 },
1698 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001699 .compatible = "ti,omap3-pre-es3-hsmmc",
1700 .data = &omap3_pre_es3_mmc_of_data,
1701 },
1702 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301703 .compatible = "ti,omap3-hsmmc",
1704 },
1705 {
1706 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001707 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301708 },
1709 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001710};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301711MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1712
1713static struct omap_mmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
1714{
1715 struct omap_mmc_platform_data *pdata;
1716 struct device_node *np = dev->of_node;
Daniel Mackd8714e82012-10-15 21:35:06 +05301717 u32 bus_width, max_freq;
Jan Luebbedc642c22013-01-30 10:07:17 +01001718 int cd_gpio, wp_gpio;
1719
1720 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
1721 wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1722 if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
1723 return ERR_PTR(-EPROBE_DEFER);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301724
1725 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1726 if (!pdata)
1727 return NULL; /* out of memory */
1728
1729 if (of_find_property(np, "ti,dual-volt", NULL))
1730 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1731
1732 /* This driver only supports 1 slot */
1733 pdata->nr_slots = 1;
Jan Luebbedc642c22013-01-30 10:07:17 +01001734 pdata->slots[0].switch_pin = cd_gpio;
1735 pdata->slots[0].gpio_wp = wp_gpio;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301736
1737 if (of_find_property(np, "ti,non-removable", NULL)) {
1738 pdata->slots[0].nonremovable = true;
1739 pdata->slots[0].no_regulator_off_init = true;
1740 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001741 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301742 if (bus_width == 4)
1743 pdata->slots[0].caps |= MMC_CAP_4_BIT_DATA;
1744 else if (bus_width == 8)
1745 pdata->slots[0].caps |= MMC_CAP_8_BIT_DATA;
1746
1747 if (of_find_property(np, "ti,needs-special-reset", NULL))
1748 pdata->slots[0].features |= HSMMC_HAS_UPDATED_RESET;
1749
Daniel Mackd8714e82012-10-15 21:35:06 +05301750 if (!of_property_read_u32(np, "max-frequency", &max_freq))
1751 pdata->max_freq = max_freq;
1752
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301753 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
1754 pdata->slots[0].features |= HSMMC_HAS_HSPE_SUPPORT;
1755
Rajendra Nayak46856a62012-03-12 20:32:37 +05301756 return pdata;
1757}
1758#else
1759static inline struct omap_mmc_platform_data
1760 *of_get_hsmmc_pdata(struct device *dev)
1761{
1762 return NULL;
1763}
1764#endif
1765
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001766static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001767{
1768 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1769 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001770 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001771 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001772 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301773 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001774 dma_cap_mask_t mask;
1775 unsigned tx_req, rx_req;
Daniel Mack46b76032012-10-15 21:35:05 +05301776 struct pinctrl *pinctrl;
Nishanth Menon59445b12014-02-13 23:45:48 -06001777 const struct omap_mmc_of_data *data;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301778
1779 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1780 if (match) {
1781 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01001782
1783 if (IS_ERR(pdata))
1784 return PTR_ERR(pdata);
1785
Rajendra Nayak46856a62012-03-12 20:32:37 +05301786 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06001787 data = match->data;
1788 pdata->reg_offset = data->reg_offset;
1789 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301790 }
1791 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001792
1793 if (pdata == NULL) {
1794 dev_err(&pdev->dev, "Platform Data is missing\n");
1795 return -ENXIO;
1796 }
1797
1798 if (pdata->nr_slots == 0) {
1799 dev_err(&pdev->dev, "No Slots\n");
1800 return -ENXIO;
1801 }
1802
1803 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1804 irq = platform_get_irq(pdev, 0);
1805 if (res == NULL || irq < 0)
1806 return -ENXIO;
1807
Chris Ball984b2032011-03-22 16:34:42 -07001808 res = request_mem_region(res->start, resource_size(res), pdev->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001809 if (res == NULL)
1810 return -EBUSY;
1811
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001812 ret = omap_hsmmc_gpio_init(pdata);
1813 if (ret)
1814 goto err;
1815
Denis Karpov70a33412009-09-22 16:44:59 -07001816 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001817 if (!mmc) {
1818 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001819 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001820 }
1821
1822 host = mmc_priv(mmc);
1823 host->mmc = mmc;
1824 host->pdata = pdata;
1825 host->dev = &pdev->dev;
1826 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001827 host->dma_ch = -1;
1828 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001829 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05301830 host->mapbase = res->start + pdata->reg_offset;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001831 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Hunter6da20c82010-02-15 10:03:34 -08001832 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001833 host->next_data.cookie = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001834
1835 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001836
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301837 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001838
Daniel Mackd418ed82012-02-19 13:20:33 +01001839 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1840
1841 if (pdata->max_freq > 0)
1842 mmc->f_max = pdata->max_freq;
1843 else
1844 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001845
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001846 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001847
Russell King6f7607c2009-01-28 10:22:50 +00001848 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001849 if (IS_ERR(host->fclk)) {
1850 ret = PTR_ERR(host->fclk);
1851 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001852 goto err1;
1853 }
1854
Paul Walmsley9b682562011-10-06 14:50:35 -06001855 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1856 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
1857 mmc->caps2 |= MMC_CAP2_NO_MULTI_READ;
1858 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001859
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301860 pm_runtime_enable(host->dev);
1861 pm_runtime_get_sync(host->dev);
1862 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1863 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001864
Balaji T K92a3aeb2012-02-24 21:14:34 +05301865 omap_hsmmc_context_save(host);
1866
Tony Lindgrencf5ae402013-05-10 17:42:33 +05301867 /* This can be removed once we support PBIAS with DT */
Balaji T Ke0022642013-10-21 00:25:18 +05301868 if (host->dev->of_node && res->start == 0x4809c000)
Tony Lindgrencf5ae402013-05-10 17:42:33 +05301869 host->pbias_disable = 1;
1870
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301871 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1872 /*
1873 * MMC can still work without debounce clock.
1874 */
1875 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301876 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05301877 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301878 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
1879 clk_put(host->dbclk);
1880 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07001881 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001882
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001883 /* Since we do only SG emulation, we can have as many segs
1884 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001885 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001886
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001887 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1888 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1889 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1890 mmc->max_seg_size = mmc->max_req_size;
1891
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001892 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07001893 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001894
Sukumar Ghorai3a638332010-09-15 14:49:23 +00001895 mmc->caps |= mmc_slot(host).caps;
1896 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001897 mmc->caps |= MMC_CAP_4_BIT_DATA;
1898
Denis Karpov191d1f12009-09-22 16:44:55 -07001899 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001900 mmc->caps |= MMC_CAP_NONREMOVABLE;
1901
Eliad Peller6fdc75d2011-11-22 16:02:18 +02001902 mmc->pm_caps = mmc_slot(host).pm_caps;
1903
Denis Karpov70a33412009-09-22 16:44:59 -07001904 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001905
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05301906 if (!pdev->dev.of_node) {
1907 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1908 if (!res) {
1909 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
1910 ret = -ENXIO;
1911 goto err_irq;
1912 }
1913 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001914
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05301915 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1916 if (!res) {
1917 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
1918 ret = -ENXIO;
1919 goto err_irq;
1920 }
1921 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05001922 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001923
Russell King26b88522012-04-13 12:27:37 +01001924 dma_cap_zero(mask);
1925 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01001926
Matt Porterd272fbf2013-05-10 17:42:34 +05301927 host->rx_chan =
1928 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
1929 &rx_req, &pdev->dev, "rx");
1930
Russell King26b88522012-04-13 12:27:37 +01001931 if (!host->rx_chan) {
1932 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001933 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001934 goto err_irq;
1935 }
1936
Matt Porterd272fbf2013-05-10 17:42:34 +05301937 host->tx_chan =
1938 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
1939 &tx_req, &pdev->dev, "tx");
1940
Russell King26b88522012-04-13 12:27:37 +01001941 if (!host->tx_chan) {
1942 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01001943 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01001944 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01001945 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001946
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001947 /* Request IRQ for MMC operations */
Yong Zhangd9618e92011-09-22 16:59:04 +08001948 ret = request_irq(host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001949 mmc_hostname(mmc), host);
1950 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301951 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001952 goto err_irq;
1953 }
1954
1955 if (pdata->init != NULL) {
1956 if (pdata->init(&pdev->dev) != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301957 dev_err(mmc_dev(host->mmc),
Denis Karpov70a33412009-09-22 16:44:59 -07001958 "Unable to configure MMC IRQs\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001959 goto err_irq_cd_init;
1960 }
1961 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001962
Adrian Hunterb702b102010-02-15 10:03:35 -08001963 if (omap_hsmmc_have_reg() && !mmc_slot(host).set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001964 ret = omap_hsmmc_reg_get(host);
1965 if (ret)
1966 goto err_reg;
1967 host->use_reg = 1;
1968 }
1969
David Brownellb583f262009-05-28 14:04:03 -07001970 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001971
1972 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001973 if ((mmc_slot(host).card_detect_irq)) {
NeilBrown7efab4f2011-12-30 12:35:13 +11001974 ret = request_threaded_irq(mmc_slot(host).card_detect_irq,
1975 NULL,
1976 omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08001977 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11001978 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001979 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301980 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001981 "Unable to grab MMC CD IRQ\n");
1982 goto err_irq_cd;
1983 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00001984 pdata->suspend = omap_hsmmc_suspend_cdirq;
1985 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001986 }
1987
Adrian Hunterb4175772010-05-26 14:42:06 -07001988 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001989
Daniel Mack46b76032012-10-15 21:35:05 +05301990 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1991 if (IS_ERR(pinctrl))
1992 dev_warn(&pdev->dev,
1993 "pins are not configured from the driver\n");
1994
Adrian Hunterb62f6222009-09-22 16:45:01 -07001995 omap_hsmmc_protect_card(host);
1996
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001997 mmc_add_host(mmc);
1998
Denis Karpov191d1f12009-09-22 16:44:55 -07001999 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002000 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2001 if (ret < 0)
2002 goto err_slot_name;
2003 }
Denis Karpov191d1f12009-09-22 16:44:55 -07002004 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002005 ret = device_create_file(&mmc->class_dev,
2006 &dev_attr_cover_switch);
2007 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002008 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002009 }
2010
Denis Karpov70a33412009-09-22 16:44:59 -07002011 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302012 pm_runtime_mark_last_busy(host->dev);
2013 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002015 return 0;
2016
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002017err_slot_name:
2018 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002019 free_irq(mmc_slot(host).card_detect_irq, host);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002020err_irq_cd:
2021 if (host->use_reg)
2022 omap_hsmmc_reg_put(host);
2023err_reg:
2024 if (host->pdata->cleanup)
2025 host->pdata->cleanup(&pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002026err_irq_cd_init:
2027 free_irq(host->irq, host);
2028err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002029 if (host->tx_chan)
2030 dma_release_channel(host->tx_chan);
2031 if (host->rx_chan)
2032 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302033 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002034 pm_runtime_disable(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002035 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302036 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302037 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002038 clk_put(host->dbclk);
2039 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002040err1:
2041 iounmap(host->base);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002042 mmc_free_host(mmc);
2043err_alloc:
2044 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002045err:
Russell King48b332f2012-04-18 11:11:57 +01002046 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2047 if (res)
2048 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002049 return ret;
2050}
2051
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002052static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002053{
Denis Karpov70a33412009-09-22 16:44:59 -07002054 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002055 struct resource *res;
2056
Felipe Balbi927ce942012-03-14 11:18:27 +02002057 pm_runtime_get_sync(host->dev);
2058 mmc_remove_host(host->mmc);
2059 if (host->use_reg)
2060 omap_hsmmc_reg_put(host);
2061 if (host->pdata->cleanup)
2062 host->pdata->cleanup(&pdev->dev);
2063 free_irq(host->irq, host);
2064 if (mmc_slot(host).card_detect_irq)
2065 free_irq(mmc_slot(host).card_detect_irq, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002066
Russell Kingc5c98922012-04-13 12:14:39 +01002067 if (host->tx_chan)
2068 dma_release_channel(host->tx_chan);
2069 if (host->rx_chan)
2070 dma_release_channel(host->rx_chan);
2071
Felipe Balbi927ce942012-03-14 11:18:27 +02002072 pm_runtime_put_sync(host->dev);
2073 pm_runtime_disable(host->dev);
2074 clk_put(host->fclk);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302075 if (host->dbclk) {
Rajendra Nayak94c18142012-06-27 14:19:54 +05302076 clk_disable_unprepare(host->dbclk);
Felipe Balbi927ce942012-03-14 11:18:27 +02002077 clk_put(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002078 }
2079
Balaji T K9ea28ec2012-10-15 21:35:08 +05302080 omap_hsmmc_gpio_free(host->pdata);
Felipe Balbi927ce942012-03-14 11:18:27 +02002081 iounmap(host->base);
Balaji T K9d1f0282012-10-15 21:35:07 +05302082 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002083
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002084 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2085 if (res)
Chris Ball984b2032011-03-22 16:34:42 -07002086 release_mem_region(res->start, resource_size(res));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002087
2088 return 0;
2089}
2090
2091#ifdef CONFIG_PM
Felipe Balbia48ce882012-11-19 21:59:59 +05302092static int omap_hsmmc_prepare(struct device *dev)
2093{
2094 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2095
2096 if (host->pdata->suspend)
2097 return host->pdata->suspend(dev, host->slot_id);
2098
2099 return 0;
2100}
2101
2102static void omap_hsmmc_complete(struct device *dev)
2103{
2104 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2105
2106 if (host->pdata->resume)
2107 host->pdata->resume(dev, host->slot_id);
2108
2109}
2110
Kevin Hilmana791daa2010-05-26 14:42:07 -07002111static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002112{
Felipe Balbi927ce942012-03-14 11:18:27 +02002113 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2114
2115 if (!host)
2116 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002117
Felipe Balbi927ce942012-03-14 11:18:27 +02002118 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002119
2120 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
2121 omap_hsmmc_disable_irq(host);
2122 OMAP_HSMMC_WRITE(host->base, HCTL,
2123 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2124 }
2125
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302126 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302127 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002128
Eliad Peller31f9d462011-11-22 16:02:17 +02002129 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002130 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002131}
2132
2133/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002134static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002135{
Felipe Balbi927ce942012-03-14 11:18:27 +02002136 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2137
2138 if (!host)
2139 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002140
Felipe Balbi927ce942012-03-14 11:18:27 +02002141 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002142
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302143 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302144 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002145
Felipe Balbi927ce942012-03-14 11:18:27 +02002146 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2147 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002148
Felipe Balbi927ce942012-03-14 11:18:27 +02002149 omap_hsmmc_protect_card(host);
2150
Felipe Balbi927ce942012-03-14 11:18:27 +02002151 pm_runtime_mark_last_busy(host->dev);
2152 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002153 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002154}
2155
2156#else
Felipe Balbia48ce882012-11-19 21:59:59 +05302157#define omap_hsmmc_prepare NULL
2158#define omap_hsmmc_complete NULL
Denis Karpov70a33412009-09-22 16:44:59 -07002159#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302160#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002161#endif
2162
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302163static int omap_hsmmc_runtime_suspend(struct device *dev)
2164{
2165 struct omap_hsmmc_host *host;
2166
2167 host = platform_get_drvdata(to_platform_device(dev));
2168 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002169 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302170
2171 return 0;
2172}
2173
2174static int omap_hsmmc_runtime_resume(struct device *dev)
2175{
2176 struct omap_hsmmc_host *host;
2177
2178 host = platform_get_drvdata(to_platform_device(dev));
2179 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002180 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302181
2182 return 0;
2183}
2184
Kevin Hilmana791daa2010-05-26 14:42:07 -07002185static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002186 .suspend = omap_hsmmc_suspend,
2187 .resume = omap_hsmmc_resume,
Felipe Balbia48ce882012-11-19 21:59:59 +05302188 .prepare = omap_hsmmc_prepare,
2189 .complete = omap_hsmmc_complete,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302190 .runtime_suspend = omap_hsmmc_runtime_suspend,
2191 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002192};
2193
2194static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002195 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002196 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002197 .driver = {
2198 .name = DRIVER_NAME,
2199 .owner = THIS_MODULE,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002200 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302201 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002202 },
2203};
2204
Felipe Balbib7964502012-03-14 11:18:32 +02002205module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002206MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2207MODULE_LICENSE("GPL");
2208MODULE_ALIAS("platform:" DRIVER_NAME);
2209MODULE_AUTHOR("Texas Instruments Inc");